Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / linux-user / syscall.c
bloba48efb77065505c3eec343f244fb4ff3e5cfe2b7
1 /*
2 * Linux syscalls
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"
28 #include <elf.h>
29 #include <endian.h>
30 #include <grp.h>
31 #include <sys/ipc.h>
32 #include <sys/msg.h>
33 #include <sys/wait.h>
34 #include <sys/mount.h>
35 #include <sys/file.h>
36 #include <sys/fsuid.h>
37 #include <sys/personality.h>
38 #include <sys/prctl.h>
39 #include <sys/resource.h>
40 #include <sys/swap.h>
41 #include <linux/capability.h>
42 #include <sched.h>
43 #include <sys/timex.h>
44 #include <sys/socket.h>
45 #include <linux/sockios.h>
46 #include <sys/un.h>
47 #include <sys/uio.h>
48 #include <poll.h>
49 #include <sys/times.h>
50 #include <sys/shm.h>
51 #include <sys/sem.h>
52 #include <sys/statfs.h>
53 #include <utime.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>
68 #ifdef CONFIG_TIMERFD
69 #include <sys/timerfd.h>
70 #endif
71 #ifdef CONFIG_EVENTFD
72 #include <sys/eventfd.h>
73 #endif
74 #ifdef CONFIG_EPOLL
75 #include <sys/epoll.h>
76 #endif
77 #ifdef CONFIG_ATTR
78 #include "qemu/xattr.h"
79 #endif
80 #ifdef CONFIG_SENDFILE
81 #include <sys/sendfile.h>
82 #endif
83 #ifdef HAVE_SYS_KCOV_H
84 #include <sys/kcov.h>
85 #endif
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>
99 #include <linux/kd.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>
105 #endif
106 #include <linux/fb.h>
107 #if defined(CONFIG_USBFS)
108 #include <linux/usbdevice_fs.h>
109 #include <linux/usb/ch9.h>
110 #endif
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>
122 #ifdef HAVE_BTRFS_H
123 #include <linux/btrfs.h>
124 #endif
125 #ifdef HAVE_DRM_H
126 #include <libdrm/drm.h>
127 #include <libdrm/i915_drm.h>
128 #endif
129 #include "linux_loop.h"
130 #include "uname.h"
132 #include "qemu.h"
133 #include "user-internals.h"
134 #include "strace.h"
135 #include "signal-common.h"
136 #include "loader.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"
147 #ifndef CLONE_IO
148 #define CLONE_IO 0x80000000 /* Clone io context */
149 #endif
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)
173 #ifndef CLONE_PIDFD
174 # define CLONE_PIDFD 0x00001000
175 #endif
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)
213 #undef _syscall0
214 #undef _syscall1
215 #undef _syscall2
216 #undef _syscall3
217 #undef _syscall4
218 #undef _syscall5
219 #undef _syscall6
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, \
252 type5,arg5) \
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, \
262 type6 arg6) \
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
278 #endif
279 #if defined(__NR_futex_time64)
280 # define __NR_sys_futex_time64 __NR_futex_time64
281 #endif
282 #define __NR_sys_statx __NR_statx
284 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
285 #define __NR__llseek __NR_lseek
286 #endif
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
291 #endif
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
296 #endif
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
309 #endif
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);
313 #endif
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);
318 #endif
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);
322 #endif
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,
325 siginfo_t *, uinfo)
326 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
327 #ifdef __NR_exit_group
328 _syscall1(int,exit_group,int,error_code)
329 #endif
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)
335 #endif
336 #endif
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)
340 #endif
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)
344 #endif
345 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
346 _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
347 #endif
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);
351 #endif
352 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
353 _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
354 #endif
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 */
362 struct sched_attr {
363 uint32_t size;
364 uint32_t sched_policy;
365 uint64_t sched_flags;
366 int32_t sched_nice;
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,
394 void *, arg);
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)
401 #endif
402 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
403 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
404 #endif
405 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
406 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
407 #endif
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)
412 #endif
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)
420 #endif
421 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
422 _syscall2(int, membarrier, int, cmd, int, flags)
423 #endif
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, },
441 #endif
442 #if defined(O_NOATIME)
443 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
444 #endif
445 #if defined(O_CLOEXEC)
446 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
447 #endif
448 #if defined(O_PATH)
449 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
450 #endif
451 #if defined(O_TMPFILE)
452 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
453 #endif
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, },
457 #endif
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)
467 #else
468 static int sys_utimensat(int dirfd, const char *pathname,
469 const struct timespec times[2], int flags)
471 errno = ENOSYS;
472 return -1;
474 #endif
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)
482 #else
483 static int sys_renameat2(int oldfd, const char *old,
484 int newfd, const char *new, int flags)
486 if (flags == 0) {
487 return renameat(oldfd, old, newfd, new);
489 errno = ENOSYS;
490 return -1;
492 #endif
493 #endif /* TARGET_NR_renameat2 */
495 #ifdef CONFIG_INOTIFY
496 #include <sys/inotify.h>
497 #else
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
508 #endif
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 {
512 uint64_t rlim_cur;
513 uint64_t rlim_max;
515 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
516 const struct host_rlimit64 *, new_limit,
517 struct host_rlimit64 *, old_limit)
518 #endif
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)
529 int k;
530 for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
531 if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
532 return k;
535 return -1;
538 static inline void free_host_timer_slot(int id)
540 qatomic_store_release(g_posix_timer_allocated + id, 0);
542 #endif
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"
549 #undef E
550 default:
551 return host_errno;
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"
560 #undef E
561 default:
562 return target_errno;
566 abi_long get_errno(abi_long ret)
568 if (ret == -1)
569 return -host_to_target_errno(errno);
570 else
571 return ret;
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)
588 int i;
589 uint8_t b;
590 if (usize <= ksize) {
591 return 1;
593 for (i = ksize; i < usize; i++) {
594 if (get_user_u8(b, addr + i)) {
595 return -TARGET_EFAULT;
597 if (b != 0) {
598 return 0;
601 return 1;
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, \
629 type4, arg4) \
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, \
638 type5 arg5) \
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)
658 #endif
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)
668 #endif
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,
672 size_t, sigsetsize)
673 #endif
674 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
675 int, maxevents, int, timeout, const sigset_t *, sigmask,
676 size_t, sigsetsize)
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)
680 #endif
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)
684 #endif
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,
696 socklen_t, addrlen)
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)
707 #endif
708 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
709 int, flags)
710 #if defined(TARGET_NR_nanosleep)
711 safe_syscall2(int, nanosleep, const struct timespec *, req,
712 struct timespec *, rem)
713 #endif
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)
718 #endif
719 #ifdef __NR_ipc
720 #ifdef __s390x__
721 safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
722 void *, ptr)
723 #else
724 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
725 void *, ptr, long, fifth)
726 #endif
727 #endif
728 #ifdef __NR_msgsnd
729 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
730 int, flags)
731 #endif
732 #ifdef __NR_msgrcv
733 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
734 long, msgtype, int, flags)
735 #endif
736 #ifdef __NR_semtimedop
737 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
738 unsigned, nsops, const struct timespec *, timeout)
739 #endif
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)
744 #endif
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)
749 #endif
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,
753 unsigned int, flags)
754 #endif
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
758 * the libc function.
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.
766 #ifdef __NR_fcntl64
767 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
768 #else
769 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
770 #endif
772 static inline int host_to_target_sock_type(int host_type)
774 int target_type;
776 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
777 case SOCK_DGRAM:
778 target_type = TARGET_SOCK_DGRAM;
779 break;
780 case SOCK_STREAM:
781 target_type = TARGET_SOCK_STREAM;
782 break;
783 default:
784 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
785 break;
788 #if defined(SOCK_CLOEXEC)
789 if (host_type & SOCK_CLOEXEC) {
790 target_type |= TARGET_SOCK_CLOEXEC;
792 #endif
794 #if defined(SOCK_NONBLOCK)
795 if (host_type & SOCK_NONBLOCK) {
796 target_type |= TARGET_SOCK_NONBLOCK;
798 #endif
800 return target_type;
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;
815 abi_ulong new_brk;
816 abi_ulong old_brk;
818 /* brk pointers are always untagged */
820 /* do not allow to shrink below initial brk value */
821 if (brk_val < initial_target_brk) {
822 return 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;
831 return target_brk;
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;
839 return target_brk;
842 mapped_addr = target_mmap(old_brk, new_brk - old_brk,
843 PROT_READ | PROT_WRITE,
844 MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
845 -1, 0);
847 if (mapped_addr == old_brk) {
848 target_brk = brk_val;
849 return target_brk;
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;
856 #endif
857 /* For everything else, return the previous break. */
858 return target_brk;
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,
865 int n)
867 int i, nw, j, k;
868 abi_ulong b, *target_fds;
870 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
871 if (!(target_fds = lock_user(VERIFY_READ,
872 target_fds_addr,
873 sizeof(abi_ulong) * nw,
874 1)))
875 return -TARGET_EFAULT;
877 FD_ZERO(fds);
878 k = 0;
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 */
884 if ((b >> j) & 1)
885 FD_SET(k, fds);
886 k++;
890 unlock_user(target_fds, target_fds_addr, 0);
892 return 0;
895 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
896 abi_ulong target_fds_addr,
897 int n)
899 if (target_fds_addr) {
900 if (copy_from_user_fdset(fds, target_fds_addr, n))
901 return -TARGET_EFAULT;
902 *fds_ptr = fds;
903 } else {
904 *fds_ptr = NULL;
906 return 0;
909 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
910 const fd_set *fds,
911 int n)
913 int i, nw, j, k;
914 abi_long v;
915 abi_ulong *target_fds;
917 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
918 if (!(target_fds = lock_user(VERIFY_WRITE,
919 target_fds_addr,
920 sizeof(abi_ulong) * nw,
921 0)))
922 return -TARGET_EFAULT;
924 k = 0;
925 for (i = 0; i < nw; i++) {
926 v = 0;
927 for (j = 0; j < TARGET_ABI_BITS; j++) {
928 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
929 k++;
931 __put_user(v, &target_fds[i]);
934 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
936 return 0;
938 #endif
940 #if defined(__alpha__)
941 #define HOST_HZ 1024
942 #else
943 #define HOST_HZ 100
944 #endif
946 static inline abi_long host_to_target_clock_t(long ticks)
948 #if HOST_HZ == TARGET_HZ
949 return ticks;
950 #else
951 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
952 #endif
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);
982 return 0;
985 #ifdef TARGET_NR_setrlimit
986 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
988 abi_ulong target_rlim_swap;
989 rlim_t result;
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;
999 return result;
1001 #endif
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;
1007 abi_ulong result;
1009 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1010 target_rlim_swap = TARGET_RLIM_INFINITY;
1011 else
1012 target_rlim_swap = rlim;
1013 result = tswapal(target_rlim_swap);
1015 return result;
1017 #endif
1019 static inline int target_to_host_resource(int code)
1021 switch (code) {
1022 case TARGET_RLIMIT_AS:
1023 return RLIMIT_AS;
1024 case TARGET_RLIMIT_CORE:
1025 return RLIMIT_CORE;
1026 case TARGET_RLIMIT_CPU:
1027 return RLIMIT_CPU;
1028 case TARGET_RLIMIT_DATA:
1029 return 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:
1039 return 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:
1045 return RLIMIT_RSS;
1046 case TARGET_RLIMIT_RTPRIO:
1047 return RLIMIT_RTPRIO;
1048 #ifdef RLIMIT_RTTIME
1049 case TARGET_RLIMIT_RTTIME:
1050 return RLIMIT_RTTIME;
1051 #endif
1052 case TARGET_RLIMIT_SIGPENDING:
1053 return RLIMIT_SIGPENDING;
1054 case TARGET_RLIMIT_STACK:
1055 return RLIMIT_STACK;
1056 default:
1057 return code;
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);
1075 return 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);
1092 return 0;
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);
1110 return 0;
1112 #endif
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);
1128 return 0;
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);
1151 return 0;
1153 #endif
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);
1179 return 0;
1181 #endif
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);
1194 return 0;
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);
1208 return 0;
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);
1226 return 0;
1228 #endif
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);
1245 return 0;
1247 #endif
1249 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1250 #include <mqueue.h>
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);
1268 return 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);
1287 return 0;
1289 #endif
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;
1299 struct timeval tv;
1300 struct timespec ts, *ts_ptr;
1301 abi_long ret;
1303 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1304 if (ret) {
1305 return ret;
1307 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1308 if (ret) {
1309 return ret;
1311 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1312 if (ret) {
1313 return ret;
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;
1321 ts_ptr = &ts;
1322 } else {
1323 ts_ptr = NULL;
1326 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1327 ts_ptr, NULL));
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;
1346 return ret;
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;
1354 long nsel;
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);
1370 #endif
1371 #endif
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,
1376 bool time64)
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;
1382 abi_long ret;
1385 * The 6th arg is actually two args smashed together,
1386 * so we cannot use the C library.
1388 struct {
1389 sigset_t *set;
1390 size_t size;
1391 } sig, *sig_ptr;
1393 abi_ulong arg_sigset, arg_sigsize, *arg7;
1395 n = arg1;
1396 rfd_addr = arg2;
1397 wfd_addr = arg3;
1398 efd_addr = arg4;
1399 ts_addr = arg5;
1401 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1402 if (ret) {
1403 return ret;
1405 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1406 if (ret) {
1407 return ret;
1409 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1410 if (ret) {
1411 return ret;
1415 * This takes a timespec, and not a timeval, so we cannot
1416 * use the do_select() helper ...
1418 if (ts_addr) {
1419 if (time64) {
1420 if (target_to_host_timespec64(&ts, ts_addr)) {
1421 return -TARGET_EFAULT;
1423 } else {
1424 if (target_to_host_timespec(&ts, ts_addr)) {
1425 return -TARGET_EFAULT;
1428 ts_ptr = &ts;
1429 } else {
1430 ts_ptr = NULL;
1433 /* Extract the two packed args for the sigset */
1434 sig_ptr = NULL;
1435 if (arg6) {
1436 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
1437 if (!arg7) {
1438 return -TARGET_EFAULT;
1440 arg_sigset = tswapal(arg7[0]);
1441 arg_sigsize = tswapal(arg7[1]);
1442 unlock_user(arg7, arg6, 0);
1444 if (arg_sigset) {
1445 ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
1446 if (ret != 0) {
1447 return ret;
1449 sig_ptr = &sig;
1450 sig.size = SIGSET_T_SIZE;
1454 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1455 ts_ptr, sig_ptr));
1457 if (sig_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;
1471 if (time64) {
1472 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
1473 return -TARGET_EFAULT;
1475 } else {
1476 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
1477 return -TARGET_EFAULT;
1481 return ret;
1483 #endif
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;
1492 struct pollfd *pfd;
1493 unsigned int i;
1494 abi_long ret;
1496 pfd = NULL;
1497 target_pfd = NULL;
1498 if (nfds) {
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);
1504 if (!target_pfd) {
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);
1514 if (ppoll) {
1515 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
1516 sigset_t *set = NULL;
1518 if (arg3) {
1519 if (time64) {
1520 if (target_to_host_timespec64(timeout_ts, arg3)) {
1521 unlock_user(target_pfd, arg1, 0);
1522 return -TARGET_EFAULT;
1524 } else {
1525 if (target_to_host_timespec(timeout_ts, arg3)) {
1526 unlock_user(target_pfd, arg1, 0);
1527 return -TARGET_EFAULT;
1530 } else {
1531 timeout_ts = NULL;
1534 if (arg4) {
1535 ret = process_sigsuspend_mask(&set, arg4, arg5);
1536 if (ret != 0) {
1537 unlock_user(target_pfd, arg1, 0);
1538 return ret;
1542 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
1543 set, SIGSET_T_SIZE));
1545 if (set) {
1546 finish_sigsuspend_mask(ret);
1548 if (!is_error(ret) && arg3) {
1549 if (time64) {
1550 if (host_to_target_timespec64(arg3, timeout_ts)) {
1551 return -TARGET_EFAULT;
1553 } else {
1554 if (host_to_target_timespec(arg3, timeout_ts)) {
1555 return -TARGET_EFAULT;
1559 } else {
1560 struct timespec ts, *pts;
1562 if (arg3 >= 0) {
1563 /* Convert ms to secs, ns */
1564 ts.tv_sec = arg3 / 1000;
1565 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
1566 pts = &ts;
1567 } else {
1568 /* -ve poll() timeout means "infinite" */
1569 pts = NULL;
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);
1580 return ret;
1582 #endif
1584 static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
1585 int flags, int is_pipe2)
1587 int host_pipe[2];
1588 abi_long ret;
1589 ret = pipe2(host_pipe, flags);
1591 if (is_error(ret))
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. */
1596 if (!is_pipe2) {
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];
1609 #endif
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,
1620 socklen_t len)
1622 struct target_ip_mreqn *target_smreqn;
1624 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1625 if (!target_smreqn)
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);
1633 return 0;
1636 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1637 abi_ulong target_addr,
1638 socklen_t len)
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);
1649 if (!target_saddr)
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] )
1667 len++;
1669 if (len > unix_maxlen)
1670 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);
1695 return 0;
1698 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1699 struct sockaddr *addr,
1700 socklen_t len)
1702 struct target_sockaddr *target_saddr;
1704 if (len == 0) {
1705 return 0;
1707 assert(addr);
1709 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1710 if (!target_saddr)
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);
1735 return 0;
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))
1749 goto the_end;
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;
1753 if (!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));
1780 break;
1783 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1784 cmsg->cmsg_level = SOL_SOCKET;
1785 } else {
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);
1816 } else {
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,
1824 target_cmsg_start);
1826 unlock_user(target_cmsg, target_cmsg_addr, 0);
1827 the_end:
1828 msgh->msg_controllen = space;
1829 return 0;
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))
1843 goto the_end;
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;
1847 if (!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);
1865 break;
1868 if (cmsg->cmsg_level == SOL_SOCKET) {
1869 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1870 } else {
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.
1878 tgt_len = len;
1879 switch (cmsg->cmsg_level) {
1880 case SOL_SOCKET:
1881 switch (cmsg->cmsg_type) {
1882 case SO_TIMESTAMP:
1883 tgt_len = sizeof(struct target_timeval);
1884 break;
1885 default:
1886 break;
1888 break;
1889 default:
1890 break;
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) {
1904 case SOL_SOCKET:
1905 switch (cmsg->cmsg_type) {
1906 case SCM_RIGHTS:
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);
1915 break;
1917 case SO_TIMESTAMP:
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)) {
1925 goto unimplemented;
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);
1931 break;
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);
1942 break;
1944 default:
1945 goto unimplemented;
1947 break;
1949 case SOL_IP:
1950 switch (cmsg->cmsg_type) {
1951 case IP_TTL:
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)) {
1958 goto unimplemented;
1960 __put_user(*v, t_int);
1961 break;
1963 case IP_RECVERR:
1965 struct errhdr_t {
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)) {
1975 goto unimplemented;
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));
1986 break;
1988 default:
1989 goto unimplemented;
1991 break;
1993 case SOL_IPV6:
1994 switch (cmsg->cmsg_type) {
1995 case IPV6_HOPLIMIT:
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)) {
2002 goto unimplemented;
2004 __put_user(*v, t_int);
2005 break;
2007 case IPV6_RECVERR:
2009 struct errhdr6_t {
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)) {
2019 goto unimplemented;
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));
2030 break;
2032 default:
2033 goto unimplemented;
2035 break;
2037 default:
2038 unimplemented:
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;
2053 space += tgt_space;
2054 cmsg = CMSG_NXTHDR(msgh, cmsg);
2055 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
2056 target_cmsg_start);
2058 unlock_user(target_cmsg, target_cmsg_addr, space);
2059 the_end:
2060 target_msgh->msg_controllen = tswapal(space);
2061 return 0;
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)
2068 abi_long ret;
2069 int val;
2070 struct ip_mreqn *ip_mreq;
2071 struct ip_mreq_source *ip_mreq_source;
2073 switch(level) {
2074 case SOL_TCP:
2075 case SOL_UDP:
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)));
2083 break;
2084 case SOL_IP:
2085 switch(optname) {
2086 case IP_TOS:
2087 case IP_TTL:
2088 case IP_HDRINCL:
2089 case IP_ROUTER_ALERT:
2090 case IP_RECVOPTS:
2091 case IP_RETOPTS:
2092 case IP_PKTINFO:
2093 case IP_MTU_DISCOVER:
2094 case IP_RECVERR:
2095 case IP_RECVTTL:
2096 case IP_RECVTOS:
2097 #ifdef IP_FREEBIND
2098 case IP_FREEBIND:
2099 #endif
2100 case IP_MULTICAST_TTL:
2101 case IP_MULTICAST_LOOP:
2102 val = 0;
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)));
2111 break;
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));
2121 break;
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);
2136 break;
2138 default:
2139 goto unimplemented;
2141 break;
2142 case SOL_IPV6:
2143 switch (optname) {
2144 case IPV6_MTU_DISCOVER:
2145 case IPV6_MTU:
2146 case IPV6_V6ONLY:
2147 case IPV6_RECVPKTINFO:
2148 case IPV6_UNICAST_HOPS:
2149 case IPV6_MULTICAST_HOPS:
2150 case IPV6_MULTICAST_LOOP:
2151 case IPV6_RECVERR:
2152 case IPV6_RECVHOPLIMIT:
2153 case IPV6_2292HOPLIMIT:
2154 case IPV6_CHECKSUM:
2155 case IPV6_ADDRFORM:
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:
2164 case IPV6_TCLASS:
2165 case IPV6_ADDR_PREFERENCES:
2166 #ifdef IPV6_RECVPATHMTU
2167 case IPV6_RECVPATHMTU:
2168 #endif
2169 #ifdef IPV6_TRANSPARENT
2170 case IPV6_TRANSPARENT:
2171 #endif
2172 #ifdef IPV6_FREEBIND
2173 case IPV6_FREEBIND:
2174 #endif
2175 #ifdef IPV6_RECVORIGDSTADDR
2176 case IPV6_RECVORIGDSTADDR:
2177 #endif
2178 val = 0;
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)));
2187 break;
2188 case IPV6_PKTINFO:
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)));
2204 break;
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)));
2223 break;
2225 default:
2226 goto unimplemented;
2228 break;
2229 case SOL_ICMPV6:
2230 switch (optname) {
2231 case ICMPV6_FILTER:
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,
2248 &icmp6f, optlen));
2249 break;
2251 default:
2252 goto unimplemented;
2254 break;
2255 case SOL_RAW:
2256 switch (optname) {
2257 case ICMP_FILTER:
2258 case IPV6_CHECKSUM:
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)));
2269 break;
2271 default:
2272 goto unimplemented;
2274 break;
2275 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2276 case SOL_ALG:
2277 switch (optname) {
2278 case ALG_SET_KEY:
2280 char *alg_key = g_malloc(optlen);
2282 if (!alg_key) {
2283 return -TARGET_ENOMEM;
2285 if (copy_from_user(alg_key, optval_addr, optlen)) {
2286 g_free(alg_key);
2287 return -TARGET_EFAULT;
2289 ret = get_errno(setsockopt(sockfd, level, optname,
2290 alg_key, optlen));
2291 g_free(alg_key);
2292 break;
2294 case ALG_SET_AEAD_AUTHSIZE:
2296 ret = get_errno(setsockopt(sockfd, level, optname,
2297 NULL, optlen));
2298 break;
2300 default:
2301 goto unimplemented;
2303 break;
2304 #endif
2305 case TARGET_SOL_SOCKET:
2306 switch (optname) {
2307 case TARGET_SO_RCVTIMEO:
2309 struct timeval tv;
2311 optname = SO_RCVTIMEO;
2313 set_timeout:
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,
2323 &tv, sizeof(tv)));
2324 return ret;
2326 case TARGET_SO_SNDTIMEO:
2327 optname = SO_SNDTIMEO;
2328 goto set_timeout;
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;
2335 int i;
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)));
2366 g_free(filter);
2368 unlock_user_struct(tfilter, tfprog->filter, 1);
2369 unlock_user_struct(tfprog, optval_addr, 1);
2370 return ret;
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);
2380 if (!dev_ifname) {
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);
2390 return ret;
2392 case TARGET_SO_LINGER:
2394 struct linger lg;
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,
2406 &lg, sizeof(lg)));
2407 unlock_user_struct(tlg, optval_addr, 0);
2408 return ret;
2410 /* Options with 'int' argument. */
2411 case TARGET_SO_DEBUG:
2412 optname = SO_DEBUG;
2413 break;
2414 case TARGET_SO_REUSEADDR:
2415 optname = SO_REUSEADDR;
2416 break;
2417 #ifdef SO_REUSEPORT
2418 case TARGET_SO_REUSEPORT:
2419 optname = SO_REUSEPORT;
2420 break;
2421 #endif
2422 case TARGET_SO_TYPE:
2423 optname = SO_TYPE;
2424 break;
2425 case TARGET_SO_ERROR:
2426 optname = SO_ERROR;
2427 break;
2428 case TARGET_SO_DONTROUTE:
2429 optname = SO_DONTROUTE;
2430 break;
2431 case TARGET_SO_BROADCAST:
2432 optname = SO_BROADCAST;
2433 break;
2434 case TARGET_SO_SNDBUF:
2435 optname = SO_SNDBUF;
2436 break;
2437 case TARGET_SO_SNDBUFFORCE:
2438 optname = SO_SNDBUFFORCE;
2439 break;
2440 case TARGET_SO_RCVBUF:
2441 optname = SO_RCVBUF;
2442 break;
2443 case TARGET_SO_RCVBUFFORCE:
2444 optname = SO_RCVBUFFORCE;
2445 break;
2446 case TARGET_SO_KEEPALIVE:
2447 optname = SO_KEEPALIVE;
2448 break;
2449 case TARGET_SO_OOBINLINE:
2450 optname = SO_OOBINLINE;
2451 break;
2452 case TARGET_SO_NO_CHECK:
2453 optname = SO_NO_CHECK;
2454 break;
2455 case TARGET_SO_PRIORITY:
2456 optname = SO_PRIORITY;
2457 break;
2458 #ifdef SO_BSDCOMPAT
2459 case TARGET_SO_BSDCOMPAT:
2460 optname = SO_BSDCOMPAT;
2461 break;
2462 #endif
2463 case TARGET_SO_PASSCRED:
2464 optname = SO_PASSCRED;
2465 break;
2466 case TARGET_SO_PASSSEC:
2467 optname = SO_PASSSEC;
2468 break;
2469 case TARGET_SO_TIMESTAMP:
2470 optname = SO_TIMESTAMP;
2471 break;
2472 case TARGET_SO_RCVLOWAT:
2473 optname = SO_RCVLOWAT;
2474 break;
2475 default:
2476 goto unimplemented;
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)));
2484 break;
2485 #ifdef SOL_NETLINK
2486 case SOL_NETLINK:
2487 switch (optname) {
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) */
2503 break;
2504 default:
2505 goto unimplemented;
2507 val = 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,
2515 sizeof(val)));
2516 break;
2517 #endif /* SOL_NETLINK */
2518 default:
2519 unimplemented:
2520 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
2521 level, optname);
2522 ret = -TARGET_ENOPROTOOPT;
2524 return ret;
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)
2531 abi_long ret;
2532 int len, val;
2533 socklen_t lv;
2535 switch(level) {
2536 case TARGET_SOL_SOCKET:
2537 level = SOL_SOCKET;
2538 switch (optname) {
2539 /* These don't just return a single integer */
2540 case TARGET_SO_PEERNAME:
2541 goto unimplemented;
2542 case TARGET_SO_RCVTIMEO: {
2543 struct timeval tv;
2544 socklen_t tvlen;
2546 optname = SO_RCVTIMEO;
2548 get_timeout:
2549 if (get_user_u32(len, optlen)) {
2550 return -TARGET_EFAULT;
2552 if (len < 0) {
2553 return -TARGET_EINVAL;
2556 tvlen = sizeof(tv);
2557 ret = get_errno(getsockopt(sockfd, level, optname,
2558 &tv, &tvlen));
2559 if (ret < 0) {
2560 return ret;
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;
2571 break;
2573 case TARGET_SO_SNDTIMEO:
2574 optname = SO_SNDTIMEO;
2575 goto get_timeout;
2576 case TARGET_SO_PEERCRED: {
2577 struct ucred cr;
2578 socklen_t crlen;
2579 struct target_ucred *tcr;
2581 if (get_user_u32(len, optlen)) {
2582 return -TARGET_EFAULT;
2584 if (len < 0) {
2585 return -TARGET_EINVAL;
2588 crlen = sizeof(cr);
2589 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2590 &cr, &crlen));
2591 if (ret < 0) {
2592 return ret;
2594 if (len > crlen) {
2595 len = crlen;
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;
2607 break;
2609 case TARGET_SO_PEERSEC: {
2610 char *name;
2612 if (get_user_u32(len, optlen)) {
2613 return -TARGET_EFAULT;
2615 if (len < 0) {
2616 return -TARGET_EINVAL;
2618 name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
2619 if (!name) {
2620 return -TARGET_EFAULT;
2622 lv = len;
2623 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
2624 name, &lv));
2625 if (put_user_u32(lv, optlen)) {
2626 ret = -TARGET_EFAULT;
2628 unlock_user(name, optval_addr, lv);
2629 break;
2631 case TARGET_SO_LINGER:
2633 struct linger lg;
2634 socklen_t lglen;
2635 struct target_linger *tlg;
2637 if (get_user_u32(len, optlen)) {
2638 return -TARGET_EFAULT;
2640 if (len < 0) {
2641 return -TARGET_EINVAL;
2644 lglen = sizeof(lg);
2645 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2646 &lg, &lglen));
2647 if (ret < 0) {
2648 return ret;
2650 if (len > lglen) {
2651 len = lglen;
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;
2662 break;
2664 /* Options with 'int' argument. */
2665 case TARGET_SO_DEBUG:
2666 optname = SO_DEBUG;
2667 goto int_case;
2668 case TARGET_SO_REUSEADDR:
2669 optname = SO_REUSEADDR;
2670 goto int_case;
2671 #ifdef SO_REUSEPORT
2672 case TARGET_SO_REUSEPORT:
2673 optname = SO_REUSEPORT;
2674 goto int_case;
2675 #endif
2676 case TARGET_SO_TYPE:
2677 optname = SO_TYPE;
2678 goto int_case;
2679 case TARGET_SO_ERROR:
2680 optname = SO_ERROR;
2681 goto int_case;
2682 case TARGET_SO_DONTROUTE:
2683 optname = SO_DONTROUTE;
2684 goto int_case;
2685 case TARGET_SO_BROADCAST:
2686 optname = SO_BROADCAST;
2687 goto int_case;
2688 case TARGET_SO_SNDBUF:
2689 optname = SO_SNDBUF;
2690 goto int_case;
2691 case TARGET_SO_RCVBUF:
2692 optname = SO_RCVBUF;
2693 goto int_case;
2694 case TARGET_SO_KEEPALIVE:
2695 optname = SO_KEEPALIVE;
2696 goto int_case;
2697 case TARGET_SO_OOBINLINE:
2698 optname = SO_OOBINLINE;
2699 goto int_case;
2700 case TARGET_SO_NO_CHECK:
2701 optname = SO_NO_CHECK;
2702 goto int_case;
2703 case TARGET_SO_PRIORITY:
2704 optname = SO_PRIORITY;
2705 goto int_case;
2706 #ifdef SO_BSDCOMPAT
2707 case TARGET_SO_BSDCOMPAT:
2708 optname = SO_BSDCOMPAT;
2709 goto int_case;
2710 #endif
2711 case TARGET_SO_PASSCRED:
2712 optname = SO_PASSCRED;
2713 goto int_case;
2714 case TARGET_SO_TIMESTAMP:
2715 optname = SO_TIMESTAMP;
2716 goto int_case;
2717 case TARGET_SO_RCVLOWAT:
2718 optname = SO_RCVLOWAT;
2719 goto int_case;
2720 case TARGET_SO_ACCEPTCONN:
2721 optname = SO_ACCEPTCONN;
2722 goto int_case;
2723 case TARGET_SO_PROTOCOL:
2724 optname = SO_PROTOCOL;
2725 goto int_case;
2726 case TARGET_SO_DOMAIN:
2727 optname = SO_DOMAIN;
2728 goto int_case;
2729 default:
2730 goto int_case;
2732 break;
2733 case SOL_TCP:
2734 case SOL_UDP:
2735 /* TCP and UDP options all take an 'int' value. */
2736 int_case:
2737 if (get_user_u32(len, optlen))
2738 return -TARGET_EFAULT;
2739 if (len < 0)
2740 return -TARGET_EINVAL;
2741 lv = sizeof(lv);
2742 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2743 if (ret < 0)
2744 return ret;
2745 switch (optname) {
2746 case SO_TYPE:
2747 val = host_to_target_sock_type(val);
2748 break;
2749 case SO_ERROR:
2750 val = host_to_target_errno(val);
2751 break;
2753 if (len > lv)
2754 len = lv;
2755 if (len == 4) {
2756 if (put_user_u32(val, optval_addr))
2757 return -TARGET_EFAULT;
2758 } else {
2759 if (put_user_u8(val, optval_addr))
2760 return -TARGET_EFAULT;
2762 if (put_user_u32(len, optlen))
2763 return -TARGET_EFAULT;
2764 break;
2765 case SOL_IP:
2766 switch(optname) {
2767 case IP_TOS:
2768 case IP_TTL:
2769 case IP_HDRINCL:
2770 case IP_ROUTER_ALERT:
2771 case IP_RECVOPTS:
2772 case IP_RETOPTS:
2773 case IP_PKTINFO:
2774 case IP_MTU_DISCOVER:
2775 case IP_RECVERR:
2776 case IP_RECVTOS:
2777 #ifdef IP_FREEBIND
2778 case IP_FREEBIND:
2779 #endif
2780 case IP_MULTICAST_TTL:
2781 case IP_MULTICAST_LOOP:
2782 if (get_user_u32(len, optlen))
2783 return -TARGET_EFAULT;
2784 if (len < 0)
2785 return -TARGET_EINVAL;
2786 lv = sizeof(lv);
2787 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2788 if (ret < 0)
2789 return ret;
2790 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2791 len = 1;
2792 if (put_user_u32(len, optlen)
2793 || put_user_u8(val, optval_addr))
2794 return -TARGET_EFAULT;
2795 } else {
2796 if (len > sizeof(int))
2797 len = sizeof(int);
2798 if (put_user_u32(len, optlen)
2799 || put_user_u32(val, optval_addr))
2800 return -TARGET_EFAULT;
2802 break;
2803 default:
2804 ret = -TARGET_ENOPROTOOPT;
2805 break;
2807 break;
2808 case SOL_IPV6:
2809 switch (optname) {
2810 case IPV6_MTU_DISCOVER:
2811 case IPV6_MTU:
2812 case IPV6_V6ONLY:
2813 case IPV6_RECVPKTINFO:
2814 case IPV6_UNICAST_HOPS:
2815 case IPV6_MULTICAST_HOPS:
2816 case IPV6_MULTICAST_LOOP:
2817 case IPV6_RECVERR:
2818 case IPV6_RECVHOPLIMIT:
2819 case IPV6_2292HOPLIMIT:
2820 case IPV6_CHECKSUM:
2821 case IPV6_ADDRFORM:
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:
2830 case IPV6_TCLASS:
2831 case IPV6_ADDR_PREFERENCES:
2832 #ifdef IPV6_RECVPATHMTU
2833 case IPV6_RECVPATHMTU:
2834 #endif
2835 #ifdef IPV6_TRANSPARENT
2836 case IPV6_TRANSPARENT:
2837 #endif
2838 #ifdef IPV6_FREEBIND
2839 case IPV6_FREEBIND:
2840 #endif
2841 #ifdef IPV6_RECVORIGDSTADDR
2842 case IPV6_RECVORIGDSTADDR:
2843 #endif
2844 if (get_user_u32(len, optlen))
2845 return -TARGET_EFAULT;
2846 if (len < 0)
2847 return -TARGET_EINVAL;
2848 lv = sizeof(lv);
2849 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2850 if (ret < 0)
2851 return ret;
2852 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2853 len = 1;
2854 if (put_user_u32(len, optlen)
2855 || put_user_u8(val, optval_addr))
2856 return -TARGET_EFAULT;
2857 } else {
2858 if (len > sizeof(int))
2859 len = sizeof(int);
2860 if (put_user_u32(len, optlen)
2861 || put_user_u32(val, optval_addr))
2862 return -TARGET_EFAULT;
2864 break;
2865 default:
2866 ret = -TARGET_ENOPROTOOPT;
2867 break;
2869 break;
2870 #ifdef SOL_NETLINK
2871 case SOL_NETLINK:
2872 switch (optname) {
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;
2892 lv = len;
2893 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2894 if (ret < 0) {
2895 return ret;
2897 if (put_user_u32(lv, optlen)
2898 || put_user_u32(val, optval_addr)) {
2899 return -TARGET_EFAULT;
2901 break;
2902 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2903 case NETLINK_LIST_MEMBERSHIPS:
2905 uint32_t *results;
2906 int i;
2907 if (get_user_u32(len, optlen)) {
2908 return -TARGET_EFAULT;
2910 if (len < 0) {
2911 return -TARGET_EINVAL;
2913 results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
2914 if (!results && len > 0) {
2915 return -TARGET_EFAULT;
2917 lv = len;
2918 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
2919 if (ret < 0) {
2920 unlock_user(results, optval_addr, 0);
2921 return ret;
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);
2931 break;
2933 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2934 default:
2935 goto unimplemented;
2937 break;
2938 #endif /* SOL_NETLINK */
2939 default:
2940 unimplemented:
2941 qemu_log_mask(LOG_UNIMP,
2942 "getsockopt level=%d optname=%d not yet supported\n",
2943 level, optname);
2944 ret = -TARGET_EOPNOTSUPP;
2945 break;
2947 return ret;
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,
2955 abi_ulong thigh,
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;
2963 *hlow = off;
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;
2971 struct iovec *vec;
2972 abi_ulong total_len, max_len;
2973 int i;
2974 int err = 0;
2975 bool bad_address = false;
2977 if (count == 0) {
2978 errno = 0;
2979 return NULL;
2981 if (count > IOV_MAX) {
2982 errno = EINVAL;
2983 return NULL;
2986 vec = g_try_new0(struct iovec, count);
2987 if (vec == NULL) {
2988 errno = ENOMEM;
2989 return NULL;
2992 target_vec = lock_user(VERIFY_READ, target_addr,
2993 count * sizeof(struct target_iovec), 1);
2994 if (target_vec == NULL) {
2995 err = EFAULT;
2996 goto fail2;
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;
3002 total_len = 0;
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);
3008 if (len < 0) {
3009 err = EINVAL;
3010 goto fail;
3011 } else if (len == 0) {
3012 /* Zero length pointer is ignored. */
3013 vec[i].iov_base = 0;
3014 } else {
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
3019 * zero lengths. */
3020 if (!vec[i].iov_base) {
3021 if (i == 0) {
3022 err = EFAULT;
3023 goto fail;
3024 } else {
3025 bad_address = true;
3028 if (bad_address) {
3029 len = 0;
3031 if (len > max_len - total_len) {
3032 len = max_len - total_len;
3035 vec[i].iov_len = len;
3036 total_len += len;
3039 unlock_user(target_vec, target_addr, 0);
3040 return vec;
3042 fail:
3043 while (--i >= 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);
3049 fail2:
3050 g_free(vec);
3051 errno = err;
3052 return NULL;
3055 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
3056 abi_ulong count, int copy)
3058 struct target_iovec *target_vec;
3059 int i;
3061 target_vec = lock_user(VERIFY_READ, target_addr,
3062 count * sizeof(struct target_iovec), 1);
3063 if (target_vec) {
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);
3067 if (len < 0) {
3068 break;
3070 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3072 unlock_user(target_vec, target_addr, 0);
3075 g_free(vec);
3078 static inline int target_to_host_sock_type(int *type)
3080 int host_type = 0;
3081 int target_type = *type;
3083 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3084 case TARGET_SOCK_DGRAM:
3085 host_type = SOCK_DGRAM;
3086 break;
3087 case TARGET_SOCK_STREAM:
3088 host_type = SOCK_STREAM;
3089 break;
3090 default:
3091 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3092 break;
3094 if (target_type & TARGET_SOCK_CLOEXEC) {
3095 #if defined(SOCK_CLOEXEC)
3096 host_type |= SOCK_CLOEXEC;
3097 #else
3098 return -TARGET_EINVAL;
3099 #endif
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;
3106 #endif
3108 *type = host_type;
3109 return 0;
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) {
3119 close(fd);
3120 return -TARGET_EINVAL;
3123 #endif
3124 return fd;
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;
3131 int ret;
3133 ret = target_to_host_sock_type(&type);
3134 if (ret) {
3135 return ret;
3138 if (domain == PF_NETLINK && !(
3139 #ifdef CONFIG_RTNETLINK
3140 protocol == NETLINK_ROUTE ||
3141 #endif
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));
3153 if (ret >= 0) {
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) {
3161 switch (protocol) {
3162 #ifdef CONFIG_RTNETLINK
3163 case NETLINK_ROUTE:
3164 fd_trans_register(ret, &target_netlink_route_trans);
3165 break;
3166 #endif
3167 case NETLINK_KOBJECT_UEVENT:
3168 /* nothing to do: messages are strings */
3169 break;
3170 case NETLINK_AUDIT:
3171 fd_trans_register(ret, &target_netlink_audit_trans);
3172 break;
3173 default:
3174 g_assert_not_reached();
3178 return ret;
3181 /* do_bind() Must return target values and target errnos. */
3182 static abi_long do_bind(int sockfd, abi_ulong target_addr,
3183 socklen_t addrlen)
3185 void *addr;
3186 abi_long ret;
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);
3195 if (ret)
3196 return ret;
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,
3203 socklen_t addrlen)
3205 void *addr;
3206 abi_long ret;
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);
3215 if (ret)
3216 return ret;
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)
3225 abi_long ret, len;
3226 struct msghdr msg;
3227 abi_ulong count;
3228 struct iovec *vec;
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),
3236 msg.msg_namelen);
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;
3244 } else if (ret) {
3245 goto out2;
3247 } else {
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;
3265 goto out2;
3268 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3269 target_vec, count, send);
3270 if (vec == NULL) {
3271 ret = -host_to_target_errno(errno);
3272 /* allow sending packet without any iov, e.g. with MSG_MORE flag */
3273 if (!send || ret) {
3274 goto out2;
3277 msg.msg_iovlen = count;
3278 msg.msg_iov = vec;
3280 if (send) {
3281 if (fd_trans_target_to_host_data(fd)) {
3282 void *host_msg;
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);
3288 if (ret >= 0) {
3289 msg.msg_iov->iov_base = host_msg;
3290 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3292 g_free(host_msg);
3293 } else {
3294 ret = target_to_host_cmsg(&msg, msgp);
3295 if (ret == 0) {
3296 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3299 } else {
3300 ret = get_errno(safe_recvmsg(fd, &msg, flags));
3301 if (!is_error(ret)) {
3302 len = 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);
3316 if (ret) {
3317 goto out;
3321 ret = len;
3326 out:
3327 if (vec) {
3328 unlock_iovec(vec, target_vec, count, !send);
3330 out2:
3331 return ret;
3334 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3335 int flags, int send)
3337 abi_long ret;
3338 struct target_msghdr *msgp;
3340 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3341 msgp,
3342 target_msg,
3343 send ? 1 : 0)) {
3344 return -TARGET_EFAULT;
3346 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
3347 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3348 return ret;
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
3356 #endif
3358 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3359 unsigned int vlen, unsigned int flags,
3360 int send)
3362 struct target_mmsghdr *mmsgp;
3363 abi_long ret = 0;
3364 int i;
3366 if (vlen > UIO_MAXIOV) {
3367 vlen = UIO_MAXIOV;
3370 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3371 if (!mmsgp) {
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)) {
3378 break;
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.
3392 if (i) {
3393 return i;
3395 return ret;
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;
3403 void *addr;
3404 abi_long ret;
3405 int host_flags;
3407 if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
3408 return -TARGET_EINVAL;
3411 host_flags = 0;
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;
3445 return ret;
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;
3453 void *addr;
3454 abi_long ret;
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;
3477 return ret;
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;
3485 void *addr;
3486 abi_long ret;
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;
3509 return ret;
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)
3516 int tab[2];
3517 abi_long ret;
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;
3527 return ret;
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)
3534 void *addr;
3535 void *host_msg;
3536 void *copy_msg = NULL;
3537 abi_long ret;
3539 if ((int)addrlen < 0) {
3540 return -TARGET_EINVAL;
3543 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3544 if (!host_msg)
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);
3551 if (ret < 0) {
3552 goto fail;
3555 if (target_addr) {
3556 addr = alloca(addrlen+1);
3557 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
3558 if (ret) {
3559 goto fail;
3561 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
3562 } else {
3563 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
3565 fail:
3566 if (copy_msg) {
3567 g_free(host_msg);
3568 host_msg = copy_msg;
3570 unlock_user(host_msg, msg, 0);
3571 return ret;
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;
3580 void *addr;
3581 void *host_msg;
3582 abi_long ret;
3584 if (!msg) {
3585 host_msg = NULL;
3586 } else {
3587 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3588 if (!host_msg) {
3589 return -TARGET_EFAULT;
3592 if (target_addr) {
3593 if (get_user_u32(addrlen, target_addrlen)) {
3594 ret = -TARGET_EFAULT;
3595 goto fail;
3597 if ((int)addrlen < 0) {
3598 ret = -TARGET_EINVAL;
3599 goto fail;
3601 addr = alloca(addrlen);
3602 ret_addrlen = addrlen;
3603 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3604 addr, &ret_addrlen));
3605 } else {
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)) {
3612 abi_long trans;
3613 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3614 if (is_error(trans)) {
3615 ret = trans;
3616 goto fail;
3619 if (target_addr) {
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;
3624 goto fail;
3627 unlock_user(host_msg, msg, len);
3628 } else {
3629 fail:
3630 unlock_user(host_msg, msg, 0);
3632 return ret;
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 */
3662 unsigned i;
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 */
3680 switch (num) {
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);
3721 default:
3722 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
3723 return -TARGET_EINVAL;
3726 #endif
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;
3736 #endif
3737 abi_ulong sem_ctime;
3738 #if TARGET_ABI_BITS == 32
3739 abi_ulong __unused2;
3740 #endif
3741 abi_ulong sem_nsems;
3742 abi_ulong __unused3;
3743 abi_ulong __unused4;
3745 #endif
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);
3763 #else
3764 host_ip->mode = tswap16(target_ip->mode);
3765 #endif
3766 #if defined(TARGET_PPC)
3767 host_ip->__seq = tswap32(target_ip->__seq);
3768 #else
3769 host_ip->__seq = tswap16(target_ip->__seq);
3770 #endif
3771 unlock_user_struct(target_sd, target_addr, 0);
3772 return 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);
3791 #else
3792 target_ip->mode = tswap16(host_ip->mode);
3793 #endif
3794 #if defined(TARGET_PPC)
3795 target_ip->__seq = tswap32(host_ip->__seq);
3796 #else
3797 target_ip->__seq = tswap16(host_ip->__seq);
3798 #endif
3799 unlock_user_struct(target_sd, target_addr, 1);
3800 return 0;
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);
3816 return 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);
3832 return 0;
3835 struct target_seminfo {
3836 int semmap;
3837 int semmni;
3838 int semmns;
3839 int semmnu;
3840 int semmsl;
3841 int semopm;
3842 int semume;
3843 int semusz;
3844 int semvmx;
3845 int semaem;
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);
3865 return 0;
3868 union semun {
3869 int val;
3870 struct semid_ds *buf;
3871 unsigned short *array;
3872 struct seminfo *__buf;
3875 union target_semun {
3876 int val;
3877 abi_ulong buf;
3878 abi_ulong array;
3879 abi_ulong __buf;
3882 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3883 abi_ulong target_addr)
3885 int nsems;
3886 unsigned short *array;
3887 union semun semun;
3888 struct semid_ds semid_ds;
3889 int i, ret;
3891 semun.buf = &semid_ds;
3893 ret = semctl(semid, 0, IPC_STAT, semun);
3894 if (ret == -1)
3895 return get_errno(ret);
3897 nsems = semid_ds.sem_nsems;
3899 *host_array = g_try_new(unsigned short, nsems);
3900 if (!*host_array) {
3901 return -TARGET_ENOMEM;
3903 array = lock_user(VERIFY_READ, target_addr,
3904 nsems*sizeof(unsigned short), 1);
3905 if (!array) {
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);
3915 return 0;
3918 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3919 unsigned short **host_array)
3921 int nsems;
3922 unsigned short *array;
3923 union semun semun;
3924 struct semid_ds semid_ds;
3925 int i, ret;
3927 semun.buf = &semid_ds;
3929 ret = semctl(semid, 0, IPC_STAT, semun);
3930 if (ret == -1)
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);
3937 if (!array)
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);
3946 return 0;
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 };
3953 union semun arg;
3954 struct semid_ds dsarg;
3955 unsigned short *array = NULL;
3956 struct seminfo seminfo;
3957 abi_long ret = -TARGET_EINVAL;
3958 abi_long err;
3959 cmd &= 0xff;
3961 switch( cmd ) {
3962 case GETVAL:
3963 case SETVAL:
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);
3972 } else {
3973 arg.val = target_su.val;
3975 ret = get_errno(semctl(semid, semnum, cmd, arg));
3976 break;
3977 case GETALL:
3978 case SETALL:
3979 err = target_to_host_semarray(semid, &array, target_su.array);
3980 if (err)
3981 return err;
3982 arg.array = array;
3983 ret = get_errno(semctl(semid, semnum, cmd, arg));
3984 err = host_to_target_semarray(semid, target_su.array, &array);
3985 if (err)
3986 return err;
3987 break;
3988 case IPC_STAT:
3989 case IPC_SET:
3990 case SEM_STAT:
3991 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3992 if (err)
3993 return err;
3994 arg.buf = &dsarg;
3995 ret = get_errno(semctl(semid, semnum, cmd, arg));
3996 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3997 if (err)
3998 return err;
3999 break;
4000 case IPC_INFO:
4001 case SEM_INFO:
4002 arg.__buf = &seminfo;
4003 ret = get_errno(semctl(semid, semnum, cmd, arg));
4004 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4005 if (err)
4006 return err;
4007 break;
4008 case IPC_RMID:
4009 case GETPID:
4010 case GETNCNT:
4011 case GETZCNT:
4012 ret = get_errno(semctl(semid, semnum, cmd, NULL));
4013 break;
4016 return ret;
4019 struct target_sembuf {
4020 unsigned short sem_num;
4021 short sem_op;
4022 short sem_flg;
4025 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4026 abi_ulong target_addr,
4027 unsigned nsops)
4029 struct target_sembuf *target_sembuf;
4030 int i;
4032 target_sembuf = lock_user(VERIFY_READ, target_addr,
4033 nsops*sizeof(struct target_sembuf), 1);
4034 if (!target_sembuf)
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);
4045 return 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.
4055 #ifdef __s390x__
4056 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4057 (__nsops), (__timeout), (__sops)
4058 #else
4059 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4060 (__nsops), 0, (__sops), (__timeout)
4061 #endif
4063 static inline abi_long do_semtimedop(int semid,
4064 abi_long ptr,
4065 unsigned nsops,
4066 abi_long timeout, bool time64)
4068 struct sembuf *sops;
4069 struct timespec ts, *pts = NULL;
4070 abi_long ret;
4072 if (timeout) {
4073 pts = &ts;
4074 if (time64) {
4075 if (target_to_host_timespec64(pts, timeout)) {
4076 return -TARGET_EFAULT;
4078 } else {
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)) {
4092 g_free(sops);
4093 return -TARGET_EFAULT;
4096 ret = -TARGET_ENOSYS;
4097 #ifdef __NR_semtimedop
4098 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
4099 #endif
4100 #ifdef __NR_ipc
4101 if (ret == -TARGET_ENOSYS) {
4102 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
4103 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
4105 #endif
4106 g_free(sops);
4107 return ret;
4109 #endif
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;
4117 #endif
4118 abi_ulong msg_rtime;
4119 #if TARGET_ABI_BITS == 32
4120 abi_ulong __unused2;
4121 #endif
4122 abi_ulong msg_ctime;
4123 #if TARGET_ABI_BITS == 32
4124 abi_ulong __unused3;
4125 #endif
4126 abi_ulong __msg_cbytes;
4127 abi_ulong msg_qnum;
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);
4153 return 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);
4174 return 0;
4177 struct target_msginfo {
4178 int msgpool;
4179 int msgmap;
4180 int msgmax;
4181 int msgmnb;
4182 int msgmni;
4183 int msgssz;
4184 int msgtql;
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);
4203 return 0;
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;
4212 cmd &= 0xff;
4214 switch (cmd) {
4215 case IPC_STAT:
4216 case IPC_SET:
4217 case MSG_STAT:
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;
4223 break;
4224 case IPC_RMID:
4225 ret = get_errno(msgctl(msgid, cmd, NULL));
4226 break;
4227 case IPC_INFO:
4228 case MSG_INFO:
4229 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4230 if (host_to_target_msginfo(ptr, &msginfo))
4231 return -TARGET_EFAULT;
4232 break;
4235 return ret;
4238 struct target_msgbuf {
4239 abi_long mtype;
4240 char mtext[1];
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;
4248 abi_long ret = 0;
4250 if (msgsz < 0) {
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));
4257 if (!host_mb) {
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;
4264 #ifdef __NR_msgsnd
4265 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
4266 #endif
4267 #ifdef __NR_ipc
4268 if (ret == -TARGET_ENOSYS) {
4269 #ifdef __s390x__
4270 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4271 host_mb));
4272 #else
4273 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4274 host_mb, 0));
4275 #endif
4277 #endif
4278 g_free(host_mb);
4279 unlock_user_struct(target_mb, msgp, 0);
4281 return ret;
4284 #ifdef __NR_ipc
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})
4292 #else
4293 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4294 ((long int[]){(long int)__msgp, __msgtyp}), 0
4295 #endif
4296 #endif
4298 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
4299 ssize_t msgsz, abi_long msgtyp,
4300 int msgflg)
4302 struct target_msgbuf *target_mb;
4303 char *target_mtext;
4304 struct msgbuf *host_mb;
4305 abi_long ret = 0;
4307 if (msgsz < 0) {
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));
4315 if (!host_mb) {
4316 ret = -TARGET_ENOMEM;
4317 goto end;
4319 ret = -TARGET_ENOSYS;
4320 #ifdef __NR_msgrcv
4321 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
4322 #endif
4323 #ifdef __NR_ipc
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)));
4328 #endif
4330 if (ret > 0) {
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;
4335 goto end;
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);
4343 end:
4344 if (target_mb)
4345 unlock_user_struct(target_mb, msgp, 1);
4346 g_free(host_mb);
4347 return ret;
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);
4367 return 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);
4387 return 0;
4390 struct target_shminfo {
4391 abi_ulong shmmax;
4392 abi_ulong shmmin;
4393 abi_ulong shmmni;
4394 abi_ulong shmseg;
4395 abi_ulong shmall;
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);
4410 return 0;
4413 struct target_shm_info {
4414 int used_ids;
4415 abi_ulong shm_tot;
4416 abi_ulong shm_rss;
4417 abi_ulong shm_swp;
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);
4435 return 0;
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;
4445 cmd &= 0xff;
4447 switch(cmd) {
4448 case IPC_STAT:
4449 case IPC_SET:
4450 case SHM_STAT:
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;
4456 break;
4457 case IPC_INFO:
4458 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4459 if (host_to_target_shminfo(buf, &shminfo))
4460 return -TARGET_EFAULT;
4461 break;
4462 case SHM_INFO:
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;
4466 break;
4467 case IPC_RMID:
4468 case SHM_LOCK:
4469 case SHM_UNLOCK:
4470 ret = get_errno(shmctl(shmid, cmd, NULL));
4471 break;
4474 return ret;
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)
4485 int version;
4486 abi_long ret = 0;
4488 version = call >> 16;
4489 call &= 0xffff;
4491 switch (call) {
4492 case IPCOP_semop:
4493 ret = do_semtimedop(first, ptr, second, 0, false);
4494 break;
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);
4504 #else
4505 ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
4506 #endif
4507 break;
4509 case IPCOP_semget:
4510 ret = get_errno(semget(first, second, third));
4511 break;
4513 case IPCOP_semctl: {
4514 /* The semun argument to semctl is passed by value, so dereference the
4515 * ptr argument. */
4516 abi_ulong atptr;
4517 get_user_ual(atptr, ptr);
4518 ret = do_semctl(first, second, third, atptr);
4519 break;
4522 case IPCOP_msgget:
4523 ret = get_errno(msgget(first, second));
4524 break;
4526 case IPCOP_msgsnd:
4527 ret = do_msgsnd(first, ptr, second, third);
4528 break;
4530 case IPCOP_msgctl:
4531 ret = do_msgctl(first, second, ptr);
4532 break;
4534 case IPCOP_msgrcv:
4535 switch (version) {
4536 case 0:
4538 struct target_ipc_kludge {
4539 abi_long msgp;
4540 abi_long msgtyp;
4541 } *tmp;
4543 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4544 ret = -TARGET_EFAULT;
4545 break;
4548 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
4550 unlock_user_struct(tmp, ptr, 0);
4551 break;
4553 default:
4554 ret = do_msgrcv(first, ptr, second, fifth, third);
4556 break;
4558 case IPCOP_shmat:
4559 switch (version) {
4560 default:
4562 abi_ulong raddr;
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;
4568 break;
4570 case 1:
4571 ret = -TARGET_EINVAL;
4572 break;
4574 break;
4575 case IPCOP_shmdt:
4576 ret = target_shmdt(ptr);
4577 break;
4579 case IPCOP_shmget:
4580 /* IPC_* flag values are the same on all linux platforms */
4581 ret = get_errno(shmget(first, second, third));
4582 break;
4584 /* IPC_* and SHM_* command values are the same on all linux platforms */
4585 case IPCOP_shmctl:
4586 ret = do_shmctl(first, second, ptr);
4587 break;
4588 default:
4589 qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
4590 call, version);
4591 ret = -TARGET_ENOSYS;
4592 break;
4594 return ret;
4596 #endif
4598 /* kernel structure types definitions */
4600 #define STRUCT(name, ...) STRUCT_ ## name,
4601 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4602 enum {
4603 #include "syscall_types.h"
4604 STRUCT_MAX
4606 #undef STRUCT
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"
4612 #undef STRUCT
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
4631 * ioctl.
4633 int target_size_in, target_size_out;
4634 struct fiemap *fm;
4635 const argtype *arg_type = ie->arg_type;
4636 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4637 void *argptr, *p;
4638 abi_long ret;
4639 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4640 uint32_t outbufsz;
4641 int free_fm = 0;
4643 assert(arg_type[0] == TYPE_PTR);
4644 assert(ie->access == IOC_RW);
4645 arg_type++;
4646 target_size_in = thunk_type_size(arg_type, 0);
4647 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4648 if (!argptr) {
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);
4666 if (!fm) {
4667 return -TARGET_ENOMEM;
4669 memcpy(fm, buf_temp, sizeof(struct fiemap));
4670 free_fm = 1;
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);
4682 if (!argptr) {
4683 ret = -TARGET_EFAULT;
4684 } else {
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,
4692 THUNK_TARGET);
4693 p += extent_size;
4696 unlock_user(argptr, arg, target_size_out);
4699 if (free_fm) {
4700 g_free(fm);
4702 return ret;
4704 #endif
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;
4710 int target_size;
4711 void *argptr;
4712 int ret;
4713 struct ifconf *host_ifconf;
4714 uint32_t outbufsz;
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;
4718 int nb_ifreq;
4719 int free_buf = 0;
4720 int i;
4721 int target_ifc_len;
4722 abi_long target_ifc_buf;
4723 int host_ifc_len;
4724 char *host_ifc_buf;
4726 assert(arg_type[0] == TYPE_PTR);
4727 assert(ie->access == IOC_RW);
4729 arg_type++;
4730 target_size = thunk_type_size(arg_type, 0);
4732 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4733 if (!argptr)
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);
4754 if (!host_ifconf) {
4755 return -TARGET_ENOMEM;
4757 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4758 free_buf = 1;
4760 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
4762 host_ifconf->ifc_len = host_ifc_len;
4763 } else {
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);
4783 if (!argptr)
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);
4800 if (free_buf) {
4801 g_free(host_ifconf);
4804 return ret;
4807 #if defined(CONFIG_USBFS)
4808 #if HOST_LONG_BITS > 64
4809 #error USBDEVFS thunks do not support >64 bit hosts yet.
4810 #endif
4811 struct live_urb {
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);
4846 static abi_long
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;
4853 void *argptr;
4854 uint64_t hurb;
4855 int target_size;
4856 uintptr_t target_urb_adr;
4857 abi_long ret;
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)) {
4864 return 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);
4882 if (!argptr) {
4883 g_free(lurb);
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);
4892 if (!argptr) {
4893 g_free(lurb);
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);
4902 g_free(lurb);
4903 return ret;
4906 static abi_long
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);
4915 if (!lurb) {
4916 return -TARGET_EFAULT;
4918 return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4921 static abi_long
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;
4926 int target_size;
4927 abi_long ret;
4928 void *argptr;
4929 int rw_dir;
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.
4938 arg_type++;
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);
4943 if (!lurb) {
4944 return -TARGET_ENOMEM;
4947 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4948 if (!argptr) {
4949 g_free(lurb);
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) {
4964 g_free(lurb);
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);
4974 g_free(lurb);
4975 } else {
4976 urb_hashtable_insert(lurb);
4979 return ret;
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)
4986 void *argptr;
4987 struct dm_ioctl *host_dm;
4988 abi_long guest_data;
4989 uint32_t guest_data_size;
4990 int target_size;
4991 const argtype *arg_type = ie->arg_type;
4992 abi_long ret;
4993 void *big_buf = NULL;
4994 char *host_data;
4996 arg_type++;
4997 target_size = thunk_type_size(arg_type, 0);
4998 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4999 if (!argptr) {
5000 ret = -TARGET_EFAULT;
5001 goto out;
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);
5009 buf_temp = big_buf;
5010 host_dm = big_buf;
5012 guest_data = arg + host_dm->data_start;
5013 if ((guest_data - arg) < 0) {
5014 ret = -TARGET_EINVAL;
5015 goto out;
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);
5021 if (!argptr) {
5022 ret = -TARGET_EFAULT;
5023 goto out;
5026 switch (ie->host_cmd) {
5027 case DM_REMOVE_ALL:
5028 case DM_LIST_DEVICES:
5029 case DM_DEV_CREATE:
5030 case DM_DEV_REMOVE:
5031 case DM_DEV_SUSPEND:
5032 case DM_DEV_STATUS:
5033 case DM_DEV_WAIT:
5034 case DM_TABLE_STATUS:
5035 case DM_TABLE_CLEAR:
5036 case DM_TABLE_DEPS:
5037 case DM_LIST_VERSIONS:
5038 /* no input data */
5039 break;
5040 case DM_DEV_RENAME:
5041 case DM_DEV_SET_GEOMETRY:
5042 /* data contains only strings */
5043 memcpy(host_data, argptr, guest_data_size);
5044 break;
5045 case DM_TARGET_MSG:
5046 memcpy(host_data, argptr, guest_data_size);
5047 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5048 break;
5049 case DM_TABLE_LOAD:
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);
5055 int i;
5057 for (i = 0; i < host_dm->target_count; i++) {
5058 struct dm_target_spec *spec = cur_data;
5059 uint32_t next;
5060 int slen;
5062 thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST);
5063 slen = strlen((char*)gspec + spec_size) + 1;
5064 next = spec->next;
5065 spec->next = sizeof(*spec) + slen;
5066 strcpy((char*)&spec[1], gspec + spec_size);
5067 gspec += next;
5068 cur_data += spec->next;
5070 break;
5072 default:
5073 ret = -TARGET_EINVAL;
5074 unlock_user(argptr, guest_data, 0);
5075 goto out;
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) {
5085 case DM_REMOVE_ALL:
5086 case DM_DEV_CREATE:
5087 case DM_DEV_REMOVE:
5088 case DM_DEV_RENAME:
5089 case DM_DEV_SUSPEND:
5090 case DM_DEV_STATUS:
5091 case DM_TABLE_LOAD:
5092 case DM_TABLE_CLEAR:
5093 case DM_TARGET_MSG:
5094 case DM_DEV_SET_GEOMETRY:
5095 /* no return data */
5096 break;
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 */
5105 while (1) {
5106 uint32_t next = nl->next;
5107 if (next) {
5108 nl->next = nl_size + (strlen(nl->name) + 1);
5110 if (remaining_data < nl->next) {
5111 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5112 break;
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;
5118 if (!next) {
5119 break;
5121 nl = (void*)nl + next;
5123 break;
5125 case DM_DEV_WAIT:
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);
5132 int i;
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;
5140 break;
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;
5147 break;
5149 case DM_TABLE_DEPS:
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;
5155 int i;
5157 *(uint32_t*)argptr = tswap32(count);
5158 for (i = 0; i < count; i++) {
5159 *gdev = tswap64(*hdev);
5160 gdev++;
5161 hdev++;
5163 break;
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);
5173 while (1) {
5174 uint32_t next = vers->next;
5175 if (next) {
5176 vers->next = vers_size + (strlen(vers->name) + 1);
5178 if (remaining_data < vers->next) {
5179 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5180 break;
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;
5186 if (!next) {
5187 break;
5189 vers = (void*)vers + next;
5191 break;
5193 default:
5194 unlock_user(argptr, guest_data, 0);
5195 ret = -TARGET_EINVAL;
5196 goto out;
5198 unlock_user(argptr, guest_data, guest_data_size);
5200 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5201 if (!argptr) {
5202 ret = -TARGET_EFAULT;
5203 goto out;
5205 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5206 unlock_user(argptr, arg, target_size);
5208 out:
5209 g_free(big_buf);
5210 return ret;
5213 static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
5214 int cmd, abi_long arg)
5216 void *argptr;
5217 int target_size;
5218 const argtype *arg_type = ie->arg_type;
5219 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5220 abi_long ret;
5222 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5223 struct blkpg_partition host_part;
5225 /* Read and convert blkpg */
5226 arg_type++;
5227 target_size = thunk_type_size(arg_type, 0);
5228 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5229 if (!argptr) {
5230 ret = -TARGET_EFAULT;
5231 goto out;
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 */
5240 break;
5241 default:
5242 /* Unknown opcode */
5243 ret = -TARGET_EINVAL;
5244 goto out;
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);
5251 if (!argptr) {
5252 ret = -TARGET_EFAULT;
5253 goto out;
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));
5262 out:
5263 return ret;
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;
5273 int target_size;
5274 void *argptr;
5275 abi_ulong *target_rt_dev_ptr = NULL;
5276 unsigned long *host_rt_dev_ptr = NULL;
5277 abi_long ret;
5278 int i;
5280 assert(ie->access == IOC_W);
5281 assert(*arg_type == TYPE_PTR);
5282 arg_type++;
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);
5286 if (!argptr) {
5287 return -TARGET_EFAULT;
5289 arg_type++;
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;
5309 } else {
5310 *host_rt_dev_ptr = 0;
5312 field_types++;
5313 continue;
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);
5329 return ret;
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)
5342 struct timeval tv;
5343 abi_long ret;
5345 ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
5346 if (is_error(ret)) {
5347 return ret;
5350 if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
5351 if (copy_to_user_timeval(arg, &tv)) {
5352 return -TARGET_EFAULT;
5354 } else {
5355 if (copy_to_user_timeval64(arg, &tv)) {
5356 return -TARGET_EFAULT;
5360 return ret;
5363 static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
5364 int fd, int cmd, abi_long arg)
5366 struct timespec ts;
5367 abi_long ret;
5369 ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
5370 if (is_error(ret)) {
5371 return ret;
5374 if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
5375 if (host_to_target_timespec(arg, &ts)) {
5376 return -TARGET_EFAULT;
5378 } else{
5379 if (host_to_target_timespec64(arg, &ts)) {
5380 return -TARGET_EFAULT;
5384 return ret;
5387 #ifdef TIOCGPTPEER
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));
5394 #endif
5396 #ifdef HAVE_DRM_H
5398 static void unlock_drm_version(struct drm_version *host_ver,
5399 struct target_drm_version *target_ver,
5400 bool copy)
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) {
5420 return -EFAULT;
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) {
5429 goto err;
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) {
5438 goto err;
5442 return 0;
5443 err:
5444 unlock_drm_version(host_ver, target_ver, false);
5445 return -EFAULT;
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;
5466 abi_long ret;
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);
5479 } else {
5480 host_to_target_drmversion(target_ver, ver);
5483 unlock_user_struct(target_ver, arg, 0);
5484 return ret;
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)
5493 abi_long ret;
5494 int value;
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);
5507 return ret;
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,
5517 fd, arg);
5518 default:
5519 return -TARGET_ENOSYS;
5523 #endif
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;
5530 char *target_addr;
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 >
5544 MAX_STRUCT_SIZE) {
5545 return -TARGET_EFAULT;
5548 target_addr = lock_user(VERIFY_READ,
5549 arg + offsetof(struct tun_filter, addr),
5550 filter->count * ETH_ALEN, 1);
5551 if (!target_addr) {
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 },
5568 #include "ioctls.h"
5569 { 0, 0, },
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;
5578 abi_long ret;
5579 uint8_t buf_temp[MAX_STRUCT_SIZE];
5580 int target_size;
5581 void *argptr;
5583 ie = ioctl_entries;
5584 for(;;) {
5585 if (ie->target_cmd == 0) {
5586 qemu_log_mask(
5587 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
5588 return -TARGET_ENOTTY;
5590 if (ie->target_cmd == cmd)
5591 break;
5592 ie++;
5594 arg_type = ie->arg_type;
5595 if (ie->do_ioctl) {
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]) {
5604 case TYPE_NULL:
5605 /* no argument */
5606 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
5607 break;
5608 case TYPE_PTRVOID:
5609 case TYPE_INT:
5610 case TYPE_LONG:
5611 case TYPE_ULONG:
5612 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
5613 break;
5614 case TYPE_PTR:
5615 arg_type++;
5616 target_size = thunk_type_size(arg_type, 0);
5617 switch(ie->access) {
5618 case IOC_R:
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);
5622 if (!argptr)
5623 return -TARGET_EFAULT;
5624 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5625 unlock_user(argptr, arg, target_size);
5627 break;
5628 case IOC_W:
5629 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5630 if (!argptr)
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));
5635 break;
5636 default:
5637 case IOC_RW:
5638 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5639 if (!argptr)
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);
5646 if (!argptr)
5647 return -TARGET_EFAULT;
5648 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5649 unlock_user(argptr, arg, target_size);
5651 break;
5653 break;
5654 default:
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;
5659 break;
5661 return ret;
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;
5767 host->c_iflag =
5768 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5769 host->c_oflag =
5770 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5771 host->c_cflag =
5772 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5773 host->c_lflag =
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;
5802 target->c_iflag =
5803 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5804 target->c_oflag =
5805 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5806 target->c_cflag =
5807 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5808 target->c_lflag =
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. */
5840 #ifndef MAP_SYNC
5841 #define MAP_SYNC 0
5842 #endif
5843 #ifndef MAP_UNINITIALIZED
5844 #define MAP_UNINITIALIZED 0
5845 #endif
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
5879 #endif
5880 #ifndef TARGET_MAP_HUGE_2MB
5881 #define TARGET_MAP_HUGE_2MB 0
5882 #endif
5883 #ifndef TARGET_MAP_HUGE_1GB
5884 #define TARGET_MAP_HUGE_1GB 0
5885 #endif
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.
5893 enum {
5894 TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
5895 | TARGET_MAP_PRIVATE
5896 | TARGET_MAP_FIXED
5897 | TARGET_MAP_ANONYMOUS
5898 | TARGET_MAP_DENYWRITE
5899 | TARGET_MAP_EXECUTABLE
5900 | TARGET_MAP_UNINITIALIZED
5901 | TARGET_MAP_GROWSDOWN
5902 | TARGET_MAP_LOCKED
5903 | TARGET_MAP_NORESERVE
5904 | TARGET_MAP_POPULATE
5905 | TARGET_MAP_NONBLOCK
5906 | TARGET_MAP_STACK
5907 | TARGET_MAP_HUGETLB
5908 | TARGET_MAP_32BIT
5909 | TARGET_MAP_HUGE_2MB
5910 | TARGET_MAP_HUGE_1GB
5912 int host_flags;
5914 switch (target_flags & TARGET_MAP_TYPE) {
5915 case TARGET_MAP_PRIVATE:
5916 host_flags = MAP_PRIVATE;
5917 break;
5918 case TARGET_MAP_SHARED:
5919 host_flags = MAP_SHARED;
5920 break;
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;
5933 break;
5934 default:
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)
5953 int size;
5954 void *p;
5956 if (!ldt_table)
5957 return 0;
5958 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5959 if (size > bytecount)
5960 size = bytecount;
5961 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5962 if (!p)
5963 return -TARGET_EFAULT;
5964 /* ??? Should this by byteswapped? */
5965 memcpy(p, ldt_table, size);
5966 unlock_user(p, ptr, size);
5967 return 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;
5998 #ifdef TARGET_ABI32
5999 lm = 0;
6000 #else
6001 lm = (ldt_info.flags >> 7) & 1;
6002 #endif
6003 if (contents == 3) {
6004 if (oldmode)
6005 return -TARGET_EINVAL;
6006 if (seg_not_present == 0)
6007 return -TARGET_EINVAL;
6009 /* allocate the LDT */
6010 if (!ldt_table) {
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) {
6026 if (oldmode ||
6027 (contents == 0 &&
6028 read_exec_only == 1 &&
6029 seg_32bit == 0 &&
6030 limit_in_pages == 0 &&
6031 seg_not_present == 1 &&
6032 useable == 0 )) {
6033 entry_1 = 0;
6034 entry_2 = 0;
6035 goto install;
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) |
6045 (contents << 10) |
6046 ((seg_not_present ^ 1) << 15) |
6047 (seg_32bit << 22) |
6048 (limit_in_pages << 23) |
6049 (lm << 21) |
6050 0x7000;
6051 if (!oldmode)
6052 entry_2 |= (useable << 20);
6054 /* Install the new entry ... */
6055 install:
6056 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
6057 lp[0] = tswap32(entry_1);
6058 lp[1] = tswap32(entry_2);
6059 return 0;
6062 /* specific and weird i386 syscalls */
6063 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
6064 unsigned long bytecount)
6066 abi_long ret;
6068 switch (func) {
6069 case 0:
6070 ret = read_ldt(ptr, bytecount);
6071 break;
6072 case 1:
6073 ret = write_ldt(env, ptr, bytecount, 1);
6074 break;
6075 case 0x11:
6076 ret = write_ldt(env, ptr, bytecount, 0);
6077 break;
6078 default:
6079 ret = -TARGET_ENOSYS;
6080 break;
6082 return ret;
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;
6094 int i;
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);
6108 break;
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;
6123 #ifdef TARGET_ABI32
6124 lm = 0;
6125 #else
6126 lm = (ldt_info.flags >> 7) & 1;
6127 #endif
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 &&
6139 seg_32bit == 0 &&
6140 limit_in_pages == 0 &&
6141 seg_not_present == 1 &&
6142 useable == 0 )) {
6143 entry_1 = 0;
6144 entry_2 = 0;
6145 goto install;
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) |
6155 (contents << 10) |
6156 ((seg_not_present ^ 1) << 15) |
6157 (seg_32bit << 22) |
6158 (limit_in_pages << 23) |
6159 (useable << 20) |
6160 (lm << 21) |
6161 0x7000;
6163 /* Install the new entry ... */
6164 install:
6165 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
6166 lp[0] = tswap32(entry_1);
6167 lp[1] = tswap32(entry_2);
6168 return 0;
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;
6199 #ifdef TARGET_ABI32
6200 lm = 0;
6201 #else
6202 lm = (entry_2 >> 21) & 1;
6203 #endif
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);
6215 return 0;
6218 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6220 return -TARGET_ENOSYS;
6222 #else
6223 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6225 abi_long ret = 0;
6226 abi_ulong val;
6227 int idx;
6229 switch(code) {
6230 case TARGET_ARCH_SET_GS:
6231 case TARGET_ARCH_SET_FS:
6232 if (code == TARGET_ARCH_SET_GS)
6233 idx = R_GS;
6234 else
6235 idx = R_FS;
6236 cpu_x86_load_seg(env, idx, 0);
6237 env->segs[idx].base = addr;
6238 break;
6239 case TARGET_ARCH_GET_GS:
6240 case TARGET_ARCH_GET_FS:
6241 if (code == TARGET_ARCH_GET_GS)
6242 idx = R_GS;
6243 else
6244 idx = R_FS;
6245 val = env->segs[idx].base;
6246 if (put_user(val, addr, abi_ulong))
6247 ret = -TARGET_EFAULT;
6248 break;
6249 default:
6250 ret = -TARGET_EINVAL;
6251 break;
6253 return ret;
6255 #endif /* defined(TARGET_ABI32 */
6256 #endif /* defined(TARGET_I386) */
6259 * These constants are generic. Supply any that are missing from the host.
6261 #ifndef PR_SET_NAME
6262 # define PR_SET_NAME 15
6263 # define PR_GET_NAME 16
6264 #endif
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)
6270 #endif
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)
6276 #endif
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)
6284 #endif
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)
6289 #endif
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)
6298 #endif
6299 #ifndef PR_SET_IO_FLUSHER
6300 # define PR_SET_IO_FLUSHER 57
6301 # define PR_GET_IO_FLUSHER 58
6302 #endif
6303 #ifndef PR_SET_SYSCALL_USER_DISPATCH
6304 # define PR_SET_SYSCALL_USER_DISPATCH 59
6305 #endif
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)
6311 #endif
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
6327 #endif
6328 #ifndef do_prctl_set_fp_mode
6329 #define do_prctl_set_fp_mode do_prctl_inval1
6330 #endif
6331 #ifndef do_prctl_sve_get_vl
6332 #define do_prctl_sve_get_vl do_prctl_inval0
6333 #endif
6334 #ifndef do_prctl_sve_set_vl
6335 #define do_prctl_sve_set_vl do_prctl_inval1
6336 #endif
6337 #ifndef do_prctl_reset_keys
6338 #define do_prctl_reset_keys do_prctl_inval1
6339 #endif
6340 #ifndef do_prctl_set_tagged_addr_ctrl
6341 #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6342 #endif
6343 #ifndef do_prctl_get_tagged_addr_ctrl
6344 #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6345 #endif
6346 #ifndef do_prctl_get_unalign
6347 #define do_prctl_get_unalign do_prctl_inval1
6348 #endif
6349 #ifndef do_prctl_set_unalign
6350 #define do_prctl_set_unalign do_prctl_inval1
6351 #endif
6352 #ifndef do_prctl_sme_get_vl
6353 #define do_prctl_sme_get_vl do_prctl_inval0
6354 #endif
6355 #ifndef do_prctl_sme_set_vl
6356 #define do_prctl_sme_set_vl do_prctl_inval1
6357 #endif
6359 static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
6360 abi_long arg3, abi_long arg4, abi_long arg5)
6362 abi_long ret;
6364 switch (option) {
6365 case PR_GET_PDEATHSIG:
6367 int deathsig;
6368 ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
6369 arg3, arg4, arg5));
6370 if (!is_error(ret) &&
6371 put_user_s32(host_to_target_signal(deathsig), arg2)) {
6372 return -TARGET_EFAULT;
6374 return ret;
6376 case PR_SET_PDEATHSIG:
6377 return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
6378 arg3, arg4, arg5));
6379 case PR_GET_NAME:
6381 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
6382 if (!name) {
6383 return -TARGET_EFAULT;
6385 ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
6386 arg3, arg4, arg5));
6387 unlock_user(name, arg2, 16);
6388 return ret;
6390 case PR_SET_NAME:
6392 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
6393 if (!name) {
6394 return -TARGET_EFAULT;
6396 ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
6397 arg3, arg4, arg5));
6398 unlock_user(name, arg2, 0);
6399 return ret;
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);
6405 case PR_SVE_GET_VL:
6406 return do_prctl_sve_get_vl(env);
6407 case PR_SVE_SET_VL:
6408 return do_prctl_sve_set_vl(env, arg2);
6409 case PR_SME_GET_VL:
6410 return do_prctl_sme_get_vl(env);
6411 case PR_SME_SET_VL:
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:
6443 case PR_GET_TIMING:
6444 case PR_SET_TIMING:
6445 case PR_GET_TIMERSLACK:
6446 case PR_SET_TIMERSLACK:
6447 case PR_MCE_KILL:
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 case PR_SET_CHILD_SUBREAPER:
6454 case PR_GET_SPECULATION_CTRL:
6455 case PR_SET_SPECULATION_CTRL:
6456 /* Some prctl options have no pointer arguments and we can pass on. */
6457 return get_errno(prctl(option, arg2, arg3, arg4, arg5));
6459 case PR_GET_CHILD_SUBREAPER:
6461 int val;
6462 ret = get_errno(prctl(PR_GET_CHILD_SUBREAPER, &val,
6463 arg3, arg4, arg5));
6464 if (!is_error(ret) && put_user_s32(val, arg2)) {
6465 return -TARGET_EFAULT;
6467 return ret;
6470 case PR_GET_TID_ADDRESS:
6472 TaskState *ts = env_cpu(env)->opaque;
6473 return put_user_ual(ts->child_tidptr, arg2);
6476 case PR_GET_FPEXC:
6477 case PR_SET_FPEXC:
6478 /* Was used for SPE on PowerPC. */
6479 return -TARGET_EINVAL;
6481 case PR_GET_ENDIAN:
6482 case PR_SET_ENDIAN:
6483 case PR_GET_FPEMU:
6484 case PR_SET_FPEMU:
6485 case PR_SET_MM:
6486 case PR_GET_SECCOMP:
6487 case PR_SET_SECCOMP:
6488 case PR_SET_SYSCALL_USER_DISPATCH:
6489 case PR_GET_THP_DISABLE:
6490 case PR_SET_THP_DISABLE:
6491 case PR_GET_TSC:
6492 case PR_SET_TSC:
6493 /* Disable to prevent the target disabling stuff we need. */
6494 return -TARGET_EINVAL;
6496 default:
6497 qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
6498 option);
6499 return -TARGET_EINVAL;
6503 #define NEW_STACK_SIZE 0x40000
6506 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6507 typedef struct {
6508 CPUArchState *env;
6509 pthread_mutex_t mutex;
6510 pthread_cond_t cond;
6511 pthread_t thread;
6512 uint32_t tid;
6513 abi_ulong child_tidptr;
6514 abi_ulong parent_tidptr;
6515 sigset_t sigmask;
6516 } new_thread_info;
6518 static void * G_NORETURN clone_func(void *arg)
6520 new_thread_info *info = arg;
6521 CPUArchState *env;
6522 CPUState *cpu;
6523 TaskState *ts;
6525 rcu_register_thread();
6526 tcg_register_thread();
6527 env = info->env;
6528 cpu = env_cpu(env);
6529 thread_cpu = cpu;
6530 ts = get_task_state(cpu);
6531 info->tid = sys_gettid();
6532 task_settid(ts);
6533 if (info->child_tidptr)
6534 put_user_u32(info->tid, info->child_tidptr);
6535 if (info->parent_tidptr)
6536 put_user_u32(info->tid, info->parent_tidptr);
6537 qemu_guest_random_seed_thread_part2(cpu->random_seed);
6538 /* Enable signals. */
6539 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6540 /* Signal to the parent that we're ready. */
6541 pthread_mutex_lock(&info->mutex);
6542 pthread_cond_broadcast(&info->cond);
6543 pthread_mutex_unlock(&info->mutex);
6544 /* Wait until the parent has finished initializing the tls state. */
6545 pthread_mutex_lock(&clone_lock);
6546 pthread_mutex_unlock(&clone_lock);
6547 cpu_loop(env);
6548 /* never exits */
6551 /* do_fork() Must return host values and target errnos (unlike most
6552 do_*() functions). */
6553 static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
6554 abi_ulong parent_tidptr, target_ulong newtls,
6555 abi_ulong child_tidptr)
6557 CPUState *cpu = env_cpu(env);
6558 int ret;
6559 TaskState *ts;
6560 CPUState *new_cpu;
6561 CPUArchState *new_env;
6562 sigset_t sigmask;
6564 flags &= ~CLONE_IGNORED_FLAGS;
6566 /* Emulate vfork() with fork() */
6567 if (flags & CLONE_VFORK)
6568 flags &= ~(CLONE_VFORK | CLONE_VM);
6570 if (flags & CLONE_VM) {
6571 TaskState *parent_ts = get_task_state(cpu);
6572 new_thread_info info;
6573 pthread_attr_t attr;
6575 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6576 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6577 return -TARGET_EINVAL;
6580 ts = g_new0(TaskState, 1);
6581 init_task_state(ts);
6583 /* Grab a mutex so that thread setup appears atomic. */
6584 pthread_mutex_lock(&clone_lock);
6587 * If this is our first additional thread, we need to ensure we
6588 * generate code for parallel execution and flush old translations.
6589 * Do this now so that the copy gets CF_PARALLEL too.
6591 if (!(cpu->tcg_cflags & CF_PARALLEL)) {
6592 cpu->tcg_cflags |= CF_PARALLEL;
6593 tb_flush(cpu);
6596 /* we create a new CPU instance. */
6597 new_env = cpu_copy(env);
6598 /* Init regs that differ from the parent. */
6599 cpu_clone_regs_child(new_env, newsp, flags);
6600 cpu_clone_regs_parent(env, flags);
6601 new_cpu = env_cpu(new_env);
6602 new_cpu->opaque = ts;
6603 ts->bprm = parent_ts->bprm;
6604 ts->info = parent_ts->info;
6605 ts->signal_mask = parent_ts->signal_mask;
6607 if (flags & CLONE_CHILD_CLEARTID) {
6608 ts->child_tidptr = child_tidptr;
6611 if (flags & CLONE_SETTLS) {
6612 cpu_set_tls (new_env, newtls);
6615 memset(&info, 0, sizeof(info));
6616 pthread_mutex_init(&info.mutex, NULL);
6617 pthread_mutex_lock(&info.mutex);
6618 pthread_cond_init(&info.cond, NULL);
6619 info.env = new_env;
6620 if (flags & CLONE_CHILD_SETTID) {
6621 info.child_tidptr = child_tidptr;
6623 if (flags & CLONE_PARENT_SETTID) {
6624 info.parent_tidptr = parent_tidptr;
6627 ret = pthread_attr_init(&attr);
6628 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6629 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
6630 /* It is not safe to deliver signals until the child has finished
6631 initializing, so temporarily block all signals. */
6632 sigfillset(&sigmask);
6633 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
6634 cpu->random_seed = qemu_guest_random_seed_thread_part1();
6636 ret = pthread_create(&info.thread, &attr, clone_func, &info);
6637 /* TODO: Free new CPU state if thread creation failed. */
6639 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6640 pthread_attr_destroy(&attr);
6641 if (ret == 0) {
6642 /* Wait for the child to initialize. */
6643 pthread_cond_wait(&info.cond, &info.mutex);
6644 ret = info.tid;
6645 } else {
6646 ret = -1;
6648 pthread_mutex_unlock(&info.mutex);
6649 pthread_cond_destroy(&info.cond);
6650 pthread_mutex_destroy(&info.mutex);
6651 pthread_mutex_unlock(&clone_lock);
6652 } else {
6653 /* if no CLONE_VM, we consider it is a fork */
6654 if (flags & CLONE_INVALID_FORK_FLAGS) {
6655 return -TARGET_EINVAL;
6658 /* We can't support custom termination signals */
6659 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
6660 return -TARGET_EINVAL;
6663 #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
6664 if (flags & CLONE_PIDFD) {
6665 return -TARGET_EINVAL;
6667 #endif
6669 /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
6670 if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
6671 return -TARGET_EINVAL;
6674 if (block_signals()) {
6675 return -QEMU_ERESTARTSYS;
6678 fork_start();
6679 ret = fork();
6680 if (ret == 0) {
6681 /* Child Process. */
6682 cpu_clone_regs_child(env, newsp, flags);
6683 fork_end(ret);
6684 /* There is a race condition here. The parent process could
6685 theoretically read the TID in the child process before the child
6686 tid is set. This would require using either ptrace
6687 (not implemented) or having *_tidptr to point at a shared memory
6688 mapping. We can't repeat the spinlock hack used above because
6689 the child process gets its own copy of the lock. */
6690 if (flags & CLONE_CHILD_SETTID)
6691 put_user_u32(sys_gettid(), child_tidptr);
6692 if (flags & CLONE_PARENT_SETTID)
6693 put_user_u32(sys_gettid(), parent_tidptr);
6694 ts = get_task_state(cpu);
6695 if (flags & CLONE_SETTLS)
6696 cpu_set_tls (env, newtls);
6697 if (flags & CLONE_CHILD_CLEARTID)
6698 ts->child_tidptr = child_tidptr;
6699 } else {
6700 cpu_clone_regs_parent(env, flags);
6701 if (flags & CLONE_PIDFD) {
6702 int pid_fd = 0;
6703 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
6704 int pid_child = ret;
6705 pid_fd = pidfd_open(pid_child, 0);
6706 if (pid_fd >= 0) {
6707 fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
6708 | FD_CLOEXEC);
6709 } else {
6710 pid_fd = 0;
6712 #endif
6713 put_user_u32(pid_fd, parent_tidptr);
6715 fork_end(ret);
6717 g_assert(!cpu_in_exclusive_context(cpu));
6719 return ret;
6722 /* warning : doesn't handle linux specific flags... */
6723 static int target_to_host_fcntl_cmd(int cmd)
6725 int ret;
6727 switch(cmd) {
6728 case TARGET_F_DUPFD:
6729 case TARGET_F_GETFD:
6730 case TARGET_F_SETFD:
6731 case TARGET_F_GETFL:
6732 case TARGET_F_SETFL:
6733 case TARGET_F_OFD_GETLK:
6734 case TARGET_F_OFD_SETLK:
6735 case TARGET_F_OFD_SETLKW:
6736 ret = cmd;
6737 break;
6738 case TARGET_F_GETLK:
6739 ret = F_GETLK64;
6740 break;
6741 case TARGET_F_SETLK:
6742 ret = F_SETLK64;
6743 break;
6744 case TARGET_F_SETLKW:
6745 ret = F_SETLKW64;
6746 break;
6747 case TARGET_F_GETOWN:
6748 ret = F_GETOWN;
6749 break;
6750 case TARGET_F_SETOWN:
6751 ret = F_SETOWN;
6752 break;
6753 case TARGET_F_GETSIG:
6754 ret = F_GETSIG;
6755 break;
6756 case TARGET_F_SETSIG:
6757 ret = F_SETSIG;
6758 break;
6759 #if TARGET_ABI_BITS == 32
6760 case TARGET_F_GETLK64:
6761 ret = F_GETLK64;
6762 break;
6763 case TARGET_F_SETLK64:
6764 ret = F_SETLK64;
6765 break;
6766 case TARGET_F_SETLKW64:
6767 ret = F_SETLKW64;
6768 break;
6769 #endif
6770 case TARGET_F_SETLEASE:
6771 ret = F_SETLEASE;
6772 break;
6773 case TARGET_F_GETLEASE:
6774 ret = F_GETLEASE;
6775 break;
6776 #ifdef F_DUPFD_CLOEXEC
6777 case TARGET_F_DUPFD_CLOEXEC:
6778 ret = F_DUPFD_CLOEXEC;
6779 break;
6780 #endif
6781 case TARGET_F_NOTIFY:
6782 ret = F_NOTIFY;
6783 break;
6784 #ifdef F_GETOWN_EX
6785 case TARGET_F_GETOWN_EX:
6786 ret = F_GETOWN_EX;
6787 break;
6788 #endif
6789 #ifdef F_SETOWN_EX
6790 case TARGET_F_SETOWN_EX:
6791 ret = F_SETOWN_EX;
6792 break;
6793 #endif
6794 #ifdef F_SETPIPE_SZ
6795 case TARGET_F_SETPIPE_SZ:
6796 ret = F_SETPIPE_SZ;
6797 break;
6798 case TARGET_F_GETPIPE_SZ:
6799 ret = F_GETPIPE_SZ;
6800 break;
6801 #endif
6802 #ifdef F_ADD_SEALS
6803 case TARGET_F_ADD_SEALS:
6804 ret = F_ADD_SEALS;
6805 break;
6806 case TARGET_F_GET_SEALS:
6807 ret = F_GET_SEALS;
6808 break;
6809 #endif
6810 default:
6811 ret = -TARGET_EINVAL;
6812 break;
6815 #if defined(__powerpc64__)
6816 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6817 * is not supported by kernel. The glibc fcntl call actually adjusts
6818 * them to 5, 6 and 7 before making the syscall(). Since we make the
6819 * syscall directly, adjust to what is supported by the kernel.
6821 if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
6822 ret -= F_GETLK64 - 5;
6824 #endif
6826 return ret;
6829 #define FLOCK_TRANSTBL \
6830 switch (type) { \
6831 TRANSTBL_CONVERT(F_RDLCK); \
6832 TRANSTBL_CONVERT(F_WRLCK); \
6833 TRANSTBL_CONVERT(F_UNLCK); \
6836 static int target_to_host_flock(int type)
6838 #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6839 FLOCK_TRANSTBL
6840 #undef TRANSTBL_CONVERT
6841 return -TARGET_EINVAL;
6844 static int host_to_target_flock(int type)
6846 #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6847 FLOCK_TRANSTBL
6848 #undef TRANSTBL_CONVERT
6849 /* if we don't know how to convert the value coming
6850 * from the host we copy to the target field as-is
6852 return type;
6855 static inline abi_long copy_from_user_flock(struct flock64 *fl,
6856 abi_ulong target_flock_addr)
6858 struct target_flock *target_fl;
6859 int l_type;
6861 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6862 return -TARGET_EFAULT;
6865 __get_user(l_type, &target_fl->l_type);
6866 l_type = target_to_host_flock(l_type);
6867 if (l_type < 0) {
6868 return l_type;
6870 fl->l_type = l_type;
6871 __get_user(fl->l_whence, &target_fl->l_whence);
6872 __get_user(fl->l_start, &target_fl->l_start);
6873 __get_user(fl->l_len, &target_fl->l_len);
6874 __get_user(fl->l_pid, &target_fl->l_pid);
6875 unlock_user_struct(target_fl, target_flock_addr, 0);
6876 return 0;
6879 static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6880 const struct flock64 *fl)
6882 struct target_flock *target_fl;
6883 short l_type;
6885 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6886 return -TARGET_EFAULT;
6889 l_type = host_to_target_flock(fl->l_type);
6890 __put_user(l_type, &target_fl->l_type);
6891 __put_user(fl->l_whence, &target_fl->l_whence);
6892 __put_user(fl->l_start, &target_fl->l_start);
6893 __put_user(fl->l_len, &target_fl->l_len);
6894 __put_user(fl->l_pid, &target_fl->l_pid);
6895 unlock_user_struct(target_fl, target_flock_addr, 1);
6896 return 0;
6899 typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6900 typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6902 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6903 struct target_oabi_flock64 {
6904 abi_short l_type;
6905 abi_short l_whence;
6906 abi_llong l_start;
6907 abi_llong l_len;
6908 abi_int l_pid;
6909 } QEMU_PACKED;
6911 static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
6912 abi_ulong target_flock_addr)
6914 struct target_oabi_flock64 *target_fl;
6915 int l_type;
6917 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6918 return -TARGET_EFAULT;
6921 __get_user(l_type, &target_fl->l_type);
6922 l_type = target_to_host_flock(l_type);
6923 if (l_type < 0) {
6924 return l_type;
6926 fl->l_type = l_type;
6927 __get_user(fl->l_whence, &target_fl->l_whence);
6928 __get_user(fl->l_start, &target_fl->l_start);
6929 __get_user(fl->l_len, &target_fl->l_len);
6930 __get_user(fl->l_pid, &target_fl->l_pid);
6931 unlock_user_struct(target_fl, target_flock_addr, 0);
6932 return 0;
6935 static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
6936 const struct flock64 *fl)
6938 struct target_oabi_flock64 *target_fl;
6939 short l_type;
6941 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6942 return -TARGET_EFAULT;
6945 l_type = host_to_target_flock(fl->l_type);
6946 __put_user(l_type, &target_fl->l_type);
6947 __put_user(fl->l_whence, &target_fl->l_whence);
6948 __put_user(fl->l_start, &target_fl->l_start);
6949 __put_user(fl->l_len, &target_fl->l_len);
6950 __put_user(fl->l_pid, &target_fl->l_pid);
6951 unlock_user_struct(target_fl, target_flock_addr, 1);
6952 return 0;
6954 #endif
6956 static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6957 abi_ulong target_flock_addr)
6959 struct target_flock64 *target_fl;
6960 int l_type;
6962 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6963 return -TARGET_EFAULT;
6966 __get_user(l_type, &target_fl->l_type);
6967 l_type = target_to_host_flock(l_type);
6968 if (l_type < 0) {
6969 return l_type;
6971 fl->l_type = l_type;
6972 __get_user(fl->l_whence, &target_fl->l_whence);
6973 __get_user(fl->l_start, &target_fl->l_start);
6974 __get_user(fl->l_len, &target_fl->l_len);
6975 __get_user(fl->l_pid, &target_fl->l_pid);
6976 unlock_user_struct(target_fl, target_flock_addr, 0);
6977 return 0;
6980 static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
6981 const struct flock64 *fl)
6983 struct target_flock64 *target_fl;
6984 short l_type;
6986 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6987 return -TARGET_EFAULT;
6990 l_type = host_to_target_flock(fl->l_type);
6991 __put_user(l_type, &target_fl->l_type);
6992 __put_user(fl->l_whence, &target_fl->l_whence);
6993 __put_user(fl->l_start, &target_fl->l_start);
6994 __put_user(fl->l_len, &target_fl->l_len);
6995 __put_user(fl->l_pid, &target_fl->l_pid);
6996 unlock_user_struct(target_fl, target_flock_addr, 1);
6997 return 0;
7000 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
7002 struct flock64 fl64;
7003 #ifdef F_GETOWN_EX
7004 struct f_owner_ex fox;
7005 struct target_f_owner_ex *target_fox;
7006 #endif
7007 abi_long ret;
7008 int host_cmd = target_to_host_fcntl_cmd(cmd);
7010 if (host_cmd == -TARGET_EINVAL)
7011 return host_cmd;
7013 switch(cmd) {
7014 case TARGET_F_GETLK:
7015 ret = copy_from_user_flock(&fl64, arg);
7016 if (ret) {
7017 return ret;
7019 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7020 if (ret == 0) {
7021 ret = copy_to_user_flock(arg, &fl64);
7023 break;
7025 case TARGET_F_SETLK:
7026 case TARGET_F_SETLKW:
7027 ret = copy_from_user_flock(&fl64, arg);
7028 if (ret) {
7029 return ret;
7031 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7032 break;
7034 case TARGET_F_GETLK64:
7035 case TARGET_F_OFD_GETLK:
7036 ret = copy_from_user_flock64(&fl64, arg);
7037 if (ret) {
7038 return ret;
7040 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7041 if (ret == 0) {
7042 ret = copy_to_user_flock64(arg, &fl64);
7044 break;
7045 case TARGET_F_SETLK64:
7046 case TARGET_F_SETLKW64:
7047 case TARGET_F_OFD_SETLK:
7048 case TARGET_F_OFD_SETLKW:
7049 ret = copy_from_user_flock64(&fl64, arg);
7050 if (ret) {
7051 return ret;
7053 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7054 break;
7056 case TARGET_F_GETFL:
7057 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
7058 if (ret >= 0) {
7059 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
7060 /* tell 32-bit guests it uses largefile on 64-bit hosts: */
7061 if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
7062 ret |= TARGET_O_LARGEFILE;
7065 break;
7067 case TARGET_F_SETFL:
7068 ret = get_errno(safe_fcntl(fd, host_cmd,
7069 target_to_host_bitmask(arg,
7070 fcntl_flags_tbl)));
7071 break;
7073 #ifdef F_GETOWN_EX
7074 case TARGET_F_GETOWN_EX:
7075 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
7076 if (ret >= 0) {
7077 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
7078 return -TARGET_EFAULT;
7079 target_fox->type = tswap32(fox.type);
7080 target_fox->pid = tswap32(fox.pid);
7081 unlock_user_struct(target_fox, arg, 1);
7083 break;
7084 #endif
7086 #ifdef F_SETOWN_EX
7087 case TARGET_F_SETOWN_EX:
7088 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
7089 return -TARGET_EFAULT;
7090 fox.type = tswap32(target_fox->type);
7091 fox.pid = tswap32(target_fox->pid);
7092 unlock_user_struct(target_fox, arg, 0);
7093 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
7094 break;
7095 #endif
7097 case TARGET_F_SETSIG:
7098 ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
7099 break;
7101 case TARGET_F_GETSIG:
7102 ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
7103 break;
7105 case TARGET_F_SETOWN:
7106 case TARGET_F_GETOWN:
7107 case TARGET_F_SETLEASE:
7108 case TARGET_F_GETLEASE:
7109 case TARGET_F_SETPIPE_SZ:
7110 case TARGET_F_GETPIPE_SZ:
7111 case TARGET_F_ADD_SEALS:
7112 case TARGET_F_GET_SEALS:
7113 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
7114 break;
7116 default:
7117 ret = get_errno(safe_fcntl(fd, cmd, arg));
7118 break;
7120 return ret;
7123 #ifdef USE_UID16
7125 static inline int high2lowuid(int uid)
7127 if (uid > 65535)
7128 return 65534;
7129 else
7130 return uid;
7133 static inline int high2lowgid(int gid)
7135 if (gid > 65535)
7136 return 65534;
7137 else
7138 return gid;
7141 static inline int low2highuid(int uid)
7143 if ((int16_t)uid == -1)
7144 return -1;
7145 else
7146 return uid;
7149 static inline int low2highgid(int gid)
7151 if ((int16_t)gid == -1)
7152 return -1;
7153 else
7154 return gid;
7156 static inline int tswapid(int id)
7158 return tswap16(id);
7161 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7163 #else /* !USE_UID16 */
7164 static inline int high2lowuid(int uid)
7166 return uid;
7168 static inline int high2lowgid(int gid)
7170 return gid;
7172 static inline int low2highuid(int uid)
7174 return uid;
7176 static inline int low2highgid(int gid)
7178 return gid;
7180 static inline int tswapid(int id)
7182 return tswap32(id);
7185 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7187 #endif /* USE_UID16 */
7189 /* We must do direct syscalls for setting UID/GID, because we want to
7190 * implement the Linux system call semantics of "change only for this thread",
7191 * not the libc/POSIX semantics of "change for all threads in process".
7192 * (See http://ewontfix.com/17/ for more details.)
7193 * We use the 32-bit version of the syscalls if present; if it is not
7194 * then either the host architecture supports 32-bit UIDs natively with
7195 * the standard syscall, or the 16-bit UID is the best we can do.
7197 #ifdef __NR_setuid32
7198 #define __NR_sys_setuid __NR_setuid32
7199 #else
7200 #define __NR_sys_setuid __NR_setuid
7201 #endif
7202 #ifdef __NR_setgid32
7203 #define __NR_sys_setgid __NR_setgid32
7204 #else
7205 #define __NR_sys_setgid __NR_setgid
7206 #endif
7207 #ifdef __NR_setresuid32
7208 #define __NR_sys_setresuid __NR_setresuid32
7209 #else
7210 #define __NR_sys_setresuid __NR_setresuid
7211 #endif
7212 #ifdef __NR_setresgid32
7213 #define __NR_sys_setresgid __NR_setresgid32
7214 #else
7215 #define __NR_sys_setresgid __NR_setresgid
7216 #endif
7218 _syscall1(int, sys_setuid, uid_t, uid)
7219 _syscall1(int, sys_setgid, gid_t, gid)
7220 _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
7221 _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
7223 void syscall_init(void)
7225 IOCTLEntry *ie;
7226 const argtype *arg_type;
7227 int size;
7229 thunk_init(STRUCT_MAX);
7231 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
7232 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
7233 #include "syscall_types.h"
7234 #undef STRUCT
7235 #undef STRUCT_SPECIAL
7237 /* we patch the ioctl size if necessary. We rely on the fact that
7238 no ioctl has all the bits at '1' in the size field */
7239 ie = ioctl_entries;
7240 while (ie->target_cmd != 0) {
7241 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
7242 TARGET_IOC_SIZEMASK) {
7243 arg_type = ie->arg_type;
7244 if (arg_type[0] != TYPE_PTR) {
7245 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
7246 ie->target_cmd);
7247 exit(1);
7249 arg_type++;
7250 size = thunk_type_size(arg_type, 0);
7251 ie->target_cmd = (ie->target_cmd &
7252 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
7253 (size << TARGET_IOC_SIZESHIFT);
7256 /* automatic consistency check if same arch */
7257 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7258 (defined(__x86_64__) && defined(TARGET_X86_64))
7259 if (unlikely(ie->target_cmd != ie->host_cmd)) {
7260 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7261 ie->name, ie->target_cmd, ie->host_cmd);
7263 #endif
7264 ie++;
7268 #ifdef TARGET_NR_truncate64
7269 static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
7270 abi_long arg2,
7271 abi_long arg3,
7272 abi_long arg4)
7274 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
7275 arg2 = arg3;
7276 arg3 = arg4;
7278 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
7280 #endif
7282 #ifdef TARGET_NR_ftruncate64
7283 static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
7284 abi_long arg2,
7285 abi_long arg3,
7286 abi_long arg4)
7288 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
7289 arg2 = arg3;
7290 arg3 = arg4;
7292 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
7294 #endif
7296 #if defined(TARGET_NR_timer_settime) || \
7297 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
7298 static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
7299 abi_ulong target_addr)
7301 if (target_to_host_timespec(&host_its->it_interval, target_addr +
7302 offsetof(struct target_itimerspec,
7303 it_interval)) ||
7304 target_to_host_timespec(&host_its->it_value, target_addr +
7305 offsetof(struct target_itimerspec,
7306 it_value))) {
7307 return -TARGET_EFAULT;
7310 return 0;
7312 #endif
7314 #if defined(TARGET_NR_timer_settime64) || \
7315 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7316 static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
7317 abi_ulong target_addr)
7319 if (target_to_host_timespec64(&host_its->it_interval, target_addr +
7320 offsetof(struct target__kernel_itimerspec,
7321 it_interval)) ||
7322 target_to_host_timespec64(&host_its->it_value, target_addr +
7323 offsetof(struct target__kernel_itimerspec,
7324 it_value))) {
7325 return -TARGET_EFAULT;
7328 return 0;
7330 #endif
7332 #if ((defined(TARGET_NR_timerfd_gettime) || \
7333 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
7334 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
7335 static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
7336 struct itimerspec *host_its)
7338 if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7339 it_interval),
7340 &host_its->it_interval) ||
7341 host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7342 it_value),
7343 &host_its->it_value)) {
7344 return -TARGET_EFAULT;
7346 return 0;
7348 #endif
7350 #if ((defined(TARGET_NR_timerfd_gettime64) || \
7351 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7352 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7353 static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
7354 struct itimerspec *host_its)
7356 if (host_to_target_timespec64(target_addr +
7357 offsetof(struct target__kernel_itimerspec,
7358 it_interval),
7359 &host_its->it_interval) ||
7360 host_to_target_timespec64(target_addr +
7361 offsetof(struct target__kernel_itimerspec,
7362 it_value),
7363 &host_its->it_value)) {
7364 return -TARGET_EFAULT;
7366 return 0;
7368 #endif
7370 #if defined(TARGET_NR_adjtimex) || \
7371 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
7372 static inline abi_long target_to_host_timex(struct timex *host_tx,
7373 abi_long target_addr)
7375 struct target_timex *target_tx;
7377 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7378 return -TARGET_EFAULT;
7381 __get_user(host_tx->modes, &target_tx->modes);
7382 __get_user(host_tx->offset, &target_tx->offset);
7383 __get_user(host_tx->freq, &target_tx->freq);
7384 __get_user(host_tx->maxerror, &target_tx->maxerror);
7385 __get_user(host_tx->esterror, &target_tx->esterror);
7386 __get_user(host_tx->status, &target_tx->status);
7387 __get_user(host_tx->constant, &target_tx->constant);
7388 __get_user(host_tx->precision, &target_tx->precision);
7389 __get_user(host_tx->tolerance, &target_tx->tolerance);
7390 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7391 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7392 __get_user(host_tx->tick, &target_tx->tick);
7393 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7394 __get_user(host_tx->jitter, &target_tx->jitter);
7395 __get_user(host_tx->shift, &target_tx->shift);
7396 __get_user(host_tx->stabil, &target_tx->stabil);
7397 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7398 __get_user(host_tx->calcnt, &target_tx->calcnt);
7399 __get_user(host_tx->errcnt, &target_tx->errcnt);
7400 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7401 __get_user(host_tx->tai, &target_tx->tai);
7403 unlock_user_struct(target_tx, target_addr, 0);
7404 return 0;
7407 static inline abi_long host_to_target_timex(abi_long target_addr,
7408 struct timex *host_tx)
7410 struct target_timex *target_tx;
7412 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7413 return -TARGET_EFAULT;
7416 __put_user(host_tx->modes, &target_tx->modes);
7417 __put_user(host_tx->offset, &target_tx->offset);
7418 __put_user(host_tx->freq, &target_tx->freq);
7419 __put_user(host_tx->maxerror, &target_tx->maxerror);
7420 __put_user(host_tx->esterror, &target_tx->esterror);
7421 __put_user(host_tx->status, &target_tx->status);
7422 __put_user(host_tx->constant, &target_tx->constant);
7423 __put_user(host_tx->precision, &target_tx->precision);
7424 __put_user(host_tx->tolerance, &target_tx->tolerance);
7425 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7426 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7427 __put_user(host_tx->tick, &target_tx->tick);
7428 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7429 __put_user(host_tx->jitter, &target_tx->jitter);
7430 __put_user(host_tx->shift, &target_tx->shift);
7431 __put_user(host_tx->stabil, &target_tx->stabil);
7432 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7433 __put_user(host_tx->calcnt, &target_tx->calcnt);
7434 __put_user(host_tx->errcnt, &target_tx->errcnt);
7435 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7436 __put_user(host_tx->tai, &target_tx->tai);
7438 unlock_user_struct(target_tx, target_addr, 1);
7439 return 0;
7441 #endif
7444 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7445 static inline abi_long target_to_host_timex64(struct timex *host_tx,
7446 abi_long target_addr)
7448 struct target__kernel_timex *target_tx;
7450 if (copy_from_user_timeval64(&host_tx->time, target_addr +
7451 offsetof(struct target__kernel_timex,
7452 time))) {
7453 return -TARGET_EFAULT;
7456 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7457 return -TARGET_EFAULT;
7460 __get_user(host_tx->modes, &target_tx->modes);
7461 __get_user(host_tx->offset, &target_tx->offset);
7462 __get_user(host_tx->freq, &target_tx->freq);
7463 __get_user(host_tx->maxerror, &target_tx->maxerror);
7464 __get_user(host_tx->esterror, &target_tx->esterror);
7465 __get_user(host_tx->status, &target_tx->status);
7466 __get_user(host_tx->constant, &target_tx->constant);
7467 __get_user(host_tx->precision, &target_tx->precision);
7468 __get_user(host_tx->tolerance, &target_tx->tolerance);
7469 __get_user(host_tx->tick, &target_tx->tick);
7470 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7471 __get_user(host_tx->jitter, &target_tx->jitter);
7472 __get_user(host_tx->shift, &target_tx->shift);
7473 __get_user(host_tx->stabil, &target_tx->stabil);
7474 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7475 __get_user(host_tx->calcnt, &target_tx->calcnt);
7476 __get_user(host_tx->errcnt, &target_tx->errcnt);
7477 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7478 __get_user(host_tx->tai, &target_tx->tai);
7480 unlock_user_struct(target_tx, target_addr, 0);
7481 return 0;
7484 static inline abi_long host_to_target_timex64(abi_long target_addr,
7485 struct timex *host_tx)
7487 struct target__kernel_timex *target_tx;
7489 if (copy_to_user_timeval64(target_addr +
7490 offsetof(struct target__kernel_timex, time),
7491 &host_tx->time)) {
7492 return -TARGET_EFAULT;
7495 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7496 return -TARGET_EFAULT;
7499 __put_user(host_tx->modes, &target_tx->modes);
7500 __put_user(host_tx->offset, &target_tx->offset);
7501 __put_user(host_tx->freq, &target_tx->freq);
7502 __put_user(host_tx->maxerror, &target_tx->maxerror);
7503 __put_user(host_tx->esterror, &target_tx->esterror);
7504 __put_user(host_tx->status, &target_tx->status);
7505 __put_user(host_tx->constant, &target_tx->constant);
7506 __put_user(host_tx->precision, &target_tx->precision);
7507 __put_user(host_tx->tolerance, &target_tx->tolerance);
7508 __put_user(host_tx->tick, &target_tx->tick);
7509 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7510 __put_user(host_tx->jitter, &target_tx->jitter);
7511 __put_user(host_tx->shift, &target_tx->shift);
7512 __put_user(host_tx->stabil, &target_tx->stabil);
7513 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7514 __put_user(host_tx->calcnt, &target_tx->calcnt);
7515 __put_user(host_tx->errcnt, &target_tx->errcnt);
7516 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7517 __put_user(host_tx->tai, &target_tx->tai);
7519 unlock_user_struct(target_tx, target_addr, 1);
7520 return 0;
7522 #endif
7524 #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7525 #define sigev_notify_thread_id _sigev_un._tid
7526 #endif
7528 static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
7529 abi_ulong target_addr)
7531 struct target_sigevent *target_sevp;
7533 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
7534 return -TARGET_EFAULT;
7537 /* This union is awkward on 64 bit systems because it has a 32 bit
7538 * integer and a pointer in it; we follow the conversion approach
7539 * used for handling sigval types in signal.c so the guest should get
7540 * the correct value back even if we did a 64 bit byteswap and it's
7541 * using the 32 bit integer.
7543 host_sevp->sigev_value.sival_ptr =
7544 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
7545 host_sevp->sigev_signo =
7546 target_to_host_signal(tswap32(target_sevp->sigev_signo));
7547 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
7548 host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
7550 unlock_user_struct(target_sevp, target_addr, 1);
7551 return 0;
7554 #if defined(TARGET_NR_mlockall)
7555 static inline int target_to_host_mlockall_arg(int arg)
7557 int result = 0;
7559 if (arg & TARGET_MCL_CURRENT) {
7560 result |= MCL_CURRENT;
7562 if (arg & TARGET_MCL_FUTURE) {
7563 result |= MCL_FUTURE;
7565 #ifdef MCL_ONFAULT
7566 if (arg & TARGET_MCL_ONFAULT) {
7567 result |= MCL_ONFAULT;
7569 #endif
7571 return result;
7573 #endif
7575 static inline int target_to_host_msync_arg(abi_long arg)
7577 return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
7578 ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
7579 ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
7580 (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
7583 #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7584 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7585 defined(TARGET_NR_newfstatat))
7586 static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
7587 abi_ulong target_addr,
7588 struct stat *host_st)
7590 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
7591 if (cpu_env->eabi) {
7592 struct target_eabi_stat64 *target_st;
7594 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7595 return -TARGET_EFAULT;
7596 memset(target_st, 0, sizeof(struct target_eabi_stat64));
7597 __put_user(host_st->st_dev, &target_st->st_dev);
7598 __put_user(host_st->st_ino, &target_st->st_ino);
7599 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7600 __put_user(host_st->st_ino, &target_st->__st_ino);
7601 #endif
7602 __put_user(host_st->st_mode, &target_st->st_mode);
7603 __put_user(host_st->st_nlink, &target_st->st_nlink);
7604 __put_user(host_st->st_uid, &target_st->st_uid);
7605 __put_user(host_st->st_gid, &target_st->st_gid);
7606 __put_user(host_st->st_rdev, &target_st->st_rdev);
7607 __put_user(host_st->st_size, &target_st->st_size);
7608 __put_user(host_st->st_blksize, &target_st->st_blksize);
7609 __put_user(host_st->st_blocks, &target_st->st_blocks);
7610 __put_user(host_st->st_atime, &target_st->target_st_atime);
7611 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7612 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
7613 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7614 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7615 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7616 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7617 #endif
7618 unlock_user_struct(target_st, target_addr, 1);
7619 } else
7620 #endif
7622 #if defined(TARGET_HAS_STRUCT_STAT64)
7623 struct target_stat64 *target_st;
7624 #else
7625 struct target_stat *target_st;
7626 #endif
7628 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7629 return -TARGET_EFAULT;
7630 memset(target_st, 0, sizeof(*target_st));
7631 __put_user(host_st->st_dev, &target_st->st_dev);
7632 __put_user(host_st->st_ino, &target_st->st_ino);
7633 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7634 __put_user(host_st->st_ino, &target_st->__st_ino);
7635 #endif
7636 __put_user(host_st->st_mode, &target_st->st_mode);
7637 __put_user(host_st->st_nlink, &target_st->st_nlink);
7638 __put_user(host_st->st_uid, &target_st->st_uid);
7639 __put_user(host_st->st_gid, &target_st->st_gid);
7640 __put_user(host_st->st_rdev, &target_st->st_rdev);
7641 /* XXX: better use of kernel struct */
7642 __put_user(host_st->st_size, &target_st->st_size);
7643 __put_user(host_st->st_blksize, &target_st->st_blksize);
7644 __put_user(host_st->st_blocks, &target_st->st_blocks);
7645 __put_user(host_st->st_atime, &target_st->target_st_atime);
7646 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7647 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
7648 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7649 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7650 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7651 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7652 #endif
7653 unlock_user_struct(target_st, target_addr, 1);
7656 return 0;
7658 #endif
7660 #if defined(TARGET_NR_statx) && defined(__NR_statx)
7661 static inline abi_long host_to_target_statx(struct target_statx *host_stx,
7662 abi_ulong target_addr)
7664 struct target_statx *target_stx;
7666 if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
7667 return -TARGET_EFAULT;
7669 memset(target_stx, 0, sizeof(*target_stx));
7671 __put_user(host_stx->stx_mask, &target_stx->stx_mask);
7672 __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
7673 __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
7674 __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
7675 __put_user(host_stx->stx_uid, &target_stx->stx_uid);
7676 __put_user(host_stx->stx_gid, &target_stx->stx_gid);
7677 __put_user(host_stx->stx_mode, &target_stx->stx_mode);
7678 __put_user(host_stx->stx_ino, &target_stx->stx_ino);
7679 __put_user(host_stx->stx_size, &target_stx->stx_size);
7680 __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
7681 __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
7682 __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
7683 __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
7684 __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
7685 __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
7686 __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
7687 __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
7688 __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
7689 __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
7690 __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
7691 __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
7692 __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
7693 __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
7695 unlock_user_struct(target_stx, target_addr, 1);
7697 return 0;
7699 #endif
7701 static int do_sys_futex(int *uaddr, int op, int val,
7702 const struct timespec *timeout, int *uaddr2,
7703 int val3)
7705 #if HOST_LONG_BITS == 64
7706 #if defined(__NR_futex)
7707 /* always a 64-bit time_t, it doesn't define _time64 version */
7708 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7710 #endif
7711 #else /* HOST_LONG_BITS == 64 */
7712 #if defined(__NR_futex_time64)
7713 if (sizeof(timeout->tv_sec) == 8) {
7714 /* _time64 function on 32bit arch */
7715 return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
7717 #endif
7718 #if defined(__NR_futex)
7719 /* old function on 32bit arch */
7720 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7721 #endif
7722 #endif /* HOST_LONG_BITS == 64 */
7723 g_assert_not_reached();
7726 static int do_safe_futex(int *uaddr, int op, int val,
7727 const struct timespec *timeout, int *uaddr2,
7728 int val3)
7730 #if HOST_LONG_BITS == 64
7731 #if defined(__NR_futex)
7732 /* always a 64-bit time_t, it doesn't define _time64 version */
7733 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7734 #endif
7735 #else /* HOST_LONG_BITS == 64 */
7736 #if defined(__NR_futex_time64)
7737 if (sizeof(timeout->tv_sec) == 8) {
7738 /* _time64 function on 32bit arch */
7739 return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
7740 val3));
7742 #endif
7743 #if defined(__NR_futex)
7744 /* old function on 32bit arch */
7745 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7746 #endif
7747 #endif /* HOST_LONG_BITS == 64 */
7748 return -TARGET_ENOSYS;
7751 /* ??? Using host futex calls even when target atomic operations
7752 are not really atomic probably breaks things. However implementing
7753 futexes locally would make futexes shared between multiple processes
7754 tricky. However they're probably useless because guest atomic
7755 operations won't work either. */
7756 #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7757 static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
7758 int op, int val, target_ulong timeout,
7759 target_ulong uaddr2, int val3)
7761 struct timespec ts, *pts = NULL;
7762 void *haddr2 = NULL;
7763 int base_op;
7765 /* We assume FUTEX_* constants are the same on both host and target. */
7766 #ifdef FUTEX_CMD_MASK
7767 base_op = op & FUTEX_CMD_MASK;
7768 #else
7769 base_op = op;
7770 #endif
7771 switch (base_op) {
7772 case FUTEX_WAIT:
7773 case FUTEX_WAIT_BITSET:
7774 val = tswap32(val);
7775 break;
7776 case FUTEX_WAIT_REQUEUE_PI:
7777 val = tswap32(val);
7778 haddr2 = g2h(cpu, uaddr2);
7779 break;
7780 case FUTEX_LOCK_PI:
7781 case FUTEX_LOCK_PI2:
7782 break;
7783 case FUTEX_WAKE:
7784 case FUTEX_WAKE_BITSET:
7785 case FUTEX_TRYLOCK_PI:
7786 case FUTEX_UNLOCK_PI:
7787 timeout = 0;
7788 break;
7789 case FUTEX_FD:
7790 val = target_to_host_signal(val);
7791 timeout = 0;
7792 break;
7793 case FUTEX_CMP_REQUEUE:
7794 case FUTEX_CMP_REQUEUE_PI:
7795 val3 = tswap32(val3);
7796 /* fall through */
7797 case FUTEX_REQUEUE:
7798 case FUTEX_WAKE_OP:
7800 * For these, the 4th argument is not TIMEOUT, but VAL2.
7801 * But the prototype of do_safe_futex takes a pointer, so
7802 * insert casts to satisfy the compiler. We do not need
7803 * to tswap VAL2 since it's not compared to guest memory.
7805 pts = (struct timespec *)(uintptr_t)timeout;
7806 timeout = 0;
7807 haddr2 = g2h(cpu, uaddr2);
7808 break;
7809 default:
7810 return -TARGET_ENOSYS;
7812 if (timeout) {
7813 pts = &ts;
7814 if (time64
7815 ? target_to_host_timespec64(pts, timeout)
7816 : target_to_host_timespec(pts, timeout)) {
7817 return -TARGET_EFAULT;
7820 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
7822 #endif
7824 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7825 static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7826 abi_long handle, abi_long mount_id,
7827 abi_long flags)
7829 struct file_handle *target_fh;
7830 struct file_handle *fh;
7831 int mid = 0;
7832 abi_long ret;
7833 char *name;
7834 unsigned int size, total_size;
7836 if (get_user_s32(size, handle)) {
7837 return -TARGET_EFAULT;
7840 name = lock_user_string(pathname);
7841 if (!name) {
7842 return -TARGET_EFAULT;
7845 total_size = sizeof(struct file_handle) + size;
7846 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7847 if (!target_fh) {
7848 unlock_user(name, pathname, 0);
7849 return -TARGET_EFAULT;
7852 fh = g_malloc0(total_size);
7853 fh->handle_bytes = size;
7855 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7856 unlock_user(name, pathname, 0);
7858 /* man name_to_handle_at(2):
7859 * Other than the use of the handle_bytes field, the caller should treat
7860 * the file_handle structure as an opaque data type
7863 memcpy(target_fh, fh, total_size);
7864 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7865 target_fh->handle_type = tswap32(fh->handle_type);
7866 g_free(fh);
7867 unlock_user(target_fh, handle, total_size);
7869 if (put_user_s32(mid, mount_id)) {
7870 return -TARGET_EFAULT;
7873 return ret;
7876 #endif
7878 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7879 static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7880 abi_long flags)
7882 struct file_handle *target_fh;
7883 struct file_handle *fh;
7884 unsigned int size, total_size;
7885 abi_long ret;
7887 if (get_user_s32(size, handle)) {
7888 return -TARGET_EFAULT;
7891 total_size = sizeof(struct file_handle) + size;
7892 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7893 if (!target_fh) {
7894 return -TARGET_EFAULT;
7897 fh = g_memdup(target_fh, total_size);
7898 fh->handle_bytes = size;
7899 fh->handle_type = tswap32(target_fh->handle_type);
7901 ret = get_errno(open_by_handle_at(mount_fd, fh,
7902 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7904 g_free(fh);
7906 unlock_user(target_fh, handle, total_size);
7908 return ret;
7910 #endif
7912 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7914 static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7916 int host_flags;
7917 target_sigset_t *target_mask;
7918 sigset_t host_mask;
7919 abi_long ret;
7921 if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
7922 return -TARGET_EINVAL;
7924 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7925 return -TARGET_EFAULT;
7928 target_to_host_sigset(&host_mask, target_mask);
7930 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7932 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7933 if (ret >= 0) {
7934 fd_trans_register(ret, &target_signalfd_trans);
7937 unlock_user_struct(target_mask, mask, 0);
7939 return ret;
7941 #endif
7943 /* Map host to target signal numbers for the wait family of syscalls.
7944 Assume all other status bits are the same. */
7945 int host_to_target_waitstatus(int status)
7947 if (WIFSIGNALED(status)) {
7948 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
7950 if (WIFSTOPPED(status)) {
7951 return (host_to_target_signal(WSTOPSIG(status)) << 8)
7952 | (status & 0xff);
7954 return status;
7957 static int open_self_cmdline(CPUArchState *cpu_env, int fd)
7959 CPUState *cpu = env_cpu(cpu_env);
7960 struct linux_binprm *bprm = get_task_state(cpu)->bprm;
7961 int i;
7963 for (i = 0; i < bprm->argc; i++) {
7964 size_t len = strlen(bprm->argv[i]) + 1;
7966 if (write(fd, bprm->argv[i], len) != len) {
7967 return -1;
7971 return 0;
7974 struct open_self_maps_data {
7975 TaskState *ts;
7976 IntervalTreeRoot *host_maps;
7977 int fd;
7978 bool smaps;
7982 * Subroutine to output one line of /proc/self/maps,
7983 * or one region of /proc/self/smaps.
7986 #ifdef TARGET_HPPA
7987 # define test_stack(S, E, L) (E == L)
7988 #else
7989 # define test_stack(S, E, L) (S == L)
7990 #endif
7992 static void open_self_maps_4(const struct open_self_maps_data *d,
7993 const MapInfo *mi, abi_ptr start,
7994 abi_ptr end, unsigned flags)
7996 const struct image_info *info = d->ts->info;
7997 const char *path = mi->path;
7998 uint64_t offset;
7999 int fd = d->fd;
8000 int count;
8002 if (test_stack(start, end, info->stack_limit)) {
8003 path = "[stack]";
8004 } else if (start == info->brk) {
8005 path = "[heap]";
8006 } else if (start == info->vdso) {
8007 path = "[vdso]";
8008 #ifdef TARGET_X86_64
8009 } else if (start == TARGET_VSYSCALL_PAGE) {
8010 path = "[vsyscall]";
8011 #endif
8014 /* Except null device (MAP_ANON), adjust offset for this fragment. */
8015 offset = mi->offset;
8016 if (mi->dev) {
8017 uintptr_t hstart = (uintptr_t)g2h_untagged(start);
8018 offset += hstart - mi->itree.start;
8021 count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
8022 " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64,
8023 start, end,
8024 (flags & PAGE_READ) ? 'r' : '-',
8025 (flags & PAGE_WRITE_ORG) ? 'w' : '-',
8026 (flags & PAGE_EXEC) ? 'x' : '-',
8027 mi->is_priv ? 'p' : 's',
8028 offset, major(mi->dev), minor(mi->dev),
8029 (uint64_t)mi->inode);
8030 if (path) {
8031 dprintf(fd, "%*s%s\n", 73 - count, "", path);
8032 } else {
8033 dprintf(fd, "\n");
8036 if (d->smaps) {
8037 unsigned long size = end - start;
8038 unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
8039 unsigned long size_kb = size >> 10;
8041 dprintf(fd, "Size: %lu kB\n"
8042 "KernelPageSize: %lu kB\n"
8043 "MMUPageSize: %lu kB\n"
8044 "Rss: 0 kB\n"
8045 "Pss: 0 kB\n"
8046 "Pss_Dirty: 0 kB\n"
8047 "Shared_Clean: 0 kB\n"
8048 "Shared_Dirty: 0 kB\n"
8049 "Private_Clean: 0 kB\n"
8050 "Private_Dirty: 0 kB\n"
8051 "Referenced: 0 kB\n"
8052 "Anonymous: %lu kB\n"
8053 "LazyFree: 0 kB\n"
8054 "AnonHugePages: 0 kB\n"
8055 "ShmemPmdMapped: 0 kB\n"
8056 "FilePmdMapped: 0 kB\n"
8057 "Shared_Hugetlb: 0 kB\n"
8058 "Private_Hugetlb: 0 kB\n"
8059 "Swap: 0 kB\n"
8060 "SwapPss: 0 kB\n"
8061 "Locked: 0 kB\n"
8062 "THPeligible: 0\n"
8063 "VmFlags:%s%s%s%s%s%s%s%s\n",
8064 size_kb, page_size_kb, page_size_kb,
8065 (flags & PAGE_ANON ? size_kb : 0),
8066 (flags & PAGE_READ) ? " rd" : "",
8067 (flags & PAGE_WRITE_ORG) ? " wr" : "",
8068 (flags & PAGE_EXEC) ? " ex" : "",
8069 mi->is_priv ? "" : " sh",
8070 (flags & PAGE_READ) ? " mr" : "",
8071 (flags & PAGE_WRITE_ORG) ? " mw" : "",
8072 (flags & PAGE_EXEC) ? " me" : "",
8073 mi->is_priv ? "" : " ms");
8078 * Callback for walk_memory_regions, when read_self_maps() fails.
8079 * Proceed without the benefit of host /proc/self/maps cross-check.
8081 static int open_self_maps_3(void *opaque, target_ulong guest_start,
8082 target_ulong guest_end, unsigned long flags)
8084 static const MapInfo mi = { .is_priv = true };
8086 open_self_maps_4(opaque, &mi, guest_start, guest_end, flags);
8087 return 0;
8091 * Callback for walk_memory_regions, when read_self_maps() succeeds.
8093 static int open_self_maps_2(void *opaque, target_ulong guest_start,
8094 target_ulong guest_end, unsigned long flags)
8096 const struct open_self_maps_data *d = opaque;
8097 uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start);
8098 uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1);
8100 #ifdef TARGET_X86_64
8102 * Because of the extremely high position of the page within the guest
8103 * virtual address space, this is not backed by host memory at all.
8104 * Therefore the loop below would fail. This is the only instance
8105 * of not having host backing memory.
8107 if (guest_start == TARGET_VSYSCALL_PAGE) {
8108 return open_self_maps_3(opaque, guest_start, guest_end, flags);
8110 #endif
8112 while (1) {
8113 IntervalTreeNode *n =
8114 interval_tree_iter_first(d->host_maps, host_start, host_start);
8115 MapInfo *mi = container_of(n, MapInfo, itree);
8116 uintptr_t this_hlast = MIN(host_last, n->last);
8117 target_ulong this_gend = h2g(this_hlast) + 1;
8119 open_self_maps_4(d, mi, guest_start, this_gend, flags);
8121 if (this_hlast == host_last) {
8122 return 0;
8124 host_start = this_hlast + 1;
8125 guest_start = h2g(host_start);
8129 static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
8131 struct open_self_maps_data d = {
8132 .ts = env_cpu(env)->opaque,
8133 .host_maps = read_self_maps(),
8134 .fd = fd,
8135 .smaps = smaps
8138 if (d.host_maps) {
8139 walk_memory_regions(&d, open_self_maps_2);
8140 free_self_maps(d.host_maps);
8141 } else {
8142 walk_memory_regions(&d, open_self_maps_3);
8144 return 0;
8147 static int open_self_maps(CPUArchState *cpu_env, int fd)
8149 return open_self_maps_1(cpu_env, fd, false);
8152 static int open_self_smaps(CPUArchState *cpu_env, int fd)
8154 return open_self_maps_1(cpu_env, fd, true);
8157 static int open_self_stat(CPUArchState *cpu_env, int fd)
8159 CPUState *cpu = env_cpu(cpu_env);
8160 TaskState *ts = get_task_state(cpu);
8161 g_autoptr(GString) buf = g_string_new(NULL);
8162 int i;
8164 for (i = 0; i < 44; i++) {
8165 if (i == 0) {
8166 /* pid */
8167 g_string_printf(buf, FMT_pid " ", getpid());
8168 } else if (i == 1) {
8169 /* app name */
8170 gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
8171 bin = bin ? bin + 1 : ts->bprm->argv[0];
8172 g_string_printf(buf, "(%.15s) ", bin);
8173 } else if (i == 2) {
8174 /* task state */
8175 g_string_assign(buf, "R "); /* we are running right now */
8176 } else if (i == 3) {
8177 /* ppid */
8178 g_string_printf(buf, FMT_pid " ", getppid());
8179 } else if (i == 21) {
8180 /* starttime */
8181 g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
8182 } else if (i == 27) {
8183 /* stack bottom */
8184 g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
8185 } else {
8186 /* for the rest, there is MasterCard */
8187 g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
8190 if (write(fd, buf->str, buf->len) != buf->len) {
8191 return -1;
8195 return 0;
8198 static int open_self_auxv(CPUArchState *cpu_env, int fd)
8200 CPUState *cpu = env_cpu(cpu_env);
8201 TaskState *ts = get_task_state(cpu);
8202 abi_ulong auxv = ts->info->saved_auxv;
8203 abi_ulong len = ts->info->auxv_len;
8204 char *ptr;
8207 * Auxiliary vector is stored in target process stack.
8208 * read in whole auxv vector and copy it to file
8210 ptr = lock_user(VERIFY_READ, auxv, len, 0);
8211 if (ptr != NULL) {
8212 while (len > 0) {
8213 ssize_t r;
8214 r = write(fd, ptr, len);
8215 if (r <= 0) {
8216 break;
8218 len -= r;
8219 ptr += r;
8221 lseek(fd, 0, SEEK_SET);
8222 unlock_user(ptr, auxv, len);
8225 return 0;
8228 static int is_proc_myself(const char *filename, const char *entry)
8230 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
8231 filename += strlen("/proc/");
8232 if (!strncmp(filename, "self/", strlen("self/"))) {
8233 filename += strlen("self/");
8234 } else if (*filename >= '1' && *filename <= '9') {
8235 char myself[80];
8236 snprintf(myself, sizeof(myself), "%d/", getpid());
8237 if (!strncmp(filename, myself, strlen(myself))) {
8238 filename += strlen(myself);
8239 } else {
8240 return 0;
8242 } else {
8243 return 0;
8245 if (!strcmp(filename, entry)) {
8246 return 1;
8249 return 0;
8252 static void excp_dump_file(FILE *logfile, CPUArchState *env,
8253 const char *fmt, int code)
8255 if (logfile) {
8256 CPUState *cs = env_cpu(env);
8258 fprintf(logfile, fmt, code);
8259 fprintf(logfile, "Failing executable: %s\n", exec_path);
8260 cpu_dump_state(cs, logfile, 0);
8261 open_self_maps(env, fileno(logfile));
8265 void target_exception_dump(CPUArchState *env, const char *fmt, int code)
8267 /* dump to console */
8268 excp_dump_file(stderr, env, fmt, code);
8270 /* dump to log file */
8271 if (qemu_log_separate()) {
8272 FILE *logfile = qemu_log_trylock();
8274 excp_dump_file(logfile, env, fmt, code);
8275 qemu_log_unlock(logfile);
8279 #include "target_proc.h"
8281 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
8282 defined(HAVE_ARCH_PROC_CPUINFO) || \
8283 defined(HAVE_ARCH_PROC_HARDWARE)
8284 static int is_proc(const char *filename, const char *entry)
8286 return strcmp(filename, entry) == 0;
8288 #endif
8290 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8291 static int open_net_route(CPUArchState *cpu_env, int fd)
8293 FILE *fp;
8294 char *line = NULL;
8295 size_t len = 0;
8296 ssize_t read;
8298 fp = fopen("/proc/net/route", "r");
8299 if (fp == NULL) {
8300 return -1;
8303 /* read header */
8305 read = getline(&line, &len, fp);
8306 dprintf(fd, "%s", line);
8308 /* read routes */
8310 while ((read = getline(&line, &len, fp)) != -1) {
8311 char iface[16];
8312 uint32_t dest, gw, mask;
8313 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
8314 int fields;
8316 fields = sscanf(line,
8317 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8318 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
8319 &mask, &mtu, &window, &irtt);
8320 if (fields != 11) {
8321 continue;
8323 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8324 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
8325 metric, tswap32(mask), mtu, window, irtt);
8328 free(line);
8329 fclose(fp);
8331 return 0;
8333 #endif
8335 int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
8336 int flags, mode_t mode, bool safe)
8338 g_autofree char *proc_name = NULL;
8339 const char *pathname;
8340 struct fake_open {
8341 const char *filename;
8342 int (*fill)(CPUArchState *cpu_env, int fd);
8343 int (*cmp)(const char *s1, const char *s2);
8345 const struct fake_open *fake_open;
8346 static const struct fake_open fakes[] = {
8347 { "maps", open_self_maps, is_proc_myself },
8348 { "smaps", open_self_smaps, is_proc_myself },
8349 { "stat", open_self_stat, is_proc_myself },
8350 { "auxv", open_self_auxv, is_proc_myself },
8351 { "cmdline", open_self_cmdline, is_proc_myself },
8352 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8353 { "/proc/net/route", open_net_route, is_proc },
8354 #endif
8355 #if defined(HAVE_ARCH_PROC_CPUINFO)
8356 { "/proc/cpuinfo", open_cpuinfo, is_proc },
8357 #endif
8358 #if defined(HAVE_ARCH_PROC_HARDWARE)
8359 { "/proc/hardware", open_hardware, is_proc },
8360 #endif
8361 { NULL, NULL, NULL }
8364 /* if this is a file from /proc/ filesystem, expand full name */
8365 proc_name = realpath(fname, NULL);
8366 if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
8367 pathname = proc_name;
8368 } else {
8369 pathname = fname;
8372 if (is_proc_myself(pathname, "exe")) {
8373 if (safe) {
8374 return safe_openat(dirfd, exec_path, flags, mode);
8375 } else {
8376 return openat(dirfd, exec_path, flags, mode);
8380 for (fake_open = fakes; fake_open->filename; fake_open++) {
8381 if (fake_open->cmp(pathname, fake_open->filename)) {
8382 break;
8386 if (fake_open->filename) {
8387 const char *tmpdir;
8388 char filename[PATH_MAX];
8389 int fd, r;
8391 fd = memfd_create("qemu-open", 0);
8392 if (fd < 0) {
8393 if (errno != ENOSYS) {
8394 return fd;
8396 /* create temporary file to map stat to */
8397 tmpdir = getenv("TMPDIR");
8398 if (!tmpdir)
8399 tmpdir = "/tmp";
8400 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
8401 fd = mkstemp(filename);
8402 if (fd < 0) {
8403 return fd;
8405 unlink(filename);
8408 if ((r = fake_open->fill(cpu_env, fd))) {
8409 int e = errno;
8410 close(fd);
8411 errno = e;
8412 return r;
8414 lseek(fd, 0, SEEK_SET);
8416 return fd;
8419 if (safe) {
8420 return safe_openat(dirfd, path(pathname), flags, mode);
8421 } else {
8422 return openat(dirfd, path(pathname), flags, mode);
8426 ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
8428 ssize_t ret;
8430 if (!pathname || !buf) {
8431 errno = EFAULT;
8432 return -1;
8435 if (!bufsiz) {
8436 /* Short circuit this for the magic exe check. */
8437 errno = EINVAL;
8438 return -1;
8441 if (is_proc_myself((const char *)pathname, "exe")) {
8443 * Don't worry about sign mismatch as earlier mapping
8444 * logic would have thrown a bad address error.
8446 ret = MIN(strlen(exec_path), bufsiz);
8447 /* We cannot NUL terminate the string. */
8448 memcpy(buf, exec_path, ret);
8449 } else {
8450 ret = readlink(path(pathname), buf, bufsiz);
8453 return ret;
8456 static int do_execv(CPUArchState *cpu_env, int dirfd,
8457 abi_long pathname, abi_long guest_argp,
8458 abi_long guest_envp, int flags, bool is_execveat)
8460 int ret;
8461 char **argp, **envp;
8462 int argc, envc;
8463 abi_ulong gp;
8464 abi_ulong addr;
8465 char **q;
8466 void *p;
8468 argc = 0;
8470 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
8471 if (get_user_ual(addr, gp)) {
8472 return -TARGET_EFAULT;
8474 if (!addr) {
8475 break;
8477 argc++;
8479 envc = 0;
8480 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
8481 if (get_user_ual(addr, gp)) {
8482 return -TARGET_EFAULT;
8484 if (!addr) {
8485 break;
8487 envc++;
8490 argp = g_new0(char *, argc + 1);
8491 envp = g_new0(char *, envc + 1);
8493 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
8494 if (get_user_ual(addr, gp)) {
8495 goto execve_efault;
8497 if (!addr) {
8498 break;
8500 *q = lock_user_string(addr);
8501 if (!*q) {
8502 goto execve_efault;
8505 *q = NULL;
8507 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
8508 if (get_user_ual(addr, gp)) {
8509 goto execve_efault;
8511 if (!addr) {
8512 break;
8514 *q = lock_user_string(addr);
8515 if (!*q) {
8516 goto execve_efault;
8519 *q = NULL;
8522 * Although execve() is not an interruptible syscall it is
8523 * a special case where we must use the safe_syscall wrapper:
8524 * if we allow a signal to happen before we make the host
8525 * syscall then we will 'lose' it, because at the point of
8526 * execve the process leaves QEMU's control. So we use the
8527 * safe syscall wrapper to ensure that we either take the
8528 * signal as a guest signal, or else it does not happen
8529 * before the execve completes and makes it the other
8530 * program's problem.
8532 p = lock_user_string(pathname);
8533 if (!p) {
8534 goto execve_efault;
8537 const char *exe = p;
8538 if (is_proc_myself(p, "exe")) {
8539 exe = exec_path;
8541 ret = is_execveat
8542 ? safe_execveat(dirfd, exe, argp, envp, flags)
8543 : safe_execve(exe, argp, envp);
8544 ret = get_errno(ret);
8546 unlock_user(p, pathname, 0);
8548 goto execve_end;
8550 execve_efault:
8551 ret = -TARGET_EFAULT;
8553 execve_end:
8554 for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
8555 if (get_user_ual(addr, gp) || !addr) {
8556 break;
8558 unlock_user(*q, addr, 0);
8560 for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
8561 if (get_user_ual(addr, gp) || !addr) {
8562 break;
8564 unlock_user(*q, addr, 0);
8567 g_free(argp);
8568 g_free(envp);
8569 return ret;
8572 #define TIMER_MAGIC 0x0caf0000
8573 #define TIMER_MAGIC_MASK 0xffff0000
8575 /* Convert QEMU provided timer ID back to internal 16bit index format */
8576 static target_timer_t get_timer_id(abi_long arg)
8578 target_timer_t timerid = arg;
8580 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
8581 return -TARGET_EINVAL;
8584 timerid &= 0xffff;
8586 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
8587 return -TARGET_EINVAL;
8590 return timerid;
8593 static int target_to_host_cpu_mask(unsigned long *host_mask,
8594 size_t host_size,
8595 abi_ulong target_addr,
8596 size_t target_size)
8598 unsigned target_bits = sizeof(abi_ulong) * 8;
8599 unsigned host_bits = sizeof(*host_mask) * 8;
8600 abi_ulong *target_mask;
8601 unsigned i, j;
8603 assert(host_size >= target_size);
8605 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
8606 if (!target_mask) {
8607 return -TARGET_EFAULT;
8609 memset(host_mask, 0, host_size);
8611 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8612 unsigned bit = i * target_bits;
8613 abi_ulong val;
8615 __get_user(val, &target_mask[i]);
8616 for (j = 0; j < target_bits; j++, bit++) {
8617 if (val & (1UL << j)) {
8618 host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
8623 unlock_user(target_mask, target_addr, 0);
8624 return 0;
8627 static int host_to_target_cpu_mask(const unsigned long *host_mask,
8628 size_t host_size,
8629 abi_ulong target_addr,
8630 size_t target_size)
8632 unsigned target_bits = sizeof(abi_ulong) * 8;
8633 unsigned host_bits = sizeof(*host_mask) * 8;
8634 abi_ulong *target_mask;
8635 unsigned i, j;
8637 assert(host_size >= target_size);
8639 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
8640 if (!target_mask) {
8641 return -TARGET_EFAULT;
8644 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8645 unsigned bit = i * target_bits;
8646 abi_ulong val = 0;
8648 for (j = 0; j < target_bits; j++, bit++) {
8649 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
8650 val |= 1UL << j;
8653 __put_user(val, &target_mask[i]);
8656 unlock_user(target_mask, target_addr, target_size);
8657 return 0;
8660 #ifdef TARGET_NR_getdents
8661 static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
8663 g_autofree void *hdirp = NULL;
8664 void *tdirp;
8665 int hlen, hoff, toff;
8666 int hreclen, treclen;
8667 off64_t prev_diroff = 0;
8669 hdirp = g_try_malloc(count);
8670 if (!hdirp) {
8671 return -TARGET_ENOMEM;
8674 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8675 hlen = sys_getdents(dirfd, hdirp, count);
8676 #else
8677 hlen = sys_getdents64(dirfd, hdirp, count);
8678 #endif
8680 hlen = get_errno(hlen);
8681 if (is_error(hlen)) {
8682 return hlen;
8685 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8686 if (!tdirp) {
8687 return -TARGET_EFAULT;
8690 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8691 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8692 struct linux_dirent *hde = hdirp + hoff;
8693 #else
8694 struct linux_dirent64 *hde = hdirp + hoff;
8695 #endif
8696 struct target_dirent *tde = tdirp + toff;
8697 int namelen;
8698 uint8_t type;
8700 namelen = strlen(hde->d_name);
8701 hreclen = hde->d_reclen;
8702 treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
8703 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
8705 if (toff + treclen > count) {
8707 * If the host struct is smaller than the target struct, or
8708 * requires less alignment and thus packs into less space,
8709 * then the host can return more entries than we can pass
8710 * on to the guest.
8712 if (toff == 0) {
8713 toff = -TARGET_EINVAL; /* result buffer is too small */
8714 break;
8717 * Return what we have, resetting the file pointer to the
8718 * location of the first record not returned.
8720 lseek64(dirfd, prev_diroff, SEEK_SET);
8721 break;
8724 prev_diroff = hde->d_off;
8725 tde->d_ino = tswapal(hde->d_ino);
8726 tde->d_off = tswapal(hde->d_off);
8727 tde->d_reclen = tswap16(treclen);
8728 memcpy(tde->d_name, hde->d_name, namelen + 1);
8731 * The getdents type is in what was formerly a padding byte at the
8732 * end of the structure.
8734 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8735 type = *((uint8_t *)hde + hreclen - 1);
8736 #else
8737 type = hde->d_type;
8738 #endif
8739 *((uint8_t *)tde + treclen - 1) = type;
8742 unlock_user(tdirp, arg2, toff);
8743 return toff;
8745 #endif /* TARGET_NR_getdents */
8747 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
8748 static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
8750 g_autofree void *hdirp = NULL;
8751 void *tdirp;
8752 int hlen, hoff, toff;
8753 int hreclen, treclen;
8754 off64_t prev_diroff = 0;
8756 hdirp = g_try_malloc(count);
8757 if (!hdirp) {
8758 return -TARGET_ENOMEM;
8761 hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
8762 if (is_error(hlen)) {
8763 return hlen;
8766 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8767 if (!tdirp) {
8768 return -TARGET_EFAULT;
8771 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8772 struct linux_dirent64 *hde = hdirp + hoff;
8773 struct target_dirent64 *tde = tdirp + toff;
8774 int namelen;
8776 namelen = strlen(hde->d_name) + 1;
8777 hreclen = hde->d_reclen;
8778 treclen = offsetof(struct target_dirent64, d_name) + namelen;
8779 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
8781 if (toff + treclen > count) {
8783 * If the host struct is smaller than the target struct, or
8784 * requires less alignment and thus packs into less space,
8785 * then the host can return more entries than we can pass
8786 * on to the guest.
8788 if (toff == 0) {
8789 toff = -TARGET_EINVAL; /* result buffer is too small */
8790 break;
8793 * Return what we have, resetting the file pointer to the
8794 * location of the first record not returned.
8796 lseek64(dirfd, prev_diroff, SEEK_SET);
8797 break;
8800 prev_diroff = hde->d_off;
8801 tde->d_ino = tswap64(hde->d_ino);
8802 tde->d_off = tswap64(hde->d_off);
8803 tde->d_reclen = tswap16(treclen);
8804 tde->d_type = hde->d_type;
8805 memcpy(tde->d_name, hde->d_name, namelen);
8808 unlock_user(tdirp, arg2, toff);
8809 return toff;
8811 #endif /* TARGET_NR_getdents64 */
8813 #if defined(TARGET_NR_riscv_hwprobe)
8815 #define RISCV_HWPROBE_KEY_MVENDORID 0
8816 #define RISCV_HWPROBE_KEY_MARCHID 1
8817 #define RISCV_HWPROBE_KEY_MIMPID 2
8819 #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
8820 #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
8822 #define RISCV_HWPROBE_KEY_IMA_EXT_0 4
8823 #define RISCV_HWPROBE_IMA_FD (1 << 0)
8824 #define RISCV_HWPROBE_IMA_C (1 << 1)
8825 #define RISCV_HWPROBE_IMA_V (1 << 2)
8826 #define RISCV_HWPROBE_EXT_ZBA (1 << 3)
8827 #define RISCV_HWPROBE_EXT_ZBB (1 << 4)
8828 #define RISCV_HWPROBE_EXT_ZBS (1 << 5)
8829 #define RISCV_HWPROBE_EXT_ZICBOZ (1 << 6)
8830 #define RISCV_HWPROBE_EXT_ZBC (1 << 7)
8831 #define RISCV_HWPROBE_EXT_ZBKB (1 << 8)
8832 #define RISCV_HWPROBE_EXT_ZBKC (1 << 9)
8833 #define RISCV_HWPROBE_EXT_ZBKX (1 << 10)
8834 #define RISCV_HWPROBE_EXT_ZKND (1 << 11)
8835 #define RISCV_HWPROBE_EXT_ZKNE (1 << 12)
8836 #define RISCV_HWPROBE_EXT_ZKNH (1 << 13)
8837 #define RISCV_HWPROBE_EXT_ZKSED (1 << 14)
8838 #define RISCV_HWPROBE_EXT_ZKSH (1 << 15)
8839 #define RISCV_HWPROBE_EXT_ZKT (1 << 16)
8840 #define RISCV_HWPROBE_EXT_ZVBB (1 << 17)
8841 #define RISCV_HWPROBE_EXT_ZVBC (1 << 18)
8842 #define RISCV_HWPROBE_EXT_ZVKB (1 << 19)
8843 #define RISCV_HWPROBE_EXT_ZVKG (1 << 20)
8844 #define RISCV_HWPROBE_EXT_ZVKNED (1 << 21)
8845 #define RISCV_HWPROBE_EXT_ZVKNHA (1 << 22)
8846 #define RISCV_HWPROBE_EXT_ZVKNHB (1 << 23)
8847 #define RISCV_HWPROBE_EXT_ZVKSED (1 << 24)
8848 #define RISCV_HWPROBE_EXT_ZVKSH (1 << 25)
8849 #define RISCV_HWPROBE_EXT_ZVKT (1 << 26)
8850 #define RISCV_HWPROBE_EXT_ZFH (1 << 27)
8851 #define RISCV_HWPROBE_EXT_ZFHMIN (1 << 28)
8852 #define RISCV_HWPROBE_EXT_ZIHINTNTL (1 << 29)
8853 #define RISCV_HWPROBE_EXT_ZVFH (1 << 30)
8854 #define RISCV_HWPROBE_EXT_ZVFHMIN (1 << 31)
8855 #define RISCV_HWPROBE_EXT_ZFA (1ULL << 32)
8856 #define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33)
8857 #define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34)
8858 #define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35)
8860 #define RISCV_HWPROBE_KEY_CPUPERF_0 5
8861 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
8862 #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
8863 #define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
8864 #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
8865 #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
8866 #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
8868 #define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6
8870 struct riscv_hwprobe {
8871 abi_llong key;
8872 abi_ullong value;
8875 static void risc_hwprobe_fill_pairs(CPURISCVState *env,
8876 struct riscv_hwprobe *pair,
8877 size_t pair_count)
8879 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
8881 for (; pair_count > 0; pair_count--, pair++) {
8882 abi_llong key;
8883 abi_ullong value;
8884 __put_user(0, &pair->value);
8885 __get_user(key, &pair->key);
8886 switch (key) {
8887 case RISCV_HWPROBE_KEY_MVENDORID:
8888 __put_user(cfg->mvendorid, &pair->value);
8889 break;
8890 case RISCV_HWPROBE_KEY_MARCHID:
8891 __put_user(cfg->marchid, &pair->value);
8892 break;
8893 case RISCV_HWPROBE_KEY_MIMPID:
8894 __put_user(cfg->mimpid, &pair->value);
8895 break;
8896 case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
8897 value = riscv_has_ext(env, RVI) &&
8898 riscv_has_ext(env, RVM) &&
8899 riscv_has_ext(env, RVA) ?
8900 RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
8901 __put_user(value, &pair->value);
8902 break;
8903 case RISCV_HWPROBE_KEY_IMA_EXT_0:
8904 value = riscv_has_ext(env, RVF) &&
8905 riscv_has_ext(env, RVD) ?
8906 RISCV_HWPROBE_IMA_FD : 0;
8907 value |= riscv_has_ext(env, RVC) ?
8908 RISCV_HWPROBE_IMA_C : 0;
8909 value |= riscv_has_ext(env, RVV) ?
8910 RISCV_HWPROBE_IMA_V : 0;
8911 value |= cfg->ext_zba ?
8912 RISCV_HWPROBE_EXT_ZBA : 0;
8913 value |= cfg->ext_zbb ?
8914 RISCV_HWPROBE_EXT_ZBB : 0;
8915 value |= cfg->ext_zbs ?
8916 RISCV_HWPROBE_EXT_ZBS : 0;
8917 value |= cfg->ext_zicboz ?
8918 RISCV_HWPROBE_EXT_ZICBOZ : 0;
8919 value |= cfg->ext_zbc ?
8920 RISCV_HWPROBE_EXT_ZBC : 0;
8921 value |= cfg->ext_zbkb ?
8922 RISCV_HWPROBE_EXT_ZBKB : 0;
8923 value |= cfg->ext_zbkc ?
8924 RISCV_HWPROBE_EXT_ZBKC : 0;
8925 value |= cfg->ext_zbkx ?
8926 RISCV_HWPROBE_EXT_ZBKX : 0;
8927 value |= cfg->ext_zknd ?
8928 RISCV_HWPROBE_EXT_ZKND : 0;
8929 value |= cfg->ext_zkne ?
8930 RISCV_HWPROBE_EXT_ZKNE : 0;
8931 value |= cfg->ext_zknh ?
8932 RISCV_HWPROBE_EXT_ZKNH : 0;
8933 value |= cfg->ext_zksed ?
8934 RISCV_HWPROBE_EXT_ZKSED : 0;
8935 value |= cfg->ext_zksh ?
8936 RISCV_HWPROBE_EXT_ZKSH : 0;
8937 value |= cfg->ext_zkt ?
8938 RISCV_HWPROBE_EXT_ZKT : 0;
8939 value |= cfg->ext_zvbb ?
8940 RISCV_HWPROBE_EXT_ZVBB : 0;
8941 value |= cfg->ext_zvbc ?
8942 RISCV_HWPROBE_EXT_ZVBC : 0;
8943 value |= cfg->ext_zvkb ?
8944 RISCV_HWPROBE_EXT_ZVKB : 0;
8945 value |= cfg->ext_zvkg ?
8946 RISCV_HWPROBE_EXT_ZVKG : 0;
8947 value |= cfg->ext_zvkned ?
8948 RISCV_HWPROBE_EXT_ZVKNED : 0;
8949 value |= cfg->ext_zvknha ?
8950 RISCV_HWPROBE_EXT_ZVKNHA : 0;
8951 value |= cfg->ext_zvknhb ?
8952 RISCV_HWPROBE_EXT_ZVKNHB : 0;
8953 value |= cfg->ext_zvksed ?
8954 RISCV_HWPROBE_EXT_ZVKSED : 0;
8955 value |= cfg->ext_zvksh ?
8956 RISCV_HWPROBE_EXT_ZVKSH : 0;
8957 value |= cfg->ext_zvkt ?
8958 RISCV_HWPROBE_EXT_ZVKT : 0;
8959 value |= cfg->ext_zfh ?
8960 RISCV_HWPROBE_EXT_ZFH : 0;
8961 value |= cfg->ext_zfhmin ?
8962 RISCV_HWPROBE_EXT_ZFHMIN : 0;
8963 value |= cfg->ext_zihintntl ?
8964 RISCV_HWPROBE_EXT_ZIHINTNTL : 0;
8965 value |= cfg->ext_zvfh ?
8966 RISCV_HWPROBE_EXT_ZVFH : 0;
8967 value |= cfg->ext_zvfhmin ?
8968 RISCV_HWPROBE_EXT_ZVFHMIN : 0;
8969 value |= cfg->ext_zfa ?
8970 RISCV_HWPROBE_EXT_ZFA : 0;
8971 value |= cfg->ext_ztso ?
8972 RISCV_HWPROBE_EXT_ZTSO : 0;
8973 value |= cfg->ext_zacas ?
8974 RISCV_HWPROBE_EXT_ZACAS : 0;
8975 value |= cfg->ext_zicond ?
8976 RISCV_HWPROBE_EXT_ZICOND : 0;
8977 __put_user(value, &pair->value);
8978 break;
8979 case RISCV_HWPROBE_KEY_CPUPERF_0:
8980 __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
8981 break;
8982 case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:
8983 value = cfg->ext_zicboz ? cfg->cboz_blocksize : 0;
8984 __put_user(value, &pair->value);
8985 break;
8986 default:
8987 __put_user(-1, &pair->key);
8988 break;
8993 static int cpu_set_valid(abi_long arg3, abi_long arg4)
8995 int ret, i, tmp;
8996 size_t host_mask_size, target_mask_size;
8997 unsigned long *host_mask;
9000 * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
9001 * arg3 contains the cpu count.
9003 tmp = (8 * sizeof(abi_ulong));
9004 target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
9005 host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
9006 ~(sizeof(*host_mask) - 1);
9008 host_mask = alloca(host_mask_size);
9010 ret = target_to_host_cpu_mask(host_mask, host_mask_size,
9011 arg4, target_mask_size);
9012 if (ret != 0) {
9013 return ret;
9016 for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
9017 if (host_mask[i] != 0) {
9018 return 0;
9021 return -TARGET_EINVAL;
9024 static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
9025 abi_long arg2, abi_long arg3,
9026 abi_long arg4, abi_long arg5)
9028 int ret;
9029 struct riscv_hwprobe *host_pairs;
9031 /* flags must be 0 */
9032 if (arg5 != 0) {
9033 return -TARGET_EINVAL;
9036 /* check cpu_set */
9037 if (arg3 != 0) {
9038 ret = cpu_set_valid(arg3, arg4);
9039 if (ret != 0) {
9040 return ret;
9042 } else if (arg4 != 0) {
9043 return -TARGET_EINVAL;
9046 /* no pairs */
9047 if (arg2 == 0) {
9048 return 0;
9051 host_pairs = lock_user(VERIFY_WRITE, arg1,
9052 sizeof(*host_pairs) * (size_t)arg2, 0);
9053 if (host_pairs == NULL) {
9054 return -TARGET_EFAULT;
9056 risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
9057 unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
9058 return 0;
9060 #endif /* TARGET_NR_riscv_hwprobe */
9062 #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
9063 _syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
9064 #endif
9066 #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9067 #define __NR_sys_open_tree __NR_open_tree
9068 _syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
9069 unsigned int, __flags)
9070 #endif
9072 #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9073 #define __NR_sys_move_mount __NR_move_mount
9074 _syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
9075 int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
9076 #endif
9078 /* This is an internal helper for do_syscall so that it is easier
9079 * to have a single return point, so that actions, such as logging
9080 * of syscall results, can be performed.
9081 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
9083 static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
9084 abi_long arg2, abi_long arg3, abi_long arg4,
9085 abi_long arg5, abi_long arg6, abi_long arg7,
9086 abi_long arg8)
9088 CPUState *cpu = env_cpu(cpu_env);
9089 abi_long ret;
9090 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
9091 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
9092 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
9093 || defined(TARGET_NR_statx)
9094 struct stat st;
9095 #endif
9096 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
9097 || defined(TARGET_NR_fstatfs)
9098 struct statfs stfs;
9099 #endif
9100 void *p;
9102 switch(num) {
9103 case TARGET_NR_exit:
9104 /* In old applications this may be used to implement _exit(2).
9105 However in threaded applications it is used for thread termination,
9106 and _exit_group is used for application termination.
9107 Do thread termination if we have more then one thread. */
9109 if (block_signals()) {
9110 return -QEMU_ERESTARTSYS;
9113 pthread_mutex_lock(&clone_lock);
9115 if (CPU_NEXT(first_cpu)) {
9116 TaskState *ts = get_task_state(cpu);
9118 if (ts->child_tidptr) {
9119 put_user_u32(0, ts->child_tidptr);
9120 do_sys_futex(g2h(cpu, ts->child_tidptr),
9121 FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
9124 object_unparent(OBJECT(cpu));
9125 object_unref(OBJECT(cpu));
9127 * At this point the CPU should be unrealized and removed
9128 * from cpu lists. We can clean-up the rest of the thread
9129 * data without the lock held.
9132 pthread_mutex_unlock(&clone_lock);
9134 thread_cpu = NULL;
9135 g_free(ts);
9136 rcu_unregister_thread();
9137 pthread_exit(NULL);
9140 pthread_mutex_unlock(&clone_lock);
9141 preexit_cleanup(cpu_env, arg1);
9142 _exit(arg1);
9143 return 0; /* avoid warning */
9144 case TARGET_NR_read:
9145 if (arg2 == 0 && arg3 == 0) {
9146 return get_errno(safe_read(arg1, 0, 0));
9147 } else {
9148 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
9149 return -TARGET_EFAULT;
9150 ret = get_errno(safe_read(arg1, p, arg3));
9151 if (ret >= 0 &&
9152 fd_trans_host_to_target_data(arg1)) {
9153 ret = fd_trans_host_to_target_data(arg1)(p, ret);
9155 unlock_user(p, arg2, ret);
9157 return ret;
9158 case TARGET_NR_write:
9159 if (arg2 == 0 && arg3 == 0) {
9160 return get_errno(safe_write(arg1, 0, 0));
9162 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
9163 return -TARGET_EFAULT;
9164 if (fd_trans_target_to_host_data(arg1)) {
9165 void *copy = g_malloc(arg3);
9166 memcpy(copy, p, arg3);
9167 ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
9168 if (ret >= 0) {
9169 ret = get_errno(safe_write(arg1, copy, ret));
9171 g_free(copy);
9172 } else {
9173 ret = get_errno(safe_write(arg1, p, arg3));
9175 unlock_user(p, arg2, 0);
9176 return ret;
9178 #ifdef TARGET_NR_open
9179 case TARGET_NR_open:
9180 if (!(p = lock_user_string(arg1)))
9181 return -TARGET_EFAULT;
9182 ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
9183 target_to_host_bitmask(arg2, fcntl_flags_tbl),
9184 arg3, true));
9185 fd_trans_unregister(ret);
9186 unlock_user(p, arg1, 0);
9187 return ret;
9188 #endif
9189 case TARGET_NR_openat:
9190 if (!(p = lock_user_string(arg2)))
9191 return -TARGET_EFAULT;
9192 ret = get_errno(do_guest_openat(cpu_env, arg1, p,
9193 target_to_host_bitmask(arg3, fcntl_flags_tbl),
9194 arg4, true));
9195 fd_trans_unregister(ret);
9196 unlock_user(p, arg2, 0);
9197 return ret;
9198 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9199 case TARGET_NR_name_to_handle_at:
9200 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
9201 return ret;
9202 #endif
9203 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9204 case TARGET_NR_open_by_handle_at:
9205 ret = do_open_by_handle_at(arg1, arg2, arg3);
9206 fd_trans_unregister(ret);
9207 return ret;
9208 #endif
9209 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
9210 case TARGET_NR_pidfd_open:
9211 return get_errno(pidfd_open(arg1, arg2));
9212 #endif
9213 #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
9214 case TARGET_NR_pidfd_send_signal:
9216 siginfo_t uinfo, *puinfo;
9218 if (arg3) {
9219 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
9220 if (!p) {
9221 return -TARGET_EFAULT;
9223 target_to_host_siginfo(&uinfo, p);
9224 unlock_user(p, arg3, 0);
9225 puinfo = &uinfo;
9226 } else {
9227 puinfo = NULL;
9229 ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
9230 puinfo, arg4));
9232 return ret;
9233 #endif
9234 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
9235 case TARGET_NR_pidfd_getfd:
9236 return get_errno(pidfd_getfd(arg1, arg2, arg3));
9237 #endif
9238 case TARGET_NR_close:
9239 fd_trans_unregister(arg1);
9240 return get_errno(close(arg1));
9241 #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
9242 case TARGET_NR_close_range:
9243 ret = get_errno(sys_close_range(arg1, arg2, arg3));
9244 if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
9245 abi_long fd, maxfd;
9246 maxfd = MIN(arg2, target_fd_max);
9247 for (fd = arg1; fd < maxfd; fd++) {
9248 fd_trans_unregister(fd);
9251 return ret;
9252 #endif
9254 case TARGET_NR_brk:
9255 return do_brk(arg1);
9256 #ifdef TARGET_NR_fork
9257 case TARGET_NR_fork:
9258 return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
9259 #endif
9260 #ifdef TARGET_NR_waitpid
9261 case TARGET_NR_waitpid:
9263 int status;
9264 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
9265 if (!is_error(ret) && arg2 && ret
9266 && put_user_s32(host_to_target_waitstatus(status), arg2))
9267 return -TARGET_EFAULT;
9269 return ret;
9270 #endif
9271 #ifdef TARGET_NR_waitid
9272 case TARGET_NR_waitid:
9274 struct rusage ru;
9275 siginfo_t info;
9277 ret = get_errno(safe_waitid(arg1, arg2, (arg3 ? &info : NULL),
9278 arg4, (arg5 ? &ru : NULL)));
9279 if (!is_error(ret)) {
9280 if (arg3) {
9281 p = lock_user(VERIFY_WRITE, arg3,
9282 sizeof(target_siginfo_t), 0);
9283 if (!p) {
9284 return -TARGET_EFAULT;
9286 host_to_target_siginfo(p, &info);
9287 unlock_user(p, arg3, sizeof(target_siginfo_t));
9289 if (arg5 && host_to_target_rusage(arg5, &ru)) {
9290 return -TARGET_EFAULT;
9294 return ret;
9295 #endif
9296 #ifdef TARGET_NR_creat /* not on alpha */
9297 case TARGET_NR_creat:
9298 if (!(p = lock_user_string(arg1)))
9299 return -TARGET_EFAULT;
9300 ret = get_errno(creat(p, arg2));
9301 fd_trans_unregister(ret);
9302 unlock_user(p, arg1, 0);
9303 return ret;
9304 #endif
9305 #ifdef TARGET_NR_link
9306 case TARGET_NR_link:
9308 void * p2;
9309 p = lock_user_string(arg1);
9310 p2 = lock_user_string(arg2);
9311 if (!p || !p2)
9312 ret = -TARGET_EFAULT;
9313 else
9314 ret = get_errno(link(p, p2));
9315 unlock_user(p2, arg2, 0);
9316 unlock_user(p, arg1, 0);
9318 return ret;
9319 #endif
9320 #if defined(TARGET_NR_linkat)
9321 case TARGET_NR_linkat:
9323 void * p2 = NULL;
9324 if (!arg2 || !arg4)
9325 return -TARGET_EFAULT;
9326 p = lock_user_string(arg2);
9327 p2 = lock_user_string(arg4);
9328 if (!p || !p2)
9329 ret = -TARGET_EFAULT;
9330 else
9331 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
9332 unlock_user(p, arg2, 0);
9333 unlock_user(p2, arg4, 0);
9335 return ret;
9336 #endif
9337 #ifdef TARGET_NR_unlink
9338 case TARGET_NR_unlink:
9339 if (!(p = lock_user_string(arg1)))
9340 return -TARGET_EFAULT;
9341 ret = get_errno(unlink(p));
9342 unlock_user(p, arg1, 0);
9343 return ret;
9344 #endif
9345 #if defined(TARGET_NR_unlinkat)
9346 case TARGET_NR_unlinkat:
9347 if (!(p = lock_user_string(arg2)))
9348 return -TARGET_EFAULT;
9349 ret = get_errno(unlinkat(arg1, p, arg3));
9350 unlock_user(p, arg2, 0);
9351 return ret;
9352 #endif
9353 case TARGET_NR_execveat:
9354 return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
9355 case TARGET_NR_execve:
9356 return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
9357 case TARGET_NR_chdir:
9358 if (!(p = lock_user_string(arg1)))
9359 return -TARGET_EFAULT;
9360 ret = get_errno(chdir(p));
9361 unlock_user(p, arg1, 0);
9362 return ret;
9363 #ifdef TARGET_NR_time
9364 case TARGET_NR_time:
9366 time_t host_time;
9367 ret = get_errno(time(&host_time));
9368 if (!is_error(ret)
9369 && arg1
9370 && put_user_sal(host_time, arg1))
9371 return -TARGET_EFAULT;
9373 return ret;
9374 #endif
9375 #ifdef TARGET_NR_mknod
9376 case TARGET_NR_mknod:
9377 if (!(p = lock_user_string(arg1)))
9378 return -TARGET_EFAULT;
9379 ret = get_errno(mknod(p, arg2, arg3));
9380 unlock_user(p, arg1, 0);
9381 return ret;
9382 #endif
9383 #if defined(TARGET_NR_mknodat)
9384 case TARGET_NR_mknodat:
9385 if (!(p = lock_user_string(arg2)))
9386 return -TARGET_EFAULT;
9387 ret = get_errno(mknodat(arg1, p, arg3, arg4));
9388 unlock_user(p, arg2, 0);
9389 return ret;
9390 #endif
9391 #ifdef TARGET_NR_chmod
9392 case TARGET_NR_chmod:
9393 if (!(p = lock_user_string(arg1)))
9394 return -TARGET_EFAULT;
9395 ret = get_errno(chmod(p, arg2));
9396 unlock_user(p, arg1, 0);
9397 return ret;
9398 #endif
9399 #ifdef TARGET_NR_lseek
9400 case TARGET_NR_lseek:
9401 return get_errno(lseek(arg1, arg2, arg3));
9402 #endif
9403 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
9404 /* Alpha specific */
9405 case TARGET_NR_getxpid:
9406 cpu_env->ir[IR_A4] = getppid();
9407 return get_errno(getpid());
9408 #endif
9409 #ifdef TARGET_NR_getpid
9410 case TARGET_NR_getpid:
9411 return get_errno(getpid());
9412 #endif
9413 case TARGET_NR_mount:
9415 /* need to look at the data field */
9416 void *p2, *p3;
9418 if (arg1) {
9419 p = lock_user_string(arg1);
9420 if (!p) {
9421 return -TARGET_EFAULT;
9423 } else {
9424 p = NULL;
9427 p2 = lock_user_string(arg2);
9428 if (!p2) {
9429 if (arg1) {
9430 unlock_user(p, arg1, 0);
9432 return -TARGET_EFAULT;
9435 if (arg3) {
9436 p3 = lock_user_string(arg3);
9437 if (!p3) {
9438 if (arg1) {
9439 unlock_user(p, arg1, 0);
9441 unlock_user(p2, arg2, 0);
9442 return -TARGET_EFAULT;
9444 } else {
9445 p3 = NULL;
9448 /* FIXME - arg5 should be locked, but it isn't clear how to
9449 * do that since it's not guaranteed to be a NULL-terminated
9450 * string.
9452 if (!arg5) {
9453 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
9454 } else {
9455 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
9457 ret = get_errno(ret);
9459 if (arg1) {
9460 unlock_user(p, arg1, 0);
9462 unlock_user(p2, arg2, 0);
9463 if (arg3) {
9464 unlock_user(p3, arg3, 0);
9467 return ret;
9468 #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9469 #if defined(TARGET_NR_umount)
9470 case TARGET_NR_umount:
9471 #endif
9472 #if defined(TARGET_NR_oldumount)
9473 case TARGET_NR_oldumount:
9474 #endif
9475 if (!(p = lock_user_string(arg1)))
9476 return -TARGET_EFAULT;
9477 ret = get_errno(umount(p));
9478 unlock_user(p, arg1, 0);
9479 return ret;
9480 #endif
9481 #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9482 case TARGET_NR_move_mount:
9484 void *p2, *p4;
9486 if (!arg2 || !arg4) {
9487 return -TARGET_EFAULT;
9490 p2 = lock_user_string(arg2);
9491 if (!p2) {
9492 return -TARGET_EFAULT;
9495 p4 = lock_user_string(arg4);
9496 if (!p4) {
9497 unlock_user(p2, arg2, 0);
9498 return -TARGET_EFAULT;
9500 ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
9502 unlock_user(p2, arg2, 0);
9503 unlock_user(p4, arg4, 0);
9505 return ret;
9507 #endif
9508 #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9509 case TARGET_NR_open_tree:
9511 void *p2;
9512 int host_flags;
9514 if (!arg2) {
9515 return -TARGET_EFAULT;
9518 p2 = lock_user_string(arg2);
9519 if (!p2) {
9520 return -TARGET_EFAULT;
9523 host_flags = arg3 & ~TARGET_O_CLOEXEC;
9524 if (arg3 & TARGET_O_CLOEXEC) {
9525 host_flags |= O_CLOEXEC;
9528 ret = get_errno(sys_open_tree(arg1, p2, host_flags));
9530 unlock_user(p2, arg2, 0);
9532 return ret;
9534 #endif
9535 #ifdef TARGET_NR_stime /* not on alpha */
9536 case TARGET_NR_stime:
9538 struct timespec ts;
9539 ts.tv_nsec = 0;
9540 if (get_user_sal(ts.tv_sec, arg1)) {
9541 return -TARGET_EFAULT;
9543 return get_errno(clock_settime(CLOCK_REALTIME, &ts));
9545 #endif
9546 #ifdef TARGET_NR_alarm /* not on alpha */
9547 case TARGET_NR_alarm:
9548 return alarm(arg1);
9549 #endif
9550 #ifdef TARGET_NR_pause /* not on alpha */
9551 case TARGET_NR_pause:
9552 if (!block_signals()) {
9553 sigsuspend(&get_task_state(cpu)->signal_mask);
9555 return -TARGET_EINTR;
9556 #endif
9557 #ifdef TARGET_NR_utime
9558 case TARGET_NR_utime:
9560 struct utimbuf tbuf, *host_tbuf;
9561 struct target_utimbuf *target_tbuf;
9562 if (arg2) {
9563 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
9564 return -TARGET_EFAULT;
9565 tbuf.actime = tswapal(target_tbuf->actime);
9566 tbuf.modtime = tswapal(target_tbuf->modtime);
9567 unlock_user_struct(target_tbuf, arg2, 0);
9568 host_tbuf = &tbuf;
9569 } else {
9570 host_tbuf = NULL;
9572 if (!(p = lock_user_string(arg1)))
9573 return -TARGET_EFAULT;
9574 ret = get_errno(utime(p, host_tbuf));
9575 unlock_user(p, arg1, 0);
9577 return ret;
9578 #endif
9579 #ifdef TARGET_NR_utimes
9580 case TARGET_NR_utimes:
9582 struct timeval *tvp, tv[2];
9583 if (arg2) {
9584 if (copy_from_user_timeval(&tv[0], arg2)
9585 || copy_from_user_timeval(&tv[1],
9586 arg2 + sizeof(struct target_timeval)))
9587 return -TARGET_EFAULT;
9588 tvp = tv;
9589 } else {
9590 tvp = NULL;
9592 if (!(p = lock_user_string(arg1)))
9593 return -TARGET_EFAULT;
9594 ret = get_errno(utimes(p, tvp));
9595 unlock_user(p, arg1, 0);
9597 return ret;
9598 #endif
9599 #if defined(TARGET_NR_futimesat)
9600 case TARGET_NR_futimesat:
9602 struct timeval *tvp, tv[2];
9603 if (arg3) {
9604 if (copy_from_user_timeval(&tv[0], arg3)
9605 || copy_from_user_timeval(&tv[1],
9606 arg3 + sizeof(struct target_timeval)))
9607 return -TARGET_EFAULT;
9608 tvp = tv;
9609 } else {
9610 tvp = NULL;
9612 if (!(p = lock_user_string(arg2))) {
9613 return -TARGET_EFAULT;
9615 ret = get_errno(futimesat(arg1, path(p), tvp));
9616 unlock_user(p, arg2, 0);
9618 return ret;
9619 #endif
9620 #ifdef TARGET_NR_access
9621 case TARGET_NR_access:
9622 if (!(p = lock_user_string(arg1))) {
9623 return -TARGET_EFAULT;
9625 ret = get_errno(access(path(p), arg2));
9626 unlock_user(p, arg1, 0);
9627 return ret;
9628 #endif
9629 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9630 case TARGET_NR_faccessat:
9631 if (!(p = lock_user_string(arg2))) {
9632 return -TARGET_EFAULT;
9634 ret = get_errno(faccessat(arg1, p, arg3, 0));
9635 unlock_user(p, arg2, 0);
9636 return ret;
9637 #endif
9638 #if defined(TARGET_NR_faccessat2)
9639 case TARGET_NR_faccessat2:
9640 if (!(p = lock_user_string(arg2))) {
9641 return -TARGET_EFAULT;
9643 ret = get_errno(faccessat(arg1, p, arg3, arg4));
9644 unlock_user(p, arg2, 0);
9645 return ret;
9646 #endif
9647 #ifdef TARGET_NR_nice /* not on alpha */
9648 case TARGET_NR_nice:
9649 return get_errno(nice(arg1));
9650 #endif
9651 case TARGET_NR_sync:
9652 sync();
9653 return 0;
9654 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9655 case TARGET_NR_syncfs:
9656 return get_errno(syncfs(arg1));
9657 #endif
9658 case TARGET_NR_kill:
9659 return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
9660 #ifdef TARGET_NR_rename
9661 case TARGET_NR_rename:
9663 void *p2;
9664 p = lock_user_string(arg1);
9665 p2 = lock_user_string(arg2);
9666 if (!p || !p2)
9667 ret = -TARGET_EFAULT;
9668 else
9669 ret = get_errno(rename(p, p2));
9670 unlock_user(p2, arg2, 0);
9671 unlock_user(p, arg1, 0);
9673 return ret;
9674 #endif
9675 #if defined(TARGET_NR_renameat)
9676 case TARGET_NR_renameat:
9678 void *p2;
9679 p = lock_user_string(arg2);
9680 p2 = lock_user_string(arg4);
9681 if (!p || !p2)
9682 ret = -TARGET_EFAULT;
9683 else
9684 ret = get_errno(renameat(arg1, p, arg3, p2));
9685 unlock_user(p2, arg4, 0);
9686 unlock_user(p, arg2, 0);
9688 return ret;
9689 #endif
9690 #if defined(TARGET_NR_renameat2)
9691 case TARGET_NR_renameat2:
9693 void *p2;
9694 p = lock_user_string(arg2);
9695 p2 = lock_user_string(arg4);
9696 if (!p || !p2) {
9697 ret = -TARGET_EFAULT;
9698 } else {
9699 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
9701 unlock_user(p2, arg4, 0);
9702 unlock_user(p, arg2, 0);
9704 return ret;
9705 #endif
9706 #ifdef TARGET_NR_mkdir
9707 case TARGET_NR_mkdir:
9708 if (!(p = lock_user_string(arg1)))
9709 return -TARGET_EFAULT;
9710 ret = get_errno(mkdir(p, arg2));
9711 unlock_user(p, arg1, 0);
9712 return ret;
9713 #endif
9714 #if defined(TARGET_NR_mkdirat)
9715 case TARGET_NR_mkdirat:
9716 if (!(p = lock_user_string(arg2)))
9717 return -TARGET_EFAULT;
9718 ret = get_errno(mkdirat(arg1, p, arg3));
9719 unlock_user(p, arg2, 0);
9720 return ret;
9721 #endif
9722 #ifdef TARGET_NR_rmdir
9723 case TARGET_NR_rmdir:
9724 if (!(p = lock_user_string(arg1)))
9725 return -TARGET_EFAULT;
9726 ret = get_errno(rmdir(p));
9727 unlock_user(p, arg1, 0);
9728 return ret;
9729 #endif
9730 case TARGET_NR_dup:
9731 ret = get_errno(dup(arg1));
9732 if (ret >= 0) {
9733 fd_trans_dup(arg1, ret);
9735 return ret;
9736 #ifdef TARGET_NR_pipe
9737 case TARGET_NR_pipe:
9738 return do_pipe(cpu_env, arg1, 0, 0);
9739 #endif
9740 #ifdef TARGET_NR_pipe2
9741 case TARGET_NR_pipe2:
9742 return do_pipe(cpu_env, arg1,
9743 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
9744 #endif
9745 case TARGET_NR_times:
9747 struct target_tms *tmsp;
9748 struct tms tms;
9749 ret = get_errno(times(&tms));
9750 if (arg1) {
9751 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
9752 if (!tmsp)
9753 return -TARGET_EFAULT;
9754 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
9755 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
9756 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
9757 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
9759 if (!is_error(ret))
9760 ret = host_to_target_clock_t(ret);
9762 return ret;
9763 case TARGET_NR_acct:
9764 if (arg1 == 0) {
9765 ret = get_errno(acct(NULL));
9766 } else {
9767 if (!(p = lock_user_string(arg1))) {
9768 return -TARGET_EFAULT;
9770 ret = get_errno(acct(path(p)));
9771 unlock_user(p, arg1, 0);
9773 return ret;
9774 #ifdef TARGET_NR_umount2
9775 case TARGET_NR_umount2:
9776 if (!(p = lock_user_string(arg1)))
9777 return -TARGET_EFAULT;
9778 ret = get_errno(umount2(p, arg2));
9779 unlock_user(p, arg1, 0);
9780 return ret;
9781 #endif
9782 case TARGET_NR_ioctl:
9783 return do_ioctl(arg1, arg2, arg3);
9784 #ifdef TARGET_NR_fcntl
9785 case TARGET_NR_fcntl:
9786 return do_fcntl(arg1, arg2, arg3);
9787 #endif
9788 case TARGET_NR_setpgid:
9789 return get_errno(setpgid(arg1, arg2));
9790 case TARGET_NR_umask:
9791 return get_errno(umask(arg1));
9792 case TARGET_NR_chroot:
9793 if (!(p = lock_user_string(arg1)))
9794 return -TARGET_EFAULT;
9795 ret = get_errno(chroot(p));
9796 unlock_user(p, arg1, 0);
9797 return ret;
9798 #ifdef TARGET_NR_dup2
9799 case TARGET_NR_dup2:
9800 ret = get_errno(dup2(arg1, arg2));
9801 if (ret >= 0) {
9802 fd_trans_dup(arg1, arg2);
9804 return ret;
9805 #endif
9806 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9807 case TARGET_NR_dup3:
9809 int host_flags;
9811 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
9812 return -EINVAL;
9814 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
9815 ret = get_errno(dup3(arg1, arg2, host_flags));
9816 if (ret >= 0) {
9817 fd_trans_dup(arg1, arg2);
9819 return ret;
9821 #endif
9822 #ifdef TARGET_NR_getppid /* not on alpha */
9823 case TARGET_NR_getppid:
9824 return get_errno(getppid());
9825 #endif
9826 #ifdef TARGET_NR_getpgrp
9827 case TARGET_NR_getpgrp:
9828 return get_errno(getpgrp());
9829 #endif
9830 case TARGET_NR_setsid:
9831 return get_errno(setsid());
9832 #ifdef TARGET_NR_sigaction
9833 case TARGET_NR_sigaction:
9835 #if defined(TARGET_MIPS)
9836 struct target_sigaction act, oact, *pact, *old_act;
9838 if (arg2) {
9839 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
9840 return -TARGET_EFAULT;
9841 act._sa_handler = old_act->_sa_handler;
9842 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
9843 act.sa_flags = old_act->sa_flags;
9844 unlock_user_struct(old_act, arg2, 0);
9845 pact = &act;
9846 } else {
9847 pact = NULL;
9850 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
9852 if (!is_error(ret) && arg3) {
9853 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
9854 return -TARGET_EFAULT;
9855 old_act->_sa_handler = oact._sa_handler;
9856 old_act->sa_flags = oact.sa_flags;
9857 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
9858 old_act->sa_mask.sig[1] = 0;
9859 old_act->sa_mask.sig[2] = 0;
9860 old_act->sa_mask.sig[3] = 0;
9861 unlock_user_struct(old_act, arg3, 1);
9863 #else
9864 struct target_old_sigaction *old_act;
9865 struct target_sigaction act, oact, *pact;
9866 if (arg2) {
9867 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
9868 return -TARGET_EFAULT;
9869 act._sa_handler = old_act->_sa_handler;
9870 target_siginitset(&act.sa_mask, old_act->sa_mask);
9871 act.sa_flags = old_act->sa_flags;
9872 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9873 act.sa_restorer = old_act->sa_restorer;
9874 #endif
9875 unlock_user_struct(old_act, arg2, 0);
9876 pact = &act;
9877 } else {
9878 pact = NULL;
9880 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
9881 if (!is_error(ret) && arg3) {
9882 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
9883 return -TARGET_EFAULT;
9884 old_act->_sa_handler = oact._sa_handler;
9885 old_act->sa_mask = oact.sa_mask.sig[0];
9886 old_act->sa_flags = oact.sa_flags;
9887 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9888 old_act->sa_restorer = oact.sa_restorer;
9889 #endif
9890 unlock_user_struct(old_act, arg3, 1);
9892 #endif
9894 return ret;
9895 #endif
9896 case TARGET_NR_rt_sigaction:
9899 * For Alpha and SPARC this is a 5 argument syscall, with
9900 * a 'restorer' parameter which must be copied into the
9901 * sa_restorer field of the sigaction struct.
9902 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9903 * and arg5 is the sigsetsize.
9905 #if defined(TARGET_ALPHA)
9906 target_ulong sigsetsize = arg4;
9907 target_ulong restorer = arg5;
9908 #elif defined(TARGET_SPARC)
9909 target_ulong restorer = arg4;
9910 target_ulong sigsetsize = arg5;
9911 #else
9912 target_ulong sigsetsize = arg4;
9913 target_ulong restorer = 0;
9914 #endif
9915 struct target_sigaction *act = NULL;
9916 struct target_sigaction *oact = NULL;
9918 if (sigsetsize != sizeof(target_sigset_t)) {
9919 return -TARGET_EINVAL;
9921 if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
9922 return -TARGET_EFAULT;
9924 if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
9925 ret = -TARGET_EFAULT;
9926 } else {
9927 ret = get_errno(do_sigaction(arg1, act, oact, restorer));
9928 if (oact) {
9929 unlock_user_struct(oact, arg3, 1);
9932 if (act) {
9933 unlock_user_struct(act, arg2, 0);
9936 return ret;
9937 #ifdef TARGET_NR_sgetmask /* not on alpha */
9938 case TARGET_NR_sgetmask:
9940 sigset_t cur_set;
9941 abi_ulong target_set;
9942 ret = do_sigprocmask(0, NULL, &cur_set);
9943 if (!ret) {
9944 host_to_target_old_sigset(&target_set, &cur_set);
9945 ret = target_set;
9948 return ret;
9949 #endif
9950 #ifdef TARGET_NR_ssetmask /* not on alpha */
9951 case TARGET_NR_ssetmask:
9953 sigset_t set, oset;
9954 abi_ulong target_set = arg1;
9955 target_to_host_old_sigset(&set, &target_set);
9956 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
9957 if (!ret) {
9958 host_to_target_old_sigset(&target_set, &oset);
9959 ret = target_set;
9962 return ret;
9963 #endif
9964 #ifdef TARGET_NR_sigprocmask
9965 case TARGET_NR_sigprocmask:
9967 #if defined(TARGET_ALPHA)
9968 sigset_t set, oldset;
9969 abi_ulong mask;
9970 int how;
9972 switch (arg1) {
9973 case TARGET_SIG_BLOCK:
9974 how = SIG_BLOCK;
9975 break;
9976 case TARGET_SIG_UNBLOCK:
9977 how = SIG_UNBLOCK;
9978 break;
9979 case TARGET_SIG_SETMASK:
9980 how = SIG_SETMASK;
9981 break;
9982 default:
9983 return -TARGET_EINVAL;
9985 mask = arg2;
9986 target_to_host_old_sigset(&set, &mask);
9988 ret = do_sigprocmask(how, &set, &oldset);
9989 if (!is_error(ret)) {
9990 host_to_target_old_sigset(&mask, &oldset);
9991 ret = mask;
9992 cpu_env->ir[IR_V0] = 0; /* force no error */
9994 #else
9995 sigset_t set, oldset, *set_ptr;
9996 int how;
9998 if (arg2) {
9999 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
10000 if (!p) {
10001 return -TARGET_EFAULT;
10003 target_to_host_old_sigset(&set, p);
10004 unlock_user(p, arg2, 0);
10005 set_ptr = &set;
10006 switch (arg1) {
10007 case TARGET_SIG_BLOCK:
10008 how = SIG_BLOCK;
10009 break;
10010 case TARGET_SIG_UNBLOCK:
10011 how = SIG_UNBLOCK;
10012 break;
10013 case TARGET_SIG_SETMASK:
10014 how = SIG_SETMASK;
10015 break;
10016 default:
10017 return -TARGET_EINVAL;
10019 } else {
10020 how = 0;
10021 set_ptr = NULL;
10023 ret = do_sigprocmask(how, set_ptr, &oldset);
10024 if (!is_error(ret) && arg3) {
10025 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
10026 return -TARGET_EFAULT;
10027 host_to_target_old_sigset(p, &oldset);
10028 unlock_user(p, arg3, sizeof(target_sigset_t));
10030 #endif
10032 return ret;
10033 #endif
10034 case TARGET_NR_rt_sigprocmask:
10036 int how = arg1;
10037 sigset_t set, oldset, *set_ptr;
10039 if (arg4 != sizeof(target_sigset_t)) {
10040 return -TARGET_EINVAL;
10043 if (arg2) {
10044 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
10045 if (!p) {
10046 return -TARGET_EFAULT;
10048 target_to_host_sigset(&set, p);
10049 unlock_user(p, arg2, 0);
10050 set_ptr = &set;
10051 switch(how) {
10052 case TARGET_SIG_BLOCK:
10053 how = SIG_BLOCK;
10054 break;
10055 case TARGET_SIG_UNBLOCK:
10056 how = SIG_UNBLOCK;
10057 break;
10058 case TARGET_SIG_SETMASK:
10059 how = SIG_SETMASK;
10060 break;
10061 default:
10062 return -TARGET_EINVAL;
10064 } else {
10065 how = 0;
10066 set_ptr = NULL;
10068 ret = do_sigprocmask(how, set_ptr, &oldset);
10069 if (!is_error(ret) && arg3) {
10070 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
10071 return -TARGET_EFAULT;
10072 host_to_target_sigset(p, &oldset);
10073 unlock_user(p, arg3, sizeof(target_sigset_t));
10076 return ret;
10077 #ifdef TARGET_NR_sigpending
10078 case TARGET_NR_sigpending:
10080 sigset_t set;
10081 ret = get_errno(sigpending(&set));
10082 if (!is_error(ret)) {
10083 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
10084 return -TARGET_EFAULT;
10085 host_to_target_old_sigset(p, &set);
10086 unlock_user(p, arg1, sizeof(target_sigset_t));
10089 return ret;
10090 #endif
10091 case TARGET_NR_rt_sigpending:
10093 sigset_t set;
10095 /* Yes, this check is >, not != like most. We follow the kernel's
10096 * logic and it does it like this because it implements
10097 * NR_sigpending through the same code path, and in that case
10098 * the old_sigset_t is smaller in size.
10100 if (arg2 > sizeof(target_sigset_t)) {
10101 return -TARGET_EINVAL;
10104 ret = get_errno(sigpending(&set));
10105 if (!is_error(ret)) {
10106 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
10107 return -TARGET_EFAULT;
10108 host_to_target_sigset(p, &set);
10109 unlock_user(p, arg1, sizeof(target_sigset_t));
10112 return ret;
10113 #ifdef TARGET_NR_sigsuspend
10114 case TARGET_NR_sigsuspend:
10116 sigset_t *set;
10118 #if defined(TARGET_ALPHA)
10119 TaskState *ts = get_task_state(cpu);
10120 /* target_to_host_old_sigset will bswap back */
10121 abi_ulong mask = tswapal(arg1);
10122 set = &ts->sigsuspend_mask;
10123 target_to_host_old_sigset(set, &mask);
10124 #else
10125 ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
10126 if (ret != 0) {
10127 return ret;
10129 #endif
10130 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10131 finish_sigsuspend_mask(ret);
10133 return ret;
10134 #endif
10135 case TARGET_NR_rt_sigsuspend:
10137 sigset_t *set;
10139 ret = process_sigsuspend_mask(&set, arg1, arg2);
10140 if (ret != 0) {
10141 return ret;
10143 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10144 finish_sigsuspend_mask(ret);
10146 return ret;
10147 #ifdef TARGET_NR_rt_sigtimedwait
10148 case TARGET_NR_rt_sigtimedwait:
10150 sigset_t set;
10151 struct timespec uts, *puts;
10152 siginfo_t uinfo;
10154 if (arg4 != sizeof(target_sigset_t)) {
10155 return -TARGET_EINVAL;
10158 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
10159 return -TARGET_EFAULT;
10160 target_to_host_sigset(&set, p);
10161 unlock_user(p, arg1, 0);
10162 if (arg3) {
10163 puts = &uts;
10164 if (target_to_host_timespec(puts, arg3)) {
10165 return -TARGET_EFAULT;
10167 } else {
10168 puts = NULL;
10170 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10171 SIGSET_T_SIZE));
10172 if (!is_error(ret)) {
10173 if (arg2) {
10174 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
10176 if (!p) {
10177 return -TARGET_EFAULT;
10179 host_to_target_siginfo(p, &uinfo);
10180 unlock_user(p, arg2, sizeof(target_siginfo_t));
10182 ret = host_to_target_signal(ret);
10185 return ret;
10186 #endif
10187 #ifdef TARGET_NR_rt_sigtimedwait_time64
10188 case TARGET_NR_rt_sigtimedwait_time64:
10190 sigset_t set;
10191 struct timespec uts, *puts;
10192 siginfo_t uinfo;
10194 if (arg4 != sizeof(target_sigset_t)) {
10195 return -TARGET_EINVAL;
10198 p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
10199 if (!p) {
10200 return -TARGET_EFAULT;
10202 target_to_host_sigset(&set, p);
10203 unlock_user(p, arg1, 0);
10204 if (arg3) {
10205 puts = &uts;
10206 if (target_to_host_timespec64(puts, arg3)) {
10207 return -TARGET_EFAULT;
10209 } else {
10210 puts = NULL;
10212 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10213 SIGSET_T_SIZE));
10214 if (!is_error(ret)) {
10215 if (arg2) {
10216 p = lock_user(VERIFY_WRITE, arg2,
10217 sizeof(target_siginfo_t), 0);
10218 if (!p) {
10219 return -TARGET_EFAULT;
10221 host_to_target_siginfo(p, &uinfo);
10222 unlock_user(p, arg2, sizeof(target_siginfo_t));
10224 ret = host_to_target_signal(ret);
10227 return ret;
10228 #endif
10229 case TARGET_NR_rt_sigqueueinfo:
10231 siginfo_t uinfo;
10233 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
10234 if (!p) {
10235 return -TARGET_EFAULT;
10237 target_to_host_siginfo(&uinfo, p);
10238 unlock_user(p, arg3, 0);
10239 ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
10241 return ret;
10242 case TARGET_NR_rt_tgsigqueueinfo:
10244 siginfo_t uinfo;
10246 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
10247 if (!p) {
10248 return -TARGET_EFAULT;
10250 target_to_host_siginfo(&uinfo, p);
10251 unlock_user(p, arg4, 0);
10252 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
10254 return ret;
10255 #ifdef TARGET_NR_sigreturn
10256 case TARGET_NR_sigreturn:
10257 if (block_signals()) {
10258 return -QEMU_ERESTARTSYS;
10260 return do_sigreturn(cpu_env);
10261 #endif
10262 case TARGET_NR_rt_sigreturn:
10263 if (block_signals()) {
10264 return -QEMU_ERESTARTSYS;
10266 return do_rt_sigreturn(cpu_env);
10267 case TARGET_NR_sethostname:
10268 if (!(p = lock_user_string(arg1)))
10269 return -TARGET_EFAULT;
10270 ret = get_errno(sethostname(p, arg2));
10271 unlock_user(p, arg1, 0);
10272 return ret;
10273 #ifdef TARGET_NR_setrlimit
10274 case TARGET_NR_setrlimit:
10276 int resource = target_to_host_resource(arg1);
10277 struct target_rlimit *target_rlim;
10278 struct rlimit rlim;
10279 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
10280 return -TARGET_EFAULT;
10281 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
10282 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
10283 unlock_user_struct(target_rlim, arg2, 0);
10285 * If we just passed through resource limit settings for memory then
10286 * they would also apply to QEMU's own allocations, and QEMU will
10287 * crash or hang or die if its allocations fail. Ideally we would
10288 * track the guest allocations in QEMU and apply the limits ourselves.
10289 * For now, just tell the guest the call succeeded but don't actually
10290 * limit anything.
10292 if (resource != RLIMIT_AS &&
10293 resource != RLIMIT_DATA &&
10294 resource != RLIMIT_STACK) {
10295 return get_errno(setrlimit(resource, &rlim));
10296 } else {
10297 return 0;
10300 #endif
10301 #ifdef TARGET_NR_getrlimit
10302 case TARGET_NR_getrlimit:
10304 int resource = target_to_host_resource(arg1);
10305 struct target_rlimit *target_rlim;
10306 struct rlimit rlim;
10308 ret = get_errno(getrlimit(resource, &rlim));
10309 if (!is_error(ret)) {
10310 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
10311 return -TARGET_EFAULT;
10312 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
10313 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
10314 unlock_user_struct(target_rlim, arg2, 1);
10317 return ret;
10318 #endif
10319 case TARGET_NR_getrusage:
10321 struct rusage rusage;
10322 ret = get_errno(getrusage(arg1, &rusage));
10323 if (!is_error(ret)) {
10324 ret = host_to_target_rusage(arg2, &rusage);
10327 return ret;
10328 #if defined(TARGET_NR_gettimeofday)
10329 case TARGET_NR_gettimeofday:
10331 struct timeval tv;
10332 struct timezone tz;
10334 ret = get_errno(gettimeofday(&tv, &tz));
10335 if (!is_error(ret)) {
10336 if (arg1 && copy_to_user_timeval(arg1, &tv)) {
10337 return -TARGET_EFAULT;
10339 if (arg2 && copy_to_user_timezone(arg2, &tz)) {
10340 return -TARGET_EFAULT;
10344 return ret;
10345 #endif
10346 #if defined(TARGET_NR_settimeofday)
10347 case TARGET_NR_settimeofday:
10349 struct timeval tv, *ptv = NULL;
10350 struct timezone tz, *ptz = NULL;
10352 if (arg1) {
10353 if (copy_from_user_timeval(&tv, arg1)) {
10354 return -TARGET_EFAULT;
10356 ptv = &tv;
10359 if (arg2) {
10360 if (copy_from_user_timezone(&tz, arg2)) {
10361 return -TARGET_EFAULT;
10363 ptz = &tz;
10366 return get_errno(settimeofday(ptv, ptz));
10368 #endif
10369 #if defined(TARGET_NR_select)
10370 case TARGET_NR_select:
10371 #if defined(TARGET_WANT_NI_OLD_SELECT)
10372 /* some architectures used to have old_select here
10373 * but now ENOSYS it.
10375 ret = -TARGET_ENOSYS;
10376 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
10377 ret = do_old_select(arg1);
10378 #else
10379 ret = do_select(arg1, arg2, arg3, arg4, arg5);
10380 #endif
10381 return ret;
10382 #endif
10383 #ifdef TARGET_NR_pselect6
10384 case TARGET_NR_pselect6:
10385 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
10386 #endif
10387 #ifdef TARGET_NR_pselect6_time64
10388 case TARGET_NR_pselect6_time64:
10389 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
10390 #endif
10391 #ifdef TARGET_NR_symlink
10392 case TARGET_NR_symlink:
10394 void *p2;
10395 p = lock_user_string(arg1);
10396 p2 = lock_user_string(arg2);
10397 if (!p || !p2)
10398 ret = -TARGET_EFAULT;
10399 else
10400 ret = get_errno(symlink(p, p2));
10401 unlock_user(p2, arg2, 0);
10402 unlock_user(p, arg1, 0);
10404 return ret;
10405 #endif
10406 #if defined(TARGET_NR_symlinkat)
10407 case TARGET_NR_symlinkat:
10409 void *p2;
10410 p = lock_user_string(arg1);
10411 p2 = lock_user_string(arg3);
10412 if (!p || !p2)
10413 ret = -TARGET_EFAULT;
10414 else
10415 ret = get_errno(symlinkat(p, arg2, p2));
10416 unlock_user(p2, arg3, 0);
10417 unlock_user(p, arg1, 0);
10419 return ret;
10420 #endif
10421 #ifdef TARGET_NR_readlink
10422 case TARGET_NR_readlink:
10424 void *p2;
10425 p = lock_user_string(arg1);
10426 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10427 ret = get_errno(do_guest_readlink(p, p2, arg3));
10428 unlock_user(p2, arg2, ret);
10429 unlock_user(p, arg1, 0);
10431 return ret;
10432 #endif
10433 #if defined(TARGET_NR_readlinkat)
10434 case TARGET_NR_readlinkat:
10436 void *p2;
10437 p = lock_user_string(arg2);
10438 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
10439 if (!p || !p2) {
10440 ret = -TARGET_EFAULT;
10441 } else if (!arg4) {
10442 /* Short circuit this for the magic exe check. */
10443 ret = -TARGET_EINVAL;
10444 } else if (is_proc_myself((const char *)p, "exe")) {
10446 * Don't worry about sign mismatch as earlier mapping
10447 * logic would have thrown a bad address error.
10449 ret = MIN(strlen(exec_path), arg4);
10450 /* We cannot NUL terminate the string. */
10451 memcpy(p2, exec_path, ret);
10452 } else {
10453 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
10455 unlock_user(p2, arg3, ret);
10456 unlock_user(p, arg2, 0);
10458 return ret;
10459 #endif
10460 #ifdef TARGET_NR_swapon
10461 case TARGET_NR_swapon:
10462 if (!(p = lock_user_string(arg1)))
10463 return -TARGET_EFAULT;
10464 ret = get_errno(swapon(p, arg2));
10465 unlock_user(p, arg1, 0);
10466 return ret;
10467 #endif
10468 case TARGET_NR_reboot:
10469 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
10470 /* arg4 must be ignored in all other cases */
10471 p = lock_user_string(arg4);
10472 if (!p) {
10473 return -TARGET_EFAULT;
10475 ret = get_errno(reboot(arg1, arg2, arg3, p));
10476 unlock_user(p, arg4, 0);
10477 } else {
10478 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
10480 return ret;
10481 #ifdef TARGET_NR_mmap
10482 case TARGET_NR_mmap:
10483 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
10484 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
10485 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
10486 || defined(TARGET_S390X)
10488 abi_ulong *v;
10489 abi_ulong v1, v2, v3, v4, v5, v6;
10490 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
10491 return -TARGET_EFAULT;
10492 v1 = tswapal(v[0]);
10493 v2 = tswapal(v[1]);
10494 v3 = tswapal(v[2]);
10495 v4 = tswapal(v[3]);
10496 v5 = tswapal(v[4]);
10497 v6 = tswapal(v[5]);
10498 unlock_user(v, arg1, 0);
10499 return do_mmap(v1, v2, v3, v4, v5, v6);
10501 #else
10502 /* mmap pointers are always untagged */
10503 return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
10504 #endif
10505 #endif
10506 #ifdef TARGET_NR_mmap2
10507 case TARGET_NR_mmap2:
10508 #ifndef MMAP_SHIFT
10509 #define MMAP_SHIFT 12
10510 #endif
10511 return do_mmap(arg1, arg2, arg3, arg4, arg5,
10512 (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
10513 #endif
10514 case TARGET_NR_munmap:
10515 arg1 = cpu_untagged_addr(cpu, arg1);
10516 return get_errno(target_munmap(arg1, arg2));
10517 case TARGET_NR_mprotect:
10518 arg1 = cpu_untagged_addr(cpu, arg1);
10520 TaskState *ts = get_task_state(cpu);
10521 /* Special hack to detect libc making the stack executable. */
10522 if ((arg3 & PROT_GROWSDOWN)
10523 && arg1 >= ts->info->stack_limit
10524 && arg1 <= ts->info->start_stack) {
10525 arg3 &= ~PROT_GROWSDOWN;
10526 arg2 = arg2 + arg1 - ts->info->stack_limit;
10527 arg1 = ts->info->stack_limit;
10530 return get_errno(target_mprotect(arg1, arg2, arg3));
10531 #ifdef TARGET_NR_mremap
10532 case TARGET_NR_mremap:
10533 arg1 = cpu_untagged_addr(cpu, arg1);
10534 /* mremap new_addr (arg5) is always untagged */
10535 return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
10536 #endif
10537 /* ??? msync/mlock/munlock are broken for softmmu. */
10538 #ifdef TARGET_NR_msync
10539 case TARGET_NR_msync:
10540 return get_errno(msync(g2h(cpu, arg1), arg2,
10541 target_to_host_msync_arg(arg3)));
10542 #endif
10543 #ifdef TARGET_NR_mlock
10544 case TARGET_NR_mlock:
10545 return get_errno(mlock(g2h(cpu, arg1), arg2));
10546 #endif
10547 #ifdef TARGET_NR_munlock
10548 case TARGET_NR_munlock:
10549 return get_errno(munlock(g2h(cpu, arg1), arg2));
10550 #endif
10551 #ifdef TARGET_NR_mlockall
10552 case TARGET_NR_mlockall:
10553 return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
10554 #endif
10555 #ifdef TARGET_NR_munlockall
10556 case TARGET_NR_munlockall:
10557 return get_errno(munlockall());
10558 #endif
10559 #ifdef TARGET_NR_truncate
10560 case TARGET_NR_truncate:
10561 if (!(p = lock_user_string(arg1)))
10562 return -TARGET_EFAULT;
10563 ret = get_errno(truncate(p, arg2));
10564 unlock_user(p, arg1, 0);
10565 return ret;
10566 #endif
10567 #ifdef TARGET_NR_ftruncate
10568 case TARGET_NR_ftruncate:
10569 return get_errno(ftruncate(arg1, arg2));
10570 #endif
10571 case TARGET_NR_fchmod:
10572 return get_errno(fchmod(arg1, arg2));
10573 #if defined(TARGET_NR_fchmodat)
10574 case TARGET_NR_fchmodat:
10575 if (!(p = lock_user_string(arg2)))
10576 return -TARGET_EFAULT;
10577 ret = get_errno(fchmodat(arg1, p, arg3, 0));
10578 unlock_user(p, arg2, 0);
10579 return ret;
10580 #endif
10581 case TARGET_NR_getpriority:
10582 /* Note that negative values are valid for getpriority, so we must
10583 differentiate based on errno settings. */
10584 errno = 0;
10585 ret = getpriority(arg1, arg2);
10586 if (ret == -1 && errno != 0) {
10587 return -host_to_target_errno(errno);
10589 #ifdef TARGET_ALPHA
10590 /* Return value is the unbiased priority. Signal no error. */
10591 cpu_env->ir[IR_V0] = 0;
10592 #else
10593 /* Return value is a biased priority to avoid negative numbers. */
10594 ret = 20 - ret;
10595 #endif
10596 return ret;
10597 case TARGET_NR_setpriority:
10598 return get_errno(setpriority(arg1, arg2, arg3));
10599 #ifdef TARGET_NR_statfs
10600 case TARGET_NR_statfs:
10601 if (!(p = lock_user_string(arg1))) {
10602 return -TARGET_EFAULT;
10604 ret = get_errno(statfs(path(p), &stfs));
10605 unlock_user(p, arg1, 0);
10606 convert_statfs:
10607 if (!is_error(ret)) {
10608 struct target_statfs *target_stfs;
10610 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
10611 return -TARGET_EFAULT;
10612 __put_user(stfs.f_type, &target_stfs->f_type);
10613 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10614 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10615 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10616 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10617 __put_user(stfs.f_files, &target_stfs->f_files);
10618 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10619 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10620 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10621 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
10622 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
10623 #ifdef _STATFS_F_FLAGS
10624 __put_user(stfs.f_flags, &target_stfs->f_flags);
10625 #else
10626 __put_user(0, &target_stfs->f_flags);
10627 #endif
10628 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
10629 unlock_user_struct(target_stfs, arg2, 1);
10631 return ret;
10632 #endif
10633 #ifdef TARGET_NR_fstatfs
10634 case TARGET_NR_fstatfs:
10635 ret = get_errno(fstatfs(arg1, &stfs));
10636 goto convert_statfs;
10637 #endif
10638 #ifdef TARGET_NR_statfs64
10639 case TARGET_NR_statfs64:
10640 if (!(p = lock_user_string(arg1))) {
10641 return -TARGET_EFAULT;
10643 ret = get_errno(statfs(path(p), &stfs));
10644 unlock_user(p, arg1, 0);
10645 convert_statfs64:
10646 if (!is_error(ret)) {
10647 struct target_statfs64 *target_stfs;
10649 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
10650 return -TARGET_EFAULT;
10651 __put_user(stfs.f_type, &target_stfs->f_type);
10652 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10653 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10654 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10655 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10656 __put_user(stfs.f_files, &target_stfs->f_files);
10657 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10658 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10659 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10660 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
10661 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
10662 #ifdef _STATFS_F_FLAGS
10663 __put_user(stfs.f_flags, &target_stfs->f_flags);
10664 #else
10665 __put_user(0, &target_stfs->f_flags);
10666 #endif
10667 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
10668 unlock_user_struct(target_stfs, arg3, 1);
10670 return ret;
10671 case TARGET_NR_fstatfs64:
10672 ret = get_errno(fstatfs(arg1, &stfs));
10673 goto convert_statfs64;
10674 #endif
10675 #ifdef TARGET_NR_socketcall
10676 case TARGET_NR_socketcall:
10677 return do_socketcall(arg1, arg2);
10678 #endif
10679 #ifdef TARGET_NR_accept
10680 case TARGET_NR_accept:
10681 return do_accept4(arg1, arg2, arg3, 0);
10682 #endif
10683 #ifdef TARGET_NR_accept4
10684 case TARGET_NR_accept4:
10685 return do_accept4(arg1, arg2, arg3, arg4);
10686 #endif
10687 #ifdef TARGET_NR_bind
10688 case TARGET_NR_bind:
10689 return do_bind(arg1, arg2, arg3);
10690 #endif
10691 #ifdef TARGET_NR_connect
10692 case TARGET_NR_connect:
10693 return do_connect(arg1, arg2, arg3);
10694 #endif
10695 #ifdef TARGET_NR_getpeername
10696 case TARGET_NR_getpeername:
10697 return do_getpeername(arg1, arg2, arg3);
10698 #endif
10699 #ifdef TARGET_NR_getsockname
10700 case TARGET_NR_getsockname:
10701 return do_getsockname(arg1, arg2, arg3);
10702 #endif
10703 #ifdef TARGET_NR_getsockopt
10704 case TARGET_NR_getsockopt:
10705 return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
10706 #endif
10707 #ifdef TARGET_NR_listen
10708 case TARGET_NR_listen:
10709 return get_errno(listen(arg1, arg2));
10710 #endif
10711 #ifdef TARGET_NR_recv
10712 case TARGET_NR_recv:
10713 return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
10714 #endif
10715 #ifdef TARGET_NR_recvfrom
10716 case TARGET_NR_recvfrom:
10717 return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
10718 #endif
10719 #ifdef TARGET_NR_recvmsg
10720 case TARGET_NR_recvmsg:
10721 return do_sendrecvmsg(arg1, arg2, arg3, 0);
10722 #endif
10723 #ifdef TARGET_NR_send
10724 case TARGET_NR_send:
10725 return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
10726 #endif
10727 #ifdef TARGET_NR_sendmsg
10728 case TARGET_NR_sendmsg:
10729 return do_sendrecvmsg(arg1, arg2, arg3, 1);
10730 #endif
10731 #ifdef TARGET_NR_sendmmsg
10732 case TARGET_NR_sendmmsg:
10733 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
10734 #endif
10735 #ifdef TARGET_NR_recvmmsg
10736 case TARGET_NR_recvmmsg:
10737 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
10738 #endif
10739 #ifdef TARGET_NR_sendto
10740 case TARGET_NR_sendto:
10741 return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
10742 #endif
10743 #ifdef TARGET_NR_shutdown
10744 case TARGET_NR_shutdown:
10745 return get_errno(shutdown(arg1, arg2));
10746 #endif
10747 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10748 case TARGET_NR_getrandom:
10749 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10750 if (!p) {
10751 return -TARGET_EFAULT;
10753 ret = get_errno(getrandom(p, arg2, arg3));
10754 unlock_user(p, arg1, ret);
10755 return ret;
10756 #endif
10757 #ifdef TARGET_NR_socket
10758 case TARGET_NR_socket:
10759 return do_socket(arg1, arg2, arg3);
10760 #endif
10761 #ifdef TARGET_NR_socketpair
10762 case TARGET_NR_socketpair:
10763 return do_socketpair(arg1, arg2, arg3, arg4);
10764 #endif
10765 #ifdef TARGET_NR_setsockopt
10766 case TARGET_NR_setsockopt:
10767 return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
10768 #endif
10769 #if defined(TARGET_NR_syslog)
10770 case TARGET_NR_syslog:
10772 int len = arg2;
10774 switch (arg1) {
10775 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
10776 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
10777 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
10778 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
10779 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
10780 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
10781 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
10782 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
10783 return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
10784 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
10785 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
10786 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
10788 if (len < 0) {
10789 return -TARGET_EINVAL;
10791 if (len == 0) {
10792 return 0;
10794 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10795 if (!p) {
10796 return -TARGET_EFAULT;
10798 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
10799 unlock_user(p, arg2, arg3);
10801 return ret;
10802 default:
10803 return -TARGET_EINVAL;
10806 break;
10807 #endif
10808 case TARGET_NR_setitimer:
10810 struct itimerval value, ovalue, *pvalue;
10812 if (arg2) {
10813 pvalue = &value;
10814 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
10815 || copy_from_user_timeval(&pvalue->it_value,
10816 arg2 + sizeof(struct target_timeval)))
10817 return -TARGET_EFAULT;
10818 } else {
10819 pvalue = NULL;
10821 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
10822 if (!is_error(ret) && arg3) {
10823 if (copy_to_user_timeval(arg3,
10824 &ovalue.it_interval)
10825 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
10826 &ovalue.it_value))
10827 return -TARGET_EFAULT;
10830 return ret;
10831 case TARGET_NR_getitimer:
10833 struct itimerval value;
10835 ret = get_errno(getitimer(arg1, &value));
10836 if (!is_error(ret) && arg2) {
10837 if (copy_to_user_timeval(arg2,
10838 &value.it_interval)
10839 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
10840 &value.it_value))
10841 return -TARGET_EFAULT;
10844 return ret;
10845 #ifdef TARGET_NR_stat
10846 case TARGET_NR_stat:
10847 if (!(p = lock_user_string(arg1))) {
10848 return -TARGET_EFAULT;
10850 ret = get_errno(stat(path(p), &st));
10851 unlock_user(p, arg1, 0);
10852 goto do_stat;
10853 #endif
10854 #ifdef TARGET_NR_lstat
10855 case TARGET_NR_lstat:
10856 if (!(p = lock_user_string(arg1))) {
10857 return -TARGET_EFAULT;
10859 ret = get_errno(lstat(path(p), &st));
10860 unlock_user(p, arg1, 0);
10861 goto do_stat;
10862 #endif
10863 #ifdef TARGET_NR_fstat
10864 case TARGET_NR_fstat:
10866 ret = get_errno(fstat(arg1, &st));
10867 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
10868 do_stat:
10869 #endif
10870 if (!is_error(ret)) {
10871 struct target_stat *target_st;
10873 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
10874 return -TARGET_EFAULT;
10875 memset(target_st, 0, sizeof(*target_st));
10876 __put_user(st.st_dev, &target_st->st_dev);
10877 __put_user(st.st_ino, &target_st->st_ino);
10878 __put_user(st.st_mode, &target_st->st_mode);
10879 __put_user(st.st_uid, &target_st->st_uid);
10880 __put_user(st.st_gid, &target_st->st_gid);
10881 __put_user(st.st_nlink, &target_st->st_nlink);
10882 __put_user(st.st_rdev, &target_st->st_rdev);
10883 __put_user(st.st_size, &target_st->st_size);
10884 __put_user(st.st_blksize, &target_st->st_blksize);
10885 __put_user(st.st_blocks, &target_st->st_blocks);
10886 __put_user(st.st_atime, &target_st->target_st_atime);
10887 __put_user(st.st_mtime, &target_st->target_st_mtime);
10888 __put_user(st.st_ctime, &target_st->target_st_ctime);
10889 #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
10890 __put_user(st.st_atim.tv_nsec,
10891 &target_st->target_st_atime_nsec);
10892 __put_user(st.st_mtim.tv_nsec,
10893 &target_st->target_st_mtime_nsec);
10894 __put_user(st.st_ctim.tv_nsec,
10895 &target_st->target_st_ctime_nsec);
10896 #endif
10897 unlock_user_struct(target_st, arg2, 1);
10900 return ret;
10901 #endif
10902 case TARGET_NR_vhangup:
10903 return get_errno(vhangup());
10904 #ifdef TARGET_NR_syscall
10905 case TARGET_NR_syscall:
10906 return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
10907 arg6, arg7, arg8, 0);
10908 #endif
10909 #if defined(TARGET_NR_wait4)
10910 case TARGET_NR_wait4:
10912 int status;
10913 abi_long status_ptr = arg2;
10914 struct rusage rusage, *rusage_ptr;
10915 abi_ulong target_rusage = arg4;
10916 abi_long rusage_err;
10917 if (target_rusage)
10918 rusage_ptr = &rusage;
10919 else
10920 rusage_ptr = NULL;
10921 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
10922 if (!is_error(ret)) {
10923 if (status_ptr && ret) {
10924 status = host_to_target_waitstatus(status);
10925 if (put_user_s32(status, status_ptr))
10926 return -TARGET_EFAULT;
10928 if (target_rusage) {
10929 rusage_err = host_to_target_rusage(target_rusage, &rusage);
10930 if (rusage_err) {
10931 ret = rusage_err;
10936 return ret;
10937 #endif
10938 #ifdef TARGET_NR_swapoff
10939 case TARGET_NR_swapoff:
10940 if (!(p = lock_user_string(arg1)))
10941 return -TARGET_EFAULT;
10942 ret = get_errno(swapoff(p));
10943 unlock_user(p, arg1, 0);
10944 return ret;
10945 #endif
10946 case TARGET_NR_sysinfo:
10948 struct target_sysinfo *target_value;
10949 struct sysinfo value;
10950 ret = get_errno(sysinfo(&value));
10951 if (!is_error(ret) && arg1)
10953 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
10954 return -TARGET_EFAULT;
10955 __put_user(value.uptime, &target_value->uptime);
10956 __put_user(value.loads[0], &target_value->loads[0]);
10957 __put_user(value.loads[1], &target_value->loads[1]);
10958 __put_user(value.loads[2], &target_value->loads[2]);
10959 __put_user(value.totalram, &target_value->totalram);
10960 __put_user(value.freeram, &target_value->freeram);
10961 __put_user(value.sharedram, &target_value->sharedram);
10962 __put_user(value.bufferram, &target_value->bufferram);
10963 __put_user(value.totalswap, &target_value->totalswap);
10964 __put_user(value.freeswap, &target_value->freeswap);
10965 __put_user(value.procs, &target_value->procs);
10966 __put_user(value.totalhigh, &target_value->totalhigh);
10967 __put_user(value.freehigh, &target_value->freehigh);
10968 __put_user(value.mem_unit, &target_value->mem_unit);
10969 unlock_user_struct(target_value, arg1, 1);
10972 return ret;
10973 #ifdef TARGET_NR_ipc
10974 case TARGET_NR_ipc:
10975 return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
10976 #endif
10977 #ifdef TARGET_NR_semget
10978 case TARGET_NR_semget:
10979 return get_errno(semget(arg1, arg2, arg3));
10980 #endif
10981 #ifdef TARGET_NR_semop
10982 case TARGET_NR_semop:
10983 return do_semtimedop(arg1, arg2, arg3, 0, false);
10984 #endif
10985 #ifdef TARGET_NR_semtimedop
10986 case TARGET_NR_semtimedop:
10987 return do_semtimedop(arg1, arg2, arg3, arg4, false);
10988 #endif
10989 #ifdef TARGET_NR_semtimedop_time64
10990 case TARGET_NR_semtimedop_time64:
10991 return do_semtimedop(arg1, arg2, arg3, arg4, true);
10992 #endif
10993 #ifdef TARGET_NR_semctl
10994 case TARGET_NR_semctl:
10995 return do_semctl(arg1, arg2, arg3, arg4);
10996 #endif
10997 #ifdef TARGET_NR_msgctl
10998 case TARGET_NR_msgctl:
10999 return do_msgctl(arg1, arg2, arg3);
11000 #endif
11001 #ifdef TARGET_NR_msgget
11002 case TARGET_NR_msgget:
11003 return get_errno(msgget(arg1, arg2));
11004 #endif
11005 #ifdef TARGET_NR_msgrcv
11006 case TARGET_NR_msgrcv:
11007 return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
11008 #endif
11009 #ifdef TARGET_NR_msgsnd
11010 case TARGET_NR_msgsnd:
11011 return do_msgsnd(arg1, arg2, arg3, arg4);
11012 #endif
11013 #ifdef TARGET_NR_shmget
11014 case TARGET_NR_shmget:
11015 return get_errno(shmget(arg1, arg2, arg3));
11016 #endif
11017 #ifdef TARGET_NR_shmctl
11018 case TARGET_NR_shmctl:
11019 return do_shmctl(arg1, arg2, arg3);
11020 #endif
11021 #ifdef TARGET_NR_shmat
11022 case TARGET_NR_shmat:
11023 return target_shmat(cpu_env, arg1, arg2, arg3);
11024 #endif
11025 #ifdef TARGET_NR_shmdt
11026 case TARGET_NR_shmdt:
11027 return target_shmdt(arg1);
11028 #endif
11029 case TARGET_NR_fsync:
11030 return get_errno(fsync(arg1));
11031 case TARGET_NR_clone:
11032 /* Linux manages to have three different orderings for its
11033 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
11034 * match the kernel's CONFIG_CLONE_* settings.
11035 * Microblaze is further special in that it uses a sixth
11036 * implicit argument to clone for the TLS pointer.
11038 #if defined(TARGET_MICROBLAZE)
11039 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
11040 #elif defined(TARGET_CLONE_BACKWARDS)
11041 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
11042 #elif defined(TARGET_CLONE_BACKWARDS2)
11043 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
11044 #else
11045 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
11046 #endif
11047 return ret;
11048 #ifdef __NR_exit_group
11049 /* new thread calls */
11050 case TARGET_NR_exit_group:
11051 preexit_cleanup(cpu_env, arg1);
11052 return get_errno(exit_group(arg1));
11053 #endif
11054 case TARGET_NR_setdomainname:
11055 if (!(p = lock_user_string(arg1)))
11056 return -TARGET_EFAULT;
11057 ret = get_errno(setdomainname(p, arg2));
11058 unlock_user(p, arg1, 0);
11059 return ret;
11060 case TARGET_NR_uname:
11061 /* no need to transcode because we use the linux syscall */
11063 struct new_utsname * buf;
11065 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
11066 return -TARGET_EFAULT;
11067 ret = get_errno(sys_uname(buf));
11068 if (!is_error(ret)) {
11069 /* Overwrite the native machine name with whatever is being
11070 emulated. */
11071 g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
11072 sizeof(buf->machine));
11073 /* Allow the user to override the reported release. */
11074 if (qemu_uname_release && *qemu_uname_release) {
11075 g_strlcpy(buf->release, qemu_uname_release,
11076 sizeof(buf->release));
11079 unlock_user_struct(buf, arg1, 1);
11081 return ret;
11082 #ifdef TARGET_I386
11083 case TARGET_NR_modify_ldt:
11084 return do_modify_ldt(cpu_env, arg1, arg2, arg3);
11085 #if !defined(TARGET_X86_64)
11086 case TARGET_NR_vm86:
11087 return do_vm86(cpu_env, arg1, arg2);
11088 #endif
11089 #endif
11090 #if defined(TARGET_NR_adjtimex)
11091 case TARGET_NR_adjtimex:
11093 struct timex host_buf;
11095 if (target_to_host_timex(&host_buf, arg1) != 0) {
11096 return -TARGET_EFAULT;
11098 ret = get_errno(adjtimex(&host_buf));
11099 if (!is_error(ret)) {
11100 if (host_to_target_timex(arg1, &host_buf) != 0) {
11101 return -TARGET_EFAULT;
11105 return ret;
11106 #endif
11107 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
11108 case TARGET_NR_clock_adjtime:
11110 struct timex htx;
11112 if (target_to_host_timex(&htx, arg2) != 0) {
11113 return -TARGET_EFAULT;
11115 ret = get_errno(clock_adjtime(arg1, &htx));
11116 if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
11117 return -TARGET_EFAULT;
11120 return ret;
11121 #endif
11122 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
11123 case TARGET_NR_clock_adjtime64:
11125 struct timex htx;
11127 if (target_to_host_timex64(&htx, arg2) != 0) {
11128 return -TARGET_EFAULT;
11130 ret = get_errno(clock_adjtime(arg1, &htx));
11131 if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
11132 return -TARGET_EFAULT;
11135 return ret;
11136 #endif
11137 case TARGET_NR_getpgid:
11138 return get_errno(getpgid(arg1));
11139 case TARGET_NR_fchdir:
11140 return get_errno(fchdir(arg1));
11141 case TARGET_NR_personality:
11142 return get_errno(personality(arg1));
11143 #ifdef TARGET_NR__llseek /* Not on alpha */
11144 case TARGET_NR__llseek:
11146 int64_t res;
11147 #if !defined(__NR_llseek)
11148 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
11149 if (res == -1) {
11150 ret = get_errno(res);
11151 } else {
11152 ret = 0;
11154 #else
11155 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
11156 #endif
11157 if ((ret == 0) && put_user_s64(res, arg4)) {
11158 return -TARGET_EFAULT;
11161 return ret;
11162 #endif
11163 #ifdef TARGET_NR_getdents
11164 case TARGET_NR_getdents:
11165 return do_getdents(arg1, arg2, arg3);
11166 #endif /* TARGET_NR_getdents */
11167 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
11168 case TARGET_NR_getdents64:
11169 return do_getdents64(arg1, arg2, arg3);
11170 #endif /* TARGET_NR_getdents64 */
11171 #if defined(TARGET_NR__newselect)
11172 case TARGET_NR__newselect:
11173 return do_select(arg1, arg2, arg3, arg4, arg5);
11174 #endif
11175 #ifdef TARGET_NR_poll
11176 case TARGET_NR_poll:
11177 return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
11178 #endif
11179 #ifdef TARGET_NR_ppoll
11180 case TARGET_NR_ppoll:
11181 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
11182 #endif
11183 #ifdef TARGET_NR_ppoll_time64
11184 case TARGET_NR_ppoll_time64:
11185 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
11186 #endif
11187 case TARGET_NR_flock:
11188 /* NOTE: the flock constant seems to be the same for every
11189 Linux platform */
11190 return get_errno(safe_flock(arg1, arg2));
11191 case TARGET_NR_readv:
11193 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11194 if (vec != NULL) {
11195 ret = get_errno(safe_readv(arg1, vec, arg3));
11196 unlock_iovec(vec, arg2, arg3, 1);
11197 } else {
11198 ret = -host_to_target_errno(errno);
11201 return ret;
11202 case TARGET_NR_writev:
11204 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11205 if (vec != NULL) {
11206 ret = get_errno(safe_writev(arg1, vec, arg3));
11207 unlock_iovec(vec, arg2, arg3, 0);
11208 } else {
11209 ret = -host_to_target_errno(errno);
11212 return ret;
11213 #if defined(TARGET_NR_preadv)
11214 case TARGET_NR_preadv:
11216 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11217 if (vec != NULL) {
11218 unsigned long low, high;
11220 target_to_host_low_high(arg4, arg5, &low, &high);
11221 ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
11222 unlock_iovec(vec, arg2, arg3, 1);
11223 } else {
11224 ret = -host_to_target_errno(errno);
11227 return ret;
11228 #endif
11229 #if defined(TARGET_NR_pwritev)
11230 case TARGET_NR_pwritev:
11232 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11233 if (vec != NULL) {
11234 unsigned long low, high;
11236 target_to_host_low_high(arg4, arg5, &low, &high);
11237 ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
11238 unlock_iovec(vec, arg2, arg3, 0);
11239 } else {
11240 ret = -host_to_target_errno(errno);
11243 return ret;
11244 #endif
11245 case TARGET_NR_getsid:
11246 return get_errno(getsid(arg1));
11247 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
11248 case TARGET_NR_fdatasync:
11249 return get_errno(fdatasync(arg1));
11250 #endif
11251 case TARGET_NR_sched_getaffinity:
11253 unsigned int mask_size;
11254 unsigned long *mask;
11257 * sched_getaffinity needs multiples of ulong, so need to take
11258 * care of mismatches between target ulong and host ulong sizes.
11260 if (arg2 & (sizeof(abi_ulong) - 1)) {
11261 return -TARGET_EINVAL;
11263 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
11265 mask = alloca(mask_size);
11266 memset(mask, 0, mask_size);
11267 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
11269 if (!is_error(ret)) {
11270 if (ret > arg2) {
11271 /* More data returned than the caller's buffer will fit.
11272 * This only happens if sizeof(abi_long) < sizeof(long)
11273 * and the caller passed us a buffer holding an odd number
11274 * of abi_longs. If the host kernel is actually using the
11275 * extra 4 bytes then fail EINVAL; otherwise we can just
11276 * ignore them and only copy the interesting part.
11278 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
11279 if (numcpus > arg2 * 8) {
11280 return -TARGET_EINVAL;
11282 ret = arg2;
11285 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
11286 return -TARGET_EFAULT;
11290 return ret;
11291 case TARGET_NR_sched_setaffinity:
11293 unsigned int mask_size;
11294 unsigned long *mask;
11297 * sched_setaffinity needs multiples of ulong, so need to take
11298 * care of mismatches between target ulong and host ulong sizes.
11300 if (arg2 & (sizeof(abi_ulong) - 1)) {
11301 return -TARGET_EINVAL;
11303 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
11304 mask = alloca(mask_size);
11306 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
11307 if (ret) {
11308 return ret;
11311 return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
11313 case TARGET_NR_getcpu:
11315 unsigned cpuid, node;
11316 ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL,
11317 arg2 ? &node : NULL,
11318 NULL));
11319 if (is_error(ret)) {
11320 return ret;
11322 if (arg1 && put_user_u32(cpuid, arg1)) {
11323 return -TARGET_EFAULT;
11325 if (arg2 && put_user_u32(node, arg2)) {
11326 return -TARGET_EFAULT;
11329 return ret;
11330 case TARGET_NR_sched_setparam:
11332 struct target_sched_param *target_schp;
11333 struct sched_param schp;
11335 if (arg2 == 0) {
11336 return -TARGET_EINVAL;
11338 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
11339 return -TARGET_EFAULT;
11341 schp.sched_priority = tswap32(target_schp->sched_priority);
11342 unlock_user_struct(target_schp, arg2, 0);
11343 return get_errno(sys_sched_setparam(arg1, &schp));
11345 case TARGET_NR_sched_getparam:
11347 struct target_sched_param *target_schp;
11348 struct sched_param schp;
11350 if (arg2 == 0) {
11351 return -TARGET_EINVAL;
11353 ret = get_errno(sys_sched_getparam(arg1, &schp));
11354 if (!is_error(ret)) {
11355 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
11356 return -TARGET_EFAULT;
11358 target_schp->sched_priority = tswap32(schp.sched_priority);
11359 unlock_user_struct(target_schp, arg2, 1);
11362 return ret;
11363 case TARGET_NR_sched_setscheduler:
11365 struct target_sched_param *target_schp;
11366 struct sched_param schp;
11367 if (arg3 == 0) {
11368 return -TARGET_EINVAL;
11370 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
11371 return -TARGET_EFAULT;
11373 schp.sched_priority = tswap32(target_schp->sched_priority);
11374 unlock_user_struct(target_schp, arg3, 0);
11375 return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
11377 case TARGET_NR_sched_getscheduler:
11378 return get_errno(sys_sched_getscheduler(arg1));
11379 case TARGET_NR_sched_getattr:
11381 struct target_sched_attr *target_scha;
11382 struct sched_attr scha;
11383 if (arg2 == 0) {
11384 return -TARGET_EINVAL;
11386 if (arg3 > sizeof(scha)) {
11387 arg3 = sizeof(scha);
11389 ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
11390 if (!is_error(ret)) {
11391 target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11392 if (!target_scha) {
11393 return -TARGET_EFAULT;
11395 target_scha->size = tswap32(scha.size);
11396 target_scha->sched_policy = tswap32(scha.sched_policy);
11397 target_scha->sched_flags = tswap64(scha.sched_flags);
11398 target_scha->sched_nice = tswap32(scha.sched_nice);
11399 target_scha->sched_priority = tswap32(scha.sched_priority);
11400 target_scha->sched_runtime = tswap64(scha.sched_runtime);
11401 target_scha->sched_deadline = tswap64(scha.sched_deadline);
11402 target_scha->sched_period = tswap64(scha.sched_period);
11403 if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
11404 target_scha->sched_util_min = tswap32(scha.sched_util_min);
11405 target_scha->sched_util_max = tswap32(scha.sched_util_max);
11407 unlock_user(target_scha, arg2, arg3);
11409 return ret;
11411 case TARGET_NR_sched_setattr:
11413 struct target_sched_attr *target_scha;
11414 struct sched_attr scha;
11415 uint32_t size;
11416 int zeroed;
11417 if (arg2 == 0) {
11418 return -TARGET_EINVAL;
11420 if (get_user_u32(size, arg2)) {
11421 return -TARGET_EFAULT;
11423 if (!size) {
11424 size = offsetof(struct target_sched_attr, sched_util_min);
11426 if (size < offsetof(struct target_sched_attr, sched_util_min)) {
11427 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11428 return -TARGET_EFAULT;
11430 return -TARGET_E2BIG;
11433 zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
11434 if (zeroed < 0) {
11435 return zeroed;
11436 } else if (zeroed == 0) {
11437 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11438 return -TARGET_EFAULT;
11440 return -TARGET_E2BIG;
11442 if (size > sizeof(struct target_sched_attr)) {
11443 size = sizeof(struct target_sched_attr);
11446 target_scha = lock_user(VERIFY_READ, arg2, size, 1);
11447 if (!target_scha) {
11448 return -TARGET_EFAULT;
11450 scha.size = size;
11451 scha.sched_policy = tswap32(target_scha->sched_policy);
11452 scha.sched_flags = tswap64(target_scha->sched_flags);
11453 scha.sched_nice = tswap32(target_scha->sched_nice);
11454 scha.sched_priority = tswap32(target_scha->sched_priority);
11455 scha.sched_runtime = tswap64(target_scha->sched_runtime);
11456 scha.sched_deadline = tswap64(target_scha->sched_deadline);
11457 scha.sched_period = tswap64(target_scha->sched_period);
11458 if (size > offsetof(struct target_sched_attr, sched_util_min)) {
11459 scha.sched_util_min = tswap32(target_scha->sched_util_min);
11460 scha.sched_util_max = tswap32(target_scha->sched_util_max);
11462 unlock_user(target_scha, arg2, 0);
11463 return get_errno(sys_sched_setattr(arg1, &scha, arg3));
11465 case TARGET_NR_sched_yield:
11466 return get_errno(sched_yield());
11467 case TARGET_NR_sched_get_priority_max:
11468 return get_errno(sched_get_priority_max(arg1));
11469 case TARGET_NR_sched_get_priority_min:
11470 return get_errno(sched_get_priority_min(arg1));
11471 #ifdef TARGET_NR_sched_rr_get_interval
11472 case TARGET_NR_sched_rr_get_interval:
11474 struct timespec ts;
11475 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11476 if (!is_error(ret)) {
11477 ret = host_to_target_timespec(arg2, &ts);
11480 return ret;
11481 #endif
11482 #ifdef TARGET_NR_sched_rr_get_interval_time64
11483 case TARGET_NR_sched_rr_get_interval_time64:
11485 struct timespec ts;
11486 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11487 if (!is_error(ret)) {
11488 ret = host_to_target_timespec64(arg2, &ts);
11491 return ret;
11492 #endif
11493 #if defined(TARGET_NR_nanosleep)
11494 case TARGET_NR_nanosleep:
11496 struct timespec req, rem;
11497 target_to_host_timespec(&req, arg1);
11498 ret = get_errno(safe_nanosleep(&req, &rem));
11499 if (is_error(ret) && arg2) {
11500 host_to_target_timespec(arg2, &rem);
11503 return ret;
11504 #endif
11505 case TARGET_NR_prctl:
11506 return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
11507 break;
11508 #ifdef TARGET_NR_arch_prctl
11509 case TARGET_NR_arch_prctl:
11510 return do_arch_prctl(cpu_env, arg1, arg2);
11511 #endif
11512 #ifdef TARGET_NR_pread64
11513 case TARGET_NR_pread64:
11514 if (regpairs_aligned(cpu_env, num)) {
11515 arg4 = arg5;
11516 arg5 = arg6;
11518 if (arg2 == 0 && arg3 == 0) {
11519 /* Special-case NULL buffer and zero length, which should succeed */
11520 p = 0;
11521 } else {
11522 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11523 if (!p) {
11524 return -TARGET_EFAULT;
11527 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
11528 unlock_user(p, arg2, ret);
11529 return ret;
11530 case TARGET_NR_pwrite64:
11531 if (regpairs_aligned(cpu_env, num)) {
11532 arg4 = arg5;
11533 arg5 = arg6;
11535 if (arg2 == 0 && arg3 == 0) {
11536 /* Special-case NULL buffer and zero length, which should succeed */
11537 p = 0;
11538 } else {
11539 p = lock_user(VERIFY_READ, arg2, arg3, 1);
11540 if (!p) {
11541 return -TARGET_EFAULT;
11544 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
11545 unlock_user(p, arg2, 0);
11546 return ret;
11547 #endif
11548 case TARGET_NR_getcwd:
11549 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
11550 return -TARGET_EFAULT;
11551 ret = get_errno(sys_getcwd1(p, arg2));
11552 unlock_user(p, arg1, ret);
11553 return ret;
11554 case TARGET_NR_capget:
11555 case TARGET_NR_capset:
11557 struct target_user_cap_header *target_header;
11558 struct target_user_cap_data *target_data = NULL;
11559 struct __user_cap_header_struct header;
11560 struct __user_cap_data_struct data[2];
11561 struct __user_cap_data_struct *dataptr = NULL;
11562 int i, target_datalen;
11563 int data_items = 1;
11565 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
11566 return -TARGET_EFAULT;
11568 header.version = tswap32(target_header->version);
11569 header.pid = tswap32(target_header->pid);
11571 if (header.version != _LINUX_CAPABILITY_VERSION) {
11572 /* Version 2 and up takes pointer to two user_data structs */
11573 data_items = 2;
11576 target_datalen = sizeof(*target_data) * data_items;
11578 if (arg2) {
11579 if (num == TARGET_NR_capget) {
11580 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
11581 } else {
11582 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
11584 if (!target_data) {
11585 unlock_user_struct(target_header, arg1, 0);
11586 return -TARGET_EFAULT;
11589 if (num == TARGET_NR_capset) {
11590 for (i = 0; i < data_items; i++) {
11591 data[i].effective = tswap32(target_data[i].effective);
11592 data[i].permitted = tswap32(target_data[i].permitted);
11593 data[i].inheritable = tswap32(target_data[i].inheritable);
11597 dataptr = data;
11600 if (num == TARGET_NR_capget) {
11601 ret = get_errno(capget(&header, dataptr));
11602 } else {
11603 ret = get_errno(capset(&header, dataptr));
11606 /* The kernel always updates version for both capget and capset */
11607 target_header->version = tswap32(header.version);
11608 unlock_user_struct(target_header, arg1, 1);
11610 if (arg2) {
11611 if (num == TARGET_NR_capget) {
11612 for (i = 0; i < data_items; i++) {
11613 target_data[i].effective = tswap32(data[i].effective);
11614 target_data[i].permitted = tswap32(data[i].permitted);
11615 target_data[i].inheritable = tswap32(data[i].inheritable);
11617 unlock_user(target_data, arg2, target_datalen);
11618 } else {
11619 unlock_user(target_data, arg2, 0);
11622 return ret;
11624 case TARGET_NR_sigaltstack:
11625 return do_sigaltstack(arg1, arg2, cpu_env);
11627 #ifdef CONFIG_SENDFILE
11628 #ifdef TARGET_NR_sendfile
11629 case TARGET_NR_sendfile:
11631 off_t *offp = NULL;
11632 off_t off;
11633 if (arg3) {
11634 ret = get_user_sal(off, arg3);
11635 if (is_error(ret)) {
11636 return ret;
11638 offp = &off;
11640 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11641 if (!is_error(ret) && arg3) {
11642 abi_long ret2 = put_user_sal(off, arg3);
11643 if (is_error(ret2)) {
11644 ret = ret2;
11647 return ret;
11649 #endif
11650 #ifdef TARGET_NR_sendfile64
11651 case TARGET_NR_sendfile64:
11653 off_t *offp = NULL;
11654 off_t off;
11655 if (arg3) {
11656 ret = get_user_s64(off, arg3);
11657 if (is_error(ret)) {
11658 return ret;
11660 offp = &off;
11662 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11663 if (!is_error(ret) && arg3) {
11664 abi_long ret2 = put_user_s64(off, arg3);
11665 if (is_error(ret2)) {
11666 ret = ret2;
11669 return ret;
11671 #endif
11672 #endif
11673 #ifdef TARGET_NR_vfork
11674 case TARGET_NR_vfork:
11675 return get_errno(do_fork(cpu_env,
11676 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
11677 0, 0, 0, 0));
11678 #endif
11679 #ifdef TARGET_NR_ugetrlimit
11680 case TARGET_NR_ugetrlimit:
11682 struct rlimit rlim;
11683 int resource = target_to_host_resource(arg1);
11684 ret = get_errno(getrlimit(resource, &rlim));
11685 if (!is_error(ret)) {
11686 struct target_rlimit *target_rlim;
11687 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
11688 return -TARGET_EFAULT;
11689 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
11690 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
11691 unlock_user_struct(target_rlim, arg2, 1);
11693 return ret;
11695 #endif
11696 #ifdef TARGET_NR_truncate64
11697 case TARGET_NR_truncate64:
11698 if (!(p = lock_user_string(arg1)))
11699 return -TARGET_EFAULT;
11700 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
11701 unlock_user(p, arg1, 0);
11702 return ret;
11703 #endif
11704 #ifdef TARGET_NR_ftruncate64
11705 case TARGET_NR_ftruncate64:
11706 return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
11707 #endif
11708 #ifdef TARGET_NR_stat64
11709 case TARGET_NR_stat64:
11710 if (!(p = lock_user_string(arg1))) {
11711 return -TARGET_EFAULT;
11713 ret = get_errno(stat(path(p), &st));
11714 unlock_user(p, arg1, 0);
11715 if (!is_error(ret))
11716 ret = host_to_target_stat64(cpu_env, arg2, &st);
11717 return ret;
11718 #endif
11719 #ifdef TARGET_NR_lstat64
11720 case TARGET_NR_lstat64:
11721 if (!(p = lock_user_string(arg1))) {
11722 return -TARGET_EFAULT;
11724 ret = get_errno(lstat(path(p), &st));
11725 unlock_user(p, arg1, 0);
11726 if (!is_error(ret))
11727 ret = host_to_target_stat64(cpu_env, arg2, &st);
11728 return ret;
11729 #endif
11730 #ifdef TARGET_NR_fstat64
11731 case TARGET_NR_fstat64:
11732 ret = get_errno(fstat(arg1, &st));
11733 if (!is_error(ret))
11734 ret = host_to_target_stat64(cpu_env, arg2, &st);
11735 return ret;
11736 #endif
11737 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
11738 #ifdef TARGET_NR_fstatat64
11739 case TARGET_NR_fstatat64:
11740 #endif
11741 #ifdef TARGET_NR_newfstatat
11742 case TARGET_NR_newfstatat:
11743 #endif
11744 if (!(p = lock_user_string(arg2))) {
11745 return -TARGET_EFAULT;
11747 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
11748 unlock_user(p, arg2, 0);
11749 if (!is_error(ret))
11750 ret = host_to_target_stat64(cpu_env, arg3, &st);
11751 return ret;
11752 #endif
11753 #if defined(TARGET_NR_statx)
11754 case TARGET_NR_statx:
11756 struct target_statx *target_stx;
11757 int dirfd = arg1;
11758 int flags = arg3;
11760 p = lock_user_string(arg2);
11761 if (p == NULL) {
11762 return -TARGET_EFAULT;
11764 #if defined(__NR_statx)
11767 * It is assumed that struct statx is architecture independent.
11769 struct target_statx host_stx;
11770 int mask = arg4;
11772 ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
11773 if (!is_error(ret)) {
11774 if (host_to_target_statx(&host_stx, arg5) != 0) {
11775 unlock_user(p, arg2, 0);
11776 return -TARGET_EFAULT;
11780 if (ret != -TARGET_ENOSYS) {
11781 unlock_user(p, arg2, 0);
11782 return ret;
11785 #endif
11786 ret = get_errno(fstatat(dirfd, path(p), &st, flags));
11787 unlock_user(p, arg2, 0);
11789 if (!is_error(ret)) {
11790 if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
11791 return -TARGET_EFAULT;
11793 memset(target_stx, 0, sizeof(*target_stx));
11794 __put_user(major(st.st_dev), &target_stx->stx_dev_major);
11795 __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
11796 __put_user(st.st_ino, &target_stx->stx_ino);
11797 __put_user(st.st_mode, &target_stx->stx_mode);
11798 __put_user(st.st_uid, &target_stx->stx_uid);
11799 __put_user(st.st_gid, &target_stx->stx_gid);
11800 __put_user(st.st_nlink, &target_stx->stx_nlink);
11801 __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
11802 __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
11803 __put_user(st.st_size, &target_stx->stx_size);
11804 __put_user(st.st_blksize, &target_stx->stx_blksize);
11805 __put_user(st.st_blocks, &target_stx->stx_blocks);
11806 __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
11807 __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
11808 __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
11809 unlock_user_struct(target_stx, arg5, 1);
11812 return ret;
11813 #endif
11814 #ifdef TARGET_NR_lchown
11815 case TARGET_NR_lchown:
11816 if (!(p = lock_user_string(arg1)))
11817 return -TARGET_EFAULT;
11818 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
11819 unlock_user(p, arg1, 0);
11820 return ret;
11821 #endif
11822 #ifdef TARGET_NR_getuid
11823 case TARGET_NR_getuid:
11824 return get_errno(high2lowuid(getuid()));
11825 #endif
11826 #ifdef TARGET_NR_getgid
11827 case TARGET_NR_getgid:
11828 return get_errno(high2lowgid(getgid()));
11829 #endif
11830 #ifdef TARGET_NR_geteuid
11831 case TARGET_NR_geteuid:
11832 return get_errno(high2lowuid(geteuid()));
11833 #endif
11834 #ifdef TARGET_NR_getegid
11835 case TARGET_NR_getegid:
11836 return get_errno(high2lowgid(getegid()));
11837 #endif
11838 case TARGET_NR_setreuid:
11839 return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
11840 case TARGET_NR_setregid:
11841 return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
11842 case TARGET_NR_getgroups:
11843 { /* the same code as for TARGET_NR_getgroups32 */
11844 int gidsetsize = arg1;
11845 target_id *target_grouplist;
11846 g_autofree gid_t *grouplist = NULL;
11847 int i;
11849 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
11850 return -TARGET_EINVAL;
11852 if (gidsetsize > 0) {
11853 grouplist = g_try_new(gid_t, gidsetsize);
11854 if (!grouplist) {
11855 return -TARGET_ENOMEM;
11858 ret = get_errno(getgroups(gidsetsize, grouplist));
11859 if (!is_error(ret) && gidsetsize > 0) {
11860 target_grouplist = lock_user(VERIFY_WRITE, arg2,
11861 gidsetsize * sizeof(target_id), 0);
11862 if (!target_grouplist) {
11863 return -TARGET_EFAULT;
11865 for (i = 0; i < ret; i++) {
11866 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
11868 unlock_user(target_grouplist, arg2,
11869 gidsetsize * sizeof(target_id));
11871 return ret;
11873 case TARGET_NR_setgroups:
11874 { /* the same code as for TARGET_NR_setgroups32 */
11875 int gidsetsize = arg1;
11876 target_id *target_grouplist;
11877 g_autofree gid_t *grouplist = NULL;
11878 int i;
11880 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
11881 return -TARGET_EINVAL;
11883 if (gidsetsize > 0) {
11884 grouplist = g_try_new(gid_t, gidsetsize);
11885 if (!grouplist) {
11886 return -TARGET_ENOMEM;
11888 target_grouplist = lock_user(VERIFY_READ, arg2,
11889 gidsetsize * sizeof(target_id), 1);
11890 if (!target_grouplist) {
11891 return -TARGET_EFAULT;
11893 for (i = 0; i < gidsetsize; i++) {
11894 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
11896 unlock_user(target_grouplist, arg2,
11897 gidsetsize * sizeof(target_id));
11899 return get_errno(setgroups(gidsetsize, grouplist));
11901 case TARGET_NR_fchown:
11902 return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
11903 #if defined(TARGET_NR_fchownat)
11904 case TARGET_NR_fchownat:
11905 if (!(p = lock_user_string(arg2)))
11906 return -TARGET_EFAULT;
11907 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
11908 low2highgid(arg4), arg5));
11909 unlock_user(p, arg2, 0);
11910 return ret;
11911 #endif
11912 #ifdef TARGET_NR_setresuid
11913 case TARGET_NR_setresuid:
11914 return get_errno(sys_setresuid(low2highuid(arg1),
11915 low2highuid(arg2),
11916 low2highuid(arg3)));
11917 #endif
11918 #ifdef TARGET_NR_getresuid
11919 case TARGET_NR_getresuid:
11921 uid_t ruid, euid, suid;
11922 ret = get_errno(getresuid(&ruid, &euid, &suid));
11923 if (!is_error(ret)) {
11924 if (put_user_id(high2lowuid(ruid), arg1)
11925 || put_user_id(high2lowuid(euid), arg2)
11926 || put_user_id(high2lowuid(suid), arg3))
11927 return -TARGET_EFAULT;
11930 return ret;
11931 #endif
11932 #ifdef TARGET_NR_getresgid
11933 case TARGET_NR_setresgid:
11934 return get_errno(sys_setresgid(low2highgid(arg1),
11935 low2highgid(arg2),
11936 low2highgid(arg3)));
11937 #endif
11938 #ifdef TARGET_NR_getresgid
11939 case TARGET_NR_getresgid:
11941 gid_t rgid, egid, sgid;
11942 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11943 if (!is_error(ret)) {
11944 if (put_user_id(high2lowgid(rgid), arg1)
11945 || put_user_id(high2lowgid(egid), arg2)
11946 || put_user_id(high2lowgid(sgid), arg3))
11947 return -TARGET_EFAULT;
11950 return ret;
11951 #endif
11952 #ifdef TARGET_NR_chown
11953 case TARGET_NR_chown:
11954 if (!(p = lock_user_string(arg1)))
11955 return -TARGET_EFAULT;
11956 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
11957 unlock_user(p, arg1, 0);
11958 return ret;
11959 #endif
11960 case TARGET_NR_setuid:
11961 return get_errno(sys_setuid(low2highuid(arg1)));
11962 case TARGET_NR_setgid:
11963 return get_errno(sys_setgid(low2highgid(arg1)));
11964 case TARGET_NR_setfsuid:
11965 return get_errno(setfsuid(arg1));
11966 case TARGET_NR_setfsgid:
11967 return get_errno(setfsgid(arg1));
11969 #ifdef TARGET_NR_lchown32
11970 case TARGET_NR_lchown32:
11971 if (!(p = lock_user_string(arg1)))
11972 return -TARGET_EFAULT;
11973 ret = get_errno(lchown(p, arg2, arg3));
11974 unlock_user(p, arg1, 0);
11975 return ret;
11976 #endif
11977 #ifdef TARGET_NR_getuid32
11978 case TARGET_NR_getuid32:
11979 return get_errno(getuid());
11980 #endif
11982 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11983 /* Alpha specific */
11984 case TARGET_NR_getxuid:
11986 uid_t euid;
11987 euid=geteuid();
11988 cpu_env->ir[IR_A4]=euid;
11990 return get_errno(getuid());
11991 #endif
11992 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11993 /* Alpha specific */
11994 case TARGET_NR_getxgid:
11996 uid_t egid;
11997 egid=getegid();
11998 cpu_env->ir[IR_A4]=egid;
12000 return get_errno(getgid());
12001 #endif
12002 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
12003 /* Alpha specific */
12004 case TARGET_NR_osf_getsysinfo:
12005 ret = -TARGET_EOPNOTSUPP;
12006 switch (arg1) {
12007 case TARGET_GSI_IEEE_FP_CONTROL:
12009 uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
12010 uint64_t swcr = cpu_env->swcr;
12012 swcr &= ~SWCR_STATUS_MASK;
12013 swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
12015 if (put_user_u64 (swcr, arg2))
12016 return -TARGET_EFAULT;
12017 ret = 0;
12019 break;
12021 /* case GSI_IEEE_STATE_AT_SIGNAL:
12022 -- Not implemented in linux kernel.
12023 case GSI_UACPROC:
12024 -- Retrieves current unaligned access state; not much used.
12025 case GSI_PROC_TYPE:
12026 -- Retrieves implver information; surely not used.
12027 case GSI_GET_HWRPB:
12028 -- Grabs a copy of the HWRPB; surely not used.
12031 return ret;
12032 #endif
12033 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
12034 /* Alpha specific */
12035 case TARGET_NR_osf_setsysinfo:
12036 ret = -TARGET_EOPNOTSUPP;
12037 switch (arg1) {
12038 case TARGET_SSI_IEEE_FP_CONTROL:
12040 uint64_t swcr, fpcr;
12042 if (get_user_u64 (swcr, arg2)) {
12043 return -TARGET_EFAULT;
12047 * The kernel calls swcr_update_status to update the
12048 * status bits from the fpcr at every point that it
12049 * could be queried. Therefore, we store the status
12050 * bits only in FPCR.
12052 cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
12054 fpcr = cpu_alpha_load_fpcr(cpu_env);
12055 fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
12056 fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
12057 cpu_alpha_store_fpcr(cpu_env, fpcr);
12058 ret = 0;
12060 break;
12062 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
12064 uint64_t exc, fpcr, fex;
12066 if (get_user_u64(exc, arg2)) {
12067 return -TARGET_EFAULT;
12069 exc &= SWCR_STATUS_MASK;
12070 fpcr = cpu_alpha_load_fpcr(cpu_env);
12072 /* Old exceptions are not signaled. */
12073 fex = alpha_ieee_fpcr_to_swcr(fpcr);
12074 fex = exc & ~fex;
12075 fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
12076 fex &= (cpu_env)->swcr;
12078 /* Update the hardware fpcr. */
12079 fpcr |= alpha_ieee_swcr_to_fpcr(exc);
12080 cpu_alpha_store_fpcr(cpu_env, fpcr);
12082 if (fex) {
12083 int si_code = TARGET_FPE_FLTUNK;
12084 target_siginfo_t info;
12086 if (fex & SWCR_TRAP_ENABLE_DNO) {
12087 si_code = TARGET_FPE_FLTUND;
12089 if (fex & SWCR_TRAP_ENABLE_INE) {
12090 si_code = TARGET_FPE_FLTRES;
12092 if (fex & SWCR_TRAP_ENABLE_UNF) {
12093 si_code = TARGET_FPE_FLTUND;
12095 if (fex & SWCR_TRAP_ENABLE_OVF) {
12096 si_code = TARGET_FPE_FLTOVF;
12098 if (fex & SWCR_TRAP_ENABLE_DZE) {
12099 si_code = TARGET_FPE_FLTDIV;
12101 if (fex & SWCR_TRAP_ENABLE_INV) {
12102 si_code = TARGET_FPE_FLTINV;
12105 info.si_signo = SIGFPE;
12106 info.si_errno = 0;
12107 info.si_code = si_code;
12108 info._sifields._sigfault._addr = (cpu_env)->pc;
12109 queue_signal(cpu_env, info.si_signo,
12110 QEMU_SI_FAULT, &info);
12112 ret = 0;
12114 break;
12116 /* case SSI_NVPAIRS:
12117 -- Used with SSIN_UACPROC to enable unaligned accesses.
12118 case SSI_IEEE_STATE_AT_SIGNAL:
12119 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
12120 -- Not implemented in linux kernel
12123 return ret;
12124 #endif
12125 #ifdef TARGET_NR_osf_sigprocmask
12126 /* Alpha specific. */
12127 case TARGET_NR_osf_sigprocmask:
12129 abi_ulong mask;
12130 int how;
12131 sigset_t set, oldset;
12133 switch(arg1) {
12134 case TARGET_SIG_BLOCK:
12135 how = SIG_BLOCK;
12136 break;
12137 case TARGET_SIG_UNBLOCK:
12138 how = SIG_UNBLOCK;
12139 break;
12140 case TARGET_SIG_SETMASK:
12141 how = SIG_SETMASK;
12142 break;
12143 default:
12144 return -TARGET_EINVAL;
12146 mask = arg2;
12147 target_to_host_old_sigset(&set, &mask);
12148 ret = do_sigprocmask(how, &set, &oldset);
12149 if (!ret) {
12150 host_to_target_old_sigset(&mask, &oldset);
12151 ret = mask;
12154 return ret;
12155 #endif
12157 #ifdef TARGET_NR_getgid32
12158 case TARGET_NR_getgid32:
12159 return get_errno(getgid());
12160 #endif
12161 #ifdef TARGET_NR_geteuid32
12162 case TARGET_NR_geteuid32:
12163 return get_errno(geteuid());
12164 #endif
12165 #ifdef TARGET_NR_getegid32
12166 case TARGET_NR_getegid32:
12167 return get_errno(getegid());
12168 #endif
12169 #ifdef TARGET_NR_setreuid32
12170 case TARGET_NR_setreuid32:
12171 return get_errno(setreuid(arg1, arg2));
12172 #endif
12173 #ifdef TARGET_NR_setregid32
12174 case TARGET_NR_setregid32:
12175 return get_errno(setregid(arg1, arg2));
12176 #endif
12177 #ifdef TARGET_NR_getgroups32
12178 case TARGET_NR_getgroups32:
12179 { /* the same code as for TARGET_NR_getgroups */
12180 int gidsetsize = arg1;
12181 uint32_t *target_grouplist;
12182 g_autofree gid_t *grouplist = NULL;
12183 int i;
12185 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
12186 return -TARGET_EINVAL;
12188 if (gidsetsize > 0) {
12189 grouplist = g_try_new(gid_t, gidsetsize);
12190 if (!grouplist) {
12191 return -TARGET_ENOMEM;
12194 ret = get_errno(getgroups(gidsetsize, grouplist));
12195 if (!is_error(ret) && gidsetsize > 0) {
12196 target_grouplist = lock_user(VERIFY_WRITE, arg2,
12197 gidsetsize * 4, 0);
12198 if (!target_grouplist) {
12199 return -TARGET_EFAULT;
12201 for (i = 0; i < ret; i++) {
12202 target_grouplist[i] = tswap32(grouplist[i]);
12204 unlock_user(target_grouplist, arg2, gidsetsize * 4);
12206 return ret;
12208 #endif
12209 #ifdef TARGET_NR_setgroups32
12210 case TARGET_NR_setgroups32:
12211 { /* the same code as for TARGET_NR_setgroups */
12212 int gidsetsize = arg1;
12213 uint32_t *target_grouplist;
12214 g_autofree gid_t *grouplist = NULL;
12215 int i;
12217 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
12218 return -TARGET_EINVAL;
12220 if (gidsetsize > 0) {
12221 grouplist = g_try_new(gid_t, gidsetsize);
12222 if (!grouplist) {
12223 return -TARGET_ENOMEM;
12225 target_grouplist = lock_user(VERIFY_READ, arg2,
12226 gidsetsize * 4, 1);
12227 if (!target_grouplist) {
12228 return -TARGET_EFAULT;
12230 for (i = 0; i < gidsetsize; i++) {
12231 grouplist[i] = tswap32(target_grouplist[i]);
12233 unlock_user(target_grouplist, arg2, 0);
12235 return get_errno(setgroups(gidsetsize, grouplist));
12237 #endif
12238 #ifdef TARGET_NR_fchown32
12239 case TARGET_NR_fchown32:
12240 return get_errno(fchown(arg1, arg2, arg3));
12241 #endif
12242 #ifdef TARGET_NR_setresuid32
12243 case TARGET_NR_setresuid32:
12244 return get_errno(sys_setresuid(arg1, arg2, arg3));
12245 #endif
12246 #ifdef TARGET_NR_getresuid32
12247 case TARGET_NR_getresuid32:
12249 uid_t ruid, euid, suid;
12250 ret = get_errno(getresuid(&ruid, &euid, &suid));
12251 if (!is_error(ret)) {
12252 if (put_user_u32(ruid, arg1)
12253 || put_user_u32(euid, arg2)
12254 || put_user_u32(suid, arg3))
12255 return -TARGET_EFAULT;
12258 return ret;
12259 #endif
12260 #ifdef TARGET_NR_setresgid32
12261 case TARGET_NR_setresgid32:
12262 return get_errno(sys_setresgid(arg1, arg2, arg3));
12263 #endif
12264 #ifdef TARGET_NR_getresgid32
12265 case TARGET_NR_getresgid32:
12267 gid_t rgid, egid, sgid;
12268 ret = get_errno(getresgid(&rgid, &egid, &sgid));
12269 if (!is_error(ret)) {
12270 if (put_user_u32(rgid, arg1)
12271 || put_user_u32(egid, arg2)
12272 || put_user_u32(sgid, arg3))
12273 return -TARGET_EFAULT;
12276 return ret;
12277 #endif
12278 #ifdef TARGET_NR_chown32
12279 case TARGET_NR_chown32:
12280 if (!(p = lock_user_string(arg1)))
12281 return -TARGET_EFAULT;
12282 ret = get_errno(chown(p, arg2, arg3));
12283 unlock_user(p, arg1, 0);
12284 return ret;
12285 #endif
12286 #ifdef TARGET_NR_setuid32
12287 case TARGET_NR_setuid32:
12288 return get_errno(sys_setuid(arg1));
12289 #endif
12290 #ifdef TARGET_NR_setgid32
12291 case TARGET_NR_setgid32:
12292 return get_errno(sys_setgid(arg1));
12293 #endif
12294 #ifdef TARGET_NR_setfsuid32
12295 case TARGET_NR_setfsuid32:
12296 return get_errno(setfsuid(arg1));
12297 #endif
12298 #ifdef TARGET_NR_setfsgid32
12299 case TARGET_NR_setfsgid32:
12300 return get_errno(setfsgid(arg1));
12301 #endif
12302 #ifdef TARGET_NR_mincore
12303 case TARGET_NR_mincore:
12305 void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
12306 if (!a) {
12307 return -TARGET_ENOMEM;
12309 p = lock_user_string(arg3);
12310 if (!p) {
12311 ret = -TARGET_EFAULT;
12312 } else {
12313 ret = get_errno(mincore(a, arg2, p));
12314 unlock_user(p, arg3, ret);
12316 unlock_user(a, arg1, 0);
12318 return ret;
12319 #endif
12320 #ifdef TARGET_NR_arm_fadvise64_64
12321 case TARGET_NR_arm_fadvise64_64:
12322 /* arm_fadvise64_64 looks like fadvise64_64 but
12323 * with different argument order: fd, advice, offset, len
12324 * rather than the usual fd, offset, len, advice.
12325 * Note that offset and len are both 64-bit so appear as
12326 * pairs of 32-bit registers.
12328 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
12329 target_offset64(arg5, arg6), arg2);
12330 return -host_to_target_errno(ret);
12331 #endif
12333 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12335 #ifdef TARGET_NR_fadvise64_64
12336 case TARGET_NR_fadvise64_64:
12337 #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
12338 /* 6 args: fd, advice, offset (high, low), len (high, low) */
12339 ret = arg2;
12340 arg2 = arg3;
12341 arg3 = arg4;
12342 arg4 = arg5;
12343 arg5 = arg6;
12344 arg6 = ret;
12345 #else
12346 /* 6 args: fd, offset (high, low), len (high, low), advice */
12347 if (regpairs_aligned(cpu_env, num)) {
12348 /* offset is in (3,4), len in (5,6) and advice in 7 */
12349 arg2 = arg3;
12350 arg3 = arg4;
12351 arg4 = arg5;
12352 arg5 = arg6;
12353 arg6 = arg7;
12355 #endif
12356 ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
12357 target_offset64(arg4, arg5), arg6);
12358 return -host_to_target_errno(ret);
12359 #endif
12361 #ifdef TARGET_NR_fadvise64
12362 case TARGET_NR_fadvise64:
12363 /* 5 args: fd, offset (high, low), len, advice */
12364 if (regpairs_aligned(cpu_env, num)) {
12365 /* offset is in (3,4), len in 5 and advice in 6 */
12366 arg2 = arg3;
12367 arg3 = arg4;
12368 arg4 = arg5;
12369 arg5 = arg6;
12371 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
12372 return -host_to_target_errno(ret);
12373 #endif
12375 #else /* not a 32-bit ABI */
12376 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
12377 #ifdef TARGET_NR_fadvise64_64
12378 case TARGET_NR_fadvise64_64:
12379 #endif
12380 #ifdef TARGET_NR_fadvise64
12381 case TARGET_NR_fadvise64:
12382 #endif
12383 #ifdef TARGET_S390X
12384 switch (arg4) {
12385 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
12386 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
12387 case 6: arg4 = POSIX_FADV_DONTNEED; break;
12388 case 7: arg4 = POSIX_FADV_NOREUSE; break;
12389 default: break;
12391 #endif
12392 return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
12393 #endif
12394 #endif /* end of 64-bit ABI fadvise handling */
12396 #ifdef TARGET_NR_madvise
12397 case TARGET_NR_madvise:
12398 return target_madvise(arg1, arg2, arg3);
12399 #endif
12400 #ifdef TARGET_NR_fcntl64
12401 case TARGET_NR_fcntl64:
12403 int cmd;
12404 struct flock64 fl;
12405 from_flock64_fn *copyfrom = copy_from_user_flock64;
12406 to_flock64_fn *copyto = copy_to_user_flock64;
12408 #ifdef TARGET_ARM
12409 if (!cpu_env->eabi) {
12410 copyfrom = copy_from_user_oabi_flock64;
12411 copyto = copy_to_user_oabi_flock64;
12413 #endif
12415 cmd = target_to_host_fcntl_cmd(arg2);
12416 if (cmd == -TARGET_EINVAL) {
12417 return cmd;
12420 switch(arg2) {
12421 case TARGET_F_GETLK64:
12422 ret = copyfrom(&fl, arg3);
12423 if (ret) {
12424 break;
12426 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
12427 if (ret == 0) {
12428 ret = copyto(arg3, &fl);
12430 break;
12432 case TARGET_F_SETLK64:
12433 case TARGET_F_SETLKW64:
12434 ret = copyfrom(&fl, arg3);
12435 if (ret) {
12436 break;
12438 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
12439 break;
12440 default:
12441 ret = do_fcntl(arg1, arg2, arg3);
12442 break;
12444 return ret;
12446 #endif
12447 #ifdef TARGET_NR_cacheflush
12448 case TARGET_NR_cacheflush:
12449 /* self-modifying code is handled automatically, so nothing needed */
12450 return 0;
12451 #endif
12452 #ifdef TARGET_NR_getpagesize
12453 case TARGET_NR_getpagesize:
12454 return TARGET_PAGE_SIZE;
12455 #endif
12456 case TARGET_NR_gettid:
12457 return get_errno(sys_gettid());
12458 #ifdef TARGET_NR_readahead
12459 case TARGET_NR_readahead:
12460 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12461 if (regpairs_aligned(cpu_env, num)) {
12462 arg2 = arg3;
12463 arg3 = arg4;
12464 arg4 = arg5;
12466 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
12467 #else
12468 ret = get_errno(readahead(arg1, arg2, arg3));
12469 #endif
12470 return ret;
12471 #endif
12472 #ifdef CONFIG_ATTR
12473 #ifdef TARGET_NR_setxattr
12474 case TARGET_NR_listxattr:
12475 case TARGET_NR_llistxattr:
12477 void *b = 0;
12478 if (arg2) {
12479 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12480 if (!b) {
12481 return -TARGET_EFAULT;
12484 p = lock_user_string(arg1);
12485 if (p) {
12486 if (num == TARGET_NR_listxattr) {
12487 ret = get_errno(listxattr(p, b, arg3));
12488 } else {
12489 ret = get_errno(llistxattr(p, b, arg3));
12491 } else {
12492 ret = -TARGET_EFAULT;
12494 unlock_user(p, arg1, 0);
12495 unlock_user(b, arg2, arg3);
12496 return ret;
12498 case TARGET_NR_flistxattr:
12500 void *b = 0;
12501 if (arg2) {
12502 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12503 if (!b) {
12504 return -TARGET_EFAULT;
12507 ret = get_errno(flistxattr(arg1, b, arg3));
12508 unlock_user(b, arg2, arg3);
12509 return ret;
12511 case TARGET_NR_setxattr:
12512 case TARGET_NR_lsetxattr:
12514 void *n, *v = 0;
12515 if (arg3) {
12516 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12517 if (!v) {
12518 return -TARGET_EFAULT;
12521 p = lock_user_string(arg1);
12522 n = lock_user_string(arg2);
12523 if (p && n) {
12524 if (num == TARGET_NR_setxattr) {
12525 ret = get_errno(setxattr(p, n, v, arg4, arg5));
12526 } else {
12527 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
12529 } else {
12530 ret = -TARGET_EFAULT;
12532 unlock_user(p, arg1, 0);
12533 unlock_user(n, arg2, 0);
12534 unlock_user(v, arg3, 0);
12536 return ret;
12537 case TARGET_NR_fsetxattr:
12539 void *n, *v = 0;
12540 if (arg3) {
12541 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12542 if (!v) {
12543 return -TARGET_EFAULT;
12546 n = lock_user_string(arg2);
12547 if (n) {
12548 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
12549 } else {
12550 ret = -TARGET_EFAULT;
12552 unlock_user(n, arg2, 0);
12553 unlock_user(v, arg3, 0);
12555 return ret;
12556 case TARGET_NR_getxattr:
12557 case TARGET_NR_lgetxattr:
12559 void *n, *v = 0;
12560 if (arg3) {
12561 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12562 if (!v) {
12563 return -TARGET_EFAULT;
12566 p = lock_user_string(arg1);
12567 n = lock_user_string(arg2);
12568 if (p && n) {
12569 if (num == TARGET_NR_getxattr) {
12570 ret = get_errno(getxattr(p, n, v, arg4));
12571 } else {
12572 ret = get_errno(lgetxattr(p, n, v, arg4));
12574 } else {
12575 ret = -TARGET_EFAULT;
12577 unlock_user(p, arg1, 0);
12578 unlock_user(n, arg2, 0);
12579 unlock_user(v, arg3, arg4);
12581 return ret;
12582 case TARGET_NR_fgetxattr:
12584 void *n, *v = 0;
12585 if (arg3) {
12586 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12587 if (!v) {
12588 return -TARGET_EFAULT;
12591 n = lock_user_string(arg2);
12592 if (n) {
12593 ret = get_errno(fgetxattr(arg1, n, v, arg4));
12594 } else {
12595 ret = -TARGET_EFAULT;
12597 unlock_user(n, arg2, 0);
12598 unlock_user(v, arg3, arg4);
12600 return ret;
12601 case TARGET_NR_removexattr:
12602 case TARGET_NR_lremovexattr:
12604 void *n;
12605 p = lock_user_string(arg1);
12606 n = lock_user_string(arg2);
12607 if (p && n) {
12608 if (num == TARGET_NR_removexattr) {
12609 ret = get_errno(removexattr(p, n));
12610 } else {
12611 ret = get_errno(lremovexattr(p, n));
12613 } else {
12614 ret = -TARGET_EFAULT;
12616 unlock_user(p, arg1, 0);
12617 unlock_user(n, arg2, 0);
12619 return ret;
12620 case TARGET_NR_fremovexattr:
12622 void *n;
12623 n = lock_user_string(arg2);
12624 if (n) {
12625 ret = get_errno(fremovexattr(arg1, n));
12626 } else {
12627 ret = -TARGET_EFAULT;
12629 unlock_user(n, arg2, 0);
12631 return ret;
12632 #endif
12633 #endif /* CONFIG_ATTR */
12634 #ifdef TARGET_NR_set_thread_area
12635 case TARGET_NR_set_thread_area:
12636 #if defined(TARGET_MIPS)
12637 cpu_env->active_tc.CP0_UserLocal = arg1;
12638 return 0;
12639 #elif defined(TARGET_CRIS)
12640 if (arg1 & 0xff)
12641 ret = -TARGET_EINVAL;
12642 else {
12643 cpu_env->pregs[PR_PID] = arg1;
12644 ret = 0;
12646 return ret;
12647 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
12648 return do_set_thread_area(cpu_env, arg1);
12649 #elif defined(TARGET_M68K)
12651 TaskState *ts = get_task_state(cpu);
12652 ts->tp_value = arg1;
12653 return 0;
12655 #else
12656 return -TARGET_ENOSYS;
12657 #endif
12658 #endif
12659 #ifdef TARGET_NR_get_thread_area
12660 case TARGET_NR_get_thread_area:
12661 #if defined(TARGET_I386) && defined(TARGET_ABI32)
12662 return do_get_thread_area(cpu_env, arg1);
12663 #elif defined(TARGET_M68K)
12665 TaskState *ts = get_task_state(cpu);
12666 return ts->tp_value;
12668 #else
12669 return -TARGET_ENOSYS;
12670 #endif
12671 #endif
12672 #ifdef TARGET_NR_getdomainname
12673 case TARGET_NR_getdomainname:
12674 return -TARGET_ENOSYS;
12675 #endif
12677 #ifdef TARGET_NR_clock_settime
12678 case TARGET_NR_clock_settime:
12680 struct timespec ts;
12682 ret = target_to_host_timespec(&ts, arg2);
12683 if (!is_error(ret)) {
12684 ret = get_errno(clock_settime(arg1, &ts));
12686 return ret;
12688 #endif
12689 #ifdef TARGET_NR_clock_settime64
12690 case TARGET_NR_clock_settime64:
12692 struct timespec ts;
12694 ret = target_to_host_timespec64(&ts, arg2);
12695 if (!is_error(ret)) {
12696 ret = get_errno(clock_settime(arg1, &ts));
12698 return ret;
12700 #endif
12701 #ifdef TARGET_NR_clock_gettime
12702 case TARGET_NR_clock_gettime:
12704 struct timespec ts;
12705 ret = get_errno(clock_gettime(arg1, &ts));
12706 if (!is_error(ret)) {
12707 ret = host_to_target_timespec(arg2, &ts);
12709 return ret;
12711 #endif
12712 #ifdef TARGET_NR_clock_gettime64
12713 case TARGET_NR_clock_gettime64:
12715 struct timespec ts;
12716 ret = get_errno(clock_gettime(arg1, &ts));
12717 if (!is_error(ret)) {
12718 ret = host_to_target_timespec64(arg2, &ts);
12720 return ret;
12722 #endif
12723 #ifdef TARGET_NR_clock_getres
12724 case TARGET_NR_clock_getres:
12726 struct timespec ts;
12727 ret = get_errno(clock_getres(arg1, &ts));
12728 if (!is_error(ret)) {
12729 host_to_target_timespec(arg2, &ts);
12731 return ret;
12733 #endif
12734 #ifdef TARGET_NR_clock_getres_time64
12735 case TARGET_NR_clock_getres_time64:
12737 struct timespec ts;
12738 ret = get_errno(clock_getres(arg1, &ts));
12739 if (!is_error(ret)) {
12740 host_to_target_timespec64(arg2, &ts);
12742 return ret;
12744 #endif
12745 #ifdef TARGET_NR_clock_nanosleep
12746 case TARGET_NR_clock_nanosleep:
12748 struct timespec ts;
12749 if (target_to_host_timespec(&ts, arg3)) {
12750 return -TARGET_EFAULT;
12752 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12753 &ts, arg4 ? &ts : NULL));
12755 * if the call is interrupted by a signal handler, it fails
12756 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12757 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12759 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12760 host_to_target_timespec(arg4, &ts)) {
12761 return -TARGET_EFAULT;
12764 return ret;
12766 #endif
12767 #ifdef TARGET_NR_clock_nanosleep_time64
12768 case TARGET_NR_clock_nanosleep_time64:
12770 struct timespec ts;
12772 if (target_to_host_timespec64(&ts, arg3)) {
12773 return -TARGET_EFAULT;
12776 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12777 &ts, arg4 ? &ts : NULL));
12779 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12780 host_to_target_timespec64(arg4, &ts)) {
12781 return -TARGET_EFAULT;
12783 return ret;
12785 #endif
12787 #if defined(TARGET_NR_set_tid_address)
12788 case TARGET_NR_set_tid_address:
12790 TaskState *ts = get_task_state(cpu);
12791 ts->child_tidptr = arg1;
12792 /* do not call host set_tid_address() syscall, instead return tid() */
12793 return get_errno(sys_gettid());
12795 #endif
12797 case TARGET_NR_tkill:
12798 return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
12800 case TARGET_NR_tgkill:
12801 return get_errno(safe_tgkill((int)arg1, (int)arg2,
12802 target_to_host_signal(arg3)));
12804 #ifdef TARGET_NR_set_robust_list
12805 case TARGET_NR_set_robust_list:
12806 case TARGET_NR_get_robust_list:
12807 /* The ABI for supporting robust futexes has userspace pass
12808 * the kernel a pointer to a linked list which is updated by
12809 * userspace after the syscall; the list is walked by the kernel
12810 * when the thread exits. Since the linked list in QEMU guest
12811 * memory isn't a valid linked list for the host and we have
12812 * no way to reliably intercept the thread-death event, we can't
12813 * support these. Silently return ENOSYS so that guest userspace
12814 * falls back to a non-robust futex implementation (which should
12815 * be OK except in the corner case of the guest crashing while
12816 * holding a mutex that is shared with another process via
12817 * shared memory).
12819 return -TARGET_ENOSYS;
12820 #endif
12822 #if defined(TARGET_NR_utimensat)
12823 case TARGET_NR_utimensat:
12825 struct timespec *tsp, ts[2];
12826 if (!arg3) {
12827 tsp = NULL;
12828 } else {
12829 if (target_to_host_timespec(ts, arg3)) {
12830 return -TARGET_EFAULT;
12832 if (target_to_host_timespec(ts + 1, arg3 +
12833 sizeof(struct target_timespec))) {
12834 return -TARGET_EFAULT;
12836 tsp = ts;
12838 if (!arg2)
12839 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
12840 else {
12841 if (!(p = lock_user_string(arg2))) {
12842 return -TARGET_EFAULT;
12844 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
12845 unlock_user(p, arg2, 0);
12848 return ret;
12849 #endif
12850 #ifdef TARGET_NR_utimensat_time64
12851 case TARGET_NR_utimensat_time64:
12853 struct timespec *tsp, ts[2];
12854 if (!arg3) {
12855 tsp = NULL;
12856 } else {
12857 if (target_to_host_timespec64(ts, arg3)) {
12858 return -TARGET_EFAULT;
12860 if (target_to_host_timespec64(ts + 1, arg3 +
12861 sizeof(struct target__kernel_timespec))) {
12862 return -TARGET_EFAULT;
12864 tsp = ts;
12866 if (!arg2)
12867 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
12868 else {
12869 p = lock_user_string(arg2);
12870 if (!p) {
12871 return -TARGET_EFAULT;
12873 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
12874 unlock_user(p, arg2, 0);
12877 return ret;
12878 #endif
12879 #ifdef TARGET_NR_futex
12880 case TARGET_NR_futex:
12881 return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
12882 #endif
12883 #ifdef TARGET_NR_futex_time64
12884 case TARGET_NR_futex_time64:
12885 return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
12886 #endif
12887 #ifdef CONFIG_INOTIFY
12888 #if defined(TARGET_NR_inotify_init)
12889 case TARGET_NR_inotify_init:
12890 ret = get_errno(inotify_init());
12891 if (ret >= 0) {
12892 fd_trans_register(ret, &target_inotify_trans);
12894 return ret;
12895 #endif
12896 #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
12897 case TARGET_NR_inotify_init1:
12898 ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
12899 fcntl_flags_tbl)));
12900 if (ret >= 0) {
12901 fd_trans_register(ret, &target_inotify_trans);
12903 return ret;
12904 #endif
12905 #if defined(TARGET_NR_inotify_add_watch)
12906 case TARGET_NR_inotify_add_watch:
12907 p = lock_user_string(arg2);
12908 ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
12909 unlock_user(p, arg2, 0);
12910 return ret;
12911 #endif
12912 #if defined(TARGET_NR_inotify_rm_watch)
12913 case TARGET_NR_inotify_rm_watch:
12914 return get_errno(inotify_rm_watch(arg1, arg2));
12915 #endif
12916 #endif
12918 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
12919 case TARGET_NR_mq_open:
12921 struct mq_attr posix_mq_attr;
12922 struct mq_attr *pposix_mq_attr;
12923 int host_flags;
12925 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
12926 pposix_mq_attr = NULL;
12927 if (arg4) {
12928 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
12929 return -TARGET_EFAULT;
12931 pposix_mq_attr = &posix_mq_attr;
12933 p = lock_user_string(arg1 - 1);
12934 if (!p) {
12935 return -TARGET_EFAULT;
12937 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
12938 unlock_user (p, arg1, 0);
12940 return ret;
12942 case TARGET_NR_mq_unlink:
12943 p = lock_user_string(arg1 - 1);
12944 if (!p) {
12945 return -TARGET_EFAULT;
12947 ret = get_errno(mq_unlink(p));
12948 unlock_user (p, arg1, 0);
12949 return ret;
12951 #ifdef TARGET_NR_mq_timedsend
12952 case TARGET_NR_mq_timedsend:
12954 struct timespec ts;
12956 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12957 if (arg5 != 0) {
12958 if (target_to_host_timespec(&ts, arg5)) {
12959 return -TARGET_EFAULT;
12961 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
12962 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12963 return -TARGET_EFAULT;
12965 } else {
12966 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
12968 unlock_user (p, arg2, arg3);
12970 return ret;
12971 #endif
12972 #ifdef TARGET_NR_mq_timedsend_time64
12973 case TARGET_NR_mq_timedsend_time64:
12975 struct timespec ts;
12977 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12978 if (arg5 != 0) {
12979 if (target_to_host_timespec64(&ts, arg5)) {
12980 return -TARGET_EFAULT;
12982 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
12983 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12984 return -TARGET_EFAULT;
12986 } else {
12987 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
12989 unlock_user(p, arg2, arg3);
12991 return ret;
12992 #endif
12994 #ifdef TARGET_NR_mq_timedreceive
12995 case TARGET_NR_mq_timedreceive:
12997 struct timespec ts;
12998 unsigned int prio;
13000 p = lock_user (VERIFY_READ, arg2, arg3, 1);
13001 if (arg5 != 0) {
13002 if (target_to_host_timespec(&ts, arg5)) {
13003 return -TARGET_EFAULT;
13005 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13006 &prio, &ts));
13007 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
13008 return -TARGET_EFAULT;
13010 } else {
13011 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13012 &prio, NULL));
13014 unlock_user (p, arg2, arg3);
13015 if (arg4 != 0)
13016 put_user_u32(prio, arg4);
13018 return ret;
13019 #endif
13020 #ifdef TARGET_NR_mq_timedreceive_time64
13021 case TARGET_NR_mq_timedreceive_time64:
13023 struct timespec ts;
13024 unsigned int prio;
13026 p = lock_user(VERIFY_READ, arg2, arg3, 1);
13027 if (arg5 != 0) {
13028 if (target_to_host_timespec64(&ts, arg5)) {
13029 return -TARGET_EFAULT;
13031 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13032 &prio, &ts));
13033 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
13034 return -TARGET_EFAULT;
13036 } else {
13037 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13038 &prio, NULL));
13040 unlock_user(p, arg2, arg3);
13041 if (arg4 != 0) {
13042 put_user_u32(prio, arg4);
13045 return ret;
13046 #endif
13048 /* Not implemented for now... */
13049 /* case TARGET_NR_mq_notify: */
13050 /* break; */
13052 case TARGET_NR_mq_getsetattr:
13054 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
13055 ret = 0;
13056 if (arg2 != 0) {
13057 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
13058 ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
13059 &posix_mq_attr_out));
13060 } else if (arg3 != 0) {
13061 ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
13063 if (ret == 0 && arg3 != 0) {
13064 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
13067 return ret;
13068 #endif
13070 #ifdef CONFIG_SPLICE
13071 #ifdef TARGET_NR_tee
13072 case TARGET_NR_tee:
13074 ret = get_errno(tee(arg1,arg2,arg3,arg4));
13076 return ret;
13077 #endif
13078 #ifdef TARGET_NR_splice
13079 case TARGET_NR_splice:
13081 loff_t loff_in, loff_out;
13082 loff_t *ploff_in = NULL, *ploff_out = NULL;
13083 if (arg2) {
13084 if (get_user_u64(loff_in, arg2)) {
13085 return -TARGET_EFAULT;
13087 ploff_in = &loff_in;
13089 if (arg4) {
13090 if (get_user_u64(loff_out, arg4)) {
13091 return -TARGET_EFAULT;
13093 ploff_out = &loff_out;
13095 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
13096 if (arg2) {
13097 if (put_user_u64(loff_in, arg2)) {
13098 return -TARGET_EFAULT;
13101 if (arg4) {
13102 if (put_user_u64(loff_out, arg4)) {
13103 return -TARGET_EFAULT;
13107 return ret;
13108 #endif
13109 #ifdef TARGET_NR_vmsplice
13110 case TARGET_NR_vmsplice:
13112 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
13113 if (vec != NULL) {
13114 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
13115 unlock_iovec(vec, arg2, arg3, 0);
13116 } else {
13117 ret = -host_to_target_errno(errno);
13120 return ret;
13121 #endif
13122 #endif /* CONFIG_SPLICE */
13123 #ifdef CONFIG_EVENTFD
13124 #if defined(TARGET_NR_eventfd)
13125 case TARGET_NR_eventfd:
13126 ret = get_errno(eventfd(arg1, 0));
13127 if (ret >= 0) {
13128 fd_trans_register(ret, &target_eventfd_trans);
13130 return ret;
13131 #endif
13132 #if defined(TARGET_NR_eventfd2)
13133 case TARGET_NR_eventfd2:
13135 int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
13136 if (arg2 & TARGET_O_NONBLOCK) {
13137 host_flags |= O_NONBLOCK;
13139 if (arg2 & TARGET_O_CLOEXEC) {
13140 host_flags |= O_CLOEXEC;
13142 ret = get_errno(eventfd(arg1, host_flags));
13143 if (ret >= 0) {
13144 fd_trans_register(ret, &target_eventfd_trans);
13146 return ret;
13148 #endif
13149 #endif /* CONFIG_EVENTFD */
13150 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
13151 case TARGET_NR_fallocate:
13152 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13153 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
13154 target_offset64(arg5, arg6)));
13155 #else
13156 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
13157 #endif
13158 return ret;
13159 #endif
13160 #if defined(CONFIG_SYNC_FILE_RANGE)
13161 #if defined(TARGET_NR_sync_file_range)
13162 case TARGET_NR_sync_file_range:
13163 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13164 #if defined(TARGET_MIPS)
13165 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13166 target_offset64(arg5, arg6), arg7));
13167 #else
13168 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
13169 target_offset64(arg4, arg5), arg6));
13170 #endif /* !TARGET_MIPS */
13171 #else
13172 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
13173 #endif
13174 return ret;
13175 #endif
13176 #if defined(TARGET_NR_sync_file_range2) || \
13177 defined(TARGET_NR_arm_sync_file_range)
13178 #if defined(TARGET_NR_sync_file_range2)
13179 case TARGET_NR_sync_file_range2:
13180 #endif
13181 #if defined(TARGET_NR_arm_sync_file_range)
13182 case TARGET_NR_arm_sync_file_range:
13183 #endif
13184 /* This is like sync_file_range but the arguments are reordered */
13185 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13186 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13187 target_offset64(arg5, arg6), arg2));
13188 #else
13189 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
13190 #endif
13191 return ret;
13192 #endif
13193 #endif
13194 #if defined(TARGET_NR_signalfd4)
13195 case TARGET_NR_signalfd4:
13196 return do_signalfd4(arg1, arg2, arg4);
13197 #endif
13198 #if defined(TARGET_NR_signalfd)
13199 case TARGET_NR_signalfd:
13200 return do_signalfd4(arg1, arg2, 0);
13201 #endif
13202 #if defined(CONFIG_EPOLL)
13203 #if defined(TARGET_NR_epoll_create)
13204 case TARGET_NR_epoll_create:
13205 return get_errno(epoll_create(arg1));
13206 #endif
13207 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
13208 case TARGET_NR_epoll_create1:
13209 return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
13210 #endif
13211 #if defined(TARGET_NR_epoll_ctl)
13212 case TARGET_NR_epoll_ctl:
13214 struct epoll_event ep;
13215 struct epoll_event *epp = 0;
13216 if (arg4) {
13217 if (arg2 != EPOLL_CTL_DEL) {
13218 struct target_epoll_event *target_ep;
13219 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
13220 return -TARGET_EFAULT;
13222 ep.events = tswap32(target_ep->events);
13224 * The epoll_data_t union is just opaque data to the kernel,
13225 * so we transfer all 64 bits across and need not worry what
13226 * actual data type it is.
13228 ep.data.u64 = tswap64(target_ep->data.u64);
13229 unlock_user_struct(target_ep, arg4, 0);
13232 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
13233 * non-null pointer, even though this argument is ignored.
13236 epp = &ep;
13238 return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
13240 #endif
13242 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
13243 #if defined(TARGET_NR_epoll_wait)
13244 case TARGET_NR_epoll_wait:
13245 #endif
13246 #if defined(TARGET_NR_epoll_pwait)
13247 case TARGET_NR_epoll_pwait:
13248 #endif
13250 struct target_epoll_event *target_ep;
13251 struct epoll_event *ep;
13252 int epfd = arg1;
13253 int maxevents = arg3;
13254 int timeout = arg4;
13256 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
13257 return -TARGET_EINVAL;
13260 target_ep = lock_user(VERIFY_WRITE, arg2,
13261 maxevents * sizeof(struct target_epoll_event), 1);
13262 if (!target_ep) {
13263 return -TARGET_EFAULT;
13266 ep = g_try_new(struct epoll_event, maxevents);
13267 if (!ep) {
13268 unlock_user(target_ep, arg2, 0);
13269 return -TARGET_ENOMEM;
13272 switch (num) {
13273 #if defined(TARGET_NR_epoll_pwait)
13274 case TARGET_NR_epoll_pwait:
13276 sigset_t *set = NULL;
13278 if (arg5) {
13279 ret = process_sigsuspend_mask(&set, arg5, arg6);
13280 if (ret != 0) {
13281 break;
13285 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13286 set, SIGSET_T_SIZE));
13288 if (set) {
13289 finish_sigsuspend_mask(ret);
13291 break;
13293 #endif
13294 #if defined(TARGET_NR_epoll_wait)
13295 case TARGET_NR_epoll_wait:
13296 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13297 NULL, 0));
13298 break;
13299 #endif
13300 default:
13301 ret = -TARGET_ENOSYS;
13303 if (!is_error(ret)) {
13304 int i;
13305 for (i = 0; i < ret; i++) {
13306 target_ep[i].events = tswap32(ep[i].events);
13307 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
13309 unlock_user(target_ep, arg2,
13310 ret * sizeof(struct target_epoll_event));
13311 } else {
13312 unlock_user(target_ep, arg2, 0);
13314 g_free(ep);
13315 return ret;
13317 #endif
13318 #endif
13319 #ifdef TARGET_NR_prlimit64
13320 case TARGET_NR_prlimit64:
13322 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
13323 struct target_rlimit64 *target_rnew, *target_rold;
13324 struct host_rlimit64 rnew, rold, *rnewp = 0;
13325 int resource = target_to_host_resource(arg2);
13327 if (arg3 && (resource != RLIMIT_AS &&
13328 resource != RLIMIT_DATA &&
13329 resource != RLIMIT_STACK)) {
13330 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
13331 return -TARGET_EFAULT;
13333 __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
13334 __get_user(rnew.rlim_max, &target_rnew->rlim_max);
13335 unlock_user_struct(target_rnew, arg3, 0);
13336 rnewp = &rnew;
13339 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
13340 if (!is_error(ret) && arg4) {
13341 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
13342 return -TARGET_EFAULT;
13344 __put_user(rold.rlim_cur, &target_rold->rlim_cur);
13345 __put_user(rold.rlim_max, &target_rold->rlim_max);
13346 unlock_user_struct(target_rold, arg4, 1);
13348 return ret;
13350 #endif
13351 #ifdef TARGET_NR_gethostname
13352 case TARGET_NR_gethostname:
13354 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
13355 if (name) {
13356 ret = get_errno(gethostname(name, arg2));
13357 unlock_user(name, arg1, arg2);
13358 } else {
13359 ret = -TARGET_EFAULT;
13361 return ret;
13363 #endif
13364 #ifdef TARGET_NR_atomic_cmpxchg_32
13365 case TARGET_NR_atomic_cmpxchg_32:
13367 /* should use start_exclusive from main.c */
13368 abi_ulong mem_value;
13369 if (get_user_u32(mem_value, arg6)) {
13370 target_siginfo_t info;
13371 info.si_signo = SIGSEGV;
13372 info.si_errno = 0;
13373 info.si_code = TARGET_SEGV_MAPERR;
13374 info._sifields._sigfault._addr = arg6;
13375 queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
13376 ret = 0xdeadbeef;
13379 if (mem_value == arg2)
13380 put_user_u32(arg1, arg6);
13381 return mem_value;
13383 #endif
13384 #ifdef TARGET_NR_atomic_barrier
13385 case TARGET_NR_atomic_barrier:
13386 /* Like the kernel implementation and the
13387 qemu arm barrier, no-op this? */
13388 return 0;
13389 #endif
13391 #ifdef TARGET_NR_timer_create
13392 case TARGET_NR_timer_create:
13394 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
13396 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
13398 int clkid = arg1;
13399 int timer_index = next_free_host_timer();
13401 if (timer_index < 0) {
13402 ret = -TARGET_EAGAIN;
13403 } else {
13404 timer_t *phtimer = g_posix_timers + timer_index;
13406 if (arg2) {
13407 phost_sevp = &host_sevp;
13408 ret = target_to_host_sigevent(phost_sevp, arg2);
13409 if (ret != 0) {
13410 free_host_timer_slot(timer_index);
13411 return ret;
13415 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
13416 if (ret) {
13417 free_host_timer_slot(timer_index);
13418 } else {
13419 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
13420 timer_delete(*phtimer);
13421 free_host_timer_slot(timer_index);
13422 return -TARGET_EFAULT;
13426 return ret;
13428 #endif
13430 #ifdef TARGET_NR_timer_settime
13431 case TARGET_NR_timer_settime:
13433 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
13434 * struct itimerspec * old_value */
13435 target_timer_t timerid = get_timer_id(arg1);
13437 if (timerid < 0) {
13438 ret = timerid;
13439 } else if (arg3 == 0) {
13440 ret = -TARGET_EINVAL;
13441 } else {
13442 timer_t htimer = g_posix_timers[timerid];
13443 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13445 if (target_to_host_itimerspec(&hspec_new, arg3)) {
13446 return -TARGET_EFAULT;
13448 ret = get_errno(
13449 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
13450 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
13451 return -TARGET_EFAULT;
13454 return ret;
13456 #endif
13458 #ifdef TARGET_NR_timer_settime64
13459 case TARGET_NR_timer_settime64:
13461 target_timer_t timerid = get_timer_id(arg1);
13463 if (timerid < 0) {
13464 ret = timerid;
13465 } else if (arg3 == 0) {
13466 ret = -TARGET_EINVAL;
13467 } else {
13468 timer_t htimer = g_posix_timers[timerid];
13469 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13471 if (target_to_host_itimerspec64(&hspec_new, arg3)) {
13472 return -TARGET_EFAULT;
13474 ret = get_errno(
13475 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
13476 if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
13477 return -TARGET_EFAULT;
13480 return ret;
13482 #endif
13484 #ifdef TARGET_NR_timer_gettime
13485 case TARGET_NR_timer_gettime:
13487 /* args: timer_t timerid, struct itimerspec *curr_value */
13488 target_timer_t timerid = get_timer_id(arg1);
13490 if (timerid < 0) {
13491 ret = timerid;
13492 } else if (!arg2) {
13493 ret = -TARGET_EFAULT;
13494 } else {
13495 timer_t htimer = g_posix_timers[timerid];
13496 struct itimerspec hspec;
13497 ret = get_errno(timer_gettime(htimer, &hspec));
13499 if (host_to_target_itimerspec(arg2, &hspec)) {
13500 ret = -TARGET_EFAULT;
13503 return ret;
13505 #endif
13507 #ifdef TARGET_NR_timer_gettime64
13508 case TARGET_NR_timer_gettime64:
13510 /* args: timer_t timerid, struct itimerspec64 *curr_value */
13511 target_timer_t timerid = get_timer_id(arg1);
13513 if (timerid < 0) {
13514 ret = timerid;
13515 } else if (!arg2) {
13516 ret = -TARGET_EFAULT;
13517 } else {
13518 timer_t htimer = g_posix_timers[timerid];
13519 struct itimerspec hspec;
13520 ret = get_errno(timer_gettime(htimer, &hspec));
13522 if (host_to_target_itimerspec64(arg2, &hspec)) {
13523 ret = -TARGET_EFAULT;
13526 return ret;
13528 #endif
13530 #ifdef TARGET_NR_timer_getoverrun
13531 case TARGET_NR_timer_getoverrun:
13533 /* args: timer_t timerid */
13534 target_timer_t timerid = get_timer_id(arg1);
13536 if (timerid < 0) {
13537 ret = timerid;
13538 } else {
13539 timer_t htimer = g_posix_timers[timerid];
13540 ret = get_errno(timer_getoverrun(htimer));
13542 return ret;
13544 #endif
13546 #ifdef TARGET_NR_timer_delete
13547 case TARGET_NR_timer_delete:
13549 /* args: timer_t timerid */
13550 target_timer_t timerid = get_timer_id(arg1);
13552 if (timerid < 0) {
13553 ret = timerid;
13554 } else {
13555 timer_t htimer = g_posix_timers[timerid];
13556 ret = get_errno(timer_delete(htimer));
13557 free_host_timer_slot(timerid);
13559 return ret;
13561 #endif
13563 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13564 case TARGET_NR_timerfd_create:
13565 ret = get_errno(timerfd_create(arg1,
13566 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
13567 if (ret >= 0) {
13568 fd_trans_register(ret, &target_timerfd_trans);
13570 return ret;
13571 #endif
13573 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13574 case TARGET_NR_timerfd_gettime:
13576 struct itimerspec its_curr;
13578 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13580 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
13581 return -TARGET_EFAULT;
13584 return ret;
13585 #endif
13587 #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13588 case TARGET_NR_timerfd_gettime64:
13590 struct itimerspec its_curr;
13592 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13594 if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
13595 return -TARGET_EFAULT;
13598 return ret;
13599 #endif
13601 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13602 case TARGET_NR_timerfd_settime:
13604 struct itimerspec its_new, its_old, *p_new;
13606 if (arg3) {
13607 if (target_to_host_itimerspec(&its_new, arg3)) {
13608 return -TARGET_EFAULT;
13610 p_new = &its_new;
13611 } else {
13612 p_new = NULL;
13615 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13617 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
13618 return -TARGET_EFAULT;
13621 return ret;
13622 #endif
13624 #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13625 case TARGET_NR_timerfd_settime64:
13627 struct itimerspec its_new, its_old, *p_new;
13629 if (arg3) {
13630 if (target_to_host_itimerspec64(&its_new, arg3)) {
13631 return -TARGET_EFAULT;
13633 p_new = &its_new;
13634 } else {
13635 p_new = NULL;
13638 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13640 if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
13641 return -TARGET_EFAULT;
13644 return ret;
13645 #endif
13647 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13648 case TARGET_NR_ioprio_get:
13649 return get_errno(ioprio_get(arg1, arg2));
13650 #endif
13652 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13653 case TARGET_NR_ioprio_set:
13654 return get_errno(ioprio_set(arg1, arg2, arg3));
13655 #endif
13657 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13658 case TARGET_NR_setns:
13659 return get_errno(setns(arg1, arg2));
13660 #endif
13661 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13662 case TARGET_NR_unshare:
13663 return get_errno(unshare(arg1));
13664 #endif
13665 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13666 case TARGET_NR_kcmp:
13667 return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
13668 #endif
13669 #ifdef TARGET_NR_swapcontext
13670 case TARGET_NR_swapcontext:
13671 /* PowerPC specific. */
13672 return do_swapcontext(cpu_env, arg1, arg2, arg3);
13673 #endif
13674 #ifdef TARGET_NR_memfd_create
13675 case TARGET_NR_memfd_create:
13676 p = lock_user_string(arg1);
13677 if (!p) {
13678 return -TARGET_EFAULT;
13680 ret = get_errno(memfd_create(p, arg2));
13681 fd_trans_unregister(ret);
13682 unlock_user(p, arg1, 0);
13683 return ret;
13684 #endif
13685 #if defined TARGET_NR_membarrier && defined __NR_membarrier
13686 case TARGET_NR_membarrier:
13687 return get_errno(membarrier(arg1, arg2));
13688 #endif
13690 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13691 case TARGET_NR_copy_file_range:
13693 loff_t inoff, outoff;
13694 loff_t *pinoff = NULL, *poutoff = NULL;
13696 if (arg2) {
13697 if (get_user_u64(inoff, arg2)) {
13698 return -TARGET_EFAULT;
13700 pinoff = &inoff;
13702 if (arg4) {
13703 if (get_user_u64(outoff, arg4)) {
13704 return -TARGET_EFAULT;
13706 poutoff = &outoff;
13708 /* Do not sign-extend the count parameter. */
13709 ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
13710 (abi_ulong)arg5, arg6));
13711 if (!is_error(ret) && ret > 0) {
13712 if (arg2) {
13713 if (put_user_u64(inoff, arg2)) {
13714 return -TARGET_EFAULT;
13717 if (arg4) {
13718 if (put_user_u64(outoff, arg4)) {
13719 return -TARGET_EFAULT;
13724 return ret;
13725 #endif
13727 #if defined(TARGET_NR_pivot_root)
13728 case TARGET_NR_pivot_root:
13730 void *p2;
13731 p = lock_user_string(arg1); /* new_root */
13732 p2 = lock_user_string(arg2); /* put_old */
13733 if (!p || !p2) {
13734 ret = -TARGET_EFAULT;
13735 } else {
13736 ret = get_errno(pivot_root(p, p2));
13738 unlock_user(p2, arg2, 0);
13739 unlock_user(p, arg1, 0);
13741 return ret;
13742 #endif
13744 #if defined(TARGET_NR_riscv_hwprobe)
13745 case TARGET_NR_riscv_hwprobe:
13746 return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
13747 #endif
13749 default:
13750 qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
13751 return -TARGET_ENOSYS;
13753 return ret;
13756 abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
13757 abi_long arg2, abi_long arg3, abi_long arg4,
13758 abi_long arg5, abi_long arg6, abi_long arg7,
13759 abi_long arg8)
13761 CPUState *cpu = env_cpu(cpu_env);
13762 abi_long ret;
13764 #ifdef DEBUG_ERESTARTSYS
13765 /* Debug-only code for exercising the syscall-restart code paths
13766 * in the per-architecture cpu main loops: restart every syscall
13767 * the guest makes once before letting it through.
13770 static bool flag;
13771 flag = !flag;
13772 if (flag) {
13773 return -QEMU_ERESTARTSYS;
13776 #endif
13778 record_syscall_start(cpu, num, arg1,
13779 arg2, arg3, arg4, arg5, arg6, arg7, arg8);
13781 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
13782 print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
13785 ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
13786 arg5, arg6, arg7, arg8);
13788 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
13789 print_syscall_ret(cpu_env, num, ret, arg1, arg2,
13790 arg3, arg4, arg5, arg6);
13793 record_syscall_return(cpu, num, ret);
13794 return ret;