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 "tcg/startup.h"
27 #include "target_mman.h"
34 #include <sys/mount.h>
36 #include <sys/fsuid.h>
37 #include <sys/personality.h>
38 #include <sys/prctl.h>
39 #include <sys/resource.h>
41 #include <linux/capability.h>
43 #include <sys/timex.h>
44 #include <sys/socket.h>
45 #include <linux/sockios.h>
49 #include <sys/times.h>
52 #include <sys/statfs.h>
54 #include <sys/sysinfo.h>
55 #include <sys/signalfd.h>
56 //#include <sys/user.h>
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
59 #include <netinet/tcp.h>
60 #include <netinet/udp.h>
61 #include <linux/wireless.h>
62 #include <linux/icmp.h>
63 #include <linux/icmpv6.h>
64 #include <linux/if_tun.h>
65 #include <linux/in6.h>
66 #include <linux/errqueue.h>
67 #include <linux/random.h>
69 #include <sys/timerfd.h>
72 #include <sys/eventfd.h>
75 #include <sys/epoll.h>
78 #include "qemu/xattr.h"
80 #ifdef CONFIG_SENDFILE
81 #include <sys/sendfile.h>
83 #ifdef HAVE_SYS_KCOV_H
87 #define termios host_termios
88 #define winsize host_winsize
89 #define termio host_termio
90 #define sgttyb host_sgttyb /* same as target */
91 #define tchars host_tchars /* same as target */
92 #define ltchars host_ltchars /* same as target */
94 #include <linux/termios.h>
95 #include <linux/unistd.h>
96 #include <linux/cdrom.h>
97 #include <linux/hdreg.h>
98 #include <linux/soundcard.h>
100 #include <linux/mtio.h>
101 #include <linux/fs.h>
102 #include <linux/fd.h>
103 #if defined(CONFIG_FIEMAP)
104 #include <linux/fiemap.h>
106 #include <linux/fb.h>
107 #if defined(CONFIG_USBFS)
108 #include <linux/usbdevice_fs.h>
109 #include <linux/usb/ch9.h>
111 #include <linux/vt.h>
112 #include <linux/dm-ioctl.h>
113 #include <linux/reboot.h>
114 #include <linux/route.h>
115 #include <linux/filter.h>
116 #include <linux/blkpg.h>
117 #include <netpacket/packet.h>
118 #include <linux/netlink.h>
119 #include <linux/if_alg.h>
120 #include <linux/rtc.h>
121 #include <sound/asound.h>
123 #include <linux/btrfs.h>
126 #include <libdrm/drm.h>
127 #include <libdrm/i915_drm.h>
129 #include "linux_loop.h"
133 #include "user-internals.h"
135 #include "signal-common.h"
137 #include "user-mmap.h"
138 #include "user/safe-syscall.h"
139 #include "qemu/guest-random.h"
140 #include "qemu/selfmap.h"
141 #include "user/syscall-trace.h"
142 #include "special-errno.h"
143 #include "qapi/error.h"
144 #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
, unsigned int, fd
, struct linux_dirent
*, dirp
, unsigned int, 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
, unsigned int, fd
, struct linux_dirent64
*, dirp
, unsigned int, count
);
319 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
320 _syscall5(int, _llseek
, unsigned int, fd
, unsigned long, hi
, unsigned long, lo
,
321 loff_t
*, res
, unsigned int, 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
, },
460 _syscall2(int, sys_getcwd1
, char *, buf
, size_t, size
)
462 #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
463 #if defined(__NR_utimensat)
464 #define __NR_sys_utimensat __NR_utimensat
465 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
466 const struct timespec
*,tsp
,int,flags
)
468 static int sys_utimensat(int dirfd
, const char *pathname
,
469 const struct timespec times
[2], int flags
)
475 #endif /* TARGET_NR_utimensat */
477 #ifdef TARGET_NR_renameat2
478 #if defined(__NR_renameat2)
479 #define __NR_sys_renameat2 __NR_renameat2
480 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
481 const char *, new, unsigned int, flags
)
483 static int sys_renameat2(int oldfd
, const char *old
,
484 int newfd
, const char *new, int flags
)
487 return renameat(oldfd
, old
, newfd
, new);
493 #endif /* TARGET_NR_renameat2 */
495 #ifdef CONFIG_INOTIFY
496 #include <sys/inotify.h>
498 /* Userspace can usually survive runtime without inotify */
499 #undef TARGET_NR_inotify_init
500 #undef TARGET_NR_inotify_init1
501 #undef TARGET_NR_inotify_add_watch
502 #undef TARGET_NR_inotify_rm_watch
503 #endif /* CONFIG_INOTIFY */
505 #if defined(TARGET_NR_prlimit64)
506 #ifndef __NR_prlimit64
507 # define __NR_prlimit64 -1
509 #define __NR_sys_prlimit64 __NR_prlimit64
510 /* The glibc rlimit structure may not be that used by the underlying syscall */
511 struct host_rlimit64
{
515 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
516 const struct host_rlimit64
*, new_limit
,
517 struct host_rlimit64
*, old_limit
)
521 #if defined(TARGET_NR_timer_create)
522 /* Maximum of 32 active POSIX timers allowed at any one time. */
523 #define GUEST_TIMER_MAX 32
524 static timer_t g_posix_timers
[GUEST_TIMER_MAX
];
525 static int g_posix_timer_allocated
[GUEST_TIMER_MAX
];
527 static inline int next_free_host_timer(void)
530 for (k
= 0; k
< ARRAY_SIZE(g_posix_timer_allocated
); k
++) {
531 if (qatomic_xchg(g_posix_timer_allocated
+ k
, 1) == 0) {
538 static inline void free_host_timer_slot(int id
)
540 qatomic_store_release(g_posix_timer_allocated
+ id
, 0);
544 static inline int host_to_target_errno(int host_errno
)
546 switch (host_errno
) {
547 #define E(X) case X: return TARGET_##X;
548 #include "errnos.c.inc"
555 static inline int target_to_host_errno(int target_errno
)
557 switch (target_errno
) {
558 #define E(X) case TARGET_##X: return X;
559 #include "errnos.c.inc"
566 abi_long
get_errno(abi_long ret
)
569 return -host_to_target_errno(errno
);
574 const char *target_strerror(int err
)
576 if (err
== QEMU_ERESTARTSYS
) {
577 return "To be restarted";
579 if (err
== QEMU_ESIGRETURN
) {
580 return "Successful exit from sigreturn";
583 return strerror(target_to_host_errno(err
));
586 static int check_zeroed_user(abi_long addr
, size_t ksize
, size_t usize
)
590 if (usize
<= ksize
) {
593 for (i
= ksize
; i
< usize
; i
++) {
594 if (get_user_u8(b
, addr
+ i
)) {
595 return -TARGET_EFAULT
;
604 #define safe_syscall0(type, name) \
605 static type safe_##name(void) \
607 return safe_syscall(__NR_##name); \
610 #define safe_syscall1(type, name, type1, arg1) \
611 static type safe_##name(type1 arg1) \
613 return safe_syscall(__NR_##name, arg1); \
616 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
617 static type safe_##name(type1 arg1, type2 arg2) \
619 return safe_syscall(__NR_##name, arg1, arg2); \
622 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
623 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
625 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
628 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
630 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
632 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
635 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
636 type4, arg4, type5, arg5) \
637 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
640 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
643 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
644 type4, arg4, type5, arg5, type6, arg6) \
645 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
646 type5 arg5, type6 arg6) \
648 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
651 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
652 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
653 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
654 int, flags
, mode_t
, mode
)
655 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
656 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
657 struct rusage
*, rusage
)
659 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
660 int, options
, struct rusage
*, rusage
)
661 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
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
, initial_target_brk
;
805 void target_set_brk(abi_ulong new_brk
)
807 target_brk
= TARGET_PAGE_ALIGN(new_brk
);
808 initial_target_brk
= target_brk
;
811 /* do_brk() must return target values and target errnos. */
812 abi_long
do_brk(abi_ulong brk_val
)
814 abi_long mapped_addr
;
818 /* brk pointers are always untagged */
820 /* do not allow to shrink below initial brk value */
821 if (brk_val
< initial_target_brk
) {
825 new_brk
= TARGET_PAGE_ALIGN(brk_val
);
826 old_brk
= TARGET_PAGE_ALIGN(target_brk
);
828 /* new and old target_brk might be on the same page */
829 if (new_brk
== old_brk
) {
830 target_brk
= brk_val
;
834 /* Release heap if necessary */
835 if (new_brk
< old_brk
) {
836 target_munmap(new_brk
, old_brk
- new_brk
);
838 target_brk
= brk_val
;
842 mapped_addr
= target_mmap(old_brk
, new_brk
- old_brk
,
843 PROT_READ
| PROT_WRITE
,
844 MAP_FIXED_NOREPLACE
| MAP_ANON
| MAP_PRIVATE
,
847 if (mapped_addr
== old_brk
) {
848 target_brk
= brk_val
;
852 #if defined(TARGET_ALPHA)
853 /* We (partially) emulate OSF/1 on Alpha, which requires we
854 return a proper errno, not an unchanged brk value. */
855 return -TARGET_ENOMEM
;
857 /* For everything else, return the previous break. */
861 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
862 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
863 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
864 abi_ulong target_fds_addr
,
868 abi_ulong b
, *target_fds
;
870 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
871 if (!(target_fds
= lock_user(VERIFY_READ
,
873 sizeof(abi_ulong
) * nw
,
875 return -TARGET_EFAULT
;
879 for (i
= 0; i
< nw
; i
++) {
880 /* grab the abi_ulong */
881 __get_user(b
, &target_fds
[i
]);
882 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
883 /* check the bit inside the abi_ulong */
890 unlock_user(target_fds
, target_fds_addr
, 0);
895 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
896 abi_ulong target_fds_addr
,
899 if (target_fds_addr
) {
900 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
901 return -TARGET_EFAULT
;
909 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
915 abi_ulong
*target_fds
;
917 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
918 if (!(target_fds
= lock_user(VERIFY_WRITE
,
920 sizeof(abi_ulong
) * nw
,
922 return -TARGET_EFAULT
;
925 for (i
= 0; i
< nw
; i
++) {
927 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
928 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
931 __put_user(v
, &target_fds
[i
]);
934 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
940 #if defined(__alpha__)
946 static inline abi_long
host_to_target_clock_t(long ticks
)
948 #if HOST_HZ == TARGET_HZ
951 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
955 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
956 const struct rusage
*rusage
)
958 struct target_rusage
*target_rusage
;
960 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
961 return -TARGET_EFAULT
;
962 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
963 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
964 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
965 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
966 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
967 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
968 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
969 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
970 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
971 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
972 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
973 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
974 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
975 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
976 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
977 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
978 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
979 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
980 unlock_user_struct(target_rusage
, target_addr
, 1);
985 #ifdef TARGET_NR_setrlimit
986 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
988 abi_ulong target_rlim_swap
;
991 target_rlim_swap
= tswapal(target_rlim
);
992 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
993 return RLIM_INFINITY
;
995 result
= target_rlim_swap
;
996 if (target_rlim_swap
!= (rlim_t
)result
)
997 return RLIM_INFINITY
;
1003 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
1004 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1006 abi_ulong target_rlim_swap
;
1009 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1010 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1012 target_rlim_swap
= rlim
;
1013 result
= tswapal(target_rlim_swap
);
1019 static inline int target_to_host_resource(int code
)
1022 case TARGET_RLIMIT_AS
:
1024 case TARGET_RLIMIT_CORE
:
1026 case TARGET_RLIMIT_CPU
:
1028 case TARGET_RLIMIT_DATA
:
1030 case TARGET_RLIMIT_FSIZE
:
1031 return RLIMIT_FSIZE
;
1032 case TARGET_RLIMIT_LOCKS
:
1033 return RLIMIT_LOCKS
;
1034 case TARGET_RLIMIT_MEMLOCK
:
1035 return RLIMIT_MEMLOCK
;
1036 case TARGET_RLIMIT_MSGQUEUE
:
1037 return RLIMIT_MSGQUEUE
;
1038 case TARGET_RLIMIT_NICE
:
1040 case TARGET_RLIMIT_NOFILE
:
1041 return RLIMIT_NOFILE
;
1042 case TARGET_RLIMIT_NPROC
:
1043 return RLIMIT_NPROC
;
1044 case TARGET_RLIMIT_RSS
:
1046 case TARGET_RLIMIT_RTPRIO
:
1047 return RLIMIT_RTPRIO
;
1048 #ifdef RLIMIT_RTTIME
1049 case TARGET_RLIMIT_RTTIME
:
1050 return RLIMIT_RTTIME
;
1052 case TARGET_RLIMIT_SIGPENDING
:
1053 return RLIMIT_SIGPENDING
;
1054 case TARGET_RLIMIT_STACK
:
1055 return RLIMIT_STACK
;
1061 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1062 abi_ulong target_tv_addr
)
1064 struct target_timeval
*target_tv
;
1066 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1067 return -TARGET_EFAULT
;
1070 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1071 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1073 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1078 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1079 const struct timeval
*tv
)
1081 struct target_timeval
*target_tv
;
1083 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1084 return -TARGET_EFAULT
;
1087 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1088 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1090 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1095 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1096 static inline abi_long
copy_from_user_timeval64(struct timeval
*tv
,
1097 abi_ulong target_tv_addr
)
1099 struct target__kernel_sock_timeval
*target_tv
;
1101 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1102 return -TARGET_EFAULT
;
1105 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1106 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1108 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1114 static inline abi_long
copy_to_user_timeval64(abi_ulong target_tv_addr
,
1115 const struct timeval
*tv
)
1117 struct target__kernel_sock_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_futex) || \
1132 defined(TARGET_NR_rt_sigtimedwait) || \
1133 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1134 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1135 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
1136 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
1137 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1138 defined(TARGET_NR_timer_settime) || \
1139 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
1140 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
1141 abi_ulong target_addr
)
1143 struct target_timespec
*target_ts
;
1145 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1146 return -TARGET_EFAULT
;
1148 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1149 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1150 unlock_user_struct(target_ts
, target_addr
, 0);
1155 #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1156 defined(TARGET_NR_timer_settime64) || \
1157 defined(TARGET_NR_mq_timedsend_time64) || \
1158 defined(TARGET_NR_mq_timedreceive_time64) || \
1159 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
1160 defined(TARGET_NR_clock_nanosleep_time64) || \
1161 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1162 defined(TARGET_NR_utimensat) || \
1163 defined(TARGET_NR_utimensat_time64) || \
1164 defined(TARGET_NR_semtimedop_time64) || \
1165 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
1166 static inline abi_long
target_to_host_timespec64(struct timespec
*host_ts
,
1167 abi_ulong target_addr
)
1169 struct target__kernel_timespec
*target_ts
;
1171 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1172 return -TARGET_EFAULT
;
1174 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1175 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1176 /* in 32bit mode, this drops the padding */
1177 host_ts
->tv_nsec
= (long)(abi_long
)host_ts
->tv_nsec
;
1178 unlock_user_struct(target_ts
, target_addr
, 0);
1183 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
1184 struct timespec
*host_ts
)
1186 struct target_timespec
*target_ts
;
1188 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1189 return -TARGET_EFAULT
;
1191 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1192 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1193 unlock_user_struct(target_ts
, target_addr
, 1);
1197 static inline abi_long
host_to_target_timespec64(abi_ulong target_addr
,
1198 struct timespec
*host_ts
)
1200 struct target__kernel_timespec
*target_ts
;
1202 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1203 return -TARGET_EFAULT
;
1205 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1206 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1207 unlock_user_struct(target_ts
, target_addr
, 1);
1211 #if defined(TARGET_NR_gettimeofday)
1212 static inline abi_long
copy_to_user_timezone(abi_ulong target_tz_addr
,
1213 struct timezone
*tz
)
1215 struct target_timezone
*target_tz
;
1217 if (!lock_user_struct(VERIFY_WRITE
, target_tz
, target_tz_addr
, 1)) {
1218 return -TARGET_EFAULT
;
1221 __put_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1222 __put_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1224 unlock_user_struct(target_tz
, target_tz_addr
, 1);
1230 #if defined(TARGET_NR_settimeofday)
1231 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1232 abi_ulong target_tz_addr
)
1234 struct target_timezone
*target_tz
;
1236 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1237 return -TARGET_EFAULT
;
1240 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1241 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1243 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1249 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1252 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1253 abi_ulong target_mq_attr_addr
)
1255 struct target_mq_attr
*target_mq_attr
;
1257 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1258 target_mq_attr_addr
, 1))
1259 return -TARGET_EFAULT
;
1261 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1262 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1263 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1264 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1266 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1271 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1272 const struct mq_attr
*attr
)
1274 struct target_mq_attr
*target_mq_attr
;
1276 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1277 target_mq_attr_addr
, 0))
1278 return -TARGET_EFAULT
;
1280 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1281 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1282 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1283 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1285 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1291 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1292 /* do_select() must return target values and target errnos. */
1293 static abi_long
do_select(int n
,
1294 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1295 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1297 fd_set rfds
, wfds
, efds
;
1298 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1300 struct timespec ts
, *ts_ptr
;
1303 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1307 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1311 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1316 if (target_tv_addr
) {
1317 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1318 return -TARGET_EFAULT
;
1319 ts
.tv_sec
= tv
.tv_sec
;
1320 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1326 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1329 if (!is_error(ret
)) {
1330 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1331 return -TARGET_EFAULT
;
1332 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1333 return -TARGET_EFAULT
;
1334 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1335 return -TARGET_EFAULT
;
1337 if (target_tv_addr
) {
1338 tv
.tv_sec
= ts
.tv_sec
;
1339 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1340 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1341 return -TARGET_EFAULT
;
1349 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1350 static abi_long
do_old_select(abi_ulong arg1
)
1352 struct target_sel_arg_struct
*sel
;
1353 abi_ulong inp
, outp
, exp
, tvp
;
1356 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1357 return -TARGET_EFAULT
;
1360 nsel
= tswapal(sel
->n
);
1361 inp
= tswapal(sel
->inp
);
1362 outp
= tswapal(sel
->outp
);
1363 exp
= tswapal(sel
->exp
);
1364 tvp
= tswapal(sel
->tvp
);
1366 unlock_user_struct(sel
, arg1
, 0);
1368 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1373 #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1374 static abi_long
do_pselect6(abi_long arg1
, abi_long arg2
, abi_long arg3
,
1375 abi_long arg4
, abi_long arg5
, abi_long arg6
,
1378 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
1379 fd_set rfds
, wfds
, efds
;
1380 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1381 struct timespec ts
, *ts_ptr
;
1385 * The 6th arg is actually two args smashed together,
1386 * so we cannot use the C library.
1393 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
1401 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1405 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1409 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1415 * This takes a timespec, and not a timeval, so we cannot
1416 * use the do_select() helper ...
1420 if (target_to_host_timespec64(&ts
, ts_addr
)) {
1421 return -TARGET_EFAULT
;
1424 if (target_to_host_timespec(&ts
, ts_addr
)) {
1425 return -TARGET_EFAULT
;
1433 /* Extract the two packed args for the sigset */
1436 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
1438 return -TARGET_EFAULT
;
1440 arg_sigset
= tswapal(arg7
[0]);
1441 arg_sigsize
= tswapal(arg7
[1]);
1442 unlock_user(arg7
, arg6
, 0);
1445 ret
= process_sigsuspend_mask(&sig
.set
, arg_sigset
, arg_sigsize
);
1450 sig
.size
= SIGSET_T_SIZE
;
1454 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1458 finish_sigsuspend_mask(ret
);
1461 if (!is_error(ret
)) {
1462 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
)) {
1463 return -TARGET_EFAULT
;
1465 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
)) {
1466 return -TARGET_EFAULT
;
1468 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
)) {
1469 return -TARGET_EFAULT
;
1472 if (ts_addr
&& host_to_target_timespec64(ts_addr
, &ts
)) {
1473 return -TARGET_EFAULT
;
1476 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
)) {
1477 return -TARGET_EFAULT
;
1485 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1486 defined(TARGET_NR_ppoll_time64)
1487 static abi_long
do_ppoll(abi_long arg1
, abi_long arg2
, abi_long arg3
,
1488 abi_long arg4
, abi_long arg5
, bool ppoll
, bool time64
)
1490 struct target_pollfd
*target_pfd
;
1491 unsigned int nfds
= arg2
;
1499 if (nfds
> (INT_MAX
/ sizeof(struct target_pollfd
))) {
1500 return -TARGET_EINVAL
;
1502 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
1503 sizeof(struct target_pollfd
) * nfds
, 1);
1505 return -TARGET_EFAULT
;
1508 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
1509 for (i
= 0; i
< nfds
; i
++) {
1510 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
1511 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
1515 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
1516 sigset_t
*set
= NULL
;
1520 if (target_to_host_timespec64(timeout_ts
, arg3
)) {
1521 unlock_user(target_pfd
, arg1
, 0);
1522 return -TARGET_EFAULT
;
1525 if (target_to_host_timespec(timeout_ts
, arg3
)) {
1526 unlock_user(target_pfd
, arg1
, 0);
1527 return -TARGET_EFAULT
;
1535 ret
= process_sigsuspend_mask(&set
, arg4
, arg5
);
1537 unlock_user(target_pfd
, arg1
, 0);
1542 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
1543 set
, SIGSET_T_SIZE
));
1546 finish_sigsuspend_mask(ret
);
1548 if (!is_error(ret
) && arg3
) {
1550 if (host_to_target_timespec64(arg3
, timeout_ts
)) {
1551 return -TARGET_EFAULT
;
1554 if (host_to_target_timespec(arg3
, timeout_ts
)) {
1555 return -TARGET_EFAULT
;
1560 struct timespec ts
, *pts
;
1563 /* Convert ms to secs, ns */
1564 ts
.tv_sec
= arg3
/ 1000;
1565 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
1568 /* -ve poll() timeout means "infinite" */
1571 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
1574 if (!is_error(ret
)) {
1575 for (i
= 0; i
< nfds
; i
++) {
1576 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
1579 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
1584 static abi_long
do_pipe(CPUArchState
*cpu_env
, abi_ulong pipedes
,
1585 int flags
, int is_pipe2
)
1589 ret
= pipe2(host_pipe
, flags
);
1592 return get_errno(ret
);
1594 /* Several targets have special calling conventions for the original
1595 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1597 #if defined(TARGET_ALPHA)
1598 cpu_env
->ir
[IR_A4
] = host_pipe
[1];
1599 return host_pipe
[0];
1600 #elif defined(TARGET_MIPS)
1601 cpu_env
->active_tc
.gpr
[3] = host_pipe
[1];
1602 return host_pipe
[0];
1603 #elif defined(TARGET_SH4)
1604 cpu_env
->gregs
[1] = host_pipe
[1];
1605 return host_pipe
[0];
1606 #elif defined(TARGET_SPARC)
1607 cpu_env
->regwptr
[1] = host_pipe
[1];
1608 return host_pipe
[0];
1612 if (put_user_s32(host_pipe
[0], pipedes
)
1613 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(abi_int
)))
1614 return -TARGET_EFAULT
;
1615 return get_errno(ret
);
1618 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1619 abi_ulong target_addr
,
1622 struct target_ip_mreqn
*target_smreqn
;
1624 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1626 return -TARGET_EFAULT
;
1627 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1628 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1629 if (len
== sizeof(struct target_ip_mreqn
))
1630 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1631 unlock_user(target_smreqn
, target_addr
, 0);
1636 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1637 abi_ulong target_addr
,
1640 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1641 sa_family_t sa_family
;
1642 struct target_sockaddr
*target_saddr
;
1644 if (fd_trans_target_to_host_addr(fd
)) {
1645 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1648 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1650 return -TARGET_EFAULT
;
1652 sa_family
= tswap16(target_saddr
->sa_family
);
1654 /* Oops. The caller might send a incomplete sun_path; sun_path
1655 * must be terminated by \0 (see the manual page), but
1656 * unfortunately it is quite common to specify sockaddr_un
1657 * length as "strlen(x->sun_path)" while it should be
1658 * "strlen(...) + 1". We'll fix that here if needed.
1659 * Linux kernel has a similar feature.
1662 if (sa_family
== AF_UNIX
) {
1663 if (len
< unix_maxlen
&& len
> 0) {
1664 char *cp
= (char*)target_saddr
;
1666 if ( cp
[len
-1] && !cp
[len
] )
1669 if (len
> unix_maxlen
)
1673 memcpy(addr
, target_saddr
, len
);
1674 addr
->sa_family
= sa_family
;
1675 if (sa_family
== AF_NETLINK
) {
1676 struct sockaddr_nl
*nladdr
;
1678 nladdr
= (struct sockaddr_nl
*)addr
;
1679 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1680 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1681 } else if (sa_family
== AF_PACKET
) {
1682 struct target_sockaddr_ll
*lladdr
;
1684 lladdr
= (struct target_sockaddr_ll
*)addr
;
1685 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1686 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1687 } else if (sa_family
== AF_INET6
) {
1688 struct sockaddr_in6
*in6addr
;
1690 in6addr
= (struct sockaddr_in6
*)addr
;
1691 in6addr
->sin6_scope_id
= tswap32(in6addr
->sin6_scope_id
);
1693 unlock_user(target_saddr
, target_addr
, 0);
1698 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1699 struct sockaddr
*addr
,
1702 struct target_sockaddr
*target_saddr
;
1709 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1711 return -TARGET_EFAULT
;
1712 memcpy(target_saddr
, addr
, len
);
1713 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1714 sizeof(target_saddr
->sa_family
)) {
1715 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1717 if (addr
->sa_family
== AF_NETLINK
&&
1718 len
>= sizeof(struct target_sockaddr_nl
)) {
1719 struct target_sockaddr_nl
*target_nl
=
1720 (struct target_sockaddr_nl
*)target_saddr
;
1721 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1722 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1723 } else if (addr
->sa_family
== AF_PACKET
) {
1724 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1725 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1726 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1727 } else if (addr
->sa_family
== AF_INET6
&&
1728 len
>= sizeof(struct target_sockaddr_in6
)) {
1729 struct target_sockaddr_in6
*target_in6
=
1730 (struct target_sockaddr_in6
*)target_saddr
;
1731 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1733 unlock_user(target_saddr
, target_addr
, len
);
1738 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1739 struct target_msghdr
*target_msgh
)
1741 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1742 abi_long msg_controllen
;
1743 abi_ulong target_cmsg_addr
;
1744 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1745 socklen_t space
= 0;
1747 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1748 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1750 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1751 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1752 target_cmsg_start
= target_cmsg
;
1754 return -TARGET_EFAULT
;
1756 while (cmsg
&& target_cmsg
) {
1757 void *data
= CMSG_DATA(cmsg
);
1758 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1760 int len
= tswapal(target_cmsg
->cmsg_len
)
1761 - sizeof(struct target_cmsghdr
);
1763 space
+= CMSG_SPACE(len
);
1764 if (space
> msgh
->msg_controllen
) {
1765 space
-= CMSG_SPACE(len
);
1766 /* This is a QEMU bug, since we allocated the payload
1767 * area ourselves (unlike overflow in host-to-target
1768 * conversion, which is just the guest giving us a buffer
1769 * that's too small). It can't happen for the payload types
1770 * we currently support; if it becomes an issue in future
1771 * we would need to improve our allocation strategy to
1772 * something more intelligent than "twice the size of the
1773 * target buffer we're reading from".
1775 qemu_log_mask(LOG_UNIMP
,
1776 ("Unsupported ancillary data %d/%d: "
1777 "unhandled msg size\n"),
1778 tswap32(target_cmsg
->cmsg_level
),
1779 tswap32(target_cmsg
->cmsg_type
));
1783 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1784 cmsg
->cmsg_level
= SOL_SOCKET
;
1786 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1788 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1789 cmsg
->cmsg_len
= CMSG_LEN(len
);
1791 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1792 int *fd
= (int *)data
;
1793 int *target_fd
= (int *)target_data
;
1794 int i
, numfds
= len
/ sizeof(int);
1796 for (i
= 0; i
< numfds
; i
++) {
1797 __get_user(fd
[i
], target_fd
+ i
);
1799 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1800 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1801 struct ucred
*cred
= (struct ucred
*)data
;
1802 struct target_ucred
*target_cred
=
1803 (struct target_ucred
*)target_data
;
1805 __get_user(cred
->pid
, &target_cred
->pid
);
1806 __get_user(cred
->uid
, &target_cred
->uid
);
1807 __get_user(cred
->gid
, &target_cred
->gid
);
1808 } else if (cmsg
->cmsg_level
== SOL_ALG
) {
1809 uint32_t *dst
= (uint32_t *)data
;
1811 memcpy(dst
, target_data
, len
);
1812 /* fix endianness of first 32-bit word */
1813 if (len
>= sizeof(uint32_t)) {
1814 *dst
= tswap32(*dst
);
1817 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1818 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1819 memcpy(data
, target_data
, len
);
1822 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1823 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1826 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1828 msgh
->msg_controllen
= space
;
1832 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1833 struct msghdr
*msgh
)
1835 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1836 abi_long msg_controllen
;
1837 abi_ulong target_cmsg_addr
;
1838 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1839 socklen_t space
= 0;
1841 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1842 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1844 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1845 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1846 target_cmsg_start
= target_cmsg
;
1848 return -TARGET_EFAULT
;
1850 while (cmsg
&& target_cmsg
) {
1851 void *data
= CMSG_DATA(cmsg
);
1852 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1854 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1855 int tgt_len
, tgt_space
;
1857 /* We never copy a half-header but may copy half-data;
1858 * this is Linux's behaviour in put_cmsg(). Note that
1859 * truncation here is a guest problem (which we report
1860 * to the guest via the CTRUNC bit), unlike truncation
1861 * in target_to_host_cmsg, which is a QEMU bug.
1863 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1864 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1868 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1869 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1871 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1873 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1875 /* Payload types which need a different size of payload on
1876 * the target must adjust tgt_len here.
1879 switch (cmsg
->cmsg_level
) {
1881 switch (cmsg
->cmsg_type
) {
1883 tgt_len
= sizeof(struct target_timeval
);
1893 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1894 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1895 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1898 /* We must now copy-and-convert len bytes of payload
1899 * into tgt_len bytes of destination space. Bear in mind
1900 * that in both source and destination we may be dealing
1901 * with a truncated value!
1903 switch (cmsg
->cmsg_level
) {
1905 switch (cmsg
->cmsg_type
) {
1908 int *fd
= (int *)data
;
1909 int *target_fd
= (int *)target_data
;
1910 int i
, numfds
= tgt_len
/ sizeof(int);
1912 for (i
= 0; i
< numfds
; i
++) {
1913 __put_user(fd
[i
], target_fd
+ i
);
1919 struct timeval
*tv
= (struct timeval
*)data
;
1920 struct target_timeval
*target_tv
=
1921 (struct target_timeval
*)target_data
;
1923 if (len
!= sizeof(struct timeval
) ||
1924 tgt_len
!= sizeof(struct target_timeval
)) {
1928 /* copy struct timeval to target */
1929 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1930 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1933 case SCM_CREDENTIALS
:
1935 struct ucred
*cred
= (struct ucred
*)data
;
1936 struct target_ucred
*target_cred
=
1937 (struct target_ucred
*)target_data
;
1939 __put_user(cred
->pid
, &target_cred
->pid
);
1940 __put_user(cred
->uid
, &target_cred
->uid
);
1941 __put_user(cred
->gid
, &target_cred
->gid
);
1950 switch (cmsg
->cmsg_type
) {
1953 uint32_t *v
= (uint32_t *)data
;
1954 uint32_t *t_int
= (uint32_t *)target_data
;
1956 if (len
!= sizeof(uint32_t) ||
1957 tgt_len
!= sizeof(uint32_t)) {
1960 __put_user(*v
, t_int
);
1966 struct sock_extended_err ee
;
1967 struct sockaddr_in offender
;
1969 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1970 struct errhdr_t
*target_errh
=
1971 (struct errhdr_t
*)target_data
;
1973 if (len
!= sizeof(struct errhdr_t
) ||
1974 tgt_len
!= sizeof(struct errhdr_t
)) {
1977 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1978 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1979 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1980 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1981 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1982 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1983 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1984 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1985 (void *) &errh
->offender
, sizeof(errh
->offender
));
1994 switch (cmsg
->cmsg_type
) {
1997 uint32_t *v
= (uint32_t *)data
;
1998 uint32_t *t_int
= (uint32_t *)target_data
;
2000 if (len
!= sizeof(uint32_t) ||
2001 tgt_len
!= sizeof(uint32_t)) {
2004 __put_user(*v
, t_int
);
2010 struct sock_extended_err ee
;
2011 struct sockaddr_in6 offender
;
2013 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
2014 struct errhdr6_t
*target_errh
=
2015 (struct errhdr6_t
*)target_data
;
2017 if (len
!= sizeof(struct errhdr6_t
) ||
2018 tgt_len
!= sizeof(struct errhdr6_t
)) {
2021 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
2022 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
2023 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
2024 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
2025 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
2026 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
2027 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
2028 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
2029 (void *) &errh
->offender
, sizeof(errh
->offender
));
2039 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
2040 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
2041 memcpy(target_data
, data
, MIN(len
, tgt_len
));
2042 if (tgt_len
> len
) {
2043 memset(target_data
+ len
, 0, tgt_len
- len
);
2047 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
2048 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
2049 if (msg_controllen
< tgt_space
) {
2050 tgt_space
= msg_controllen
;
2052 msg_controllen
-= tgt_space
;
2054 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
2055 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
2058 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
2060 target_msgh
->msg_controllen
= tswapal(space
);
2064 /* do_setsockopt() Must return target values and target errnos. */
2065 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
2066 abi_ulong optval_addr
, socklen_t optlen
)
2070 struct ip_mreqn
*ip_mreq
;
2071 struct ip_mreq_source
*ip_mreq_source
;
2076 /* TCP and UDP options all take an 'int' value. */
2077 if (optlen
< sizeof(uint32_t))
2078 return -TARGET_EINVAL
;
2080 if (get_user_u32(val
, optval_addr
))
2081 return -TARGET_EFAULT
;
2082 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2089 case IP_ROUTER_ALERT
:
2093 case IP_MTU_DISCOVER
:
2100 case IP_MULTICAST_TTL
:
2101 case IP_MULTICAST_LOOP
:
2103 if (optlen
>= sizeof(uint32_t)) {
2104 if (get_user_u32(val
, optval_addr
))
2105 return -TARGET_EFAULT
;
2106 } else if (optlen
>= 1) {
2107 if (get_user_u8(val
, optval_addr
))
2108 return -TARGET_EFAULT
;
2110 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2112 case IP_ADD_MEMBERSHIP
:
2113 case IP_DROP_MEMBERSHIP
:
2114 if (optlen
< sizeof (struct target_ip_mreq
) ||
2115 optlen
> sizeof (struct target_ip_mreqn
))
2116 return -TARGET_EINVAL
;
2118 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
2119 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
2120 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
2123 case IP_BLOCK_SOURCE
:
2124 case IP_UNBLOCK_SOURCE
:
2125 case IP_ADD_SOURCE_MEMBERSHIP
:
2126 case IP_DROP_SOURCE_MEMBERSHIP
:
2127 if (optlen
!= sizeof (struct target_ip_mreq_source
))
2128 return -TARGET_EINVAL
;
2130 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2131 if (!ip_mreq_source
) {
2132 return -TARGET_EFAULT
;
2134 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
2135 unlock_user (ip_mreq_source
, optval_addr
, 0);
2144 case IPV6_MTU_DISCOVER
:
2147 case IPV6_RECVPKTINFO
:
2148 case IPV6_UNICAST_HOPS
:
2149 case IPV6_MULTICAST_HOPS
:
2150 case IPV6_MULTICAST_LOOP
:
2152 case IPV6_RECVHOPLIMIT
:
2153 case IPV6_2292HOPLIMIT
:
2156 case IPV6_2292PKTINFO
:
2157 case IPV6_RECVTCLASS
:
2158 case IPV6_RECVRTHDR
:
2159 case IPV6_2292RTHDR
:
2160 case IPV6_RECVHOPOPTS
:
2161 case IPV6_2292HOPOPTS
:
2162 case IPV6_RECVDSTOPTS
:
2163 case IPV6_2292DSTOPTS
:
2165 case IPV6_ADDR_PREFERENCES
:
2166 #ifdef IPV6_RECVPATHMTU
2167 case IPV6_RECVPATHMTU
:
2169 #ifdef IPV6_TRANSPARENT
2170 case IPV6_TRANSPARENT
:
2172 #ifdef IPV6_FREEBIND
2175 #ifdef IPV6_RECVORIGDSTADDR
2176 case IPV6_RECVORIGDSTADDR
:
2179 if (optlen
< sizeof(uint32_t)) {
2180 return -TARGET_EINVAL
;
2182 if (get_user_u32(val
, optval_addr
)) {
2183 return -TARGET_EFAULT
;
2185 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2186 &val
, sizeof(val
)));
2190 struct in6_pktinfo pki
;
2192 if (optlen
< sizeof(pki
)) {
2193 return -TARGET_EINVAL
;
2196 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2197 return -TARGET_EFAULT
;
2200 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2202 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2203 &pki
, sizeof(pki
)));
2206 case IPV6_ADD_MEMBERSHIP
:
2207 case IPV6_DROP_MEMBERSHIP
:
2209 struct ipv6_mreq ipv6mreq
;
2211 if (optlen
< sizeof(ipv6mreq
)) {
2212 return -TARGET_EINVAL
;
2215 if (copy_from_user(&ipv6mreq
, optval_addr
, sizeof(ipv6mreq
))) {
2216 return -TARGET_EFAULT
;
2219 ipv6mreq
.ipv6mr_interface
= tswap32(ipv6mreq
.ipv6mr_interface
);
2221 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2222 &ipv6mreq
, sizeof(ipv6mreq
)));
2233 struct icmp6_filter icmp6f
;
2235 if (optlen
> sizeof(icmp6f
)) {
2236 optlen
= sizeof(icmp6f
);
2239 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
2240 return -TARGET_EFAULT
;
2243 for (val
= 0; val
< 8; val
++) {
2244 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
2247 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2259 /* those take an u32 value */
2260 if (optlen
< sizeof(uint32_t)) {
2261 return -TARGET_EINVAL
;
2264 if (get_user_u32(val
, optval_addr
)) {
2265 return -TARGET_EFAULT
;
2267 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2268 &val
, sizeof(val
)));
2275 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2280 char *alg_key
= g_malloc(optlen
);
2283 return -TARGET_ENOMEM
;
2285 if (copy_from_user(alg_key
, optval_addr
, optlen
)) {
2287 return -TARGET_EFAULT
;
2289 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2294 case ALG_SET_AEAD_AUTHSIZE
:
2296 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2305 case TARGET_SOL_SOCKET
:
2307 case TARGET_SO_RCVTIMEO
:
2311 optname
= SO_RCVTIMEO
;
2314 if (optlen
!= sizeof(struct target_timeval
)) {
2315 return -TARGET_EINVAL
;
2318 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2319 return -TARGET_EFAULT
;
2322 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2326 case TARGET_SO_SNDTIMEO
:
2327 optname
= SO_SNDTIMEO
;
2329 case TARGET_SO_ATTACH_FILTER
:
2331 struct target_sock_fprog
*tfprog
;
2332 struct target_sock_filter
*tfilter
;
2333 struct sock_fprog fprog
;
2334 struct sock_filter
*filter
;
2337 if (optlen
!= sizeof(*tfprog
)) {
2338 return -TARGET_EINVAL
;
2340 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2341 return -TARGET_EFAULT
;
2343 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2344 tswapal(tfprog
->filter
), 0)) {
2345 unlock_user_struct(tfprog
, optval_addr
, 1);
2346 return -TARGET_EFAULT
;
2349 fprog
.len
= tswap16(tfprog
->len
);
2350 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2351 if (filter
== NULL
) {
2352 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2353 unlock_user_struct(tfprog
, optval_addr
, 1);
2354 return -TARGET_ENOMEM
;
2356 for (i
= 0; i
< fprog
.len
; i
++) {
2357 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2358 filter
[i
].jt
= tfilter
[i
].jt
;
2359 filter
[i
].jf
= tfilter
[i
].jf
;
2360 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2362 fprog
.filter
= filter
;
2364 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2365 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2368 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2369 unlock_user_struct(tfprog
, optval_addr
, 1);
2372 case TARGET_SO_BINDTODEVICE
:
2374 char *dev_ifname
, *addr_ifname
;
2376 if (optlen
> IFNAMSIZ
- 1) {
2377 optlen
= IFNAMSIZ
- 1;
2379 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2381 return -TARGET_EFAULT
;
2383 optname
= SO_BINDTODEVICE
;
2384 addr_ifname
= alloca(IFNAMSIZ
);
2385 memcpy(addr_ifname
, dev_ifname
, optlen
);
2386 addr_ifname
[optlen
] = 0;
2387 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2388 addr_ifname
, optlen
));
2389 unlock_user (dev_ifname
, optval_addr
, 0);
2392 case TARGET_SO_LINGER
:
2395 struct target_linger
*tlg
;
2397 if (optlen
!= sizeof(struct target_linger
)) {
2398 return -TARGET_EINVAL
;
2400 if (!lock_user_struct(VERIFY_READ
, tlg
, optval_addr
, 1)) {
2401 return -TARGET_EFAULT
;
2403 __get_user(lg
.l_onoff
, &tlg
->l_onoff
);
2404 __get_user(lg
.l_linger
, &tlg
->l_linger
);
2405 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
,
2407 unlock_user_struct(tlg
, optval_addr
, 0);
2410 /* Options with 'int' argument. */
2411 case TARGET_SO_DEBUG
:
2414 case TARGET_SO_REUSEADDR
:
2415 optname
= SO_REUSEADDR
;
2418 case TARGET_SO_REUSEPORT
:
2419 optname
= SO_REUSEPORT
;
2422 case TARGET_SO_TYPE
:
2425 case TARGET_SO_ERROR
:
2428 case TARGET_SO_DONTROUTE
:
2429 optname
= SO_DONTROUTE
;
2431 case TARGET_SO_BROADCAST
:
2432 optname
= SO_BROADCAST
;
2434 case TARGET_SO_SNDBUF
:
2435 optname
= SO_SNDBUF
;
2437 case TARGET_SO_SNDBUFFORCE
:
2438 optname
= SO_SNDBUFFORCE
;
2440 case TARGET_SO_RCVBUF
:
2441 optname
= SO_RCVBUF
;
2443 case TARGET_SO_RCVBUFFORCE
:
2444 optname
= SO_RCVBUFFORCE
;
2446 case TARGET_SO_KEEPALIVE
:
2447 optname
= SO_KEEPALIVE
;
2449 case TARGET_SO_OOBINLINE
:
2450 optname
= SO_OOBINLINE
;
2452 case TARGET_SO_NO_CHECK
:
2453 optname
= SO_NO_CHECK
;
2455 case TARGET_SO_PRIORITY
:
2456 optname
= SO_PRIORITY
;
2459 case TARGET_SO_BSDCOMPAT
:
2460 optname
= SO_BSDCOMPAT
;
2463 case TARGET_SO_PASSCRED
:
2464 optname
= SO_PASSCRED
;
2466 case TARGET_SO_PASSSEC
:
2467 optname
= SO_PASSSEC
;
2469 case TARGET_SO_TIMESTAMP
:
2470 optname
= SO_TIMESTAMP
;
2472 case TARGET_SO_RCVLOWAT
:
2473 optname
= SO_RCVLOWAT
;
2478 if (optlen
< sizeof(uint32_t))
2479 return -TARGET_EINVAL
;
2481 if (get_user_u32(val
, optval_addr
))
2482 return -TARGET_EFAULT
;
2483 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2488 case NETLINK_PKTINFO
:
2489 case NETLINK_ADD_MEMBERSHIP
:
2490 case NETLINK_DROP_MEMBERSHIP
:
2491 case NETLINK_BROADCAST_ERROR
:
2492 case NETLINK_NO_ENOBUFS
:
2493 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2494 case NETLINK_LISTEN_ALL_NSID
:
2495 case NETLINK_CAP_ACK
:
2496 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2497 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2498 case NETLINK_EXT_ACK
:
2499 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2500 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2501 case NETLINK_GET_STRICT_CHK
:
2502 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2508 if (optlen
< sizeof(uint32_t)) {
2509 return -TARGET_EINVAL
;
2511 if (get_user_u32(val
, optval_addr
)) {
2512 return -TARGET_EFAULT
;
2514 ret
= get_errno(setsockopt(sockfd
, SOL_NETLINK
, optname
, &val
,
2517 #endif /* SOL_NETLINK */
2520 qemu_log_mask(LOG_UNIMP
, "Unsupported setsockopt level=%d optname=%d\n",
2522 ret
= -TARGET_ENOPROTOOPT
;
2527 /* do_getsockopt() Must return target values and target errnos. */
2528 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2529 abi_ulong optval_addr
, abi_ulong optlen
)
2536 case TARGET_SOL_SOCKET
:
2539 /* These don't just return a single integer */
2540 case TARGET_SO_PEERNAME
:
2542 case TARGET_SO_RCVTIMEO
: {
2546 optname
= SO_RCVTIMEO
;
2549 if (get_user_u32(len
, optlen
)) {
2550 return -TARGET_EFAULT
;
2553 return -TARGET_EINVAL
;
2557 ret
= get_errno(getsockopt(sockfd
, level
, optname
,
2562 if (len
> sizeof(struct target_timeval
)) {
2563 len
= sizeof(struct target_timeval
);
2565 if (copy_to_user_timeval(optval_addr
, &tv
)) {
2566 return -TARGET_EFAULT
;
2568 if (put_user_u32(len
, optlen
)) {
2569 return -TARGET_EFAULT
;
2573 case TARGET_SO_SNDTIMEO
:
2574 optname
= SO_SNDTIMEO
;
2576 case TARGET_SO_PEERCRED
: {
2579 struct target_ucred
*tcr
;
2581 if (get_user_u32(len
, optlen
)) {
2582 return -TARGET_EFAULT
;
2585 return -TARGET_EINVAL
;
2589 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2597 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2598 return -TARGET_EFAULT
;
2600 __put_user(cr
.pid
, &tcr
->pid
);
2601 __put_user(cr
.uid
, &tcr
->uid
);
2602 __put_user(cr
.gid
, &tcr
->gid
);
2603 unlock_user_struct(tcr
, optval_addr
, 1);
2604 if (put_user_u32(len
, optlen
)) {
2605 return -TARGET_EFAULT
;
2609 case TARGET_SO_PEERSEC
: {
2612 if (get_user_u32(len
, optlen
)) {
2613 return -TARGET_EFAULT
;
2616 return -TARGET_EINVAL
;
2618 name
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 0);
2620 return -TARGET_EFAULT
;
2623 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERSEC
,
2625 if (put_user_u32(lv
, optlen
)) {
2626 ret
= -TARGET_EFAULT
;
2628 unlock_user(name
, optval_addr
, lv
);
2631 case TARGET_SO_LINGER
:
2635 struct target_linger
*tlg
;
2637 if (get_user_u32(len
, optlen
)) {
2638 return -TARGET_EFAULT
;
2641 return -TARGET_EINVAL
;
2645 ret
= get_errno(getsockopt(sockfd
, level
, SO_LINGER
,
2653 if (!lock_user_struct(VERIFY_WRITE
, tlg
, optval_addr
, 0)) {
2654 return -TARGET_EFAULT
;
2656 __put_user(lg
.l_onoff
, &tlg
->l_onoff
);
2657 __put_user(lg
.l_linger
, &tlg
->l_linger
);
2658 unlock_user_struct(tlg
, optval_addr
, 1);
2659 if (put_user_u32(len
, optlen
)) {
2660 return -TARGET_EFAULT
;
2664 /* Options with 'int' argument. */
2665 case TARGET_SO_DEBUG
:
2668 case TARGET_SO_REUSEADDR
:
2669 optname
= SO_REUSEADDR
;
2672 case TARGET_SO_REUSEPORT
:
2673 optname
= SO_REUSEPORT
;
2676 case TARGET_SO_TYPE
:
2679 case TARGET_SO_ERROR
:
2682 case TARGET_SO_DONTROUTE
:
2683 optname
= SO_DONTROUTE
;
2685 case TARGET_SO_BROADCAST
:
2686 optname
= SO_BROADCAST
;
2688 case TARGET_SO_SNDBUF
:
2689 optname
= SO_SNDBUF
;
2691 case TARGET_SO_RCVBUF
:
2692 optname
= SO_RCVBUF
;
2694 case TARGET_SO_KEEPALIVE
:
2695 optname
= SO_KEEPALIVE
;
2697 case TARGET_SO_OOBINLINE
:
2698 optname
= SO_OOBINLINE
;
2700 case TARGET_SO_NO_CHECK
:
2701 optname
= SO_NO_CHECK
;
2703 case TARGET_SO_PRIORITY
:
2704 optname
= SO_PRIORITY
;
2707 case TARGET_SO_BSDCOMPAT
:
2708 optname
= SO_BSDCOMPAT
;
2711 case TARGET_SO_PASSCRED
:
2712 optname
= SO_PASSCRED
;
2714 case TARGET_SO_TIMESTAMP
:
2715 optname
= SO_TIMESTAMP
;
2717 case TARGET_SO_RCVLOWAT
:
2718 optname
= SO_RCVLOWAT
;
2720 case TARGET_SO_ACCEPTCONN
:
2721 optname
= SO_ACCEPTCONN
;
2723 case TARGET_SO_PROTOCOL
:
2724 optname
= SO_PROTOCOL
;
2726 case TARGET_SO_DOMAIN
:
2727 optname
= SO_DOMAIN
;
2735 /* TCP and UDP options all take an 'int' value. */
2737 if (get_user_u32(len
, optlen
))
2738 return -TARGET_EFAULT
;
2740 return -TARGET_EINVAL
;
2742 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2747 val
= host_to_target_sock_type(val
);
2750 val
= host_to_target_errno(val
);
2756 if (put_user_u32(val
, optval_addr
))
2757 return -TARGET_EFAULT
;
2759 if (put_user_u8(val
, optval_addr
))
2760 return -TARGET_EFAULT
;
2762 if (put_user_u32(len
, optlen
))
2763 return -TARGET_EFAULT
;
2770 case IP_ROUTER_ALERT
:
2774 case IP_MTU_DISCOVER
:
2780 case IP_MULTICAST_TTL
:
2781 case IP_MULTICAST_LOOP
:
2782 if (get_user_u32(len
, optlen
))
2783 return -TARGET_EFAULT
;
2785 return -TARGET_EINVAL
;
2787 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2790 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2792 if (put_user_u32(len
, optlen
)
2793 || put_user_u8(val
, optval_addr
))
2794 return -TARGET_EFAULT
;
2796 if (len
> sizeof(int))
2798 if (put_user_u32(len
, optlen
)
2799 || put_user_u32(val
, optval_addr
))
2800 return -TARGET_EFAULT
;
2804 ret
= -TARGET_ENOPROTOOPT
;
2810 case IPV6_MTU_DISCOVER
:
2813 case IPV6_RECVPKTINFO
:
2814 case IPV6_UNICAST_HOPS
:
2815 case IPV6_MULTICAST_HOPS
:
2816 case IPV6_MULTICAST_LOOP
:
2818 case IPV6_RECVHOPLIMIT
:
2819 case IPV6_2292HOPLIMIT
:
2822 case IPV6_2292PKTINFO
:
2823 case IPV6_RECVTCLASS
:
2824 case IPV6_RECVRTHDR
:
2825 case IPV6_2292RTHDR
:
2826 case IPV6_RECVHOPOPTS
:
2827 case IPV6_2292HOPOPTS
:
2828 case IPV6_RECVDSTOPTS
:
2829 case IPV6_2292DSTOPTS
:
2831 case IPV6_ADDR_PREFERENCES
:
2832 #ifdef IPV6_RECVPATHMTU
2833 case IPV6_RECVPATHMTU
:
2835 #ifdef IPV6_TRANSPARENT
2836 case IPV6_TRANSPARENT
:
2838 #ifdef IPV6_FREEBIND
2841 #ifdef IPV6_RECVORIGDSTADDR
2842 case IPV6_RECVORIGDSTADDR
:
2844 if (get_user_u32(len
, optlen
))
2845 return -TARGET_EFAULT
;
2847 return -TARGET_EINVAL
;
2849 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2852 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2854 if (put_user_u32(len
, optlen
)
2855 || put_user_u8(val
, optval_addr
))
2856 return -TARGET_EFAULT
;
2858 if (len
> sizeof(int))
2860 if (put_user_u32(len
, optlen
)
2861 || put_user_u32(val
, optval_addr
))
2862 return -TARGET_EFAULT
;
2866 ret
= -TARGET_ENOPROTOOPT
;
2873 case NETLINK_PKTINFO
:
2874 case NETLINK_BROADCAST_ERROR
:
2875 case NETLINK_NO_ENOBUFS
:
2876 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2877 case NETLINK_LISTEN_ALL_NSID
:
2878 case NETLINK_CAP_ACK
:
2879 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2880 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2881 case NETLINK_EXT_ACK
:
2882 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2883 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2884 case NETLINK_GET_STRICT_CHK
:
2885 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2886 if (get_user_u32(len
, optlen
)) {
2887 return -TARGET_EFAULT
;
2889 if (len
!= sizeof(val
)) {
2890 return -TARGET_EINVAL
;
2893 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2897 if (put_user_u32(lv
, optlen
)
2898 || put_user_u32(val
, optval_addr
)) {
2899 return -TARGET_EFAULT
;
2902 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2903 case NETLINK_LIST_MEMBERSHIPS
:
2907 if (get_user_u32(len
, optlen
)) {
2908 return -TARGET_EFAULT
;
2911 return -TARGET_EINVAL
;
2913 results
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 1);
2914 if (!results
&& len
> 0) {
2915 return -TARGET_EFAULT
;
2918 ret
= get_errno(getsockopt(sockfd
, level
, optname
, results
, &lv
));
2920 unlock_user(results
, optval_addr
, 0);
2923 /* swap host endianness to target endianness. */
2924 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
2925 results
[i
] = tswap32(results
[i
]);
2927 if (put_user_u32(lv
, optlen
)) {
2928 return -TARGET_EFAULT
;
2930 unlock_user(results
, optval_addr
, 0);
2933 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2938 #endif /* SOL_NETLINK */
2941 qemu_log_mask(LOG_UNIMP
,
2942 "getsockopt level=%d optname=%d not yet supported\n",
2944 ret
= -TARGET_EOPNOTSUPP
;
2950 /* Convert target low/high pair representing file offset into the host
2951 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2952 * as the kernel doesn't handle them either.
2954 static void target_to_host_low_high(abi_ulong tlow
,
2956 unsigned long *hlow
,
2957 unsigned long *hhigh
)
2959 uint64_t off
= tlow
|
2960 ((unsigned long long)thigh
<< TARGET_LONG_BITS
/ 2) <<
2961 TARGET_LONG_BITS
/ 2;
2964 *hhigh
= (off
>> HOST_LONG_BITS
/ 2) >> HOST_LONG_BITS
/ 2;
2967 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
2968 abi_ulong count
, int copy
)
2970 struct target_iovec
*target_vec
;
2972 abi_ulong total_len
, max_len
;
2975 bool bad_address
= false;
2981 if (count
> IOV_MAX
) {
2986 vec
= g_try_new0(struct iovec
, count
);
2992 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2993 count
* sizeof(struct target_iovec
), 1);
2994 if (target_vec
== NULL
) {
2999 /* ??? If host page size > target page size, this will result in a
3000 value larger than what we can actually support. */
3001 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
3004 for (i
= 0; i
< count
; i
++) {
3005 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3006 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3011 } else if (len
== 0) {
3012 /* Zero length pointer is ignored. */
3013 vec
[i
].iov_base
= 0;
3015 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
3016 /* If the first buffer pointer is bad, this is a fault. But
3017 * subsequent bad buffers will result in a partial write; this
3018 * is realized by filling the vector with null pointers and
3020 if (!vec
[i
].iov_base
) {
3031 if (len
> max_len
- total_len
) {
3032 len
= max_len
- total_len
;
3035 vec
[i
].iov_len
= len
;
3039 unlock_user(target_vec
, target_addr
, 0);
3044 if (tswapal(target_vec
[i
].iov_len
) > 0) {
3045 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
3048 unlock_user(target_vec
, target_addr
, 0);
3055 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
3056 abi_ulong count
, int copy
)
3058 struct target_iovec
*target_vec
;
3061 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3062 count
* sizeof(struct target_iovec
), 1);
3064 for (i
= 0; i
< count
; i
++) {
3065 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3066 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3070 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
3072 unlock_user(target_vec
, target_addr
, 0);
3078 static inline int target_to_host_sock_type(int *type
)
3081 int target_type
= *type
;
3083 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
3084 case TARGET_SOCK_DGRAM
:
3085 host_type
= SOCK_DGRAM
;
3087 case TARGET_SOCK_STREAM
:
3088 host_type
= SOCK_STREAM
;
3091 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
3094 if (target_type
& TARGET_SOCK_CLOEXEC
) {
3095 #if defined(SOCK_CLOEXEC)
3096 host_type
|= SOCK_CLOEXEC
;
3098 return -TARGET_EINVAL
;
3101 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3102 #if defined(SOCK_NONBLOCK)
3103 host_type
|= SOCK_NONBLOCK
;
3104 #elif !defined(O_NONBLOCK)
3105 return -TARGET_EINVAL
;
3112 /* Try to emulate socket type flags after socket creation. */
3113 static int sock_flags_fixup(int fd
, int target_type
)
3115 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3116 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3117 int flags
= fcntl(fd
, F_GETFL
);
3118 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
3120 return -TARGET_EINVAL
;
3127 /* do_socket() Must return target values and target errnos. */
3128 static abi_long
do_socket(int domain
, int type
, int protocol
)
3130 int target_type
= type
;
3133 ret
= target_to_host_sock_type(&type
);
3138 if (domain
== PF_NETLINK
&& !(
3139 #ifdef CONFIG_RTNETLINK
3140 protocol
== NETLINK_ROUTE
||
3142 protocol
== NETLINK_KOBJECT_UEVENT
||
3143 protocol
== NETLINK_AUDIT
)) {
3144 return -TARGET_EPROTONOSUPPORT
;
3147 if (domain
== AF_PACKET
||
3148 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
3149 protocol
= tswap16(protocol
);
3152 ret
= get_errno(socket(domain
, type
, protocol
));
3154 ret
= sock_flags_fixup(ret
, target_type
);
3155 if (type
== SOCK_PACKET
) {
3156 /* Manage an obsolete case :
3157 * if socket type is SOCK_PACKET, bind by name
3159 fd_trans_register(ret
, &target_packet_trans
);
3160 } else if (domain
== PF_NETLINK
) {
3162 #ifdef CONFIG_RTNETLINK
3164 fd_trans_register(ret
, &target_netlink_route_trans
);
3167 case NETLINK_KOBJECT_UEVENT
:
3168 /* nothing to do: messages are strings */
3171 fd_trans_register(ret
, &target_netlink_audit_trans
);
3174 g_assert_not_reached();
3181 /* do_bind() Must return target values and target errnos. */
3182 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3188 if ((int)addrlen
< 0) {
3189 return -TARGET_EINVAL
;
3192 addr
= alloca(addrlen
+1);
3194 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3198 return get_errno(bind(sockfd
, addr
, addrlen
));
3201 /* do_connect() Must return target values and target errnos. */
3202 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3208 if ((int)addrlen
< 0) {
3209 return -TARGET_EINVAL
;
3212 addr
= alloca(addrlen
+1);
3214 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3218 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3221 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3222 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3223 int flags
, int send
)
3229 abi_ulong target_vec
;
3231 if (msgp
->msg_name
) {
3232 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3233 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3234 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3235 tswapal(msgp
->msg_name
),
3237 if (ret
== -TARGET_EFAULT
) {
3238 /* For connected sockets msg_name and msg_namelen must
3239 * be ignored, so returning EFAULT immediately is wrong.
3240 * Instead, pass a bad msg_name to the host kernel, and
3241 * let it decide whether to return EFAULT or not.
3243 msg
.msg_name
= (void *)-1;
3248 msg
.msg_name
= NULL
;
3249 msg
.msg_namelen
= 0;
3251 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3252 msg
.msg_control
= alloca(msg
.msg_controllen
);
3253 memset(msg
.msg_control
, 0, msg
.msg_controllen
);
3255 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3257 count
= tswapal(msgp
->msg_iovlen
);
3258 target_vec
= tswapal(msgp
->msg_iov
);
3260 if (count
> IOV_MAX
) {
3261 /* sendrcvmsg returns a different errno for this condition than
3262 * readv/writev, so we must catch it here before lock_iovec() does.
3264 ret
= -TARGET_EMSGSIZE
;
3268 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3269 target_vec
, count
, send
);
3271 ret
= -host_to_target_errno(errno
);
3272 /* allow sending packet without any iov, e.g. with MSG_MORE flag */
3277 msg
.msg_iovlen
= count
;
3281 if (fd_trans_target_to_host_data(fd
)) {
3284 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3285 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3286 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3287 msg
.msg_iov
->iov_len
);
3289 msg
.msg_iov
->iov_base
= host_msg
;
3290 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3294 ret
= target_to_host_cmsg(&msg
, msgp
);
3296 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3300 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3301 if (!is_error(ret
)) {
3303 if (fd_trans_host_to_target_data(fd
)) {
3304 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3305 MIN(msg
.msg_iov
->iov_len
, len
));
3307 if (!is_error(ret
)) {
3308 ret
= host_to_target_cmsg(msgp
, &msg
);
3310 if (!is_error(ret
)) {
3311 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3312 msgp
->msg_flags
= tswap32(msg
.msg_flags
);
3313 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3314 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3315 msg
.msg_name
, msg
.msg_namelen
);
3328 unlock_iovec(vec
, target_vec
, count
, !send
);
3334 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3335 int flags
, int send
)
3338 struct target_msghdr
*msgp
;
3340 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3344 return -TARGET_EFAULT
;
3346 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3347 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3351 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3352 * so it might not have this *mmsg-specific flag either.
3354 #ifndef MSG_WAITFORONE
3355 #define MSG_WAITFORONE 0x10000
3358 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3359 unsigned int vlen
, unsigned int flags
,
3362 struct target_mmsghdr
*mmsgp
;
3366 if (vlen
> UIO_MAXIOV
) {
3370 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3372 return -TARGET_EFAULT
;
3375 for (i
= 0; i
< vlen
; i
++) {
3376 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3377 if (is_error(ret
)) {
3380 mmsgp
[i
].msg_len
= tswap32(ret
);
3381 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3382 if (flags
& MSG_WAITFORONE
) {
3383 flags
|= MSG_DONTWAIT
;
3387 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3389 /* Return number of datagrams sent if we sent any at all;
3390 * otherwise return the error.
3398 /* do_accept4() Must return target values and target errnos. */
3399 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3400 abi_ulong target_addrlen_addr
, int flags
)
3402 socklen_t addrlen
, ret_addrlen
;
3407 if (flags
& ~(TARGET_SOCK_CLOEXEC
| TARGET_SOCK_NONBLOCK
)) {
3408 return -TARGET_EINVAL
;
3412 if (flags
& TARGET_SOCK_NONBLOCK
) {
3413 host_flags
|= SOCK_NONBLOCK
;
3415 if (flags
& TARGET_SOCK_CLOEXEC
) {
3416 host_flags
|= SOCK_CLOEXEC
;
3419 if (target_addr
== 0) {
3420 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3423 /* linux returns EFAULT if addrlen pointer is invalid */
3424 if (get_user_u32(addrlen
, target_addrlen_addr
))
3425 return -TARGET_EFAULT
;
3427 if ((int)addrlen
< 0) {
3428 return -TARGET_EINVAL
;
3431 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3432 return -TARGET_EFAULT
;
3435 addr
= alloca(addrlen
);
3437 ret_addrlen
= addrlen
;
3438 ret
= get_errno(safe_accept4(fd
, addr
, &ret_addrlen
, host_flags
));
3439 if (!is_error(ret
)) {
3440 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3441 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3442 ret
= -TARGET_EFAULT
;
3448 /* do_getpeername() Must return target values and target errnos. */
3449 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3450 abi_ulong target_addrlen_addr
)
3452 socklen_t addrlen
, ret_addrlen
;
3456 if (get_user_u32(addrlen
, target_addrlen_addr
))
3457 return -TARGET_EFAULT
;
3459 if ((int)addrlen
< 0) {
3460 return -TARGET_EINVAL
;
3463 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3464 return -TARGET_EFAULT
;
3467 addr
= alloca(addrlen
);
3469 ret_addrlen
= addrlen
;
3470 ret
= get_errno(getpeername(fd
, addr
, &ret_addrlen
));
3471 if (!is_error(ret
)) {
3472 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3473 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3474 ret
= -TARGET_EFAULT
;
3480 /* do_getsockname() Must return target values and target errnos. */
3481 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3482 abi_ulong target_addrlen_addr
)
3484 socklen_t addrlen
, ret_addrlen
;
3488 if (get_user_u32(addrlen
, target_addrlen_addr
))
3489 return -TARGET_EFAULT
;
3491 if ((int)addrlen
< 0) {
3492 return -TARGET_EINVAL
;
3495 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3496 return -TARGET_EFAULT
;
3499 addr
= alloca(addrlen
);
3501 ret_addrlen
= addrlen
;
3502 ret
= get_errno(getsockname(fd
, addr
, &ret_addrlen
));
3503 if (!is_error(ret
)) {
3504 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3505 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3506 ret
= -TARGET_EFAULT
;
3512 /* do_socketpair() Must return target values and target errnos. */
3513 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3514 abi_ulong target_tab_addr
)
3519 target_to_host_sock_type(&type
);
3521 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
3522 if (!is_error(ret
)) {
3523 if (put_user_s32(tab
[0], target_tab_addr
)
3524 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
3525 ret
= -TARGET_EFAULT
;
3530 /* do_sendto() Must return target values and target errnos. */
3531 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
3532 abi_ulong target_addr
, socklen_t addrlen
)
3536 void *copy_msg
= NULL
;
3539 if ((int)addrlen
< 0) {
3540 return -TARGET_EINVAL
;
3543 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
3545 return -TARGET_EFAULT
;
3546 if (fd_trans_target_to_host_data(fd
)) {
3547 copy_msg
= host_msg
;
3548 host_msg
= g_malloc(len
);
3549 memcpy(host_msg
, copy_msg
, len
);
3550 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
3556 addr
= alloca(addrlen
+1);
3557 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
3561 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
3563 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
3568 host_msg
= copy_msg
;
3570 unlock_user(host_msg
, msg
, 0);
3574 /* do_recvfrom() Must return target values and target errnos. */
3575 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
3576 abi_ulong target_addr
,
3577 abi_ulong target_addrlen
)
3579 socklen_t addrlen
, ret_addrlen
;
3587 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
3589 return -TARGET_EFAULT
;
3593 if (get_user_u32(addrlen
, target_addrlen
)) {
3594 ret
= -TARGET_EFAULT
;
3597 if ((int)addrlen
< 0) {
3598 ret
= -TARGET_EINVAL
;
3601 addr
= alloca(addrlen
);
3602 ret_addrlen
= addrlen
;
3603 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
3604 addr
, &ret_addrlen
));
3606 addr
= NULL
; /* To keep compiler quiet. */
3607 addrlen
= 0; /* To keep compiler quiet. */
3608 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
3610 if (!is_error(ret
)) {
3611 if (fd_trans_host_to_target_data(fd
)) {
3613 trans
= fd_trans_host_to_target_data(fd
)(host_msg
, MIN(ret
, len
));
3614 if (is_error(trans
)) {
3620 host_to_target_sockaddr(target_addr
, addr
,
3621 MIN(addrlen
, ret_addrlen
));
3622 if (put_user_u32(ret_addrlen
, target_addrlen
)) {
3623 ret
= -TARGET_EFAULT
;
3627 unlock_user(host_msg
, msg
, len
);
3630 unlock_user(host_msg
, msg
, 0);
3635 #ifdef TARGET_NR_socketcall
3636 /* do_socketcall() must return target values and target errnos. */
3637 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
3639 static const unsigned nargs
[] = { /* number of arguments per operation */
3640 [TARGET_SYS_SOCKET
] = 3, /* domain, type, protocol */
3641 [TARGET_SYS_BIND
] = 3, /* fd, addr, addrlen */
3642 [TARGET_SYS_CONNECT
] = 3, /* fd, addr, addrlen */
3643 [TARGET_SYS_LISTEN
] = 2, /* fd, backlog */
3644 [TARGET_SYS_ACCEPT
] = 3, /* fd, addr, addrlen */
3645 [TARGET_SYS_GETSOCKNAME
] = 3, /* fd, addr, addrlen */
3646 [TARGET_SYS_GETPEERNAME
] = 3, /* fd, addr, addrlen */
3647 [TARGET_SYS_SOCKETPAIR
] = 4, /* domain, type, protocol, tab */
3648 [TARGET_SYS_SEND
] = 4, /* fd, msg, len, flags */
3649 [TARGET_SYS_RECV
] = 4, /* fd, msg, len, flags */
3650 [TARGET_SYS_SENDTO
] = 6, /* fd, msg, len, flags, addr, addrlen */
3651 [TARGET_SYS_RECVFROM
] = 6, /* fd, msg, len, flags, addr, addrlen */
3652 [TARGET_SYS_SHUTDOWN
] = 2, /* fd, how */
3653 [TARGET_SYS_SETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3654 [TARGET_SYS_GETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3655 [TARGET_SYS_SENDMSG
] = 3, /* fd, msg, flags */
3656 [TARGET_SYS_RECVMSG
] = 3, /* fd, msg, flags */
3657 [TARGET_SYS_ACCEPT4
] = 4, /* fd, addr, addrlen, flags */
3658 [TARGET_SYS_RECVMMSG
] = 4, /* fd, msgvec, vlen, flags */
3659 [TARGET_SYS_SENDMMSG
] = 4, /* fd, msgvec, vlen, flags */
3661 abi_long a
[6]; /* max 6 args */
3664 /* check the range of the first argument num */
3665 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3666 if (num
< 1 || num
> TARGET_SYS_SENDMMSG
) {
3667 return -TARGET_EINVAL
;
3669 /* ensure we have space for args */
3670 if (nargs
[num
] > ARRAY_SIZE(a
)) {
3671 return -TARGET_EINVAL
;
3673 /* collect the arguments in a[] according to nargs[] */
3674 for (i
= 0; i
< nargs
[num
]; ++i
) {
3675 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
3676 return -TARGET_EFAULT
;
3679 /* now when we have the args, invoke the appropriate underlying function */
3681 case TARGET_SYS_SOCKET
: /* domain, type, protocol */
3682 return do_socket(a
[0], a
[1], a
[2]);
3683 case TARGET_SYS_BIND
: /* sockfd, addr, addrlen */
3684 return do_bind(a
[0], a
[1], a
[2]);
3685 case TARGET_SYS_CONNECT
: /* sockfd, addr, addrlen */
3686 return do_connect(a
[0], a
[1], a
[2]);
3687 case TARGET_SYS_LISTEN
: /* sockfd, backlog */
3688 return get_errno(listen(a
[0], a
[1]));
3689 case TARGET_SYS_ACCEPT
: /* sockfd, addr, addrlen */
3690 return do_accept4(a
[0], a
[1], a
[2], 0);
3691 case TARGET_SYS_GETSOCKNAME
: /* sockfd, addr, addrlen */
3692 return do_getsockname(a
[0], a
[1], a
[2]);
3693 case TARGET_SYS_GETPEERNAME
: /* sockfd, addr, addrlen */
3694 return do_getpeername(a
[0], a
[1], a
[2]);
3695 case TARGET_SYS_SOCKETPAIR
: /* domain, type, protocol, tab */
3696 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
3697 case TARGET_SYS_SEND
: /* sockfd, msg, len, flags */
3698 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
3699 case TARGET_SYS_RECV
: /* sockfd, msg, len, flags */
3700 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
3701 case TARGET_SYS_SENDTO
: /* sockfd, msg, len, flags, addr, addrlen */
3702 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3703 case TARGET_SYS_RECVFROM
: /* sockfd, msg, len, flags, addr, addrlen */
3704 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3705 case TARGET_SYS_SHUTDOWN
: /* sockfd, how */
3706 return get_errno(shutdown(a
[0], a
[1]));
3707 case TARGET_SYS_SETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3708 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3709 case TARGET_SYS_GETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3710 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3711 case TARGET_SYS_SENDMSG
: /* sockfd, msg, flags */
3712 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
3713 case TARGET_SYS_RECVMSG
: /* sockfd, msg, flags */
3714 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
3715 case TARGET_SYS_ACCEPT4
: /* sockfd, addr, addrlen, flags */
3716 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
3717 case TARGET_SYS_RECVMMSG
: /* sockfd, msgvec, vlen, flags */
3718 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
3719 case TARGET_SYS_SENDMMSG
: /* sockfd, msgvec, vlen, flags */
3720 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
3722 qemu_log_mask(LOG_UNIMP
, "Unsupported socketcall: %d\n", num
);
3723 return -TARGET_EINVAL
;
3728 #ifndef TARGET_SEMID64_DS
3729 /* asm-generic version of this struct */
3730 struct target_semid64_ds
3732 struct target_ipc_perm sem_perm
;
3733 abi_ulong sem_otime
;
3734 #if TARGET_ABI_BITS == 32
3735 abi_ulong __unused1
;
3737 abi_ulong sem_ctime
;
3738 #if TARGET_ABI_BITS == 32
3739 abi_ulong __unused2
;
3741 abi_ulong sem_nsems
;
3742 abi_ulong __unused3
;
3743 abi_ulong __unused4
;
3747 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
3748 abi_ulong target_addr
)
3750 struct target_ipc_perm
*target_ip
;
3751 struct target_semid64_ds
*target_sd
;
3753 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3754 return -TARGET_EFAULT
;
3755 target_ip
= &(target_sd
->sem_perm
);
3756 host_ip
->__key
= tswap32(target_ip
->__key
);
3757 host_ip
->uid
= tswap32(target_ip
->uid
);
3758 host_ip
->gid
= tswap32(target_ip
->gid
);
3759 host_ip
->cuid
= tswap32(target_ip
->cuid
);
3760 host_ip
->cgid
= tswap32(target_ip
->cgid
);
3761 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3762 host_ip
->mode
= tswap32(target_ip
->mode
);
3764 host_ip
->mode
= tswap16(target_ip
->mode
);
3766 #if defined(TARGET_PPC)
3767 host_ip
->__seq
= tswap32(target_ip
->__seq
);
3769 host_ip
->__seq
= tswap16(target_ip
->__seq
);
3771 unlock_user_struct(target_sd
, target_addr
, 0);
3775 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
3776 struct ipc_perm
*host_ip
)
3778 struct target_ipc_perm
*target_ip
;
3779 struct target_semid64_ds
*target_sd
;
3781 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3782 return -TARGET_EFAULT
;
3783 target_ip
= &(target_sd
->sem_perm
);
3784 target_ip
->__key
= tswap32(host_ip
->__key
);
3785 target_ip
->uid
= tswap32(host_ip
->uid
);
3786 target_ip
->gid
= tswap32(host_ip
->gid
);
3787 target_ip
->cuid
= tswap32(host_ip
->cuid
);
3788 target_ip
->cgid
= tswap32(host_ip
->cgid
);
3789 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3790 target_ip
->mode
= tswap32(host_ip
->mode
);
3792 target_ip
->mode
= tswap16(host_ip
->mode
);
3794 #if defined(TARGET_PPC)
3795 target_ip
->__seq
= tswap32(host_ip
->__seq
);
3797 target_ip
->__seq
= tswap16(host_ip
->__seq
);
3799 unlock_user_struct(target_sd
, target_addr
, 1);
3803 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
3804 abi_ulong target_addr
)
3806 struct target_semid64_ds
*target_sd
;
3808 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3809 return -TARGET_EFAULT
;
3810 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
3811 return -TARGET_EFAULT
;
3812 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
3813 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
3814 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
3815 unlock_user_struct(target_sd
, target_addr
, 0);
3819 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
3820 struct semid_ds
*host_sd
)
3822 struct target_semid64_ds
*target_sd
;
3824 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3825 return -TARGET_EFAULT
;
3826 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
3827 return -TARGET_EFAULT
;
3828 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
3829 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
3830 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
3831 unlock_user_struct(target_sd
, target_addr
, 1);
3835 struct target_seminfo
{
3848 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
3849 struct seminfo
*host_seminfo
)
3851 struct target_seminfo
*target_seminfo
;
3852 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
3853 return -TARGET_EFAULT
;
3854 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
3855 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
3856 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
3857 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
3858 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
3859 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
3860 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
3861 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
3862 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
3863 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
3864 unlock_user_struct(target_seminfo
, target_addr
, 1);
3870 struct semid_ds
*buf
;
3871 unsigned short *array
;
3872 struct seminfo
*__buf
;
3875 union target_semun
{
3882 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
3883 abi_ulong target_addr
)
3886 unsigned short *array
;
3888 struct semid_ds semid_ds
;
3891 semun
.buf
= &semid_ds
;
3893 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3895 return get_errno(ret
);
3897 nsems
= semid_ds
.sem_nsems
;
3899 *host_array
= g_try_new(unsigned short, nsems
);
3901 return -TARGET_ENOMEM
;
3903 array
= lock_user(VERIFY_READ
, target_addr
,
3904 nsems
*sizeof(unsigned short), 1);
3906 g_free(*host_array
);
3907 return -TARGET_EFAULT
;
3910 for(i
=0; i
<nsems
; i
++) {
3911 __get_user((*host_array
)[i
], &array
[i
]);
3913 unlock_user(array
, target_addr
, 0);
3918 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
3919 unsigned short **host_array
)
3922 unsigned short *array
;
3924 struct semid_ds semid_ds
;
3927 semun
.buf
= &semid_ds
;
3929 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3931 return get_errno(ret
);
3933 nsems
= semid_ds
.sem_nsems
;
3935 array
= lock_user(VERIFY_WRITE
, target_addr
,
3936 nsems
*sizeof(unsigned short), 0);
3938 return -TARGET_EFAULT
;
3940 for(i
=0; i
<nsems
; i
++) {
3941 __put_user((*host_array
)[i
], &array
[i
]);
3943 g_free(*host_array
);
3944 unlock_user(array
, target_addr
, 1);
3949 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
3950 abi_ulong target_arg
)
3952 union target_semun target_su
= { .buf
= target_arg
};
3954 struct semid_ds dsarg
;
3955 unsigned short *array
= NULL
;
3956 struct seminfo seminfo
;
3957 abi_long ret
= -TARGET_EINVAL
;
3964 /* In 64 bit cross-endian situations, we will erroneously pick up
3965 * the wrong half of the union for the "val" element. To rectify
3966 * this, the entire 8-byte structure is byteswapped, followed by
3967 * a swap of the 4 byte val field. In other cases, the data is
3968 * already in proper host byte order. */
3969 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
3970 target_su
.buf
= tswapal(target_su
.buf
);
3971 arg
.val
= tswap32(target_su
.val
);
3973 arg
.val
= target_su
.val
;
3975 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3979 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
3983 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3984 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
3991 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
3995 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3996 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
4002 arg
.__buf
= &seminfo
;
4003 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4004 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
4012 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
4019 struct target_sembuf
{
4020 unsigned short sem_num
;
4025 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
4026 abi_ulong target_addr
,
4029 struct target_sembuf
*target_sembuf
;
4032 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
4033 nsops
*sizeof(struct target_sembuf
), 1);
4035 return -TARGET_EFAULT
;
4037 for(i
=0; i
<nsops
; i
++) {
4038 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
4039 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
4040 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
4043 unlock_user(target_sembuf
, target_addr
, 0);
4048 #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
4049 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
4052 * This macro is required to handle the s390 variants, which passes the
4053 * arguments in a different order than default.
4056 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4057 (__nsops), (__timeout), (__sops)
4059 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4060 (__nsops), 0, (__sops), (__timeout)
4063 static inline abi_long
do_semtimedop(int semid
,
4066 abi_long timeout
, bool time64
)
4068 struct sembuf
*sops
;
4069 struct timespec ts
, *pts
= NULL
;
4075 if (target_to_host_timespec64(pts
, timeout
)) {
4076 return -TARGET_EFAULT
;
4079 if (target_to_host_timespec(pts
, timeout
)) {
4080 return -TARGET_EFAULT
;
4085 if (nsops
> TARGET_SEMOPM
) {
4086 return -TARGET_E2BIG
;
4089 sops
= g_new(struct sembuf
, nsops
);
4091 if (target_to_host_sembuf(sops
, ptr
, nsops
)) {
4093 return -TARGET_EFAULT
;
4096 ret
= -TARGET_ENOSYS
;
4097 #ifdef __NR_semtimedop
4098 ret
= get_errno(safe_semtimedop(semid
, sops
, nsops
, pts
));
4101 if (ret
== -TARGET_ENOSYS
) {
4102 ret
= get_errno(safe_ipc(IPCOP_semtimedop
, semid
,
4103 SEMTIMEDOP_IPC_ARGS(nsops
, sops
, (long)pts
)));
4111 struct target_msqid_ds
4113 struct target_ipc_perm msg_perm
;
4114 abi_ulong msg_stime
;
4115 #if TARGET_ABI_BITS == 32
4116 abi_ulong __unused1
;
4118 abi_ulong msg_rtime
;
4119 #if TARGET_ABI_BITS == 32
4120 abi_ulong __unused2
;
4122 abi_ulong msg_ctime
;
4123 #if TARGET_ABI_BITS == 32
4124 abi_ulong __unused3
;
4126 abi_ulong __msg_cbytes
;
4128 abi_ulong msg_qbytes
;
4129 abi_ulong msg_lspid
;
4130 abi_ulong msg_lrpid
;
4131 abi_ulong __unused4
;
4132 abi_ulong __unused5
;
4135 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
4136 abi_ulong target_addr
)
4138 struct target_msqid_ds
*target_md
;
4140 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
4141 return -TARGET_EFAULT
;
4142 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
4143 return -TARGET_EFAULT
;
4144 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
4145 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
4146 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
4147 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
4148 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
4149 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
4150 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
4151 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
4152 unlock_user_struct(target_md
, target_addr
, 0);
4156 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
4157 struct msqid_ds
*host_md
)
4159 struct target_msqid_ds
*target_md
;
4161 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
4162 return -TARGET_EFAULT
;
4163 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
4164 return -TARGET_EFAULT
;
4165 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
4166 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
4167 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
4168 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
4169 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
4170 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
4171 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
4172 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
4173 unlock_user_struct(target_md
, target_addr
, 1);
4177 struct target_msginfo
{
4185 unsigned short int msgseg
;
4188 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
4189 struct msginfo
*host_msginfo
)
4191 struct target_msginfo
*target_msginfo
;
4192 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
4193 return -TARGET_EFAULT
;
4194 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
4195 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
4196 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
4197 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
4198 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
4199 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
4200 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
4201 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
4202 unlock_user_struct(target_msginfo
, target_addr
, 1);
4206 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
4208 struct msqid_ds dsarg
;
4209 struct msginfo msginfo
;
4210 abi_long ret
= -TARGET_EINVAL
;
4218 if (target_to_host_msqid_ds(&dsarg
,ptr
))
4219 return -TARGET_EFAULT
;
4220 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
4221 if (host_to_target_msqid_ds(ptr
,&dsarg
))
4222 return -TARGET_EFAULT
;
4225 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
4229 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
4230 if (host_to_target_msginfo(ptr
, &msginfo
))
4231 return -TARGET_EFAULT
;
4238 struct target_msgbuf
{
4243 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
4244 ssize_t msgsz
, int msgflg
)
4246 struct target_msgbuf
*target_mb
;
4247 struct msgbuf
*host_mb
;
4251 return -TARGET_EINVAL
;
4254 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4255 return -TARGET_EFAULT
;
4256 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4258 unlock_user_struct(target_mb
, msgp
, 0);
4259 return -TARGET_ENOMEM
;
4261 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4262 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4263 ret
= -TARGET_ENOSYS
;
4265 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4268 if (ret
== -TARGET_ENOSYS
) {
4270 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4273 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4279 unlock_user_struct(target_mb
, msgp
, 0);
4285 #if defined(__sparc__)
4286 /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4287 #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4288 #elif defined(__s390x__)
4289 /* The s390 sys_ipc variant has only five parameters. */
4290 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4291 ((long int[]){(long int)__msgp, __msgtyp})
4293 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4294 ((long int[]){(long int)__msgp, __msgtyp}), 0
4298 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4299 ssize_t msgsz
, abi_long msgtyp
,
4302 struct target_msgbuf
*target_mb
;
4304 struct msgbuf
*host_mb
;
4308 return -TARGET_EINVAL
;
4311 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4312 return -TARGET_EFAULT
;
4314 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4316 ret
= -TARGET_ENOMEM
;
4319 ret
= -TARGET_ENOSYS
;
4321 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4324 if (ret
== -TARGET_ENOSYS
) {
4325 ret
= get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv
), msqid
, msgsz
,
4326 msgflg
, MSGRCV_ARGS(host_mb
, msgtyp
)));
4331 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4332 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4333 if (!target_mtext
) {
4334 ret
= -TARGET_EFAULT
;
4337 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4338 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4341 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4345 unlock_user_struct(target_mb
, msgp
, 1);
4350 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4351 abi_ulong target_addr
)
4353 struct target_shmid_ds
*target_sd
;
4355 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4356 return -TARGET_EFAULT
;
4357 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4358 return -TARGET_EFAULT
;
4359 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4360 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4361 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4362 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4363 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4364 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4365 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4366 unlock_user_struct(target_sd
, target_addr
, 0);
4370 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4371 struct shmid_ds
*host_sd
)
4373 struct target_shmid_ds
*target_sd
;
4375 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4376 return -TARGET_EFAULT
;
4377 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4378 return -TARGET_EFAULT
;
4379 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4380 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4381 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4382 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4383 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4384 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4385 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4386 unlock_user_struct(target_sd
, target_addr
, 1);
4390 struct target_shminfo
{
4398 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4399 struct shminfo
*host_shminfo
)
4401 struct target_shminfo
*target_shminfo
;
4402 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4403 return -TARGET_EFAULT
;
4404 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4405 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4406 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4407 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4408 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4409 unlock_user_struct(target_shminfo
, target_addr
, 1);
4413 struct target_shm_info
{
4418 abi_ulong swap_attempts
;
4419 abi_ulong swap_successes
;
4422 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4423 struct shm_info
*host_shm_info
)
4425 struct target_shm_info
*target_shm_info
;
4426 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4427 return -TARGET_EFAULT
;
4428 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4429 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4430 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4431 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4432 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4433 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4434 unlock_user_struct(target_shm_info
, target_addr
, 1);
4438 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4440 struct shmid_ds dsarg
;
4441 struct shminfo shminfo
;
4442 struct shm_info shm_info
;
4443 abi_long ret
= -TARGET_EINVAL
;
4451 if (target_to_host_shmid_ds(&dsarg
, buf
))
4452 return -TARGET_EFAULT
;
4453 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4454 if (host_to_target_shmid_ds(buf
, &dsarg
))
4455 return -TARGET_EFAULT
;
4458 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4459 if (host_to_target_shminfo(buf
, &shminfo
))
4460 return -TARGET_EFAULT
;
4463 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4464 if (host_to_target_shm_info(buf
, &shm_info
))
4465 return -TARGET_EFAULT
;
4470 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4477 #ifdef TARGET_NR_ipc
4478 /* ??? This only works with linear mappings. */
4479 /* do_ipc() must return target values and target errnos. */
4480 static abi_long
do_ipc(CPUArchState
*cpu_env
,
4481 unsigned int call
, abi_long first
,
4482 abi_long second
, abi_long third
,
4483 abi_long ptr
, abi_long fifth
)
4488 version
= call
>> 16;
4493 ret
= do_semtimedop(first
, ptr
, second
, 0, false);
4495 case IPCOP_semtimedop
:
4497 * The s390 sys_ipc variant has only five parameters instead of six
4498 * (as for default variant) and the only difference is the handling of
4499 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4500 * to a struct timespec where the generic variant uses fifth parameter.
4502 #if defined(TARGET_S390X)
4503 ret
= do_semtimedop(first
, ptr
, second
, third
, TARGET_ABI_BITS
== 64);
4505 ret
= do_semtimedop(first
, ptr
, second
, fifth
, TARGET_ABI_BITS
== 64);
4510 ret
= get_errno(semget(first
, second
, third
));
4513 case IPCOP_semctl
: {
4514 /* The semun argument to semctl is passed by value, so dereference the
4517 get_user_ual(atptr
, ptr
);
4518 ret
= do_semctl(first
, second
, third
, atptr
);
4523 ret
= get_errno(msgget(first
, second
));
4527 ret
= do_msgsnd(first
, ptr
, second
, third
);
4531 ret
= do_msgctl(first
, second
, ptr
);
4538 struct target_ipc_kludge
{
4543 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
4544 ret
= -TARGET_EFAULT
;
4548 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
4550 unlock_user_struct(tmp
, ptr
, 0);
4554 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
4563 raddr
= target_shmat(cpu_env
, first
, ptr
, second
);
4564 if (is_error(raddr
))
4565 return get_errno(raddr
);
4566 if (put_user_ual(raddr
, third
))
4567 return -TARGET_EFAULT
;
4571 ret
= -TARGET_EINVAL
;
4576 ret
= target_shmdt(ptr
);
4580 /* IPC_* flag values are the same on all linux platforms */
4581 ret
= get_errno(shmget(first
, second
, third
));
4584 /* IPC_* and SHM_* command values are the same on all linux platforms */
4586 ret
= do_shmctl(first
, second
, ptr
);
4589 qemu_log_mask(LOG_UNIMP
, "Unsupported ipc call: %d (version %d)\n",
4591 ret
= -TARGET_ENOSYS
;
4598 /* kernel structure types definitions */
4600 #define STRUCT(name, ...) STRUCT_ ## name,
4601 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4603 #include "syscall_types.h"
4607 #undef STRUCT_SPECIAL
4609 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4610 #define STRUCT_SPECIAL(name)
4611 #include "syscall_types.h"
4613 #undef STRUCT_SPECIAL
4615 #define MAX_STRUCT_SIZE 4096
4617 #ifdef CONFIG_FIEMAP
4618 /* So fiemap access checks don't overflow on 32 bit systems.
4619 * This is very slightly smaller than the limit imposed by
4620 * the underlying kernel.
4622 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4623 / sizeof(struct fiemap_extent))
4625 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4626 int fd
, int cmd
, abi_long arg
)
4628 /* The parameter for this ioctl is a struct fiemap followed
4629 * by an array of struct fiemap_extent whose size is set
4630 * in fiemap->fm_extent_count. The array is filled in by the
4633 int target_size_in
, target_size_out
;
4635 const argtype
*arg_type
= ie
->arg_type
;
4636 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
4639 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
4643 assert(arg_type
[0] == TYPE_PTR
);
4644 assert(ie
->access
== IOC_RW
);
4646 target_size_in
= thunk_type_size(arg_type
, 0);
4647 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
4649 return -TARGET_EFAULT
;
4651 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4652 unlock_user(argptr
, arg
, 0);
4653 fm
= (struct fiemap
*)buf_temp
;
4654 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
4655 return -TARGET_EINVAL
;
4658 outbufsz
= sizeof (*fm
) +
4659 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
4661 if (outbufsz
> MAX_STRUCT_SIZE
) {
4662 /* We can't fit all the extents into the fixed size buffer.
4663 * Allocate one that is large enough and use it instead.
4665 fm
= g_try_malloc(outbufsz
);
4667 return -TARGET_ENOMEM
;
4669 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
4672 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
4673 if (!is_error(ret
)) {
4674 target_size_out
= target_size_in
;
4675 /* An extent_count of 0 means we were only counting the extents
4676 * so there are no structs to copy
4678 if (fm
->fm_extent_count
!= 0) {
4679 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
4681 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
4683 ret
= -TARGET_EFAULT
;
4685 /* Convert the struct fiemap */
4686 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
4687 if (fm
->fm_extent_count
!= 0) {
4688 p
= argptr
+ target_size_in
;
4689 /* ...and then all the struct fiemap_extents */
4690 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
4691 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
4696 unlock_user(argptr
, arg
, target_size_out
);
4706 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4707 int fd
, int cmd
, abi_long arg
)
4709 const argtype
*arg_type
= ie
->arg_type
;
4713 struct ifconf
*host_ifconf
;
4715 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
4716 const argtype ifreq_max_type
[] = { MK_STRUCT(STRUCT_ifmap_ifreq
) };
4717 int target_ifreq_size
;
4722 abi_long target_ifc_buf
;
4726 assert(arg_type
[0] == TYPE_PTR
);
4727 assert(ie
->access
== IOC_RW
);
4730 target_size
= thunk_type_size(arg_type
, 0);
4732 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4734 return -TARGET_EFAULT
;
4735 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4736 unlock_user(argptr
, arg
, 0);
4738 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
4739 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
4740 target_ifreq_size
= thunk_type_size(ifreq_max_type
, 0);
4742 if (target_ifc_buf
!= 0) {
4743 target_ifc_len
= host_ifconf
->ifc_len
;
4744 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
4745 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
4747 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
4748 if (outbufsz
> MAX_STRUCT_SIZE
) {
4750 * We can't fit all the extents into the fixed size buffer.
4751 * Allocate one that is large enough and use it instead.
4753 host_ifconf
= g_try_malloc(outbufsz
);
4755 return -TARGET_ENOMEM
;
4757 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
4760 host_ifc_buf
= (char *)host_ifconf
+ sizeof(*host_ifconf
);
4762 host_ifconf
->ifc_len
= host_ifc_len
;
4764 host_ifc_buf
= NULL
;
4766 host_ifconf
->ifc_buf
= host_ifc_buf
;
4768 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
4769 if (!is_error(ret
)) {
4770 /* convert host ifc_len to target ifc_len */
4772 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
4773 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
4774 host_ifconf
->ifc_len
= target_ifc_len
;
4776 /* restore target ifc_buf */
4778 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
4780 /* copy struct ifconf to target user */
4782 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4784 return -TARGET_EFAULT
;
4785 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
4786 unlock_user(argptr
, arg
, target_size
);
4788 if (target_ifc_buf
!= 0) {
4789 /* copy ifreq[] to target user */
4790 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
4791 for (i
= 0; i
< nb_ifreq
; i
++) {
4792 thunk_convert(argptr
+ i
* target_ifreq_size
,
4793 host_ifc_buf
+ i
* sizeof(struct ifreq
),
4794 ifreq_arg_type
, THUNK_TARGET
);
4796 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
4801 g_free(host_ifconf
);
4807 #if defined(CONFIG_USBFS)
4808 #if HOST_LONG_BITS > 64
4809 #error USBDEVFS thunks do not support >64 bit hosts yet.
4812 uint64_t target_urb_adr
;
4813 uint64_t target_buf_adr
;
4814 char *target_buf_ptr
;
4815 struct usbdevfs_urb host_urb
;
4818 static GHashTable
*usbdevfs_urb_hashtable(void)
4820 static GHashTable
*urb_hashtable
;
4822 if (!urb_hashtable
) {
4823 urb_hashtable
= g_hash_table_new(g_int64_hash
, g_int64_equal
);
4825 return urb_hashtable
;
4828 static void urb_hashtable_insert(struct live_urb
*urb
)
4830 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4831 g_hash_table_insert(urb_hashtable
, urb
, urb
);
4834 static struct live_urb
*urb_hashtable_lookup(uint64_t target_urb_adr
)
4836 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4837 return g_hash_table_lookup(urb_hashtable
, &target_urb_adr
);
4840 static void urb_hashtable_remove(struct live_urb
*urb
)
4842 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4843 g_hash_table_remove(urb_hashtable
, urb
);
4847 do_ioctl_usbdevfs_reapurb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4848 int fd
, int cmd
, abi_long arg
)
4850 const argtype usbfsurb_arg_type
[] = { MK_STRUCT(STRUCT_usbdevfs_urb
) };
4851 const argtype ptrvoid_arg_type
[] = { TYPE_PTRVOID
, 0, 0 };
4852 struct live_urb
*lurb
;
4856 uintptr_t target_urb_adr
;
4859 target_size
= thunk_type_size(usbfsurb_arg_type
, THUNK_TARGET
);
4861 memset(buf_temp
, 0, sizeof(uint64_t));
4862 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
4863 if (is_error(ret
)) {
4867 memcpy(&hurb
, buf_temp
, sizeof(uint64_t));
4868 lurb
= (void *)((uintptr_t)hurb
- offsetof(struct live_urb
, host_urb
));
4869 if (!lurb
->target_urb_adr
) {
4870 return -TARGET_EFAULT
;
4872 urb_hashtable_remove(lurb
);
4873 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
,
4874 lurb
->host_urb
.buffer_length
);
4875 lurb
->target_buf_ptr
= NULL
;
4877 /* restore the guest buffer pointer */
4878 lurb
->host_urb
.buffer
= (void *)(uintptr_t)lurb
->target_buf_adr
;
4880 /* update the guest urb struct */
4881 argptr
= lock_user(VERIFY_WRITE
, lurb
->target_urb_adr
, target_size
, 0);
4884 return -TARGET_EFAULT
;
4886 thunk_convert(argptr
, &lurb
->host_urb
, usbfsurb_arg_type
, THUNK_TARGET
);
4887 unlock_user(argptr
, lurb
->target_urb_adr
, target_size
);
4889 target_size
= thunk_type_size(ptrvoid_arg_type
, THUNK_TARGET
);
4890 /* write back the urb handle */
4891 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4894 return -TARGET_EFAULT
;
4897 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
4898 target_urb_adr
= lurb
->target_urb_adr
;
4899 thunk_convert(argptr
, &target_urb_adr
, ptrvoid_arg_type
, THUNK_TARGET
);
4900 unlock_user(argptr
, arg
, target_size
);
4907 do_ioctl_usbdevfs_discardurb(const IOCTLEntry
*ie
,
4908 uint8_t *buf_temp
__attribute__((unused
)),
4909 int fd
, int cmd
, abi_long arg
)
4911 struct live_urb
*lurb
;
4913 /* map target address back to host URB with metadata. */
4914 lurb
= urb_hashtable_lookup(arg
);
4916 return -TARGET_EFAULT
;
4918 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
4922 do_ioctl_usbdevfs_submiturb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4923 int fd
, int cmd
, abi_long arg
)
4925 const argtype
*arg_type
= ie
->arg_type
;
4930 struct live_urb
*lurb
;
4933 * each submitted URB needs to map to a unique ID for the
4934 * kernel, and that unique ID needs to be a pointer to
4935 * host memory. hence, we need to malloc for each URB.
4936 * isochronous transfers have a variable length struct.
4939 target_size
= thunk_type_size(arg_type
, THUNK_TARGET
);
4941 /* construct host copy of urb and metadata */
4942 lurb
= g_try_new0(struct live_urb
, 1);
4944 return -TARGET_ENOMEM
;
4947 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4950 return -TARGET_EFAULT
;
4952 thunk_convert(&lurb
->host_urb
, argptr
, arg_type
, THUNK_HOST
);
4953 unlock_user(argptr
, arg
, 0);
4955 lurb
->target_urb_adr
= arg
;
4956 lurb
->target_buf_adr
= (uintptr_t)lurb
->host_urb
.buffer
;
4958 /* buffer space used depends on endpoint type so lock the entire buffer */
4959 /* control type urbs should check the buffer contents for true direction */
4960 rw_dir
= lurb
->host_urb
.endpoint
& USB_DIR_IN
? VERIFY_WRITE
: VERIFY_READ
;
4961 lurb
->target_buf_ptr
= lock_user(rw_dir
, lurb
->target_buf_adr
,
4962 lurb
->host_urb
.buffer_length
, 1);
4963 if (lurb
->target_buf_ptr
== NULL
) {
4965 return -TARGET_EFAULT
;
4968 /* update buffer pointer in host copy */
4969 lurb
->host_urb
.buffer
= lurb
->target_buf_ptr
;
4971 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
4972 if (is_error(ret
)) {
4973 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
, 0);
4976 urb_hashtable_insert(lurb
);
4981 #endif /* CONFIG_USBFS */
4983 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
4984 int cmd
, abi_long arg
)
4987 struct dm_ioctl
*host_dm
;
4988 abi_long guest_data
;
4989 uint32_t guest_data_size
;
4991 const argtype
*arg_type
= ie
->arg_type
;
4993 void *big_buf
= NULL
;
4997 target_size
= thunk_type_size(arg_type
, 0);
4998 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5000 ret
= -TARGET_EFAULT
;
5003 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5004 unlock_user(argptr
, arg
, 0);
5006 /* buf_temp is too small, so fetch things into a bigger buffer */
5007 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
5008 memcpy(big_buf
, buf_temp
, target_size
);
5012 guest_data
= arg
+ host_dm
->data_start
;
5013 if ((guest_data
- arg
) < 0) {
5014 ret
= -TARGET_EINVAL
;
5017 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5018 host_data
= (char*)host_dm
+ host_dm
->data_start
;
5020 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
5022 ret
= -TARGET_EFAULT
;
5026 switch (ie
->host_cmd
) {
5028 case DM_LIST_DEVICES
:
5031 case DM_DEV_SUSPEND
:
5034 case DM_TABLE_STATUS
:
5035 case DM_TABLE_CLEAR
:
5037 case DM_LIST_VERSIONS
:
5041 case DM_DEV_SET_GEOMETRY
:
5042 /* data contains only strings */
5043 memcpy(host_data
, argptr
, guest_data_size
);
5046 memcpy(host_data
, argptr
, guest_data_size
);
5047 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
5051 void *gspec
= argptr
;
5052 void *cur_data
= host_data
;
5053 const argtype dm_arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5054 int spec_size
= thunk_type_size(dm_arg_type
, 0);
5057 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5058 struct dm_target_spec
*spec
= cur_data
;
5062 thunk_convert(spec
, gspec
, dm_arg_type
, THUNK_HOST
);
5063 slen
= strlen((char*)gspec
+ spec_size
) + 1;
5065 spec
->next
= sizeof(*spec
) + slen
;
5066 strcpy((char*)&spec
[1], gspec
+ spec_size
);
5068 cur_data
+= spec
->next
;
5073 ret
= -TARGET_EINVAL
;
5074 unlock_user(argptr
, guest_data
, 0);
5077 unlock_user(argptr
, guest_data
, 0);
5079 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5080 if (!is_error(ret
)) {
5081 guest_data
= arg
+ host_dm
->data_start
;
5082 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5083 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
5084 switch (ie
->host_cmd
) {
5089 case DM_DEV_SUSPEND
:
5092 case DM_TABLE_CLEAR
:
5094 case DM_DEV_SET_GEOMETRY
:
5095 /* no return data */
5097 case DM_LIST_DEVICES
:
5099 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
5100 uint32_t remaining_data
= guest_data_size
;
5101 void *cur_data
= argptr
;
5102 const argtype dm_arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
5103 int nl_size
= 12; /* can't use thunk_size due to alignment */
5106 uint32_t next
= nl
->next
;
5108 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
5110 if (remaining_data
< nl
->next
) {
5111 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5114 thunk_convert(cur_data
, nl
, dm_arg_type
, THUNK_TARGET
);
5115 strcpy(cur_data
+ nl_size
, nl
->name
);
5116 cur_data
+= nl
->next
;
5117 remaining_data
-= nl
->next
;
5121 nl
= (void*)nl
+ next
;
5126 case DM_TABLE_STATUS
:
5128 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
5129 void *cur_data
= argptr
;
5130 const argtype dm_arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5131 int spec_size
= thunk_type_size(dm_arg_type
, 0);
5134 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5135 uint32_t next
= spec
->next
;
5136 int slen
= strlen((char*)&spec
[1]) + 1;
5137 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
5138 if (guest_data_size
< spec
->next
) {
5139 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5142 thunk_convert(cur_data
, spec
, dm_arg_type
, THUNK_TARGET
);
5143 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
5144 cur_data
= argptr
+ spec
->next
;
5145 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
5151 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
5152 int count
= *(uint32_t*)hdata
;
5153 uint64_t *hdev
= hdata
+ 8;
5154 uint64_t *gdev
= argptr
+ 8;
5157 *(uint32_t*)argptr
= tswap32(count
);
5158 for (i
= 0; i
< count
; i
++) {
5159 *gdev
= tswap64(*hdev
);
5165 case DM_LIST_VERSIONS
:
5167 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
5168 uint32_t remaining_data
= guest_data_size
;
5169 void *cur_data
= argptr
;
5170 const argtype dm_arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
5171 int vers_size
= thunk_type_size(dm_arg_type
, 0);
5174 uint32_t next
= vers
->next
;
5176 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
5178 if (remaining_data
< vers
->next
) {
5179 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5182 thunk_convert(cur_data
, vers
, dm_arg_type
, THUNK_TARGET
);
5183 strcpy(cur_data
+ vers_size
, vers
->name
);
5184 cur_data
+= vers
->next
;
5185 remaining_data
-= vers
->next
;
5189 vers
= (void*)vers
+ next
;
5194 unlock_user(argptr
, guest_data
, 0);
5195 ret
= -TARGET_EINVAL
;
5198 unlock_user(argptr
, guest_data
, guest_data_size
);
5200 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5202 ret
= -TARGET_EFAULT
;
5205 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5206 unlock_user(argptr
, arg
, target_size
);
5213 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5214 int cmd
, abi_long arg
)
5218 const argtype
*arg_type
= ie
->arg_type
;
5219 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5222 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5223 struct blkpg_partition host_part
;
5225 /* Read and convert blkpg */
5227 target_size
= thunk_type_size(arg_type
, 0);
5228 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5230 ret
= -TARGET_EFAULT
;
5233 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5234 unlock_user(argptr
, arg
, 0);
5236 switch (host_blkpg
->op
) {
5237 case BLKPG_ADD_PARTITION
:
5238 case BLKPG_DEL_PARTITION
:
5239 /* payload is struct blkpg_partition */
5242 /* Unknown opcode */
5243 ret
= -TARGET_EINVAL
;
5247 /* Read and convert blkpg->data */
5248 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5249 target_size
= thunk_type_size(part_arg_type
, 0);
5250 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5252 ret
= -TARGET_EFAULT
;
5255 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5256 unlock_user(argptr
, arg
, 0);
5258 /* Swizzle the data pointer to our local copy and call! */
5259 host_blkpg
->data
= &host_part
;
5260 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5266 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5267 int fd
, int cmd
, abi_long arg
)
5269 const argtype
*arg_type
= ie
->arg_type
;
5270 const StructEntry
*se
;
5271 const argtype
*field_types
;
5272 const int *dst_offsets
, *src_offsets
;
5275 abi_ulong
*target_rt_dev_ptr
= NULL
;
5276 unsigned long *host_rt_dev_ptr
= NULL
;
5280 assert(ie
->access
== IOC_W
);
5281 assert(*arg_type
== TYPE_PTR
);
5283 assert(*arg_type
== TYPE_STRUCT
);
5284 target_size
= thunk_type_size(arg_type
, 0);
5285 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5287 return -TARGET_EFAULT
;
5290 assert(*arg_type
== (int)STRUCT_rtentry
);
5291 se
= struct_entries
+ *arg_type
++;
5292 assert(se
->convert
[0] == NULL
);
5293 /* convert struct here to be able to catch rt_dev string */
5294 field_types
= se
->field_types
;
5295 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5296 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5297 for (i
= 0; i
< se
->nb_fields
; i
++) {
5298 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5299 assert(*field_types
== TYPE_PTRVOID
);
5300 target_rt_dev_ptr
= argptr
+ src_offsets
[i
];
5301 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5302 if (*target_rt_dev_ptr
!= 0) {
5303 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5304 tswapal(*target_rt_dev_ptr
));
5305 if (!*host_rt_dev_ptr
) {
5306 unlock_user(argptr
, arg
, 0);
5307 return -TARGET_EFAULT
;
5310 *host_rt_dev_ptr
= 0;
5315 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5316 argptr
+ src_offsets
[i
],
5317 field_types
, THUNK_HOST
);
5319 unlock_user(argptr
, arg
, 0);
5321 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5323 assert(host_rt_dev_ptr
!= NULL
);
5324 assert(target_rt_dev_ptr
!= NULL
);
5325 if (*host_rt_dev_ptr
!= 0) {
5326 unlock_user((void *)*host_rt_dev_ptr
,
5327 *target_rt_dev_ptr
, 0);
5332 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5333 int fd
, int cmd
, abi_long arg
)
5335 int sig
= target_to_host_signal(arg
);
5336 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5339 static abi_long
do_ioctl_SIOCGSTAMP(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5340 int fd
, int cmd
, abi_long arg
)
5345 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMP
, &tv
));
5346 if (is_error(ret
)) {
5350 if (cmd
== (int)TARGET_SIOCGSTAMP_OLD
) {
5351 if (copy_to_user_timeval(arg
, &tv
)) {
5352 return -TARGET_EFAULT
;
5355 if (copy_to_user_timeval64(arg
, &tv
)) {
5356 return -TARGET_EFAULT
;
5363 static abi_long
do_ioctl_SIOCGSTAMPNS(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5364 int fd
, int cmd
, abi_long arg
)
5369 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMPNS
, &ts
));
5370 if (is_error(ret
)) {
5374 if (cmd
== (int)TARGET_SIOCGSTAMPNS_OLD
) {
5375 if (host_to_target_timespec(arg
, &ts
)) {
5376 return -TARGET_EFAULT
;
5379 if (host_to_target_timespec64(arg
, &ts
)) {
5380 return -TARGET_EFAULT
;
5388 static abi_long
do_ioctl_tiocgptpeer(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5389 int fd
, int cmd
, abi_long arg
)
5391 int flags
= target_to_host_bitmask(arg
, fcntl_flags_tbl
);
5392 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, flags
));
5398 static void unlock_drm_version(struct drm_version
*host_ver
,
5399 struct target_drm_version
*target_ver
,
5402 unlock_user(host_ver
->name
, target_ver
->name
,
5403 copy
? host_ver
->name_len
: 0);
5404 unlock_user(host_ver
->date
, target_ver
->date
,
5405 copy
? host_ver
->date_len
: 0);
5406 unlock_user(host_ver
->desc
, target_ver
->desc
,
5407 copy
? host_ver
->desc_len
: 0);
5410 static inline abi_long
target_to_host_drmversion(struct drm_version
*host_ver
,
5411 struct target_drm_version
*target_ver
)
5413 memset(host_ver
, 0, sizeof(*host_ver
));
5415 __get_user(host_ver
->name_len
, &target_ver
->name_len
);
5416 if (host_ver
->name_len
) {
5417 host_ver
->name
= lock_user(VERIFY_WRITE
, target_ver
->name
,
5418 target_ver
->name_len
, 0);
5419 if (!host_ver
->name
) {
5424 __get_user(host_ver
->date_len
, &target_ver
->date_len
);
5425 if (host_ver
->date_len
) {
5426 host_ver
->date
= lock_user(VERIFY_WRITE
, target_ver
->date
,
5427 target_ver
->date_len
, 0);
5428 if (!host_ver
->date
) {
5433 __get_user(host_ver
->desc_len
, &target_ver
->desc_len
);
5434 if (host_ver
->desc_len
) {
5435 host_ver
->desc
= lock_user(VERIFY_WRITE
, target_ver
->desc
,
5436 target_ver
->desc_len
, 0);
5437 if (!host_ver
->desc
) {
5444 unlock_drm_version(host_ver
, target_ver
, false);
5448 static inline void host_to_target_drmversion(
5449 struct target_drm_version
*target_ver
,
5450 struct drm_version
*host_ver
)
5452 __put_user(host_ver
->version_major
, &target_ver
->version_major
);
5453 __put_user(host_ver
->version_minor
, &target_ver
->version_minor
);
5454 __put_user(host_ver
->version_patchlevel
, &target_ver
->version_patchlevel
);
5455 __put_user(host_ver
->name_len
, &target_ver
->name_len
);
5456 __put_user(host_ver
->date_len
, &target_ver
->date_len
);
5457 __put_user(host_ver
->desc_len
, &target_ver
->desc_len
);
5458 unlock_drm_version(host_ver
, target_ver
, true);
5461 static abi_long
do_ioctl_drm(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5462 int fd
, int cmd
, abi_long arg
)
5464 struct drm_version
*ver
;
5465 struct target_drm_version
*target_ver
;
5468 switch (ie
->host_cmd
) {
5469 case DRM_IOCTL_VERSION
:
5470 if (!lock_user_struct(VERIFY_WRITE
, target_ver
, arg
, 0)) {
5471 return -TARGET_EFAULT
;
5473 ver
= (struct drm_version
*)buf_temp
;
5474 ret
= target_to_host_drmversion(ver
, target_ver
);
5475 if (!is_error(ret
)) {
5476 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, ver
));
5477 if (is_error(ret
)) {
5478 unlock_drm_version(ver
, target_ver
, false);
5480 host_to_target_drmversion(target_ver
, ver
);
5483 unlock_user_struct(target_ver
, arg
, 0);
5486 return -TARGET_ENOSYS
;
5489 static abi_long
do_ioctl_drm_i915_getparam(const IOCTLEntry
*ie
,
5490 struct drm_i915_getparam
*gparam
,
5491 int fd
, abi_long arg
)
5495 struct target_drm_i915_getparam
*target_gparam
;
5497 if (!lock_user_struct(VERIFY_READ
, target_gparam
, arg
, 0)) {
5498 return -TARGET_EFAULT
;
5501 __get_user(gparam
->param
, &target_gparam
->param
);
5502 gparam
->value
= &value
;
5503 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, gparam
));
5504 put_user_s32(value
, target_gparam
->value
);
5506 unlock_user_struct(target_gparam
, arg
, 0);
5510 static abi_long
do_ioctl_drm_i915(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5511 int fd
, int cmd
, abi_long arg
)
5513 switch (ie
->host_cmd
) {
5514 case DRM_IOCTL_I915_GETPARAM
:
5515 return do_ioctl_drm_i915_getparam(ie
,
5516 (struct drm_i915_getparam
*)buf_temp
,
5519 return -TARGET_ENOSYS
;
5525 static abi_long
do_ioctl_TUNSETTXFILTER(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5526 int fd
, int cmd
, abi_long arg
)
5528 struct tun_filter
*filter
= (struct tun_filter
*)buf_temp
;
5529 struct tun_filter
*target_filter
;
5532 assert(ie
->access
== IOC_W
);
5534 target_filter
= lock_user(VERIFY_READ
, arg
, sizeof(*target_filter
), 1);
5535 if (!target_filter
) {
5536 return -TARGET_EFAULT
;
5538 filter
->flags
= tswap16(target_filter
->flags
);
5539 filter
->count
= tswap16(target_filter
->count
);
5540 unlock_user(target_filter
, arg
, 0);
5542 if (filter
->count
) {
5543 if (offsetof(struct tun_filter
, addr
) + filter
->count
* ETH_ALEN
>
5545 return -TARGET_EFAULT
;
5548 target_addr
= lock_user(VERIFY_READ
,
5549 arg
+ offsetof(struct tun_filter
, addr
),
5550 filter
->count
* ETH_ALEN
, 1);
5552 return -TARGET_EFAULT
;
5554 memcpy(filter
->addr
, target_addr
, filter
->count
* ETH_ALEN
);
5555 unlock_user(target_addr
, arg
+ offsetof(struct tun_filter
, addr
), 0);
5558 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, filter
));
5561 IOCTLEntry ioctl_entries
[] = {
5562 #define IOCTL(cmd, access, ...) \
5563 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5564 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5565 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5566 #define IOCTL_IGNORE(cmd) \
5567 { TARGET_ ## cmd, 0, #cmd },
5572 /* ??? Implement proper locking for ioctls. */
5573 /* do_ioctl() Must return target values and target errnos. */
5574 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5576 const IOCTLEntry
*ie
;
5577 const argtype
*arg_type
;
5579 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5585 if (ie
->target_cmd
== 0) {
5587 LOG_UNIMP
, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5588 return -TARGET_ENOTTY
;
5590 if (ie
->target_cmd
== cmd
)
5594 arg_type
= ie
->arg_type
;
5596 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5597 } else if (!ie
->host_cmd
) {
5598 /* Some architectures define BSD ioctls in their headers
5599 that are not implemented in Linux. */
5600 return -TARGET_ENOTTY
;
5603 switch(arg_type
[0]) {
5606 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5612 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5616 target_size
= thunk_type_size(arg_type
, 0);
5617 switch(ie
->access
) {
5619 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5620 if (!is_error(ret
)) {
5621 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5623 return -TARGET_EFAULT
;
5624 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5625 unlock_user(argptr
, arg
, target_size
);
5629 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5631 return -TARGET_EFAULT
;
5632 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5633 unlock_user(argptr
, arg
, 0);
5634 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5638 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5640 return -TARGET_EFAULT
;
5641 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5642 unlock_user(argptr
, arg
, 0);
5643 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5644 if (!is_error(ret
)) {
5645 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5647 return -TARGET_EFAULT
;
5648 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5649 unlock_user(argptr
, arg
, target_size
);
5655 qemu_log_mask(LOG_UNIMP
,
5656 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5657 (long)cmd
, arg_type
[0]);
5658 ret
= -TARGET_ENOTTY
;
5664 static const bitmask_transtbl iflag_tbl
[] = {
5665 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5666 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5667 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5668 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5669 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5670 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5671 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5672 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5673 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5674 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5675 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5676 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5677 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5678 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5679 { TARGET_IUTF8
, TARGET_IUTF8
, IUTF8
, IUTF8
},
5682 static const bitmask_transtbl oflag_tbl
[] = {
5683 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5684 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5685 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5686 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5687 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5688 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5689 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5690 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5691 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5692 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5693 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5694 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5695 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5696 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5697 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5698 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5699 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5700 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5701 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5702 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5703 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5704 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5705 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5706 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5709 static const bitmask_transtbl cflag_tbl
[] = {
5710 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5711 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5712 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5713 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5714 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5715 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5716 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5717 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5718 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5719 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5720 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5721 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5722 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5723 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5724 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5725 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5726 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5727 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5728 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5729 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5730 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5731 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5732 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5733 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5734 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5735 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5736 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5737 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5738 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5739 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5740 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5743 static const bitmask_transtbl lflag_tbl
[] = {
5744 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5745 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5746 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5747 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5748 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5749 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5750 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5751 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5752 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5753 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5754 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5755 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5756 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5757 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5758 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5759 { TARGET_EXTPROC
, TARGET_EXTPROC
, EXTPROC
, EXTPROC
},
5762 static void target_to_host_termios (void *dst
, const void *src
)
5764 struct host_termios
*host
= dst
;
5765 const struct target_termios
*target
= src
;
5768 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5770 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5772 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5774 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5775 host
->c_line
= target
->c_line
;
5777 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5778 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5779 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5780 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5781 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5782 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5783 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5784 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5785 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5786 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5787 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5788 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5789 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5790 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5791 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5792 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5793 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5794 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5797 static void host_to_target_termios (void *dst
, const void *src
)
5799 struct target_termios
*target
= dst
;
5800 const struct host_termios
*host
= src
;
5803 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5805 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5807 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5809 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5810 target
->c_line
= host
->c_line
;
5812 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5813 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5814 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5815 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5816 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5817 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5818 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5819 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5820 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5821 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5822 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5823 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5824 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5825 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5826 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5827 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5828 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5829 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5832 static const StructEntry struct_termios_def
= {
5833 .convert
= { host_to_target_termios
, target_to_host_termios
},
5834 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
5835 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
5836 .print
= print_termios
,
5839 /* If the host does not provide these bits, they may be safely discarded. */
5843 #ifndef MAP_UNINITIALIZED
5844 #define MAP_UNINITIALIZED 0
5847 static const bitmask_transtbl mmap_flags_tbl
[] = {
5848 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
5849 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
,
5850 MAP_ANONYMOUS
, MAP_ANONYMOUS
},
5851 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
,
5852 MAP_GROWSDOWN
, MAP_GROWSDOWN
},
5853 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
,
5854 MAP_DENYWRITE
, MAP_DENYWRITE
},
5855 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
,
5856 MAP_EXECUTABLE
, MAP_EXECUTABLE
},
5857 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
5858 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
,
5859 MAP_NORESERVE
, MAP_NORESERVE
},
5860 { TARGET_MAP_HUGETLB
, TARGET_MAP_HUGETLB
, MAP_HUGETLB
, MAP_HUGETLB
},
5861 /* MAP_STACK had been ignored by the kernel for quite some time.
5862 Recognize it for the target insofar as we do not want to pass
5863 it through to the host. */
5864 { TARGET_MAP_STACK
, TARGET_MAP_STACK
, 0, 0 },
5865 { TARGET_MAP_NONBLOCK
, TARGET_MAP_NONBLOCK
, MAP_NONBLOCK
, MAP_NONBLOCK
},
5866 { TARGET_MAP_POPULATE
, TARGET_MAP_POPULATE
, MAP_POPULATE
, MAP_POPULATE
},
5867 { TARGET_MAP_FIXED_NOREPLACE
, TARGET_MAP_FIXED_NOREPLACE
,
5868 MAP_FIXED_NOREPLACE
, MAP_FIXED_NOREPLACE
},
5869 { TARGET_MAP_UNINITIALIZED
, TARGET_MAP_UNINITIALIZED
,
5870 MAP_UNINITIALIZED
, MAP_UNINITIALIZED
},
5874 * Arrange for legacy / undefined architecture specific flags to be
5875 * ignored by mmap handling code.
5877 #ifndef TARGET_MAP_32BIT
5878 #define TARGET_MAP_32BIT 0
5880 #ifndef TARGET_MAP_HUGE_2MB
5881 #define TARGET_MAP_HUGE_2MB 0
5883 #ifndef TARGET_MAP_HUGE_1GB
5884 #define TARGET_MAP_HUGE_1GB 0
5887 static abi_long
do_mmap(abi_ulong addr
, abi_ulong len
, int prot
,
5888 int target_flags
, int fd
, off_t offset
)
5891 * The historical set of flags that all mmap types implicitly support.
5894 TARGET_LEGACY_MAP_MASK
= TARGET_MAP_SHARED
5895 | TARGET_MAP_PRIVATE
5897 | TARGET_MAP_ANONYMOUS
5898 | TARGET_MAP_DENYWRITE
5899 | TARGET_MAP_EXECUTABLE
5900 | TARGET_MAP_UNINITIALIZED
5901 | TARGET_MAP_GROWSDOWN
5903 | TARGET_MAP_NORESERVE
5904 | TARGET_MAP_POPULATE
5905 | TARGET_MAP_NONBLOCK
5907 | TARGET_MAP_HUGETLB
5909 | TARGET_MAP_HUGE_2MB
5910 | TARGET_MAP_HUGE_1GB
5914 switch (target_flags
& TARGET_MAP_TYPE
) {
5915 case TARGET_MAP_PRIVATE
:
5916 host_flags
= MAP_PRIVATE
;
5918 case TARGET_MAP_SHARED
:
5919 host_flags
= MAP_SHARED
;
5921 case TARGET_MAP_SHARED_VALIDATE
:
5923 * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
5924 * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
5926 if (target_flags
& ~(TARGET_LEGACY_MAP_MASK
| TARGET_MAP_SYNC
)) {
5927 return -TARGET_EOPNOTSUPP
;
5929 host_flags
= MAP_SHARED_VALIDATE
;
5930 if (target_flags
& TARGET_MAP_SYNC
) {
5931 host_flags
|= MAP_SYNC
;
5935 return -TARGET_EINVAL
;
5937 host_flags
|= target_to_host_bitmask(target_flags
, mmap_flags_tbl
);
5939 return get_errno(target_mmap(addr
, len
, prot
, host_flags
, fd
, offset
));
5943 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
5944 * TARGET_I386 is defined if TARGET_X86_64 is defined
5946 #if defined(TARGET_I386)
5948 /* NOTE: there is really one LDT for all the threads */
5949 static uint8_t *ldt_table
;
5951 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
5958 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
5959 if (size
> bytecount
)
5961 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
5963 return -TARGET_EFAULT
;
5964 /* ??? Should this by byteswapped? */
5965 memcpy(p
, ldt_table
, size
);
5966 unlock_user(p
, ptr
, size
);
5970 /* XXX: add locking support */
5971 static abi_long
write_ldt(CPUX86State
*env
,
5972 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
5974 struct target_modify_ldt_ldt_s ldt_info
;
5975 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5976 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5977 int seg_not_present
, useable
, lm
;
5978 uint32_t *lp
, entry_1
, entry_2
;
5980 if (bytecount
!= sizeof(ldt_info
))
5981 return -TARGET_EINVAL
;
5982 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
5983 return -TARGET_EFAULT
;
5984 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
5985 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
5986 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
5987 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
5988 unlock_user_struct(target_ldt_info
, ptr
, 0);
5990 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
5991 return -TARGET_EINVAL
;
5992 seg_32bit
= ldt_info
.flags
& 1;
5993 contents
= (ldt_info
.flags
>> 1) & 3;
5994 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
5995 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
5996 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
5997 useable
= (ldt_info
.flags
>> 6) & 1;
6001 lm
= (ldt_info
.flags
>> 7) & 1;
6003 if (contents
== 3) {
6005 return -TARGET_EINVAL
;
6006 if (seg_not_present
== 0)
6007 return -TARGET_EINVAL
;
6009 /* allocate the LDT */
6011 env
->ldt
.base
= target_mmap(0,
6012 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
6013 PROT_READ
|PROT_WRITE
,
6014 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
6015 if (env
->ldt
.base
== -1)
6016 return -TARGET_ENOMEM
;
6017 memset(g2h_untagged(env
->ldt
.base
), 0,
6018 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
6019 env
->ldt
.limit
= 0xffff;
6020 ldt_table
= g2h_untagged(env
->ldt
.base
);
6023 /* NOTE: same code as Linux kernel */
6024 /* Allow LDTs to be cleared by the user. */
6025 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6028 read_exec_only
== 1 &&
6030 limit_in_pages
== 0 &&
6031 seg_not_present
== 1 &&
6039 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6040 (ldt_info
.limit
& 0x0ffff);
6041 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6042 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6043 (ldt_info
.limit
& 0xf0000) |
6044 ((read_exec_only
^ 1) << 9) |
6046 ((seg_not_present
^ 1) << 15) |
6048 (limit_in_pages
<< 23) |
6052 entry_2
|= (useable
<< 20);
6054 /* Install the new entry ... */
6056 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
6057 lp
[0] = tswap32(entry_1
);
6058 lp
[1] = tswap32(entry_2
);
6062 /* specific and weird i386 syscalls */
6063 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
6064 unsigned long bytecount
)
6070 ret
= read_ldt(ptr
, bytecount
);
6073 ret
= write_ldt(env
, ptr
, bytecount
, 1);
6076 ret
= write_ldt(env
, ptr
, bytecount
, 0);
6079 ret
= -TARGET_ENOSYS
;
6085 #if defined(TARGET_ABI32)
6086 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6088 uint64_t *gdt_table
= g2h_untagged(env
->gdt
.base
);
6089 struct target_modify_ldt_ldt_s ldt_info
;
6090 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6091 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6092 int seg_not_present
, useable
, lm
;
6093 uint32_t *lp
, entry_1
, entry_2
;
6096 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6097 if (!target_ldt_info
)
6098 return -TARGET_EFAULT
;
6099 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6100 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6101 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6102 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6103 if (ldt_info
.entry_number
== -1) {
6104 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
6105 if (gdt_table
[i
] == 0) {
6106 ldt_info
.entry_number
= i
;
6107 target_ldt_info
->entry_number
= tswap32(i
);
6112 unlock_user_struct(target_ldt_info
, ptr
, 1);
6114 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
6115 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
6116 return -TARGET_EINVAL
;
6117 seg_32bit
= ldt_info
.flags
& 1;
6118 contents
= (ldt_info
.flags
>> 1) & 3;
6119 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6120 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6121 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6122 useable
= (ldt_info
.flags
>> 6) & 1;
6126 lm
= (ldt_info
.flags
>> 7) & 1;
6129 if (contents
== 3) {
6130 if (seg_not_present
== 0)
6131 return -TARGET_EINVAL
;
6134 /* NOTE: same code as Linux kernel */
6135 /* Allow LDTs to be cleared by the user. */
6136 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6137 if ((contents
== 0 &&
6138 read_exec_only
== 1 &&
6140 limit_in_pages
== 0 &&
6141 seg_not_present
== 1 &&
6149 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6150 (ldt_info
.limit
& 0x0ffff);
6151 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6152 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6153 (ldt_info
.limit
& 0xf0000) |
6154 ((read_exec_only
^ 1) << 9) |
6156 ((seg_not_present
^ 1) << 15) |
6158 (limit_in_pages
<< 23) |
6163 /* Install the new entry ... */
6165 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
6166 lp
[0] = tswap32(entry_1
);
6167 lp
[1] = tswap32(entry_2
);
6171 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6173 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6174 uint64_t *gdt_table
= g2h_untagged(env
->gdt
.base
);
6175 uint32_t base_addr
, limit
, flags
;
6176 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
6177 int seg_not_present
, useable
, lm
;
6178 uint32_t *lp
, entry_1
, entry_2
;
6180 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6181 if (!target_ldt_info
)
6182 return -TARGET_EFAULT
;
6183 idx
= tswap32(target_ldt_info
->entry_number
);
6184 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
6185 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
6186 unlock_user_struct(target_ldt_info
, ptr
, 1);
6187 return -TARGET_EINVAL
;
6189 lp
= (uint32_t *)(gdt_table
+ idx
);
6190 entry_1
= tswap32(lp
[0]);
6191 entry_2
= tswap32(lp
[1]);
6193 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
6194 contents
= (entry_2
>> 10) & 3;
6195 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
6196 seg_32bit
= (entry_2
>> 22) & 1;
6197 limit_in_pages
= (entry_2
>> 23) & 1;
6198 useable
= (entry_2
>> 20) & 1;
6202 lm
= (entry_2
>> 21) & 1;
6204 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
6205 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
6206 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
6207 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
6208 base_addr
= (entry_1
>> 16) |
6209 (entry_2
& 0xff000000) |
6210 ((entry_2
& 0xff) << 16);
6211 target_ldt_info
->base_addr
= tswapal(base_addr
);
6212 target_ldt_info
->limit
= tswap32(limit
);
6213 target_ldt_info
->flags
= tswap32(flags
);
6214 unlock_user_struct(target_ldt_info
, ptr
, 1);
6218 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6220 return -TARGET_ENOSYS
;
6223 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6230 case TARGET_ARCH_SET_GS
:
6231 case TARGET_ARCH_SET_FS
:
6232 if (code
== TARGET_ARCH_SET_GS
)
6236 cpu_x86_load_seg(env
, idx
, 0);
6237 env
->segs
[idx
].base
= addr
;
6239 case TARGET_ARCH_GET_GS
:
6240 case TARGET_ARCH_GET_FS
:
6241 if (code
== TARGET_ARCH_GET_GS
)
6245 val
= env
->segs
[idx
].base
;
6246 if (put_user(val
, addr
, abi_ulong
))
6247 ret
= -TARGET_EFAULT
;
6250 ret
= -TARGET_EINVAL
;
6255 #endif /* defined(TARGET_ABI32 */
6256 #endif /* defined(TARGET_I386) */
6259 * These constants are generic. Supply any that are missing from the host.
6262 # define PR_SET_NAME 15
6263 # define PR_GET_NAME 16
6265 #ifndef PR_SET_FP_MODE
6266 # define PR_SET_FP_MODE 45
6267 # define PR_GET_FP_MODE 46
6268 # define PR_FP_MODE_FR (1 << 0)
6269 # define PR_FP_MODE_FRE (1 << 1)
6271 #ifndef PR_SVE_SET_VL
6272 # define PR_SVE_SET_VL 50
6273 # define PR_SVE_GET_VL 51
6274 # define PR_SVE_VL_LEN_MASK 0xffff
6275 # define PR_SVE_VL_INHERIT (1 << 17)
6277 #ifndef PR_PAC_RESET_KEYS
6278 # define PR_PAC_RESET_KEYS 54
6279 # define PR_PAC_APIAKEY (1 << 0)
6280 # define PR_PAC_APIBKEY (1 << 1)
6281 # define PR_PAC_APDAKEY (1 << 2)
6282 # define PR_PAC_APDBKEY (1 << 3)
6283 # define PR_PAC_APGAKEY (1 << 4)
6285 #ifndef PR_SET_TAGGED_ADDR_CTRL
6286 # define PR_SET_TAGGED_ADDR_CTRL 55
6287 # define PR_GET_TAGGED_ADDR_CTRL 56
6288 # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6290 #ifndef PR_MTE_TCF_SHIFT
6291 # define PR_MTE_TCF_SHIFT 1
6292 # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6293 # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6294 # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6295 # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6296 # define PR_MTE_TAG_SHIFT 3
6297 # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6299 #ifndef PR_SET_IO_FLUSHER
6300 # define PR_SET_IO_FLUSHER 57
6301 # define PR_GET_IO_FLUSHER 58
6303 #ifndef PR_SET_SYSCALL_USER_DISPATCH
6304 # define PR_SET_SYSCALL_USER_DISPATCH 59
6306 #ifndef PR_SME_SET_VL
6307 # define PR_SME_SET_VL 63
6308 # define PR_SME_GET_VL 64
6309 # define PR_SME_VL_LEN_MASK 0xffff
6310 # define PR_SME_VL_INHERIT (1 << 17)
6313 #include "target_prctl.h"
6315 static abi_long
do_prctl_inval0(CPUArchState
*env
)
6317 return -TARGET_EINVAL
;
6320 static abi_long
do_prctl_inval1(CPUArchState
*env
, abi_long arg2
)
6322 return -TARGET_EINVAL
;
6325 #ifndef do_prctl_get_fp_mode
6326 #define do_prctl_get_fp_mode do_prctl_inval0
6328 #ifndef do_prctl_set_fp_mode
6329 #define do_prctl_set_fp_mode do_prctl_inval1
6331 #ifndef do_prctl_sve_get_vl
6332 #define do_prctl_sve_get_vl do_prctl_inval0
6334 #ifndef do_prctl_sve_set_vl
6335 #define do_prctl_sve_set_vl do_prctl_inval1
6337 #ifndef do_prctl_reset_keys
6338 #define do_prctl_reset_keys do_prctl_inval1
6340 #ifndef do_prctl_set_tagged_addr_ctrl
6341 #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6343 #ifndef do_prctl_get_tagged_addr_ctrl
6344 #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6346 #ifndef do_prctl_get_unalign
6347 #define do_prctl_get_unalign do_prctl_inval1
6349 #ifndef do_prctl_set_unalign
6350 #define do_prctl_set_unalign do_prctl_inval1
6352 #ifndef do_prctl_sme_get_vl
6353 #define do_prctl_sme_get_vl do_prctl_inval0
6355 #ifndef do_prctl_sme_set_vl
6356 #define do_prctl_sme_set_vl do_prctl_inval1
6359 static abi_long
do_prctl(CPUArchState
*env
, abi_long option
, abi_long arg2
,
6360 abi_long arg3
, abi_long arg4
, abi_long arg5
)
6365 case PR_GET_PDEATHSIG
:
6368 ret
= get_errno(prctl(PR_GET_PDEATHSIG
, &deathsig
,
6370 if (!is_error(ret
) &&
6371 put_user_s32(host_to_target_signal(deathsig
), arg2
)) {
6372 return -TARGET_EFAULT
;
6376 case PR_SET_PDEATHSIG
:
6377 return get_errno(prctl(PR_SET_PDEATHSIG
, target_to_host_signal(arg2
),
6381 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
6383 return -TARGET_EFAULT
;
6385 ret
= get_errno(prctl(PR_GET_NAME
, (uintptr_t)name
,
6387 unlock_user(name
, arg2
, 16);
6392 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
6394 return -TARGET_EFAULT
;
6396 ret
= get_errno(prctl(PR_SET_NAME
, (uintptr_t)name
,
6398 unlock_user(name
, arg2
, 0);
6401 case PR_GET_FP_MODE
:
6402 return do_prctl_get_fp_mode(env
);
6403 case PR_SET_FP_MODE
:
6404 return do_prctl_set_fp_mode(env
, arg2
);
6406 return do_prctl_sve_get_vl(env
);
6408 return do_prctl_sve_set_vl(env
, arg2
);
6410 return do_prctl_sme_get_vl(env
);
6412 return do_prctl_sme_set_vl(env
, arg2
);
6413 case PR_PAC_RESET_KEYS
:
6414 if (arg3
|| arg4
|| arg5
) {
6415 return -TARGET_EINVAL
;
6417 return do_prctl_reset_keys(env
, arg2
);
6418 case PR_SET_TAGGED_ADDR_CTRL
:
6419 if (arg3
|| arg4
|| arg5
) {
6420 return -TARGET_EINVAL
;
6422 return do_prctl_set_tagged_addr_ctrl(env
, arg2
);
6423 case PR_GET_TAGGED_ADDR_CTRL
:
6424 if (arg2
|| arg3
|| arg4
|| arg5
) {
6425 return -TARGET_EINVAL
;
6427 return do_prctl_get_tagged_addr_ctrl(env
);
6429 case PR_GET_UNALIGN
:
6430 return do_prctl_get_unalign(env
, arg2
);
6431 case PR_SET_UNALIGN
:
6432 return do_prctl_set_unalign(env
, arg2
);
6434 case PR_CAP_AMBIENT
:
6435 case PR_CAPBSET_READ
:
6436 case PR_CAPBSET_DROP
:
6437 case PR_GET_DUMPABLE
:
6438 case PR_SET_DUMPABLE
:
6439 case PR_GET_KEEPCAPS
:
6440 case PR_SET_KEEPCAPS
:
6441 case PR_GET_SECUREBITS
:
6442 case PR_SET_SECUREBITS
:
6445 case PR_GET_TIMERSLACK
:
6446 case PR_SET_TIMERSLACK
:
6448 case PR_MCE_KILL_GET
:
6449 case PR_GET_NO_NEW_PRIVS
:
6450 case PR_SET_NO_NEW_PRIVS
:
6451 case PR_GET_IO_FLUSHER
:
6452 case PR_SET_IO_FLUSHER
:
6453 /* Some prctl options have no pointer arguments and we can pass on. */
6454 return get_errno(prctl(option
, arg2
, arg3
, arg4
, arg5
));
6456 case PR_GET_CHILD_SUBREAPER
:
6457 case PR_SET_CHILD_SUBREAPER
:
6458 case PR_GET_SPECULATION_CTRL
:
6459 case PR_SET_SPECULATION_CTRL
:
6460 case PR_GET_TID_ADDRESS
:
6462 return -TARGET_EINVAL
;
6466 /* Was used for SPE on PowerPC. */
6467 return -TARGET_EINVAL
;
6474 case PR_GET_SECCOMP
:
6475 case PR_SET_SECCOMP
:
6476 case PR_SET_SYSCALL_USER_DISPATCH
:
6477 case PR_GET_THP_DISABLE
:
6478 case PR_SET_THP_DISABLE
:
6481 /* Disable to prevent the target disabling stuff we need. */
6482 return -TARGET_EINVAL
;
6485 qemu_log_mask(LOG_UNIMP
, "Unsupported prctl: " TARGET_ABI_FMT_ld
"\n",
6487 return -TARGET_EINVAL
;
6491 #define NEW_STACK_SIZE 0x40000
6494 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
6497 pthread_mutex_t mutex
;
6498 pthread_cond_t cond
;
6501 abi_ulong child_tidptr
;
6502 abi_ulong parent_tidptr
;
6506 static void *clone_func(void *arg
)
6508 new_thread_info
*info
= arg
;
6513 rcu_register_thread();
6514 tcg_register_thread();
6518 ts
= get_task_state(cpu
);
6519 info
->tid
= sys_gettid();
6521 if (info
->child_tidptr
)
6522 put_user_u32(info
->tid
, info
->child_tidptr
);
6523 if (info
->parent_tidptr
)
6524 put_user_u32(info
->tid
, info
->parent_tidptr
);
6525 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
6526 /* Enable signals. */
6527 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
6528 /* Signal to the parent that we're ready. */
6529 pthread_mutex_lock(&info
->mutex
);
6530 pthread_cond_broadcast(&info
->cond
);
6531 pthread_mutex_unlock(&info
->mutex
);
6532 /* Wait until the parent has finished initializing the tls state. */
6533 pthread_mutex_lock(&clone_lock
);
6534 pthread_mutex_unlock(&clone_lock
);
6540 /* do_fork() Must return host values and target errnos (unlike most
6541 do_*() functions). */
6542 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
6543 abi_ulong parent_tidptr
, target_ulong newtls
,
6544 abi_ulong child_tidptr
)
6546 CPUState
*cpu
= env_cpu(env
);
6550 CPUArchState
*new_env
;
6553 flags
&= ~CLONE_IGNORED_FLAGS
;
6555 /* Emulate vfork() with fork() */
6556 if (flags
& CLONE_VFORK
)
6557 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
6559 if (flags
& CLONE_VM
) {
6560 TaskState
*parent_ts
= get_task_state(cpu
);
6561 new_thread_info info
;
6562 pthread_attr_t attr
;
6564 if (((flags
& CLONE_THREAD_FLAGS
) != CLONE_THREAD_FLAGS
) ||
6565 (flags
& CLONE_INVALID_THREAD_FLAGS
)) {
6566 return -TARGET_EINVAL
;
6569 ts
= g_new0(TaskState
, 1);
6570 init_task_state(ts
);
6572 /* Grab a mutex so that thread setup appears atomic. */
6573 pthread_mutex_lock(&clone_lock
);
6576 * If this is our first additional thread, we need to ensure we
6577 * generate code for parallel execution and flush old translations.
6578 * Do this now so that the copy gets CF_PARALLEL too.
6580 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
6581 cpu
->tcg_cflags
|= CF_PARALLEL
;
6585 /* we create a new CPU instance. */
6586 new_env
= cpu_copy(env
);
6587 /* Init regs that differ from the parent. */
6588 cpu_clone_regs_child(new_env
, newsp
, flags
);
6589 cpu_clone_regs_parent(env
, flags
);
6590 new_cpu
= env_cpu(new_env
);
6591 new_cpu
->opaque
= ts
;
6592 ts
->bprm
= parent_ts
->bprm
;
6593 ts
->info
= parent_ts
->info
;
6594 ts
->signal_mask
= parent_ts
->signal_mask
;
6596 if (flags
& CLONE_CHILD_CLEARTID
) {
6597 ts
->child_tidptr
= child_tidptr
;
6600 if (flags
& CLONE_SETTLS
) {
6601 cpu_set_tls (new_env
, newtls
);
6604 memset(&info
, 0, sizeof(info
));
6605 pthread_mutex_init(&info
.mutex
, NULL
);
6606 pthread_mutex_lock(&info
.mutex
);
6607 pthread_cond_init(&info
.cond
, NULL
);
6609 if (flags
& CLONE_CHILD_SETTID
) {
6610 info
.child_tidptr
= child_tidptr
;
6612 if (flags
& CLONE_PARENT_SETTID
) {
6613 info
.parent_tidptr
= parent_tidptr
;
6616 ret
= pthread_attr_init(&attr
);
6617 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
6618 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
6619 /* It is not safe to deliver signals until the child has finished
6620 initializing, so temporarily block all signals. */
6621 sigfillset(&sigmask
);
6622 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
6623 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
6625 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
6626 /* TODO: Free new CPU state if thread creation failed. */
6628 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
6629 pthread_attr_destroy(&attr
);
6631 /* Wait for the child to initialize. */
6632 pthread_cond_wait(&info
.cond
, &info
.mutex
);
6637 pthread_mutex_unlock(&info
.mutex
);
6638 pthread_cond_destroy(&info
.cond
);
6639 pthread_mutex_destroy(&info
.mutex
);
6640 pthread_mutex_unlock(&clone_lock
);
6642 /* if no CLONE_VM, we consider it is a fork */
6643 if (flags
& CLONE_INVALID_FORK_FLAGS
) {
6644 return -TARGET_EINVAL
;
6647 /* We can't support custom termination signals */
6648 if ((flags
& CSIGNAL
) != TARGET_SIGCHLD
) {
6649 return -TARGET_EINVAL
;
6652 #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
6653 if (flags
& CLONE_PIDFD
) {
6654 return -TARGET_EINVAL
;
6658 /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
6659 if ((flags
& CLONE_PIDFD
) && (flags
& CLONE_PARENT_SETTID
)) {
6660 return -TARGET_EINVAL
;
6663 if (block_signals()) {
6664 return -QEMU_ERESTARTSYS
;
6670 /* Child Process. */
6671 cpu_clone_regs_child(env
, newsp
, flags
);
6673 /* There is a race condition here. The parent process could
6674 theoretically read the TID in the child process before the child
6675 tid is set. This would require using either ptrace
6676 (not implemented) or having *_tidptr to point at a shared memory
6677 mapping. We can't repeat the spinlock hack used above because
6678 the child process gets its own copy of the lock. */
6679 if (flags
& CLONE_CHILD_SETTID
)
6680 put_user_u32(sys_gettid(), child_tidptr
);
6681 if (flags
& CLONE_PARENT_SETTID
)
6682 put_user_u32(sys_gettid(), parent_tidptr
);
6683 ts
= get_task_state(cpu
);
6684 if (flags
& CLONE_SETTLS
)
6685 cpu_set_tls (env
, newtls
);
6686 if (flags
& CLONE_CHILD_CLEARTID
)
6687 ts
->child_tidptr
= child_tidptr
;
6689 cpu_clone_regs_parent(env
, flags
);
6690 if (flags
& CLONE_PIDFD
) {
6692 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
6693 int pid_child
= ret
;
6694 pid_fd
= pidfd_open(pid_child
, 0);
6696 fcntl(pid_fd
, F_SETFD
, fcntl(pid_fd
, F_GETFL
)
6702 put_user_u32(pid_fd
, parent_tidptr
);
6706 g_assert(!cpu_in_exclusive_context(cpu
));
6711 /* warning : doesn't handle linux specific flags... */
6712 static int target_to_host_fcntl_cmd(int cmd
)
6717 case TARGET_F_DUPFD
:
6718 case TARGET_F_GETFD
:
6719 case TARGET_F_SETFD
:
6720 case TARGET_F_GETFL
:
6721 case TARGET_F_SETFL
:
6722 case TARGET_F_OFD_GETLK
:
6723 case TARGET_F_OFD_SETLK
:
6724 case TARGET_F_OFD_SETLKW
:
6727 case TARGET_F_GETLK
:
6730 case TARGET_F_SETLK
:
6733 case TARGET_F_SETLKW
:
6736 case TARGET_F_GETOWN
:
6739 case TARGET_F_SETOWN
:
6742 case TARGET_F_GETSIG
:
6745 case TARGET_F_SETSIG
:
6748 #if TARGET_ABI_BITS == 32
6749 case TARGET_F_GETLK64
:
6752 case TARGET_F_SETLK64
:
6755 case TARGET_F_SETLKW64
:
6759 case TARGET_F_SETLEASE
:
6762 case TARGET_F_GETLEASE
:
6765 #ifdef F_DUPFD_CLOEXEC
6766 case TARGET_F_DUPFD_CLOEXEC
:
6767 ret
= F_DUPFD_CLOEXEC
;
6770 case TARGET_F_NOTIFY
:
6774 case TARGET_F_GETOWN_EX
:
6779 case TARGET_F_SETOWN_EX
:
6784 case TARGET_F_SETPIPE_SZ
:
6787 case TARGET_F_GETPIPE_SZ
:
6792 case TARGET_F_ADD_SEALS
:
6795 case TARGET_F_GET_SEALS
:
6800 ret
= -TARGET_EINVAL
;
6804 #if defined(__powerpc64__)
6805 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6806 * is not supported by kernel. The glibc fcntl call actually adjusts
6807 * them to 5, 6 and 7 before making the syscall(). Since we make the
6808 * syscall directly, adjust to what is supported by the kernel.
6810 if (ret
>= F_GETLK64
&& ret
<= F_SETLKW64
) {
6811 ret
-= F_GETLK64
- 5;
6818 #define FLOCK_TRANSTBL \
6820 TRANSTBL_CONVERT(F_RDLCK); \
6821 TRANSTBL_CONVERT(F_WRLCK); \
6822 TRANSTBL_CONVERT(F_UNLCK); \
6825 static int target_to_host_flock(int type
)
6827 #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6829 #undef TRANSTBL_CONVERT
6830 return -TARGET_EINVAL
;
6833 static int host_to_target_flock(int type
)
6835 #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6837 #undef TRANSTBL_CONVERT
6838 /* if we don't know how to convert the value coming
6839 * from the host we copy to the target field as-is
6844 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
6845 abi_ulong target_flock_addr
)
6847 struct target_flock
*target_fl
;
6850 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6851 return -TARGET_EFAULT
;
6854 __get_user(l_type
, &target_fl
->l_type
);
6855 l_type
= target_to_host_flock(l_type
);
6859 fl
->l_type
= l_type
;
6860 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6861 __get_user(fl
->l_start
, &target_fl
->l_start
);
6862 __get_user(fl
->l_len
, &target_fl
->l_len
);
6863 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6864 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6868 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6869 const struct flock64
*fl
)
6871 struct target_flock
*target_fl
;
6874 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6875 return -TARGET_EFAULT
;
6878 l_type
= host_to_target_flock(fl
->l_type
);
6879 __put_user(l_type
, &target_fl
->l_type
);
6880 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6881 __put_user(fl
->l_start
, &target_fl
->l_start
);
6882 __put_user(fl
->l_len
, &target_fl
->l_len
);
6883 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6884 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6888 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6889 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6891 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6892 struct target_oabi_flock64
{
6900 static inline abi_long
copy_from_user_oabi_flock64(struct flock64
*fl
,
6901 abi_ulong target_flock_addr
)
6903 struct target_oabi_flock64
*target_fl
;
6906 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6907 return -TARGET_EFAULT
;
6910 __get_user(l_type
, &target_fl
->l_type
);
6911 l_type
= target_to_host_flock(l_type
);
6915 fl
->l_type
= l_type
;
6916 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6917 __get_user(fl
->l_start
, &target_fl
->l_start
);
6918 __get_user(fl
->l_len
, &target_fl
->l_len
);
6919 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6920 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6924 static inline abi_long
copy_to_user_oabi_flock64(abi_ulong target_flock_addr
,
6925 const struct flock64
*fl
)
6927 struct target_oabi_flock64
*target_fl
;
6930 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6931 return -TARGET_EFAULT
;
6934 l_type
= host_to_target_flock(fl
->l_type
);
6935 __put_user(l_type
, &target_fl
->l_type
);
6936 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6937 __put_user(fl
->l_start
, &target_fl
->l_start
);
6938 __put_user(fl
->l_len
, &target_fl
->l_len
);
6939 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6940 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6945 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
6946 abi_ulong target_flock_addr
)
6948 struct target_flock64
*target_fl
;
6951 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6952 return -TARGET_EFAULT
;
6955 __get_user(l_type
, &target_fl
->l_type
);
6956 l_type
= target_to_host_flock(l_type
);
6960 fl
->l_type
= l_type
;
6961 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6962 __get_user(fl
->l_start
, &target_fl
->l_start
);
6963 __get_user(fl
->l_len
, &target_fl
->l_len
);
6964 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6965 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6969 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
6970 const struct flock64
*fl
)
6972 struct target_flock64
*target_fl
;
6975 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6976 return -TARGET_EFAULT
;
6979 l_type
= host_to_target_flock(fl
->l_type
);
6980 __put_user(l_type
, &target_fl
->l_type
);
6981 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6982 __put_user(fl
->l_start
, &target_fl
->l_start
);
6983 __put_user(fl
->l_len
, &target_fl
->l_len
);
6984 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6985 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6989 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
6991 struct flock64 fl64
;
6993 struct f_owner_ex fox
;
6994 struct target_f_owner_ex
*target_fox
;
6997 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
6999 if (host_cmd
== -TARGET_EINVAL
)
7003 case TARGET_F_GETLK
:
7004 ret
= copy_from_user_flock(&fl64
, arg
);
7008 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7010 ret
= copy_to_user_flock(arg
, &fl64
);
7014 case TARGET_F_SETLK
:
7015 case TARGET_F_SETLKW
:
7016 ret
= copy_from_user_flock(&fl64
, arg
);
7020 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7023 case TARGET_F_GETLK64
:
7024 case TARGET_F_OFD_GETLK
:
7025 ret
= copy_from_user_flock64(&fl64
, arg
);
7029 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7031 ret
= copy_to_user_flock64(arg
, &fl64
);
7034 case TARGET_F_SETLK64
:
7035 case TARGET_F_SETLKW64
:
7036 case TARGET_F_OFD_SETLK
:
7037 case TARGET_F_OFD_SETLKW
:
7038 ret
= copy_from_user_flock64(&fl64
, arg
);
7042 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7045 case TARGET_F_GETFL
:
7046 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
7048 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
7049 /* tell 32-bit guests it uses largefile on 64-bit hosts: */
7050 if (O_LARGEFILE
== 0 && HOST_LONG_BITS
== 64) {
7051 ret
|= TARGET_O_LARGEFILE
;
7056 case TARGET_F_SETFL
:
7057 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
7058 target_to_host_bitmask(arg
,
7063 case TARGET_F_GETOWN_EX
:
7064 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
7066 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
7067 return -TARGET_EFAULT
;
7068 target_fox
->type
= tswap32(fox
.type
);
7069 target_fox
->pid
= tswap32(fox
.pid
);
7070 unlock_user_struct(target_fox
, arg
, 1);
7076 case TARGET_F_SETOWN_EX
:
7077 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
7078 return -TARGET_EFAULT
;
7079 fox
.type
= tswap32(target_fox
->type
);
7080 fox
.pid
= tswap32(target_fox
->pid
);
7081 unlock_user_struct(target_fox
, arg
, 0);
7082 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
7086 case TARGET_F_SETSIG
:
7087 ret
= get_errno(safe_fcntl(fd
, host_cmd
, target_to_host_signal(arg
)));
7090 case TARGET_F_GETSIG
:
7091 ret
= host_to_target_signal(get_errno(safe_fcntl(fd
, host_cmd
, arg
)));
7094 case TARGET_F_SETOWN
:
7095 case TARGET_F_GETOWN
:
7096 case TARGET_F_SETLEASE
:
7097 case TARGET_F_GETLEASE
:
7098 case TARGET_F_SETPIPE_SZ
:
7099 case TARGET_F_GETPIPE_SZ
:
7100 case TARGET_F_ADD_SEALS
:
7101 case TARGET_F_GET_SEALS
:
7102 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
7106 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
7114 static inline int high2lowuid(int uid
)
7122 static inline int high2lowgid(int gid
)
7130 static inline int low2highuid(int uid
)
7132 if ((int16_t)uid
== -1)
7138 static inline int low2highgid(int gid
)
7140 if ((int16_t)gid
== -1)
7145 static inline int tswapid(int id
)
7150 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7152 #else /* !USE_UID16 */
7153 static inline int high2lowuid(int uid
)
7157 static inline int high2lowgid(int gid
)
7161 static inline int low2highuid(int uid
)
7165 static inline int low2highgid(int gid
)
7169 static inline int tswapid(int id
)
7174 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7176 #endif /* USE_UID16 */
7178 /* We must do direct syscalls for setting UID/GID, because we want to
7179 * implement the Linux system call semantics of "change only for this thread",
7180 * not the libc/POSIX semantics of "change for all threads in process".
7181 * (See http://ewontfix.com/17/ for more details.)
7182 * We use the 32-bit version of the syscalls if present; if it is not
7183 * then either the host architecture supports 32-bit UIDs natively with
7184 * the standard syscall, or the 16-bit UID is the best we can do.
7186 #ifdef __NR_setuid32
7187 #define __NR_sys_setuid __NR_setuid32
7189 #define __NR_sys_setuid __NR_setuid
7191 #ifdef __NR_setgid32
7192 #define __NR_sys_setgid __NR_setgid32
7194 #define __NR_sys_setgid __NR_setgid
7196 #ifdef __NR_setresuid32
7197 #define __NR_sys_setresuid __NR_setresuid32
7199 #define __NR_sys_setresuid __NR_setresuid
7201 #ifdef __NR_setresgid32
7202 #define __NR_sys_setresgid __NR_setresgid32
7204 #define __NR_sys_setresgid __NR_setresgid
7207 _syscall1(int, sys_setuid
, uid_t
, uid
)
7208 _syscall1(int, sys_setgid
, gid_t
, gid
)
7209 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
7210 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
7212 void syscall_init(void)
7215 const argtype
*arg_type
;
7218 thunk_init(STRUCT_MAX
);
7220 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
7221 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
7222 #include "syscall_types.h"
7224 #undef STRUCT_SPECIAL
7226 /* we patch the ioctl size if necessary. We rely on the fact that
7227 no ioctl has all the bits at '1' in the size field */
7229 while (ie
->target_cmd
!= 0) {
7230 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
7231 TARGET_IOC_SIZEMASK
) {
7232 arg_type
= ie
->arg_type
;
7233 if (arg_type
[0] != TYPE_PTR
) {
7234 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
7239 size
= thunk_type_size(arg_type
, 0);
7240 ie
->target_cmd
= (ie
->target_cmd
&
7241 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
7242 (size
<< TARGET_IOC_SIZESHIFT
);
7245 /* automatic consistency check if same arch */
7246 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7247 (defined(__x86_64__) && defined(TARGET_X86_64))
7248 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
7249 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7250 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
7257 #ifdef TARGET_NR_truncate64
7258 static inline abi_long
target_truncate64(CPUArchState
*cpu_env
, const char *arg1
,
7263 if (regpairs_aligned(cpu_env
, TARGET_NR_truncate64
)) {
7267 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
7271 #ifdef TARGET_NR_ftruncate64
7272 static inline abi_long
target_ftruncate64(CPUArchState
*cpu_env
, abi_long arg1
,
7277 if (regpairs_aligned(cpu_env
, TARGET_NR_ftruncate64
)) {
7281 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
7285 #if defined(TARGET_NR_timer_settime) || \
7286 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
7287 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_its
,
7288 abi_ulong target_addr
)
7290 if (target_to_host_timespec(&host_its
->it_interval
, target_addr
+
7291 offsetof(struct target_itimerspec
,
7293 target_to_host_timespec(&host_its
->it_value
, target_addr
+
7294 offsetof(struct target_itimerspec
,
7296 return -TARGET_EFAULT
;
7303 #if defined(TARGET_NR_timer_settime64) || \
7304 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7305 static inline abi_long
target_to_host_itimerspec64(struct itimerspec
*host_its
,
7306 abi_ulong target_addr
)
7308 if (target_to_host_timespec64(&host_its
->it_interval
, target_addr
+
7309 offsetof(struct target__kernel_itimerspec
,
7311 target_to_host_timespec64(&host_its
->it_value
, target_addr
+
7312 offsetof(struct target__kernel_itimerspec
,
7314 return -TARGET_EFAULT
;
7321 #if ((defined(TARGET_NR_timerfd_gettime) || \
7322 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
7323 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
7324 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
7325 struct itimerspec
*host_its
)
7327 if (host_to_target_timespec(target_addr
+ offsetof(struct target_itimerspec
,
7329 &host_its
->it_interval
) ||
7330 host_to_target_timespec(target_addr
+ offsetof(struct target_itimerspec
,
7332 &host_its
->it_value
)) {
7333 return -TARGET_EFAULT
;
7339 #if ((defined(TARGET_NR_timerfd_gettime64) || \
7340 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7341 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7342 static inline abi_long
host_to_target_itimerspec64(abi_ulong target_addr
,
7343 struct itimerspec
*host_its
)
7345 if (host_to_target_timespec64(target_addr
+
7346 offsetof(struct target__kernel_itimerspec
,
7348 &host_its
->it_interval
) ||
7349 host_to_target_timespec64(target_addr
+
7350 offsetof(struct target__kernel_itimerspec
,
7352 &host_its
->it_value
)) {
7353 return -TARGET_EFAULT
;
7359 #if defined(TARGET_NR_adjtimex) || \
7360 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
7361 static inline abi_long
target_to_host_timex(struct timex
*host_tx
,
7362 abi_long target_addr
)
7364 struct target_timex
*target_tx
;
7366 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7367 return -TARGET_EFAULT
;
7370 __get_user(host_tx
->modes
, &target_tx
->modes
);
7371 __get_user(host_tx
->offset
, &target_tx
->offset
);
7372 __get_user(host_tx
->freq
, &target_tx
->freq
);
7373 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7374 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7375 __get_user(host_tx
->status
, &target_tx
->status
);
7376 __get_user(host_tx
->constant
, &target_tx
->constant
);
7377 __get_user(host_tx
->precision
, &target_tx
->precision
);
7378 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7379 __get_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7380 __get_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7381 __get_user(host_tx
->tick
, &target_tx
->tick
);
7382 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7383 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7384 __get_user(host_tx
->shift
, &target_tx
->shift
);
7385 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7386 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7387 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7388 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7389 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7390 __get_user(host_tx
->tai
, &target_tx
->tai
);
7392 unlock_user_struct(target_tx
, target_addr
, 0);
7396 static inline abi_long
host_to_target_timex(abi_long target_addr
,
7397 struct timex
*host_tx
)
7399 struct target_timex
*target_tx
;
7401 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7402 return -TARGET_EFAULT
;
7405 __put_user(host_tx
->modes
, &target_tx
->modes
);
7406 __put_user(host_tx
->offset
, &target_tx
->offset
);
7407 __put_user(host_tx
->freq
, &target_tx
->freq
);
7408 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7409 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7410 __put_user(host_tx
->status
, &target_tx
->status
);
7411 __put_user(host_tx
->constant
, &target_tx
->constant
);
7412 __put_user(host_tx
->precision
, &target_tx
->precision
);
7413 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7414 __put_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7415 __put_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7416 __put_user(host_tx
->tick
, &target_tx
->tick
);
7417 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7418 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7419 __put_user(host_tx
->shift
, &target_tx
->shift
);
7420 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7421 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7422 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7423 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7424 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7425 __put_user(host_tx
->tai
, &target_tx
->tai
);
7427 unlock_user_struct(target_tx
, target_addr
, 1);
7433 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7434 static inline abi_long
target_to_host_timex64(struct timex
*host_tx
,
7435 abi_long target_addr
)
7437 struct target__kernel_timex
*target_tx
;
7439 if (copy_from_user_timeval64(&host_tx
->time
, target_addr
+
7440 offsetof(struct target__kernel_timex
,
7442 return -TARGET_EFAULT
;
7445 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7446 return -TARGET_EFAULT
;
7449 __get_user(host_tx
->modes
, &target_tx
->modes
);
7450 __get_user(host_tx
->offset
, &target_tx
->offset
);
7451 __get_user(host_tx
->freq
, &target_tx
->freq
);
7452 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7453 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7454 __get_user(host_tx
->status
, &target_tx
->status
);
7455 __get_user(host_tx
->constant
, &target_tx
->constant
);
7456 __get_user(host_tx
->precision
, &target_tx
->precision
);
7457 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7458 __get_user(host_tx
->tick
, &target_tx
->tick
);
7459 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7460 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7461 __get_user(host_tx
->shift
, &target_tx
->shift
);
7462 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7463 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7464 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7465 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7466 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7467 __get_user(host_tx
->tai
, &target_tx
->tai
);
7469 unlock_user_struct(target_tx
, target_addr
, 0);
7473 static inline abi_long
host_to_target_timex64(abi_long target_addr
,
7474 struct timex
*host_tx
)
7476 struct target__kernel_timex
*target_tx
;
7478 if (copy_to_user_timeval64(target_addr
+
7479 offsetof(struct target__kernel_timex
, time
),
7481 return -TARGET_EFAULT
;
7484 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7485 return -TARGET_EFAULT
;
7488 __put_user(host_tx
->modes
, &target_tx
->modes
);
7489 __put_user(host_tx
->offset
, &target_tx
->offset
);
7490 __put_user(host_tx
->freq
, &target_tx
->freq
);
7491 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7492 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7493 __put_user(host_tx
->status
, &target_tx
->status
);
7494 __put_user(host_tx
->constant
, &target_tx
->constant
);
7495 __put_user(host_tx
->precision
, &target_tx
->precision
);
7496 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7497 __put_user(host_tx
->tick
, &target_tx
->tick
);
7498 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7499 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7500 __put_user(host_tx
->shift
, &target_tx
->shift
);
7501 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7502 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7503 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7504 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7505 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7506 __put_user(host_tx
->tai
, &target_tx
->tai
);
7508 unlock_user_struct(target_tx
, target_addr
, 1);
7513 #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7514 #define sigev_notify_thread_id _sigev_un._tid
7517 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
7518 abi_ulong target_addr
)
7520 struct target_sigevent
*target_sevp
;
7522 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
7523 return -TARGET_EFAULT
;
7526 /* This union is awkward on 64 bit systems because it has a 32 bit
7527 * integer and a pointer in it; we follow the conversion approach
7528 * used for handling sigval types in signal.c so the guest should get
7529 * the correct value back even if we did a 64 bit byteswap and it's
7530 * using the 32 bit integer.
7532 host_sevp
->sigev_value
.sival_ptr
=
7533 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
7534 host_sevp
->sigev_signo
=
7535 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
7536 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
7537 host_sevp
->sigev_notify_thread_id
= tswap32(target_sevp
->_sigev_un
._tid
);
7539 unlock_user_struct(target_sevp
, target_addr
, 1);
7543 #if defined(TARGET_NR_mlockall)
7544 static inline int target_to_host_mlockall_arg(int arg
)
7548 if (arg
& TARGET_MCL_CURRENT
) {
7549 result
|= MCL_CURRENT
;
7551 if (arg
& TARGET_MCL_FUTURE
) {
7552 result
|= MCL_FUTURE
;
7555 if (arg
& TARGET_MCL_ONFAULT
) {
7556 result
|= MCL_ONFAULT
;
7564 static inline int target_to_host_msync_arg(abi_long arg
)
7566 return ((arg
& TARGET_MS_ASYNC
) ? MS_ASYNC
: 0) |
7567 ((arg
& TARGET_MS_INVALIDATE
) ? MS_INVALIDATE
: 0) |
7568 ((arg
& TARGET_MS_SYNC
) ? MS_SYNC
: 0) |
7569 (arg
& ~(TARGET_MS_ASYNC
| TARGET_MS_INVALIDATE
| TARGET_MS_SYNC
));
7572 #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7573 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7574 defined(TARGET_NR_newfstatat))
7575 static inline abi_long
host_to_target_stat64(CPUArchState
*cpu_env
,
7576 abi_ulong target_addr
,
7577 struct stat
*host_st
)
7579 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
7580 if (cpu_env
->eabi
) {
7581 struct target_eabi_stat64
*target_st
;
7583 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7584 return -TARGET_EFAULT
;
7585 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
7586 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7587 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7588 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7589 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7591 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7592 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7593 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7594 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7595 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7596 __put_user(host_st
->st_size
, &target_st
->st_size
);
7597 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7598 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7599 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7600 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7601 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7602 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7603 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
7604 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
7605 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
7607 unlock_user_struct(target_st
, target_addr
, 1);
7611 #if defined(TARGET_HAS_STRUCT_STAT64)
7612 struct target_stat64
*target_st
;
7614 struct target_stat
*target_st
;
7617 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7618 return -TARGET_EFAULT
;
7619 memset(target_st
, 0, sizeof(*target_st
));
7620 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7621 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7622 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7623 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7625 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7626 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7627 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7628 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7629 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7630 /* XXX: better use of kernel struct */
7631 __put_user(host_st
->st_size
, &target_st
->st_size
);
7632 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7633 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7634 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7635 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7636 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7637 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7638 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
7639 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
7640 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
7642 unlock_user_struct(target_st
, target_addr
, 1);
7649 #if defined(TARGET_NR_statx) && defined(__NR_statx)
7650 static inline abi_long
host_to_target_statx(struct target_statx
*host_stx
,
7651 abi_ulong target_addr
)
7653 struct target_statx
*target_stx
;
7655 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, target_addr
, 0)) {
7656 return -TARGET_EFAULT
;
7658 memset(target_stx
, 0, sizeof(*target_stx
));
7660 __put_user(host_stx
->stx_mask
, &target_stx
->stx_mask
);
7661 __put_user(host_stx
->stx_blksize
, &target_stx
->stx_blksize
);
7662 __put_user(host_stx
->stx_attributes
, &target_stx
->stx_attributes
);
7663 __put_user(host_stx
->stx_nlink
, &target_stx
->stx_nlink
);
7664 __put_user(host_stx
->stx_uid
, &target_stx
->stx_uid
);
7665 __put_user(host_stx
->stx_gid
, &target_stx
->stx_gid
);
7666 __put_user(host_stx
->stx_mode
, &target_stx
->stx_mode
);
7667 __put_user(host_stx
->stx_ino
, &target_stx
->stx_ino
);
7668 __put_user(host_stx
->stx_size
, &target_stx
->stx_size
);
7669 __put_user(host_stx
->stx_blocks
, &target_stx
->stx_blocks
);
7670 __put_user(host_stx
->stx_attributes_mask
, &target_stx
->stx_attributes_mask
);
7671 __put_user(host_stx
->stx_atime
.tv_sec
, &target_stx
->stx_atime
.tv_sec
);
7672 __put_user(host_stx
->stx_atime
.tv_nsec
, &target_stx
->stx_atime
.tv_nsec
);
7673 __put_user(host_stx
->stx_btime
.tv_sec
, &target_stx
->stx_btime
.tv_sec
);
7674 __put_user(host_stx
->stx_btime
.tv_nsec
, &target_stx
->stx_btime
.tv_nsec
);
7675 __put_user(host_stx
->stx_ctime
.tv_sec
, &target_stx
->stx_ctime
.tv_sec
);
7676 __put_user(host_stx
->stx_ctime
.tv_nsec
, &target_stx
->stx_ctime
.tv_nsec
);
7677 __put_user(host_stx
->stx_mtime
.tv_sec
, &target_stx
->stx_mtime
.tv_sec
);
7678 __put_user(host_stx
->stx_mtime
.tv_nsec
, &target_stx
->stx_mtime
.tv_nsec
);
7679 __put_user(host_stx
->stx_rdev_major
, &target_stx
->stx_rdev_major
);
7680 __put_user(host_stx
->stx_rdev_minor
, &target_stx
->stx_rdev_minor
);
7681 __put_user(host_stx
->stx_dev_major
, &target_stx
->stx_dev_major
);
7682 __put_user(host_stx
->stx_dev_minor
, &target_stx
->stx_dev_minor
);
7684 unlock_user_struct(target_stx
, target_addr
, 1);
7690 static int do_sys_futex(int *uaddr
, int op
, int val
,
7691 const struct timespec
*timeout
, int *uaddr2
,
7694 #if HOST_LONG_BITS == 64
7695 #if defined(__NR_futex)
7696 /* always a 64-bit time_t, it doesn't define _time64 version */
7697 return sys_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7700 #else /* HOST_LONG_BITS == 64 */
7701 #if defined(__NR_futex_time64)
7702 if (sizeof(timeout
->tv_sec
) == 8) {
7703 /* _time64 function on 32bit arch */
7704 return sys_futex_time64(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7707 #if defined(__NR_futex)
7708 /* old function on 32bit arch */
7709 return sys_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7711 #endif /* HOST_LONG_BITS == 64 */
7712 g_assert_not_reached();
7715 static int do_safe_futex(int *uaddr
, int op
, int val
,
7716 const struct timespec
*timeout
, int *uaddr2
,
7719 #if HOST_LONG_BITS == 64
7720 #if defined(__NR_futex)
7721 /* always a 64-bit time_t, it doesn't define _time64 version */
7722 return get_errno(safe_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
));
7724 #else /* HOST_LONG_BITS == 64 */
7725 #if defined(__NR_futex_time64)
7726 if (sizeof(timeout
->tv_sec
) == 8) {
7727 /* _time64 function on 32bit arch */
7728 return get_errno(safe_futex_time64(uaddr
, op
, val
, timeout
, uaddr2
,
7732 #if defined(__NR_futex)
7733 /* old function on 32bit arch */
7734 return get_errno(safe_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
));
7736 #endif /* HOST_LONG_BITS == 64 */
7737 return -TARGET_ENOSYS
;
7740 /* ??? Using host futex calls even when target atomic operations
7741 are not really atomic probably breaks things. However implementing
7742 futexes locally would make futexes shared between multiple processes
7743 tricky. However they're probably useless because guest atomic
7744 operations won't work either. */
7745 #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7746 static int do_futex(CPUState
*cpu
, bool time64
, target_ulong uaddr
,
7747 int op
, int val
, target_ulong timeout
,
7748 target_ulong uaddr2
, int val3
)
7750 struct timespec ts
, *pts
= NULL
;
7751 void *haddr2
= NULL
;
7754 /* We assume FUTEX_* constants are the same on both host and target. */
7755 #ifdef FUTEX_CMD_MASK
7756 base_op
= op
& FUTEX_CMD_MASK
;
7762 case FUTEX_WAIT_BITSET
:
7765 case FUTEX_WAIT_REQUEUE_PI
:
7767 haddr2
= g2h(cpu
, uaddr2
);
7770 case FUTEX_LOCK_PI2
:
7773 case FUTEX_WAKE_BITSET
:
7774 case FUTEX_TRYLOCK_PI
:
7775 case FUTEX_UNLOCK_PI
:
7779 val
= target_to_host_signal(val
);
7782 case FUTEX_CMP_REQUEUE
:
7783 case FUTEX_CMP_REQUEUE_PI
:
7784 val3
= tswap32(val3
);
7789 * For these, the 4th argument is not TIMEOUT, but VAL2.
7790 * But the prototype of do_safe_futex takes a pointer, so
7791 * insert casts to satisfy the compiler. We do not need
7792 * to tswap VAL2 since it's not compared to guest memory.
7794 pts
= (struct timespec
*)(uintptr_t)timeout
;
7796 haddr2
= g2h(cpu
, uaddr2
);
7799 return -TARGET_ENOSYS
;
7804 ? target_to_host_timespec64(pts
, timeout
)
7805 : target_to_host_timespec(pts
, timeout
)) {
7806 return -TARGET_EFAULT
;
7809 return do_safe_futex(g2h(cpu
, uaddr
), op
, val
, pts
, haddr2
, val3
);
7813 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7814 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
7815 abi_long handle
, abi_long mount_id
,
7818 struct file_handle
*target_fh
;
7819 struct file_handle
*fh
;
7823 unsigned int size
, total_size
;
7825 if (get_user_s32(size
, handle
)) {
7826 return -TARGET_EFAULT
;
7829 name
= lock_user_string(pathname
);
7831 return -TARGET_EFAULT
;
7834 total_size
= sizeof(struct file_handle
) + size
;
7835 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
7837 unlock_user(name
, pathname
, 0);
7838 return -TARGET_EFAULT
;
7841 fh
= g_malloc0(total_size
);
7842 fh
->handle_bytes
= size
;
7844 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
7845 unlock_user(name
, pathname
, 0);
7847 /* man name_to_handle_at(2):
7848 * Other than the use of the handle_bytes field, the caller should treat
7849 * the file_handle structure as an opaque data type
7852 memcpy(target_fh
, fh
, total_size
);
7853 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
7854 target_fh
->handle_type
= tswap32(fh
->handle_type
);
7856 unlock_user(target_fh
, handle
, total_size
);
7858 if (put_user_s32(mid
, mount_id
)) {
7859 return -TARGET_EFAULT
;
7867 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7868 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
7871 struct file_handle
*target_fh
;
7872 struct file_handle
*fh
;
7873 unsigned int size
, total_size
;
7876 if (get_user_s32(size
, handle
)) {
7877 return -TARGET_EFAULT
;
7880 total_size
= sizeof(struct file_handle
) + size
;
7881 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
7883 return -TARGET_EFAULT
;
7886 fh
= g_memdup(target_fh
, total_size
);
7887 fh
->handle_bytes
= size
;
7888 fh
->handle_type
= tswap32(target_fh
->handle_type
);
7890 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
7891 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
7895 unlock_user(target_fh
, handle
, total_size
);
7901 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7903 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
7906 target_sigset_t
*target_mask
;
7910 if (flags
& ~(TARGET_O_NONBLOCK_MASK
| TARGET_O_CLOEXEC
)) {
7911 return -TARGET_EINVAL
;
7913 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
7914 return -TARGET_EFAULT
;
7917 target_to_host_sigset(&host_mask
, target_mask
);
7919 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
7921 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
7923 fd_trans_register(ret
, &target_signalfd_trans
);
7926 unlock_user_struct(target_mask
, mask
, 0);
7932 /* Map host to target signal numbers for the wait family of syscalls.
7933 Assume all other status bits are the same. */
7934 int host_to_target_waitstatus(int status
)
7936 if (WIFSIGNALED(status
)) {
7937 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
7939 if (WIFSTOPPED(status
)) {
7940 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
7946 static int open_self_cmdline(CPUArchState
*cpu_env
, int fd
)
7948 CPUState
*cpu
= env_cpu(cpu_env
);
7949 struct linux_binprm
*bprm
= get_task_state(cpu
)->bprm
;
7952 for (i
= 0; i
< bprm
->argc
; i
++) {
7953 size_t len
= strlen(bprm
->argv
[i
]) + 1;
7955 if (write(fd
, bprm
->argv
[i
], len
) != len
) {
7963 struct open_self_maps_data
{
7965 IntervalTreeRoot
*host_maps
;
7971 * Subroutine to output one line of /proc/self/maps,
7972 * or one region of /proc/self/smaps.
7976 # define test_stack(S, E, L) (E == L)
7978 # define test_stack(S, E, L) (S == L)
7981 static void open_self_maps_4(const struct open_self_maps_data
*d
,
7982 const MapInfo
*mi
, abi_ptr start
,
7983 abi_ptr end
, unsigned flags
)
7985 const struct image_info
*info
= d
->ts
->info
;
7986 const char *path
= mi
->path
;
7991 if (test_stack(start
, end
, info
->stack_limit
)) {
7993 } else if (start
== info
->brk
) {
7995 } else if (start
== info
->vdso
) {
7997 #ifdef TARGET_X86_64
7998 } else if (start
== TARGET_VSYSCALL_PAGE
) {
7999 path
= "[vsyscall]";
8003 /* Except null device (MAP_ANON), adjust offset for this fragment. */
8004 offset
= mi
->offset
;
8006 uintptr_t hstart
= (uintptr_t)g2h_untagged(start
);
8007 offset
+= hstart
- mi
->itree
.start
;
8010 count
= dprintf(fd
, TARGET_ABI_FMT_ptr
"-" TARGET_ABI_FMT_ptr
8011 " %c%c%c%c %08" PRIx64
" %02x:%02x %"PRId64
,
8013 (flags
& PAGE_READ
) ? 'r' : '-',
8014 (flags
& PAGE_WRITE_ORG
) ? 'w' : '-',
8015 (flags
& PAGE_EXEC
) ? 'x' : '-',
8016 mi
->is_priv
? 'p' : 's',
8017 offset
, major(mi
->dev
), minor(mi
->dev
),
8018 (uint64_t)mi
->inode
);
8020 dprintf(fd
, "%*s%s\n", 73 - count
, "", path
);
8026 unsigned long size
= end
- start
;
8027 unsigned long page_size_kb
= TARGET_PAGE_SIZE
>> 10;
8028 unsigned long size_kb
= size
>> 10;
8030 dprintf(fd
, "Size: %lu kB\n"
8031 "KernelPageSize: %lu kB\n"
8032 "MMUPageSize: %lu kB\n"
8036 "Shared_Clean: 0 kB\n"
8037 "Shared_Dirty: 0 kB\n"
8038 "Private_Clean: 0 kB\n"
8039 "Private_Dirty: 0 kB\n"
8040 "Referenced: 0 kB\n"
8041 "Anonymous: %lu kB\n"
8043 "AnonHugePages: 0 kB\n"
8044 "ShmemPmdMapped: 0 kB\n"
8045 "FilePmdMapped: 0 kB\n"
8046 "Shared_Hugetlb: 0 kB\n"
8047 "Private_Hugetlb: 0 kB\n"
8052 "VmFlags:%s%s%s%s%s%s%s%s\n",
8053 size_kb
, page_size_kb
, page_size_kb
,
8054 (flags
& PAGE_ANON
? size_kb
: 0),
8055 (flags
& PAGE_READ
) ? " rd" : "",
8056 (flags
& PAGE_WRITE_ORG
) ? " wr" : "",
8057 (flags
& PAGE_EXEC
) ? " ex" : "",
8058 mi
->is_priv
? "" : " sh",
8059 (flags
& PAGE_READ
) ? " mr" : "",
8060 (flags
& PAGE_WRITE_ORG
) ? " mw" : "",
8061 (flags
& PAGE_EXEC
) ? " me" : "",
8062 mi
->is_priv
? "" : " ms");
8067 * Callback for walk_memory_regions, when read_self_maps() fails.
8068 * Proceed without the benefit of host /proc/self/maps cross-check.
8070 static int open_self_maps_3(void *opaque
, target_ulong guest_start
,
8071 target_ulong guest_end
, unsigned long flags
)
8073 static const MapInfo mi
= { .is_priv
= true };
8075 open_self_maps_4(opaque
, &mi
, guest_start
, guest_end
, flags
);
8080 * Callback for walk_memory_regions, when read_self_maps() succeeds.
8082 static int open_self_maps_2(void *opaque
, target_ulong guest_start
,
8083 target_ulong guest_end
, unsigned long flags
)
8085 const struct open_self_maps_data
*d
= opaque
;
8086 uintptr_t host_start
= (uintptr_t)g2h_untagged(guest_start
);
8087 uintptr_t host_last
= (uintptr_t)g2h_untagged(guest_end
- 1);
8089 #ifdef TARGET_X86_64
8091 * Because of the extremely high position of the page within the guest
8092 * virtual address space, this is not backed by host memory at all.
8093 * Therefore the loop below would fail. This is the only instance
8094 * of not having host backing memory.
8096 if (guest_start
== TARGET_VSYSCALL_PAGE
) {
8097 return open_self_maps_3(opaque
, guest_start
, guest_end
, flags
);
8102 IntervalTreeNode
*n
=
8103 interval_tree_iter_first(d
->host_maps
, host_start
, host_start
);
8104 MapInfo
*mi
= container_of(n
, MapInfo
, itree
);
8105 uintptr_t this_hlast
= MIN(host_last
, n
->last
);
8106 target_ulong this_gend
= h2g(this_hlast
) + 1;
8108 open_self_maps_4(d
, mi
, guest_start
, this_gend
, flags
);
8110 if (this_hlast
== host_last
) {
8113 host_start
= this_hlast
+ 1;
8114 guest_start
= h2g(host_start
);
8118 static int open_self_maps_1(CPUArchState
*env
, int fd
, bool smaps
)
8120 struct open_self_maps_data d
= {
8121 .ts
= env_cpu(env
)->opaque
,
8122 .host_maps
= read_self_maps(),
8128 walk_memory_regions(&d
, open_self_maps_2
);
8129 free_self_maps(d
.host_maps
);
8131 walk_memory_regions(&d
, open_self_maps_3
);
8136 static int open_self_maps(CPUArchState
*cpu_env
, int fd
)
8138 return open_self_maps_1(cpu_env
, fd
, false);
8141 static int open_self_smaps(CPUArchState
*cpu_env
, int fd
)
8143 return open_self_maps_1(cpu_env
, fd
, true);
8146 static int open_self_stat(CPUArchState
*cpu_env
, int fd
)
8148 CPUState
*cpu
= env_cpu(cpu_env
);
8149 TaskState
*ts
= get_task_state(cpu
);
8150 g_autoptr(GString
) buf
= g_string_new(NULL
);
8153 for (i
= 0; i
< 44; i
++) {
8156 g_string_printf(buf
, FMT_pid
" ", getpid());
8157 } else if (i
== 1) {
8159 gchar
*bin
= g_strrstr(ts
->bprm
->argv
[0], "/");
8160 bin
= bin
? bin
+ 1 : ts
->bprm
->argv
[0];
8161 g_string_printf(buf
, "(%.15s) ", bin
);
8162 } else if (i
== 2) {
8164 g_string_assign(buf
, "R "); /* we are running right now */
8165 } else if (i
== 3) {
8167 g_string_printf(buf
, FMT_pid
" ", getppid());
8168 } else if (i
== 21) {
8170 g_string_printf(buf
, "%" PRIu64
" ", ts
->start_boottime
);
8171 } else if (i
== 27) {
8173 g_string_printf(buf
, TARGET_ABI_FMT_ld
" ", ts
->info
->start_stack
);
8175 /* for the rest, there is MasterCard */
8176 g_string_printf(buf
, "0%c", i
== 43 ? '\n' : ' ');
8179 if (write(fd
, buf
->str
, buf
->len
) != buf
->len
) {
8187 static int open_self_auxv(CPUArchState
*cpu_env
, int fd
)
8189 CPUState
*cpu
= env_cpu(cpu_env
);
8190 TaskState
*ts
= get_task_state(cpu
);
8191 abi_ulong auxv
= ts
->info
->saved_auxv
;
8192 abi_ulong len
= ts
->info
->auxv_len
;
8196 * Auxiliary vector is stored in target process stack.
8197 * read in whole auxv vector and copy it to file
8199 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
8203 r
= write(fd
, ptr
, len
);
8210 lseek(fd
, 0, SEEK_SET
);
8211 unlock_user(ptr
, auxv
, len
);
8217 static int is_proc_myself(const char *filename
, const char *entry
)
8219 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
8220 filename
+= strlen("/proc/");
8221 if (!strncmp(filename
, "self/", strlen("self/"))) {
8222 filename
+= strlen("self/");
8223 } else if (*filename
>= '1' && *filename
<= '9') {
8225 snprintf(myself
, sizeof(myself
), "%d/", getpid());
8226 if (!strncmp(filename
, myself
, strlen(myself
))) {
8227 filename
+= strlen(myself
);
8234 if (!strcmp(filename
, entry
)) {
8241 static void excp_dump_file(FILE *logfile
, CPUArchState
*env
,
8242 const char *fmt
, int code
)
8245 CPUState
*cs
= env_cpu(env
);
8247 fprintf(logfile
, fmt
, code
);
8248 fprintf(logfile
, "Failing executable: %s\n", exec_path
);
8249 cpu_dump_state(cs
, logfile
, 0);
8250 open_self_maps(env
, fileno(logfile
));
8254 void target_exception_dump(CPUArchState
*env
, const char *fmt
, int code
)
8256 /* dump to console */
8257 excp_dump_file(stderr
, env
, fmt
, code
);
8259 /* dump to log file */
8260 if (qemu_log_separate()) {
8261 FILE *logfile
= qemu_log_trylock();
8263 excp_dump_file(logfile
, env
, fmt
, code
);
8264 qemu_log_unlock(logfile
);
8268 #include "target_proc.h"
8270 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
8271 defined(HAVE_ARCH_PROC_CPUINFO) || \
8272 defined(HAVE_ARCH_PROC_HARDWARE)
8273 static int is_proc(const char *filename
, const char *entry
)
8275 return strcmp(filename
, entry
) == 0;
8279 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8280 static int open_net_route(CPUArchState
*cpu_env
, int fd
)
8287 fp
= fopen("/proc/net/route", "r");
8294 read
= getline(&line
, &len
, fp
);
8295 dprintf(fd
, "%s", line
);
8299 while ((read
= getline(&line
, &len
, fp
)) != -1) {
8301 uint32_t dest
, gw
, mask
;
8302 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
8305 fields
= sscanf(line
,
8306 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8307 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
8308 &mask
, &mtu
, &window
, &irtt
);
8312 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8313 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
8314 metric
, tswap32(mask
), mtu
, window
, irtt
);
8324 int do_guest_openat(CPUArchState
*cpu_env
, int dirfd
, const char *fname
,
8325 int flags
, mode_t mode
, bool safe
)
8327 g_autofree
char *proc_name
= NULL
;
8328 const char *pathname
;
8330 const char *filename
;
8331 int (*fill
)(CPUArchState
*cpu_env
, int fd
);
8332 int (*cmp
)(const char *s1
, const char *s2
);
8334 const struct fake_open
*fake_open
;
8335 static const struct fake_open fakes
[] = {
8336 { "maps", open_self_maps
, is_proc_myself
},
8337 { "smaps", open_self_smaps
, is_proc_myself
},
8338 { "stat", open_self_stat
, is_proc_myself
},
8339 { "auxv", open_self_auxv
, is_proc_myself
},
8340 { "cmdline", open_self_cmdline
, is_proc_myself
},
8341 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8342 { "/proc/net/route", open_net_route
, is_proc
},
8344 #if defined(HAVE_ARCH_PROC_CPUINFO)
8345 { "/proc/cpuinfo", open_cpuinfo
, is_proc
},
8347 #if defined(HAVE_ARCH_PROC_HARDWARE)
8348 { "/proc/hardware", open_hardware
, is_proc
},
8350 { NULL
, NULL
, NULL
}
8353 /* if this is a file from /proc/ filesystem, expand full name */
8354 proc_name
= realpath(fname
, NULL
);
8355 if (proc_name
&& strncmp(proc_name
, "/proc/", 6) == 0) {
8356 pathname
= proc_name
;
8361 if (is_proc_myself(pathname
, "exe")) {
8363 return safe_openat(dirfd
, exec_path
, flags
, mode
);
8365 return openat(dirfd
, exec_path
, flags
, mode
);
8369 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
8370 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
8375 if (fake_open
->filename
) {
8377 char filename
[PATH_MAX
];
8380 fd
= memfd_create("qemu-open", 0);
8382 if (errno
!= ENOSYS
) {
8385 /* create temporary file to map stat to */
8386 tmpdir
= getenv("TMPDIR");
8389 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
8390 fd
= mkstemp(filename
);
8397 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
8403 lseek(fd
, 0, SEEK_SET
);
8409 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
8411 return openat(dirfd
, path(pathname
), flags
, mode
);
8415 ssize_t
do_guest_readlink(const char *pathname
, char *buf
, size_t bufsiz
)
8419 if (!pathname
|| !buf
) {
8425 /* Short circuit this for the magic exe check. */
8430 if (is_proc_myself((const char *)pathname
, "exe")) {
8432 * Don't worry about sign mismatch as earlier mapping
8433 * logic would have thrown a bad address error.
8435 ret
= MIN(strlen(exec_path
), bufsiz
);
8436 /* We cannot NUL terminate the string. */
8437 memcpy(buf
, exec_path
, ret
);
8439 ret
= readlink(path(pathname
), buf
, bufsiz
);
8445 static int do_execv(CPUArchState
*cpu_env
, int dirfd
,
8446 abi_long pathname
, abi_long guest_argp
,
8447 abi_long guest_envp
, int flags
, bool is_execveat
)
8450 char **argp
, **envp
;
8459 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
8460 if (get_user_ual(addr
, gp
)) {
8461 return -TARGET_EFAULT
;
8469 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
8470 if (get_user_ual(addr
, gp
)) {
8471 return -TARGET_EFAULT
;
8479 argp
= g_new0(char *, argc
+ 1);
8480 envp
= g_new0(char *, envc
+ 1);
8482 for (gp
= guest_argp
, q
= argp
; gp
; gp
+= sizeof(abi_ulong
), q
++) {
8483 if (get_user_ual(addr
, gp
)) {
8489 *q
= lock_user_string(addr
);
8496 for (gp
= guest_envp
, q
= envp
; gp
; gp
+= sizeof(abi_ulong
), q
++) {
8497 if (get_user_ual(addr
, gp
)) {
8503 *q
= lock_user_string(addr
);
8511 * Although execve() is not an interruptible syscall it is
8512 * a special case where we must use the safe_syscall wrapper:
8513 * if we allow a signal to happen before we make the host
8514 * syscall then we will 'lose' it, because at the point of
8515 * execve the process leaves QEMU's control. So we use the
8516 * safe syscall wrapper to ensure that we either take the
8517 * signal as a guest signal, or else it does not happen
8518 * before the execve completes and makes it the other
8519 * program's problem.
8521 p
= lock_user_string(pathname
);
8526 const char *exe
= p
;
8527 if (is_proc_myself(p
, "exe")) {
8531 ? safe_execveat(dirfd
, exe
, argp
, envp
, flags
)
8532 : safe_execve(exe
, argp
, envp
);
8533 ret
= get_errno(ret
);
8535 unlock_user(p
, pathname
, 0);
8540 ret
= -TARGET_EFAULT
;
8543 for (gp
= guest_argp
, q
= argp
; *q
; gp
+= sizeof(abi_ulong
), q
++) {
8544 if (get_user_ual(addr
, gp
) || !addr
) {
8547 unlock_user(*q
, addr
, 0);
8549 for (gp
= guest_envp
, q
= envp
; *q
; gp
+= sizeof(abi_ulong
), q
++) {
8550 if (get_user_ual(addr
, gp
) || !addr
) {
8553 unlock_user(*q
, addr
, 0);
8561 #define TIMER_MAGIC 0x0caf0000
8562 #define TIMER_MAGIC_MASK 0xffff0000
8564 /* Convert QEMU provided timer ID back to internal 16bit index format */
8565 static target_timer_t
get_timer_id(abi_long arg
)
8567 target_timer_t timerid
= arg
;
8569 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
8570 return -TARGET_EINVAL
;
8575 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
8576 return -TARGET_EINVAL
;
8582 static int target_to_host_cpu_mask(unsigned long *host_mask
,
8584 abi_ulong target_addr
,
8587 unsigned target_bits
= sizeof(abi_ulong
) * 8;
8588 unsigned host_bits
= sizeof(*host_mask
) * 8;
8589 abi_ulong
*target_mask
;
8592 assert(host_size
>= target_size
);
8594 target_mask
= lock_user(VERIFY_READ
, target_addr
, target_size
, 1);
8596 return -TARGET_EFAULT
;
8598 memset(host_mask
, 0, host_size
);
8600 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
8601 unsigned bit
= i
* target_bits
;
8604 __get_user(val
, &target_mask
[i
]);
8605 for (j
= 0; j
< target_bits
; j
++, bit
++) {
8606 if (val
& (1UL << j
)) {
8607 host_mask
[bit
/ host_bits
] |= 1UL << (bit
% host_bits
);
8612 unlock_user(target_mask
, target_addr
, 0);
8616 static int host_to_target_cpu_mask(const unsigned long *host_mask
,
8618 abi_ulong target_addr
,
8621 unsigned target_bits
= sizeof(abi_ulong
) * 8;
8622 unsigned host_bits
= sizeof(*host_mask
) * 8;
8623 abi_ulong
*target_mask
;
8626 assert(host_size
>= target_size
);
8628 target_mask
= lock_user(VERIFY_WRITE
, target_addr
, target_size
, 0);
8630 return -TARGET_EFAULT
;
8633 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
8634 unsigned bit
= i
* target_bits
;
8637 for (j
= 0; j
< target_bits
; j
++, bit
++) {
8638 if (host_mask
[bit
/ host_bits
] & (1UL << (bit
% host_bits
))) {
8642 __put_user(val
, &target_mask
[i
]);
8645 unlock_user(target_mask
, target_addr
, target_size
);
8649 #ifdef TARGET_NR_getdents
8650 static int do_getdents(abi_long dirfd
, abi_long arg2
, abi_long count
)
8652 g_autofree
void *hdirp
= NULL
;
8654 int hlen
, hoff
, toff
;
8655 int hreclen
, treclen
;
8656 off64_t prev_diroff
= 0;
8658 hdirp
= g_try_malloc(count
);
8660 return -TARGET_ENOMEM
;
8663 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8664 hlen
= sys_getdents(dirfd
, hdirp
, count
);
8666 hlen
= sys_getdents64(dirfd
, hdirp
, count
);
8669 hlen
= get_errno(hlen
);
8670 if (is_error(hlen
)) {
8674 tdirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8676 return -TARGET_EFAULT
;
8679 for (hoff
= toff
= 0; hoff
< hlen
; hoff
+= hreclen
, toff
+= treclen
) {
8680 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8681 struct linux_dirent
*hde
= hdirp
+ hoff
;
8683 struct linux_dirent64
*hde
= hdirp
+ hoff
;
8685 struct target_dirent
*tde
= tdirp
+ toff
;
8689 namelen
= strlen(hde
->d_name
);
8690 hreclen
= hde
->d_reclen
;
8691 treclen
= offsetof(struct target_dirent
, d_name
) + namelen
+ 2;
8692 treclen
= QEMU_ALIGN_UP(treclen
, __alignof(struct target_dirent
));
8694 if (toff
+ treclen
> count
) {
8696 * If the host struct is smaller than the target struct, or
8697 * requires less alignment and thus packs into less space,
8698 * then the host can return more entries than we can pass
8702 toff
= -TARGET_EINVAL
; /* result buffer is too small */
8706 * Return what we have, resetting the file pointer to the
8707 * location of the first record not returned.
8709 lseek64(dirfd
, prev_diroff
, SEEK_SET
);
8713 prev_diroff
= hde
->d_off
;
8714 tde
->d_ino
= tswapal(hde
->d_ino
);
8715 tde
->d_off
= tswapal(hde
->d_off
);
8716 tde
->d_reclen
= tswap16(treclen
);
8717 memcpy(tde
->d_name
, hde
->d_name
, namelen
+ 1);
8720 * The getdents type is in what was formerly a padding byte at the
8721 * end of the structure.
8723 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8724 type
= *((uint8_t *)hde
+ hreclen
- 1);
8728 *((uint8_t *)tde
+ treclen
- 1) = type
;
8731 unlock_user(tdirp
, arg2
, toff
);
8734 #endif /* TARGET_NR_getdents */
8736 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
8737 static int do_getdents64(abi_long dirfd
, abi_long arg2
, abi_long count
)
8739 g_autofree
void *hdirp
= NULL
;
8741 int hlen
, hoff
, toff
;
8742 int hreclen
, treclen
;
8743 off64_t prev_diroff
= 0;
8745 hdirp
= g_try_malloc(count
);
8747 return -TARGET_ENOMEM
;
8750 hlen
= get_errno(sys_getdents64(dirfd
, hdirp
, count
));
8751 if (is_error(hlen
)) {
8755 tdirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8757 return -TARGET_EFAULT
;
8760 for (hoff
= toff
= 0; hoff
< hlen
; hoff
+= hreclen
, toff
+= treclen
) {
8761 struct linux_dirent64
*hde
= hdirp
+ hoff
;
8762 struct target_dirent64
*tde
= tdirp
+ toff
;
8765 namelen
= strlen(hde
->d_name
) + 1;
8766 hreclen
= hde
->d_reclen
;
8767 treclen
= offsetof(struct target_dirent64
, d_name
) + namelen
;
8768 treclen
= QEMU_ALIGN_UP(treclen
, __alignof(struct target_dirent64
));
8770 if (toff
+ treclen
> count
) {
8772 * If the host struct is smaller than the target struct, or
8773 * requires less alignment and thus packs into less space,
8774 * then the host can return more entries than we can pass
8778 toff
= -TARGET_EINVAL
; /* result buffer is too small */
8782 * Return what we have, resetting the file pointer to the
8783 * location of the first record not returned.
8785 lseek64(dirfd
, prev_diroff
, SEEK_SET
);
8789 prev_diroff
= hde
->d_off
;
8790 tde
->d_ino
= tswap64(hde
->d_ino
);
8791 tde
->d_off
= tswap64(hde
->d_off
);
8792 tde
->d_reclen
= tswap16(treclen
);
8793 tde
->d_type
= hde
->d_type
;
8794 memcpy(tde
->d_name
, hde
->d_name
, namelen
);
8797 unlock_user(tdirp
, arg2
, toff
);
8800 #endif /* TARGET_NR_getdents64 */
8802 #if defined(TARGET_NR_riscv_hwprobe)
8804 #define RISCV_HWPROBE_KEY_MVENDORID 0
8805 #define RISCV_HWPROBE_KEY_MARCHID 1
8806 #define RISCV_HWPROBE_KEY_MIMPID 2
8808 #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
8809 #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
8811 #define RISCV_HWPROBE_KEY_IMA_EXT_0 4
8812 #define RISCV_HWPROBE_IMA_FD (1 << 0)
8813 #define RISCV_HWPROBE_IMA_C (1 << 1)
8814 #define RISCV_HWPROBE_IMA_V (1 << 2)
8815 #define RISCV_HWPROBE_EXT_ZBA (1 << 3)
8816 #define RISCV_HWPROBE_EXT_ZBB (1 << 4)
8817 #define RISCV_HWPROBE_EXT_ZBS (1 << 5)
8819 #define RISCV_HWPROBE_KEY_CPUPERF_0 5
8820 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
8821 #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
8822 #define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
8823 #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
8824 #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
8825 #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
8827 #define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6
8829 struct riscv_hwprobe
{
8834 static void risc_hwprobe_fill_pairs(CPURISCVState
*env
,
8835 struct riscv_hwprobe
*pair
,
8838 const RISCVCPUConfig
*cfg
= riscv_cpu_cfg(env
);
8840 for (; pair_count
> 0; pair_count
--, pair
++) {
8843 __put_user(0, &pair
->value
);
8844 __get_user(key
, &pair
->key
);
8846 case RISCV_HWPROBE_KEY_MVENDORID
:
8847 __put_user(cfg
->mvendorid
, &pair
->value
);
8849 case RISCV_HWPROBE_KEY_MARCHID
:
8850 __put_user(cfg
->marchid
, &pair
->value
);
8852 case RISCV_HWPROBE_KEY_MIMPID
:
8853 __put_user(cfg
->mimpid
, &pair
->value
);
8855 case RISCV_HWPROBE_KEY_BASE_BEHAVIOR
:
8856 value
= riscv_has_ext(env
, RVI
) &&
8857 riscv_has_ext(env
, RVM
) &&
8858 riscv_has_ext(env
, RVA
) ?
8859 RISCV_HWPROBE_BASE_BEHAVIOR_IMA
: 0;
8860 __put_user(value
, &pair
->value
);
8862 case RISCV_HWPROBE_KEY_IMA_EXT_0
:
8863 value
= riscv_has_ext(env
, RVF
) &&
8864 riscv_has_ext(env
, RVD
) ?
8865 RISCV_HWPROBE_IMA_FD
: 0;
8866 value
|= riscv_has_ext(env
, RVC
) ?
8867 RISCV_HWPROBE_IMA_C
: 0;
8868 value
|= riscv_has_ext(env
, RVV
) ?
8869 RISCV_HWPROBE_IMA_V
: 0;
8870 value
|= cfg
->ext_zba
?
8871 RISCV_HWPROBE_EXT_ZBA
: 0;
8872 value
|= cfg
->ext_zbb
?
8873 RISCV_HWPROBE_EXT_ZBB
: 0;
8874 value
|= cfg
->ext_zbs
?
8875 RISCV_HWPROBE_EXT_ZBS
: 0;
8876 __put_user(value
, &pair
->value
);
8878 case RISCV_HWPROBE_KEY_CPUPERF_0
:
8879 __put_user(RISCV_HWPROBE_MISALIGNED_FAST
, &pair
->value
);
8881 case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE
:
8882 value
= cfg
->ext_zicboz
? cfg
->cboz_blocksize
: 0;
8883 __put_user(value
, &pair
->value
);
8886 __put_user(-1, &pair
->key
);
8892 static int cpu_set_valid(abi_long arg3
, abi_long arg4
)
8895 size_t host_mask_size
, target_mask_size
;
8896 unsigned long *host_mask
;
8899 * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
8900 * arg3 contains the cpu count.
8902 tmp
= (8 * sizeof(abi_ulong
));
8903 target_mask_size
= ((arg3
+ tmp
- 1) / tmp
) * sizeof(abi_ulong
);
8904 host_mask_size
= (target_mask_size
+ (sizeof(*host_mask
) - 1)) &
8905 ~(sizeof(*host_mask
) - 1);
8907 host_mask
= alloca(host_mask_size
);
8909 ret
= target_to_host_cpu_mask(host_mask
, host_mask_size
,
8910 arg4
, target_mask_size
);
8915 for (i
= 0 ; i
< host_mask_size
/ sizeof(*host_mask
); i
++) {
8916 if (host_mask
[i
] != 0) {
8920 return -TARGET_EINVAL
;
8923 static abi_long
do_riscv_hwprobe(CPUArchState
*cpu_env
, abi_long arg1
,
8924 abi_long arg2
, abi_long arg3
,
8925 abi_long arg4
, abi_long arg5
)
8928 struct riscv_hwprobe
*host_pairs
;
8930 /* flags must be 0 */
8932 return -TARGET_EINVAL
;
8937 ret
= cpu_set_valid(arg3
, arg4
);
8941 } else if (arg4
!= 0) {
8942 return -TARGET_EINVAL
;
8950 host_pairs
= lock_user(VERIFY_WRITE
, arg1
,
8951 sizeof(*host_pairs
) * (size_t)arg2
, 0);
8952 if (host_pairs
== NULL
) {
8953 return -TARGET_EFAULT
;
8955 risc_hwprobe_fill_pairs(cpu_env
, host_pairs
, arg2
);
8956 unlock_user(host_pairs
, arg1
, sizeof(*host_pairs
) * (size_t)arg2
);
8959 #endif /* TARGET_NR_riscv_hwprobe */
8961 #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
8962 _syscall2(int, pivot_root
, const char *, new_root
, const char *, put_old
)
8965 #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
8966 #define __NR_sys_open_tree __NR_open_tree
8967 _syscall3(int, sys_open_tree
, int, __dfd
, const char *, __filename
,
8968 unsigned int, __flags
)
8971 #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
8972 #define __NR_sys_move_mount __NR_move_mount
8973 _syscall5(int, sys_move_mount
, int, __from_dfd
, const char *, __from_pathname
,
8974 int, __to_dfd
, const char *, __to_pathname
, unsigned int, flag
)
8977 /* This is an internal helper for do_syscall so that it is easier
8978 * to have a single return point, so that actions, such as logging
8979 * of syscall results, can be performed.
8980 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
8982 static abi_long
do_syscall1(CPUArchState
*cpu_env
, int num
, abi_long arg1
,
8983 abi_long arg2
, abi_long arg3
, abi_long arg4
,
8984 abi_long arg5
, abi_long arg6
, abi_long arg7
,
8987 CPUState
*cpu
= env_cpu(cpu_env
);
8989 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
8990 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
8991 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
8992 || defined(TARGET_NR_statx)
8995 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
8996 || defined(TARGET_NR_fstatfs)
9002 case TARGET_NR_exit
:
9003 /* In old applications this may be used to implement _exit(2).
9004 However in threaded applications it is used for thread termination,
9005 and _exit_group is used for application termination.
9006 Do thread termination if we have more then one thread. */
9008 if (block_signals()) {
9009 return -QEMU_ERESTARTSYS
;
9012 pthread_mutex_lock(&clone_lock
);
9014 if (CPU_NEXT(first_cpu
)) {
9015 TaskState
*ts
= get_task_state(cpu
);
9017 if (ts
->child_tidptr
) {
9018 put_user_u32(0, ts
->child_tidptr
);
9019 do_sys_futex(g2h(cpu
, ts
->child_tidptr
),
9020 FUTEX_WAKE
, INT_MAX
, NULL
, NULL
, 0);
9023 object_unparent(OBJECT(cpu
));
9024 object_unref(OBJECT(cpu
));
9026 * At this point the CPU should be unrealized and removed
9027 * from cpu lists. We can clean-up the rest of the thread
9028 * data without the lock held.
9031 pthread_mutex_unlock(&clone_lock
);
9035 rcu_unregister_thread();
9039 pthread_mutex_unlock(&clone_lock
);
9040 preexit_cleanup(cpu_env
, arg1
);
9042 return 0; /* avoid warning */
9043 case TARGET_NR_read
:
9044 if (arg2
== 0 && arg3
== 0) {
9045 return get_errno(safe_read(arg1
, 0, 0));
9047 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
9048 return -TARGET_EFAULT
;
9049 ret
= get_errno(safe_read(arg1
, p
, arg3
));
9051 fd_trans_host_to_target_data(arg1
)) {
9052 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
9054 unlock_user(p
, arg2
, ret
);
9057 case TARGET_NR_write
:
9058 if (arg2
== 0 && arg3
== 0) {
9059 return get_errno(safe_write(arg1
, 0, 0));
9061 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
9062 return -TARGET_EFAULT
;
9063 if (fd_trans_target_to_host_data(arg1
)) {
9064 void *copy
= g_malloc(arg3
);
9065 memcpy(copy
, p
, arg3
);
9066 ret
= fd_trans_target_to_host_data(arg1
)(copy
, arg3
);
9068 ret
= get_errno(safe_write(arg1
, copy
, ret
));
9072 ret
= get_errno(safe_write(arg1
, p
, arg3
));
9074 unlock_user(p
, arg2
, 0);
9077 #ifdef TARGET_NR_open
9078 case TARGET_NR_open
:
9079 if (!(p
= lock_user_string(arg1
)))
9080 return -TARGET_EFAULT
;
9081 ret
= get_errno(do_guest_openat(cpu_env
, AT_FDCWD
, p
,
9082 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
9084 fd_trans_unregister(ret
);
9085 unlock_user(p
, arg1
, 0);
9088 case TARGET_NR_openat
:
9089 if (!(p
= lock_user_string(arg2
)))
9090 return -TARGET_EFAULT
;
9091 ret
= get_errno(do_guest_openat(cpu_env
, arg1
, p
,
9092 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
9094 fd_trans_unregister(ret
);
9095 unlock_user(p
, arg2
, 0);
9097 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9098 case TARGET_NR_name_to_handle_at
:
9099 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
9102 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9103 case TARGET_NR_open_by_handle_at
:
9104 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
9105 fd_trans_unregister(ret
);
9108 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
9109 case TARGET_NR_pidfd_open
:
9110 return get_errno(pidfd_open(arg1
, arg2
));
9112 #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
9113 case TARGET_NR_pidfd_send_signal
:
9115 siginfo_t uinfo
, *puinfo
;
9118 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
9120 return -TARGET_EFAULT
;
9122 target_to_host_siginfo(&uinfo
, p
);
9123 unlock_user(p
, arg3
, 0);
9128 ret
= get_errno(pidfd_send_signal(arg1
, target_to_host_signal(arg2
),
9133 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
9134 case TARGET_NR_pidfd_getfd
:
9135 return get_errno(pidfd_getfd(arg1
, arg2
, arg3
));
9137 case TARGET_NR_close
:
9138 fd_trans_unregister(arg1
);
9139 return get_errno(close(arg1
));
9140 #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
9141 case TARGET_NR_close_range
:
9142 ret
= get_errno(sys_close_range(arg1
, arg2
, arg3
));
9143 if (ret
== 0 && !(arg3
& CLOSE_RANGE_CLOEXEC
)) {
9145 maxfd
= MIN(arg2
, target_fd_max
);
9146 for (fd
= arg1
; fd
< maxfd
; fd
++) {
9147 fd_trans_unregister(fd
);
9154 return do_brk(arg1
);
9155 #ifdef TARGET_NR_fork
9156 case TARGET_NR_fork
:
9157 return get_errno(do_fork(cpu_env
, TARGET_SIGCHLD
, 0, 0, 0, 0));
9159 #ifdef TARGET_NR_waitpid
9160 case TARGET_NR_waitpid
:
9163 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
9164 if (!is_error(ret
) && arg2
&& ret
9165 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
9166 return -TARGET_EFAULT
;
9170 #ifdef TARGET_NR_waitid
9171 case TARGET_NR_waitid
:
9175 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
9176 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
9177 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
9178 return -TARGET_EFAULT
;
9179 host_to_target_siginfo(p
, &info
);
9180 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
9185 #ifdef TARGET_NR_creat /* not on alpha */
9186 case TARGET_NR_creat
:
9187 if (!(p
= lock_user_string(arg1
)))
9188 return -TARGET_EFAULT
;
9189 ret
= get_errno(creat(p
, arg2
));
9190 fd_trans_unregister(ret
);
9191 unlock_user(p
, arg1
, 0);
9194 #ifdef TARGET_NR_link
9195 case TARGET_NR_link
:
9198 p
= lock_user_string(arg1
);
9199 p2
= lock_user_string(arg2
);
9201 ret
= -TARGET_EFAULT
;
9203 ret
= get_errno(link(p
, p2
));
9204 unlock_user(p2
, arg2
, 0);
9205 unlock_user(p
, arg1
, 0);
9209 #if defined(TARGET_NR_linkat)
9210 case TARGET_NR_linkat
:
9214 return -TARGET_EFAULT
;
9215 p
= lock_user_string(arg2
);
9216 p2
= lock_user_string(arg4
);
9218 ret
= -TARGET_EFAULT
;
9220 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
9221 unlock_user(p
, arg2
, 0);
9222 unlock_user(p2
, arg4
, 0);
9226 #ifdef TARGET_NR_unlink
9227 case TARGET_NR_unlink
:
9228 if (!(p
= lock_user_string(arg1
)))
9229 return -TARGET_EFAULT
;
9230 ret
= get_errno(unlink(p
));
9231 unlock_user(p
, arg1
, 0);
9234 #if defined(TARGET_NR_unlinkat)
9235 case TARGET_NR_unlinkat
:
9236 if (!(p
= lock_user_string(arg2
)))
9237 return -TARGET_EFAULT
;
9238 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
9239 unlock_user(p
, arg2
, 0);
9242 case TARGET_NR_execveat
:
9243 return do_execv(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, true);
9244 case TARGET_NR_execve
:
9245 return do_execv(cpu_env
, AT_FDCWD
, arg1
, arg2
, arg3
, 0, false);
9246 case TARGET_NR_chdir
:
9247 if (!(p
= lock_user_string(arg1
)))
9248 return -TARGET_EFAULT
;
9249 ret
= get_errno(chdir(p
));
9250 unlock_user(p
, arg1
, 0);
9252 #ifdef TARGET_NR_time
9253 case TARGET_NR_time
:
9256 ret
= get_errno(time(&host_time
));
9259 && put_user_sal(host_time
, arg1
))
9260 return -TARGET_EFAULT
;
9264 #ifdef TARGET_NR_mknod
9265 case TARGET_NR_mknod
:
9266 if (!(p
= lock_user_string(arg1
)))
9267 return -TARGET_EFAULT
;
9268 ret
= get_errno(mknod(p
, arg2
, arg3
));
9269 unlock_user(p
, arg1
, 0);
9272 #if defined(TARGET_NR_mknodat)
9273 case TARGET_NR_mknodat
:
9274 if (!(p
= lock_user_string(arg2
)))
9275 return -TARGET_EFAULT
;
9276 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
9277 unlock_user(p
, arg2
, 0);
9280 #ifdef TARGET_NR_chmod
9281 case TARGET_NR_chmod
:
9282 if (!(p
= lock_user_string(arg1
)))
9283 return -TARGET_EFAULT
;
9284 ret
= get_errno(chmod(p
, arg2
));
9285 unlock_user(p
, arg1
, 0);
9288 #ifdef TARGET_NR_lseek
9289 case TARGET_NR_lseek
:
9290 return get_errno(lseek(arg1
, arg2
, arg3
));
9292 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
9293 /* Alpha specific */
9294 case TARGET_NR_getxpid
:
9295 cpu_env
->ir
[IR_A4
] = getppid();
9296 return get_errno(getpid());
9298 #ifdef TARGET_NR_getpid
9299 case TARGET_NR_getpid
:
9300 return get_errno(getpid());
9302 case TARGET_NR_mount
:
9304 /* need to look at the data field */
9308 p
= lock_user_string(arg1
);
9310 return -TARGET_EFAULT
;
9316 p2
= lock_user_string(arg2
);
9319 unlock_user(p
, arg1
, 0);
9321 return -TARGET_EFAULT
;
9325 p3
= lock_user_string(arg3
);
9328 unlock_user(p
, arg1
, 0);
9330 unlock_user(p2
, arg2
, 0);
9331 return -TARGET_EFAULT
;
9337 /* FIXME - arg5 should be locked, but it isn't clear how to
9338 * do that since it's not guaranteed to be a NULL-terminated
9342 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
9344 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(cpu
, arg5
));
9346 ret
= get_errno(ret
);
9349 unlock_user(p
, arg1
, 0);
9351 unlock_user(p2
, arg2
, 0);
9353 unlock_user(p3
, arg3
, 0);
9357 #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9358 #if defined(TARGET_NR_umount)
9359 case TARGET_NR_umount
:
9361 #if defined(TARGET_NR_oldumount)
9362 case TARGET_NR_oldumount
:
9364 if (!(p
= lock_user_string(arg1
)))
9365 return -TARGET_EFAULT
;
9366 ret
= get_errno(umount(p
));
9367 unlock_user(p
, arg1
, 0);
9370 #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9371 case TARGET_NR_move_mount
:
9375 if (!arg2
|| !arg4
) {
9376 return -TARGET_EFAULT
;
9379 p2
= lock_user_string(arg2
);
9381 return -TARGET_EFAULT
;
9384 p4
= lock_user_string(arg4
);
9386 unlock_user(p2
, arg2
, 0);
9387 return -TARGET_EFAULT
;
9389 ret
= get_errno(sys_move_mount(arg1
, p2
, arg3
, p4
, arg5
));
9391 unlock_user(p2
, arg2
, 0);
9392 unlock_user(p4
, arg4
, 0);
9397 #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9398 case TARGET_NR_open_tree
:
9404 return -TARGET_EFAULT
;
9407 p2
= lock_user_string(arg2
);
9409 return -TARGET_EFAULT
;
9412 host_flags
= arg3
& ~TARGET_O_CLOEXEC
;
9413 if (arg3
& TARGET_O_CLOEXEC
) {
9414 host_flags
|= O_CLOEXEC
;
9417 ret
= get_errno(sys_open_tree(arg1
, p2
, host_flags
));
9419 unlock_user(p2
, arg2
, 0);
9424 #ifdef TARGET_NR_stime /* not on alpha */
9425 case TARGET_NR_stime
:
9429 if (get_user_sal(ts
.tv_sec
, arg1
)) {
9430 return -TARGET_EFAULT
;
9432 return get_errno(clock_settime(CLOCK_REALTIME
, &ts
));
9435 #ifdef TARGET_NR_alarm /* not on alpha */
9436 case TARGET_NR_alarm
:
9439 #ifdef TARGET_NR_pause /* not on alpha */
9440 case TARGET_NR_pause
:
9441 if (!block_signals()) {
9442 sigsuspend(&get_task_state(cpu
)->signal_mask
);
9444 return -TARGET_EINTR
;
9446 #ifdef TARGET_NR_utime
9447 case TARGET_NR_utime
:
9449 struct utimbuf tbuf
, *host_tbuf
;
9450 struct target_utimbuf
*target_tbuf
;
9452 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
9453 return -TARGET_EFAULT
;
9454 tbuf
.actime
= tswapal(target_tbuf
->actime
);
9455 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
9456 unlock_user_struct(target_tbuf
, arg2
, 0);
9461 if (!(p
= lock_user_string(arg1
)))
9462 return -TARGET_EFAULT
;
9463 ret
= get_errno(utime(p
, host_tbuf
));
9464 unlock_user(p
, arg1
, 0);
9468 #ifdef TARGET_NR_utimes
9469 case TARGET_NR_utimes
:
9471 struct timeval
*tvp
, tv
[2];
9473 if (copy_from_user_timeval(&tv
[0], arg2
)
9474 || copy_from_user_timeval(&tv
[1],
9475 arg2
+ sizeof(struct target_timeval
)))
9476 return -TARGET_EFAULT
;
9481 if (!(p
= lock_user_string(arg1
)))
9482 return -TARGET_EFAULT
;
9483 ret
= get_errno(utimes(p
, tvp
));
9484 unlock_user(p
, arg1
, 0);
9488 #if defined(TARGET_NR_futimesat)
9489 case TARGET_NR_futimesat
:
9491 struct timeval
*tvp
, tv
[2];
9493 if (copy_from_user_timeval(&tv
[0], arg3
)
9494 || copy_from_user_timeval(&tv
[1],
9495 arg3
+ sizeof(struct target_timeval
)))
9496 return -TARGET_EFAULT
;
9501 if (!(p
= lock_user_string(arg2
))) {
9502 return -TARGET_EFAULT
;
9504 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
9505 unlock_user(p
, arg2
, 0);
9509 #ifdef TARGET_NR_access
9510 case TARGET_NR_access
:
9511 if (!(p
= lock_user_string(arg1
))) {
9512 return -TARGET_EFAULT
;
9514 ret
= get_errno(access(path(p
), arg2
));
9515 unlock_user(p
, arg1
, 0);
9518 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9519 case TARGET_NR_faccessat
:
9520 if (!(p
= lock_user_string(arg2
))) {
9521 return -TARGET_EFAULT
;
9523 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
9524 unlock_user(p
, arg2
, 0);
9527 #if defined(TARGET_NR_faccessat2)
9528 case TARGET_NR_faccessat2
:
9529 if (!(p
= lock_user_string(arg2
))) {
9530 return -TARGET_EFAULT
;
9532 ret
= get_errno(faccessat(arg1
, p
, arg3
, arg4
));
9533 unlock_user(p
, arg2
, 0);
9536 #ifdef TARGET_NR_nice /* not on alpha */
9537 case TARGET_NR_nice
:
9538 return get_errno(nice(arg1
));
9540 case TARGET_NR_sync
:
9543 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9544 case TARGET_NR_syncfs
:
9545 return get_errno(syncfs(arg1
));
9547 case TARGET_NR_kill
:
9548 return get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
9549 #ifdef TARGET_NR_rename
9550 case TARGET_NR_rename
:
9553 p
= lock_user_string(arg1
);
9554 p2
= lock_user_string(arg2
);
9556 ret
= -TARGET_EFAULT
;
9558 ret
= get_errno(rename(p
, p2
));
9559 unlock_user(p2
, arg2
, 0);
9560 unlock_user(p
, arg1
, 0);
9564 #if defined(TARGET_NR_renameat)
9565 case TARGET_NR_renameat
:
9568 p
= lock_user_string(arg2
);
9569 p2
= lock_user_string(arg4
);
9571 ret
= -TARGET_EFAULT
;
9573 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
9574 unlock_user(p2
, arg4
, 0);
9575 unlock_user(p
, arg2
, 0);
9579 #if defined(TARGET_NR_renameat2)
9580 case TARGET_NR_renameat2
:
9583 p
= lock_user_string(arg2
);
9584 p2
= lock_user_string(arg4
);
9586 ret
= -TARGET_EFAULT
;
9588 ret
= get_errno(sys_renameat2(arg1
, p
, arg3
, p2
, arg5
));
9590 unlock_user(p2
, arg4
, 0);
9591 unlock_user(p
, arg2
, 0);
9595 #ifdef TARGET_NR_mkdir
9596 case TARGET_NR_mkdir
:
9597 if (!(p
= lock_user_string(arg1
)))
9598 return -TARGET_EFAULT
;
9599 ret
= get_errno(mkdir(p
, arg2
));
9600 unlock_user(p
, arg1
, 0);
9603 #if defined(TARGET_NR_mkdirat)
9604 case TARGET_NR_mkdirat
:
9605 if (!(p
= lock_user_string(arg2
)))
9606 return -TARGET_EFAULT
;
9607 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
9608 unlock_user(p
, arg2
, 0);
9611 #ifdef TARGET_NR_rmdir
9612 case TARGET_NR_rmdir
:
9613 if (!(p
= lock_user_string(arg1
)))
9614 return -TARGET_EFAULT
;
9615 ret
= get_errno(rmdir(p
));
9616 unlock_user(p
, arg1
, 0);
9620 ret
= get_errno(dup(arg1
));
9622 fd_trans_dup(arg1
, ret
);
9625 #ifdef TARGET_NR_pipe
9626 case TARGET_NR_pipe
:
9627 return do_pipe(cpu_env
, arg1
, 0, 0);
9629 #ifdef TARGET_NR_pipe2
9630 case TARGET_NR_pipe2
:
9631 return do_pipe(cpu_env
, arg1
,
9632 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
9634 case TARGET_NR_times
:
9636 struct target_tms
*tmsp
;
9638 ret
= get_errno(times(&tms
));
9640 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
9642 return -TARGET_EFAULT
;
9643 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
9644 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
9645 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
9646 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
9649 ret
= host_to_target_clock_t(ret
);
9652 case TARGET_NR_acct
:
9654 ret
= get_errno(acct(NULL
));
9656 if (!(p
= lock_user_string(arg1
))) {
9657 return -TARGET_EFAULT
;
9659 ret
= get_errno(acct(path(p
)));
9660 unlock_user(p
, arg1
, 0);
9663 #ifdef TARGET_NR_umount2
9664 case TARGET_NR_umount2
:
9665 if (!(p
= lock_user_string(arg1
)))
9666 return -TARGET_EFAULT
;
9667 ret
= get_errno(umount2(p
, arg2
));
9668 unlock_user(p
, arg1
, 0);
9671 case TARGET_NR_ioctl
:
9672 return do_ioctl(arg1
, arg2
, arg3
);
9673 #ifdef TARGET_NR_fcntl
9674 case TARGET_NR_fcntl
:
9675 return do_fcntl(arg1
, arg2
, arg3
);
9677 case TARGET_NR_setpgid
:
9678 return get_errno(setpgid(arg1
, arg2
));
9679 case TARGET_NR_umask
:
9680 return get_errno(umask(arg1
));
9681 case TARGET_NR_chroot
:
9682 if (!(p
= lock_user_string(arg1
)))
9683 return -TARGET_EFAULT
;
9684 ret
= get_errno(chroot(p
));
9685 unlock_user(p
, arg1
, 0);
9687 #ifdef TARGET_NR_dup2
9688 case TARGET_NR_dup2
:
9689 ret
= get_errno(dup2(arg1
, arg2
));
9691 fd_trans_dup(arg1
, arg2
);
9695 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9696 case TARGET_NR_dup3
:
9700 if ((arg3
& ~TARGET_O_CLOEXEC
) != 0) {
9703 host_flags
= target_to_host_bitmask(arg3
, fcntl_flags_tbl
);
9704 ret
= get_errno(dup3(arg1
, arg2
, host_flags
));
9706 fd_trans_dup(arg1
, arg2
);
9711 #ifdef TARGET_NR_getppid /* not on alpha */
9712 case TARGET_NR_getppid
:
9713 return get_errno(getppid());
9715 #ifdef TARGET_NR_getpgrp
9716 case TARGET_NR_getpgrp
:
9717 return get_errno(getpgrp());
9719 case TARGET_NR_setsid
:
9720 return get_errno(setsid());
9721 #ifdef TARGET_NR_sigaction
9722 case TARGET_NR_sigaction
:
9724 #if defined(TARGET_MIPS)
9725 struct target_sigaction act
, oact
, *pact
, *old_act
;
9728 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
9729 return -TARGET_EFAULT
;
9730 act
._sa_handler
= old_act
->_sa_handler
;
9731 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
9732 act
.sa_flags
= old_act
->sa_flags
;
9733 unlock_user_struct(old_act
, arg2
, 0);
9739 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
, 0));
9741 if (!is_error(ret
) && arg3
) {
9742 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
9743 return -TARGET_EFAULT
;
9744 old_act
->_sa_handler
= oact
._sa_handler
;
9745 old_act
->sa_flags
= oact
.sa_flags
;
9746 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
9747 old_act
->sa_mask
.sig
[1] = 0;
9748 old_act
->sa_mask
.sig
[2] = 0;
9749 old_act
->sa_mask
.sig
[3] = 0;
9750 unlock_user_struct(old_act
, arg3
, 1);
9753 struct target_old_sigaction
*old_act
;
9754 struct target_sigaction act
, oact
, *pact
;
9756 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
9757 return -TARGET_EFAULT
;
9758 act
._sa_handler
= old_act
->_sa_handler
;
9759 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
9760 act
.sa_flags
= old_act
->sa_flags
;
9761 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9762 act
.sa_restorer
= old_act
->sa_restorer
;
9764 unlock_user_struct(old_act
, arg2
, 0);
9769 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
, 0));
9770 if (!is_error(ret
) && arg3
) {
9771 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
9772 return -TARGET_EFAULT
;
9773 old_act
->_sa_handler
= oact
._sa_handler
;
9774 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
9775 old_act
->sa_flags
= oact
.sa_flags
;
9776 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9777 old_act
->sa_restorer
= oact
.sa_restorer
;
9779 unlock_user_struct(old_act
, arg3
, 1);
9785 case TARGET_NR_rt_sigaction
:
9788 * For Alpha and SPARC this is a 5 argument syscall, with
9789 * a 'restorer' parameter which must be copied into the
9790 * sa_restorer field of the sigaction struct.
9791 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9792 * and arg5 is the sigsetsize.
9794 #if defined(TARGET_ALPHA)
9795 target_ulong sigsetsize
= arg4
;
9796 target_ulong restorer
= arg5
;
9797 #elif defined(TARGET_SPARC)
9798 target_ulong restorer
= arg4
;
9799 target_ulong sigsetsize
= arg5
;
9801 target_ulong sigsetsize
= arg4
;
9802 target_ulong restorer
= 0;
9804 struct target_sigaction
*act
= NULL
;
9805 struct target_sigaction
*oact
= NULL
;
9807 if (sigsetsize
!= sizeof(target_sigset_t
)) {
9808 return -TARGET_EINVAL
;
9810 if (arg2
&& !lock_user_struct(VERIFY_READ
, act
, arg2
, 1)) {
9811 return -TARGET_EFAULT
;
9813 if (arg3
&& !lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
9814 ret
= -TARGET_EFAULT
;
9816 ret
= get_errno(do_sigaction(arg1
, act
, oact
, restorer
));
9818 unlock_user_struct(oact
, arg3
, 1);
9822 unlock_user_struct(act
, arg2
, 0);
9826 #ifdef TARGET_NR_sgetmask /* not on alpha */
9827 case TARGET_NR_sgetmask
:
9830 abi_ulong target_set
;
9831 ret
= do_sigprocmask(0, NULL
, &cur_set
);
9833 host_to_target_old_sigset(&target_set
, &cur_set
);
9839 #ifdef TARGET_NR_ssetmask /* not on alpha */
9840 case TARGET_NR_ssetmask
:
9843 abi_ulong target_set
= arg1
;
9844 target_to_host_old_sigset(&set
, &target_set
);
9845 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
9847 host_to_target_old_sigset(&target_set
, &oset
);
9853 #ifdef TARGET_NR_sigprocmask
9854 case TARGET_NR_sigprocmask
:
9856 #if defined(TARGET_ALPHA)
9857 sigset_t set
, oldset
;
9862 case TARGET_SIG_BLOCK
:
9865 case TARGET_SIG_UNBLOCK
:
9868 case TARGET_SIG_SETMASK
:
9872 return -TARGET_EINVAL
;
9875 target_to_host_old_sigset(&set
, &mask
);
9877 ret
= do_sigprocmask(how
, &set
, &oldset
);
9878 if (!is_error(ret
)) {
9879 host_to_target_old_sigset(&mask
, &oldset
);
9881 cpu_env
->ir
[IR_V0
] = 0; /* force no error */
9884 sigset_t set
, oldset
, *set_ptr
;
9888 p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1);
9890 return -TARGET_EFAULT
;
9892 target_to_host_old_sigset(&set
, p
);
9893 unlock_user(p
, arg2
, 0);
9896 case TARGET_SIG_BLOCK
:
9899 case TARGET_SIG_UNBLOCK
:
9902 case TARGET_SIG_SETMASK
:
9906 return -TARGET_EINVAL
;
9912 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
9913 if (!is_error(ret
) && arg3
) {
9914 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
9915 return -TARGET_EFAULT
;
9916 host_to_target_old_sigset(p
, &oldset
);
9917 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
9923 case TARGET_NR_rt_sigprocmask
:
9926 sigset_t set
, oldset
, *set_ptr
;
9928 if (arg4
!= sizeof(target_sigset_t
)) {
9929 return -TARGET_EINVAL
;
9933 p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1);
9935 return -TARGET_EFAULT
;
9937 target_to_host_sigset(&set
, p
);
9938 unlock_user(p
, arg2
, 0);
9941 case TARGET_SIG_BLOCK
:
9944 case TARGET_SIG_UNBLOCK
:
9947 case TARGET_SIG_SETMASK
:
9951 return -TARGET_EINVAL
;
9957 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
9958 if (!is_error(ret
) && arg3
) {
9959 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
9960 return -TARGET_EFAULT
;
9961 host_to_target_sigset(p
, &oldset
);
9962 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
9966 #ifdef TARGET_NR_sigpending
9967 case TARGET_NR_sigpending
:
9970 ret
= get_errno(sigpending(&set
));
9971 if (!is_error(ret
)) {
9972 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
9973 return -TARGET_EFAULT
;
9974 host_to_target_old_sigset(p
, &set
);
9975 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
9980 case TARGET_NR_rt_sigpending
:
9984 /* Yes, this check is >, not != like most. We follow the kernel's
9985 * logic and it does it like this because it implements
9986 * NR_sigpending through the same code path, and in that case
9987 * the old_sigset_t is smaller in size.
9989 if (arg2
> sizeof(target_sigset_t
)) {
9990 return -TARGET_EINVAL
;
9993 ret
= get_errno(sigpending(&set
));
9994 if (!is_error(ret
)) {
9995 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
9996 return -TARGET_EFAULT
;
9997 host_to_target_sigset(p
, &set
);
9998 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
10002 #ifdef TARGET_NR_sigsuspend
10003 case TARGET_NR_sigsuspend
:
10007 #if defined(TARGET_ALPHA)
10008 TaskState
*ts
= get_task_state(cpu
);
10009 /* target_to_host_old_sigset will bswap back */
10010 abi_ulong mask
= tswapal(arg1
);
10011 set
= &ts
->sigsuspend_mask
;
10012 target_to_host_old_sigset(set
, &mask
);
10014 ret
= process_sigsuspend_mask(&set
, arg1
, sizeof(target_sigset_t
));
10019 ret
= get_errno(safe_rt_sigsuspend(set
, SIGSET_T_SIZE
));
10020 finish_sigsuspend_mask(ret
);
10024 case TARGET_NR_rt_sigsuspend
:
10028 ret
= process_sigsuspend_mask(&set
, arg1
, arg2
);
10032 ret
= get_errno(safe_rt_sigsuspend(set
, SIGSET_T_SIZE
));
10033 finish_sigsuspend_mask(ret
);
10036 #ifdef TARGET_NR_rt_sigtimedwait
10037 case TARGET_NR_rt_sigtimedwait
:
10040 struct timespec uts
, *puts
;
10043 if (arg4
!= sizeof(target_sigset_t
)) {
10044 return -TARGET_EINVAL
;
10047 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
10048 return -TARGET_EFAULT
;
10049 target_to_host_sigset(&set
, p
);
10050 unlock_user(p
, arg1
, 0);
10053 if (target_to_host_timespec(puts
, arg3
)) {
10054 return -TARGET_EFAULT
;
10059 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
10061 if (!is_error(ret
)) {
10063 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
10066 return -TARGET_EFAULT
;
10068 host_to_target_siginfo(p
, &uinfo
);
10069 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
10071 ret
= host_to_target_signal(ret
);
10076 #ifdef TARGET_NR_rt_sigtimedwait_time64
10077 case TARGET_NR_rt_sigtimedwait_time64
:
10080 struct timespec uts
, *puts
;
10083 if (arg4
!= sizeof(target_sigset_t
)) {
10084 return -TARGET_EINVAL
;
10087 p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1);
10089 return -TARGET_EFAULT
;
10091 target_to_host_sigset(&set
, p
);
10092 unlock_user(p
, arg1
, 0);
10095 if (target_to_host_timespec64(puts
, arg3
)) {
10096 return -TARGET_EFAULT
;
10101 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
10103 if (!is_error(ret
)) {
10105 p
= lock_user(VERIFY_WRITE
, arg2
,
10106 sizeof(target_siginfo_t
), 0);
10108 return -TARGET_EFAULT
;
10110 host_to_target_siginfo(p
, &uinfo
);
10111 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
10113 ret
= host_to_target_signal(ret
);
10118 case TARGET_NR_rt_sigqueueinfo
:
10122 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
10124 return -TARGET_EFAULT
;
10126 target_to_host_siginfo(&uinfo
, p
);
10127 unlock_user(p
, arg3
, 0);
10128 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, target_to_host_signal(arg2
), &uinfo
));
10131 case TARGET_NR_rt_tgsigqueueinfo
:
10135 p
= lock_user(VERIFY_READ
, arg4
, sizeof(target_siginfo_t
), 1);
10137 return -TARGET_EFAULT
;
10139 target_to_host_siginfo(&uinfo
, p
);
10140 unlock_user(p
, arg4
, 0);
10141 ret
= get_errno(sys_rt_tgsigqueueinfo(arg1
, arg2
, target_to_host_signal(arg3
), &uinfo
));
10144 #ifdef TARGET_NR_sigreturn
10145 case TARGET_NR_sigreturn
:
10146 if (block_signals()) {
10147 return -QEMU_ERESTARTSYS
;
10149 return do_sigreturn(cpu_env
);
10151 case TARGET_NR_rt_sigreturn
:
10152 if (block_signals()) {
10153 return -QEMU_ERESTARTSYS
;
10155 return do_rt_sigreturn(cpu_env
);
10156 case TARGET_NR_sethostname
:
10157 if (!(p
= lock_user_string(arg1
)))
10158 return -TARGET_EFAULT
;
10159 ret
= get_errno(sethostname(p
, arg2
));
10160 unlock_user(p
, arg1
, 0);
10162 #ifdef TARGET_NR_setrlimit
10163 case TARGET_NR_setrlimit
:
10165 int resource
= target_to_host_resource(arg1
);
10166 struct target_rlimit
*target_rlim
;
10167 struct rlimit rlim
;
10168 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
10169 return -TARGET_EFAULT
;
10170 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
10171 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
10172 unlock_user_struct(target_rlim
, arg2
, 0);
10174 * If we just passed through resource limit settings for memory then
10175 * they would also apply to QEMU's own allocations, and QEMU will
10176 * crash or hang or die if its allocations fail. Ideally we would
10177 * track the guest allocations in QEMU and apply the limits ourselves.
10178 * For now, just tell the guest the call succeeded but don't actually
10181 if (resource
!= RLIMIT_AS
&&
10182 resource
!= RLIMIT_DATA
&&
10183 resource
!= RLIMIT_STACK
) {
10184 return get_errno(setrlimit(resource
, &rlim
));
10190 #ifdef TARGET_NR_getrlimit
10191 case TARGET_NR_getrlimit
:
10193 int resource
= target_to_host_resource(arg1
);
10194 struct target_rlimit
*target_rlim
;
10195 struct rlimit rlim
;
10197 ret
= get_errno(getrlimit(resource
, &rlim
));
10198 if (!is_error(ret
)) {
10199 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
10200 return -TARGET_EFAULT
;
10201 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
10202 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
10203 unlock_user_struct(target_rlim
, arg2
, 1);
10208 case TARGET_NR_getrusage
:
10210 struct rusage rusage
;
10211 ret
= get_errno(getrusage(arg1
, &rusage
));
10212 if (!is_error(ret
)) {
10213 ret
= host_to_target_rusage(arg2
, &rusage
);
10217 #if defined(TARGET_NR_gettimeofday)
10218 case TARGET_NR_gettimeofday
:
10221 struct timezone tz
;
10223 ret
= get_errno(gettimeofday(&tv
, &tz
));
10224 if (!is_error(ret
)) {
10225 if (arg1
&& copy_to_user_timeval(arg1
, &tv
)) {
10226 return -TARGET_EFAULT
;
10228 if (arg2
&& copy_to_user_timezone(arg2
, &tz
)) {
10229 return -TARGET_EFAULT
;
10235 #if defined(TARGET_NR_settimeofday)
10236 case TARGET_NR_settimeofday
:
10238 struct timeval tv
, *ptv
= NULL
;
10239 struct timezone tz
, *ptz
= NULL
;
10242 if (copy_from_user_timeval(&tv
, arg1
)) {
10243 return -TARGET_EFAULT
;
10249 if (copy_from_user_timezone(&tz
, arg2
)) {
10250 return -TARGET_EFAULT
;
10255 return get_errno(settimeofday(ptv
, ptz
));
10258 #if defined(TARGET_NR_select)
10259 case TARGET_NR_select
:
10260 #if defined(TARGET_WANT_NI_OLD_SELECT)
10261 /* some architectures used to have old_select here
10262 * but now ENOSYS it.
10264 ret
= -TARGET_ENOSYS
;
10265 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
10266 ret
= do_old_select(arg1
);
10268 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
10272 #ifdef TARGET_NR_pselect6
10273 case TARGET_NR_pselect6
:
10274 return do_pselect6(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, false);
10276 #ifdef TARGET_NR_pselect6_time64
10277 case TARGET_NR_pselect6_time64
:
10278 return do_pselect6(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, true);
10280 #ifdef TARGET_NR_symlink
10281 case TARGET_NR_symlink
:
10284 p
= lock_user_string(arg1
);
10285 p2
= lock_user_string(arg2
);
10287 ret
= -TARGET_EFAULT
;
10289 ret
= get_errno(symlink(p
, p2
));
10290 unlock_user(p2
, arg2
, 0);
10291 unlock_user(p
, arg1
, 0);
10295 #if defined(TARGET_NR_symlinkat)
10296 case TARGET_NR_symlinkat
:
10299 p
= lock_user_string(arg1
);
10300 p2
= lock_user_string(arg3
);
10302 ret
= -TARGET_EFAULT
;
10304 ret
= get_errno(symlinkat(p
, arg2
, p2
));
10305 unlock_user(p2
, arg3
, 0);
10306 unlock_user(p
, arg1
, 0);
10310 #ifdef TARGET_NR_readlink
10311 case TARGET_NR_readlink
:
10314 p
= lock_user_string(arg1
);
10315 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10316 ret
= get_errno(do_guest_readlink(p
, p2
, arg3
));
10317 unlock_user(p2
, arg2
, ret
);
10318 unlock_user(p
, arg1
, 0);
10322 #if defined(TARGET_NR_readlinkat)
10323 case TARGET_NR_readlinkat
:
10326 p
= lock_user_string(arg2
);
10327 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
10329 ret
= -TARGET_EFAULT
;
10330 } else if (!arg4
) {
10331 /* Short circuit this for the magic exe check. */
10332 ret
= -TARGET_EINVAL
;
10333 } else if (is_proc_myself((const char *)p
, "exe")) {
10335 * Don't worry about sign mismatch as earlier mapping
10336 * logic would have thrown a bad address error.
10338 ret
= MIN(strlen(exec_path
), arg4
);
10339 /* We cannot NUL terminate the string. */
10340 memcpy(p2
, exec_path
, ret
);
10342 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
10344 unlock_user(p2
, arg3
, ret
);
10345 unlock_user(p
, arg2
, 0);
10349 #ifdef TARGET_NR_swapon
10350 case TARGET_NR_swapon
:
10351 if (!(p
= lock_user_string(arg1
)))
10352 return -TARGET_EFAULT
;
10353 ret
= get_errno(swapon(p
, arg2
));
10354 unlock_user(p
, arg1
, 0);
10357 case TARGET_NR_reboot
:
10358 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
10359 /* arg4 must be ignored in all other cases */
10360 p
= lock_user_string(arg4
);
10362 return -TARGET_EFAULT
;
10364 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
10365 unlock_user(p
, arg4
, 0);
10367 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
10370 #ifdef TARGET_NR_mmap
10371 case TARGET_NR_mmap
:
10372 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
10373 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
10374 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
10375 || defined(TARGET_S390X)
10378 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
10379 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
10380 return -TARGET_EFAULT
;
10381 v1
= tswapal(v
[0]);
10382 v2
= tswapal(v
[1]);
10383 v3
= tswapal(v
[2]);
10384 v4
= tswapal(v
[3]);
10385 v5
= tswapal(v
[4]);
10386 v6
= tswapal(v
[5]);
10387 unlock_user(v
, arg1
, 0);
10388 return do_mmap(v1
, v2
, v3
, v4
, v5
, v6
);
10391 /* mmap pointers are always untagged */
10392 return do_mmap(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10395 #ifdef TARGET_NR_mmap2
10396 case TARGET_NR_mmap2
:
10398 #define MMAP_SHIFT 12
10400 return do_mmap(arg1
, arg2
, arg3
, arg4
, arg5
,
10401 (off_t
)(abi_ulong
)arg6
<< MMAP_SHIFT
);
10403 case TARGET_NR_munmap
:
10404 arg1
= cpu_untagged_addr(cpu
, arg1
);
10405 return get_errno(target_munmap(arg1
, arg2
));
10406 case TARGET_NR_mprotect
:
10407 arg1
= cpu_untagged_addr(cpu
, arg1
);
10409 TaskState
*ts
= get_task_state(cpu
);
10410 /* Special hack to detect libc making the stack executable. */
10411 if ((arg3
& PROT_GROWSDOWN
)
10412 && arg1
>= ts
->info
->stack_limit
10413 && arg1
<= ts
->info
->start_stack
) {
10414 arg3
&= ~PROT_GROWSDOWN
;
10415 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
10416 arg1
= ts
->info
->stack_limit
;
10419 return get_errno(target_mprotect(arg1
, arg2
, arg3
));
10420 #ifdef TARGET_NR_mremap
10421 case TARGET_NR_mremap
:
10422 arg1
= cpu_untagged_addr(cpu
, arg1
);
10423 /* mremap new_addr (arg5) is always untagged */
10424 return get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
10426 /* ??? msync/mlock/munlock are broken for softmmu. */
10427 #ifdef TARGET_NR_msync
10428 case TARGET_NR_msync
:
10429 return get_errno(msync(g2h(cpu
, arg1
), arg2
,
10430 target_to_host_msync_arg(arg3
)));
10432 #ifdef TARGET_NR_mlock
10433 case TARGET_NR_mlock
:
10434 return get_errno(mlock(g2h(cpu
, arg1
), arg2
));
10436 #ifdef TARGET_NR_munlock
10437 case TARGET_NR_munlock
:
10438 return get_errno(munlock(g2h(cpu
, arg1
), arg2
));
10440 #ifdef TARGET_NR_mlockall
10441 case TARGET_NR_mlockall
:
10442 return get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
10444 #ifdef TARGET_NR_munlockall
10445 case TARGET_NR_munlockall
:
10446 return get_errno(munlockall());
10448 #ifdef TARGET_NR_truncate
10449 case TARGET_NR_truncate
:
10450 if (!(p
= lock_user_string(arg1
)))
10451 return -TARGET_EFAULT
;
10452 ret
= get_errno(truncate(p
, arg2
));
10453 unlock_user(p
, arg1
, 0);
10456 #ifdef TARGET_NR_ftruncate
10457 case TARGET_NR_ftruncate
:
10458 return get_errno(ftruncate(arg1
, arg2
));
10460 case TARGET_NR_fchmod
:
10461 return get_errno(fchmod(arg1
, arg2
));
10462 #if defined(TARGET_NR_fchmodat)
10463 case TARGET_NR_fchmodat
:
10464 if (!(p
= lock_user_string(arg2
)))
10465 return -TARGET_EFAULT
;
10466 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
10467 unlock_user(p
, arg2
, 0);
10470 case TARGET_NR_getpriority
:
10471 /* Note that negative values are valid for getpriority, so we must
10472 differentiate based on errno settings. */
10474 ret
= getpriority(arg1
, arg2
);
10475 if (ret
== -1 && errno
!= 0) {
10476 return -host_to_target_errno(errno
);
10478 #ifdef TARGET_ALPHA
10479 /* Return value is the unbiased priority. Signal no error. */
10480 cpu_env
->ir
[IR_V0
] = 0;
10482 /* Return value is a biased priority to avoid negative numbers. */
10486 case TARGET_NR_setpriority
:
10487 return get_errno(setpriority(arg1
, arg2
, arg3
));
10488 #ifdef TARGET_NR_statfs
10489 case TARGET_NR_statfs
:
10490 if (!(p
= lock_user_string(arg1
))) {
10491 return -TARGET_EFAULT
;
10493 ret
= get_errno(statfs(path(p
), &stfs
));
10494 unlock_user(p
, arg1
, 0);
10496 if (!is_error(ret
)) {
10497 struct target_statfs
*target_stfs
;
10499 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
10500 return -TARGET_EFAULT
;
10501 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
10502 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
10503 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
10504 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
10505 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
10506 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
10507 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
10508 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
10509 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
10510 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
10511 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
10512 #ifdef _STATFS_F_FLAGS
10513 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
10515 __put_user(0, &target_stfs
->f_flags
);
10517 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
10518 unlock_user_struct(target_stfs
, arg2
, 1);
10522 #ifdef TARGET_NR_fstatfs
10523 case TARGET_NR_fstatfs
:
10524 ret
= get_errno(fstatfs(arg1
, &stfs
));
10525 goto convert_statfs
;
10527 #ifdef TARGET_NR_statfs64
10528 case TARGET_NR_statfs64
:
10529 if (!(p
= lock_user_string(arg1
))) {
10530 return -TARGET_EFAULT
;
10532 ret
= get_errno(statfs(path(p
), &stfs
));
10533 unlock_user(p
, arg1
, 0);
10535 if (!is_error(ret
)) {
10536 struct target_statfs64
*target_stfs
;
10538 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
10539 return -TARGET_EFAULT
;
10540 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
10541 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
10542 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
10543 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
10544 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
10545 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
10546 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
10547 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
10548 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
10549 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
10550 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
10551 #ifdef _STATFS_F_FLAGS
10552 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
10554 __put_user(0, &target_stfs
->f_flags
);
10556 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
10557 unlock_user_struct(target_stfs
, arg3
, 1);
10560 case TARGET_NR_fstatfs64
:
10561 ret
= get_errno(fstatfs(arg1
, &stfs
));
10562 goto convert_statfs64
;
10564 #ifdef TARGET_NR_socketcall
10565 case TARGET_NR_socketcall
:
10566 return do_socketcall(arg1
, arg2
);
10568 #ifdef TARGET_NR_accept
10569 case TARGET_NR_accept
:
10570 return do_accept4(arg1
, arg2
, arg3
, 0);
10572 #ifdef TARGET_NR_accept4
10573 case TARGET_NR_accept4
:
10574 return do_accept4(arg1
, arg2
, arg3
, arg4
);
10576 #ifdef TARGET_NR_bind
10577 case TARGET_NR_bind
:
10578 return do_bind(arg1
, arg2
, arg3
);
10580 #ifdef TARGET_NR_connect
10581 case TARGET_NR_connect
:
10582 return do_connect(arg1
, arg2
, arg3
);
10584 #ifdef TARGET_NR_getpeername
10585 case TARGET_NR_getpeername
:
10586 return do_getpeername(arg1
, arg2
, arg3
);
10588 #ifdef TARGET_NR_getsockname
10589 case TARGET_NR_getsockname
:
10590 return do_getsockname(arg1
, arg2
, arg3
);
10592 #ifdef TARGET_NR_getsockopt
10593 case TARGET_NR_getsockopt
:
10594 return do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
10596 #ifdef TARGET_NR_listen
10597 case TARGET_NR_listen
:
10598 return get_errno(listen(arg1
, arg2
));
10600 #ifdef TARGET_NR_recv
10601 case TARGET_NR_recv
:
10602 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
10604 #ifdef TARGET_NR_recvfrom
10605 case TARGET_NR_recvfrom
:
10606 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10608 #ifdef TARGET_NR_recvmsg
10609 case TARGET_NR_recvmsg
:
10610 return do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
10612 #ifdef TARGET_NR_send
10613 case TARGET_NR_send
:
10614 return do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
10616 #ifdef TARGET_NR_sendmsg
10617 case TARGET_NR_sendmsg
:
10618 return do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
10620 #ifdef TARGET_NR_sendmmsg
10621 case TARGET_NR_sendmmsg
:
10622 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
10624 #ifdef TARGET_NR_recvmmsg
10625 case TARGET_NR_recvmmsg
:
10626 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
10628 #ifdef TARGET_NR_sendto
10629 case TARGET_NR_sendto
:
10630 return do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10632 #ifdef TARGET_NR_shutdown
10633 case TARGET_NR_shutdown
:
10634 return get_errno(shutdown(arg1
, arg2
));
10636 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10637 case TARGET_NR_getrandom
:
10638 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
10640 return -TARGET_EFAULT
;
10642 ret
= get_errno(getrandom(p
, arg2
, arg3
));
10643 unlock_user(p
, arg1
, ret
);
10646 #ifdef TARGET_NR_socket
10647 case TARGET_NR_socket
:
10648 return do_socket(arg1
, arg2
, arg3
);
10650 #ifdef TARGET_NR_socketpair
10651 case TARGET_NR_socketpair
:
10652 return do_socketpair(arg1
, arg2
, arg3
, arg4
);
10654 #ifdef TARGET_NR_setsockopt
10655 case TARGET_NR_setsockopt
:
10656 return do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
10658 #if defined(TARGET_NR_syslog)
10659 case TARGET_NR_syslog
:
10664 case TARGET_SYSLOG_ACTION_CLOSE
: /* Close log */
10665 case TARGET_SYSLOG_ACTION_OPEN
: /* Open log */
10666 case TARGET_SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
10667 case TARGET_SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging */
10668 case TARGET_SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging */
10669 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL
: /* Set messages level */
10670 case TARGET_SYSLOG_ACTION_SIZE_UNREAD
: /* Number of chars */
10671 case TARGET_SYSLOG_ACTION_SIZE_BUFFER
: /* Size of the buffer */
10672 return get_errno(sys_syslog((int)arg1
, NULL
, (int)arg3
));
10673 case TARGET_SYSLOG_ACTION_READ
: /* Read from log */
10674 case TARGET_SYSLOG_ACTION_READ_CLEAR
: /* Read/clear msgs */
10675 case TARGET_SYSLOG_ACTION_READ_ALL
: /* Read last messages */
10678 return -TARGET_EINVAL
;
10683 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10685 return -TARGET_EFAULT
;
10687 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
10688 unlock_user(p
, arg2
, arg3
);
10692 return -TARGET_EINVAL
;
10697 case TARGET_NR_setitimer
:
10699 struct itimerval value
, ovalue
, *pvalue
;
10703 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
10704 || copy_from_user_timeval(&pvalue
->it_value
,
10705 arg2
+ sizeof(struct target_timeval
)))
10706 return -TARGET_EFAULT
;
10710 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
10711 if (!is_error(ret
) && arg3
) {
10712 if (copy_to_user_timeval(arg3
,
10713 &ovalue
.it_interval
)
10714 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
10716 return -TARGET_EFAULT
;
10720 case TARGET_NR_getitimer
:
10722 struct itimerval value
;
10724 ret
= get_errno(getitimer(arg1
, &value
));
10725 if (!is_error(ret
) && arg2
) {
10726 if (copy_to_user_timeval(arg2
,
10727 &value
.it_interval
)
10728 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
10730 return -TARGET_EFAULT
;
10734 #ifdef TARGET_NR_stat
10735 case TARGET_NR_stat
:
10736 if (!(p
= lock_user_string(arg1
))) {
10737 return -TARGET_EFAULT
;
10739 ret
= get_errno(stat(path(p
), &st
));
10740 unlock_user(p
, arg1
, 0);
10743 #ifdef TARGET_NR_lstat
10744 case TARGET_NR_lstat
:
10745 if (!(p
= lock_user_string(arg1
))) {
10746 return -TARGET_EFAULT
;
10748 ret
= get_errno(lstat(path(p
), &st
));
10749 unlock_user(p
, arg1
, 0);
10752 #ifdef TARGET_NR_fstat
10753 case TARGET_NR_fstat
:
10755 ret
= get_errno(fstat(arg1
, &st
));
10756 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
10759 if (!is_error(ret
)) {
10760 struct target_stat
*target_st
;
10762 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
10763 return -TARGET_EFAULT
;
10764 memset(target_st
, 0, sizeof(*target_st
));
10765 __put_user(st
.st_dev
, &target_st
->st_dev
);
10766 __put_user(st
.st_ino
, &target_st
->st_ino
);
10767 __put_user(st
.st_mode
, &target_st
->st_mode
);
10768 __put_user(st
.st_uid
, &target_st
->st_uid
);
10769 __put_user(st
.st_gid
, &target_st
->st_gid
);
10770 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
10771 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
10772 __put_user(st
.st_size
, &target_st
->st_size
);
10773 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
10774 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
10775 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
10776 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
10777 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
10778 #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
10779 __put_user(st
.st_atim
.tv_nsec
,
10780 &target_st
->target_st_atime_nsec
);
10781 __put_user(st
.st_mtim
.tv_nsec
,
10782 &target_st
->target_st_mtime_nsec
);
10783 __put_user(st
.st_ctim
.tv_nsec
,
10784 &target_st
->target_st_ctime_nsec
);
10786 unlock_user_struct(target_st
, arg2
, 1);
10791 case TARGET_NR_vhangup
:
10792 return get_errno(vhangup());
10793 #ifdef TARGET_NR_syscall
10794 case TARGET_NR_syscall
:
10795 return do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
10796 arg6
, arg7
, arg8
, 0);
10798 #if defined(TARGET_NR_wait4)
10799 case TARGET_NR_wait4
:
10802 abi_long status_ptr
= arg2
;
10803 struct rusage rusage
, *rusage_ptr
;
10804 abi_ulong target_rusage
= arg4
;
10805 abi_long rusage_err
;
10807 rusage_ptr
= &rusage
;
10810 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
10811 if (!is_error(ret
)) {
10812 if (status_ptr
&& ret
) {
10813 status
= host_to_target_waitstatus(status
);
10814 if (put_user_s32(status
, status_ptr
))
10815 return -TARGET_EFAULT
;
10817 if (target_rusage
) {
10818 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
10827 #ifdef TARGET_NR_swapoff
10828 case TARGET_NR_swapoff
:
10829 if (!(p
= lock_user_string(arg1
)))
10830 return -TARGET_EFAULT
;
10831 ret
= get_errno(swapoff(p
));
10832 unlock_user(p
, arg1
, 0);
10835 case TARGET_NR_sysinfo
:
10837 struct target_sysinfo
*target_value
;
10838 struct sysinfo value
;
10839 ret
= get_errno(sysinfo(&value
));
10840 if (!is_error(ret
) && arg1
)
10842 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
10843 return -TARGET_EFAULT
;
10844 __put_user(value
.uptime
, &target_value
->uptime
);
10845 __put_user(value
.loads
[0], &target_value
->loads
[0]);
10846 __put_user(value
.loads
[1], &target_value
->loads
[1]);
10847 __put_user(value
.loads
[2], &target_value
->loads
[2]);
10848 __put_user(value
.totalram
, &target_value
->totalram
);
10849 __put_user(value
.freeram
, &target_value
->freeram
);
10850 __put_user(value
.sharedram
, &target_value
->sharedram
);
10851 __put_user(value
.bufferram
, &target_value
->bufferram
);
10852 __put_user(value
.totalswap
, &target_value
->totalswap
);
10853 __put_user(value
.freeswap
, &target_value
->freeswap
);
10854 __put_user(value
.procs
, &target_value
->procs
);
10855 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
10856 __put_user(value
.freehigh
, &target_value
->freehigh
);
10857 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
10858 unlock_user_struct(target_value
, arg1
, 1);
10862 #ifdef TARGET_NR_ipc
10863 case TARGET_NR_ipc
:
10864 return do_ipc(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10866 #ifdef TARGET_NR_semget
10867 case TARGET_NR_semget
:
10868 return get_errno(semget(arg1
, arg2
, arg3
));
10870 #ifdef TARGET_NR_semop
10871 case TARGET_NR_semop
:
10872 return do_semtimedop(arg1
, arg2
, arg3
, 0, false);
10874 #ifdef TARGET_NR_semtimedop
10875 case TARGET_NR_semtimedop
:
10876 return do_semtimedop(arg1
, arg2
, arg3
, arg4
, false);
10878 #ifdef TARGET_NR_semtimedop_time64
10879 case TARGET_NR_semtimedop_time64
:
10880 return do_semtimedop(arg1
, arg2
, arg3
, arg4
, true);
10882 #ifdef TARGET_NR_semctl
10883 case TARGET_NR_semctl
:
10884 return do_semctl(arg1
, arg2
, arg3
, arg4
);
10886 #ifdef TARGET_NR_msgctl
10887 case TARGET_NR_msgctl
:
10888 return do_msgctl(arg1
, arg2
, arg3
);
10890 #ifdef TARGET_NR_msgget
10891 case TARGET_NR_msgget
:
10892 return get_errno(msgget(arg1
, arg2
));
10894 #ifdef TARGET_NR_msgrcv
10895 case TARGET_NR_msgrcv
:
10896 return do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
10898 #ifdef TARGET_NR_msgsnd
10899 case TARGET_NR_msgsnd
:
10900 return do_msgsnd(arg1
, arg2
, arg3
, arg4
);
10902 #ifdef TARGET_NR_shmget
10903 case TARGET_NR_shmget
:
10904 return get_errno(shmget(arg1
, arg2
, arg3
));
10906 #ifdef TARGET_NR_shmctl
10907 case TARGET_NR_shmctl
:
10908 return do_shmctl(arg1
, arg2
, arg3
);
10910 #ifdef TARGET_NR_shmat
10911 case TARGET_NR_shmat
:
10912 return target_shmat(cpu_env
, arg1
, arg2
, arg3
);
10914 #ifdef TARGET_NR_shmdt
10915 case TARGET_NR_shmdt
:
10916 return target_shmdt(arg1
);
10918 case TARGET_NR_fsync
:
10919 return get_errno(fsync(arg1
));
10920 case TARGET_NR_clone
:
10921 /* Linux manages to have three different orderings for its
10922 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10923 * match the kernel's CONFIG_CLONE_* settings.
10924 * Microblaze is further special in that it uses a sixth
10925 * implicit argument to clone for the TLS pointer.
10927 #if defined(TARGET_MICROBLAZE)
10928 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
10929 #elif defined(TARGET_CLONE_BACKWARDS)
10930 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
10931 #elif defined(TARGET_CLONE_BACKWARDS2)
10932 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
10934 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
10937 #ifdef __NR_exit_group
10938 /* new thread calls */
10939 case TARGET_NR_exit_group
:
10940 preexit_cleanup(cpu_env
, arg1
);
10941 return get_errno(exit_group(arg1
));
10943 case TARGET_NR_setdomainname
:
10944 if (!(p
= lock_user_string(arg1
)))
10945 return -TARGET_EFAULT
;
10946 ret
= get_errno(setdomainname(p
, arg2
));
10947 unlock_user(p
, arg1
, 0);
10949 case TARGET_NR_uname
:
10950 /* no need to transcode because we use the linux syscall */
10952 struct new_utsname
* buf
;
10954 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
10955 return -TARGET_EFAULT
;
10956 ret
= get_errno(sys_uname(buf
));
10957 if (!is_error(ret
)) {
10958 /* Overwrite the native machine name with whatever is being
10960 g_strlcpy(buf
->machine
, cpu_to_uname_machine(cpu_env
),
10961 sizeof(buf
->machine
));
10962 /* Allow the user to override the reported release. */
10963 if (qemu_uname_release
&& *qemu_uname_release
) {
10964 g_strlcpy(buf
->release
, qemu_uname_release
,
10965 sizeof(buf
->release
));
10968 unlock_user_struct(buf
, arg1
, 1);
10972 case TARGET_NR_modify_ldt
:
10973 return do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
10974 #if !defined(TARGET_X86_64)
10975 case TARGET_NR_vm86
:
10976 return do_vm86(cpu_env
, arg1
, arg2
);
10979 #if defined(TARGET_NR_adjtimex)
10980 case TARGET_NR_adjtimex
:
10982 struct timex host_buf
;
10984 if (target_to_host_timex(&host_buf
, arg1
) != 0) {
10985 return -TARGET_EFAULT
;
10987 ret
= get_errno(adjtimex(&host_buf
));
10988 if (!is_error(ret
)) {
10989 if (host_to_target_timex(arg1
, &host_buf
) != 0) {
10990 return -TARGET_EFAULT
;
10996 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10997 case TARGET_NR_clock_adjtime
:
11001 if (target_to_host_timex(&htx
, arg2
) != 0) {
11002 return -TARGET_EFAULT
;
11004 ret
= get_errno(clock_adjtime(arg1
, &htx
));
11005 if (!is_error(ret
) && host_to_target_timex(arg2
, &htx
)) {
11006 return -TARGET_EFAULT
;
11011 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
11012 case TARGET_NR_clock_adjtime64
:
11016 if (target_to_host_timex64(&htx
, arg2
) != 0) {
11017 return -TARGET_EFAULT
;
11019 ret
= get_errno(clock_adjtime(arg1
, &htx
));
11020 if (!is_error(ret
) && host_to_target_timex64(arg2
, &htx
)) {
11021 return -TARGET_EFAULT
;
11026 case TARGET_NR_getpgid
:
11027 return get_errno(getpgid(arg1
));
11028 case TARGET_NR_fchdir
:
11029 return get_errno(fchdir(arg1
));
11030 case TARGET_NR_personality
:
11031 return get_errno(personality(arg1
));
11032 #ifdef TARGET_NR__llseek /* Not on alpha */
11033 case TARGET_NR__llseek
:
11036 #if !defined(__NR_llseek)
11037 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | (abi_ulong
)arg3
, arg5
);
11039 ret
= get_errno(res
);
11044 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
11046 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
11047 return -TARGET_EFAULT
;
11052 #ifdef TARGET_NR_getdents
11053 case TARGET_NR_getdents
:
11054 return do_getdents(arg1
, arg2
, arg3
);
11055 #endif /* TARGET_NR_getdents */
11056 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
11057 case TARGET_NR_getdents64
:
11058 return do_getdents64(arg1
, arg2
, arg3
);
11059 #endif /* TARGET_NR_getdents64 */
11060 #if defined(TARGET_NR__newselect)
11061 case TARGET_NR__newselect
:
11062 return do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
11064 #ifdef TARGET_NR_poll
11065 case TARGET_NR_poll
:
11066 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, false, false);
11068 #ifdef TARGET_NR_ppoll
11069 case TARGET_NR_ppoll
:
11070 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, true, false);
11072 #ifdef TARGET_NR_ppoll_time64
11073 case TARGET_NR_ppoll_time64
:
11074 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, true, true);
11076 case TARGET_NR_flock
:
11077 /* NOTE: the flock constant seems to be the same for every
11079 return get_errno(safe_flock(arg1
, arg2
));
11080 case TARGET_NR_readv
:
11082 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
11084 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
11085 unlock_iovec(vec
, arg2
, arg3
, 1);
11087 ret
= -host_to_target_errno(errno
);
11091 case TARGET_NR_writev
:
11093 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
11095 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
11096 unlock_iovec(vec
, arg2
, arg3
, 0);
11098 ret
= -host_to_target_errno(errno
);
11102 #if defined(TARGET_NR_preadv)
11103 case TARGET_NR_preadv
:
11105 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
11107 unsigned long low
, high
;
11109 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
11110 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, low
, high
));
11111 unlock_iovec(vec
, arg2
, arg3
, 1);
11113 ret
= -host_to_target_errno(errno
);
11118 #if defined(TARGET_NR_pwritev)
11119 case TARGET_NR_pwritev
:
11121 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
11123 unsigned long low
, high
;
11125 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
11126 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, low
, high
));
11127 unlock_iovec(vec
, arg2
, arg3
, 0);
11129 ret
= -host_to_target_errno(errno
);
11134 case TARGET_NR_getsid
:
11135 return get_errno(getsid(arg1
));
11136 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
11137 case TARGET_NR_fdatasync
:
11138 return get_errno(fdatasync(arg1
));
11140 case TARGET_NR_sched_getaffinity
:
11142 unsigned int mask_size
;
11143 unsigned long *mask
;
11146 * sched_getaffinity needs multiples of ulong, so need to take
11147 * care of mismatches between target ulong and host ulong sizes.
11149 if (arg2
& (sizeof(abi_ulong
) - 1)) {
11150 return -TARGET_EINVAL
;
11152 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
11154 mask
= alloca(mask_size
);
11155 memset(mask
, 0, mask_size
);
11156 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
11158 if (!is_error(ret
)) {
11160 /* More data returned than the caller's buffer will fit.
11161 * This only happens if sizeof(abi_long) < sizeof(long)
11162 * and the caller passed us a buffer holding an odd number
11163 * of abi_longs. If the host kernel is actually using the
11164 * extra 4 bytes then fail EINVAL; otherwise we can just
11165 * ignore them and only copy the interesting part.
11167 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
11168 if (numcpus
> arg2
* 8) {
11169 return -TARGET_EINVAL
;
11174 if (host_to_target_cpu_mask(mask
, mask_size
, arg3
, ret
)) {
11175 return -TARGET_EFAULT
;
11180 case TARGET_NR_sched_setaffinity
:
11182 unsigned int mask_size
;
11183 unsigned long *mask
;
11186 * sched_setaffinity needs multiples of ulong, so need to take
11187 * care of mismatches between target ulong and host ulong sizes.
11189 if (arg2
& (sizeof(abi_ulong
) - 1)) {
11190 return -TARGET_EINVAL
;
11192 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
11193 mask
= alloca(mask_size
);
11195 ret
= target_to_host_cpu_mask(mask
, mask_size
, arg3
, arg2
);
11200 return get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
11202 case TARGET_NR_getcpu
:
11204 unsigned cpuid
, node
;
11205 ret
= get_errno(sys_getcpu(arg1
? &cpuid
: NULL
,
11206 arg2
? &node
: NULL
,
11208 if (is_error(ret
)) {
11211 if (arg1
&& put_user_u32(cpuid
, arg1
)) {
11212 return -TARGET_EFAULT
;
11214 if (arg2
&& put_user_u32(node
, arg2
)) {
11215 return -TARGET_EFAULT
;
11219 case TARGET_NR_sched_setparam
:
11221 struct target_sched_param
*target_schp
;
11222 struct sched_param schp
;
11225 return -TARGET_EINVAL
;
11227 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1)) {
11228 return -TARGET_EFAULT
;
11230 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
11231 unlock_user_struct(target_schp
, arg2
, 0);
11232 return get_errno(sys_sched_setparam(arg1
, &schp
));
11234 case TARGET_NR_sched_getparam
:
11236 struct target_sched_param
*target_schp
;
11237 struct sched_param schp
;
11240 return -TARGET_EINVAL
;
11242 ret
= get_errno(sys_sched_getparam(arg1
, &schp
));
11243 if (!is_error(ret
)) {
11244 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0)) {
11245 return -TARGET_EFAULT
;
11247 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
11248 unlock_user_struct(target_schp
, arg2
, 1);
11252 case TARGET_NR_sched_setscheduler
:
11254 struct target_sched_param
*target_schp
;
11255 struct sched_param schp
;
11257 return -TARGET_EINVAL
;
11259 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1)) {
11260 return -TARGET_EFAULT
;
11262 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
11263 unlock_user_struct(target_schp
, arg3
, 0);
11264 return get_errno(sys_sched_setscheduler(arg1
, arg2
, &schp
));
11266 case TARGET_NR_sched_getscheduler
:
11267 return get_errno(sys_sched_getscheduler(arg1
));
11268 case TARGET_NR_sched_getattr
:
11270 struct target_sched_attr
*target_scha
;
11271 struct sched_attr scha
;
11273 return -TARGET_EINVAL
;
11275 if (arg3
> sizeof(scha
)) {
11276 arg3
= sizeof(scha
);
11278 ret
= get_errno(sys_sched_getattr(arg1
, &scha
, arg3
, arg4
));
11279 if (!is_error(ret
)) {
11280 target_scha
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11281 if (!target_scha
) {
11282 return -TARGET_EFAULT
;
11284 target_scha
->size
= tswap32(scha
.size
);
11285 target_scha
->sched_policy
= tswap32(scha
.sched_policy
);
11286 target_scha
->sched_flags
= tswap64(scha
.sched_flags
);
11287 target_scha
->sched_nice
= tswap32(scha
.sched_nice
);
11288 target_scha
->sched_priority
= tswap32(scha
.sched_priority
);
11289 target_scha
->sched_runtime
= tswap64(scha
.sched_runtime
);
11290 target_scha
->sched_deadline
= tswap64(scha
.sched_deadline
);
11291 target_scha
->sched_period
= tswap64(scha
.sched_period
);
11292 if (scha
.size
> offsetof(struct sched_attr
, sched_util_min
)) {
11293 target_scha
->sched_util_min
= tswap32(scha
.sched_util_min
);
11294 target_scha
->sched_util_max
= tswap32(scha
.sched_util_max
);
11296 unlock_user(target_scha
, arg2
, arg3
);
11300 case TARGET_NR_sched_setattr
:
11302 struct target_sched_attr
*target_scha
;
11303 struct sched_attr scha
;
11307 return -TARGET_EINVAL
;
11309 if (get_user_u32(size
, arg2
)) {
11310 return -TARGET_EFAULT
;
11313 size
= offsetof(struct target_sched_attr
, sched_util_min
);
11315 if (size
< offsetof(struct target_sched_attr
, sched_util_min
)) {
11316 if (put_user_u32(sizeof(struct target_sched_attr
), arg2
)) {
11317 return -TARGET_EFAULT
;
11319 return -TARGET_E2BIG
;
11322 zeroed
= check_zeroed_user(arg2
, sizeof(struct target_sched_attr
), size
);
11325 } else if (zeroed
== 0) {
11326 if (put_user_u32(sizeof(struct target_sched_attr
), arg2
)) {
11327 return -TARGET_EFAULT
;
11329 return -TARGET_E2BIG
;
11331 if (size
> sizeof(struct target_sched_attr
)) {
11332 size
= sizeof(struct target_sched_attr
);
11335 target_scha
= lock_user(VERIFY_READ
, arg2
, size
, 1);
11336 if (!target_scha
) {
11337 return -TARGET_EFAULT
;
11340 scha
.sched_policy
= tswap32(target_scha
->sched_policy
);
11341 scha
.sched_flags
= tswap64(target_scha
->sched_flags
);
11342 scha
.sched_nice
= tswap32(target_scha
->sched_nice
);
11343 scha
.sched_priority
= tswap32(target_scha
->sched_priority
);
11344 scha
.sched_runtime
= tswap64(target_scha
->sched_runtime
);
11345 scha
.sched_deadline
= tswap64(target_scha
->sched_deadline
);
11346 scha
.sched_period
= tswap64(target_scha
->sched_period
);
11347 if (size
> offsetof(struct target_sched_attr
, sched_util_min
)) {
11348 scha
.sched_util_min
= tswap32(target_scha
->sched_util_min
);
11349 scha
.sched_util_max
= tswap32(target_scha
->sched_util_max
);
11351 unlock_user(target_scha
, arg2
, 0);
11352 return get_errno(sys_sched_setattr(arg1
, &scha
, arg3
));
11354 case TARGET_NR_sched_yield
:
11355 return get_errno(sched_yield());
11356 case TARGET_NR_sched_get_priority_max
:
11357 return get_errno(sched_get_priority_max(arg1
));
11358 case TARGET_NR_sched_get_priority_min
:
11359 return get_errno(sched_get_priority_min(arg1
));
11360 #ifdef TARGET_NR_sched_rr_get_interval
11361 case TARGET_NR_sched_rr_get_interval
:
11363 struct timespec ts
;
11364 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
11365 if (!is_error(ret
)) {
11366 ret
= host_to_target_timespec(arg2
, &ts
);
11371 #ifdef TARGET_NR_sched_rr_get_interval_time64
11372 case TARGET_NR_sched_rr_get_interval_time64
:
11374 struct timespec ts
;
11375 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
11376 if (!is_error(ret
)) {
11377 ret
= host_to_target_timespec64(arg2
, &ts
);
11382 #if defined(TARGET_NR_nanosleep)
11383 case TARGET_NR_nanosleep
:
11385 struct timespec req
, rem
;
11386 target_to_host_timespec(&req
, arg1
);
11387 ret
= get_errno(safe_nanosleep(&req
, &rem
));
11388 if (is_error(ret
) && arg2
) {
11389 host_to_target_timespec(arg2
, &rem
);
11394 case TARGET_NR_prctl
:
11395 return do_prctl(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
);
11397 #ifdef TARGET_NR_arch_prctl
11398 case TARGET_NR_arch_prctl
:
11399 return do_arch_prctl(cpu_env
, arg1
, arg2
);
11401 #ifdef TARGET_NR_pread64
11402 case TARGET_NR_pread64
:
11403 if (regpairs_aligned(cpu_env
, num
)) {
11407 if (arg2
== 0 && arg3
== 0) {
11408 /* Special-case NULL buffer and zero length, which should succeed */
11411 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11413 return -TARGET_EFAULT
;
11416 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
11417 unlock_user(p
, arg2
, ret
);
11419 case TARGET_NR_pwrite64
:
11420 if (regpairs_aligned(cpu_env
, num
)) {
11424 if (arg2
== 0 && arg3
== 0) {
11425 /* Special-case NULL buffer and zero length, which should succeed */
11428 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
11430 return -TARGET_EFAULT
;
11433 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
11434 unlock_user(p
, arg2
, 0);
11437 case TARGET_NR_getcwd
:
11438 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
11439 return -TARGET_EFAULT
;
11440 ret
= get_errno(sys_getcwd1(p
, arg2
));
11441 unlock_user(p
, arg1
, ret
);
11443 case TARGET_NR_capget
:
11444 case TARGET_NR_capset
:
11446 struct target_user_cap_header
*target_header
;
11447 struct target_user_cap_data
*target_data
= NULL
;
11448 struct __user_cap_header_struct header
;
11449 struct __user_cap_data_struct data
[2];
11450 struct __user_cap_data_struct
*dataptr
= NULL
;
11451 int i
, target_datalen
;
11452 int data_items
= 1;
11454 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
11455 return -TARGET_EFAULT
;
11457 header
.version
= tswap32(target_header
->version
);
11458 header
.pid
= tswap32(target_header
->pid
);
11460 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
11461 /* Version 2 and up takes pointer to two user_data structs */
11465 target_datalen
= sizeof(*target_data
) * data_items
;
11468 if (num
== TARGET_NR_capget
) {
11469 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
11471 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
11473 if (!target_data
) {
11474 unlock_user_struct(target_header
, arg1
, 0);
11475 return -TARGET_EFAULT
;
11478 if (num
== TARGET_NR_capset
) {
11479 for (i
= 0; i
< data_items
; i
++) {
11480 data
[i
].effective
= tswap32(target_data
[i
].effective
);
11481 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
11482 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
11489 if (num
== TARGET_NR_capget
) {
11490 ret
= get_errno(capget(&header
, dataptr
));
11492 ret
= get_errno(capset(&header
, dataptr
));
11495 /* The kernel always updates version for both capget and capset */
11496 target_header
->version
= tswap32(header
.version
);
11497 unlock_user_struct(target_header
, arg1
, 1);
11500 if (num
== TARGET_NR_capget
) {
11501 for (i
= 0; i
< data_items
; i
++) {
11502 target_data
[i
].effective
= tswap32(data
[i
].effective
);
11503 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
11504 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
11506 unlock_user(target_data
, arg2
, target_datalen
);
11508 unlock_user(target_data
, arg2
, 0);
11513 case TARGET_NR_sigaltstack
:
11514 return do_sigaltstack(arg1
, arg2
, cpu_env
);
11516 #ifdef CONFIG_SENDFILE
11517 #ifdef TARGET_NR_sendfile
11518 case TARGET_NR_sendfile
:
11520 off_t
*offp
= NULL
;
11523 ret
= get_user_sal(off
, arg3
);
11524 if (is_error(ret
)) {
11529 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
11530 if (!is_error(ret
) && arg3
) {
11531 abi_long ret2
= put_user_sal(off
, arg3
);
11532 if (is_error(ret2
)) {
11539 #ifdef TARGET_NR_sendfile64
11540 case TARGET_NR_sendfile64
:
11542 off_t
*offp
= NULL
;
11545 ret
= get_user_s64(off
, arg3
);
11546 if (is_error(ret
)) {
11551 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
11552 if (!is_error(ret
) && arg3
) {
11553 abi_long ret2
= put_user_s64(off
, arg3
);
11554 if (is_error(ret2
)) {
11562 #ifdef TARGET_NR_vfork
11563 case TARGET_NR_vfork
:
11564 return get_errno(do_fork(cpu_env
,
11565 CLONE_VFORK
| CLONE_VM
| TARGET_SIGCHLD
,
11568 #ifdef TARGET_NR_ugetrlimit
11569 case TARGET_NR_ugetrlimit
:
11571 struct rlimit rlim
;
11572 int resource
= target_to_host_resource(arg1
);
11573 ret
= get_errno(getrlimit(resource
, &rlim
));
11574 if (!is_error(ret
)) {
11575 struct target_rlimit
*target_rlim
;
11576 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
11577 return -TARGET_EFAULT
;
11578 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
11579 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
11580 unlock_user_struct(target_rlim
, arg2
, 1);
11585 #ifdef TARGET_NR_truncate64
11586 case TARGET_NR_truncate64
:
11587 if (!(p
= lock_user_string(arg1
)))
11588 return -TARGET_EFAULT
;
11589 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
11590 unlock_user(p
, arg1
, 0);
11593 #ifdef TARGET_NR_ftruncate64
11594 case TARGET_NR_ftruncate64
:
11595 return target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
11597 #ifdef TARGET_NR_stat64
11598 case TARGET_NR_stat64
:
11599 if (!(p
= lock_user_string(arg1
))) {
11600 return -TARGET_EFAULT
;
11602 ret
= get_errno(stat(path(p
), &st
));
11603 unlock_user(p
, arg1
, 0);
11604 if (!is_error(ret
))
11605 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11608 #ifdef TARGET_NR_lstat64
11609 case TARGET_NR_lstat64
:
11610 if (!(p
= lock_user_string(arg1
))) {
11611 return -TARGET_EFAULT
;
11613 ret
= get_errno(lstat(path(p
), &st
));
11614 unlock_user(p
, arg1
, 0);
11615 if (!is_error(ret
))
11616 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11619 #ifdef TARGET_NR_fstat64
11620 case TARGET_NR_fstat64
:
11621 ret
= get_errno(fstat(arg1
, &st
));
11622 if (!is_error(ret
))
11623 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11626 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
11627 #ifdef TARGET_NR_fstatat64
11628 case TARGET_NR_fstatat64
:
11630 #ifdef TARGET_NR_newfstatat
11631 case TARGET_NR_newfstatat
:
11633 if (!(p
= lock_user_string(arg2
))) {
11634 return -TARGET_EFAULT
;
11636 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
11637 unlock_user(p
, arg2
, 0);
11638 if (!is_error(ret
))
11639 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
11642 #if defined(TARGET_NR_statx)
11643 case TARGET_NR_statx
:
11645 struct target_statx
*target_stx
;
11649 p
= lock_user_string(arg2
);
11651 return -TARGET_EFAULT
;
11653 #if defined(__NR_statx)
11656 * It is assumed that struct statx is architecture independent.
11658 struct target_statx host_stx
;
11661 ret
= get_errno(sys_statx(dirfd
, p
, flags
, mask
, &host_stx
));
11662 if (!is_error(ret
)) {
11663 if (host_to_target_statx(&host_stx
, arg5
) != 0) {
11664 unlock_user(p
, arg2
, 0);
11665 return -TARGET_EFAULT
;
11669 if (ret
!= -TARGET_ENOSYS
) {
11670 unlock_user(p
, arg2
, 0);
11675 ret
= get_errno(fstatat(dirfd
, path(p
), &st
, flags
));
11676 unlock_user(p
, arg2
, 0);
11678 if (!is_error(ret
)) {
11679 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, arg5
, 0)) {
11680 return -TARGET_EFAULT
;
11682 memset(target_stx
, 0, sizeof(*target_stx
));
11683 __put_user(major(st
.st_dev
), &target_stx
->stx_dev_major
);
11684 __put_user(minor(st
.st_dev
), &target_stx
->stx_dev_minor
);
11685 __put_user(st
.st_ino
, &target_stx
->stx_ino
);
11686 __put_user(st
.st_mode
, &target_stx
->stx_mode
);
11687 __put_user(st
.st_uid
, &target_stx
->stx_uid
);
11688 __put_user(st
.st_gid
, &target_stx
->stx_gid
);
11689 __put_user(st
.st_nlink
, &target_stx
->stx_nlink
);
11690 __put_user(major(st
.st_rdev
), &target_stx
->stx_rdev_major
);
11691 __put_user(minor(st
.st_rdev
), &target_stx
->stx_rdev_minor
);
11692 __put_user(st
.st_size
, &target_stx
->stx_size
);
11693 __put_user(st
.st_blksize
, &target_stx
->stx_blksize
);
11694 __put_user(st
.st_blocks
, &target_stx
->stx_blocks
);
11695 __put_user(st
.st_atime
, &target_stx
->stx_atime
.tv_sec
);
11696 __put_user(st
.st_mtime
, &target_stx
->stx_mtime
.tv_sec
);
11697 __put_user(st
.st_ctime
, &target_stx
->stx_ctime
.tv_sec
);
11698 unlock_user_struct(target_stx
, arg5
, 1);
11703 #ifdef TARGET_NR_lchown
11704 case TARGET_NR_lchown
:
11705 if (!(p
= lock_user_string(arg1
)))
11706 return -TARGET_EFAULT
;
11707 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11708 unlock_user(p
, arg1
, 0);
11711 #ifdef TARGET_NR_getuid
11712 case TARGET_NR_getuid
:
11713 return get_errno(high2lowuid(getuid()));
11715 #ifdef TARGET_NR_getgid
11716 case TARGET_NR_getgid
:
11717 return get_errno(high2lowgid(getgid()));
11719 #ifdef TARGET_NR_geteuid
11720 case TARGET_NR_geteuid
:
11721 return get_errno(high2lowuid(geteuid()));
11723 #ifdef TARGET_NR_getegid
11724 case TARGET_NR_getegid
:
11725 return get_errno(high2lowgid(getegid()));
11727 case TARGET_NR_setreuid
:
11728 return get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
11729 case TARGET_NR_setregid
:
11730 return get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
11731 case TARGET_NR_getgroups
:
11732 { /* the same code as for TARGET_NR_getgroups32 */
11733 int gidsetsize
= arg1
;
11734 target_id
*target_grouplist
;
11735 g_autofree gid_t
*grouplist
= NULL
;
11738 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
11739 return -TARGET_EINVAL
;
11741 if (gidsetsize
> 0) {
11742 grouplist
= g_try_new(gid_t
, gidsetsize
);
11744 return -TARGET_ENOMEM
;
11747 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11748 if (!is_error(ret
) && gidsetsize
> 0) {
11749 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
,
11750 gidsetsize
* sizeof(target_id
), 0);
11751 if (!target_grouplist
) {
11752 return -TARGET_EFAULT
;
11754 for (i
= 0; i
< ret
; i
++) {
11755 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
11757 unlock_user(target_grouplist
, arg2
,
11758 gidsetsize
* sizeof(target_id
));
11762 case TARGET_NR_setgroups
:
11763 { /* the same code as for TARGET_NR_setgroups32 */
11764 int gidsetsize
= arg1
;
11765 target_id
*target_grouplist
;
11766 g_autofree gid_t
*grouplist
= NULL
;
11769 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
11770 return -TARGET_EINVAL
;
11772 if (gidsetsize
> 0) {
11773 grouplist
= g_try_new(gid_t
, gidsetsize
);
11775 return -TARGET_ENOMEM
;
11777 target_grouplist
= lock_user(VERIFY_READ
, arg2
,
11778 gidsetsize
* sizeof(target_id
), 1);
11779 if (!target_grouplist
) {
11780 return -TARGET_EFAULT
;
11782 for (i
= 0; i
< gidsetsize
; i
++) {
11783 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
11785 unlock_user(target_grouplist
, arg2
,
11786 gidsetsize
* sizeof(target_id
));
11788 return get_errno(setgroups(gidsetsize
, grouplist
));
11790 case TARGET_NR_fchown
:
11791 return get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
11792 #if defined(TARGET_NR_fchownat)
11793 case TARGET_NR_fchownat
:
11794 if (!(p
= lock_user_string(arg2
)))
11795 return -TARGET_EFAULT
;
11796 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
11797 low2highgid(arg4
), arg5
));
11798 unlock_user(p
, arg2
, 0);
11801 #ifdef TARGET_NR_setresuid
11802 case TARGET_NR_setresuid
:
11803 return get_errno(sys_setresuid(low2highuid(arg1
),
11805 low2highuid(arg3
)));
11807 #ifdef TARGET_NR_getresuid
11808 case TARGET_NR_getresuid
:
11810 uid_t ruid
, euid
, suid
;
11811 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11812 if (!is_error(ret
)) {
11813 if (put_user_id(high2lowuid(ruid
), arg1
)
11814 || put_user_id(high2lowuid(euid
), arg2
)
11815 || put_user_id(high2lowuid(suid
), arg3
))
11816 return -TARGET_EFAULT
;
11821 #ifdef TARGET_NR_getresgid
11822 case TARGET_NR_setresgid
:
11823 return get_errno(sys_setresgid(low2highgid(arg1
),
11825 low2highgid(arg3
)));
11827 #ifdef TARGET_NR_getresgid
11828 case TARGET_NR_getresgid
:
11830 gid_t rgid
, egid
, sgid
;
11831 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11832 if (!is_error(ret
)) {
11833 if (put_user_id(high2lowgid(rgid
), arg1
)
11834 || put_user_id(high2lowgid(egid
), arg2
)
11835 || put_user_id(high2lowgid(sgid
), arg3
))
11836 return -TARGET_EFAULT
;
11841 #ifdef TARGET_NR_chown
11842 case TARGET_NR_chown
:
11843 if (!(p
= lock_user_string(arg1
)))
11844 return -TARGET_EFAULT
;
11845 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11846 unlock_user(p
, arg1
, 0);
11849 case TARGET_NR_setuid
:
11850 return get_errno(sys_setuid(low2highuid(arg1
)));
11851 case TARGET_NR_setgid
:
11852 return get_errno(sys_setgid(low2highgid(arg1
)));
11853 case TARGET_NR_setfsuid
:
11854 return get_errno(setfsuid(arg1
));
11855 case TARGET_NR_setfsgid
:
11856 return get_errno(setfsgid(arg1
));
11858 #ifdef TARGET_NR_lchown32
11859 case TARGET_NR_lchown32
:
11860 if (!(p
= lock_user_string(arg1
)))
11861 return -TARGET_EFAULT
;
11862 ret
= get_errno(lchown(p
, arg2
, arg3
));
11863 unlock_user(p
, arg1
, 0);
11866 #ifdef TARGET_NR_getuid32
11867 case TARGET_NR_getuid32
:
11868 return get_errno(getuid());
11871 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11872 /* Alpha specific */
11873 case TARGET_NR_getxuid
:
11877 cpu_env
->ir
[IR_A4
]=euid
;
11879 return get_errno(getuid());
11881 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11882 /* Alpha specific */
11883 case TARGET_NR_getxgid
:
11887 cpu_env
->ir
[IR_A4
]=egid
;
11889 return get_errno(getgid());
11891 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11892 /* Alpha specific */
11893 case TARGET_NR_osf_getsysinfo
:
11894 ret
= -TARGET_EOPNOTSUPP
;
11896 case TARGET_GSI_IEEE_FP_CONTROL
:
11898 uint64_t fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11899 uint64_t swcr
= cpu_env
->swcr
;
11901 swcr
&= ~SWCR_STATUS_MASK
;
11902 swcr
|= (fpcr
>> 35) & SWCR_STATUS_MASK
;
11904 if (put_user_u64 (swcr
, arg2
))
11905 return -TARGET_EFAULT
;
11910 /* case GSI_IEEE_STATE_AT_SIGNAL:
11911 -- Not implemented in linux kernel.
11913 -- Retrieves current unaligned access state; not much used.
11914 case GSI_PROC_TYPE:
11915 -- Retrieves implver information; surely not used.
11916 case GSI_GET_HWRPB:
11917 -- Grabs a copy of the HWRPB; surely not used.
11922 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11923 /* Alpha specific */
11924 case TARGET_NR_osf_setsysinfo
:
11925 ret
= -TARGET_EOPNOTSUPP
;
11927 case TARGET_SSI_IEEE_FP_CONTROL
:
11929 uint64_t swcr
, fpcr
;
11931 if (get_user_u64 (swcr
, arg2
)) {
11932 return -TARGET_EFAULT
;
11936 * The kernel calls swcr_update_status to update the
11937 * status bits from the fpcr at every point that it
11938 * could be queried. Therefore, we store the status
11939 * bits only in FPCR.
11941 cpu_env
->swcr
= swcr
& (SWCR_TRAP_ENABLE_MASK
| SWCR_MAP_MASK
);
11943 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11944 fpcr
&= ((uint64_t)FPCR_DYN_MASK
<< 32);
11945 fpcr
|= alpha_ieee_swcr_to_fpcr(swcr
);
11946 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11951 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
11953 uint64_t exc
, fpcr
, fex
;
11955 if (get_user_u64(exc
, arg2
)) {
11956 return -TARGET_EFAULT
;
11958 exc
&= SWCR_STATUS_MASK
;
11959 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11961 /* Old exceptions are not signaled. */
11962 fex
= alpha_ieee_fpcr_to_swcr(fpcr
);
11964 fex
>>= SWCR_STATUS_TO_EXCSUM_SHIFT
;
11965 fex
&= (cpu_env
)->swcr
;
11967 /* Update the hardware fpcr. */
11968 fpcr
|= alpha_ieee_swcr_to_fpcr(exc
);
11969 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11972 int si_code
= TARGET_FPE_FLTUNK
;
11973 target_siginfo_t info
;
11975 if (fex
& SWCR_TRAP_ENABLE_DNO
) {
11976 si_code
= TARGET_FPE_FLTUND
;
11978 if (fex
& SWCR_TRAP_ENABLE_INE
) {
11979 si_code
= TARGET_FPE_FLTRES
;
11981 if (fex
& SWCR_TRAP_ENABLE_UNF
) {
11982 si_code
= TARGET_FPE_FLTUND
;
11984 if (fex
& SWCR_TRAP_ENABLE_OVF
) {
11985 si_code
= TARGET_FPE_FLTOVF
;
11987 if (fex
& SWCR_TRAP_ENABLE_DZE
) {
11988 si_code
= TARGET_FPE_FLTDIV
;
11990 if (fex
& SWCR_TRAP_ENABLE_INV
) {
11991 si_code
= TARGET_FPE_FLTINV
;
11994 info
.si_signo
= SIGFPE
;
11996 info
.si_code
= si_code
;
11997 info
._sifields
._sigfault
._addr
= (cpu_env
)->pc
;
11998 queue_signal(cpu_env
, info
.si_signo
,
11999 QEMU_SI_FAULT
, &info
);
12005 /* case SSI_NVPAIRS:
12006 -- Used with SSIN_UACPROC to enable unaligned accesses.
12007 case SSI_IEEE_STATE_AT_SIGNAL:
12008 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
12009 -- Not implemented in linux kernel
12014 #ifdef TARGET_NR_osf_sigprocmask
12015 /* Alpha specific. */
12016 case TARGET_NR_osf_sigprocmask
:
12020 sigset_t set
, oldset
;
12023 case TARGET_SIG_BLOCK
:
12026 case TARGET_SIG_UNBLOCK
:
12029 case TARGET_SIG_SETMASK
:
12033 return -TARGET_EINVAL
;
12036 target_to_host_old_sigset(&set
, &mask
);
12037 ret
= do_sigprocmask(how
, &set
, &oldset
);
12039 host_to_target_old_sigset(&mask
, &oldset
);
12046 #ifdef TARGET_NR_getgid32
12047 case TARGET_NR_getgid32
:
12048 return get_errno(getgid());
12050 #ifdef TARGET_NR_geteuid32
12051 case TARGET_NR_geteuid32
:
12052 return get_errno(geteuid());
12054 #ifdef TARGET_NR_getegid32
12055 case TARGET_NR_getegid32
:
12056 return get_errno(getegid());
12058 #ifdef TARGET_NR_setreuid32
12059 case TARGET_NR_setreuid32
:
12060 return get_errno(setreuid(arg1
, arg2
));
12062 #ifdef TARGET_NR_setregid32
12063 case TARGET_NR_setregid32
:
12064 return get_errno(setregid(arg1
, arg2
));
12066 #ifdef TARGET_NR_getgroups32
12067 case TARGET_NR_getgroups32
:
12068 { /* the same code as for TARGET_NR_getgroups */
12069 int gidsetsize
= arg1
;
12070 uint32_t *target_grouplist
;
12071 g_autofree gid_t
*grouplist
= NULL
;
12074 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
12075 return -TARGET_EINVAL
;
12077 if (gidsetsize
> 0) {
12078 grouplist
= g_try_new(gid_t
, gidsetsize
);
12080 return -TARGET_ENOMEM
;
12083 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
12084 if (!is_error(ret
) && gidsetsize
> 0) {
12085 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
,
12086 gidsetsize
* 4, 0);
12087 if (!target_grouplist
) {
12088 return -TARGET_EFAULT
;
12090 for (i
= 0; i
< ret
; i
++) {
12091 target_grouplist
[i
] = tswap32(grouplist
[i
]);
12093 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
12098 #ifdef TARGET_NR_setgroups32
12099 case TARGET_NR_setgroups32
:
12100 { /* the same code as for TARGET_NR_setgroups */
12101 int gidsetsize
= arg1
;
12102 uint32_t *target_grouplist
;
12103 g_autofree gid_t
*grouplist
= NULL
;
12106 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
12107 return -TARGET_EINVAL
;
12109 if (gidsetsize
> 0) {
12110 grouplist
= g_try_new(gid_t
, gidsetsize
);
12112 return -TARGET_ENOMEM
;
12114 target_grouplist
= lock_user(VERIFY_READ
, arg2
,
12115 gidsetsize
* 4, 1);
12116 if (!target_grouplist
) {
12117 return -TARGET_EFAULT
;
12119 for (i
= 0; i
< gidsetsize
; i
++) {
12120 grouplist
[i
] = tswap32(target_grouplist
[i
]);
12122 unlock_user(target_grouplist
, arg2
, 0);
12124 return get_errno(setgroups(gidsetsize
, grouplist
));
12127 #ifdef TARGET_NR_fchown32
12128 case TARGET_NR_fchown32
:
12129 return get_errno(fchown(arg1
, arg2
, arg3
));
12131 #ifdef TARGET_NR_setresuid32
12132 case TARGET_NR_setresuid32
:
12133 return get_errno(sys_setresuid(arg1
, arg2
, arg3
));
12135 #ifdef TARGET_NR_getresuid32
12136 case TARGET_NR_getresuid32
:
12138 uid_t ruid
, euid
, suid
;
12139 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
12140 if (!is_error(ret
)) {
12141 if (put_user_u32(ruid
, arg1
)
12142 || put_user_u32(euid
, arg2
)
12143 || put_user_u32(suid
, arg3
))
12144 return -TARGET_EFAULT
;
12149 #ifdef TARGET_NR_setresgid32
12150 case TARGET_NR_setresgid32
:
12151 return get_errno(sys_setresgid(arg1
, arg2
, arg3
));
12153 #ifdef TARGET_NR_getresgid32
12154 case TARGET_NR_getresgid32
:
12156 gid_t rgid
, egid
, sgid
;
12157 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
12158 if (!is_error(ret
)) {
12159 if (put_user_u32(rgid
, arg1
)
12160 || put_user_u32(egid
, arg2
)
12161 || put_user_u32(sgid
, arg3
))
12162 return -TARGET_EFAULT
;
12167 #ifdef TARGET_NR_chown32
12168 case TARGET_NR_chown32
:
12169 if (!(p
= lock_user_string(arg1
)))
12170 return -TARGET_EFAULT
;
12171 ret
= get_errno(chown(p
, arg2
, arg3
));
12172 unlock_user(p
, arg1
, 0);
12175 #ifdef TARGET_NR_setuid32
12176 case TARGET_NR_setuid32
:
12177 return get_errno(sys_setuid(arg1
));
12179 #ifdef TARGET_NR_setgid32
12180 case TARGET_NR_setgid32
:
12181 return get_errno(sys_setgid(arg1
));
12183 #ifdef TARGET_NR_setfsuid32
12184 case TARGET_NR_setfsuid32
:
12185 return get_errno(setfsuid(arg1
));
12187 #ifdef TARGET_NR_setfsgid32
12188 case TARGET_NR_setfsgid32
:
12189 return get_errno(setfsgid(arg1
));
12191 #ifdef TARGET_NR_mincore
12192 case TARGET_NR_mincore
:
12194 void *a
= lock_user(VERIFY_NONE
, arg1
, arg2
, 0);
12196 return -TARGET_ENOMEM
;
12198 p
= lock_user_string(arg3
);
12200 ret
= -TARGET_EFAULT
;
12202 ret
= get_errno(mincore(a
, arg2
, p
));
12203 unlock_user(p
, arg3
, ret
);
12205 unlock_user(a
, arg1
, 0);
12209 #ifdef TARGET_NR_arm_fadvise64_64
12210 case TARGET_NR_arm_fadvise64_64
:
12211 /* arm_fadvise64_64 looks like fadvise64_64 but
12212 * with different argument order: fd, advice, offset, len
12213 * rather than the usual fd, offset, len, advice.
12214 * Note that offset and len are both 64-bit so appear as
12215 * pairs of 32-bit registers.
12217 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
12218 target_offset64(arg5
, arg6
), arg2
);
12219 return -host_to_target_errno(ret
);
12222 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12224 #ifdef TARGET_NR_fadvise64_64
12225 case TARGET_NR_fadvise64_64
:
12226 #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
12227 /* 6 args: fd, advice, offset (high, low), len (high, low) */
12235 /* 6 args: fd, offset (high, low), len (high, low), advice */
12236 if (regpairs_aligned(cpu_env
, num
)) {
12237 /* offset is in (3,4), len in (5,6) and advice in 7 */
12245 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
),
12246 target_offset64(arg4
, arg5
), arg6
);
12247 return -host_to_target_errno(ret
);
12250 #ifdef TARGET_NR_fadvise64
12251 case TARGET_NR_fadvise64
:
12252 /* 5 args: fd, offset (high, low), len, advice */
12253 if (regpairs_aligned(cpu_env
, num
)) {
12254 /* offset is in (3,4), len in 5 and advice in 6 */
12260 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
), arg4
, arg5
);
12261 return -host_to_target_errno(ret
);
12264 #else /* not a 32-bit ABI */
12265 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
12266 #ifdef TARGET_NR_fadvise64_64
12267 case TARGET_NR_fadvise64_64
:
12269 #ifdef TARGET_NR_fadvise64
12270 case TARGET_NR_fadvise64
:
12272 #ifdef TARGET_S390X
12274 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
12275 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
12276 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
12277 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
12281 return -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
12283 #endif /* end of 64-bit ABI fadvise handling */
12285 #ifdef TARGET_NR_madvise
12286 case TARGET_NR_madvise
:
12287 return target_madvise(arg1
, arg2
, arg3
);
12289 #ifdef TARGET_NR_fcntl64
12290 case TARGET_NR_fcntl64
:
12294 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
12295 to_flock64_fn
*copyto
= copy_to_user_flock64
;
12298 if (!cpu_env
->eabi
) {
12299 copyfrom
= copy_from_user_oabi_flock64
;
12300 copyto
= copy_to_user_oabi_flock64
;
12304 cmd
= target_to_host_fcntl_cmd(arg2
);
12305 if (cmd
== -TARGET_EINVAL
) {
12310 case TARGET_F_GETLK64
:
12311 ret
= copyfrom(&fl
, arg3
);
12315 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
12317 ret
= copyto(arg3
, &fl
);
12321 case TARGET_F_SETLK64
:
12322 case TARGET_F_SETLKW64
:
12323 ret
= copyfrom(&fl
, arg3
);
12327 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
12330 ret
= do_fcntl(arg1
, arg2
, arg3
);
12336 #ifdef TARGET_NR_cacheflush
12337 case TARGET_NR_cacheflush
:
12338 /* self-modifying code is handled automatically, so nothing needed */
12341 #ifdef TARGET_NR_getpagesize
12342 case TARGET_NR_getpagesize
:
12343 return TARGET_PAGE_SIZE
;
12345 case TARGET_NR_gettid
:
12346 return get_errno(sys_gettid());
12347 #ifdef TARGET_NR_readahead
12348 case TARGET_NR_readahead
:
12349 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12350 if (regpairs_aligned(cpu_env
, num
)) {
12355 ret
= get_errno(readahead(arg1
, target_offset64(arg2
, arg3
) , arg4
));
12357 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
12362 #ifdef TARGET_NR_setxattr
12363 case TARGET_NR_listxattr
:
12364 case TARGET_NR_llistxattr
:
12368 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
12370 return -TARGET_EFAULT
;
12373 p
= lock_user_string(arg1
);
12375 if (num
== TARGET_NR_listxattr
) {
12376 ret
= get_errno(listxattr(p
, b
, arg3
));
12378 ret
= get_errno(llistxattr(p
, b
, arg3
));
12381 ret
= -TARGET_EFAULT
;
12383 unlock_user(p
, arg1
, 0);
12384 unlock_user(b
, arg2
, arg3
);
12387 case TARGET_NR_flistxattr
:
12391 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
12393 return -TARGET_EFAULT
;
12396 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
12397 unlock_user(b
, arg2
, arg3
);
12400 case TARGET_NR_setxattr
:
12401 case TARGET_NR_lsetxattr
:
12405 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
12407 return -TARGET_EFAULT
;
12410 p
= lock_user_string(arg1
);
12411 n
= lock_user_string(arg2
);
12413 if (num
== TARGET_NR_setxattr
) {
12414 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
12416 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
12419 ret
= -TARGET_EFAULT
;
12421 unlock_user(p
, arg1
, 0);
12422 unlock_user(n
, arg2
, 0);
12423 unlock_user(v
, arg3
, 0);
12426 case TARGET_NR_fsetxattr
:
12430 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
12432 return -TARGET_EFAULT
;
12435 n
= lock_user_string(arg2
);
12437 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
12439 ret
= -TARGET_EFAULT
;
12441 unlock_user(n
, arg2
, 0);
12442 unlock_user(v
, arg3
, 0);
12445 case TARGET_NR_getxattr
:
12446 case TARGET_NR_lgetxattr
:
12450 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
12452 return -TARGET_EFAULT
;
12455 p
= lock_user_string(arg1
);
12456 n
= lock_user_string(arg2
);
12458 if (num
== TARGET_NR_getxattr
) {
12459 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
12461 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
12464 ret
= -TARGET_EFAULT
;
12466 unlock_user(p
, arg1
, 0);
12467 unlock_user(n
, arg2
, 0);
12468 unlock_user(v
, arg3
, arg4
);
12471 case TARGET_NR_fgetxattr
:
12475 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
12477 return -TARGET_EFAULT
;
12480 n
= lock_user_string(arg2
);
12482 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
12484 ret
= -TARGET_EFAULT
;
12486 unlock_user(n
, arg2
, 0);
12487 unlock_user(v
, arg3
, arg4
);
12490 case TARGET_NR_removexattr
:
12491 case TARGET_NR_lremovexattr
:
12494 p
= lock_user_string(arg1
);
12495 n
= lock_user_string(arg2
);
12497 if (num
== TARGET_NR_removexattr
) {
12498 ret
= get_errno(removexattr(p
, n
));
12500 ret
= get_errno(lremovexattr(p
, n
));
12503 ret
= -TARGET_EFAULT
;
12505 unlock_user(p
, arg1
, 0);
12506 unlock_user(n
, arg2
, 0);
12509 case TARGET_NR_fremovexattr
:
12512 n
= lock_user_string(arg2
);
12514 ret
= get_errno(fremovexattr(arg1
, n
));
12516 ret
= -TARGET_EFAULT
;
12518 unlock_user(n
, arg2
, 0);
12522 #endif /* CONFIG_ATTR */
12523 #ifdef TARGET_NR_set_thread_area
12524 case TARGET_NR_set_thread_area
:
12525 #if defined(TARGET_MIPS)
12526 cpu_env
->active_tc
.CP0_UserLocal
= arg1
;
12528 #elif defined(TARGET_CRIS)
12530 ret
= -TARGET_EINVAL
;
12532 cpu_env
->pregs
[PR_PID
] = arg1
;
12536 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
12537 return do_set_thread_area(cpu_env
, arg1
);
12538 #elif defined(TARGET_M68K)
12540 TaskState
*ts
= get_task_state(cpu
);
12541 ts
->tp_value
= arg1
;
12545 return -TARGET_ENOSYS
;
12548 #ifdef TARGET_NR_get_thread_area
12549 case TARGET_NR_get_thread_area
:
12550 #if defined(TARGET_I386) && defined(TARGET_ABI32)
12551 return do_get_thread_area(cpu_env
, arg1
);
12552 #elif defined(TARGET_M68K)
12554 TaskState
*ts
= get_task_state(cpu
);
12555 return ts
->tp_value
;
12558 return -TARGET_ENOSYS
;
12561 #ifdef TARGET_NR_getdomainname
12562 case TARGET_NR_getdomainname
:
12563 return -TARGET_ENOSYS
;
12566 #ifdef TARGET_NR_clock_settime
12567 case TARGET_NR_clock_settime
:
12569 struct timespec ts
;
12571 ret
= target_to_host_timespec(&ts
, arg2
);
12572 if (!is_error(ret
)) {
12573 ret
= get_errno(clock_settime(arg1
, &ts
));
12578 #ifdef TARGET_NR_clock_settime64
12579 case TARGET_NR_clock_settime64
:
12581 struct timespec ts
;
12583 ret
= target_to_host_timespec64(&ts
, arg2
);
12584 if (!is_error(ret
)) {
12585 ret
= get_errno(clock_settime(arg1
, &ts
));
12590 #ifdef TARGET_NR_clock_gettime
12591 case TARGET_NR_clock_gettime
:
12593 struct timespec ts
;
12594 ret
= get_errno(clock_gettime(arg1
, &ts
));
12595 if (!is_error(ret
)) {
12596 ret
= host_to_target_timespec(arg2
, &ts
);
12601 #ifdef TARGET_NR_clock_gettime64
12602 case TARGET_NR_clock_gettime64
:
12604 struct timespec ts
;
12605 ret
= get_errno(clock_gettime(arg1
, &ts
));
12606 if (!is_error(ret
)) {
12607 ret
= host_to_target_timespec64(arg2
, &ts
);
12612 #ifdef TARGET_NR_clock_getres
12613 case TARGET_NR_clock_getres
:
12615 struct timespec ts
;
12616 ret
= get_errno(clock_getres(arg1
, &ts
));
12617 if (!is_error(ret
)) {
12618 host_to_target_timespec(arg2
, &ts
);
12623 #ifdef TARGET_NR_clock_getres_time64
12624 case TARGET_NR_clock_getres_time64
:
12626 struct timespec ts
;
12627 ret
= get_errno(clock_getres(arg1
, &ts
));
12628 if (!is_error(ret
)) {
12629 host_to_target_timespec64(arg2
, &ts
);
12634 #ifdef TARGET_NR_clock_nanosleep
12635 case TARGET_NR_clock_nanosleep
:
12637 struct timespec ts
;
12638 if (target_to_host_timespec(&ts
, arg3
)) {
12639 return -TARGET_EFAULT
;
12641 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
12642 &ts
, arg4
? &ts
: NULL
));
12644 * if the call is interrupted by a signal handler, it fails
12645 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12646 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12648 if (ret
== -TARGET_EINTR
&& arg4
&& arg2
!= TIMER_ABSTIME
&&
12649 host_to_target_timespec(arg4
, &ts
)) {
12650 return -TARGET_EFAULT
;
12656 #ifdef TARGET_NR_clock_nanosleep_time64
12657 case TARGET_NR_clock_nanosleep_time64
:
12659 struct timespec ts
;
12661 if (target_to_host_timespec64(&ts
, arg3
)) {
12662 return -TARGET_EFAULT
;
12665 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
12666 &ts
, arg4
? &ts
: NULL
));
12668 if (ret
== -TARGET_EINTR
&& arg4
&& arg2
!= TIMER_ABSTIME
&&
12669 host_to_target_timespec64(arg4
, &ts
)) {
12670 return -TARGET_EFAULT
;
12676 #if defined(TARGET_NR_set_tid_address)
12677 case TARGET_NR_set_tid_address
:
12679 TaskState
*ts
= get_task_state(cpu
);
12680 ts
->child_tidptr
= arg1
;
12681 /* do not call host set_tid_address() syscall, instead return tid() */
12682 return get_errno(sys_gettid());
12686 case TARGET_NR_tkill
:
12687 return get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
12689 case TARGET_NR_tgkill
:
12690 return get_errno(safe_tgkill((int)arg1
, (int)arg2
,
12691 target_to_host_signal(arg3
)));
12693 #ifdef TARGET_NR_set_robust_list
12694 case TARGET_NR_set_robust_list
:
12695 case TARGET_NR_get_robust_list
:
12696 /* The ABI for supporting robust futexes has userspace pass
12697 * the kernel a pointer to a linked list which is updated by
12698 * userspace after the syscall; the list is walked by the kernel
12699 * when the thread exits. Since the linked list in QEMU guest
12700 * memory isn't a valid linked list for the host and we have
12701 * no way to reliably intercept the thread-death event, we can't
12702 * support these. Silently return ENOSYS so that guest userspace
12703 * falls back to a non-robust futex implementation (which should
12704 * be OK except in the corner case of the guest crashing while
12705 * holding a mutex that is shared with another process via
12708 return -TARGET_ENOSYS
;
12711 #if defined(TARGET_NR_utimensat)
12712 case TARGET_NR_utimensat
:
12714 struct timespec
*tsp
, ts
[2];
12718 if (target_to_host_timespec(ts
, arg3
)) {
12719 return -TARGET_EFAULT
;
12721 if (target_to_host_timespec(ts
+ 1, arg3
+
12722 sizeof(struct target_timespec
))) {
12723 return -TARGET_EFAULT
;
12728 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
12730 if (!(p
= lock_user_string(arg2
))) {
12731 return -TARGET_EFAULT
;
12733 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
12734 unlock_user(p
, arg2
, 0);
12739 #ifdef TARGET_NR_utimensat_time64
12740 case TARGET_NR_utimensat_time64
:
12742 struct timespec
*tsp
, ts
[2];
12746 if (target_to_host_timespec64(ts
, arg3
)) {
12747 return -TARGET_EFAULT
;
12749 if (target_to_host_timespec64(ts
+ 1, arg3
+
12750 sizeof(struct target__kernel_timespec
))) {
12751 return -TARGET_EFAULT
;
12756 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
12758 p
= lock_user_string(arg2
);
12760 return -TARGET_EFAULT
;
12762 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
12763 unlock_user(p
, arg2
, 0);
12768 #ifdef TARGET_NR_futex
12769 case TARGET_NR_futex
:
12770 return do_futex(cpu
, false, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12772 #ifdef TARGET_NR_futex_time64
12773 case TARGET_NR_futex_time64
:
12774 return do_futex(cpu
, true, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12776 #ifdef CONFIG_INOTIFY
12777 #if defined(TARGET_NR_inotify_init)
12778 case TARGET_NR_inotify_init
:
12779 ret
= get_errno(inotify_init());
12781 fd_trans_register(ret
, &target_inotify_trans
);
12785 #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
12786 case TARGET_NR_inotify_init1
:
12787 ret
= get_errno(inotify_init1(target_to_host_bitmask(arg1
,
12788 fcntl_flags_tbl
)));
12790 fd_trans_register(ret
, &target_inotify_trans
);
12794 #if defined(TARGET_NR_inotify_add_watch)
12795 case TARGET_NR_inotify_add_watch
:
12796 p
= lock_user_string(arg2
);
12797 ret
= get_errno(inotify_add_watch(arg1
, path(p
), arg3
));
12798 unlock_user(p
, arg2
, 0);
12801 #if defined(TARGET_NR_inotify_rm_watch)
12802 case TARGET_NR_inotify_rm_watch
:
12803 return get_errno(inotify_rm_watch(arg1
, arg2
));
12807 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
12808 case TARGET_NR_mq_open
:
12810 struct mq_attr posix_mq_attr
;
12811 struct mq_attr
*pposix_mq_attr
;
12814 host_flags
= target_to_host_bitmask(arg2
, fcntl_flags_tbl
);
12815 pposix_mq_attr
= NULL
;
12817 if (copy_from_user_mq_attr(&posix_mq_attr
, arg4
) != 0) {
12818 return -TARGET_EFAULT
;
12820 pposix_mq_attr
= &posix_mq_attr
;
12822 p
= lock_user_string(arg1
- 1);
12824 return -TARGET_EFAULT
;
12826 ret
= get_errno(mq_open(p
, host_flags
, arg3
, pposix_mq_attr
));
12827 unlock_user (p
, arg1
, 0);
12831 case TARGET_NR_mq_unlink
:
12832 p
= lock_user_string(arg1
- 1);
12834 return -TARGET_EFAULT
;
12836 ret
= get_errno(mq_unlink(p
));
12837 unlock_user (p
, arg1
, 0);
12840 #ifdef TARGET_NR_mq_timedsend
12841 case TARGET_NR_mq_timedsend
:
12843 struct timespec ts
;
12845 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12847 if (target_to_host_timespec(&ts
, arg5
)) {
12848 return -TARGET_EFAULT
;
12850 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12851 if (!is_error(ret
) && host_to_target_timespec(arg5
, &ts
)) {
12852 return -TARGET_EFAULT
;
12855 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12857 unlock_user (p
, arg2
, arg3
);
12861 #ifdef TARGET_NR_mq_timedsend_time64
12862 case TARGET_NR_mq_timedsend_time64
:
12864 struct timespec ts
;
12866 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
12868 if (target_to_host_timespec64(&ts
, arg5
)) {
12869 return -TARGET_EFAULT
;
12871 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12872 if (!is_error(ret
) && host_to_target_timespec64(arg5
, &ts
)) {
12873 return -TARGET_EFAULT
;
12876 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12878 unlock_user(p
, arg2
, arg3
);
12883 #ifdef TARGET_NR_mq_timedreceive
12884 case TARGET_NR_mq_timedreceive
:
12886 struct timespec ts
;
12889 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12891 if (target_to_host_timespec(&ts
, arg5
)) {
12892 return -TARGET_EFAULT
;
12894 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12896 if (!is_error(ret
) && host_to_target_timespec(arg5
, &ts
)) {
12897 return -TARGET_EFAULT
;
12900 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12903 unlock_user (p
, arg2
, arg3
);
12905 put_user_u32(prio
, arg4
);
12909 #ifdef TARGET_NR_mq_timedreceive_time64
12910 case TARGET_NR_mq_timedreceive_time64
:
12912 struct timespec ts
;
12915 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
12917 if (target_to_host_timespec64(&ts
, arg5
)) {
12918 return -TARGET_EFAULT
;
12920 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12922 if (!is_error(ret
) && host_to_target_timespec64(arg5
, &ts
)) {
12923 return -TARGET_EFAULT
;
12926 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12929 unlock_user(p
, arg2
, arg3
);
12931 put_user_u32(prio
, arg4
);
12937 /* Not implemented for now... */
12938 /* case TARGET_NR_mq_notify: */
12941 case TARGET_NR_mq_getsetattr
:
12943 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
12946 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
12947 ret
= get_errno(mq_setattr(arg1
, &posix_mq_attr_in
,
12948 &posix_mq_attr_out
));
12949 } else if (arg3
!= 0) {
12950 ret
= get_errno(mq_getattr(arg1
, &posix_mq_attr_out
));
12952 if (ret
== 0 && arg3
!= 0) {
12953 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
12959 #ifdef CONFIG_SPLICE
12960 #ifdef TARGET_NR_tee
12961 case TARGET_NR_tee
:
12963 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
12967 #ifdef TARGET_NR_splice
12968 case TARGET_NR_splice
:
12970 loff_t loff_in
, loff_out
;
12971 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
12973 if (get_user_u64(loff_in
, arg2
)) {
12974 return -TARGET_EFAULT
;
12976 ploff_in
= &loff_in
;
12979 if (get_user_u64(loff_out
, arg4
)) {
12980 return -TARGET_EFAULT
;
12982 ploff_out
= &loff_out
;
12984 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
12986 if (put_user_u64(loff_in
, arg2
)) {
12987 return -TARGET_EFAULT
;
12991 if (put_user_u64(loff_out
, arg4
)) {
12992 return -TARGET_EFAULT
;
12998 #ifdef TARGET_NR_vmsplice
12999 case TARGET_NR_vmsplice
:
13001 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
13003 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
13004 unlock_iovec(vec
, arg2
, arg3
, 0);
13006 ret
= -host_to_target_errno(errno
);
13011 #endif /* CONFIG_SPLICE */
13012 #ifdef CONFIG_EVENTFD
13013 #if defined(TARGET_NR_eventfd)
13014 case TARGET_NR_eventfd
:
13015 ret
= get_errno(eventfd(arg1
, 0));
13017 fd_trans_register(ret
, &target_eventfd_trans
);
13021 #if defined(TARGET_NR_eventfd2)
13022 case TARGET_NR_eventfd2
:
13024 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK_MASK
| TARGET_O_CLOEXEC
));
13025 if (arg2
& TARGET_O_NONBLOCK
) {
13026 host_flags
|= O_NONBLOCK
;
13028 if (arg2
& TARGET_O_CLOEXEC
) {
13029 host_flags
|= O_CLOEXEC
;
13031 ret
= get_errno(eventfd(arg1
, host_flags
));
13033 fd_trans_register(ret
, &target_eventfd_trans
);
13038 #endif /* CONFIG_EVENTFD */
13039 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
13040 case TARGET_NR_fallocate
:
13041 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13042 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
13043 target_offset64(arg5
, arg6
)));
13045 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
13049 #if defined(CONFIG_SYNC_FILE_RANGE)
13050 #if defined(TARGET_NR_sync_file_range)
13051 case TARGET_NR_sync_file_range
:
13052 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13053 #if defined(TARGET_MIPS)
13054 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
13055 target_offset64(arg5
, arg6
), arg7
));
13057 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
13058 target_offset64(arg4
, arg5
), arg6
));
13059 #endif /* !TARGET_MIPS */
13061 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
13065 #if defined(TARGET_NR_sync_file_range2) || \
13066 defined(TARGET_NR_arm_sync_file_range)
13067 #if defined(TARGET_NR_sync_file_range2)
13068 case TARGET_NR_sync_file_range2
:
13070 #if defined(TARGET_NR_arm_sync_file_range)
13071 case TARGET_NR_arm_sync_file_range
:
13073 /* This is like sync_file_range but the arguments are reordered */
13074 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13075 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
13076 target_offset64(arg5
, arg6
), arg2
));
13078 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
13083 #if defined(TARGET_NR_signalfd4)
13084 case TARGET_NR_signalfd4
:
13085 return do_signalfd4(arg1
, arg2
, arg4
);
13087 #if defined(TARGET_NR_signalfd)
13088 case TARGET_NR_signalfd
:
13089 return do_signalfd4(arg1
, arg2
, 0);
13091 #if defined(CONFIG_EPOLL)
13092 #if defined(TARGET_NR_epoll_create)
13093 case TARGET_NR_epoll_create
:
13094 return get_errno(epoll_create(arg1
));
13096 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
13097 case TARGET_NR_epoll_create1
:
13098 return get_errno(epoll_create1(target_to_host_bitmask(arg1
, fcntl_flags_tbl
)));
13100 #if defined(TARGET_NR_epoll_ctl)
13101 case TARGET_NR_epoll_ctl
:
13103 struct epoll_event ep
;
13104 struct epoll_event
*epp
= 0;
13106 if (arg2
!= EPOLL_CTL_DEL
) {
13107 struct target_epoll_event
*target_ep
;
13108 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
13109 return -TARGET_EFAULT
;
13111 ep
.events
= tswap32(target_ep
->events
);
13113 * The epoll_data_t union is just opaque data to the kernel,
13114 * so we transfer all 64 bits across and need not worry what
13115 * actual data type it is.
13117 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
13118 unlock_user_struct(target_ep
, arg4
, 0);
13121 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
13122 * non-null pointer, even though this argument is ignored.
13127 return get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
13131 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
13132 #if defined(TARGET_NR_epoll_wait)
13133 case TARGET_NR_epoll_wait
:
13135 #if defined(TARGET_NR_epoll_pwait)
13136 case TARGET_NR_epoll_pwait
:
13139 struct target_epoll_event
*target_ep
;
13140 struct epoll_event
*ep
;
13142 int maxevents
= arg3
;
13143 int timeout
= arg4
;
13145 if (maxevents
<= 0 || maxevents
> TARGET_EP_MAX_EVENTS
) {
13146 return -TARGET_EINVAL
;
13149 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
13150 maxevents
* sizeof(struct target_epoll_event
), 1);
13152 return -TARGET_EFAULT
;
13155 ep
= g_try_new(struct epoll_event
, maxevents
);
13157 unlock_user(target_ep
, arg2
, 0);
13158 return -TARGET_ENOMEM
;
13162 #if defined(TARGET_NR_epoll_pwait)
13163 case TARGET_NR_epoll_pwait
:
13165 sigset_t
*set
= NULL
;
13168 ret
= process_sigsuspend_mask(&set
, arg5
, arg6
);
13174 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
13175 set
, SIGSET_T_SIZE
));
13178 finish_sigsuspend_mask(ret
);
13183 #if defined(TARGET_NR_epoll_wait)
13184 case TARGET_NR_epoll_wait
:
13185 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
13190 ret
= -TARGET_ENOSYS
;
13192 if (!is_error(ret
)) {
13194 for (i
= 0; i
< ret
; i
++) {
13195 target_ep
[i
].events
= tswap32(ep
[i
].events
);
13196 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
13198 unlock_user(target_ep
, arg2
,
13199 ret
* sizeof(struct target_epoll_event
));
13201 unlock_user(target_ep
, arg2
, 0);
13208 #ifdef TARGET_NR_prlimit64
13209 case TARGET_NR_prlimit64
:
13211 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
13212 struct target_rlimit64
*target_rnew
, *target_rold
;
13213 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
13214 int resource
= target_to_host_resource(arg2
);
13216 if (arg3
&& (resource
!= RLIMIT_AS
&&
13217 resource
!= RLIMIT_DATA
&&
13218 resource
!= RLIMIT_STACK
)) {
13219 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
13220 return -TARGET_EFAULT
;
13222 __get_user(rnew
.rlim_cur
, &target_rnew
->rlim_cur
);
13223 __get_user(rnew
.rlim_max
, &target_rnew
->rlim_max
);
13224 unlock_user_struct(target_rnew
, arg3
, 0);
13228 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
13229 if (!is_error(ret
) && arg4
) {
13230 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
13231 return -TARGET_EFAULT
;
13233 __put_user(rold
.rlim_cur
, &target_rold
->rlim_cur
);
13234 __put_user(rold
.rlim_max
, &target_rold
->rlim_max
);
13235 unlock_user_struct(target_rold
, arg4
, 1);
13240 #ifdef TARGET_NR_gethostname
13241 case TARGET_NR_gethostname
:
13243 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
13245 ret
= get_errno(gethostname(name
, arg2
));
13246 unlock_user(name
, arg1
, arg2
);
13248 ret
= -TARGET_EFAULT
;
13253 #ifdef TARGET_NR_atomic_cmpxchg_32
13254 case TARGET_NR_atomic_cmpxchg_32
:
13256 /* should use start_exclusive from main.c */
13257 abi_ulong mem_value
;
13258 if (get_user_u32(mem_value
, arg6
)) {
13259 target_siginfo_t info
;
13260 info
.si_signo
= SIGSEGV
;
13262 info
.si_code
= TARGET_SEGV_MAPERR
;
13263 info
._sifields
._sigfault
._addr
= arg6
;
13264 queue_signal(cpu_env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
13268 if (mem_value
== arg2
)
13269 put_user_u32(arg1
, arg6
);
13273 #ifdef TARGET_NR_atomic_barrier
13274 case TARGET_NR_atomic_barrier
:
13275 /* Like the kernel implementation and the
13276 qemu arm barrier, no-op this? */
13280 #ifdef TARGET_NR_timer_create
13281 case TARGET_NR_timer_create
:
13283 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
13285 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
13288 int timer_index
= next_free_host_timer();
13290 if (timer_index
< 0) {
13291 ret
= -TARGET_EAGAIN
;
13293 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
13296 phost_sevp
= &host_sevp
;
13297 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
13299 free_host_timer_slot(timer_index
);
13304 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
13306 free_host_timer_slot(timer_index
);
13308 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
13309 timer_delete(*phtimer
);
13310 free_host_timer_slot(timer_index
);
13311 return -TARGET_EFAULT
;
13319 #ifdef TARGET_NR_timer_settime
13320 case TARGET_NR_timer_settime
:
13322 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
13323 * struct itimerspec * old_value */
13324 target_timer_t timerid
= get_timer_id(arg1
);
13328 } else if (arg3
== 0) {
13329 ret
= -TARGET_EINVAL
;
13331 timer_t htimer
= g_posix_timers
[timerid
];
13332 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
13334 if (target_to_host_itimerspec(&hspec_new
, arg3
)) {
13335 return -TARGET_EFAULT
;
13338 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
13339 if (arg4
&& host_to_target_itimerspec(arg4
, &hspec_old
)) {
13340 return -TARGET_EFAULT
;
13347 #ifdef TARGET_NR_timer_settime64
13348 case TARGET_NR_timer_settime64
:
13350 target_timer_t timerid
= get_timer_id(arg1
);
13354 } else if (arg3
== 0) {
13355 ret
= -TARGET_EINVAL
;
13357 timer_t htimer
= g_posix_timers
[timerid
];
13358 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
13360 if (target_to_host_itimerspec64(&hspec_new
, arg3
)) {
13361 return -TARGET_EFAULT
;
13364 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
13365 if (arg4
&& host_to_target_itimerspec64(arg4
, &hspec_old
)) {
13366 return -TARGET_EFAULT
;
13373 #ifdef TARGET_NR_timer_gettime
13374 case TARGET_NR_timer_gettime
:
13376 /* args: timer_t timerid, struct itimerspec *curr_value */
13377 target_timer_t timerid
= get_timer_id(arg1
);
13381 } else if (!arg2
) {
13382 ret
= -TARGET_EFAULT
;
13384 timer_t htimer
= g_posix_timers
[timerid
];
13385 struct itimerspec hspec
;
13386 ret
= get_errno(timer_gettime(htimer
, &hspec
));
13388 if (host_to_target_itimerspec(arg2
, &hspec
)) {
13389 ret
= -TARGET_EFAULT
;
13396 #ifdef TARGET_NR_timer_gettime64
13397 case TARGET_NR_timer_gettime64
:
13399 /* args: timer_t timerid, struct itimerspec64 *curr_value */
13400 target_timer_t timerid
= get_timer_id(arg1
);
13404 } else if (!arg2
) {
13405 ret
= -TARGET_EFAULT
;
13407 timer_t htimer
= g_posix_timers
[timerid
];
13408 struct itimerspec hspec
;
13409 ret
= get_errno(timer_gettime(htimer
, &hspec
));
13411 if (host_to_target_itimerspec64(arg2
, &hspec
)) {
13412 ret
= -TARGET_EFAULT
;
13419 #ifdef TARGET_NR_timer_getoverrun
13420 case TARGET_NR_timer_getoverrun
:
13422 /* args: timer_t timerid */
13423 target_timer_t timerid
= get_timer_id(arg1
);
13428 timer_t htimer
= g_posix_timers
[timerid
];
13429 ret
= get_errno(timer_getoverrun(htimer
));
13435 #ifdef TARGET_NR_timer_delete
13436 case TARGET_NR_timer_delete
:
13438 /* args: timer_t timerid */
13439 target_timer_t timerid
= get_timer_id(arg1
);
13444 timer_t htimer
= g_posix_timers
[timerid
];
13445 ret
= get_errno(timer_delete(htimer
));
13446 free_host_timer_slot(timerid
);
13452 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13453 case TARGET_NR_timerfd_create
:
13454 ret
= get_errno(timerfd_create(arg1
,
13455 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
13457 fd_trans_register(ret
, &target_timerfd_trans
);
13462 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13463 case TARGET_NR_timerfd_gettime
:
13465 struct itimerspec its_curr
;
13467 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
13469 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
13470 return -TARGET_EFAULT
;
13476 #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13477 case TARGET_NR_timerfd_gettime64
:
13479 struct itimerspec its_curr
;
13481 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
13483 if (arg2
&& host_to_target_itimerspec64(arg2
, &its_curr
)) {
13484 return -TARGET_EFAULT
;
13490 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13491 case TARGET_NR_timerfd_settime
:
13493 struct itimerspec its_new
, its_old
, *p_new
;
13496 if (target_to_host_itimerspec(&its_new
, arg3
)) {
13497 return -TARGET_EFAULT
;
13504 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
13506 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
13507 return -TARGET_EFAULT
;
13513 #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13514 case TARGET_NR_timerfd_settime64
:
13516 struct itimerspec its_new
, its_old
, *p_new
;
13519 if (target_to_host_itimerspec64(&its_new
, arg3
)) {
13520 return -TARGET_EFAULT
;
13527 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
13529 if (arg4
&& host_to_target_itimerspec64(arg4
, &its_old
)) {
13530 return -TARGET_EFAULT
;
13536 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13537 case TARGET_NR_ioprio_get
:
13538 return get_errno(ioprio_get(arg1
, arg2
));
13541 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13542 case TARGET_NR_ioprio_set
:
13543 return get_errno(ioprio_set(arg1
, arg2
, arg3
));
13546 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13547 case TARGET_NR_setns
:
13548 return get_errno(setns(arg1
, arg2
));
13550 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13551 case TARGET_NR_unshare
:
13552 return get_errno(unshare(arg1
));
13554 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13555 case TARGET_NR_kcmp
:
13556 return get_errno(kcmp(arg1
, arg2
, arg3
, arg4
, arg5
));
13558 #ifdef TARGET_NR_swapcontext
13559 case TARGET_NR_swapcontext
:
13560 /* PowerPC specific. */
13561 return do_swapcontext(cpu_env
, arg1
, arg2
, arg3
);
13563 #ifdef TARGET_NR_memfd_create
13564 case TARGET_NR_memfd_create
:
13565 p
= lock_user_string(arg1
);
13567 return -TARGET_EFAULT
;
13569 ret
= get_errno(memfd_create(p
, arg2
));
13570 fd_trans_unregister(ret
);
13571 unlock_user(p
, arg1
, 0);
13574 #if defined TARGET_NR_membarrier && defined __NR_membarrier
13575 case TARGET_NR_membarrier
:
13576 return get_errno(membarrier(arg1
, arg2
));
13579 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13580 case TARGET_NR_copy_file_range
:
13582 loff_t inoff
, outoff
;
13583 loff_t
*pinoff
= NULL
, *poutoff
= NULL
;
13586 if (get_user_u64(inoff
, arg2
)) {
13587 return -TARGET_EFAULT
;
13592 if (get_user_u64(outoff
, arg4
)) {
13593 return -TARGET_EFAULT
;
13597 /* Do not sign-extend the count parameter. */
13598 ret
= get_errno(safe_copy_file_range(arg1
, pinoff
, arg3
, poutoff
,
13599 (abi_ulong
)arg5
, arg6
));
13600 if (!is_error(ret
) && ret
> 0) {
13602 if (put_user_u64(inoff
, arg2
)) {
13603 return -TARGET_EFAULT
;
13607 if (put_user_u64(outoff
, arg4
)) {
13608 return -TARGET_EFAULT
;
13616 #if defined(TARGET_NR_pivot_root)
13617 case TARGET_NR_pivot_root
:
13620 p
= lock_user_string(arg1
); /* new_root */
13621 p2
= lock_user_string(arg2
); /* put_old */
13623 ret
= -TARGET_EFAULT
;
13625 ret
= get_errno(pivot_root(p
, p2
));
13627 unlock_user(p2
, arg2
, 0);
13628 unlock_user(p
, arg1
, 0);
13633 #if defined(TARGET_NR_riscv_hwprobe)
13634 case TARGET_NR_riscv_hwprobe
:
13635 return do_riscv_hwprobe(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
);
13639 qemu_log_mask(LOG_UNIMP
, "Unsupported syscall: %d\n", num
);
13640 return -TARGET_ENOSYS
;
13645 abi_long
do_syscall(CPUArchState
*cpu_env
, int num
, abi_long arg1
,
13646 abi_long arg2
, abi_long arg3
, abi_long arg4
,
13647 abi_long arg5
, abi_long arg6
, abi_long arg7
,
13650 CPUState
*cpu
= env_cpu(cpu_env
);
13653 #ifdef DEBUG_ERESTARTSYS
13654 /* Debug-only code for exercising the syscall-restart code paths
13655 * in the per-architecture cpu main loops: restart every syscall
13656 * the guest makes once before letting it through.
13662 return -QEMU_ERESTARTSYS
;
13667 record_syscall_start(cpu
, num
, arg1
,
13668 arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
13670 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
13671 print_syscall(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
13674 ret
= do_syscall1(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
,
13675 arg5
, arg6
, arg7
, arg8
);
13677 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
13678 print_syscall_ret(cpu_env
, num
, ret
, arg1
, arg2
,
13679 arg3
, arg4
, arg5
, arg6
);
13682 record_syscall_return(cpu
, num
, ret
);