blob: 5950222a77e0be1feadeb8f0c6571d39979b70bb [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
2 * Linux syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
Eduardo Habkostd5b3a9b2009-06-09 18:26:31 -030019#define _ATFILE_SOURCE
Peter Maydelld39594e2016-01-26 18:17:02 +000020#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020021#include "qemu/cutils.h"
22#include "qemu/path.h"
Shu-Chun Weng9bdfa4d2019-08-19 11:09:47 -070023#include "qemu/memfd.h"
Markus Armbrusterdc5e9ac2019-08-12 07:23:49 +020024#include "qemu/queue.h"
bellard31e31b82003-02-18 22:55:36 +000025#include <elf.h>
26#include <endian.h>
John Spencerc56dc772012-12-10 07:59:46 +010027#include <grp.h>
thsd08d3bb2007-03-19 13:09:22 +000028#include <sys/ipc.h>
29#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000030#include <sys/wait.h>
bellard31e31b82003-02-18 22:55:36 +000031#include <sys/mount.h>
John Spencer586b0be2012-12-26 00:49:49 +010032#include <sys/file.h>
33#include <sys/fsuid.h>
34#include <sys/personality.h>
ths39b9aae2007-02-11 18:36:44 +000035#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000036#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000037#include <sys/swap.h>
Peter Maydelle0eb2102014-03-17 12:15:35 +000038#include <linux/capability.h>
bellard31e31b82003-02-18 22:55:36 +000039#include <sched.h>
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020040#include <sys/timex.h>
bellard31e31b82003-02-18 22:55:36 +000041#include <sys/socket.h>
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +020042#include <linux/sockios.h>
aurel32607175e2009-04-15 16:11:59 +000043#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000044#include <sys/uio.h>
Felix Janda0839f112016-09-30 19:40:21 -040045#include <poll.h>
bellard32f36bc2003-03-30 21:29:48 +000046#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000047#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000048#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000049#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000050#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000051#include <sys/sysinfo.h>
Laurent Viviere36800c2015-10-02 14:48:09 +020052#include <sys/signalfd.h>
bellard72f03902003-02-18 23:33:18 +000053//#include <sys/user.h>
Shu-Chun Weng22db1212020-12-18 11:32:12 -080054#include <netinet/in.h>
bellard8853f862004-02-22 14:57:26 +000055#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000056#include <netinet/tcp.h>
Shu-Chun Wengfe51b0a2020-12-18 11:32:11 -080057#include <netinet/udp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020058#include <linux/wireless.h>
Jing Huang920394d2012-07-24 13:59:23 +000059#include <linux/icmp.h>
Helge Delleree1ac3a2017-02-18 23:31:30 +010060#include <linux/icmpv6.h>
Shu-Chun Weng6addf062020-09-28 18:48:01 -070061#include <linux/if_tun.h>
Shu-Chun Weng22db1212020-12-18 11:32:12 -080062#include <linux/in6.h>
Helge Delleree1ac3a2017-02-18 23:31:30 +010063#include <linux/errqueue.h>
Marco A L Barbosad6d6d6f2017-10-05 10:55:30 -030064#include <linux/random.h>
Riku Voipiod80a1902014-10-01 16:05:46 +030065#ifdef CONFIG_TIMERFD
66#include <sys/timerfd.h>
67#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030068#ifdef CONFIG_EVENTFD
69#include <sys/eventfd.h>
70#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000071#ifdef CONFIG_EPOLL
72#include <sys/epoll.h>
73#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070074#ifdef CONFIG_ATTR
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010075#include "qemu/xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070076#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000077#ifdef CONFIG_SENDFILE
78#include <sys/sendfile.h>
79#endif
Thomas Huth4a9d5f82020-11-18 18:10:51 +010080#ifdef HAVE_SYS_KCOV_H
Aleksandar Markovicbd27e672020-01-16 23:49:50 +010081#include <sys/kcov.h>
82#endif
bellard31e31b82003-02-18 22:55:36 +000083
84#define termios host_termios
85#define winsize host_winsize
86#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000087#define sgttyb host_sgttyb /* same as target */
88#define tchars host_tchars /* same as target */
89#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000090
91#include <linux/termios.h>
92#include <linux/unistd.h>
bellard31e31b82003-02-18 22:55:36 +000093#include <linux/cdrom.h>
94#include <linux/hdreg.h>
95#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000096#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000097#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030098#include <linux/fs.h>
Yunqiang Suab22b4d2019-09-04 14:59:24 +020099#include <linux/fd.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000100#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +0000101#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000102#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200103#include <linux/fb.h>
Cortland Tölva6c753a62018-10-08 09:35:20 -0700104#if defined(CONFIG_USBFS)
105#include <linux/usbdevice_fs.h>
Cortland Tölvaa1333672018-10-08 09:35:21 -0700106#include <linux/usb/ch9.h>
Cortland Tölva6c753a62018-10-08 09:35:20 -0700107#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200108#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +0100109#include <linux/dm-ioctl.h>
Laurent Vivierc07ecc62013-01-07 11:40:06 +0000110#include <linux/reboot.h>
Laurent Vivier7ff7b662013-07-02 14:04:12 +0100111#include <linux/route.h>
Laurent Vivierf57d4192013-08-30 01:46:41 +0200112#include <linux/filter.h>
Andreas Färberfff8c532014-01-18 07:38:30 +0100113#include <linux/blkpg.h>
Laurent Viviera82ea932016-06-27 00:18:22 +0200114#include <netpacket/packet.h>
Laurent Vivier6c5b5642016-05-22 18:56:19 +0200115#include <linux/netlink.h>
Yunqiang Suf31dddd2019-06-19 16:17:11 +0200116#include <linux/if_alg.h>
Filip Bozuta68365f92020-01-15 20:36:35 +0100117#include <linux/rtc.h>
Filip Bozuta1c4c6fc2020-01-15 20:36:41 +0100118#include <sound/asound.h>
Thomas Huth48f670e2020-11-18 18:10:52 +0100119#ifdef HAVE_BTRFS_H
Filip Bozutad6092e02020-08-23 21:50:07 +0200120#include <linux/btrfs.h>
121#endif
Chen Gange865b972020-06-05 09:32:21 +0800122#ifdef HAVE_DRM_H
123#include <libdrm/drm.h>
Chen Gang913b03c2020-08-02 21:39:38 +0800124#include <libdrm/i915_drm.h>
Chen Gange865b972020-06-05 09:32:21 +0800125#endif
pbrookd7e40362008-05-23 16:06:43 +0000126#include "linux_loop.h"
Riku Voipio18cb0082014-02-19 12:59:58 +0200127#include "uname.h"
bellard31e31b82003-02-18 22:55:36 +0000128
bellard3ef693a2003-03-23 20:17:16 +0000129#include "qemu.h"
Peter Maydell3b249d22021-09-08 16:44:03 +0100130#include "user-internals.h"
Peter Maydella44d57a2021-09-08 16:43:58 +0100131#include "strace.h"
Peter Maydell2113aed2021-09-08 16:43:59 +0100132#include "signal-common.h"
Peter Maydell3ad0a762021-09-08 16:44:00 +0100133#include "loader.h"
Peter Maydell5423e6d2021-09-08 16:44:01 +0100134#include "user-mmap.h"
Richard Hendersonbbf15aa2021-11-17 16:14:00 +0100135#include "user/safe-syscall.h"
Richard Henderson5ebdd772019-03-14 13:10:53 -0700136#include "qemu/guest-random.h"
Alex Bennée01ef6b92020-04-03 20:11:46 +0100137#include "qemu/selfmap.h"
Emilio G. Cotac36f7a62018-10-21 13:27:44 -0400138#include "user/syscall-trace.h"
Richard Henderson5da40632021-11-17 15:46:05 +0100139#include "special-errno.h"
Richard Henderson51977e22019-03-12 19:22:20 -0700140#include "qapi/error.h"
Laurent Vivierf7e6a402018-08-24 00:22:15 +0200141#include "fd-trans.h"
Philippe Mathieu-Daudédcb32f12020-01-01 12:23:00 +0100142#include "tcg/tcg.h"
bellard31e31b82003-02-18 22:55:36 +0000143
Peter Maydell5ea2fc82016-08-02 18:41:27 +0100144#ifndef CLONE_IO
145#define CLONE_IO 0x80000000 /* Clone io context */
146#endif
147
148/* We can't directly call the host clone syscall, because this will
149 * badly confuse libc (breaking mutexes, for example). So we must
150 * divide clone flags into:
151 * * flag combinations that look like pthread_create()
152 * * flag combinations that look like fork()
153 * * flags we can implement within QEMU itself
154 * * flags we can't support and will return an error for
155 */
156/* For thread creation, all these flags must be present; for
157 * fork, none must be present.
158 */
159#define CLONE_THREAD_FLAGS \
160 (CLONE_VM | CLONE_FS | CLONE_FILES | \
161 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
162
163/* These flags are ignored:
164 * CLONE_DETACHED is now ignored by the kernel;
165 * CLONE_IO is just an optimisation hint to the I/O scheduler
166 */
167#define CLONE_IGNORED_FLAGS \
168 (CLONE_DETACHED | CLONE_IO)
169
170/* Flags for fork which we can implement within QEMU itself */
171#define CLONE_OPTIONAL_FORK_FLAGS \
172 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
173 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
174
175/* Flags for thread creation which we can implement within QEMU itself */
176#define CLONE_OPTIONAL_THREAD_FLAGS \
177 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
178 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
179
180#define CLONE_INVALID_FORK_FLAGS \
181 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
182
183#define CLONE_INVALID_THREAD_FLAGS \
184 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
185 CLONE_IGNORED_FLAGS))
186
187/* CLONE_VFORK is special cased early in do_fork(). The other flag bits
188 * have almost all been allocated. We cannot support any of
189 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
190 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
191 * The checks against the invalid thread masks above will catch these.
192 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
193 */
pbrook30813ce2008-06-02 15:45:44 +0000194
Timothy E Baldwin71a8f7f2016-05-12 18:47:45 +0100195/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
196 * once. This exercises the codepaths for restart.
197 */
198//#define DEBUG_ERESTARTSYS
bellard31e31b82003-02-18 22:55:36 +0000199
bellard1a9353d2003-03-16 20:28:50 +0000200//#include <linux/msdos_fs.h>
Richard Henderson540a7362021-11-14 11:35:37 +0100201#define VFAT_IOCTL_READDIR_BOTH \
202 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
203#define VFAT_IOCTL_READDIR_SHORT \
204 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
bellard1a9353d2003-03-16 20:28:50 +0000205
bellard70a194b2003-08-11 22:20:16 +0000206#undef _syscall0
207#undef _syscall1
208#undef _syscall2
209#undef _syscall3
210#undef _syscall4
211#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000212#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000213
bellard83fcb512006-06-14 13:37:16 +0000214#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000215static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000216{ \
217 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000218}
219
bellard83fcb512006-06-14 13:37:16 +0000220#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000221static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000222{ \
223 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000224}
225
bellard83fcb512006-06-14 13:37:16 +0000226#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000227static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000228{ \
229 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000230}
231
bellard83fcb512006-06-14 13:37:16 +0000232#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000233static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000234{ \
235 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000236}
237
bellard83fcb512006-06-14 13:37:16 +0000238#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000239static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000240{ \
241 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000242}
243
bellard83fcb512006-06-14 13:37:16 +0000244#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
245 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000246static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000247{ \
248 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000249}
bellard83fcb512006-06-14 13:37:16 +0000250
251
252#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
253 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000254static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
255 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000256{ \
257 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
258}
259
bellard70a194b2003-08-11 22:20:16 +0000260
bellard31e31b82003-02-18 22:55:36 +0000261#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000262#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000263#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000264#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000265#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000266#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +0200267#define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000268#define __NR_sys_syslog __NR_syslog
Alistair Francis14690292020-03-18 15:47:01 -0700269#if defined(__NR_futex)
270# define __NR_sys_futex __NR_futex
271#endif
272#if defined(__NR_futex_time64)
273# define __NR_sys_futex_time64 __NR_futex_time64
274#endif
aurel3239b59762008-10-01 21:46:50 +0000275#define __NR_sys_inotify_init __NR_inotify_init
276#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
277#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
Aleksandar Rikaloefa92182019-06-28 12:43:34 +0200278#define __NR_sys_statx __NR_statx
bellard31e31b82003-02-18 22:55:36 +0000279
Peter Maydellb1cef6d2018-01-25 16:19:49 +0000280#if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000281#define __NR__llseek __NR_lseek
282#endif
283
James Hogana29e5ba2014-03-25 21:51:08 +0000284/* Newer kernel ports have llseek() instead of _llseek() */
285#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
286#define TARGET_NR__llseek TARGET_NR_llseek
287#endif
288
Helge Deller78721302021-02-10 07:12:14 +0100289/* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
290#ifndef TARGET_O_NONBLOCK_MASK
291#define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
292#endif
293
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +0000294#define __NR_sys_gettid __NR_gettid
295_syscall0(int, sys_gettid)
Peter Maydell2b3f64c2018-04-19 13:57:40 +0100296
297/* For the 64-bit guest on 32-bit host case we must emulate
298 * getdents using getdents64, because otherwise the host
299 * might hand us back more dirent records than we can fit
300 * into the guest buffer after structure format conversion.
301 * Otherwise we emulate getdents with getdents if the host has it.
302 */
303#if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
304#define EMULATE_GETDENTS_WITH_GETDENTS
305#endif
306
307#if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
aurel323b3f24a2009-04-15 16:12:13 +0000308_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100309#endif
Peter Maydell2b3f64c2018-04-19 13:57:40 +0100310#if (defined(TARGET_NR_getdents) && \
311 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
Peter Maydell3307e232013-06-12 16:20:21 +0100312 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
aurel323b3f24a2009-04-15 16:12:13 +0000313_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
314#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700315#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000316_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
317 loff_t *, res, uint, wh);
318#endif
Miloš Stojanovićc1a402a2017-05-15 16:59:45 +0200319_syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +0200320_syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
321 siginfo_t *, uinfo)
aurel323b3f24a2009-04-15 16:12:13 +0000322_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
aurel323b3f24a2009-04-15 16:12:13 +0000323#ifdef __NR_exit_group
324_syscall1(int,exit_group,int,error_code)
325#endif
326#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
327_syscall1(int,set_tid_address,int *,tidptr)
328#endif
Alistair Francis14690292020-03-18 15:47:01 -0700329#if defined(__NR_futex)
aurel323b3f24a2009-04-15 16:12:13 +0000330_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
331 const struct timespec *,timeout,int *,uaddr2,int,val3)
332#endif
Alistair Francis14690292020-03-18 15:47:01 -0700333#if defined(__NR_futex_time64)
334_syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
335 const struct timespec *,timeout,int *,uaddr2,int,val3)
336#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500337#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
338_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
339 unsigned long *, user_mask_ptr);
340#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
341_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
342 unsigned long *, user_mask_ptr);
Tonis Tiigi45ad7612022-01-04 20:18:18 -0800343/* sched_attr is not defined in glibc */
344struct sched_attr {
345 uint32_t size;
346 uint32_t sched_policy;
347 uint64_t sched_flags;
348 int32_t sched_nice;
349 uint32_t sched_priority;
350 uint64_t sched_runtime;
351 uint64_t sched_deadline;
352 uint64_t sched_period;
353 uint32_t sched_util_min;
354 uint32_t sched_util_max;
355};
356#define __NR_sys_sched_getattr __NR_sched_getattr
357_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
358 unsigned int, size, unsigned int, flags);
359#define __NR_sys_sched_setattr __NR_sched_setattr
360_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
361 unsigned int, flags);
Tonis Tiigi407a1192022-01-04 20:18:19 -0800362#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
363_syscall1(int, sys_sched_getscheduler, pid_t, pid);
364#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
365_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
366 const struct sched_param *, param);
367#define __NR_sys_sched_getparam __NR_sched_getparam
368_syscall2(int, sys_sched_getparam, pid_t, pid,
369 struct sched_param *, param);
370#define __NR_sys_sched_setparam __NR_sched_setparam
371_syscall2(int, sys_sched_setparam, pid_t, pid,
372 const struct sched_param *, param);
Samuel Thibaultb827c3e2018-01-12 09:14:35 +0100373#define __NR_sys_getcpu __NR_getcpu
374_syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200375_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
376 void *, arg);
Peter Maydelle0eb2102014-03-17 12:15:35 +0000377_syscall2(int, capget, struct __user_cap_header_struct *, header,
378 struct __user_cap_data_struct *, data);
379_syscall2(int, capset, struct __user_cap_header_struct *, header,
380 struct __user_cap_data_struct *, data);
Paul Burtonab31cda2014-06-22 11:25:43 +0100381#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
382_syscall2(int, ioprio_get, int, which, int, who)
383#endif
384#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
385_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
386#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +0100387#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
388_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
389#endif
aurel323b3f24a2009-04-15 16:12:13 +0000390
Laurent Vivier2f147882016-09-25 22:20:20 +0200391#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
392_syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
393 unsigned long, idx1, unsigned long, idx2)
394#endif
395
Aleksandar Rikaloefa92182019-06-28 12:43:34 +0200396/*
397 * It is assumed that struct statx is architecture independent.
398 */
399#if defined(TARGET_NR_statx) && defined(__NR_statx)
400_syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
401 unsigned int, mask, struct target_statx *, statxbuf)
402#endif
Andreas Schwab85004762019-05-13 11:02:53 +0200403#if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
404_syscall2(int, membarrier, int, cmd, int, flags)
405#endif
Aleksandar Rikaloefa92182019-06-28 12:43:34 +0200406
Philippe Mathieu-Daudé180d4ef2021-05-17 07:52:43 +0200407static const bitmask_transtbl fcntl_flags_tbl[] = {
aurel323b3f24a2009-04-15 16:12:13 +0000408 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
409 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
410 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
411 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
412 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
413 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
414 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
415 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700416 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000417 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
418 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
419 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
420 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000421#if defined(O_DIRECT)
422 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
423#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700424#if defined(O_NOATIME)
425 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
426#endif
427#if defined(O_CLOEXEC)
428 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
429#endif
430#if defined(O_PATH)
431 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
432#endif
Riku Voipio5f9cee42017-08-08 16:01:19 +0300433#if defined(O_TMPFILE)
434 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
435#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700436 /* Don't terminate the list prematurely on 64-bit host+guest. */
437#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
438 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
439#endif
aurel323b3f24a2009-04-15 16:12:13 +0000440 { 0, 0, 0, 0 }
441};
442
Andreas Schwab0f6bb192020-07-23 12:27:13 +0200443_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
aurel323b3f24a2009-04-15 16:12:13 +0000444
Filip Bozutacac46eb2020-08-25 00:30:50 +0200445#if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
Peter Maydell700fa582016-07-18 11:47:55 +0100446#if defined(__NR_utimensat)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100447#define __NR_sys_utimensat __NR_utimensat
ths9007f0e2007-09-25 17:50:37 +0000448_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
449 const struct timespec *,tsp,int,flags)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100450#else
451static int sys_utimensat(int dirfd, const char *pathname,
452 const struct timespec times[2], int flags)
453{
454 errno = ENOSYS;
455 return -1;
456}
ths9007f0e2007-09-25 17:50:37 +0000457#endif
Peter Maydell1acae9f2013-07-02 14:04:12 +0100458#endif /* TARGET_NR_utimensat */
aurel323b3f24a2009-04-15 16:12:13 +0000459
Andreas Schwab95d03072018-01-23 11:53:31 +0100460#ifdef TARGET_NR_renameat2
461#if defined(__NR_renameat2)
462#define __NR_sys_renameat2 __NR_renameat2
463_syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
464 const char *, new, unsigned int, flags)
465#else
466static int sys_renameat2(int oldfd, const char *old,
467 int newfd, const char *new, int flags)
468{
469 if (flags == 0) {
470 return renameat(oldfd, old, newfd, new);
471 }
472 errno = ENOSYS;
473 return -1;
474}
475#endif
476#endif /* TARGET_NR_renameat2 */
477
aurel323b3f24a2009-04-15 16:12:13 +0000478#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000479#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000480
aurel3239b59762008-10-01 21:46:50 +0000481#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000482static int sys_inotify_init(void)
483{
484 return (inotify_init());
485}
aurel3239b59762008-10-01 21:46:50 +0000486#endif
487#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000488static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
489{
490 return (inotify_add_watch(fd, pathname, mask));
491}
aurel3239b59762008-10-01 21:46:50 +0000492#endif
493#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000494static int sys_inotify_rm_watch(int fd, int32_t wd)
495{
aurel328690e422009-04-17 13:50:32 +0000496 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000497}
aurel3239b59762008-10-01 21:46:50 +0000498#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000499#ifdef CONFIG_INOTIFY1
500#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
501static int sys_inotify_init1(int flags)
502{
503 return (inotify_init1(flags));
504}
505#endif
506#endif
aurel323b3f24a2009-04-15 16:12:13 +0000507#else
508/* Userspace can usually survive runtime without inotify */
509#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000510#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000511#undef TARGET_NR_inotify_add_watch
512#undef TARGET_NR_inotify_rm_watch
513#endif /* CONFIG_INOTIFY */
514
Peter Maydell163a05a2011-06-27 17:44:52 +0100515#if defined(TARGET_NR_prlimit64)
516#ifndef __NR_prlimit64
517# define __NR_prlimit64 -1
518#endif
519#define __NR_sys_prlimit64 __NR_prlimit64
520/* The glibc rlimit structure may not be that used by the underlying syscall */
521struct host_rlimit64 {
522 uint64_t rlim_cur;
523 uint64_t rlim_max;
524};
525_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
526 const struct host_rlimit64 *, new_limit,
527 struct host_rlimit64 *, old_limit)
528#endif
529
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100530
531#if defined(TARGET_NR_timer_create)
zhaolichang6f9ff552020-09-17 15:50:25 +0800532/* Maximum of 32 active POSIX timers allowed at any one time. */
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100533static timer_t g_posix_timers[32] = { 0, } ;
534
535static inline int next_free_host_timer(void)
536{
537 int k ;
538 /* FIXME: Does finding the next free slot require a lock? */
539 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
540 if (g_posix_timers[k] == 0) {
541 g_posix_timers[k] = (timer_t) 1;
542 return k;
543 }
544 }
545 return -1;
546}
547#endif
548
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200549static inline int host_to_target_errno(int host_errno)
ths637947f2007-06-01 12:09:19 +0000550{
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200551 switch (host_errno) {
552#define E(X) case X: return TARGET_##X;
553#include "errnos.c.inc"
554#undef E
555 default:
556 return host_errno;
Timothy E Baldwin24661192016-05-12 18:47:25 +0100557 }
ths637947f2007-06-01 12:09:19 +0000558}
559
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200560static inline int target_to_host_errno(int target_errno)
thsb92c47c2007-11-01 00:07:38 +0000561{
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200562 switch (target_errno) {
563#define E(X) case TARGET_##X: return X;
564#include "errnos.c.inc"
565#undef E
566 default:
567 return target_errno;
Timothy E Baldwin24661192016-05-12 18:47:25 +0100568 }
thsb92c47c2007-11-01 00:07:38 +0000569}
570
blueswir1992f48a2007-10-14 16:27:31 +0000571static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000572{
573 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000574 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000575 else
576 return ret;
577}
578
Peter Maydell7dcdaea2016-06-06 19:58:18 +0100579const char *target_strerror(int err)
thsb92c47c2007-11-01 00:07:38 +0000580{
Richard Hendersonaf254a22021-11-22 19:47:33 +0100581 if (err == QEMU_ERESTARTSYS) {
Peter Maydellda2a34f2016-06-06 19:58:19 +0100582 return "To be restarted";
583 }
Richard Henderson57a0c932021-11-17 05:14:52 -0800584 if (err == QEMU_ESIGRETURN) {
Peter Maydellda2a34f2016-06-06 19:58:19 +0100585 return "Successful exit from sigreturn";
586 }
587
thsb92c47c2007-11-01 00:07:38 +0000588 return strerror(target_to_host_errno(err));
589}
590
Tonis Tiigi45ad7612022-01-04 20:18:18 -0800591static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
592{
593 int i;
594 uint8_t b;
595 if (usize <= ksize) {
596 return 1;
597 }
598 for (i = ksize; i < usize; i++) {
599 if (get_user_u8(b, addr + i)) {
600 return -TARGET_EFAULT;
601 }
602 if (b != 0) {
603 return 0;
604 }
605 }
606 return 1;
607}
608
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100609#define safe_syscall0(type, name) \
610static type safe_##name(void) \
611{ \
612 return safe_syscall(__NR_##name); \
613}
614
615#define safe_syscall1(type, name, type1, arg1) \
616static type safe_##name(type1 arg1) \
617{ \
618 return safe_syscall(__NR_##name, arg1); \
619}
620
621#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
622static type safe_##name(type1 arg1, type2 arg2) \
623{ \
624 return safe_syscall(__NR_##name, arg1, arg2); \
625}
626
627#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
628static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
629{ \
630 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
631}
632
633#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
634 type4, arg4) \
635static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
636{ \
637 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
638}
639
640#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
641 type4, arg4, type5, arg5) \
642static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
643 type5 arg5) \
644{ \
645 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
646}
647
648#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
649 type4, arg4, type5, arg5, type6, arg6) \
650static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
651 type5 arg5, type6 arg6) \
652{ \
653 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
654}
655
Timothy E Baldwin50afd022016-05-12 18:47:47 +0100656safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
657safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
Timothy E Baldwinc10a0732016-05-12 18:47:48 +0100658safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
659 int, flags, mode_t, mode)
Alistair Francis859e8a82020-03-12 15:13:49 -0700660#if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
Timothy E Baldwin4af80a32016-05-12 18:47:49 +0100661safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
662 struct rusage *, rusage)
Alistair Francis859e8a82020-03-12 15:13:49 -0700663#endif
Timothy E Baldwin4af80a32016-05-12 18:47:49 +0100664safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
665 int, options, struct rusage *, rusage)
Timothy E Baldwinffdcbe22016-05-12 18:47:50 +0100666safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
Alistair Francis859e8a82020-03-12 15:13:49 -0700667#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
Filip Bozutae5ce9682020-08-25 00:30:49 +0200668 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
Peter Maydell6df9d382016-05-12 18:47:51 +0100669safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
670 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
Alistair Francis859e8a82020-03-12 15:13:49 -0700671#endif
Filip Bozutae5ce9682020-08-25 00:30:49 +0200672#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
Peter Maydella6130232016-06-06 19:58:10 +0100673safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
674 struct timespec *, tsp, const sigset_t *, sigmask,
675 size_t, sigsetsize)
Alistair Francis859e8a82020-03-12 15:13:49 -0700676#endif
Peter Maydell227f0212016-06-06 19:58:11 +0100677safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
678 int, maxevents, int, timeout, const sigset_t *, sigmask,
679 size_t, sigsetsize)
Alistair Francis14690292020-03-18 15:47:01 -0700680#if defined(__NR_futex)
Peter Maydelld509eeb2016-05-12 18:47:52 +0100681safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
682 const struct timespec *,timeout,int *,uaddr2,int,val3)
Alistair Francis859e8a82020-03-12 15:13:49 -0700683#endif
Alistair Francis14690292020-03-18 15:47:01 -0700684#if defined(__NR_futex_time64)
685safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
686 const struct timespec *,timeout,int *,uaddr2,int,val3)
687#endif
Peter Maydell2fe4fba2016-05-27 15:51:48 +0100688safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
Peter Maydellbef653d2016-05-27 15:51:57 +0100689safe_syscall2(int, kill, pid_t, pid, int, sig)
690safe_syscall2(int, tkill, int, tid, int, sig)
691safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
Peter Maydell918c03e2016-06-06 19:58:02 +0100692safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
693safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
Dejan Jovicevic0f263862016-10-11 11:52:46 +0200694safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
695 unsigned long, pos_l, unsigned long, pos_h)
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +0200696safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
697 unsigned long, pos_l, unsigned long, pos_h)
Peter Maydell2a3c7612016-06-06 19:58:03 +0100698safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
699 socklen_t, addrlen)
Peter Maydell66687532016-06-06 19:58:04 +0100700safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
701 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
702safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
703 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
704safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
705safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
Peter Maydell2a845982016-06-06 19:58:07 +0100706safe_syscall2(int, flock, int, fd, int, operation)
Filip Bozutaddcbde12020-08-24 21:21:16 +0200707#if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
Peter Maydellb3f82332016-06-06 19:58:08 +0100708safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
709 const struct timespec *, uts, size_t, sigsetsize)
Alistair Francis859e8a82020-03-12 15:13:49 -0700710#endif
Peter Maydellff6dc132016-06-06 19:58:13 +0100711safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
712 int, flags)
Alistair Francis859e8a82020-03-12 15:13:49 -0700713#if defined(TARGET_NR_nanosleep)
Peter Maydell9e518222016-06-06 19:58:09 +0100714safe_syscall2(int, nanosleep, const struct timespec *, req,
715 struct timespec *, rem)
Alistair Francis859e8a82020-03-12 15:13:49 -0700716#endif
Filip Bozuta6ac03b22020-08-24 21:21:15 +0200717#if defined(TARGET_NR_clock_nanosleep) || \
718 defined(TARGET_NR_clock_nanosleep_time64)
Peter Maydell9e518222016-06-06 19:58:09 +0100719safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
720 const struct timespec *, req, struct timespec *, rem)
721#endif
Laurent Vivier524fa342019-05-29 10:48:04 +0200722#ifdef __NR_ipc
Matus Kyseld8c08b12020-06-26 14:46:11 +0200723#ifdef __s390x__
724safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
725 void *, ptr)
726#else
Peter Maydell89f9fe42016-06-06 19:58:05 +0100727safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
728 void *, ptr, long, fifth)
Laurent Vivier86e63692019-05-23 19:54:13 +0200729#endif
Matus Kyseld8c08b12020-06-26 14:46:11 +0200730#endif
Laurent Vivier86e63692019-05-23 19:54:13 +0200731#ifdef __NR_msgsnd
732safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
733 int, flags)
Laurent Vivier86e63692019-05-23 19:54:13 +0200734#endif
735#ifdef __NR_msgrcv
736safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
737 long, msgtype, int, flags)
Laurent Vivier86e63692019-05-23 19:54:13 +0200738#endif
739#ifdef __NR_semtimedop
740safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
741 unsigned, nsops, const struct timespec *, timeout)
Peter Maydell89f9fe42016-06-06 19:58:05 +0100742#endif
Filip Bozutad107e372020-08-24 21:37:52 +0200743#if defined(TARGET_NR_mq_timedsend) || \
744 defined(TARGET_NR_mq_timedsend_time64)
Peter Maydelld40ecd62016-06-06 19:58:06 +0100745safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
746 size_t, len, unsigned, prio, const struct timespec *, timeout)
Alistair Francis859e8a82020-03-12 15:13:49 -0700747#endif
Filip Bozutad107e372020-08-24 21:37:52 +0200748#if defined(TARGET_NR_mq_timedreceive) || \
749 defined(TARGET_NR_mq_timedreceive_time64)
Peter Maydelld40ecd62016-06-06 19:58:06 +0100750safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
751 size_t, len, unsigned *, prio, const struct timespec *, timeout)
752#endif
Andreas Schwab84946452020-11-12 12:45:16 +0100753#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
754safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
755 int, outfd, loff_t *, poutoff, size_t, length,
756 unsigned int, flags)
757#endif
758
Peter Maydell49ca6f32016-06-06 19:58:14 +0100759/* We do ioctl like this rather than via safe_syscall3 to preserve the
760 * "third argument might be integer or pointer or not present" behaviour of
761 * the libc function.
762 */
763#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
Peter Maydell435da5e2016-06-13 11:22:05 +0100764/* Similarly for fcntl. Note that callers must always:
765 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
766 * use the flock64 struct rather than unsuffixed flock
767 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
768 */
769#ifdef __NR_fcntl64
770#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
771#else
772#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
773#endif
Timothy E Baldwin50afd022016-05-12 18:47:47 +0100774
Paul Burton8289d112014-06-22 11:25:33 +0100775static inline int host_to_target_sock_type(int host_type)
776{
777 int target_type;
778
779 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
780 case SOCK_DGRAM:
781 target_type = TARGET_SOCK_DGRAM;
782 break;
783 case SOCK_STREAM:
784 target_type = TARGET_SOCK_STREAM;
785 break;
786 default:
787 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
788 break;
789 }
790
791#if defined(SOCK_CLOEXEC)
792 if (host_type & SOCK_CLOEXEC) {
793 target_type |= TARGET_SOCK_CLOEXEC;
794 }
795#endif
796
797#if defined(SOCK_NONBLOCK)
798 if (host_type & SOCK_NONBLOCK) {
799 target_type |= TARGET_SOCK_NONBLOCK;
800 }
801#endif
802
803 return target_type;
804}
805
blueswir1992f48a2007-10-14 16:27:31 +0000806static abi_ulong target_brk;
807static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000808static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000809
blueswir1992f48a2007-10-14 16:27:31 +0000810void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000811{
blueswir14c1de732007-07-07 20:45:44 +0000812 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000813 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000814}
815
vincent4d1de872011-06-14 21:56:33 +0000816//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
817#define DEBUGF_BRK(message, args...)
818
ths0da46a62007-10-20 20:23:07 +0000819/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000820abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000821{
blueswir1992f48a2007-10-14 16:27:31 +0000822 abi_long mapped_addr;
Peter Maydellef4330c2016-07-22 13:41:52 +0100823 abi_ulong new_alloc_size;
bellard31e31b82003-02-18 22:55:36 +0000824
Richard Hendersonee1bf832021-02-12 10:48:44 -0800825 /* brk pointers are always untagged */
826
Paul Brook3a0c6c42012-02-09 19:04:27 +0000827 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000828
vincent4d1de872011-06-14 21:56:33 +0000829 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000830 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000831 return target_brk;
832 }
833 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000834 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
835 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000836 return target_brk;
837 }
bellard31e31b82003-02-18 22:55:36 +0000838
vincent4d1de872011-06-14 21:56:33 +0000839 /* If the new brk is less than the highest page reserved to the
840 * target heap allocation, set it and we're almost done... */
841 if (new_brk <= brk_page) {
842 /* Heap contents are initialized to zero, as for anonymous
843 * mapped pages. */
844 if (new_brk > target_brk) {
Richard Henderson3e8f1622021-02-12 10:48:43 -0800845 memset(g2h_untagged(target_brk), 0, new_brk - target_brk);
vincent4d1de872011-06-14 21:56:33 +0000846 }
bellard31e31b82003-02-18 22:55:36 +0000847 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000848 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
Paolo Bonzini72e21db2018-12-13 23:37:36 +0100849 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000850 }
851
Peter Maydell00faf082011-04-18 16:34:24 +0100852 /* We need to allocate more memory after the brk... Note that
853 * we don't use MAP_FIXED because that will map over the top of
854 * any existing mapping (like the one with the host libc or qemu
855 * itself); instead we treat "mapped but at wrong address" as
856 * a failure and unmap again.
857 */
vincent4d1de872011-06-14 21:56:33 +0000858 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000859 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000860 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100861 MAP_ANON|MAP_PRIVATE, 0, 0));
862
863 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200864 /* Heap contents are initialized to zero, as for anonymous
865 * mapped pages. Technically the new pages are already
866 * initialized to zero since they *are* anonymous mapped
867 * pages, however we have to take care with the contents that
868 * come from the remaining part of the previous page: it may
869 * contains garbage data due to a previous heap usage (grown
870 * then shrunken). */
Richard Henderson3e8f1622021-02-12 10:48:43 -0800871 memset(g2h_untagged(target_brk), 0, brk_page - target_brk);
Cédric VINCENT70afc342011-08-26 10:56:50 +0200872
Peter Maydell00faf082011-04-18 16:34:24 +0100873 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000874 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000875 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
876 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100877 return target_brk;
878 } else if (mapped_addr != -1) {
879 /* Mapped but at wrong address, meaning there wasn't actually
880 * enough space for this brk.
881 */
882 target_munmap(mapped_addr, new_alloc_size);
883 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000884 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000885 }
886 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000887 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100888 }
balrog7ab240a2008-04-26 12:17:34 +0000889
Richard Henderson7dd46c02010-05-03 10:07:49 -0700890#if defined(TARGET_ALPHA)
891 /* We (partially) emulate OSF/1 on Alpha, which requires we
892 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100893 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700894#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100895 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000896 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000897}
898
Alistair Francis859e8a82020-03-12 15:13:49 -0700899#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
Filip Bozutae5ce9682020-08-25 00:30:49 +0200900 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
ths26edcf42007-12-09 02:25:24 +0000901static inline abi_long copy_from_user_fdset(fd_set *fds,
902 abi_ulong target_fds_addr,
903 int n)
bellard31e31b82003-02-18 22:55:36 +0000904{
ths26edcf42007-12-09 02:25:24 +0000905 int i, nw, j, k;
906 abi_ulong b, *target_fds;
907
Laurent Vivierb1b2db22016-05-31 18:36:02 +0200908 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
ths26edcf42007-12-09 02:25:24 +0000909 if (!(target_fds = lock_user(VERIFY_READ,
910 target_fds_addr,
911 sizeof(abi_ulong) * nw,
912 1)))
913 return -TARGET_EFAULT;
914
915 FD_ZERO(fds);
916 k = 0;
917 for (i = 0; i < nw; i++) {
918 /* grab the abi_ulong */
919 __get_user(b, &target_fds[i]);
920 for (j = 0; j < TARGET_ABI_BITS; j++) {
921 /* check the bit inside the abi_ulong */
922 if ((b >> j) & 1)
923 FD_SET(k, fds);
924 k++;
bellard31e31b82003-02-18 22:55:36 +0000925 }
bellard31e31b82003-02-18 22:55:36 +0000926 }
ths26edcf42007-12-09 02:25:24 +0000927
928 unlock_user(target_fds, target_fds_addr, 0);
929
930 return 0;
bellard31e31b82003-02-18 22:55:36 +0000931}
932
Mike Frysinger055e0902011-06-03 17:01:49 -0400933static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
934 abi_ulong target_fds_addr,
935 int n)
936{
937 if (target_fds_addr) {
938 if (copy_from_user_fdset(fds, target_fds_addr, n))
939 return -TARGET_EFAULT;
940 *fds_ptr = fds;
941 } else {
942 *fds_ptr = NULL;
943 }
944 return 0;
945}
946
ths26edcf42007-12-09 02:25:24 +0000947static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
948 const fd_set *fds,
949 int n)
bellard31e31b82003-02-18 22:55:36 +0000950{
bellard31e31b82003-02-18 22:55:36 +0000951 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000952 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000953 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000954
Laurent Vivierb1b2db22016-05-31 18:36:02 +0200955 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
ths26edcf42007-12-09 02:25:24 +0000956 if (!(target_fds = lock_user(VERIFY_WRITE,
957 target_fds_addr,
958 sizeof(abi_ulong) * nw,
959 0)))
960 return -TARGET_EFAULT;
961
962 k = 0;
963 for (i = 0; i < nw; i++) {
964 v = 0;
965 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000966 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000967 k++;
bellard31e31b82003-02-18 22:55:36 +0000968 }
ths26edcf42007-12-09 02:25:24 +0000969 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000970 }
ths26edcf42007-12-09 02:25:24 +0000971
972 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
973
974 return 0;
bellard31e31b82003-02-18 22:55:36 +0000975}
Alistair Francis859e8a82020-03-12 15:13:49 -0700976#endif
bellard31e31b82003-02-18 22:55:36 +0000977
bellardc596ed12003-07-13 17:32:31 +0000978#if defined(__alpha__)
979#define HOST_HZ 1024
980#else
981#define HOST_HZ 100
982#endif
983
blueswir1992f48a2007-10-14 16:27:31 +0000984static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000985{
986#if HOST_HZ == TARGET_HZ
987 return ticks;
988#else
989 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
990#endif
991}
992
bellard579a97f2007-11-11 14:26:47 +0000993static inline abi_long host_to_target_rusage(abi_ulong target_addr,
994 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000995{
pbrook53a59602006-03-25 19:31:22 +0000996 struct target_rusage *target_rusage;
997
bellard579a97f2007-11-11 14:26:47 +0000998 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
999 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001000 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1001 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1002 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1003 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1004 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1005 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1006 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1007 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1008 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1009 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1010 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1011 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1012 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1013 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1014 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1015 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1016 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1017 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +00001018 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001019
1020 return 0;
bellardb4091862003-05-16 15:39:34 +00001021}
1022
Alistair Francis859e8a82020-03-12 15:13:49 -07001023#ifdef TARGET_NR_setrlimit
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001024static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001025{
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001026 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001027 rlim_t result;
1028
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001029 target_rlim_swap = tswapal(target_rlim);
1030 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1031 return RLIM_INFINITY;
1032
1033 result = target_rlim_swap;
1034 if (target_rlim_swap != (rlim_t)result)
1035 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001036
1037 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001038}
Alistair Francis859e8a82020-03-12 15:13:49 -07001039#endif
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001040
Alistair Francis859e8a82020-03-12 15:13:49 -07001041#if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001042static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001043{
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001044 abi_ulong target_rlim_swap;
1045 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001046
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001047 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001048 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001049 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001050 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001051 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001052
1053 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001054}
Alistair Francis859e8a82020-03-12 15:13:49 -07001055#endif
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001056
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03001057static inline int target_to_host_resource(int code)
1058{
1059 switch (code) {
1060 case TARGET_RLIMIT_AS:
1061 return RLIMIT_AS;
1062 case TARGET_RLIMIT_CORE:
1063 return RLIMIT_CORE;
1064 case TARGET_RLIMIT_CPU:
1065 return RLIMIT_CPU;
1066 case TARGET_RLIMIT_DATA:
1067 return RLIMIT_DATA;
1068 case TARGET_RLIMIT_FSIZE:
1069 return RLIMIT_FSIZE;
1070 case TARGET_RLIMIT_LOCKS:
1071 return RLIMIT_LOCKS;
1072 case TARGET_RLIMIT_MEMLOCK:
1073 return RLIMIT_MEMLOCK;
1074 case TARGET_RLIMIT_MSGQUEUE:
1075 return RLIMIT_MSGQUEUE;
1076 case TARGET_RLIMIT_NICE:
1077 return RLIMIT_NICE;
1078 case TARGET_RLIMIT_NOFILE:
1079 return RLIMIT_NOFILE;
1080 case TARGET_RLIMIT_NPROC:
1081 return RLIMIT_NPROC;
1082 case TARGET_RLIMIT_RSS:
1083 return RLIMIT_RSS;
1084 case TARGET_RLIMIT_RTPRIO:
1085 return RLIMIT_RTPRIO;
1086 case TARGET_RLIMIT_SIGPENDING:
1087 return RLIMIT_SIGPENDING;
1088 case TARGET_RLIMIT_STACK:
1089 return RLIMIT_STACK;
1090 default:
1091 return code;
1092 }
1093}
1094
ths788f5ec2007-12-09 02:37:05 +00001095static inline abi_long copy_from_user_timeval(struct timeval *tv,
1096 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001097{
pbrook53a59602006-03-25 19:31:22 +00001098 struct target_timeval *target_tv;
1099
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001100 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
bellard579a97f2007-11-11 14:26:47 +00001101 return -TARGET_EFAULT;
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001102 }
ths788f5ec2007-12-09 02:37:05 +00001103
1104 __get_user(tv->tv_sec, &target_tv->tv_sec);
1105 __get_user(tv->tv_usec, &target_tv->tv_usec);
1106
1107 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001108
1109 return 0;
bellard31e31b82003-02-18 22:55:36 +00001110}
1111
ths788f5ec2007-12-09 02:37:05 +00001112static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1113 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001114{
pbrook53a59602006-03-25 19:31:22 +00001115 struct target_timeval *target_tv;
1116
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001117 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
bellard579a97f2007-11-11 14:26:47 +00001118 return -TARGET_EFAULT;
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001119 }
ths788f5ec2007-12-09 02:37:05 +00001120
1121 __put_user(tv->tv_sec, &target_tv->tv_sec);
1122 __put_user(tv->tv_usec, &target_tv->tv_usec);
1123
1124 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001125
1126 return 0;
bellard31e31b82003-02-18 22:55:36 +00001127}
1128
Filip Bozuta6ac03b22020-08-24 21:21:15 +02001129#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1130static inline abi_long copy_from_user_timeval64(struct timeval *tv,
1131 abi_ulong target_tv_addr)
1132{
1133 struct target__kernel_sock_timeval *target_tv;
1134
1135 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
1136 return -TARGET_EFAULT;
1137 }
1138
1139 __get_user(tv->tv_sec, &target_tv->tv_sec);
1140 __get_user(tv->tv_usec, &target_tv->tv_usec);
1141
1142 unlock_user_struct(target_tv, target_tv_addr, 0);
1143
1144 return 0;
1145}
1146#endif
1147
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001148static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
Filip Bozuta6ac03b22020-08-24 21:21:15 +02001149 const struct timeval *tv)
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001150{
1151 struct target__kernel_sock_timeval *target_tv;
1152
1153 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
1154 return -TARGET_EFAULT;
1155 }
1156
1157 __put_user(tv->tv_sec, &target_tv->tv_sec);
1158 __put_user(tv->tv_usec, &target_tv->tv_usec);
1159
1160 unlock_user_struct(target_tv, target_tv_addr, 1);
1161
1162 return 0;
1163}
1164
Alistair Francis859e8a82020-03-12 15:13:49 -07001165#if defined(TARGET_NR_futex) || \
1166 defined(TARGET_NR_rt_sigtimedwait) || \
1167 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1168 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1169 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
Matus Kyseld8c08b12020-06-26 14:46:11 +02001170 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
Filip Bozuta2c86c902020-07-22 17:34:20 +02001171 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1172 defined(TARGET_NR_timer_settime) || \
1173 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001174static inline abi_long target_to_host_timespec(struct timespec *host_ts,
1175 abi_ulong target_addr)
1176{
1177 struct target_timespec *target_ts;
1178
1179 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1180 return -TARGET_EFAULT;
1181 }
1182 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1183 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1184 unlock_user_struct(target_ts, target_addr, 0);
1185 return 0;
1186}
Alistair Francis859e8a82020-03-12 15:13:49 -07001187#endif
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001188
Filip Bozuta828cb3a2020-07-22 17:34:21 +02001189#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1190 defined(TARGET_NR_timer_settime64) || \
Filip Bozutad107e372020-08-24 21:37:52 +02001191 defined(TARGET_NR_mq_timedsend_time64) || \
1192 defined(TARGET_NR_mq_timedreceive_time64) || \
Filip Bozuta6ac03b22020-08-24 21:21:15 +02001193 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
Filip Bozutaddcbde12020-08-24 21:21:16 +02001194 defined(TARGET_NR_clock_nanosleep_time64) || \
Filip Bozutacac46eb2020-08-25 00:30:50 +02001195 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1196 defined(TARGET_NR_utimensat) || \
1197 defined(TARGET_NR_utimensat_time64) || \
Filip Bozutae5ce9682020-08-25 00:30:49 +02001198 defined(TARGET_NR_semtimedop_time64) || \
1199 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
Alistair Francisc6c8d102020-03-12 15:13:53 -07001200static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
1201 abi_ulong target_addr)
1202{
1203 struct target__kernel_timespec *target_ts;
1204
1205 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1206 return -TARGET_EFAULT;
1207 }
1208 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1209 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
Laurent Vivier00576752020-08-27 09:04:49 +02001210 /* in 32bit mode, this drops the padding */
1211 host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
Alistair Francisc6c8d102020-03-12 15:13:53 -07001212 unlock_user_struct(target_ts, target_addr, 0);
1213 return 0;
1214}
1215#endif
1216
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001217static inline abi_long host_to_target_timespec(abi_ulong target_addr,
1218 struct timespec *host_ts)
1219{
1220 struct target_timespec *target_ts;
1221
1222 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1223 return -TARGET_EFAULT;
1224 }
1225 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1226 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1227 unlock_user_struct(target_ts, target_addr, 1);
1228 return 0;
1229}
1230
1231static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
1232 struct timespec *host_ts)
1233{
1234 struct target__kernel_timespec *target_ts;
1235
1236 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1237 return -TARGET_EFAULT;
1238 }
1239 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1240 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1241 unlock_user_struct(target_ts, target_addr, 1);
1242 return 0;
1243}
1244
Richard Hendersona52f5f82020-02-12 19:22:23 -08001245#if defined(TARGET_NR_gettimeofday)
1246static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
1247 struct timezone *tz)
1248{
1249 struct target_timezone *target_tz;
1250
1251 if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
1252 return -TARGET_EFAULT;
1253 }
1254
1255 __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1256 __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1257
1258 unlock_user_struct(target_tz, target_tz_addr, 1);
1259
1260 return 0;
1261}
1262#endif
1263
Alistair Francis859e8a82020-03-12 15:13:49 -07001264#if defined(TARGET_NR_settimeofday)
Paul Burtonef4467e2014-06-22 11:25:40 +01001265static inline abi_long copy_from_user_timezone(struct timezone *tz,
1266 abi_ulong target_tz_addr)
1267{
1268 struct target_timezone *target_tz;
1269
1270 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1271 return -TARGET_EFAULT;
1272 }
1273
1274 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1275 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1276
1277 unlock_user_struct(target_tz, target_tz_addr, 0);
1278
1279 return 0;
1280}
Alistair Francis859e8a82020-03-12 15:13:49 -07001281#endif
Paul Burtonef4467e2014-06-22 11:25:40 +01001282
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001283#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1284#include <mqueue.h>
1285
aurel3224e10032009-04-15 16:11:43 +00001286static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1287 abi_ulong target_mq_attr_addr)
1288{
1289 struct target_mq_attr *target_mq_attr;
1290
1291 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1292 target_mq_attr_addr, 1))
1293 return -TARGET_EFAULT;
1294
1295 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1296 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1297 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1298 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1299
1300 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1301
1302 return 0;
1303}
1304
1305static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1306 const struct mq_attr *attr)
1307{
1308 struct target_mq_attr *target_mq_attr;
1309
1310 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1311 target_mq_attr_addr, 0))
1312 return -TARGET_EFAULT;
1313
1314 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1315 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1316 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1317 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1318
1319 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1320
1321 return 0;
1322}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001323#endif
bellard31e31b82003-02-18 22:55:36 +00001324
Mike Frysinger055e0902011-06-03 17:01:49 -04001325#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001326/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001327static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001328 abi_ulong rfd_addr, abi_ulong wfd_addr,
1329 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001330{
1331 fd_set rfds, wfds, efds;
1332 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
Peter Maydell6df9d382016-05-12 18:47:51 +01001333 struct timeval tv;
1334 struct timespec ts, *ts_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001335 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001336
Mike Frysinger055e0902011-06-03 17:01:49 -04001337 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1338 if (ret) {
1339 return ret;
pbrook53a59602006-03-25 19:31:22 +00001340 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001341 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1342 if (ret) {
1343 return ret;
pbrook53a59602006-03-25 19:31:22 +00001344 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001345 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1346 if (ret) {
1347 return ret;
pbrook53a59602006-03-25 19:31:22 +00001348 }
ths3b46e622007-09-17 08:09:54 +00001349
ths26edcf42007-12-09 02:25:24 +00001350 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001351 if (copy_from_user_timeval(&tv, target_tv_addr))
1352 return -TARGET_EFAULT;
Peter Maydell6df9d382016-05-12 18:47:51 +01001353 ts.tv_sec = tv.tv_sec;
1354 ts.tv_nsec = tv.tv_usec * 1000;
1355 ts_ptr = &ts;
bellard31e31b82003-02-18 22:55:36 +00001356 } else {
Peter Maydell6df9d382016-05-12 18:47:51 +01001357 ts_ptr = NULL;
bellard31e31b82003-02-18 22:55:36 +00001358 }
ths26edcf42007-12-09 02:25:24 +00001359
Peter Maydell6df9d382016-05-12 18:47:51 +01001360 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1361 ts_ptr, NULL));
pbrook53a59602006-03-25 19:31:22 +00001362
ths26edcf42007-12-09 02:25:24 +00001363 if (!is_error(ret)) {
1364 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1365 return -TARGET_EFAULT;
1366 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1367 return -TARGET_EFAULT;
1368 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1369 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001370
Peter Maydell6df9d382016-05-12 18:47:51 +01001371 if (target_tv_addr) {
1372 tv.tv_sec = ts.tv_sec;
1373 tv.tv_usec = ts.tv_nsec / 1000;
1374 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1375 return -TARGET_EFAULT;
1376 }
1377 }
bellard31e31b82003-02-18 22:55:36 +00001378 }
bellard579a97f2007-11-11 14:26:47 +00001379
bellard31e31b82003-02-18 22:55:36 +00001380 return ret;
1381}
Laurent Vivier5457dc92016-07-08 01:17:27 +02001382
1383#if defined(TARGET_WANT_OLD_SYS_SELECT)
1384static abi_long do_old_select(abi_ulong arg1)
1385{
1386 struct target_sel_arg_struct *sel;
1387 abi_ulong inp, outp, exp, tvp;
1388 long nsel;
1389
1390 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1391 return -TARGET_EFAULT;
1392 }
1393
1394 nsel = tswapal(sel->n);
1395 inp = tswapal(sel->inp);
1396 outp = tswapal(sel->outp);
1397 exp = tswapal(sel->exp);
1398 tvp = tswapal(sel->tvp);
1399
1400 unlock_user_struct(sel, arg1, 0);
1401
1402 return do_select(nsel, inp, outp, exp, tvp);
1403}
1404#endif
Mike Frysinger055e0902011-06-03 17:01:49 -04001405#endif
bellard31e31b82003-02-18 22:55:36 +00001406
Filip Bozutae5ce9682020-08-25 00:30:49 +02001407#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1408static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
1409 abi_long arg4, abi_long arg5, abi_long arg6,
1410 bool time64)
1411{
1412 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
1413 fd_set rfds, wfds, efds;
1414 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1415 struct timespec ts, *ts_ptr;
1416 abi_long ret;
1417
1418 /*
1419 * The 6th arg is actually two args smashed together,
1420 * so we cannot use the C library.
1421 */
1422 sigset_t set;
1423 struct {
1424 sigset_t *set;
1425 size_t size;
1426 } sig, *sig_ptr;
1427
1428 abi_ulong arg_sigset, arg_sigsize, *arg7;
1429 target_sigset_t *target_sigset;
1430
1431 n = arg1;
1432 rfd_addr = arg2;
1433 wfd_addr = arg3;
1434 efd_addr = arg4;
1435 ts_addr = arg5;
1436
1437 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1438 if (ret) {
1439 return ret;
1440 }
1441 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1442 if (ret) {
1443 return ret;
1444 }
1445 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1446 if (ret) {
1447 return ret;
1448 }
1449
1450 /*
1451 * This takes a timespec, and not a timeval, so we cannot
1452 * use the do_select() helper ...
1453 */
1454 if (ts_addr) {
1455 if (time64) {
1456 if (target_to_host_timespec64(&ts, ts_addr)) {
1457 return -TARGET_EFAULT;
1458 }
1459 } else {
1460 if (target_to_host_timespec(&ts, ts_addr)) {
1461 return -TARGET_EFAULT;
1462 }
1463 }
1464 ts_ptr = &ts;
1465 } else {
1466 ts_ptr = NULL;
1467 }
1468
1469 /* Extract the two packed args for the sigset */
1470 if (arg6) {
1471 sig_ptr = &sig;
1472 sig.size = SIGSET_T_SIZE;
1473
1474 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
1475 if (!arg7) {
1476 return -TARGET_EFAULT;
1477 }
1478 arg_sigset = tswapal(arg7[0]);
1479 arg_sigsize = tswapal(arg7[1]);
1480 unlock_user(arg7, arg6, 0);
1481
1482 if (arg_sigset) {
1483 sig.set = &set;
1484 if (arg_sigsize != sizeof(*target_sigset)) {
1485 /* Like the kernel, we enforce correct size sigsets */
1486 return -TARGET_EINVAL;
1487 }
1488 target_sigset = lock_user(VERIFY_READ, arg_sigset,
1489 sizeof(*target_sigset), 1);
1490 if (!target_sigset) {
1491 return -TARGET_EFAULT;
1492 }
1493 target_to_host_sigset(&set, target_sigset);
1494 unlock_user(target_sigset, arg_sigset, 0);
1495 } else {
1496 sig.set = NULL;
1497 }
1498 } else {
1499 sig_ptr = NULL;
1500 }
1501
1502 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1503 ts_ptr, sig_ptr));
1504
1505 if (!is_error(ret)) {
1506 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
1507 return -TARGET_EFAULT;
1508 }
1509 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
1510 return -TARGET_EFAULT;
1511 }
1512 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
1513 return -TARGET_EFAULT;
1514 }
1515 if (time64) {
1516 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
1517 return -TARGET_EFAULT;
1518 }
1519 } else {
1520 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
1521 return -TARGET_EFAULT;
1522 }
1523 }
1524 }
1525 return ret;
1526}
1527#endif
1528
1529#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1530 defined(TARGET_NR_ppoll_time64)
1531static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
1532 abi_long arg4, abi_long arg5, bool ppoll, bool time64)
1533{
1534 struct target_pollfd *target_pfd;
1535 unsigned int nfds = arg2;
1536 struct pollfd *pfd;
1537 unsigned int i;
1538 abi_long ret;
1539
1540 pfd = NULL;
1541 target_pfd = NULL;
1542 if (nfds) {
1543 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
1544 return -TARGET_EINVAL;
1545 }
1546 target_pfd = lock_user(VERIFY_WRITE, arg1,
1547 sizeof(struct target_pollfd) * nfds, 1);
1548 if (!target_pfd) {
1549 return -TARGET_EFAULT;
1550 }
1551
1552 pfd = alloca(sizeof(struct pollfd) * nfds);
1553 for (i = 0; i < nfds; i++) {
1554 pfd[i].fd = tswap32(target_pfd[i].fd);
1555 pfd[i].events = tswap16(target_pfd[i].events);
1556 }
1557 }
1558 if (ppoll) {
1559 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
1560 target_sigset_t *target_set;
1561 sigset_t _set, *set = &_set;
1562
1563 if (arg3) {
1564 if (time64) {
1565 if (target_to_host_timespec64(timeout_ts, arg3)) {
1566 unlock_user(target_pfd, arg1, 0);
1567 return -TARGET_EFAULT;
1568 }
1569 } else {
1570 if (target_to_host_timespec(timeout_ts, arg3)) {
1571 unlock_user(target_pfd, arg1, 0);
1572 return -TARGET_EFAULT;
1573 }
1574 }
1575 } else {
1576 timeout_ts = NULL;
1577 }
1578
1579 if (arg4) {
1580 if (arg5 != sizeof(target_sigset_t)) {
1581 unlock_user(target_pfd, arg1, 0);
1582 return -TARGET_EINVAL;
1583 }
1584
1585 target_set = lock_user(VERIFY_READ, arg4,
1586 sizeof(target_sigset_t), 1);
1587 if (!target_set) {
1588 unlock_user(target_pfd, arg1, 0);
1589 return -TARGET_EFAULT;
1590 }
1591 target_to_host_sigset(set, target_set);
1592 } else {
1593 set = NULL;
1594 }
1595
1596 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
1597 set, SIGSET_T_SIZE));
1598
1599 if (!is_error(ret) && arg3) {
1600 if (time64) {
1601 if (host_to_target_timespec64(arg3, timeout_ts)) {
1602 return -TARGET_EFAULT;
1603 }
1604 } else {
1605 if (host_to_target_timespec(arg3, timeout_ts)) {
1606 return -TARGET_EFAULT;
1607 }
1608 }
1609 }
1610 if (arg4) {
1611 unlock_user(target_set, arg4, 0);
1612 }
1613 } else {
1614 struct timespec ts, *pts;
1615
1616 if (arg3 >= 0) {
1617 /* Convert ms to secs, ns */
1618 ts.tv_sec = arg3 / 1000;
1619 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
1620 pts = &ts;
1621 } else {
1622 /* -ve poll() timeout means "infinite" */
1623 pts = NULL;
1624 }
1625 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
1626 }
1627
1628 if (!is_error(ret)) {
1629 for (i = 0; i < nfds; i++) {
1630 target_pfd[i].revents = tswap16(pfd[i].revents);
1631 }
1632 }
1633 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
1634 return ret;
1635}
1636#endif
1637
Riku Voipio099d6b02009-05-05 12:10:04 +03001638static abi_long do_pipe2(int host_pipe[], int flags)
1639{
1640#ifdef CONFIG_PIPE2
1641 return pipe2(host_pipe, flags);
1642#else
1643 return -ENOSYS;
1644#endif
1645}
1646
Richard Hendersonfb41a662010-05-03 10:07:52 -07001647static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1648 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001649{
1650 int host_pipe[2];
1651 abi_long ret;
1652 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1653
1654 if (is_error(ret))
1655 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001656
1657 /* Several targets have special calling conventions for the original
1658 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1659 if (!is_pipe2) {
1660#if defined(TARGET_ALPHA)
1661 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1662 return host_pipe[0];
1663#elif defined(TARGET_MIPS)
1664 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1665 return host_pipe[0];
1666#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001667 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001668 return host_pipe[0];
Peter Maydell82f05b62013-07-06 17:39:48 +01001669#elif defined(TARGET_SPARC)
1670 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1671 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001672#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001673 }
1674
Riku Voipio099d6b02009-05-05 12:10:04 +03001675 if (put_user_s32(host_pipe[0], pipedes)
1676 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1677 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001678 return get_errno(ret);
1679}
1680
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001681static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1682 abi_ulong target_addr,
1683 socklen_t len)
1684{
1685 struct target_ip_mreqn *target_smreqn;
1686
1687 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1688 if (!target_smreqn)
1689 return -TARGET_EFAULT;
1690 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1691 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1692 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001693 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001694 unlock_user(target_smreqn, target_addr, 0);
1695
1696 return 0;
1697}
1698
Laurent Vivier7b36f782015-10-28 21:40:44 +01001699static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
bellard579a97f2007-11-11 14:26:47 +00001700 abi_ulong target_addr,
1701 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001702{
aurel32607175e2009-04-15 16:11:59 +00001703 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1704 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001705 struct target_sockaddr *target_saddr;
1706
Laurent Vivier7b36f782015-10-28 21:40:44 +01001707 if (fd_trans_target_to_host_addr(fd)) {
1708 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1709 }
1710
bellard579a97f2007-11-11 14:26:47 +00001711 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1712 if (!target_saddr)
1713 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001714
1715 sa_family = tswap16(target_saddr->sa_family);
1716
1717 /* Oops. The caller might send a incomplete sun_path; sun_path
1718 * must be terminated by \0 (see the manual page), but
1719 * unfortunately it is quite common to specify sockaddr_un
1720 * length as "strlen(x->sun_path)" while it should be
1721 * "strlen(...) + 1". We'll fix that here if needed.
1722 * Linux kernel has a similar feature.
1723 */
1724
1725 if (sa_family == AF_UNIX) {
1726 if (len < unix_maxlen && len > 0) {
1727 char *cp = (char*)target_saddr;
1728
1729 if ( cp[len-1] && !cp[len] )
1730 len++;
1731 }
1732 if (len > unix_maxlen)
1733 len = unix_maxlen;
1734 }
1735
pbrook53a59602006-03-25 19:31:22 +00001736 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001737 addr->sa_family = sa_family;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001738 if (sa_family == AF_NETLINK) {
1739 struct sockaddr_nl *nladdr;
1740
1741 nladdr = (struct sockaddr_nl *)addr;
1742 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1743 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1744 } else if (sa_family == AF_PACKET) {
Joakim Tjernlund33a29b52014-07-12 15:47:07 +02001745 struct target_sockaddr_ll *lladdr;
1746
1747 lladdr = (struct target_sockaddr_ll *)addr;
1748 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1749 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1750 }
pbrook53a59602006-03-25 19:31:22 +00001751 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001752
1753 return 0;
bellard7854b052003-03-29 17:22:23 +00001754}
1755
bellard579a97f2007-11-11 14:26:47 +00001756static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1757 struct sockaddr *addr,
1758 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001759{
pbrook53a59602006-03-25 19:31:22 +00001760 struct target_sockaddr *target_saddr;
1761
Peter Maydella1e22192016-07-07 15:44:43 +01001762 if (len == 0) {
1763 return 0;
1764 }
Philippe Mathieu-Daudé68607102017-07-26 23:42:18 -03001765 assert(addr);
Peter Maydella1e22192016-07-07 15:44:43 +01001766
bellard579a97f2007-11-11 14:26:47 +00001767 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1768 if (!target_saddr)
1769 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001770 memcpy(target_saddr, addr, len);
Peter Maydella1e22192016-07-07 15:44:43 +01001771 if (len >= offsetof(struct target_sockaddr, sa_family) +
1772 sizeof(target_saddr->sa_family)) {
1773 target_saddr->sa_family = tswap16(addr->sa_family);
1774 }
Philippe Mathieu-Daudéa47401b2019-10-21 13:48:52 +02001775 if (addr->sa_family == AF_NETLINK &&
1776 len >= sizeof(struct target_sockaddr_nl)) {
1777 struct target_sockaddr_nl *target_nl =
1778 (struct target_sockaddr_nl *)target_saddr;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001779 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1780 target_nl->nl_groups = tswap32(target_nl->nl_groups);
Laurent Viviera82ea932016-06-27 00:18:22 +02001781 } else if (addr->sa_family == AF_PACKET) {
1782 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1783 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1784 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
Helge Delleree1ac3a2017-02-18 23:31:30 +01001785 } else if (addr->sa_family == AF_INET6 &&
1786 len >= sizeof(struct target_sockaddr_in6)) {
1787 struct target_sockaddr_in6 *target_in6 =
1788 (struct target_sockaddr_in6 *)target_saddr;
1789 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001790 }
pbrook53a59602006-03-25 19:31:22 +00001791 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001792
1793 return 0;
bellard7854b052003-03-29 17:22:23 +00001794}
1795
bellard5a4a8982007-11-11 17:39:18 +00001796static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1797 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001798{
1799 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001800 abi_long msg_controllen;
1801 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001802 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001803 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001804
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001805 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001806 if (msg_controllen < sizeof (struct target_cmsghdr))
1807 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001808 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001809 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001810 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001811 if (!target_cmsg)
1812 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001813
1814 while (cmsg && target_cmsg) {
1815 void *data = CMSG_DATA(cmsg);
1816 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1817
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001818 int len = tswapal(target_cmsg->cmsg_len)
Peter Maydellad762b92017-12-15 13:52:56 +00001819 - sizeof(struct target_cmsghdr);
bellard7854b052003-03-29 17:22:23 +00001820
1821 space += CMSG_SPACE(len);
1822 if (space > msgh->msg_controllen) {
1823 space -= CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001824 /* This is a QEMU bug, since we allocated the payload
1825 * area ourselves (unlike overflow in host-to-target
1826 * conversion, which is just the guest giving us a buffer
1827 * that's too small). It can't happen for the payload types
1828 * we currently support; if it becomes an issue in future
1829 * we would need to improve our allocation strategy to
1830 * something more intelligent than "twice the size of the
1831 * target buffer we're reading from".
1832 */
Josh Kunz39be5352020-02-03 18:54:13 -08001833 qemu_log_mask(LOG_UNIMP,
1834 ("Unsupported ancillary data %d/%d: "
1835 "unhandled msg size\n"),
1836 tswap32(target_cmsg->cmsg_level),
1837 tswap32(target_cmsg->cmsg_type));
bellard7854b052003-03-29 17:22:23 +00001838 break;
1839 }
1840
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001841 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1842 cmsg->cmsg_level = SOL_SOCKET;
1843 } else {
1844 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1845 }
bellard7854b052003-03-29 17:22:23 +00001846 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1847 cmsg->cmsg_len = CMSG_LEN(len);
1848
Alex Suykov30b8b682014-12-23 07:52:58 +02001849 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001850 int *fd = (int *)data;
1851 int *target_fd = (int *)target_data;
1852 int i, numfds = len / sizeof(int);
1853
Peter Maydell876e23c2015-05-26 19:46:32 +01001854 for (i = 0; i < numfds; i++) {
1855 __get_user(fd[i], target_fd + i);
1856 }
Alex Suykov30b8b682014-12-23 07:52:58 +02001857 } else if (cmsg->cmsg_level == SOL_SOCKET
1858 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1859 struct ucred *cred = (struct ucred *)data;
1860 struct target_ucred *target_cred =
1861 (struct target_ucred *)target_data;
1862
Peter Maydell876e23c2015-05-26 19:46:32 +01001863 __get_user(cred->pid, &target_cred->pid);
1864 __get_user(cred->uid, &target_cred->uid);
1865 __get_user(cred->gid, &target_cred->gid);
Alex Suykov30b8b682014-12-23 07:52:58 +02001866 } else {
Josh Kunz39be5352020-02-03 18:54:13 -08001867 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
1868 cmsg->cmsg_level, cmsg->cmsg_type);
Alex Suykov30b8b682014-12-23 07:52:58 +02001869 memcpy(data, target_data, len);
bellard7854b052003-03-29 17:22:23 +00001870 }
1871
1872 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001873 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1874 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001875 }
bellard5a4a8982007-11-11 17:39:18 +00001876 unlock_user(target_cmsg, target_cmsg_addr, 0);
1877 the_end:
bellard7854b052003-03-29 17:22:23 +00001878 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001879 return 0;
bellard7854b052003-03-29 17:22:23 +00001880}
1881
bellard5a4a8982007-11-11 17:39:18 +00001882static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1883 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001884{
1885 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001886 abi_long msg_controllen;
1887 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001888 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001889 socklen_t space = 0;
1890
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001891 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001892 if (msg_controllen < sizeof (struct target_cmsghdr))
1893 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001894 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001895 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001896 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001897 if (!target_cmsg)
1898 return -TARGET_EFAULT;
1899
bellard7854b052003-03-29 17:22:23 +00001900 while (cmsg && target_cmsg) {
1901 void *data = CMSG_DATA(cmsg);
1902 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1903
Peter Maydellad762b92017-12-15 13:52:56 +00001904 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001905 int tgt_len, tgt_space;
bellard7854b052003-03-29 17:22:23 +00001906
Peter Maydellc2aeb252015-05-26 19:46:31 +01001907 /* We never copy a half-header but may copy half-data;
1908 * this is Linux's behaviour in put_cmsg(). Note that
1909 * truncation here is a guest problem (which we report
1910 * to the guest via the CTRUNC bit), unlike truncation
1911 * in target_to_host_cmsg, which is a QEMU bug.
1912 */
Peter Maydell71749702017-12-15 13:52:55 +00001913 if (msg_controllen < sizeof(struct target_cmsghdr)) {
Peter Maydellc2aeb252015-05-26 19:46:31 +01001914 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
bellard7854b052003-03-29 17:22:23 +00001915 break;
1916 }
1917
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001918 if (cmsg->cmsg_level == SOL_SOCKET) {
1919 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1920 } else {
1921 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1922 }
bellard7854b052003-03-29 17:22:23 +00001923 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
bellard7854b052003-03-29 17:22:23 +00001924
Peter Maydellc2aeb252015-05-26 19:46:31 +01001925 /* Payload types which need a different size of payload on
1926 * the target must adjust tgt_len here.
1927 */
Peter Maydell309786c2018-05-18 19:47:15 +01001928 tgt_len = len;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001929 switch (cmsg->cmsg_level) {
1930 case SOL_SOCKET:
1931 switch (cmsg->cmsg_type) {
1932 case SO_TIMESTAMP:
1933 tgt_len = sizeof(struct target_timeval);
1934 break;
1935 default:
1936 break;
1937 }
Peter Maydell309786c2018-05-18 19:47:15 +01001938 break;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001939 default:
1940 break;
1941 }
1942
Peter Maydell71749702017-12-15 13:52:55 +00001943 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
Peter Maydellc2aeb252015-05-26 19:46:31 +01001944 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
Peter Maydell71749702017-12-15 13:52:55 +00001945 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001946 }
1947
1948 /* We must now copy-and-convert len bytes of payload
1949 * into tgt_len bytes of destination space. Bear in mind
1950 * that in both source and destination we may be dealing
1951 * with a truncated value!
1952 */
Huw Davies52b65492014-04-17 14:02:47 +01001953 switch (cmsg->cmsg_level) {
1954 case SOL_SOCKET:
1955 switch (cmsg->cmsg_type) {
1956 case SCM_RIGHTS:
1957 {
1958 int *fd = (int *)data;
1959 int *target_fd = (int *)target_data;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001960 int i, numfds = tgt_len / sizeof(int);
bellard7854b052003-03-29 17:22:23 +00001961
Peter Maydell876e23c2015-05-26 19:46:32 +01001962 for (i = 0; i < numfds; i++) {
1963 __put_user(fd[i], target_fd + i);
1964 }
Huw Davies52b65492014-04-17 14:02:47 +01001965 break;
1966 }
1967 case SO_TIMESTAMP:
1968 {
1969 struct timeval *tv = (struct timeval *)data;
1970 struct target_timeval *target_tv =
1971 (struct target_timeval *)target_data;
Jing Huangaebf5bc2012-07-24 14:01:42 +00001972
Peter Maydellc2aeb252015-05-26 19:46:31 +01001973 if (len != sizeof(struct timeval) ||
1974 tgt_len != sizeof(struct target_timeval)) {
Huw Davies52b65492014-04-17 14:02:47 +01001975 goto unimplemented;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001976 }
Huw Davies52b65492014-04-17 14:02:47 +01001977
1978 /* copy struct timeval to target */
Peter Maydell876e23c2015-05-26 19:46:32 +01001979 __put_user(tv->tv_sec, &target_tv->tv_sec);
1980 __put_user(tv->tv_usec, &target_tv->tv_usec);
Huw Davies52b65492014-04-17 14:02:47 +01001981 break;
1982 }
Huw Davies4bc29752014-04-17 14:02:48 +01001983 case SCM_CREDENTIALS:
1984 {
1985 struct ucred *cred = (struct ucred *)data;
1986 struct target_ucred *target_cred =
1987 (struct target_ucred *)target_data;
1988
1989 __put_user(cred->pid, &target_cred->pid);
1990 __put_user(cred->uid, &target_cred->uid);
1991 __put_user(cred->gid, &target_cred->gid);
1992 break;
1993 }
Huw Davies52b65492014-04-17 14:02:47 +01001994 default:
1995 goto unimplemented;
1996 }
1997 break;
1998
Helge Delleree1ac3a2017-02-18 23:31:30 +01001999 case SOL_IP:
2000 switch (cmsg->cmsg_type) {
2001 case IP_TTL:
2002 {
2003 uint32_t *v = (uint32_t *)data;
2004 uint32_t *t_int = (uint32_t *)target_data;
2005
Peter Maydell71749702017-12-15 13:52:55 +00002006 if (len != sizeof(uint32_t) ||
2007 tgt_len != sizeof(uint32_t)) {
2008 goto unimplemented;
2009 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002010 __put_user(*v, t_int);
2011 break;
2012 }
2013 case IP_RECVERR:
2014 {
2015 struct errhdr_t {
2016 struct sock_extended_err ee;
2017 struct sockaddr_in offender;
2018 };
2019 struct errhdr_t *errh = (struct errhdr_t *)data;
2020 struct errhdr_t *target_errh =
2021 (struct errhdr_t *)target_data;
2022
Peter Maydell71749702017-12-15 13:52:55 +00002023 if (len != sizeof(struct errhdr_t) ||
2024 tgt_len != sizeof(struct errhdr_t)) {
2025 goto unimplemented;
2026 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002027 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
2028 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
2029 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
2030 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
2031 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
2032 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
2033 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
2034 host_to_target_sockaddr((unsigned long) &target_errh->offender,
2035 (void *) &errh->offender, sizeof(errh->offender));
2036 break;
2037 }
2038 default:
2039 goto unimplemented;
2040 }
2041 break;
2042
2043 case SOL_IPV6:
2044 switch (cmsg->cmsg_type) {
2045 case IPV6_HOPLIMIT:
2046 {
2047 uint32_t *v = (uint32_t *)data;
2048 uint32_t *t_int = (uint32_t *)target_data;
2049
Peter Maydell71749702017-12-15 13:52:55 +00002050 if (len != sizeof(uint32_t) ||
2051 tgt_len != sizeof(uint32_t)) {
2052 goto unimplemented;
2053 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002054 __put_user(*v, t_int);
2055 break;
2056 }
2057 case IPV6_RECVERR:
2058 {
2059 struct errhdr6_t {
2060 struct sock_extended_err ee;
2061 struct sockaddr_in6 offender;
2062 };
2063 struct errhdr6_t *errh = (struct errhdr6_t *)data;
2064 struct errhdr6_t *target_errh =
2065 (struct errhdr6_t *)target_data;
2066
Peter Maydell71749702017-12-15 13:52:55 +00002067 if (len != sizeof(struct errhdr6_t) ||
2068 tgt_len != sizeof(struct errhdr6_t)) {
2069 goto unimplemented;
2070 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002071 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
2072 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
2073 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
2074 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
2075 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
2076 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
2077 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
2078 host_to_target_sockaddr((unsigned long) &target_errh->offender,
2079 (void *) &errh->offender, sizeof(errh->offender));
2080 break;
2081 }
2082 default:
2083 goto unimplemented;
2084 }
2085 break;
2086
Huw Davies52b65492014-04-17 14:02:47 +01002087 default:
2088 unimplemented:
Josh Kunz39be5352020-02-03 18:54:13 -08002089 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
2090 cmsg->cmsg_level, cmsg->cmsg_type);
Peter Maydellc2aeb252015-05-26 19:46:31 +01002091 memcpy(target_data, data, MIN(len, tgt_len));
2092 if (tgt_len > len) {
2093 memset(target_data + len, 0, tgt_len - len);
2094 }
bellard7854b052003-03-29 17:22:23 +00002095 }
2096
Peter Maydell71749702017-12-15 13:52:55 +00002097 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
2098 tgt_space = TARGET_CMSG_SPACE(tgt_len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01002099 if (msg_controllen < tgt_space) {
2100 tgt_space = msg_controllen;
2101 }
2102 msg_controllen -= tgt_space;
2103 space += tgt_space;
bellard7854b052003-03-29 17:22:23 +00002104 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02002105 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
2106 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00002107 }
bellard5a4a8982007-11-11 17:39:18 +00002108 unlock_user(target_cmsg, target_cmsg_addr, space);
2109 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002110 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00002111 return 0;
bellard7854b052003-03-29 17:22:23 +00002112}
2113
ths0da46a62007-10-20 20:23:07 +00002114/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002115static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00002116 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00002117{
blueswir1992f48a2007-10-14 16:27:31 +00002118 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00002119 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02002120 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002121 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00002122
bellard8853f862004-02-22 14:57:26 +00002123 switch(level) {
2124 case SOL_TCP:
Shu-Chun Wengfe51b0a2020-12-18 11:32:11 -08002125 case SOL_UDP:
2126 /* TCP and UDP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00002127 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00002128 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00002129
bellard2f619692007-11-16 10:46:05 +00002130 if (get_user_u32(val, optval_addr))
2131 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002132 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2133 break;
2134 case SOL_IP:
2135 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00002136 case IP_TOS:
2137 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00002138 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00002139 case IP_ROUTER_ALERT:
2140 case IP_RECVOPTS:
2141 case IP_RETOPTS:
2142 case IP_PKTINFO:
2143 case IP_MTU_DISCOVER:
2144 case IP_RECVERR:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002145 case IP_RECVTTL:
bellard2efbe912005-07-23 15:10:20 +00002146 case IP_RECVTOS:
2147#ifdef IP_FREEBIND
2148 case IP_FREEBIND:
2149#endif
2150 case IP_MULTICAST_TTL:
2151 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00002152 val = 0;
2153 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00002154 if (get_user_u32(val, optval_addr))
2155 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002156 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00002157 if (get_user_u8(val, optval_addr))
2158 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002159 }
2160 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2161 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02002162 case IP_ADD_MEMBERSHIP:
2163 case IP_DROP_MEMBERSHIP:
2164 if (optlen < sizeof (struct target_ip_mreq) ||
2165 optlen > sizeof (struct target_ip_mreqn))
2166 return -TARGET_EINVAL;
2167
2168 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2169 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2170 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2171 break;
2172
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002173 case IP_BLOCK_SOURCE:
2174 case IP_UNBLOCK_SOURCE:
2175 case IP_ADD_SOURCE_MEMBERSHIP:
2176 case IP_DROP_SOURCE_MEMBERSHIP:
2177 if (optlen != sizeof (struct target_ip_mreq_source))
2178 return -TARGET_EINVAL;
2179
2180 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
Peter Maydell74e43b02021-08-09 16:54:24 +01002181 if (!ip_mreq_source) {
2182 return -TARGET_EFAULT;
2183 }
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002184 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2185 unlock_user (ip_mreq_source, optval_addr, 0);
2186 break;
2187
bellard8853f862004-02-22 14:57:26 +00002188 default:
2189 goto unimplemented;
2190 }
2191 break;
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002192 case SOL_IPV6:
2193 switch (optname) {
2194 case IPV6_MTU_DISCOVER:
2195 case IPV6_MTU:
2196 case IPV6_V6ONLY:
2197 case IPV6_RECVPKTINFO:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002198 case IPV6_UNICAST_HOPS:
Laurent Vivier21749c42018-06-27 23:21:52 +02002199 case IPV6_MULTICAST_HOPS:
2200 case IPV6_MULTICAST_LOOP:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002201 case IPV6_RECVERR:
2202 case IPV6_RECVHOPLIMIT:
2203 case IPV6_2292HOPLIMIT:
2204 case IPV6_CHECKSUM:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002205 case IPV6_ADDRFORM:
2206 case IPV6_2292PKTINFO:
2207 case IPV6_RECVTCLASS:
2208 case IPV6_RECVRTHDR:
2209 case IPV6_2292RTHDR:
2210 case IPV6_RECVHOPOPTS:
2211 case IPV6_2292HOPOPTS:
2212 case IPV6_RECVDSTOPTS:
2213 case IPV6_2292DSTOPTS:
2214 case IPV6_TCLASS:
Shu-Chun Weng22db1212020-12-18 11:32:12 -08002215 case IPV6_ADDR_PREFERENCES:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002216#ifdef IPV6_RECVPATHMTU
2217 case IPV6_RECVPATHMTU:
2218#endif
2219#ifdef IPV6_TRANSPARENT
2220 case IPV6_TRANSPARENT:
2221#endif
2222#ifdef IPV6_FREEBIND
2223 case IPV6_FREEBIND:
2224#endif
2225#ifdef IPV6_RECVORIGDSTADDR
2226 case IPV6_RECVORIGDSTADDR:
2227#endif
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002228 val = 0;
2229 if (optlen < sizeof(uint32_t)) {
2230 return -TARGET_EINVAL;
2231 }
2232 if (get_user_u32(val, optval_addr)) {
2233 return -TARGET_EFAULT;
2234 }
2235 ret = get_errno(setsockopt(sockfd, level, optname,
2236 &val, sizeof(val)));
2237 break;
Helge Delleree1ac3a2017-02-18 23:31:30 +01002238 case IPV6_PKTINFO:
2239 {
2240 struct in6_pktinfo pki;
2241
2242 if (optlen < sizeof(pki)) {
2243 return -TARGET_EINVAL;
2244 }
2245
2246 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
2247 return -TARGET_EFAULT;
2248 }
2249
2250 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
2251
2252 ret = get_errno(setsockopt(sockfd, level, optname,
2253 &pki, sizeof(pki)));
2254 break;
2255 }
Neng Chen22bf4ee2019-06-19 16:17:10 +02002256 case IPV6_ADD_MEMBERSHIP:
2257 case IPV6_DROP_MEMBERSHIP:
2258 {
2259 struct ipv6_mreq ipv6mreq;
2260
2261 if (optlen < sizeof(ipv6mreq)) {
2262 return -TARGET_EINVAL;
2263 }
2264
2265 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
2266 return -TARGET_EFAULT;
2267 }
2268
2269 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
2270
2271 ret = get_errno(setsockopt(sockfd, level, optname,
2272 &ipv6mreq, sizeof(ipv6mreq)));
2273 break;
2274 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002275 default:
2276 goto unimplemented;
2277 }
2278 break;
2279 case SOL_ICMPV6:
2280 switch (optname) {
2281 case ICMPV6_FILTER:
2282 {
2283 struct icmp6_filter icmp6f;
2284
2285 if (optlen > sizeof(icmp6f)) {
2286 optlen = sizeof(icmp6f);
2287 }
2288
2289 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
2290 return -TARGET_EFAULT;
2291 }
2292
2293 for (val = 0; val < 8; val++) {
2294 icmp6f.data[val] = tswap32(icmp6f.data[val]);
2295 }
2296
2297 ret = get_errno(setsockopt(sockfd, level, optname,
2298 &icmp6f, optlen));
2299 break;
2300 }
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002301 default:
2302 goto unimplemented;
2303 }
2304 break;
Jing Huang920394d2012-07-24 13:59:23 +00002305 case SOL_RAW:
2306 switch (optname) {
2307 case ICMP_FILTER:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002308 case IPV6_CHECKSUM:
2309 /* those take an u32 value */
Jing Huang920394d2012-07-24 13:59:23 +00002310 if (optlen < sizeof(uint32_t)) {
2311 return -TARGET_EINVAL;
2312 }
2313
2314 if (get_user_u32(val, optval_addr)) {
2315 return -TARGET_EFAULT;
2316 }
2317 ret = get_errno(setsockopt(sockfd, level, optname,
2318 &val, sizeof(val)));
2319 break;
2320
2321 default:
2322 goto unimplemented;
2323 }
2324 break;
Yunqiang Suf31dddd2019-06-19 16:17:11 +02002325#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2326 case SOL_ALG:
2327 switch (optname) {
2328 case ALG_SET_KEY:
2329 {
2330 char *alg_key = g_malloc(optlen);
2331
2332 if (!alg_key) {
2333 return -TARGET_ENOMEM;
2334 }
2335 if (copy_from_user(alg_key, optval_addr, optlen)) {
2336 g_free(alg_key);
2337 return -TARGET_EFAULT;
2338 }
2339 ret = get_errno(setsockopt(sockfd, level, optname,
2340 alg_key, optlen));
2341 g_free(alg_key);
2342 break;
2343 }
2344 case ALG_SET_AEAD_AUTHSIZE:
2345 {
2346 ret = get_errno(setsockopt(sockfd, level, optname,
2347 NULL, optlen));
2348 break;
2349 }
2350 default:
2351 goto unimplemented;
2352 }
2353 break;
2354#endif
bellard3532fa72006-06-24 15:06:03 +00002355 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00002356 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00002357 case TARGET_SO_RCVTIMEO:
2358 {
2359 struct timeval tv;
2360
2361 optname = SO_RCVTIMEO;
2362
2363set_timeout:
2364 if (optlen != sizeof(struct target_timeval)) {
2365 return -TARGET_EINVAL;
2366 }
2367
2368 if (copy_from_user_timeval(&tv, optval_addr)) {
2369 return -TARGET_EFAULT;
2370 }
2371
2372 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2373 &tv, sizeof(tv)));
2374 return ret;
2375 }
2376 case TARGET_SO_SNDTIMEO:
2377 optname = SO_SNDTIMEO;
2378 goto set_timeout;
Laurent Vivierf57d4192013-08-30 01:46:41 +02002379 case TARGET_SO_ATTACH_FILTER:
2380 {
2381 struct target_sock_fprog *tfprog;
2382 struct target_sock_filter *tfilter;
2383 struct sock_fprog fprog;
2384 struct sock_filter *filter;
2385 int i;
2386
2387 if (optlen != sizeof(*tfprog)) {
2388 return -TARGET_EINVAL;
2389 }
2390 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2391 return -TARGET_EFAULT;
2392 }
2393 if (!lock_user_struct(VERIFY_READ, tfilter,
2394 tswapal(tfprog->filter), 0)) {
2395 unlock_user_struct(tfprog, optval_addr, 1);
2396 return -TARGET_EFAULT;
2397 }
2398
2399 fprog.len = tswap16(tfprog->len);
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302400 filter = g_try_new(struct sock_filter, fprog.len);
Laurent Vivierf57d4192013-08-30 01:46:41 +02002401 if (filter == NULL) {
2402 unlock_user_struct(tfilter, tfprog->filter, 1);
2403 unlock_user_struct(tfprog, optval_addr, 1);
2404 return -TARGET_ENOMEM;
2405 }
2406 for (i = 0; i < fprog.len; i++) {
2407 filter[i].code = tswap16(tfilter[i].code);
2408 filter[i].jt = tfilter[i].jt;
2409 filter[i].jf = tfilter[i].jf;
2410 filter[i].k = tswap32(tfilter[i].k);
2411 }
2412 fprog.filter = filter;
2413
2414 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2415 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302416 g_free(filter);
Laurent Vivierf57d4192013-08-30 01:46:41 +02002417
2418 unlock_user_struct(tfilter, tfprog->filter, 1);
2419 unlock_user_struct(tfprog, optval_addr, 1);
2420 return ret;
2421 }
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02002422 case TARGET_SO_BINDTODEVICE:
2423 {
2424 char *dev_ifname, *addr_ifname;
2425
2426 if (optlen > IFNAMSIZ - 1) {
2427 optlen = IFNAMSIZ - 1;
2428 }
2429 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2430 if (!dev_ifname) {
2431 return -TARGET_EFAULT;
2432 }
2433 optname = SO_BINDTODEVICE;
2434 addr_ifname = alloca(IFNAMSIZ);
2435 memcpy(addr_ifname, dev_ifname, optlen);
2436 addr_ifname[optlen] = 0;
Chen Gangfad6c582016-01-11 13:58:50 +08002437 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2438 addr_ifname, optlen));
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02002439 unlock_user (dev_ifname, optval_addr, 0);
2440 return ret;
2441 }
Carlo Marcelo Arenas BelĂłn83eb6e52018-08-24 01:56:01 -07002442 case TARGET_SO_LINGER:
2443 {
2444 struct linger lg;
2445 struct target_linger *tlg;
2446
2447 if (optlen != sizeof(struct target_linger)) {
2448 return -TARGET_EINVAL;
2449 }
2450 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
2451 return -TARGET_EFAULT;
2452 }
2453 __get_user(lg.l_onoff, &tlg->l_onoff);
2454 __get_user(lg.l_linger, &tlg->l_linger);
2455 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
2456 &lg, sizeof(lg)));
2457 unlock_user_struct(tlg, optval_addr, 0);
2458 return ret;
2459 }
bellard8853f862004-02-22 14:57:26 +00002460 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00002461 case TARGET_SO_DEBUG:
2462 optname = SO_DEBUG;
2463 break;
2464 case TARGET_SO_REUSEADDR:
2465 optname = SO_REUSEADDR;
2466 break;
Yunqiang Su113a9dd2018-10-30 13:55:08 +01002467#ifdef SO_REUSEPORT
2468 case TARGET_SO_REUSEPORT:
2469 optname = SO_REUSEPORT;
2470 break;
2471#endif
bellard3532fa72006-06-24 15:06:03 +00002472 case TARGET_SO_TYPE:
2473 optname = SO_TYPE;
2474 break;
2475 case TARGET_SO_ERROR:
2476 optname = SO_ERROR;
2477 break;
2478 case TARGET_SO_DONTROUTE:
2479 optname = SO_DONTROUTE;
2480 break;
2481 case TARGET_SO_BROADCAST:
2482 optname = SO_BROADCAST;
2483 break;
2484 case TARGET_SO_SNDBUF:
2485 optname = SO_SNDBUF;
2486 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01002487 case TARGET_SO_SNDBUFFORCE:
2488 optname = SO_SNDBUFFORCE;
2489 break;
bellard3532fa72006-06-24 15:06:03 +00002490 case TARGET_SO_RCVBUF:
2491 optname = SO_RCVBUF;
2492 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01002493 case TARGET_SO_RCVBUFFORCE:
2494 optname = SO_RCVBUFFORCE;
2495 break;
bellard3532fa72006-06-24 15:06:03 +00002496 case TARGET_SO_KEEPALIVE:
2497 optname = SO_KEEPALIVE;
2498 break;
2499 case TARGET_SO_OOBINLINE:
2500 optname = SO_OOBINLINE;
2501 break;
2502 case TARGET_SO_NO_CHECK:
2503 optname = SO_NO_CHECK;
2504 break;
2505 case TARGET_SO_PRIORITY:
2506 optname = SO_PRIORITY;
2507 break;
bellard5e83e8e2005-03-01 22:32:06 +00002508#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00002509 case TARGET_SO_BSDCOMPAT:
2510 optname = SO_BSDCOMPAT;
2511 break;
bellard5e83e8e2005-03-01 22:32:06 +00002512#endif
bellard3532fa72006-06-24 15:06:03 +00002513 case TARGET_SO_PASSCRED:
2514 optname = SO_PASSCRED;
2515 break;
Paul Burton82d0fe62014-06-22 11:25:36 +01002516 case TARGET_SO_PASSSEC:
2517 optname = SO_PASSSEC;
2518 break;
bellard3532fa72006-06-24 15:06:03 +00002519 case TARGET_SO_TIMESTAMP:
2520 optname = SO_TIMESTAMP;
2521 break;
2522 case TARGET_SO_RCVLOWAT:
2523 optname = SO_RCVLOWAT;
2524 break;
bellard8853f862004-02-22 14:57:26 +00002525 default:
2526 goto unimplemented;
2527 }
bellard3532fa72006-06-24 15:06:03 +00002528 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00002529 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00002530
bellard2f619692007-11-16 10:46:05 +00002531 if (get_user_u32(val, optval_addr))
2532 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002533 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00002534 break;
Josh Kunza2d86682019-10-29 15:43:10 -07002535#ifdef SOL_NETLINK
2536 case SOL_NETLINK:
2537 switch (optname) {
2538 case NETLINK_PKTINFO:
2539 case NETLINK_ADD_MEMBERSHIP:
2540 case NETLINK_DROP_MEMBERSHIP:
2541 case NETLINK_BROADCAST_ERROR:
2542 case NETLINK_NO_ENOBUFS:
2543#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2544 case NETLINK_LISTEN_ALL_NSID:
2545 case NETLINK_CAP_ACK:
2546#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2547#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2548 case NETLINK_EXT_ACK:
2549#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2550#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2551 case NETLINK_GET_STRICT_CHK:
2552#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2553 break;
2554 default:
2555 goto unimplemented;
2556 }
2557 val = 0;
2558 if (optlen < sizeof(uint32_t)) {
2559 return -TARGET_EINVAL;
2560 }
2561 if (get_user_u32(val, optval_addr)) {
2562 return -TARGET_EFAULT;
2563 }
2564 ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
2565 sizeof(val)));
2566 break;
2567#endif /* SOL_NETLINK */
bellard7854b052003-03-29 17:22:23 +00002568 default:
bellard8853f862004-02-22 14:57:26 +00002569 unimplemented:
Josh Kunz39be5352020-02-03 18:54:13 -08002570 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
2571 level, optname);
ths6fa13c12007-12-18 02:41:04 +00002572 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00002573 }
bellard8853f862004-02-22 14:57:26 +00002574 return ret;
bellard7854b052003-03-29 17:22:23 +00002575}
2576
ths0da46a62007-10-20 20:23:07 +00002577/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002578static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00002579 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00002580{
blueswir1992f48a2007-10-14 16:27:31 +00002581 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00002582 int len, val;
2583 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00002584
2585 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00002586 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002587 level = SOL_SOCKET;
2588 switch (optname) {
2589 /* These don't just return a single integer */
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002590 case TARGET_SO_PEERNAME:
2591 goto unimplemented;
Andreas Schwab405dc4c2019-05-13 11:06:26 +02002592 case TARGET_SO_RCVTIMEO: {
2593 struct timeval tv;
2594 socklen_t tvlen;
2595
2596 optname = SO_RCVTIMEO;
2597
2598get_timeout:
2599 if (get_user_u32(len, optlen)) {
2600 return -TARGET_EFAULT;
2601 }
2602 if (len < 0) {
2603 return -TARGET_EINVAL;
2604 }
2605
2606 tvlen = sizeof(tv);
2607 ret = get_errno(getsockopt(sockfd, level, optname,
2608 &tv, &tvlen));
2609 if (ret < 0) {
2610 return ret;
2611 }
2612 if (len > sizeof(struct target_timeval)) {
2613 len = sizeof(struct target_timeval);
2614 }
2615 if (copy_to_user_timeval(optval_addr, &tv)) {
2616 return -TARGET_EFAULT;
2617 }
2618 if (put_user_u32(len, optlen)) {
2619 return -TARGET_EFAULT;
2620 }
2621 break;
2622 }
2623 case TARGET_SO_SNDTIMEO:
2624 optname = SO_SNDTIMEO;
2625 goto get_timeout;
Akos PASZTORY583359a2011-11-14 15:09:49 +02002626 case TARGET_SO_PEERCRED: {
2627 struct ucred cr;
2628 socklen_t crlen;
2629 struct target_ucred *tcr;
2630
2631 if (get_user_u32(len, optlen)) {
2632 return -TARGET_EFAULT;
2633 }
2634 if (len < 0) {
2635 return -TARGET_EINVAL;
2636 }
2637
2638 crlen = sizeof(cr);
2639 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2640 &cr, &crlen));
2641 if (ret < 0) {
2642 return ret;
2643 }
2644 if (len > crlen) {
2645 len = crlen;
2646 }
2647 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2648 return -TARGET_EFAULT;
2649 }
2650 __put_user(cr.pid, &tcr->pid);
2651 __put_user(cr.uid, &tcr->uid);
2652 __put_user(cr.gid, &tcr->gid);
2653 unlock_user_struct(tcr, optval_addr, 1);
2654 if (put_user_u32(len, optlen)) {
2655 return -TARGET_EFAULT;
2656 }
2657 break;
2658 }
Laurent Vivier6d485a52020-02-04 22:19:01 +01002659 case TARGET_SO_PEERSEC: {
2660 char *name;
2661
2662 if (get_user_u32(len, optlen)) {
2663 return -TARGET_EFAULT;
2664 }
2665 if (len < 0) {
2666 return -TARGET_EINVAL;
2667 }
2668 name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
2669 if (!name) {
2670 return -TARGET_EFAULT;
2671 }
2672 lv = len;
2673 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
2674 name, &lv));
2675 if (put_user_u32(lv, optlen)) {
2676 ret = -TARGET_EFAULT;
2677 }
2678 unlock_user(name, optval_addr, lv);
2679 break;
2680 }
Carlo Marcelo Arenas BelĂłn83eb6e52018-08-24 01:56:01 -07002681 case TARGET_SO_LINGER:
2682 {
2683 struct linger lg;
2684 socklen_t lglen;
2685 struct target_linger *tlg;
2686
2687 if (get_user_u32(len, optlen)) {
2688 return -TARGET_EFAULT;
2689 }
2690 if (len < 0) {
2691 return -TARGET_EINVAL;
2692 }
2693
2694 lglen = sizeof(lg);
2695 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2696 &lg, &lglen));
2697 if (ret < 0) {
2698 return ret;
2699 }
2700 if (len > lglen) {
2701 len = lglen;
2702 }
2703 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
2704 return -TARGET_EFAULT;
2705 }
2706 __put_user(lg.l_onoff, &tlg->l_onoff);
2707 __put_user(lg.l_linger, &tlg->l_linger);
2708 unlock_user_struct(tlg, optval_addr, 1);
2709 if (put_user_u32(len, optlen)) {
2710 return -TARGET_EFAULT;
2711 }
2712 break;
2713 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002714 /* Options with 'int' argument. */
2715 case TARGET_SO_DEBUG:
2716 optname = SO_DEBUG;
2717 goto int_case;
2718 case TARGET_SO_REUSEADDR:
2719 optname = SO_REUSEADDR;
2720 goto int_case;
Yunqiang Su113a9dd2018-10-30 13:55:08 +01002721#ifdef SO_REUSEPORT
2722 case TARGET_SO_REUSEPORT:
2723 optname = SO_REUSEPORT;
2724 goto int_case;
2725#endif
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002726 case TARGET_SO_TYPE:
2727 optname = SO_TYPE;
2728 goto int_case;
2729 case TARGET_SO_ERROR:
2730 optname = SO_ERROR;
2731 goto int_case;
2732 case TARGET_SO_DONTROUTE:
2733 optname = SO_DONTROUTE;
2734 goto int_case;
2735 case TARGET_SO_BROADCAST:
2736 optname = SO_BROADCAST;
2737 goto int_case;
2738 case TARGET_SO_SNDBUF:
2739 optname = SO_SNDBUF;
2740 goto int_case;
2741 case TARGET_SO_RCVBUF:
2742 optname = SO_RCVBUF;
2743 goto int_case;
2744 case TARGET_SO_KEEPALIVE:
2745 optname = SO_KEEPALIVE;
2746 goto int_case;
2747 case TARGET_SO_OOBINLINE:
2748 optname = SO_OOBINLINE;
2749 goto int_case;
2750 case TARGET_SO_NO_CHECK:
2751 optname = SO_NO_CHECK;
2752 goto int_case;
2753 case TARGET_SO_PRIORITY:
2754 optname = SO_PRIORITY;
2755 goto int_case;
2756#ifdef SO_BSDCOMPAT
2757 case TARGET_SO_BSDCOMPAT:
2758 optname = SO_BSDCOMPAT;
2759 goto int_case;
2760#endif
2761 case TARGET_SO_PASSCRED:
2762 optname = SO_PASSCRED;
2763 goto int_case;
2764 case TARGET_SO_TIMESTAMP:
2765 optname = SO_TIMESTAMP;
2766 goto int_case;
2767 case TARGET_SO_RCVLOWAT:
2768 optname = SO_RCVLOWAT;
2769 goto int_case;
Paul Burtonaec1ca42014-06-22 11:25:34 +01002770 case TARGET_SO_ACCEPTCONN:
2771 optname = SO_ACCEPTCONN;
2772 goto int_case;
Jason A. Donenfeldec63e062021-02-04 16:39:25 +01002773 case TARGET_SO_PROTOCOL:
2774 optname = SO_PROTOCOL;
2775 goto int_case;
2776 case TARGET_SO_DOMAIN:
2777 optname = SO_DOMAIN;
2778 goto int_case;
bellard8853f862004-02-22 14:57:26 +00002779 default:
bellard2efbe912005-07-23 15:10:20 +00002780 goto int_case;
2781 }
2782 break;
2783 case SOL_TCP:
Shu-Chun Wengfe51b0a2020-12-18 11:32:11 -08002784 case SOL_UDP:
2785 /* TCP and UDP options all take an 'int' value. */
bellard2efbe912005-07-23 15:10:20 +00002786 int_case:
bellard2f619692007-11-16 10:46:05 +00002787 if (get_user_u32(len, optlen))
2788 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002789 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00002790 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05002791 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00002792 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2793 if (ret < 0)
2794 return ret;
Paul Burton8289d112014-06-22 11:25:33 +01002795 if (optname == SO_TYPE) {
2796 val = host_to_target_sock_type(val);
2797 }
bellard2efbe912005-07-23 15:10:20 +00002798 if (len > lv)
2799 len = lv;
bellard2f619692007-11-16 10:46:05 +00002800 if (len == 4) {
2801 if (put_user_u32(val, optval_addr))
2802 return -TARGET_EFAULT;
2803 } else {
2804 if (put_user_u8(val, optval_addr))
2805 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002806 }
bellard2f619692007-11-16 10:46:05 +00002807 if (put_user_u32(len, optlen))
2808 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002809 break;
2810 case SOL_IP:
2811 switch(optname) {
2812 case IP_TOS:
2813 case IP_TTL:
2814 case IP_HDRINCL:
2815 case IP_ROUTER_ALERT:
2816 case IP_RECVOPTS:
2817 case IP_RETOPTS:
2818 case IP_PKTINFO:
2819 case IP_MTU_DISCOVER:
2820 case IP_RECVERR:
2821 case IP_RECVTOS:
2822#ifdef IP_FREEBIND
2823 case IP_FREEBIND:
2824#endif
2825 case IP_MULTICAST_TTL:
2826 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00002827 if (get_user_u32(len, optlen))
2828 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002829 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00002830 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05002831 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00002832 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2833 if (ret < 0)
2834 return ret;
bellard2efbe912005-07-23 15:10:20 +00002835 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00002836 len = 1;
bellard2f619692007-11-16 10:46:05 +00002837 if (put_user_u32(len, optlen)
2838 || put_user_u8(val, optval_addr))
2839 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002840 } else {
bellard2efbe912005-07-23 15:10:20 +00002841 if (len > sizeof(int))
2842 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00002843 if (put_user_u32(len, optlen)
2844 || put_user_u32(val, optval_addr))
2845 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002846 }
bellard8853f862004-02-22 14:57:26 +00002847 break;
bellard2efbe912005-07-23 15:10:20 +00002848 default:
thsc02f4992007-12-18 02:39:59 +00002849 ret = -TARGET_ENOPROTOOPT;
2850 break;
bellard8853f862004-02-22 14:57:26 +00002851 }
2852 break;
Tom Deseynbd8ed482018-12-13 14:06:11 +01002853 case SOL_IPV6:
2854 switch (optname) {
2855 case IPV6_MTU_DISCOVER:
2856 case IPV6_MTU:
2857 case IPV6_V6ONLY:
2858 case IPV6_RECVPKTINFO:
2859 case IPV6_UNICAST_HOPS:
2860 case IPV6_MULTICAST_HOPS:
2861 case IPV6_MULTICAST_LOOP:
2862 case IPV6_RECVERR:
2863 case IPV6_RECVHOPLIMIT:
2864 case IPV6_2292HOPLIMIT:
2865 case IPV6_CHECKSUM:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002866 case IPV6_ADDRFORM:
2867 case IPV6_2292PKTINFO:
2868 case IPV6_RECVTCLASS:
2869 case IPV6_RECVRTHDR:
2870 case IPV6_2292RTHDR:
2871 case IPV6_RECVHOPOPTS:
2872 case IPV6_2292HOPOPTS:
2873 case IPV6_RECVDSTOPTS:
2874 case IPV6_2292DSTOPTS:
2875 case IPV6_TCLASS:
Shu-Chun Weng22db1212020-12-18 11:32:12 -08002876 case IPV6_ADDR_PREFERENCES:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002877#ifdef IPV6_RECVPATHMTU
2878 case IPV6_RECVPATHMTU:
2879#endif
2880#ifdef IPV6_TRANSPARENT
2881 case IPV6_TRANSPARENT:
2882#endif
2883#ifdef IPV6_FREEBIND
2884 case IPV6_FREEBIND:
2885#endif
2886#ifdef IPV6_RECVORIGDSTADDR
2887 case IPV6_RECVORIGDSTADDR:
2888#endif
Tom Deseynbd8ed482018-12-13 14:06:11 +01002889 if (get_user_u32(len, optlen))
2890 return -TARGET_EFAULT;
2891 if (len < 0)
2892 return -TARGET_EINVAL;
2893 lv = sizeof(lv);
2894 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2895 if (ret < 0)
2896 return ret;
2897 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2898 len = 1;
2899 if (put_user_u32(len, optlen)
2900 || put_user_u8(val, optval_addr))
2901 return -TARGET_EFAULT;
2902 } else {
2903 if (len > sizeof(int))
2904 len = sizeof(int);
2905 if (put_user_u32(len, optlen)
2906 || put_user_u32(val, optval_addr))
2907 return -TARGET_EFAULT;
2908 }
2909 break;
2910 default:
2911 ret = -TARGET_ENOPROTOOPT;
2912 break;
2913 }
2914 break;
Josh Kunza2d86682019-10-29 15:43:10 -07002915#ifdef SOL_NETLINK
2916 case SOL_NETLINK:
2917 switch (optname) {
2918 case NETLINK_PKTINFO:
2919 case NETLINK_BROADCAST_ERROR:
2920 case NETLINK_NO_ENOBUFS:
2921#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2922 case NETLINK_LISTEN_ALL_NSID:
2923 case NETLINK_CAP_ACK:
2924#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2925#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2926 case NETLINK_EXT_ACK:
2927#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2928#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2929 case NETLINK_GET_STRICT_CHK:
2930#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2931 if (get_user_u32(len, optlen)) {
2932 return -TARGET_EFAULT;
2933 }
2934 if (len != sizeof(val)) {
2935 return -TARGET_EINVAL;
2936 }
2937 lv = len;
2938 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2939 if (ret < 0) {
2940 return ret;
2941 }
2942 if (put_user_u32(lv, optlen)
2943 || put_user_u32(val, optval_addr)) {
2944 return -TARGET_EFAULT;
2945 }
2946 break;
2947#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2948 case NETLINK_LIST_MEMBERSHIPS:
2949 {
2950 uint32_t *results;
2951 int i;
2952 if (get_user_u32(len, optlen)) {
2953 return -TARGET_EFAULT;
2954 }
2955 if (len < 0) {
2956 return -TARGET_EINVAL;
2957 }
2958 results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
Frédéric Fortier13e340c2021-03-28 14:01:35 -04002959 if (!results && len > 0) {
Josh Kunza2d86682019-10-29 15:43:10 -07002960 return -TARGET_EFAULT;
2961 }
2962 lv = len;
2963 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
2964 if (ret < 0) {
2965 unlock_user(results, optval_addr, 0);
2966 return ret;
2967 }
2968 /* swap host endianess to target endianess. */
2969 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
2970 results[i] = tswap32(results[i]);
2971 }
2972 if (put_user_u32(lv, optlen)) {
2973 return -TARGET_EFAULT;
2974 }
2975 unlock_user(results, optval_addr, 0);
2976 break;
2977 }
2978#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2979 default:
2980 goto unimplemented;
2981 }
Laurent Vivierc0cb8802019-11-12 11:50:55 +01002982 break;
Josh Kunza2d86682019-10-29 15:43:10 -07002983#endif /* SOL_NETLINK */
bellard8853f862004-02-22 14:57:26 +00002984 default:
2985 unimplemented:
Josh Kunz39be5352020-02-03 18:54:13 -08002986 qemu_log_mask(LOG_UNIMP,
2987 "getsockopt level=%d optname=%d not yet supported\n",
2988 level, optname);
thsc02f4992007-12-18 02:39:59 +00002989 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00002990 break;
2991 }
2992 return ret;
bellard7854b052003-03-29 17:22:23 +00002993}
2994
Max Filippov9ac22512018-04-04 17:30:41 -07002995/* Convert target low/high pair representing file offset into the host
2996 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2997 * as the kernel doesn't handle them either.
2998 */
2999static void target_to_host_low_high(abi_ulong tlow,
3000 abi_ulong thigh,
3001 unsigned long *hlow,
3002 unsigned long *hhigh)
3003{
3004 uint64_t off = tlow |
3005 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
3006 TARGET_LONG_BITS / 2;
3007
3008 *hlow = off;
3009 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
3010}
3011
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003012static struct iovec *lock_iovec(int type, abi_ulong target_addr,
Peter Maydelldab32b32016-07-15 14:57:26 +01003013 abi_ulong count, int copy)
pbrook53a59602006-03-25 19:31:22 +00003014{
3015 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003016 struct iovec *vec;
3017 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00003018 int i;
Peter Maydell501bb4b2014-02-17 18:55:33 +00003019 int err = 0;
Tom Musta29560a62014-08-12 13:53:43 -05003020 bool bad_address = false;
pbrook53a59602006-03-25 19:31:22 +00003021
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003022 if (count == 0) {
3023 errno = 0;
3024 return NULL;
pbrook53a59602006-03-25 19:31:22 +00003025 }
Peter Maydelldab32b32016-07-15 14:57:26 +01003026 if (count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003027 errno = EINVAL;
3028 return NULL;
3029 }
3030
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303031 vec = g_try_new0(struct iovec, count);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003032 if (vec == NULL) {
3033 errno = ENOMEM;
3034 return NULL;
3035 }
3036
3037 target_vec = lock_user(VERIFY_READ, target_addr,
3038 count * sizeof(struct target_iovec), 1);
3039 if (target_vec == NULL) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00003040 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003041 goto fail2;
3042 }
3043
3044 /* ??? If host page size > target page size, this will result in a
3045 value larger than what we can actually support. */
3046 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3047 total_len = 0;
3048
3049 for (i = 0; i < count; i++) {
3050 abi_ulong base = tswapal(target_vec[i].iov_base);
3051 abi_long len = tswapal(target_vec[i].iov_len);
3052
3053 if (len < 0) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00003054 err = EINVAL;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003055 goto fail;
3056 } else if (len == 0) {
3057 /* Zero length pointer is ignored. */
3058 vec[i].iov_base = 0;
3059 } else {
3060 vec[i].iov_base = lock_user(type, base, len, copy);
Tom Musta29560a62014-08-12 13:53:43 -05003061 /* If the first buffer pointer is bad, this is a fault. But
3062 * subsequent bad buffers will result in a partial write; this
3063 * is realized by filling the vector with null pointers and
3064 * zero lengths. */
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003065 if (!vec[i].iov_base) {
Tom Musta29560a62014-08-12 13:53:43 -05003066 if (i == 0) {
3067 err = EFAULT;
3068 goto fail;
3069 } else {
3070 bad_address = true;
3071 }
3072 }
3073 if (bad_address) {
3074 len = 0;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003075 }
3076 if (len > max_len - total_len) {
3077 len = max_len - total_len;
3078 }
3079 }
3080 vec[i].iov_len = len;
3081 total_len += len;
3082 }
3083
3084 unlock_user(target_vec, target_addr, 0);
3085 return vec;
3086
3087 fail:
Chen Gang S7eff5182015-01-23 18:01:09 +08003088 while (--i >= 0) {
3089 if (tswapal(target_vec[i].iov_len) > 0) {
3090 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3091 }
3092 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003093 unlock_user(target_vec, target_addr, 0);
Peter Maydell501bb4b2014-02-17 18:55:33 +00003094 fail2:
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303095 g_free(vec);
Peter Maydell501bb4b2014-02-17 18:55:33 +00003096 errno = err;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003097 return NULL;
pbrook53a59602006-03-25 19:31:22 +00003098}
3099
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003100static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
Peter Maydelldab32b32016-07-15 14:57:26 +01003101 abi_ulong count, int copy)
pbrook53a59602006-03-25 19:31:22 +00003102{
3103 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00003104 int i;
3105
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003106 target_vec = lock_user(VERIFY_READ, target_addr,
3107 count * sizeof(struct target_iovec), 1);
3108 if (target_vec) {
3109 for (i = 0; i < count; i++) {
3110 abi_ulong base = tswapal(target_vec[i].iov_base);
Chen Gang S71ec7ce2015-01-23 18:07:50 +08003111 abi_long len = tswapal(target_vec[i].iov_len);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003112 if (len < 0) {
3113 break;
3114 }
balrogd732dcb2008-10-28 10:21:03 +00003115 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3116 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003117 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00003118 }
bellard579a97f2007-11-11 14:26:47 +00003119
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303120 g_free(vec);
pbrook53a59602006-03-25 19:31:22 +00003121}
3122
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003123static inline int target_to_host_sock_type(int *type)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003124{
3125 int host_type = 0;
3126 int target_type = *type;
3127
3128 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3129 case TARGET_SOCK_DGRAM:
3130 host_type = SOCK_DGRAM;
3131 break;
3132 case TARGET_SOCK_STREAM:
3133 host_type = SOCK_STREAM;
3134 break;
3135 default:
3136 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3137 break;
3138 }
3139 if (target_type & TARGET_SOCK_CLOEXEC) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003140#if defined(SOCK_CLOEXEC)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003141 host_type |= SOCK_CLOEXEC;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003142#else
3143 return -TARGET_EINVAL;
3144#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003145 }
3146 if (target_type & TARGET_SOCK_NONBLOCK) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003147#if defined(SOCK_NONBLOCK)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003148 host_type |= SOCK_NONBLOCK;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003149#elif !defined(O_NONBLOCK)
3150 return -TARGET_EINVAL;
3151#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003152 }
3153 *type = host_type;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003154 return 0;
3155}
3156
3157/* Try to emulate socket type flags after socket creation. */
3158static int sock_flags_fixup(int fd, int target_type)
3159{
3160#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3161 if (target_type & TARGET_SOCK_NONBLOCK) {
3162 int flags = fcntl(fd, F_GETFL);
3163 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3164 close(fd);
3165 return -TARGET_EINVAL;
3166 }
3167 }
3168#endif
3169 return fd;
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003170}
3171
ths0da46a62007-10-20 20:23:07 +00003172/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003173static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00003174{
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003175 int target_type = type;
3176 int ret;
3177
3178 ret = target_to_host_sock_type(&type);
3179 if (ret) {
3180 return ret;
3181 }
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003182
Laurent Vivier575b22b2016-06-02 22:14:15 +02003183 if (domain == PF_NETLINK && !(
3184#ifdef CONFIG_RTNETLINK
3185 protocol == NETLINK_ROUTE ||
3186#endif
3187 protocol == NETLINK_KOBJECT_UEVENT ||
3188 protocol == NETLINK_AUDIT)) {
Josh Kunz71e24432020-07-06 17:10:36 -07003189 return -TARGET_EPROTONOSUPPORT;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003190 }
Laurent Vivierff626f22015-10-28 21:40:42 +01003191
3192 if (domain == AF_PACKET ||
3193 (domain == AF_INET && type == SOCK_PACKET)) {
3194 protocol = tswap16(protocol);
3195 }
3196
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003197 ret = get_errno(socket(domain, type, protocol));
3198 if (ret >= 0) {
3199 ret = sock_flags_fixup(ret, target_type);
Laurent Vivier0cf22722015-10-28 21:40:45 +01003200 if (type == SOCK_PACKET) {
3201 /* Manage an obsolete case :
3202 * if socket type is SOCK_PACKET, bind by name
3203 */
3204 fd_trans_register(ret, &target_packet_trans);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003205 } else if (domain == PF_NETLINK) {
3206 switch (protocol) {
Laurent Vivier575b22b2016-06-02 22:14:15 +02003207#ifdef CONFIG_RTNETLINK
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003208 case NETLINK_ROUTE:
3209 fd_trans_register(ret, &target_netlink_route_trans);
3210 break;
Laurent Vivier575b22b2016-06-02 22:14:15 +02003211#endif
Laurent Vivierb2656202016-05-22 18:56:20 +02003212 case NETLINK_KOBJECT_UEVENT:
3213 /* nothing to do: messages are strings */
3214 break;
Laurent Vivier5ce9bb52016-05-22 18:56:21 +02003215 case NETLINK_AUDIT:
3216 fd_trans_register(ret, &target_netlink_audit_trans);
3217 break;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003218 default:
3219 g_assert_not_reached();
3220 }
Laurent Vivier0cf22722015-10-28 21:40:45 +01003221 }
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003222 }
3223 return ret;
bellard3532fa72006-06-24 15:06:03 +00003224}
3225
ths0da46a62007-10-20 20:23:07 +00003226/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003227static abi_long do_bind(int sockfd, abi_ulong target_addr,
3228 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00003229{
aurel328f7aeaf2009-01-30 19:47:57 +00003230 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03003231 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00003232
Blue Swirl38724252010-09-18 05:53:14 +00003233 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003234 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003235 }
aurel328f7aeaf2009-01-30 19:47:57 +00003236
aurel32607175e2009-04-15 16:11:59 +00003237 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00003238
Laurent Vivier7b36f782015-10-28 21:40:44 +01003239 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003240 if (ret)
3241 return ret;
3242
bellard3532fa72006-06-24 15:06:03 +00003243 return get_errno(bind(sockfd, addr, addrlen));
3244}
3245
ths0da46a62007-10-20 20:23:07 +00003246/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003247static abi_long do_connect(int sockfd, abi_ulong target_addr,
3248 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00003249{
aurel328f7aeaf2009-01-30 19:47:57 +00003250 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03003251 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00003252
Blue Swirl38724252010-09-18 05:53:14 +00003253 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003254 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003255 }
aurel328f7aeaf2009-01-30 19:47:57 +00003256
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003257 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00003258
Laurent Vivier7b36f782015-10-28 21:40:44 +01003259 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003260 if (ret)
3261 return ret;
3262
Peter Maydell2a3c7612016-06-06 19:58:03 +01003263 return get_errno(safe_connect(sockfd, addr, addrlen));
bellard3532fa72006-06-24 15:06:03 +00003264}
3265
Alexander Graff19e00d2014-03-02 19:36:42 +00003266/* do_sendrecvmsg_locked() Must return target values and target errnos. */
3267static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3268 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00003269{
balrog6de645c2008-10-28 10:26:29 +00003270 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00003271 struct msghdr msg;
Peter Maydelldab32b32016-07-15 14:57:26 +01003272 abi_ulong count;
bellard3532fa72006-06-24 15:06:03 +00003273 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00003274 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00003275
bellard3532fa72006-06-24 15:06:03 +00003276 if (msgp->msg_name) {
3277 msg.msg_namelen = tswap32(msgp->msg_namelen);
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003278 msg.msg_name = alloca(msg.msg_namelen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01003279 ret = target_to_host_sockaddr(fd, msg.msg_name,
3280 tswapal(msgp->msg_name),
3281 msg.msg_namelen);
Peter Maydell26a6fc92016-07-15 14:57:28 +01003282 if (ret == -TARGET_EFAULT) {
3283 /* For connected sockets msg_name and msg_namelen must
3284 * be ignored, so returning EFAULT immediately is wrong.
3285 * Instead, pass a bad msg_name to the host kernel, and
3286 * let it decide whether to return EFAULT or not.
3287 */
3288 msg.msg_name = (void *)-1;
3289 } else if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003290 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03003291 }
bellard3532fa72006-06-24 15:06:03 +00003292 } else {
3293 msg.msg_name = NULL;
3294 msg.msg_namelen = 0;
3295 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003296 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00003297 msg.msg_control = alloca(msg.msg_controllen);
Jonas Schievink1d3d1b22018-07-12 00:12:44 +02003298 memset(msg.msg_control, 0, msg.msg_controllen);
3299
bellard3532fa72006-06-24 15:06:03 +00003300 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00003301
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003302 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003303 target_vec = tswapal(msgp->msg_iov);
Peter Maydell97b07972016-07-15 14:57:27 +01003304
3305 if (count > IOV_MAX) {
3306 /* sendrcvmsg returns a different errno for this condition than
3307 * readv/writev, so we must catch it here before lock_iovec() does.
3308 */
3309 ret = -TARGET_EMSGSIZE;
3310 goto out2;
3311 }
3312
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003313 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3314 target_vec, count, send);
3315 if (vec == NULL) {
3316 ret = -host_to_target_errno(errno);
3317 goto out2;
3318 }
bellard3532fa72006-06-24 15:06:03 +00003319 msg.msg_iovlen = count;
3320 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00003321
bellard3532fa72006-06-24 15:06:03 +00003322 if (send) {
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003323 if (fd_trans_target_to_host_data(fd)) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003324 void *host_msg;
3325
3326 host_msg = g_malloc(msg.msg_iov->iov_len);
3327 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3328 ret = fd_trans_target_to_host_data(fd)(host_msg,
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003329 msg.msg_iov->iov_len);
Laurent Vivier7d61d892016-06-21 19:51:14 +02003330 if (ret >= 0) {
3331 msg.msg_iov->iov_base = host_msg;
3332 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3333 }
3334 g_free(host_msg);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003335 } else {
3336 ret = target_to_host_cmsg(&msg, msgp);
Laurent Vivier7d61d892016-06-21 19:51:14 +02003337 if (ret == 0) {
3338 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3339 }
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003340 }
bellard3532fa72006-06-24 15:06:03 +00003341 } else {
Peter Maydell66687532016-06-06 19:58:04 +01003342 ret = get_errno(safe_recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00003343 if (!is_error(ret)) {
3344 len = ret;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003345 if (fd_trans_host_to_target_data(fd)) {
3346 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
Laurent Vivier2a03d3e2018-08-20 19:15:54 +02003347 MIN(msg.msg_iov->iov_len, len));
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003348 } else {
3349 ret = host_to_target_cmsg(msgp, &msg);
3350 }
Jing Huangca619062012-07-24 13:58:02 +00003351 if (!is_error(ret)) {
3352 msgp->msg_namelen = tswap32(msg.msg_namelen);
Andreas Schwab24894f32019-02-12 17:34:35 +01003353 msgp->msg_flags = tswap32(msg.msg_flags);
Peter Maydell26a6fc92016-07-15 14:57:28 +01003354 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
Jing Huangca619062012-07-24 13:58:02 +00003355 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3356 msg.msg_name, msg.msg_namelen);
3357 if (ret) {
3358 goto out;
3359 }
3360 }
3361
balrog6de645c2008-10-28 10:26:29 +00003362 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00003363 }
balrog6de645c2008-10-28 10:26:29 +00003364 }
bellard3532fa72006-06-24 15:06:03 +00003365 }
Jing Huangca619062012-07-24 13:58:02 +00003366
3367out:
bellard3532fa72006-06-24 15:06:03 +00003368 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003369out2:
Alexander Graff19e00d2014-03-02 19:36:42 +00003370 return ret;
3371}
3372
3373static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3374 int flags, int send)
3375{
3376 abi_long ret;
3377 struct target_msghdr *msgp;
3378
3379 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3380 msgp,
3381 target_msg,
3382 send ? 1 : 0)) {
3383 return -TARGET_EFAULT;
3384 }
3385 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
bellard579a97f2007-11-11 14:26:47 +00003386 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00003387 return ret;
3388}
3389
Alexander Graff19e00d2014-03-02 19:36:42 +00003390/* We don't rely on the C library to have sendmmsg/recvmmsg support,
3391 * so it might not have this *mmsg-specific flag either.
3392 */
3393#ifndef MSG_WAITFORONE
3394#define MSG_WAITFORONE 0x10000
3395#endif
3396
3397static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3398 unsigned int vlen, unsigned int flags,
3399 int send)
3400{
3401 struct target_mmsghdr *mmsgp;
3402 abi_long ret = 0;
3403 int i;
3404
3405 if (vlen > UIO_MAXIOV) {
3406 vlen = UIO_MAXIOV;
3407 }
3408
3409 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3410 if (!mmsgp) {
3411 return -TARGET_EFAULT;
3412 }
3413
3414 for (i = 0; i < vlen; i++) {
3415 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3416 if (is_error(ret)) {
3417 break;
3418 }
3419 mmsgp[i].msg_len = tswap32(ret);
3420 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3421 if (flags & MSG_WAITFORONE) {
3422 flags |= MSG_DONTWAIT;
3423 }
3424 }
3425
3426 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3427
3428 /* Return number of datagrams sent if we sent any at all;
3429 * otherwise return the error.
3430 */
3431 if (i) {
3432 return i;
3433 }
3434 return ret;
3435}
Alexander Graff19e00d2014-03-02 19:36:42 +00003436
Peter Maydella94b4982013-02-08 04:35:04 +00003437/* do_accept4() Must return target values and target errnos. */
3438static abi_long do_accept4(int fd, abi_ulong target_addr,
3439 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00003440{
Andreas Schwabcd813362019-02-14 12:43:40 +01003441 socklen_t addrlen, ret_addrlen;
bellard2f619692007-11-16 10:46:05 +00003442 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003443 abi_long ret;
Petar Jovanovicd25295d2014-03-31 17:41:23 +02003444 int host_flags;
3445
3446 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
pbrook1be9e1d2006-11-19 15:26:04 +00003447
Peter Maydella94b4982013-02-08 04:35:04 +00003448 if (target_addr == 0) {
Peter Maydellff6dc132016-06-06 19:58:13 +01003449 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
Peter Maydella94b4982013-02-08 04:35:04 +00003450 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003451
Matus Kysele554eb42020-09-30 17:16:16 +02003452 /* linux returns EFAULT if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00003453 if (get_user_u32(addrlen, target_addrlen_addr))
Matus Kysele554eb42020-09-30 17:16:16 +02003454 return -TARGET_EFAULT;
bellard2f619692007-11-16 10:46:05 +00003455
Blue Swirl38724252010-09-18 05:53:14 +00003456 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003457 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003458 }
aurel328f7aeaf2009-01-30 19:47:57 +00003459
Richard Hendersonc7169b02021-02-12 10:48:47 -08003460 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
Matus Kysele554eb42020-09-30 17:16:16 +02003461 return -TARGET_EFAULT;
Richard Hendersonc7169b02021-02-12 10:48:47 -08003462 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003463
bellard2f619692007-11-16 10:46:05 +00003464 addr = alloca(addrlen);
3465
Andreas Schwabcd813362019-02-14 12:43:40 +01003466 ret_addrlen = addrlen;
3467 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
pbrook1be9e1d2006-11-19 15:26:04 +00003468 if (!is_error(ret)) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003469 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3470 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
bellard2f619692007-11-16 10:46:05 +00003471 ret = -TARGET_EFAULT;
Andreas Schwabcd813362019-02-14 12:43:40 +01003472 }
pbrook1be9e1d2006-11-19 15:26:04 +00003473 }
3474 return ret;
3475}
3476
ths0da46a62007-10-20 20:23:07 +00003477/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003478static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00003479 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003480{
Andreas Schwabcd813362019-02-14 12:43:40 +01003481 socklen_t addrlen, ret_addrlen;
bellard2f619692007-11-16 10:46:05 +00003482 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003483 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003484
bellard2f619692007-11-16 10:46:05 +00003485 if (get_user_u32(addrlen, target_addrlen_addr))
3486 return -TARGET_EFAULT;
3487
Blue Swirl38724252010-09-18 05:53:14 +00003488 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003489 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003490 }
aurel328f7aeaf2009-01-30 19:47:57 +00003491
Richard Hendersonc7169b02021-02-12 10:48:47 -08003492 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
Arnaud Patard917507b2009-06-19 10:44:45 +03003493 return -TARGET_EFAULT;
Richard Hendersonc7169b02021-02-12 10:48:47 -08003494 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003495
bellard2f619692007-11-16 10:46:05 +00003496 addr = alloca(addrlen);
3497
Andreas Schwabcd813362019-02-14 12:43:40 +01003498 ret_addrlen = addrlen;
3499 ret = get_errno(getpeername(fd, addr, &ret_addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003500 if (!is_error(ret)) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003501 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3502 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
bellard2f619692007-11-16 10:46:05 +00003503 ret = -TARGET_EFAULT;
Andreas Schwabcd813362019-02-14 12:43:40 +01003504 }
pbrook1be9e1d2006-11-19 15:26:04 +00003505 }
3506 return ret;
3507}
3508
ths0da46a62007-10-20 20:23:07 +00003509/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003510static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00003511 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003512{
Andreas Schwabcd813362019-02-14 12:43:40 +01003513 socklen_t addrlen, ret_addrlen;
bellard2f619692007-11-16 10:46:05 +00003514 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003515 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003516
bellard2f619692007-11-16 10:46:05 +00003517 if (get_user_u32(addrlen, target_addrlen_addr))
3518 return -TARGET_EFAULT;
3519
Blue Swirl38724252010-09-18 05:53:14 +00003520 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003521 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003522 }
aurel328f7aeaf2009-01-30 19:47:57 +00003523
Richard Hendersonc7169b02021-02-12 10:48:47 -08003524 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
Arnaud Patard917507b2009-06-19 10:44:45 +03003525 return -TARGET_EFAULT;
Richard Hendersonc7169b02021-02-12 10:48:47 -08003526 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003527
bellard2f619692007-11-16 10:46:05 +00003528 addr = alloca(addrlen);
3529
Andreas Schwabcd813362019-02-14 12:43:40 +01003530 ret_addrlen = addrlen;
3531 ret = get_errno(getsockname(fd, addr, &ret_addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003532 if (!is_error(ret)) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003533 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3534 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
bellard2f619692007-11-16 10:46:05 +00003535 ret = -TARGET_EFAULT;
Andreas Schwabcd813362019-02-14 12:43:40 +01003536 }
pbrook1be9e1d2006-11-19 15:26:04 +00003537 }
3538 return ret;
3539}
3540
ths0da46a62007-10-20 20:23:07 +00003541/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003542static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00003543 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003544{
3545 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00003546 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003547
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003548 target_to_host_sock_type(&type);
3549
pbrook1be9e1d2006-11-19 15:26:04 +00003550 ret = get_errno(socketpair(domain, type, protocol, tab));
3551 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00003552 if (put_user_s32(tab[0], target_tab_addr)
3553 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3554 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003555 }
3556 return ret;
3557}
3558
ths0da46a62007-10-20 20:23:07 +00003559/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003560static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3561 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00003562{
3563 void *addr;
3564 void *host_msg;
Laurent Vivier7d61d892016-06-21 19:51:14 +02003565 void *copy_msg = NULL;
blueswir1992f48a2007-10-14 16:27:31 +00003566 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003567
Blue Swirl38724252010-09-18 05:53:14 +00003568 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003569 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003570 }
aurel328f7aeaf2009-01-30 19:47:57 +00003571
bellard579a97f2007-11-11 14:26:47 +00003572 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3573 if (!host_msg)
3574 return -TARGET_EFAULT;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003575 if (fd_trans_target_to_host_data(fd)) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003576 copy_msg = host_msg;
3577 host_msg = g_malloc(len);
3578 memcpy(host_msg, copy_msg, len);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003579 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3580 if (ret < 0) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003581 goto fail;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003582 }
3583 }
pbrook1be9e1d2006-11-19 15:26:04 +00003584 if (target_addr) {
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003585 addr = alloca(addrlen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01003586 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003587 if (ret) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003588 goto fail;
Arnaud Patard917507b2009-06-19 10:44:45 +03003589 }
Peter Maydell66687532016-06-06 19:58:04 +01003590 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003591 } else {
Peter Maydell66687532016-06-06 19:58:04 +01003592 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
pbrook1be9e1d2006-11-19 15:26:04 +00003593 }
Laurent Vivier7d61d892016-06-21 19:51:14 +02003594fail:
3595 if (copy_msg) {
3596 g_free(host_msg);
3597 host_msg = copy_msg;
3598 }
pbrook1be9e1d2006-11-19 15:26:04 +00003599 unlock_user(host_msg, msg, 0);
3600 return ret;
3601}
3602
ths0da46a62007-10-20 20:23:07 +00003603/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003604static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3605 abi_ulong target_addr,
3606 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00003607{
Andreas Schwabcd813362019-02-14 12:43:40 +01003608 socklen_t addrlen, ret_addrlen;
pbrook1be9e1d2006-11-19 15:26:04 +00003609 void *addr;
3610 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00003611 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003612
Zach Reizner4a1e6bc2021-03-26 22:11:16 -04003613 if (!msg) {
3614 host_msg = NULL;
3615 } else {
3616 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3617 if (!host_msg) {
3618 return -TARGET_EFAULT;
3619 }
3620 }
pbrook1be9e1d2006-11-19 15:26:04 +00003621 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00003622 if (get_user_u32(addrlen, target_addrlen)) {
3623 ret = -TARGET_EFAULT;
3624 goto fail;
3625 }
Blue Swirl38724252010-09-18 05:53:14 +00003626 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003627 ret = -TARGET_EINVAL;
3628 goto fail;
3629 }
pbrook1be9e1d2006-11-19 15:26:04 +00003630 addr = alloca(addrlen);
Andreas Schwabcd813362019-02-14 12:43:40 +01003631 ret_addrlen = addrlen;
Peter Maydell66687532016-06-06 19:58:04 +01003632 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
Andreas Schwabcd813362019-02-14 12:43:40 +01003633 addr, &ret_addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003634 } else {
3635 addr = NULL; /* To keep compiler quiet. */
Andreas Schwabcd813362019-02-14 12:43:40 +01003636 addrlen = 0; /* To keep compiler quiet. */
Peter Maydell66687532016-06-06 19:58:04 +01003637 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
pbrook1be9e1d2006-11-19 15:26:04 +00003638 }
3639 if (!is_error(ret)) {
Laurent Vivierc35e1f92016-06-21 19:51:15 +02003640 if (fd_trans_host_to_target_data(fd)) {
Laurent Vivier2a03d3e2018-08-20 19:15:54 +02003641 abi_long trans;
3642 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3643 if (is_error(trans)) {
3644 ret = trans;
3645 goto fail;
3646 }
Laurent Vivierc35e1f92016-06-21 19:51:15 +02003647 }
pbrook1be9e1d2006-11-19 15:26:04 +00003648 if (target_addr) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003649 host_to_target_sockaddr(target_addr, addr,
3650 MIN(addrlen, ret_addrlen));
3651 if (put_user_u32(ret_addrlen, target_addrlen)) {
bellard2f619692007-11-16 10:46:05 +00003652 ret = -TARGET_EFAULT;
3653 goto fail;
3654 }
pbrook1be9e1d2006-11-19 15:26:04 +00003655 }
3656 unlock_user(host_msg, msg, len);
3657 } else {
bellard2f619692007-11-16 10:46:05 +00003658fail:
pbrook1be9e1d2006-11-19 15:26:04 +00003659 unlock_user(host_msg, msg, 0);
3660 }
3661 return ret;
3662}
3663
j_mayer32407102007-09-26 23:01:49 +00003664#ifdef TARGET_NR_socketcall
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003665/* do_socketcall() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003666static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00003667{
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003668 static const unsigned nargs[] = { /* number of arguments per operation */
3669 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3670 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3671 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3672 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3673 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3674 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3675 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3676 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3677 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3678 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3679 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3680 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3681 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3682 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3683 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3684 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3685 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3686 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3687 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3688 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003689 };
3690 abi_long a[6]; /* max 6 args */
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003691 unsigned i;
bellard31e31b82003-02-18 22:55:36 +00003692
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003693 /* check the range of the first argument num */
3694 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3695 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3696 return -TARGET_EINVAL;
3697 }
3698 /* ensure we have space for args */
3699 if (nargs[num] > ARRAY_SIZE(a)) {
3700 return -TARGET_EINVAL;
3701 }
3702 /* collect the arguments in a[] according to nargs[] */
3703 for (i = 0; i < nargs[num]; ++i) {
3704 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3705 return -TARGET_EFAULT;
André Hentschelb9d36eb2014-01-06 20:18:52 +01003706 }
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003707 }
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003708 /* now when we have the args, invoke the appropriate underlying function */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003709 switch (num) {
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003710 case TARGET_SYS_SOCKET: /* domain, type, protocol */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003711 return do_socket(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003712 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003713 return do_bind(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003714 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003715 return do_connect(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003716 case TARGET_SYS_LISTEN: /* sockfd, backlog */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003717 return get_errno(listen(a[0], a[1]));
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003718 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003719 return do_accept4(a[0], a[1], a[2], 0);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003720 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003721 return do_getsockname(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003722 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003723 return do_getpeername(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003724 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003725 return do_socketpair(a[0], a[1], a[2], a[3]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003726 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003727 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003728 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003729 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003730 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003731 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003732 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003733 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003734 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003735 return get_errno(shutdown(a[0], a[1]));
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003736 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003737 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003738 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003739 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003740 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
3741 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3742 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
3743 return do_sendrecvmsg(a[0], a[1], a[2], 0);
3744 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3745 return do_accept4(a[0], a[1], a[2], a[3]);
3746 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
3747 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
3748 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3749 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
bellard31e31b82003-02-18 22:55:36 +00003750 default:
Josh Kunz39be5352020-02-03 18:54:13 -08003751 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003752 return -TARGET_EINVAL;
bellard31e31b82003-02-18 22:55:36 +00003753 }
bellard31e31b82003-02-18 22:55:36 +00003754}
j_mayer32407102007-09-26 23:01:49 +00003755#endif
bellard31e31b82003-02-18 22:55:36 +00003756
bellard8853f862004-02-22 14:57:26 +00003757#define N_SHM_REGIONS 32
3758
3759static struct shm_region {
Peter Maydellb6e17872016-02-09 15:57:11 +00003760 abi_ulong start;
3761 abi_ulong size;
3762 bool in_use;
bellard8853f862004-02-22 14:57:26 +00003763} shm_regions[N_SHM_REGIONS];
3764
Peter Maydell005eb2a2016-07-15 16:50:47 +01003765#ifndef TARGET_SEMID64_DS
3766/* asm-generic version of this struct */
3767struct target_semid64_ds
ths3eb6b042007-06-03 14:26:27 +00003768{
3769 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00003770 abi_ulong sem_otime;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003771#if TARGET_ABI_BITS == 32
blueswir1992f48a2007-10-14 16:27:31 +00003772 abi_ulong __unused1;
Tom Musta03527342014-08-12 13:53:32 -05003773#endif
blueswir1992f48a2007-10-14 16:27:31 +00003774 abi_ulong sem_ctime;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003775#if TARGET_ABI_BITS == 32
blueswir1992f48a2007-10-14 16:27:31 +00003776 abi_ulong __unused2;
Tom Musta03527342014-08-12 13:53:32 -05003777#endif
blueswir1992f48a2007-10-14 16:27:31 +00003778 abi_ulong sem_nsems;
3779 abi_ulong __unused3;
3780 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00003781};
Peter Maydell005eb2a2016-07-15 16:50:47 +01003782#endif
ths3eb6b042007-06-03 14:26:27 +00003783
bellard579a97f2007-11-11 14:26:47 +00003784static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3785 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003786{
3787 struct target_ipc_perm *target_ip;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003788 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003789
bellard579a97f2007-11-11 14:26:47 +00003790 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3791 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00003792 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003793 host_ip->__key = tswap32(target_ip->__key);
3794 host_ip->uid = tswap32(target_ip->uid);
3795 host_ip->gid = tswap32(target_ip->gid);
3796 host_ip->cuid = tswap32(target_ip->cuid);
3797 host_ip->cgid = tswap32(target_ip->cgid);
3798#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3799 host_ip->mode = tswap32(target_ip->mode);
3800#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003801 host_ip->mode = tswap16(target_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003802#endif
3803#if defined(TARGET_PPC)
3804 host_ip->__seq = tswap32(target_ip->__seq);
3805#else
3806 host_ip->__seq = tswap16(target_ip->__seq);
3807#endif
ths3eb6b042007-06-03 14:26:27 +00003808 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003809 return 0;
ths3eb6b042007-06-03 14:26:27 +00003810}
3811
bellard579a97f2007-11-11 14:26:47 +00003812static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3813 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00003814{
3815 struct target_ipc_perm *target_ip;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003816 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003817
bellard579a97f2007-11-11 14:26:47 +00003818 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3819 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00003820 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003821 target_ip->__key = tswap32(host_ip->__key);
3822 target_ip->uid = tswap32(host_ip->uid);
3823 target_ip->gid = tswap32(host_ip->gid);
3824 target_ip->cuid = tswap32(host_ip->cuid);
3825 target_ip->cgid = tswap32(host_ip->cgid);
3826#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3827 target_ip->mode = tswap32(host_ip->mode);
3828#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003829 target_ip->mode = tswap16(host_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003830#endif
3831#if defined(TARGET_PPC)
3832 target_ip->__seq = tswap32(host_ip->__seq);
3833#else
3834 target_ip->__seq = tswap16(host_ip->__seq);
3835#endif
ths3eb6b042007-06-03 14:26:27 +00003836 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003837 return 0;
ths3eb6b042007-06-03 14:26:27 +00003838}
3839
bellard579a97f2007-11-11 14:26:47 +00003840static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3841 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003842{
Peter Maydell005eb2a2016-07-15 16:50:47 +01003843 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003844
bellard579a97f2007-11-11 14:26:47 +00003845 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3846 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00003847 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3848 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003849 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3850 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3851 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00003852 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003853 return 0;
ths3eb6b042007-06-03 14:26:27 +00003854}
3855
bellard579a97f2007-11-11 14:26:47 +00003856static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3857 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00003858{
Peter Maydell005eb2a2016-07-15 16:50:47 +01003859 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003860
bellard579a97f2007-11-11 14:26:47 +00003861 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3862 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00003863 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08003864 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003865 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3866 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3867 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00003868 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003869 return 0;
ths3eb6b042007-06-03 14:26:27 +00003870}
3871
aurel32e5289082009-04-18 16:16:12 +00003872struct target_seminfo {
3873 int semmap;
3874 int semmni;
3875 int semmns;
3876 int semmnu;
3877 int semmsl;
3878 int semopm;
3879 int semume;
3880 int semusz;
3881 int semvmx;
3882 int semaem;
3883};
3884
3885static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3886 struct seminfo *host_seminfo)
3887{
3888 struct target_seminfo *target_seminfo;
3889 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3890 return -TARGET_EFAULT;
3891 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3892 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3893 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3894 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3895 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3896 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3897 __put_user(host_seminfo->semume, &target_seminfo->semume);
3898 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3899 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3900 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3901 unlock_user_struct(target_seminfo, target_addr, 1);
3902 return 0;
3903}
3904
thsfa294812007-02-02 22:05:00 +00003905union semun {
3906 int val;
ths3eb6b042007-06-03 14:26:27 +00003907 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00003908 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00003909 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00003910};
3911
ths3eb6b042007-06-03 14:26:27 +00003912union target_semun {
3913 int val;
aurel32e5289082009-04-18 16:16:12 +00003914 abi_ulong buf;
3915 abi_ulong array;
3916 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00003917};
3918
aurel32e5289082009-04-18 16:16:12 +00003919static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3920 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003921{
aurel32e5289082009-04-18 16:16:12 +00003922 int nsems;
3923 unsigned short *array;
3924 union semun semun;
3925 struct semid_ds semid_ds;
3926 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00003927
aurel32e5289082009-04-18 16:16:12 +00003928 semun.buf = &semid_ds;
3929
3930 ret = semctl(semid, 0, IPC_STAT, semun);
3931 if (ret == -1)
3932 return get_errno(ret);
3933
3934 nsems = semid_ds.sem_nsems;
3935
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303936 *host_array = g_try_new(unsigned short, nsems);
Peter Maydell69d4c702014-02-17 18:55:34 +00003937 if (!*host_array) {
3938 return -TARGET_ENOMEM;
3939 }
aurel32e5289082009-04-18 16:16:12 +00003940 array = lock_user(VERIFY_READ, target_addr,
3941 nsems*sizeof(unsigned short), 1);
Peter Maydell69d4c702014-02-17 18:55:34 +00003942 if (!array) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303943 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00003944 return -TARGET_EFAULT;
Peter Maydell69d4c702014-02-17 18:55:34 +00003945 }
aurel32e5289082009-04-18 16:16:12 +00003946
3947 for(i=0; i<nsems; i++) {
3948 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00003949 }
aurel32e5289082009-04-18 16:16:12 +00003950 unlock_user(array, target_addr, 0);
3951
bellard579a97f2007-11-11 14:26:47 +00003952 return 0;
ths3eb6b042007-06-03 14:26:27 +00003953}
3954
aurel32e5289082009-04-18 16:16:12 +00003955static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3956 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00003957{
aurel32e5289082009-04-18 16:16:12 +00003958 int nsems;
3959 unsigned short *array;
3960 union semun semun;
3961 struct semid_ds semid_ds;
3962 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00003963
aurel32e5289082009-04-18 16:16:12 +00003964 semun.buf = &semid_ds;
3965
3966 ret = semctl(semid, 0, IPC_STAT, semun);
3967 if (ret == -1)
3968 return get_errno(ret);
3969
3970 nsems = semid_ds.sem_nsems;
3971
3972 array = lock_user(VERIFY_WRITE, target_addr,
3973 nsems*sizeof(unsigned short), 0);
3974 if (!array)
3975 return -TARGET_EFAULT;
3976
3977 for(i=0; i<nsems; i++) {
3978 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00003979 }
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303980 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00003981 unlock_user(array, target_addr, 1);
3982
bellard579a97f2007-11-11 14:26:47 +00003983 return 0;
ths3eb6b042007-06-03 14:26:27 +00003984}
3985
aurel32e5289082009-04-18 16:16:12 +00003986static inline abi_long do_semctl(int semid, int semnum, int cmd,
Stefan Weild1c002b2015-02-08 15:40:58 +01003987 abi_ulong target_arg)
ths3eb6b042007-06-03 14:26:27 +00003988{
Stefan Weild1c002b2015-02-08 15:40:58 +01003989 union target_semun target_su = { .buf = target_arg };
ths3eb6b042007-06-03 14:26:27 +00003990 union semun arg;
3991 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05303992 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00003993 struct seminfo seminfo;
3994 abi_long ret = -TARGET_EINVAL;
3995 abi_long err;
3996 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00003997
3998 switch( cmd ) {
3999 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00004000 case SETVAL:
Tom Musta5464bae2014-08-12 13:53:34 -05004001 /* In 64 bit cross-endian situations, we will erroneously pick up
4002 * the wrong half of the union for the "val" element. To rectify
4003 * this, the entire 8-byte structure is byteswapped, followed by
4004 * a swap of the 4 byte val field. In other cases, the data is
4005 * already in proper host byte order. */
4006 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
4007 target_su.buf = tswapal(target_su.buf);
4008 arg.val = tswap32(target_su.val);
4009 } else {
4010 arg.val = target_su.val;
4011 }
aurel32e5289082009-04-18 16:16:12 +00004012 ret = get_errno(semctl(semid, semnum, cmd, arg));
ths3eb6b042007-06-03 14:26:27 +00004013 break;
4014 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00004015 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00004016 err = target_to_host_semarray(semid, &array, target_su.array);
4017 if (err)
4018 return err;
4019 arg.array = array;
4020 ret = get_errno(semctl(semid, semnum, cmd, arg));
4021 err = host_to_target_semarray(semid, target_su.array, &array);
4022 if (err)
4023 return err;
ths3eb6b042007-06-03 14:26:27 +00004024 break;
4025 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00004026 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00004027 case SEM_STAT:
4028 err = target_to_host_semid_ds(&dsarg, target_su.buf);
4029 if (err)
4030 return err;
4031 arg.buf = &dsarg;
4032 ret = get_errno(semctl(semid, semnum, cmd, arg));
4033 err = host_to_target_semid_ds(target_su.buf, &dsarg);
4034 if (err)
4035 return err;
ths3eb6b042007-06-03 14:26:27 +00004036 break;
aurel32e5289082009-04-18 16:16:12 +00004037 case IPC_INFO:
4038 case SEM_INFO:
4039 arg.__buf = &seminfo;
4040 ret = get_errno(semctl(semid, semnum, cmd, arg));
4041 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4042 if (err)
4043 return err;
4044 break;
4045 case IPC_RMID:
4046 case GETPID:
4047 case GETNCNT:
4048 case GETZCNT:
4049 ret = get_errno(semctl(semid, semnum, cmd, NULL));
4050 break;
ths3eb6b042007-06-03 14:26:27 +00004051 }
4052
4053 return ret;
4054}
4055
aurel32e5289082009-04-18 16:16:12 +00004056struct target_sembuf {
4057 unsigned short sem_num;
4058 short sem_op;
4059 short sem_flg;
4060};
4061
4062static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4063 abi_ulong target_addr,
4064 unsigned nsops)
4065{
4066 struct target_sembuf *target_sembuf;
4067 int i;
4068
4069 target_sembuf = lock_user(VERIFY_READ, target_addr,
4070 nsops*sizeof(struct target_sembuf), 1);
4071 if (!target_sembuf)
4072 return -TARGET_EFAULT;
4073
4074 for(i=0; i<nsops; i++) {
4075 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4076 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4077 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4078 }
4079
4080 unlock_user(target_sembuf, target_addr, 0);
4081
4082 return 0;
4083}
4084
Matus Kyseld8c08b12020-06-26 14:46:11 +02004085#if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
Filip Bozutacac46eb2020-08-25 00:30:50 +02004086 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
Matus Kyseld8c08b12020-06-26 14:46:11 +02004087
4088/*
4089 * This macro is required to handle the s390 variants, which passes the
4090 * arguments in a different order than default.
4091 */
4092#ifdef __s390x__
4093#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4094 (__nsops), (__timeout), (__sops)
4095#else
4096#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4097 (__nsops), 0, (__sops), (__timeout)
4098#endif
4099
4100static inline abi_long do_semtimedop(int semid,
4101 abi_long ptr,
4102 unsigned nsops,
Filip Bozutacac46eb2020-08-25 00:30:50 +02004103 abi_long timeout, bool time64)
aurel32e5289082009-04-18 16:16:12 +00004104{
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004105 struct sembuf *sops;
Matus Kyseld8c08b12020-06-26 14:46:11 +02004106 struct timespec ts, *pts = NULL;
Laurent Vivier524fa342019-05-29 10:48:04 +02004107 abi_long ret;
aurel32e5289082009-04-18 16:16:12 +00004108
Matus Kyseld8c08b12020-06-26 14:46:11 +02004109 if (timeout) {
4110 pts = &ts;
Filip Bozutacac46eb2020-08-25 00:30:50 +02004111 if (time64) {
4112 if (target_to_host_timespec64(pts, timeout)) {
4113 return -TARGET_EFAULT;
4114 }
4115 } else {
4116 if (target_to_host_timespec(pts, timeout)) {
4117 return -TARGET_EFAULT;
4118 }
Matus Kyseld8c08b12020-06-26 14:46:11 +02004119 }
4120 }
4121
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004122 if (nsops > TARGET_SEMOPM) {
4123 return -TARGET_E2BIG;
4124 }
4125
4126 sops = g_new(struct sembuf, nsops);
4127
4128 if (target_to_host_sembuf(sops, ptr, nsops)) {
4129 g_free(sops);
aurel32e5289082009-04-18 16:16:12 +00004130 return -TARGET_EFAULT;
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004131 }
aurel32e5289082009-04-18 16:16:12 +00004132
Laurent Vivier524fa342019-05-29 10:48:04 +02004133 ret = -TARGET_ENOSYS;
4134#ifdef __NR_semtimedop
Matus Kyseld8c08b12020-06-26 14:46:11 +02004135 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
Laurent Vivier524fa342019-05-29 10:48:04 +02004136#endif
4137#ifdef __NR_ipc
4138 if (ret == -TARGET_ENOSYS) {
Matus Kyseld8c08b12020-06-26 14:46:11 +02004139 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
4140 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
Laurent Vivier524fa342019-05-29 10:48:04 +02004141 }
4142#endif
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004143 g_free(sops);
Laurent Vivier524fa342019-05-29 10:48:04 +02004144 return ret;
aurel32e5289082009-04-18 16:16:12 +00004145}
Matus Kyseld8c08b12020-06-26 14:46:11 +02004146#endif
aurel32e5289082009-04-18 16:16:12 +00004147
ths1bc012f2007-06-03 14:27:49 +00004148struct target_msqid_ds
4149{
aurel321c54ff92008-10-13 21:08:44 +00004150 struct target_ipc_perm msg_perm;
4151 abi_ulong msg_stime;
4152#if TARGET_ABI_BITS == 32
4153 abi_ulong __unused1;
4154#endif
4155 abi_ulong msg_rtime;
4156#if TARGET_ABI_BITS == 32
4157 abi_ulong __unused2;
4158#endif
4159 abi_ulong msg_ctime;
4160#if TARGET_ABI_BITS == 32
4161 abi_ulong __unused3;
4162#endif
4163 abi_ulong __msg_cbytes;
4164 abi_ulong msg_qnum;
4165 abi_ulong msg_qbytes;
4166 abi_ulong msg_lspid;
4167 abi_ulong msg_lrpid;
4168 abi_ulong __unused4;
4169 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00004170};
4171
bellard579a97f2007-11-11 14:26:47 +00004172static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4173 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00004174{
4175 struct target_msqid_ds *target_md;
4176
bellard579a97f2007-11-11 14:26:47 +00004177 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4178 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00004179 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4180 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004181 host_md->msg_stime = tswapal(target_md->msg_stime);
4182 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4183 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4184 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4185 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4186 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4187 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4188 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00004189 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00004190 return 0;
ths1bc012f2007-06-03 14:27:49 +00004191}
4192
bellard579a97f2007-11-11 14:26:47 +00004193static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4194 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00004195{
4196 struct target_msqid_ds *target_md;
4197
bellard579a97f2007-11-11 14:26:47 +00004198 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4199 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00004200 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4201 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004202 target_md->msg_stime = tswapal(host_md->msg_stime);
4203 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4204 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4205 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4206 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4207 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4208 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4209 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00004210 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00004211 return 0;
ths1bc012f2007-06-03 14:27:49 +00004212}
4213
aurel321c54ff92008-10-13 21:08:44 +00004214struct target_msginfo {
4215 int msgpool;
4216 int msgmap;
4217 int msgmax;
4218 int msgmnb;
4219 int msgmni;
4220 int msgssz;
4221 int msgtql;
4222 unsigned short int msgseg;
4223};
4224
4225static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4226 struct msginfo *host_msginfo)
4227{
4228 struct target_msginfo *target_msginfo;
4229 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4230 return -TARGET_EFAULT;
4231 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4232 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4233 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4234 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4235 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4236 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4237 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4238 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4239 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00004240 return 0;
aurel321c54ff92008-10-13 21:08:44 +00004241}
4242
4243static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00004244{
4245 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00004246 struct msginfo msginfo;
4247 abi_long ret = -TARGET_EINVAL;
4248
4249 cmd &= 0xff;
4250
4251 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00004252 case IPC_STAT:
4253 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00004254 case MSG_STAT:
4255 if (target_to_host_msqid_ds(&dsarg,ptr))
4256 return -TARGET_EFAULT;
4257 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4258 if (host_to_target_msqid_ds(ptr,&dsarg))
4259 return -TARGET_EFAULT;
4260 break;
4261 case IPC_RMID:
4262 ret = get_errno(msgctl(msgid, cmd, NULL));
4263 break;
4264 case IPC_INFO:
4265 case MSG_INFO:
4266 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4267 if (host_to_target_msginfo(ptr, &msginfo))
4268 return -TARGET_EFAULT;
4269 break;
ths1bc012f2007-06-03 14:27:49 +00004270 }
aurel321c54ff92008-10-13 21:08:44 +00004271
ths1bc012f2007-06-03 14:27:49 +00004272 return ret;
4273}
4274
4275struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00004276 abi_long mtype;
4277 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00004278};
4279
blueswir1992f48a2007-10-14 16:27:31 +00004280static inline abi_long do_msgsnd(int msqid, abi_long msgp,
Tom Mustaedcc5f92014-08-12 13:53:37 -05004281 ssize_t msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00004282{
4283 struct target_msgbuf *target_mb;
4284 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00004285 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00004286
Tom Mustaedcc5f92014-08-12 13:53:37 -05004287 if (msgsz < 0) {
4288 return -TARGET_EINVAL;
4289 }
4290
bellard579a97f2007-11-11 14:26:47 +00004291 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4292 return -TARGET_EFAULT;
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304293 host_mb = g_try_malloc(msgsz + sizeof(long));
zhanghailiang29e03fc2014-08-14 15:29:18 +08004294 if (!host_mb) {
4295 unlock_user_struct(target_mb, msgp, 0);
4296 return -TARGET_ENOMEM;
4297 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004298 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00004299 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
Laurent Vivier524fa342019-05-29 10:48:04 +02004300 ret = -TARGET_ENOSYS;
4301#ifdef __NR_msgsnd
Peter Maydell89f9fe42016-06-06 19:58:05 +01004302 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
Laurent Vivier524fa342019-05-29 10:48:04 +02004303#endif
4304#ifdef __NR_ipc
4305 if (ret == -TARGET_ENOSYS) {
Matus Kyseld8c08b12020-06-26 14:46:11 +02004306#ifdef __s390x__
4307 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4308 host_mb));
4309#else
Laurent Vivier524fa342019-05-29 10:48:04 +02004310 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4311 host_mb, 0));
Matus Kyseld8c08b12020-06-26 14:46:11 +02004312#endif
Laurent Vivier524fa342019-05-29 10:48:04 +02004313 }
4314#endif
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304315 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00004316 unlock_user_struct(target_mb, msgp, 0);
4317
4318 return ret;
4319}
4320
Matus Kyseld8c08b12020-06-26 14:46:11 +02004321#ifdef __NR_ipc
4322#if defined(__sparc__)
4323/* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4324#define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4325#elif defined(__s390x__)
4326/* The s390 sys_ipc variant has only five parameters. */
4327#define MSGRCV_ARGS(__msgp, __msgtyp) \
4328 ((long int[]){(long int)__msgp, __msgtyp})
4329#else
4330#define MSGRCV_ARGS(__msgp, __msgtyp) \
4331 ((long int[]){(long int)__msgp, __msgtyp}), 0
4332#endif
4333#endif
4334
blueswir1992f48a2007-10-14 16:27:31 +00004335static inline abi_long do_msgrcv(int msqid, abi_long msgp,
Peter Maydell99874f62016-05-20 19:00:56 +01004336 ssize_t msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00004337 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00004338{
4339 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00004340 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00004341 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00004342 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00004343
Peter Maydell99874f62016-05-20 19:00:56 +01004344 if (msgsz < 0) {
4345 return -TARGET_EINVAL;
4346 }
4347
bellard579a97f2007-11-11 14:26:47 +00004348 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4349 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00004350
Peter Maydell415d8472016-05-20 19:00:57 +01004351 host_mb = g_try_malloc(msgsz + sizeof(long));
4352 if (!host_mb) {
4353 ret = -TARGET_ENOMEM;
4354 goto end;
4355 }
Laurent Vivier524fa342019-05-29 10:48:04 +02004356 ret = -TARGET_ENOSYS;
4357#ifdef __NR_msgrcv
Peter Maydell89f9fe42016-06-06 19:58:05 +01004358 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
Laurent Vivier524fa342019-05-29 10:48:04 +02004359#endif
4360#ifdef __NR_ipc
4361 if (ret == -TARGET_ENOSYS) {
4362 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
Matus Kyseld8c08b12020-06-26 14:46:11 +02004363 msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
Laurent Vivier524fa342019-05-29 10:48:04 +02004364 }
4365#endif
aurel321c54ff92008-10-13 21:08:44 +00004366
bellard579a97f2007-11-11 14:26:47 +00004367 if (ret > 0) {
4368 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4369 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4370 if (!target_mtext) {
4371 ret = -TARGET_EFAULT;
4372 goto end;
4373 }
aurel321c54ff92008-10-13 21:08:44 +00004374 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00004375 unlock_user(target_mtext, target_mtext_addr, ret);
4376 }
aurel321c54ff92008-10-13 21:08:44 +00004377
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004378 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00004379
bellard579a97f2007-11-11 14:26:47 +00004380end:
4381 if (target_mb)
4382 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02004383 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00004384 return ret;
4385}
4386
Riku Voipio88a8c982009-04-03 10:42:00 +03004387static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4388 abi_ulong target_addr)
4389{
4390 struct target_shmid_ds *target_sd;
4391
4392 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4393 return -TARGET_EFAULT;
4394 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4395 return -TARGET_EFAULT;
4396 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4397 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4398 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4399 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4400 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4401 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4402 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4403 unlock_user_struct(target_sd, target_addr, 0);
4404 return 0;
4405}
4406
4407static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4408 struct shmid_ds *host_sd)
4409{
4410 struct target_shmid_ds *target_sd;
4411
4412 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4413 return -TARGET_EFAULT;
4414 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
4415 return -TARGET_EFAULT;
4416 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4417 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
4418 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4419 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4420 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4421 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4422 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4423 unlock_user_struct(target_sd, target_addr, 1);
4424 return 0;
4425}
4426
4427struct target_shminfo {
4428 abi_ulong shmmax;
4429 abi_ulong shmmin;
4430 abi_ulong shmmni;
4431 abi_ulong shmseg;
4432 abi_ulong shmall;
4433};
4434
4435static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
4436 struct shminfo *host_shminfo)
4437{
4438 struct target_shminfo *target_shminfo;
4439 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
4440 return -TARGET_EFAULT;
4441 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
4442 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
4443 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
4444 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
4445 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
4446 unlock_user_struct(target_shminfo, target_addr, 1);
4447 return 0;
4448}
4449
4450struct target_shm_info {
4451 int used_ids;
4452 abi_ulong shm_tot;
4453 abi_ulong shm_rss;
4454 abi_ulong shm_swp;
4455 abi_ulong swap_attempts;
4456 abi_ulong swap_successes;
4457};
4458
4459static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
4460 struct shm_info *host_shm_info)
4461{
4462 struct target_shm_info *target_shm_info;
4463 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
4464 return -TARGET_EFAULT;
4465 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
4466 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
4467 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
4468 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
4469 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
4470 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
4471 unlock_user_struct(target_shm_info, target_addr, 1);
4472 return 0;
4473}
4474
4475static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4476{
4477 struct shmid_ds dsarg;
4478 struct shminfo shminfo;
4479 struct shm_info shm_info;
4480 abi_long ret = -TARGET_EINVAL;
4481
4482 cmd &= 0xff;
4483
4484 switch(cmd) {
4485 case IPC_STAT:
4486 case IPC_SET:
4487 case SHM_STAT:
4488 if (target_to_host_shmid_ds(&dsarg, buf))
4489 return -TARGET_EFAULT;
4490 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4491 if (host_to_target_shmid_ds(buf, &dsarg))
4492 return -TARGET_EFAULT;
4493 break;
4494 case IPC_INFO:
4495 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4496 if (host_to_target_shminfo(buf, &shminfo))
4497 return -TARGET_EFAULT;
4498 break;
4499 case SHM_INFO:
4500 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4501 if (host_to_target_shm_info(buf, &shm_info))
4502 return -TARGET_EFAULT;
4503 break;
4504 case IPC_RMID:
4505 case SHM_LOCK:
4506 case SHM_UNLOCK:
4507 ret = get_errno(shmctl(shmid, cmd, NULL));
4508 break;
4509 }
4510
4511 return ret;
4512}
4513
Peter Maydellee8e7612016-07-11 16:48:11 +01004514#ifndef TARGET_FORCE_SHMLBA
4515/* For most architectures, SHMLBA is the same as the page size;
4516 * some architectures have larger values, in which case they should
4517 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4518 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4519 * and defining its own value for SHMLBA.
4520 *
4521 * The kernel also permits SHMLBA to be set by the architecture to a
4522 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4523 * this means that addresses are rounded to the large size if
4524 * SHM_RND is set but addresses not aligned to that size are not rejected
4525 * as long as they are at least page-aligned. Since the only architecture
4526 * which uses this is ia64 this code doesn't provide for that oddity.
4527 */
4528static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
4529{
4530 return TARGET_PAGE_SIZE;
4531}
4532#endif
4533
4534static inline abi_ulong do_shmat(CPUArchState *cpu_env,
4535 int shmid, abi_ulong shmaddr, int shmflg)
Riku Voipio88a8c982009-04-03 10:42:00 +03004536{
Richard Henderson228168c2021-06-11 23:08:28 -07004537 CPUState *cpu = env_cpu(cpu_env);
Riku Voipio88a8c982009-04-03 10:42:00 +03004538 abi_long raddr;
4539 void *host_raddr;
4540 struct shmid_ds shm_info;
4541 int i,ret;
Peter Maydellee8e7612016-07-11 16:48:11 +01004542 abi_ulong shmlba;
Riku Voipio88a8c982009-04-03 10:42:00 +03004543
Richard Hendersonee1bf832021-02-12 10:48:44 -08004544 /* shmat pointers are always untagged */
4545
Riku Voipio88a8c982009-04-03 10:42:00 +03004546 /* find out the length of the shared memory segment */
4547 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
4548 if (is_error(ret)) {
4549 /* can't get length, bail out */
4550 return ret;
4551 }
4552
Peter Maydellee8e7612016-07-11 16:48:11 +01004553 shmlba = target_shmlba(cpu_env);
4554
4555 if (shmaddr & (shmlba - 1)) {
4556 if (shmflg & SHM_RND) {
4557 shmaddr &= ~(shmlba - 1);
4558 } else {
4559 return -TARGET_EINVAL;
4560 }
4561 }
Richard Henderson46b12f42021-02-12 10:48:46 -08004562 if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
Max Filippovebf9a362018-03-07 13:50:10 -08004563 return -TARGET_EINVAL;
4564 }
Peter Maydellee8e7612016-07-11 16:48:11 +01004565
Riku Voipio88a8c982009-04-03 10:42:00 +03004566 mmap_lock();
4567
Richard Henderson228168c2021-06-11 23:08:28 -07004568 /*
4569 * We're mapping shared memory, so ensure we generate code for parallel
4570 * execution and flush old translations. This will work up to the level
4571 * supported by the host -- anything that requires EXCP_ATOMIC will not
4572 * be atomic with respect to an external process.
4573 */
4574 if (!(cpu->tcg_cflags & CF_PARALLEL)) {
4575 cpu->tcg_cflags |= CF_PARALLEL;
4576 tb_flush(cpu);
4577 }
4578
Riku Voipio88a8c982009-04-03 10:42:00 +03004579 if (shmaddr)
Richard Henderson3e8f1622021-02-12 10:48:43 -08004580 host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
Riku Voipio88a8c982009-04-03 10:42:00 +03004581 else {
4582 abi_ulong mmap_start;
4583
Richard Henderson30ab9ef2019-05-19 13:19:52 -07004584 /* In order to use the host shmat, we need to honor host SHMLBA. */
4585 mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
Riku Voipio88a8c982009-04-03 10:42:00 +03004586
4587 if (mmap_start == -1) {
4588 errno = ENOMEM;
4589 host_raddr = (void *)-1;
4590 } else
Richard Henderson3e8f1622021-02-12 10:48:43 -08004591 host_raddr = shmat(shmid, g2h_untagged(mmap_start),
4592 shmflg | SHM_REMAP);
Riku Voipio88a8c982009-04-03 10:42:00 +03004593 }
4594
4595 if (host_raddr == (void *)-1) {
4596 mmap_unlock();
4597 return get_errno((long)host_raddr);
4598 }
4599 raddr=h2g((unsigned long)host_raddr);
4600
4601 page_set_flags(raddr, raddr + shm_info.shm_segsz,
Richard Hendersond9c58582021-02-12 10:48:32 -08004602 PAGE_VALID | PAGE_RESET | PAGE_READ |
4603 (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
Riku Voipio88a8c982009-04-03 10:42:00 +03004604
4605 for (i = 0; i < N_SHM_REGIONS; i++) {
Peter Maydellb6e17872016-02-09 15:57:11 +00004606 if (!shm_regions[i].in_use) {
4607 shm_regions[i].in_use = true;
Riku Voipio88a8c982009-04-03 10:42:00 +03004608 shm_regions[i].start = raddr;
4609 shm_regions[i].size = shm_info.shm_segsz;
4610 break;
4611 }
4612 }
4613
4614 mmap_unlock();
4615 return raddr;
4616
4617}
4618
4619static inline abi_long do_shmdt(abi_ulong shmaddr)
4620{
4621 int i;
Max Filippov3c5f6a52018-02-28 14:16:04 -08004622 abi_long rv;
4623
Richard Hendersonee1bf832021-02-12 10:48:44 -08004624 /* shmdt pointers are always untagged */
4625
Max Filippov3c5f6a52018-02-28 14:16:04 -08004626 mmap_lock();
Riku Voipio88a8c982009-04-03 10:42:00 +03004627
4628 for (i = 0; i < N_SHM_REGIONS; ++i) {
Peter Maydellb6e17872016-02-09 15:57:11 +00004629 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
4630 shm_regions[i].in_use = false;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09004631 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03004632 break;
4633 }
4634 }
Richard Henderson3e8f1622021-02-12 10:48:43 -08004635 rv = get_errno(shmdt(g2h_untagged(shmaddr)));
Riku Voipio88a8c982009-04-03 10:42:00 +03004636
Max Filippov3c5f6a52018-02-28 14:16:04 -08004637 mmap_unlock();
4638
4639 return rv;
Riku Voipio88a8c982009-04-03 10:42:00 +03004640}
4641
aurel321c54ff92008-10-13 21:08:44 +00004642#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00004643/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00004644/* do_ipc() must return target values and target errnos. */
Peter Maydellee8e7612016-07-11 16:48:11 +01004645static abi_long do_ipc(CPUArchState *cpu_env,
4646 unsigned int call, abi_long first,
Tom Musta37ed0952014-08-12 13:53:35 -05004647 abi_long second, abi_long third,
blueswir1992f48a2007-10-14 16:27:31 +00004648 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00004649{
4650 int version;
blueswir1992f48a2007-10-14 16:27:31 +00004651 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00004652
4653 version = call >> 16;
4654 call &= 0xffff;
4655
4656 switch (call) {
thsfa294812007-02-02 22:05:00 +00004657 case IPCOP_semop:
Filip Bozutacac46eb2020-08-25 00:30:50 +02004658 ret = do_semtimedop(first, ptr, second, 0, false);
Matus Kyseld8c08b12020-06-26 14:46:11 +02004659 break;
4660 case IPCOP_semtimedop:
4661 /*
4662 * The s390 sys_ipc variant has only five parameters instead of six
4663 * (as for default variant) and the only difference is the handling of
4664 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4665 * to a struct timespec where the generic variant uses fifth parameter.
4666 */
4667#if defined(TARGET_S390X)
Filip Bozutacac46eb2020-08-25 00:30:50 +02004668 ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
Matus Kyseld8c08b12020-06-26 14:46:11 +02004669#else
Filip Bozutacac46eb2020-08-25 00:30:50 +02004670 ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
Matus Kyseld8c08b12020-06-26 14:46:11 +02004671#endif
thsfa294812007-02-02 22:05:00 +00004672 break;
4673
4674 case IPCOP_semget:
4675 ret = get_errno(semget(first, second, third));
4676 break;
4677
Tom Musta5d2fa8e2014-08-12 13:53:33 -05004678 case IPCOP_semctl: {
4679 /* The semun argument to semctl is passed by value, so dereference the
4680 * ptr argument. */
4681 abi_ulong atptr;
Tom Musta37ed0952014-08-12 13:53:35 -05004682 get_user_ual(atptr, ptr);
Stefan Weild1c002b2015-02-08 15:40:58 +01004683 ret = do_semctl(first, second, third, atptr);
thsfa294812007-02-02 22:05:00 +00004684 break;
Tom Musta5d2fa8e2014-08-12 13:53:33 -05004685 }
thsd96372e2007-02-02 22:05:44 +00004686
aurel321c54ff92008-10-13 21:08:44 +00004687 case IPCOP_msgget:
4688 ret = get_errno(msgget(first, second));
4689 break;
thsd96372e2007-02-02 22:05:44 +00004690
aurel321c54ff92008-10-13 21:08:44 +00004691 case IPCOP_msgsnd:
4692 ret = do_msgsnd(first, ptr, second, third);
4693 break;
thsd96372e2007-02-02 22:05:44 +00004694
aurel321c54ff92008-10-13 21:08:44 +00004695 case IPCOP_msgctl:
4696 ret = do_msgctl(first, second, ptr);
4697 break;
thsd96372e2007-02-02 22:05:44 +00004698
aurel321c54ff92008-10-13 21:08:44 +00004699 case IPCOP_msgrcv:
4700 switch (version) {
4701 case 0:
4702 {
4703 struct target_ipc_kludge {
4704 abi_long msgp;
4705 abi_long msgtyp;
4706 } *tmp;
thsd96372e2007-02-02 22:05:44 +00004707
aurel321c54ff92008-10-13 21:08:44 +00004708 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4709 ret = -TARGET_EFAULT;
4710 break;
ths1bc012f2007-06-03 14:27:49 +00004711 }
aurel321c54ff92008-10-13 21:08:44 +00004712
Laurent Vivier79dd77d2012-12-20 11:00:11 +00004713 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00004714
4715 unlock_user_struct(tmp, ptr, 0);
4716 break;
4717 }
4718 default:
4719 ret = do_msgrcv(first, ptr, second, fifth, third);
4720 }
4721 break;
thsd96372e2007-02-02 22:05:44 +00004722
bellard8853f862004-02-22 14:57:26 +00004723 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03004724 switch (version) {
4725 default:
bellard5a4a8982007-11-11 17:39:18 +00004726 {
4727 abi_ulong raddr;
Peter Maydellee8e7612016-07-11 16:48:11 +01004728 raddr = do_shmat(cpu_env, first, ptr, second);
Riku Voipio88a8c982009-04-03 10:42:00 +03004729 if (is_error(raddr))
4730 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00004731 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00004732 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03004733 break;
4734 }
4735 case 1:
4736 ret = -TARGET_EINVAL;
4737 break;
bellard5a4a8982007-11-11 17:39:18 +00004738 }
bellard8853f862004-02-22 14:57:26 +00004739 break;
4740 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03004741 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00004742 break;
4743
4744 case IPCOP_shmget:
4745 /* IPC_* flag values are the same on all linux platforms */
4746 ret = get_errno(shmget(first, second, third));
4747 break;
4748
4749 /* IPC_* and SHM_* command values are the same on all linux platforms */
4750 case IPCOP_shmctl:
Petar Jovanovica2926782013-10-30 14:46:32 +01004751 ret = do_shmctl(first, second, ptr);
bellard8853f862004-02-22 14:57:26 +00004752 break;
4753 default:
Josh Kunz39be5352020-02-03 18:54:13 -08004754 qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
4755 call, version);
ths0da46a62007-10-20 20:23:07 +00004756 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00004757 break;
4758 }
4759 return ret;
4760}
j_mayer32407102007-09-26 23:01:49 +00004761#endif
bellard8853f862004-02-22 14:57:26 +00004762
bellard31e31b82003-02-18 22:55:36 +00004763/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00004764
Blue Swirl001faf32009-05-13 17:53:17 +00004765#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00004766#define STRUCT_SPECIAL(name) STRUCT_ ## name,
4767enum {
4768#include "syscall_types.h"
Alexander Graf8be656b2015-05-06 23:47:32 +02004769STRUCT_MAX
bellard31e31b82003-02-18 22:55:36 +00004770};
4771#undef STRUCT
4772#undef STRUCT_SPECIAL
4773
Blue Swirl001faf32009-05-13 17:53:17 +00004774#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00004775#define STRUCT_SPECIAL(name)
4776#include "syscall_types.h"
4777#undef STRUCT
4778#undef STRUCT_SPECIAL
4779
bellard31e31b82003-02-18 22:55:36 +00004780#define MAX_STRUCT_SIZE 4096
4781
Peter Maydelldace20d2011-01-10 13:11:24 +00004782#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00004783/* So fiemap access checks don't overflow on 32 bit systems.
4784 * This is very slightly smaller than the limit imposed by
4785 * the underlying kernel.
4786 */
4787#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4788 / sizeof(struct fiemap_extent))
4789
4790static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004791 int fd, int cmd, abi_long arg)
Peter Maydell285da2b2011-01-06 15:04:18 +00004792{
4793 /* The parameter for this ioctl is a struct fiemap followed
4794 * by an array of struct fiemap_extent whose size is set
4795 * in fiemap->fm_extent_count. The array is filled in by the
4796 * ioctl.
4797 */
4798 int target_size_in, target_size_out;
4799 struct fiemap *fm;
4800 const argtype *arg_type = ie->arg_type;
4801 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4802 void *argptr, *p;
4803 abi_long ret;
4804 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4805 uint32_t outbufsz;
4806 int free_fm = 0;
4807
4808 assert(arg_type[0] == TYPE_PTR);
4809 assert(ie->access == IOC_RW);
4810 arg_type++;
4811 target_size_in = thunk_type_size(arg_type, 0);
4812 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4813 if (!argptr) {
4814 return -TARGET_EFAULT;
4815 }
4816 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4817 unlock_user(argptr, arg, 0);
4818 fm = (struct fiemap *)buf_temp;
4819 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4820 return -TARGET_EINVAL;
4821 }
4822
4823 outbufsz = sizeof (*fm) +
4824 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4825
4826 if (outbufsz > MAX_STRUCT_SIZE) {
4827 /* We can't fit all the extents into the fixed size buffer.
4828 * Allocate one that is large enough and use it instead.
4829 */
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304830 fm = g_try_malloc(outbufsz);
Peter Maydell285da2b2011-01-06 15:04:18 +00004831 if (!fm) {
4832 return -TARGET_ENOMEM;
4833 }
4834 memcpy(fm, buf_temp, sizeof(struct fiemap));
4835 free_fm = 1;
4836 }
Peter Maydell49ca6f32016-06-06 19:58:14 +01004837 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
Peter Maydell285da2b2011-01-06 15:04:18 +00004838 if (!is_error(ret)) {
4839 target_size_out = target_size_in;
4840 /* An extent_count of 0 means we were only counting the extents
4841 * so there are no structs to copy
4842 */
4843 if (fm->fm_extent_count != 0) {
4844 target_size_out += fm->fm_mapped_extents * extent_size;
4845 }
4846 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4847 if (!argptr) {
4848 ret = -TARGET_EFAULT;
4849 } else {
4850 /* Convert the struct fiemap */
4851 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4852 if (fm->fm_extent_count != 0) {
4853 p = argptr + target_size_in;
4854 /* ...and then all the struct fiemap_extents */
4855 for (i = 0; i < fm->fm_mapped_extents; i++) {
4856 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4857 THUNK_TARGET);
4858 p += extent_size;
4859 }
4860 }
4861 unlock_user(argptr, arg, target_size_out);
4862 }
4863 }
4864 if (free_fm) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304865 g_free(fm);
Peter Maydell285da2b2011-01-06 15:04:18 +00004866 }
4867 return ret;
4868}
Peter Maydelldace20d2011-01-10 13:11:24 +00004869#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00004870
Laurent Vivier059c2f22011-03-30 00:12:12 +02004871static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004872 int fd, int cmd, abi_long arg)
Laurent Vivier059c2f22011-03-30 00:12:12 +02004873{
4874 const argtype *arg_type = ie->arg_type;
4875 int target_size;
4876 void *argptr;
4877 int ret;
4878 struct ifconf *host_ifconf;
4879 uint32_t outbufsz;
4880 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
Stefan4df7b7f2021-01-09 19:59:42 +01004881 const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
Laurent Vivier059c2f22011-03-30 00:12:12 +02004882 int target_ifreq_size;
4883 int nb_ifreq;
4884 int free_buf = 0;
4885 int i;
4886 int target_ifc_len;
4887 abi_long target_ifc_buf;
4888 int host_ifc_len;
4889 char *host_ifc_buf;
4890
4891 assert(arg_type[0] == TYPE_PTR);
4892 assert(ie->access == IOC_RW);
4893
4894 arg_type++;
4895 target_size = thunk_type_size(arg_type, 0);
4896
4897 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4898 if (!argptr)
4899 return -TARGET_EFAULT;
4900 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4901 unlock_user(argptr, arg, 0);
4902
4903 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
Laurent Vivier059c2f22011-03-30 00:12:12 +02004904 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
Stefan4df7b7f2021-01-09 19:59:42 +01004905 target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004906
Kan Li22e4a2672018-10-24 20:13:03 +00004907 if (target_ifc_buf != 0) {
4908 target_ifc_len = host_ifconf->ifc_len;
4909 nb_ifreq = target_ifc_len / target_ifreq_size;
4910 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4911
4912 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4913 if (outbufsz > MAX_STRUCT_SIZE) {
4914 /*
4915 * We can't fit all the extents into the fixed size buffer.
4916 * Allocate one that is large enough and use it instead.
4917 */
Ahmed Abouzied7a5626a2022-01-04 16:38:41 +02004918 host_ifconf = g_try_malloc(outbufsz);
Kan Li22e4a2672018-10-24 20:13:03 +00004919 if (!host_ifconf) {
4920 return -TARGET_ENOMEM;
4921 }
4922 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4923 free_buf = 1;
Laurent Vivier059c2f22011-03-30 00:12:12 +02004924 }
Kan Li22e4a2672018-10-24 20:13:03 +00004925 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004926
Kan Li22e4a2672018-10-24 20:13:03 +00004927 host_ifconf->ifc_len = host_ifc_len;
4928 } else {
4929 host_ifc_buf = NULL;
4930 }
Laurent Vivier059c2f22011-03-30 00:12:12 +02004931 host_ifconf->ifc_buf = host_ifc_buf;
4932
Peter Maydell49ca6f32016-06-06 19:58:14 +01004933 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
Laurent Vivier059c2f22011-03-30 00:12:12 +02004934 if (!is_error(ret)) {
4935 /* convert host ifc_len to target ifc_len */
4936
4937 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4938 target_ifc_len = nb_ifreq * target_ifreq_size;
4939 host_ifconf->ifc_len = target_ifc_len;
4940
4941 /* restore target ifc_buf */
4942
4943 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4944
4945 /* copy struct ifconf to target user */
4946
4947 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4948 if (!argptr)
4949 return -TARGET_EFAULT;
4950 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4951 unlock_user(argptr, arg, target_size);
4952
Kan Li22e4a2672018-10-24 20:13:03 +00004953 if (target_ifc_buf != 0) {
4954 /* copy ifreq[] to target user */
4955 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4956 for (i = 0; i < nb_ifreq ; i++) {
4957 thunk_convert(argptr + i * target_ifreq_size,
4958 host_ifc_buf + i * sizeof(struct ifreq),
4959 ifreq_arg_type, THUNK_TARGET);
4960 }
4961 unlock_user(argptr, target_ifc_buf, target_ifc_len);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004962 }
Laurent Vivier059c2f22011-03-30 00:12:12 +02004963 }
4964
4965 if (free_buf) {
Ahmed Abouzied7a5626a2022-01-04 16:38:41 +02004966 g_free(host_ifconf);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004967 }
4968
4969 return ret;
4970}
4971
Cortland Tölvaa1333672018-10-08 09:35:21 -07004972#if defined(CONFIG_USBFS)
4973#if HOST_LONG_BITS > 64
4974#error USBDEVFS thunks do not support >64 bit hosts yet.
4975#endif
4976struct live_urb {
4977 uint64_t target_urb_adr;
4978 uint64_t target_buf_adr;
4979 char *target_buf_ptr;
4980 struct usbdevfs_urb host_urb;
4981};
4982
4983static GHashTable *usbdevfs_urb_hashtable(void)
4984{
4985 static GHashTable *urb_hashtable;
4986
4987 if (!urb_hashtable) {
4988 urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
4989 }
4990 return urb_hashtable;
4991}
4992
4993static void urb_hashtable_insert(struct live_urb *urb)
4994{
4995 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4996 g_hash_table_insert(urb_hashtable, urb, urb);
4997}
4998
4999static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
5000{
5001 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
5002 return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
5003}
5004
5005static void urb_hashtable_remove(struct live_urb *urb)
5006{
5007 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
5008 g_hash_table_remove(urb_hashtable, urb);
5009}
5010
5011static abi_long
5012do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
5013 int fd, int cmd, abi_long arg)
5014{
5015 const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
5016 const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
5017 struct live_urb *lurb;
5018 void *argptr;
5019 uint64_t hurb;
5020 int target_size;
5021 uintptr_t target_urb_adr;
5022 abi_long ret;
5023
5024 target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
5025
5026 memset(buf_temp, 0, sizeof(uint64_t));
5027 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
5028 if (is_error(ret)) {
5029 return ret;
5030 }
5031
5032 memcpy(&hurb, buf_temp, sizeof(uint64_t));
5033 lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
5034 if (!lurb->target_urb_adr) {
5035 return -TARGET_EFAULT;
5036 }
5037 urb_hashtable_remove(lurb);
5038 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
5039 lurb->host_urb.buffer_length);
5040 lurb->target_buf_ptr = NULL;
5041
5042 /* restore the guest buffer pointer */
5043 lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
5044
5045 /* update the guest urb struct */
5046 argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
5047 if (!argptr) {
5048 g_free(lurb);
5049 return -TARGET_EFAULT;
5050 }
5051 thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
5052 unlock_user(argptr, lurb->target_urb_adr, target_size);
5053
5054 target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
5055 /* write back the urb handle */
5056 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5057 if (!argptr) {
5058 g_free(lurb);
5059 return -TARGET_EFAULT;
5060 }
5061
5062 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
5063 target_urb_adr = lurb->target_urb_adr;
5064 thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
5065 unlock_user(argptr, arg, target_size);
5066
5067 g_free(lurb);
5068 return ret;
5069}
5070
5071static abi_long
5072do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
5073 uint8_t *buf_temp __attribute__((unused)),
5074 int fd, int cmd, abi_long arg)
5075{
5076 struct live_urb *lurb;
5077
5078 /* map target address back to host URB with metadata. */
5079 lurb = urb_hashtable_lookup(arg);
5080 if (!lurb) {
5081 return -TARGET_EFAULT;
5082 }
5083 return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
5084}
5085
5086static abi_long
5087do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
5088 int fd, int cmd, abi_long arg)
5089{
5090 const argtype *arg_type = ie->arg_type;
5091 int target_size;
5092 abi_long ret;
5093 void *argptr;
5094 int rw_dir;
5095 struct live_urb *lurb;
5096
5097 /*
5098 * each submitted URB needs to map to a unique ID for the
5099 * kernel, and that unique ID needs to be a pointer to
5100 * host memory. hence, we need to malloc for each URB.
5101 * isochronous transfers have a variable length struct.
5102 */
5103 arg_type++;
5104 target_size = thunk_type_size(arg_type, THUNK_TARGET);
5105
5106 /* construct host copy of urb and metadata */
5107 lurb = g_try_malloc0(sizeof(struct live_urb));
5108 if (!lurb) {
5109 return -TARGET_ENOMEM;
5110 }
5111
5112 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5113 if (!argptr) {
5114 g_free(lurb);
5115 return -TARGET_EFAULT;
5116 }
5117 thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
5118 unlock_user(argptr, arg, 0);
5119
5120 lurb->target_urb_adr = arg;
5121 lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
5122
5123 /* buffer space used depends on endpoint type so lock the entire buffer */
5124 /* control type urbs should check the buffer contents for true direction */
5125 rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
5126 lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
5127 lurb->host_urb.buffer_length, 1);
5128 if (lurb->target_buf_ptr == NULL) {
5129 g_free(lurb);
5130 return -TARGET_EFAULT;
5131 }
5132
5133 /* update buffer pointer in host copy */
5134 lurb->host_urb.buffer = lurb->target_buf_ptr;
5135
5136 ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
5137 if (is_error(ret)) {
5138 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
5139 g_free(lurb);
5140 } else {
5141 urb_hashtable_insert(lurb);
5142 }
5143
5144 return ret;
5145}
5146#endif /* CONFIG_USBFS */
5147
Alexander Graf56e904e2012-01-31 18:42:06 +01005148static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005149 int cmd, abi_long arg)
Alexander Graf56e904e2012-01-31 18:42:06 +01005150{
5151 void *argptr;
5152 struct dm_ioctl *host_dm;
5153 abi_long guest_data;
5154 uint32_t guest_data_size;
5155 int target_size;
5156 const argtype *arg_type = ie->arg_type;
5157 abi_long ret;
5158 void *big_buf = NULL;
5159 char *host_data;
5160
5161 arg_type++;
5162 target_size = thunk_type_size(arg_type, 0);
5163 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5164 if (!argptr) {
5165 ret = -TARGET_EFAULT;
5166 goto out;
5167 }
5168 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5169 unlock_user(argptr, arg, 0);
5170
5171 /* buf_temp is too small, so fetch things into a bigger buffer */
5172 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
5173 memcpy(big_buf, buf_temp, target_size);
5174 buf_temp = big_buf;
5175 host_dm = big_buf;
5176
5177 guest_data = arg + host_dm->data_start;
5178 if ((guest_data - arg) < 0) {
Peter Maydellf9757b12016-07-12 13:02:14 +01005179 ret = -TARGET_EINVAL;
Alexander Graf56e904e2012-01-31 18:42:06 +01005180 goto out;
5181 }
5182 guest_data_size = host_dm->data_size - host_dm->data_start;
5183 host_data = (char*)host_dm + host_dm->data_start;
5184
5185 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
Peter Maydell32112152016-07-12 13:02:13 +01005186 if (!argptr) {
5187 ret = -TARGET_EFAULT;
5188 goto out;
5189 }
5190
Alexander Graf56e904e2012-01-31 18:42:06 +01005191 switch (ie->host_cmd) {
5192 case DM_REMOVE_ALL:
5193 case DM_LIST_DEVICES:
5194 case DM_DEV_CREATE:
5195 case DM_DEV_REMOVE:
5196 case DM_DEV_SUSPEND:
5197 case DM_DEV_STATUS:
5198 case DM_DEV_WAIT:
5199 case DM_TABLE_STATUS:
5200 case DM_TABLE_CLEAR:
5201 case DM_TABLE_DEPS:
5202 case DM_LIST_VERSIONS:
5203 /* no input data */
5204 break;
5205 case DM_DEV_RENAME:
5206 case DM_DEV_SET_GEOMETRY:
5207 /* data contains only strings */
5208 memcpy(host_data, argptr, guest_data_size);
5209 break;
5210 case DM_TARGET_MSG:
5211 memcpy(host_data, argptr, guest_data_size);
5212 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5213 break;
5214 case DM_TABLE_LOAD:
5215 {
5216 void *gspec = argptr;
5217 void *cur_data = host_data;
5218 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5219 int spec_size = thunk_type_size(arg_type, 0);
5220 int i;
5221
5222 for (i = 0; i < host_dm->target_count; i++) {
5223 struct dm_target_spec *spec = cur_data;
5224 uint32_t next;
5225 int slen;
5226
5227 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
5228 slen = strlen((char*)gspec + spec_size) + 1;
5229 next = spec->next;
5230 spec->next = sizeof(*spec) + slen;
5231 strcpy((char*)&spec[1], gspec + spec_size);
5232 gspec += next;
5233 cur_data += spec->next;
5234 }
5235 break;
5236 }
5237 default:
5238 ret = -TARGET_EINVAL;
Chen Gang Sdec04732015-01-25 08:00:42 +08005239 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005240 goto out;
5241 }
5242 unlock_user(argptr, guest_data, 0);
5243
Peter Maydell49ca6f32016-06-06 19:58:14 +01005244 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
Alexander Graf56e904e2012-01-31 18:42:06 +01005245 if (!is_error(ret)) {
5246 guest_data = arg + host_dm->data_start;
5247 guest_data_size = host_dm->data_size - host_dm->data_start;
5248 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
5249 switch (ie->host_cmd) {
5250 case DM_REMOVE_ALL:
5251 case DM_DEV_CREATE:
5252 case DM_DEV_REMOVE:
5253 case DM_DEV_RENAME:
5254 case DM_DEV_SUSPEND:
5255 case DM_DEV_STATUS:
5256 case DM_TABLE_LOAD:
5257 case DM_TABLE_CLEAR:
5258 case DM_TARGET_MSG:
5259 case DM_DEV_SET_GEOMETRY:
5260 /* no return data */
5261 break;
5262 case DM_LIST_DEVICES:
5263 {
5264 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
5265 uint32_t remaining_data = guest_data_size;
5266 void *cur_data = argptr;
5267 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
5268 int nl_size = 12; /* can't use thunk_size due to alignment */
5269
5270 while (1) {
5271 uint32_t next = nl->next;
5272 if (next) {
5273 nl->next = nl_size + (strlen(nl->name) + 1);
5274 }
5275 if (remaining_data < nl->next) {
5276 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5277 break;
5278 }
5279 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
5280 strcpy(cur_data + nl_size, nl->name);
5281 cur_data += nl->next;
5282 remaining_data -= nl->next;
5283 if (!next) {
5284 break;
5285 }
5286 nl = (void*)nl + next;
5287 }
5288 break;
5289 }
5290 case DM_DEV_WAIT:
5291 case DM_TABLE_STATUS:
5292 {
5293 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
5294 void *cur_data = argptr;
5295 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5296 int spec_size = thunk_type_size(arg_type, 0);
5297 int i;
5298
5299 for (i = 0; i < host_dm->target_count; i++) {
5300 uint32_t next = spec->next;
5301 int slen = strlen((char*)&spec[1]) + 1;
5302 spec->next = (cur_data - argptr) + spec_size + slen;
5303 if (guest_data_size < spec->next) {
5304 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5305 break;
5306 }
5307 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
5308 strcpy(cur_data + spec_size, (char*)&spec[1]);
5309 cur_data = argptr + spec->next;
5310 spec = (void*)host_dm + host_dm->data_start + next;
5311 }
5312 break;
5313 }
5314 case DM_TABLE_DEPS:
5315 {
5316 void *hdata = (void*)host_dm + host_dm->data_start;
5317 int count = *(uint32_t*)hdata;
5318 uint64_t *hdev = hdata + 8;
5319 uint64_t *gdev = argptr + 8;
5320 int i;
5321
5322 *(uint32_t*)argptr = tswap32(count);
5323 for (i = 0; i < count; i++) {
5324 *gdev = tswap64(*hdev);
5325 gdev++;
5326 hdev++;
5327 }
5328 break;
5329 }
5330 case DM_LIST_VERSIONS:
5331 {
5332 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
5333 uint32_t remaining_data = guest_data_size;
5334 void *cur_data = argptr;
5335 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
5336 int vers_size = thunk_type_size(arg_type, 0);
5337
5338 while (1) {
5339 uint32_t next = vers->next;
5340 if (next) {
5341 vers->next = vers_size + (strlen(vers->name) + 1);
5342 }
5343 if (remaining_data < vers->next) {
5344 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5345 break;
5346 }
5347 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
5348 strcpy(cur_data + vers_size, vers->name);
5349 cur_data += vers->next;
5350 remaining_data -= vers->next;
5351 if (!next) {
5352 break;
5353 }
5354 vers = (void*)vers + next;
5355 }
5356 break;
5357 }
5358 default:
Chen Gang Sdec04732015-01-25 08:00:42 +08005359 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005360 ret = -TARGET_EINVAL;
5361 goto out;
5362 }
5363 unlock_user(argptr, guest_data, guest_data_size);
5364
5365 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5366 if (!argptr) {
5367 ret = -TARGET_EFAULT;
5368 goto out;
5369 }
5370 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5371 unlock_user(argptr, arg, target_size);
5372 }
5373out:
Stefan Weilad11ad72012-09-04 22:14:19 +02005374 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01005375 return ret;
5376}
5377
Alexander Grafa59b5e32014-08-22 13:15:50 +02005378static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005379 int cmd, abi_long arg)
Alexander Grafa59b5e32014-08-22 13:15:50 +02005380{
5381 void *argptr;
5382 int target_size;
5383 const argtype *arg_type = ie->arg_type;
5384 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5385 abi_long ret;
5386
5387 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5388 struct blkpg_partition host_part;
5389
5390 /* Read and convert blkpg */
5391 arg_type++;
5392 target_size = thunk_type_size(arg_type, 0);
5393 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5394 if (!argptr) {
5395 ret = -TARGET_EFAULT;
5396 goto out;
5397 }
5398 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5399 unlock_user(argptr, arg, 0);
5400
5401 switch (host_blkpg->op) {
5402 case BLKPG_ADD_PARTITION:
5403 case BLKPG_DEL_PARTITION:
5404 /* payload is struct blkpg_partition */
5405 break;
5406 default:
5407 /* Unknown opcode */
5408 ret = -TARGET_EINVAL;
5409 goto out;
5410 }
5411
5412 /* Read and convert blkpg->data */
5413 arg = (abi_long)(uintptr_t)host_blkpg->data;
5414 target_size = thunk_type_size(part_arg_type, 0);
5415 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5416 if (!argptr) {
5417 ret = -TARGET_EFAULT;
5418 goto out;
5419 }
5420 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
5421 unlock_user(argptr, arg, 0);
5422
5423 /* Swizzle the data pointer to our local copy and call! */
5424 host_blkpg->data = &host_part;
Peter Maydell49ca6f32016-06-06 19:58:14 +01005425 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
Alexander Grafa59b5e32014-08-22 13:15:50 +02005426
5427out:
5428 return ret;
5429}
5430
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005431static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005432 int fd, int cmd, abi_long arg)
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005433{
5434 const argtype *arg_type = ie->arg_type;
5435 const StructEntry *se;
5436 const argtype *field_types;
5437 const int *dst_offsets, *src_offsets;
5438 int target_size;
5439 void *argptr;
Marc-André Lureaub78c5222019-03-05 16:15:00 +01005440 abi_ulong *target_rt_dev_ptr = NULL;
5441 unsigned long *host_rt_dev_ptr = NULL;
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005442 abi_long ret;
5443 int i;
5444
5445 assert(ie->access == IOC_W);
5446 assert(*arg_type == TYPE_PTR);
5447 arg_type++;
5448 assert(*arg_type == TYPE_STRUCT);
5449 target_size = thunk_type_size(arg_type, 0);
5450 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5451 if (!argptr) {
5452 return -TARGET_EFAULT;
5453 }
5454 arg_type++;
5455 assert(*arg_type == (int)STRUCT_rtentry);
5456 se = struct_entries + *arg_type++;
5457 assert(se->convert[0] == NULL);
5458 /* convert struct here to be able to catch rt_dev string */
5459 field_types = se->field_types;
5460 dst_offsets = se->field_offsets[THUNK_HOST];
5461 src_offsets = se->field_offsets[THUNK_TARGET];
5462 for (i = 0; i < se->nb_fields; i++) {
5463 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
5464 assert(*field_types == TYPE_PTRVOID);
5465 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
5466 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
5467 if (*target_rt_dev_ptr != 0) {
5468 *host_rt_dev_ptr = (unsigned long)lock_user_string(
5469 tswapal(*target_rt_dev_ptr));
5470 if (!*host_rt_dev_ptr) {
5471 unlock_user(argptr, arg, 0);
5472 return -TARGET_EFAULT;
5473 }
5474 } else {
5475 *host_rt_dev_ptr = 0;
5476 }
5477 field_types++;
5478 continue;
5479 }
5480 field_types = thunk_convert(buf_temp + dst_offsets[i],
5481 argptr + src_offsets[i],
5482 field_types, THUNK_HOST);
5483 }
5484 unlock_user(argptr, arg, 0);
5485
Peter Maydell49ca6f32016-06-06 19:58:14 +01005486 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
Marc-André Lureaub78c5222019-03-05 16:15:00 +01005487
5488 assert(host_rt_dev_ptr != NULL);
5489 assert(target_rt_dev_ptr != NULL);
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005490 if (*host_rt_dev_ptr != 0) {
5491 unlock_user((void *)*host_rt_dev_ptr,
5492 *target_rt_dev_ptr, 0);
5493 }
5494 return ret;
5495}
5496
Paul Burtonca56f5b2014-06-22 11:25:47 +01005497static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005498 int fd, int cmd, abi_long arg)
Paul Burtonca56f5b2014-06-22 11:25:47 +01005499{
5500 int sig = target_to_host_signal(arg);
Peter Maydell49ca6f32016-06-06 19:58:14 +01005501 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
Paul Burtonca56f5b2014-06-22 11:25:47 +01005502}
5503
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02005504static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
5505 int fd, int cmd, abi_long arg)
5506{
5507 struct timeval tv;
5508 abi_long ret;
5509
5510 ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
5511 if (is_error(ret)) {
5512 return ret;
5513 }
5514
5515 if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
5516 if (copy_to_user_timeval(arg, &tv)) {
5517 return -TARGET_EFAULT;
5518 }
5519 } else {
5520 if (copy_to_user_timeval64(arg, &tv)) {
5521 return -TARGET_EFAULT;
5522 }
5523 }
5524
5525 return ret;
5526}
5527
5528static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
5529 int fd, int cmd, abi_long arg)
5530{
5531 struct timespec ts;
5532 abi_long ret;
5533
5534 ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
5535 if (is_error(ret)) {
5536 return ret;
5537 }
5538
5539 if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
5540 if (host_to_target_timespec(arg, &ts)) {
5541 return -TARGET_EFAULT;
5542 }
5543 } else{
5544 if (host_to_target_timespec64(arg, &ts)) {
5545 return -TARGET_EFAULT;
5546 }
5547 }
5548
5549 return ret;
5550}
5551
Andreas Schwab2b74f622018-01-29 11:47:06 +01005552#ifdef TIOCGPTPEER
5553static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
5554 int fd, int cmd, abi_long arg)
5555{
5556 int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
5557 return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
5558}
5559#endif
5560
Chen Gange865b972020-06-05 09:32:21 +08005561#ifdef HAVE_DRM_H
5562
5563static void unlock_drm_version(struct drm_version *host_ver,
5564 struct target_drm_version *target_ver,
5565 bool copy)
5566{
5567 unlock_user(host_ver->name, target_ver->name,
5568 copy ? host_ver->name_len : 0);
5569 unlock_user(host_ver->date, target_ver->date,
5570 copy ? host_ver->date_len : 0);
5571 unlock_user(host_ver->desc, target_ver->desc,
5572 copy ? host_ver->desc_len : 0);
5573}
5574
5575static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
5576 struct target_drm_version *target_ver)
5577{
5578 memset(host_ver, 0, sizeof(*host_ver));
5579
5580 __get_user(host_ver->name_len, &target_ver->name_len);
5581 if (host_ver->name_len) {
5582 host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
5583 target_ver->name_len, 0);
5584 if (!host_ver->name) {
5585 return -EFAULT;
5586 }
5587 }
5588
5589 __get_user(host_ver->date_len, &target_ver->date_len);
5590 if (host_ver->date_len) {
5591 host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
5592 target_ver->date_len, 0);
5593 if (!host_ver->date) {
5594 goto err;
5595 }
5596 }
5597
5598 __get_user(host_ver->desc_len, &target_ver->desc_len);
5599 if (host_ver->desc_len) {
5600 host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
5601 target_ver->desc_len, 0);
5602 if (!host_ver->desc) {
5603 goto err;
5604 }
5605 }
5606
5607 return 0;
5608err:
5609 unlock_drm_version(host_ver, target_ver, false);
5610 return -EFAULT;
5611}
5612
5613static inline void host_to_target_drmversion(
5614 struct target_drm_version *target_ver,
5615 struct drm_version *host_ver)
5616{
5617 __put_user(host_ver->version_major, &target_ver->version_major);
5618 __put_user(host_ver->version_minor, &target_ver->version_minor);
5619 __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
5620 __put_user(host_ver->name_len, &target_ver->name_len);
5621 __put_user(host_ver->date_len, &target_ver->date_len);
5622 __put_user(host_ver->desc_len, &target_ver->desc_len);
5623 unlock_drm_version(host_ver, target_ver, true);
5624}
5625
5626static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
5627 int fd, int cmd, abi_long arg)
5628{
5629 struct drm_version *ver;
5630 struct target_drm_version *target_ver;
5631 abi_long ret;
5632
5633 switch (ie->host_cmd) {
5634 case DRM_IOCTL_VERSION:
5635 if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
5636 return -TARGET_EFAULT;
5637 }
5638 ver = (struct drm_version *)buf_temp;
5639 ret = target_to_host_drmversion(ver, target_ver);
5640 if (!is_error(ret)) {
5641 ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
5642 if (is_error(ret)) {
5643 unlock_drm_version(ver, target_ver, false);
5644 } else {
5645 host_to_target_drmversion(target_ver, ver);
5646 }
5647 }
5648 unlock_user_struct(target_ver, arg, 0);
5649 return ret;
5650 }
5651 return -TARGET_ENOSYS;
5652}
5653
Chen Gang913b03c2020-08-02 21:39:38 +08005654static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
5655 struct drm_i915_getparam *gparam,
5656 int fd, abi_long arg)
5657{
5658 abi_long ret;
5659 int value;
5660 struct target_drm_i915_getparam *target_gparam;
5661
5662 if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
5663 return -TARGET_EFAULT;
5664 }
5665
5666 __get_user(gparam->param, &target_gparam->param);
5667 gparam->value = &value;
5668 ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
5669 put_user_s32(value, target_gparam->value);
5670
5671 unlock_user_struct(target_gparam, arg, 0);
5672 return ret;
5673}
5674
5675static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
5676 int fd, int cmd, abi_long arg)
5677{
5678 switch (ie->host_cmd) {
5679 case DRM_IOCTL_I915_GETPARAM:
5680 return do_ioctl_drm_i915_getparam(ie,
5681 (struct drm_i915_getparam *)buf_temp,
5682 fd, arg);
5683 default:
5684 return -TARGET_ENOSYS;
5685 }
5686}
5687
Chen Gange865b972020-06-05 09:32:21 +08005688#endif
5689
Shu-Chun Weng6addf062020-09-28 18:48:01 -07005690static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
5691 int fd, int cmd, abi_long arg)
5692{
5693 struct tun_filter *filter = (struct tun_filter *)buf_temp;
5694 struct tun_filter *target_filter;
5695 char *target_addr;
5696
5697 assert(ie->access == IOC_W);
5698
5699 target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
5700 if (!target_filter) {
5701 return -TARGET_EFAULT;
5702 }
5703 filter->flags = tswap16(target_filter->flags);
5704 filter->count = tswap16(target_filter->count);
5705 unlock_user(target_filter, arg, 0);
5706
5707 if (filter->count) {
5708 if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
5709 MAX_STRUCT_SIZE) {
5710 return -TARGET_EFAULT;
5711 }
5712
5713 target_addr = lock_user(VERIFY_READ,
5714 arg + offsetof(struct tun_filter, addr),
5715 filter->count * ETH_ALEN, 1);
5716 if (!target_addr) {
5717 return -TARGET_EFAULT;
5718 }
5719 memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
5720 unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
5721 }
5722
5723 return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
5724}
5725
Filip Bozuta79482e592020-06-19 14:47:27 +02005726IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00005727#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00005728 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5729#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5730 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
Helge Deller8cb762f2016-12-06 16:24:03 +01005731#define IOCTL_IGNORE(cmd) \
5732 { TARGET_ ## cmd, 0, #cmd },
bellard31e31b82003-02-18 22:55:36 +00005733#include "ioctls.h"
5734 { 0, 0, },
5735};
5736
pbrook53a59602006-03-25 19:31:22 +00005737/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00005738/* do_ioctl() Must return target values and target errnos. */
Laurent Vivier45c874e2015-06-16 00:35:28 +02005739static abi_long do_ioctl(int fd, int cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00005740{
5741 const IOCTLEntry *ie;
5742 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00005743 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005744 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00005745 int target_size;
5746 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00005747
5748 ie = ioctl_entries;
5749 for(;;) {
5750 if (ie->target_cmd == 0) {
Josh Kunz39be5352020-02-03 18:54:13 -08005751 qemu_log_mask(
5752 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00005753 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005754 }
5755 if (ie->target_cmd == cmd)
5756 break;
5757 ie++;
5758 }
5759 arg_type = ie->arg_type;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00005760 if (ie->do_ioctl) {
5761 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
Helge Deller8cb762f2016-12-06 16:24:03 +01005762 } else if (!ie->host_cmd) {
5763 /* Some architectures define BSD ioctls in their headers
5764 that are not implemented in Linux. */
5765 return -TARGET_ENOSYS;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00005766 }
5767
bellard31e31b82003-02-18 22:55:36 +00005768 switch(arg_type[0]) {
5769 case TYPE_NULL:
5770 /* no argument */
Peter Maydell49ca6f32016-06-06 19:58:14 +01005771 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
bellard31e31b82003-02-18 22:55:36 +00005772 break;
5773 case TYPE_PTRVOID:
5774 case TYPE_INT:
Filip Bozutac858e532020-01-15 20:36:47 +01005775 case TYPE_LONG:
5776 case TYPE_ULONG:
Peter Maydell49ca6f32016-06-06 19:58:14 +01005777 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
bellard31e31b82003-02-18 22:55:36 +00005778 break;
5779 case TYPE_PTR:
5780 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00005781 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00005782 switch(ie->access) {
5783 case IOC_R:
Peter Maydell49ca6f32016-06-06 19:58:14 +01005784 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
bellard31e31b82003-02-18 22:55:36 +00005785 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005786 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5787 if (!argptr)
5788 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005789 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5790 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00005791 }
5792 break;
5793 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00005794 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5795 if (!argptr)
5796 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005797 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5798 unlock_user(argptr, arg, 0);
Peter Maydell49ca6f32016-06-06 19:58:14 +01005799 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
bellard31e31b82003-02-18 22:55:36 +00005800 break;
5801 default:
5802 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00005803 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5804 if (!argptr)
5805 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005806 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5807 unlock_user(argptr, arg, 0);
Peter Maydell49ca6f32016-06-06 19:58:14 +01005808 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
bellard31e31b82003-02-18 22:55:36 +00005809 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005810 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5811 if (!argptr)
5812 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005813 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5814 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00005815 }
5816 break;
5817 }
5818 break;
5819 default:
Josh Kunz39be5352020-02-03 18:54:13 -08005820 qemu_log_mask(LOG_UNIMP,
5821 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5822 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00005823 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005824 break;
5825 }
5826 return ret;
5827}
5828
blueswir1b39bc502008-10-05 10:51:10 +00005829static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00005830 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
5831 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
5832 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
5833 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
5834 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
5835 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
5836 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
5837 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
5838 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
5839 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
5840 { TARGET_IXON, TARGET_IXON, IXON, IXON },
5841 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
5842 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
5843 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
Filip Bozutac218b4e2020-07-23 23:02:32 +02005844 { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
bellard31e31b82003-02-18 22:55:36 +00005845 { 0, 0, 0, 0 }
5846};
5847
blueswir1b39bc502008-10-05 10:51:10 +00005848static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00005849 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
5850 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
5851 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
5852 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
5853 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
5854 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
5855 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
5856 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
5857 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
5858 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
5859 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
5860 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
5861 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
5862 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
5863 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
5864 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
5865 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
5866 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
5867 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
5868 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
5869 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
5870 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
5871 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
5872 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
5873 { 0, 0, 0, 0 }
5874};
5875
blueswir1b39bc502008-10-05 10:51:10 +00005876static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00005877 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
5878 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
5879 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
5880 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
5881 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
5882 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
5883 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
5884 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
5885 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
5886 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
5887 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
5888 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
5889 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
5890 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
5891 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
5892 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
5893 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
5894 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
5895 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
5896 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
5897 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
5898 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
5899 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
5900 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
5901 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
5902 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
5903 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
5904 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
5905 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
5906 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
5907 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
5908 { 0, 0, 0, 0 }
5909};
5910
blueswir1b39bc502008-10-05 10:51:10 +00005911static const bitmask_transtbl lflag_tbl[] = {
Filip Bozutac218b4e2020-07-23 23:02:32 +02005912 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
5913 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
5914 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
5915 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
5916 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
5917 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
5918 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
5919 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
5920 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
5921 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
5922 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
5923 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
5924 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
5925 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
5926 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
5927 { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
5928 { 0, 0, 0, 0 }
bellard31e31b82003-02-18 22:55:36 +00005929};
5930
5931static void target_to_host_termios (void *dst, const void *src)
5932{
5933 struct host_termios *host = dst;
5934 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00005935
ths5fafdf22007-09-16 21:08:06 +00005936 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00005937 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00005938 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00005939 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00005940 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00005941 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00005942 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00005943 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
5944 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00005945
Arnaud Patard44607122009-04-21 17:39:08 +03005946 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00005947 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
5948 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00005949 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00005950 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00005951 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00005952 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00005953 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00005954 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00005955 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00005956 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5957 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00005958 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5959 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5960 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5961 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5962 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00005963 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00005964}
ths3b46e622007-09-17 08:09:54 +00005965
bellard31e31b82003-02-18 22:55:36 +00005966static void host_to_target_termios (void *dst, const void *src)
5967{
5968 struct target_termios *target = dst;
5969 const struct host_termios *host = src;
5970
ths5fafdf22007-09-16 21:08:06 +00005971 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00005972 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00005973 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00005974 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00005975 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00005976 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00005977 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00005978 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5979 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00005980
Arnaud Patard44607122009-04-21 17:39:08 +03005981 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00005982 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5983 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5984 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5985 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5986 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5987 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5988 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5989 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5990 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5991 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5992 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5993 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5994 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5995 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5996 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5997 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5998 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5999}
6000
blueswir18e853dc2008-10-05 10:49:32 +00006001static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00006002 .convert = { host_to_target_termios, target_to_host_termios },
6003 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
6004 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
Filip Bozuta888468d2020-07-23 23:02:33 +02006005 .print = print_termios,
bellard31e31b82003-02-18 22:55:36 +00006006};
6007
Philippe Mathieu-Daudé180d4ef2021-05-17 07:52:43 +02006008static const bitmask_transtbl mmap_flags_tbl[] = {
Helge Deller541e1692017-10-31 13:53:58 +01006009 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
6010 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
6011 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
6012 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
6013 MAP_ANONYMOUS, MAP_ANONYMOUS },
6014 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
6015 MAP_GROWSDOWN, MAP_GROWSDOWN },
6016 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
6017 MAP_DENYWRITE, MAP_DENYWRITE },
6018 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
6019 MAP_EXECUTABLE, MAP_EXECUTABLE },
6020 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
6021 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
6022 MAP_NORESERVE, MAP_NORESERVE },
6023 { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
6024 /* MAP_STACK had been ignored by the kernel for quite some time.
6025 Recognize it for the target insofar as we do not want to pass
6026 it through to the host. */
6027 { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
6028 { 0, 0, 0, 0 }
bellard5286db72003-06-05 00:57:30 +00006029};
6030
Laurent Vivier9d12f792020-03-10 11:33:55 +01006031/*
6032 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
6033 * TARGET_I386 is defined if TARGET_X86_64 is defined
6034 */
bellard2ab83ea2003-06-15 19:56:46 +00006035#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00006036
6037/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00006038static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00006039
bellard03acab62007-11-11 14:57:14 +00006040static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00006041{
6042 int size;
pbrook53a59602006-03-25 19:31:22 +00006043 void *p;
bellard6dbad632003-03-16 18:05:05 +00006044
6045 if (!ldt_table)
6046 return 0;
6047 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
6048 if (size > bytecount)
6049 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00006050 p = lock_user(VERIFY_WRITE, ptr, size, 0);
6051 if (!p)
bellard03acab62007-11-11 14:57:14 +00006052 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00006053 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00006054 memcpy(p, ldt_table, size);
6055 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00006056 return size;
6057}
6058
6059/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00006060static abi_long write_ldt(CPUX86State *env,
6061 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00006062{
6063 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00006064 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00006065 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00006066 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00006067 uint32_t *lp, entry_1, entry_2;
6068
6069 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00006070 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00006071 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00006072 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00006073 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006074 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00006075 ldt_info.limit = tswap32(target_ldt_info->limit);
6076 ldt_info.flags = tswap32(target_ldt_info->flags);
6077 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00006078
bellard6dbad632003-03-16 18:05:05 +00006079 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00006080 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00006081 seg_32bit = ldt_info.flags & 1;
6082 contents = (ldt_info.flags >> 1) & 3;
6083 read_exec_only = (ldt_info.flags >> 3) & 1;
6084 limit_in_pages = (ldt_info.flags >> 4) & 1;
6085 seg_not_present = (ldt_info.flags >> 5) & 1;
6086 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00006087#ifdef TARGET_ABI32
6088 lm = 0;
6089#else
6090 lm = (ldt_info.flags >> 7) & 1;
6091#endif
bellard6dbad632003-03-16 18:05:05 +00006092 if (contents == 3) {
6093 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00006094 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00006095 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00006096 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00006097 }
6098 /* allocate the LDT */
6099 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00006100 env->ldt.base = target_mmap(0,
6101 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
6102 PROT_READ|PROT_WRITE,
6103 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
6104 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00006105 return -TARGET_ENOMEM;
Richard Henderson3e8f1622021-02-12 10:48:43 -08006106 memset(g2h_untagged(env->ldt.base), 0,
balroge4415702008-11-10 02:55:33 +00006107 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00006108 env->ldt.limit = 0xffff;
Richard Henderson3e8f1622021-02-12 10:48:43 -08006109 ldt_table = g2h_untagged(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00006110 }
6111
6112 /* NOTE: same code as Linux kernel */
6113 /* Allow LDTs to be cleared by the user. */
6114 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6115 if (oldmode ||
6116 (contents == 0 &&
6117 read_exec_only == 1 &&
6118 seg_32bit == 0 &&
6119 limit_in_pages == 0 &&
6120 seg_not_present == 1 &&
6121 useable == 0 )) {
6122 entry_1 = 0;
6123 entry_2 = 0;
6124 goto install;
6125 }
6126 }
ths3b46e622007-09-17 08:09:54 +00006127
bellard6dbad632003-03-16 18:05:05 +00006128 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6129 (ldt_info.limit & 0x0ffff);
6130 entry_2 = (ldt_info.base_addr & 0xff000000) |
6131 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6132 (ldt_info.limit & 0xf0000) |
6133 ((read_exec_only ^ 1) << 9) |
6134 (contents << 10) |
6135 ((seg_not_present ^ 1) << 15) |
6136 (seg_32bit << 22) |
6137 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00006138 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00006139 0x7000;
6140 if (!oldmode)
6141 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00006142
bellard6dbad632003-03-16 18:05:05 +00006143 /* Install the new entry ... */
6144install:
6145 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
6146 lp[0] = tswap32(entry_1);
6147 lp[1] = tswap32(entry_2);
6148 return 0;
6149}
6150
6151/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00006152static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
6153 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00006154{
bellard03acab62007-11-11 14:57:14 +00006155 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00006156
bellard6dbad632003-03-16 18:05:05 +00006157 switch (func) {
6158 case 0:
6159 ret = read_ldt(ptr, bytecount);
6160 break;
6161 case 1:
6162 ret = write_ldt(env, ptr, bytecount, 1);
6163 break;
6164 case 0x11:
6165 ret = write_ldt(env, ptr, bytecount, 0);
6166 break;
bellard03acab62007-11-11 14:57:14 +00006167 default:
6168 ret = -TARGET_ENOSYS;
6169 break;
bellard6dbad632003-03-16 18:05:05 +00006170 }
6171 return ret;
6172}
bellard1b6b0292003-03-22 17:31:38 +00006173
Laurent Vivier9d12f792020-03-10 11:33:55 +01006174#if defined(TARGET_ABI32)
Alexander Grafbc22eb42013-07-16 18:44:58 +01006175abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00006176{
Richard Henderson3e8f1622021-02-12 10:48:43 -08006177 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
bellard8d18e892007-11-14 15:18:40 +00006178 struct target_modify_ldt_ldt_s ldt_info;
6179 struct target_modify_ldt_ldt_s *target_ldt_info;
6180 int seg_32bit, contents, read_exec_only, limit_in_pages;
6181 int seg_not_present, useable, lm;
6182 uint32_t *lp, entry_1, entry_2;
6183 int i;
6184
6185 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6186 if (!target_ldt_info)
6187 return -TARGET_EFAULT;
6188 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006189 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00006190 ldt_info.limit = tswap32(target_ldt_info->limit);
6191 ldt_info.flags = tswap32(target_ldt_info->flags);
6192 if (ldt_info.entry_number == -1) {
6193 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
6194 if (gdt_table[i] == 0) {
6195 ldt_info.entry_number = i;
6196 target_ldt_info->entry_number = tswap32(i);
6197 break;
6198 }
6199 }
6200 }
6201 unlock_user_struct(target_ldt_info, ptr, 1);
6202
6203 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
6204 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
6205 return -TARGET_EINVAL;
6206 seg_32bit = ldt_info.flags & 1;
6207 contents = (ldt_info.flags >> 1) & 3;
6208 read_exec_only = (ldt_info.flags >> 3) & 1;
6209 limit_in_pages = (ldt_info.flags >> 4) & 1;
6210 seg_not_present = (ldt_info.flags >> 5) & 1;
6211 useable = (ldt_info.flags >> 6) & 1;
6212#ifdef TARGET_ABI32
6213 lm = 0;
6214#else
6215 lm = (ldt_info.flags >> 7) & 1;
6216#endif
6217
6218 if (contents == 3) {
6219 if (seg_not_present == 0)
6220 return -TARGET_EINVAL;
6221 }
6222
6223 /* NOTE: same code as Linux kernel */
6224 /* Allow LDTs to be cleared by the user. */
6225 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6226 if ((contents == 0 &&
6227 read_exec_only == 1 &&
6228 seg_32bit == 0 &&
6229 limit_in_pages == 0 &&
6230 seg_not_present == 1 &&
6231 useable == 0 )) {
6232 entry_1 = 0;
6233 entry_2 = 0;
6234 goto install;
6235 }
6236 }
6237
6238 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6239 (ldt_info.limit & 0x0ffff);
6240 entry_2 = (ldt_info.base_addr & 0xff000000) |
6241 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6242 (ldt_info.limit & 0xf0000) |
6243 ((read_exec_only ^ 1) << 9) |
6244 (contents << 10) |
6245 ((seg_not_present ^ 1) << 15) |
6246 (seg_32bit << 22) |
6247 (limit_in_pages << 23) |
6248 (useable << 20) |
6249 (lm << 21) |
6250 0x7000;
6251
6252 /* Install the new entry ... */
6253install:
6254 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
6255 lp[0] = tswap32(entry_1);
6256 lp[1] = tswap32(entry_2);
6257 return 0;
6258}
6259
blueswir18fcd3692008-08-17 20:26:25 +00006260static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00006261{
6262 struct target_modify_ldt_ldt_s *target_ldt_info;
Richard Henderson3e8f1622021-02-12 10:48:43 -08006263 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
bellard8d18e892007-11-14 15:18:40 +00006264 uint32_t base_addr, limit, flags;
6265 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
6266 int seg_not_present, useable, lm;
6267 uint32_t *lp, entry_1, entry_2;
6268
6269 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6270 if (!target_ldt_info)
6271 return -TARGET_EFAULT;
6272 idx = tswap32(target_ldt_info->entry_number);
6273 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
6274 idx > TARGET_GDT_ENTRY_TLS_MAX) {
6275 unlock_user_struct(target_ldt_info, ptr, 1);
6276 return -TARGET_EINVAL;
6277 }
6278 lp = (uint32_t *)(gdt_table + idx);
6279 entry_1 = tswap32(lp[0]);
6280 entry_2 = tswap32(lp[1]);
6281
6282 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
6283 contents = (entry_2 >> 10) & 3;
6284 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
6285 seg_32bit = (entry_2 >> 22) & 1;
6286 limit_in_pages = (entry_2 >> 23) & 1;
6287 useable = (entry_2 >> 20) & 1;
6288#ifdef TARGET_ABI32
6289 lm = 0;
6290#else
6291 lm = (entry_2 >> 21) & 1;
6292#endif
6293 flags = (seg_32bit << 0) | (contents << 1) |
6294 (read_exec_only << 3) | (limit_in_pages << 4) |
6295 (seg_not_present << 5) | (useable << 6) | (lm << 7);
6296 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
6297 base_addr = (entry_1 >> 16) |
6298 (entry_2 & 0xff000000) |
6299 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006300 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00006301 target_ldt_info->limit = tswap32(limit);
6302 target_ldt_info->flags = tswap32(flags);
6303 unlock_user_struct(target_ldt_info, ptr, 1);
6304 return 0;
6305}
6306
Laurent Vivier9d12f792020-03-10 11:33:55 +01006307abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6308{
Helge Deller538fabc2020-04-25 00:00:33 +02006309 return -TARGET_ENOSYS;
Laurent Vivier9d12f792020-03-10 11:33:55 +01006310}
6311#else
Peter Maydell2667e712013-07-16 18:44:59 +01006312abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00006313{
Juan Quintela1add8692011-06-16 17:37:09 +01006314 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00006315 abi_ulong val;
6316 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01006317
bellardd2fd1af2007-11-14 18:08:56 +00006318 switch(code) {
6319 case TARGET_ARCH_SET_GS:
6320 case TARGET_ARCH_SET_FS:
6321 if (code == TARGET_ARCH_SET_GS)
6322 idx = R_GS;
6323 else
6324 idx = R_FS;
6325 cpu_x86_load_seg(env, idx, 0);
6326 env->segs[idx].base = addr;
6327 break;
6328 case TARGET_ARCH_GET_GS:
6329 case TARGET_ARCH_GET_FS:
6330 if (code == TARGET_ARCH_GET_GS)
6331 idx = R_GS;
6332 else
6333 idx = R_FS;
6334 val = env->segs[idx].base;
6335 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01006336 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00006337 break;
6338 default:
6339 ret = -TARGET_EINVAL;
6340 break;
6341 }
Juan Quintela1add8692011-06-16 17:37:09 +01006342 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00006343}
Laurent Vivier9d12f792020-03-10 11:33:55 +01006344#endif /* defined(TARGET_ABI32 */
bellard2ab83ea2003-06-15 19:56:46 +00006345#endif /* defined(TARGET_I386) */
6346
Richard Henderson87e9bf22021-12-27 07:01:22 -08006347/*
6348 * These constants are generic. Supply any that are missing from the host.
6349 */
6350#ifndef PR_SET_NAME
6351# define PR_SET_NAME 15
6352# define PR_GET_NAME 16
6353#endif
6354#ifndef PR_SET_FP_MODE
6355# define PR_SET_FP_MODE 45
6356# define PR_GET_FP_MODE 46
6357# define PR_FP_MODE_FR (1 << 0)
6358# define PR_FP_MODE_FRE (1 << 1)
6359#endif
6360#ifndef PR_SVE_SET_VL
6361# define PR_SVE_SET_VL 50
6362# define PR_SVE_GET_VL 51
6363# define PR_SVE_VL_LEN_MASK 0xffff
6364# define PR_SVE_VL_INHERIT (1 << 17)
6365#endif
6366#ifndef PR_PAC_RESET_KEYS
6367# define PR_PAC_RESET_KEYS 54
6368# define PR_PAC_APIAKEY (1 << 0)
6369# define PR_PAC_APIBKEY (1 << 1)
6370# define PR_PAC_APDAKEY (1 << 2)
6371# define PR_PAC_APDBKEY (1 << 3)
6372# define PR_PAC_APGAKEY (1 << 4)
6373#endif
6374#ifndef PR_SET_TAGGED_ADDR_CTRL
6375# define PR_SET_TAGGED_ADDR_CTRL 55
6376# define PR_GET_TAGGED_ADDR_CTRL 56
6377# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6378#endif
6379#ifndef PR_MTE_TCF_SHIFT
6380# define PR_MTE_TCF_SHIFT 1
6381# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6382# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6383# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6384# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6385# define PR_MTE_TAG_SHIFT 3
6386# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6387#endif
Richard Henderson220717a2021-12-27 07:01:23 -08006388#ifndef PR_SET_IO_FLUSHER
6389# define PR_SET_IO_FLUSHER 57
6390# define PR_GET_IO_FLUSHER 58
6391#endif
6392#ifndef PR_SET_SYSCALL_USER_DISPATCH
6393# define PR_SET_SYSCALL_USER_DISPATCH 59
6394#endif
Richard Henderson87e9bf22021-12-27 07:01:22 -08006395
6396#include "target_prctl.h"
6397
6398static abi_long do_prctl_inval0(CPUArchState *env)
6399{
6400 return -TARGET_EINVAL;
6401}
6402
6403static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
6404{
6405 return -TARGET_EINVAL;
6406}
6407
6408#ifndef do_prctl_get_fp_mode
6409#define do_prctl_get_fp_mode do_prctl_inval0
6410#endif
6411#ifndef do_prctl_set_fp_mode
6412#define do_prctl_set_fp_mode do_prctl_inval1
6413#endif
6414#ifndef do_prctl_get_vl
6415#define do_prctl_get_vl do_prctl_inval0
6416#endif
6417#ifndef do_prctl_set_vl
6418#define do_prctl_set_vl do_prctl_inval1
6419#endif
6420#ifndef do_prctl_reset_keys
6421#define do_prctl_reset_keys do_prctl_inval1
6422#endif
6423#ifndef do_prctl_set_tagged_addr_ctrl
6424#define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6425#endif
6426#ifndef do_prctl_get_tagged_addr_ctrl
6427#define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6428#endif
Richard Henderson6e8dcac2021-12-27 07:01:24 -08006429#ifndef do_prctl_get_unalign
6430#define do_prctl_get_unalign do_prctl_inval1
6431#endif
6432#ifndef do_prctl_set_unalign
6433#define do_prctl_set_unalign do_prctl_inval1
6434#endif
Richard Henderson87e9bf22021-12-27 07:01:22 -08006435
6436static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
6437 abi_long arg3, abi_long arg4, abi_long arg5)
6438{
6439 abi_long ret;
6440
6441 switch (option) {
6442 case PR_GET_PDEATHSIG:
6443 {
6444 int deathsig;
6445 ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
6446 arg3, arg4, arg5));
Richard Henderson1edebb32022-01-06 14:57:36 -08006447 if (!is_error(ret) &&
6448 put_user_s32(host_to_target_signal(deathsig), arg2)) {
Richard Henderson87e9bf22021-12-27 07:01:22 -08006449 return -TARGET_EFAULT;
6450 }
6451 return ret;
6452 }
Richard Hendersonf746c652022-01-06 14:57:37 -08006453 case PR_SET_PDEATHSIG:
6454 return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
6455 arg3, arg4, arg5));
Richard Henderson87e9bf22021-12-27 07:01:22 -08006456 case PR_GET_NAME:
6457 {
6458 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
6459 if (!name) {
6460 return -TARGET_EFAULT;
6461 }
6462 ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
6463 arg3, arg4, arg5));
6464 unlock_user(name, arg2, 16);
6465 return ret;
6466 }
6467 case PR_SET_NAME:
6468 {
6469 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
6470 if (!name) {
6471 return -TARGET_EFAULT;
6472 }
6473 ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
6474 arg3, arg4, arg5));
6475 unlock_user(name, arg2, 0);
6476 return ret;
6477 }
6478 case PR_GET_FP_MODE:
6479 return do_prctl_get_fp_mode(env);
6480 case PR_SET_FP_MODE:
6481 return do_prctl_set_fp_mode(env, arg2);
6482 case PR_SVE_GET_VL:
6483 return do_prctl_get_vl(env);
6484 case PR_SVE_SET_VL:
6485 return do_prctl_set_vl(env, arg2);
6486 case PR_PAC_RESET_KEYS:
6487 if (arg3 || arg4 || arg5) {
6488 return -TARGET_EINVAL;
6489 }
6490 return do_prctl_reset_keys(env, arg2);
6491 case PR_SET_TAGGED_ADDR_CTRL:
6492 if (arg3 || arg4 || arg5) {
6493 return -TARGET_EINVAL;
6494 }
6495 return do_prctl_set_tagged_addr_ctrl(env, arg2);
6496 case PR_GET_TAGGED_ADDR_CTRL:
6497 if (arg2 || arg3 || arg4 || arg5) {
6498 return -TARGET_EINVAL;
6499 }
6500 return do_prctl_get_tagged_addr_ctrl(env);
Richard Henderson220717a2021-12-27 07:01:23 -08006501
Richard Henderson6e8dcac2021-12-27 07:01:24 -08006502 case PR_GET_UNALIGN:
6503 return do_prctl_get_unalign(env, arg2);
6504 case PR_SET_UNALIGN:
6505 return do_prctl_set_unalign(env, arg2);
6506
Richard Henderson4f4e5562022-01-06 14:57:38 -08006507 case PR_CAP_AMBIENT:
6508 case PR_CAPBSET_READ:
6509 case PR_CAPBSET_DROP:
Richard Henderson220717a2021-12-27 07:01:23 -08006510 case PR_GET_DUMPABLE:
6511 case PR_SET_DUMPABLE:
6512 case PR_GET_KEEPCAPS:
6513 case PR_SET_KEEPCAPS:
Richard Henderson4f4e5562022-01-06 14:57:38 -08006514 case PR_GET_SECUREBITS:
6515 case PR_SET_SECUREBITS:
Richard Henderson220717a2021-12-27 07:01:23 -08006516 case PR_GET_TIMING:
6517 case PR_SET_TIMING:
6518 case PR_GET_TIMERSLACK:
6519 case PR_SET_TIMERSLACK:
6520 case PR_MCE_KILL:
6521 case PR_MCE_KILL_GET:
6522 case PR_GET_NO_NEW_PRIVS:
6523 case PR_SET_NO_NEW_PRIVS:
6524 case PR_GET_IO_FLUSHER:
6525 case PR_SET_IO_FLUSHER:
6526 /* Some prctl options have no pointer arguments and we can pass on. */
6527 return get_errno(prctl(option, arg2, arg3, arg4, arg5));
6528
6529 case PR_GET_CHILD_SUBREAPER:
6530 case PR_SET_CHILD_SUBREAPER:
6531 case PR_GET_SPECULATION_CTRL:
6532 case PR_SET_SPECULATION_CTRL:
6533 case PR_GET_TID_ADDRESS:
6534 /* TODO */
6535 return -TARGET_EINVAL;
6536
6537 case PR_GET_FPEXC:
6538 case PR_SET_FPEXC:
6539 /* Was used for SPE on PowerPC. */
6540 return -TARGET_EINVAL;
6541
6542 case PR_GET_ENDIAN:
6543 case PR_SET_ENDIAN:
6544 case PR_GET_FPEMU:
6545 case PR_SET_FPEMU:
6546 case PR_SET_MM:
Richard Henderson87e9bf22021-12-27 07:01:22 -08006547 case PR_GET_SECCOMP:
6548 case PR_SET_SECCOMP:
Richard Henderson220717a2021-12-27 07:01:23 -08006549 case PR_SET_SYSCALL_USER_DISPATCH:
6550 case PR_GET_THP_DISABLE:
6551 case PR_SET_THP_DISABLE:
6552 case PR_GET_TSC:
6553 case PR_SET_TSC:
Richard Henderson220717a2021-12-27 07:01:23 -08006554 /* Disable to prevent the target disabling stuff we need. */
Richard Henderson87e9bf22021-12-27 07:01:22 -08006555 return -TARGET_EINVAL;
Richard Henderson220717a2021-12-27 07:01:23 -08006556
Richard Henderson87e9bf22021-12-27 07:01:22 -08006557 default:
Richard Henderson220717a2021-12-27 07:01:23 -08006558 qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
6559 option);
6560 return -TARGET_EINVAL;
Richard Henderson87e9bf22021-12-27 07:01:22 -08006561 }
6562}
6563
Riku Voipio05098a92011-03-04 15:27:29 +02006564#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00006565
pbrookd865bab2008-06-07 22:12:17 +00006566
6567static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6568typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006569 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00006570 pthread_mutex_t mutex;
6571 pthread_cond_t cond;
6572 pthread_t thread;
6573 uint32_t tid;
6574 abi_ulong child_tidptr;
6575 abi_ulong parent_tidptr;
6576 sigset_t sigmask;
6577} new_thread_info;
6578
6579static void *clone_func(void *arg)
6580{
6581 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01006582 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01006583 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03006584 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00006585
Emilio G. Cota70903762015-08-23 20:23:41 -04006586 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04006587 tcg_register_thread();
pbrookd865bab2008-06-07 22:12:17 +00006588 env = info->env;
Richard Henderson29a0af62019-03-22 16:07:18 -07006589 cpu = env_cpu(env);
Andreas Färbera2247f82013-06-09 19:47:04 +02006590 thread_cpu = cpu;
Andreas Färber0429a972013-08-26 18:14:44 +02006591 ts = (TaskState *)cpu->opaque;
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +00006592 info->tid = sys_gettid();
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03006593 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00006594 if (info->child_tidptr)
6595 put_user_u32(info->tid, info->child_tidptr);
6596 if (info->parent_tidptr)
6597 put_user_u32(info->tid, info->parent_tidptr);
Richard Henderson5ebdd772019-03-14 13:10:53 -07006598 qemu_guest_random_seed_thread_part2(cpu->random_seed);
pbrookd865bab2008-06-07 22:12:17 +00006599 /* Enable signals. */
6600 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6601 /* Signal to the parent that we're ready. */
6602 pthread_mutex_lock(&info->mutex);
6603 pthread_cond_broadcast(&info->cond);
6604 pthread_mutex_unlock(&info->mutex);
Emilio G. Cotaa4dd3d52017-10-18 18:01:41 -04006605 /* Wait until the parent has finished initializing the tls state. */
pbrookd865bab2008-06-07 22:12:17 +00006606 pthread_mutex_lock(&clone_lock);
6607 pthread_mutex_unlock(&clone_lock);
6608 cpu_loop(env);
6609 /* never exits */
6610 return NULL;
6611}
bellard1b6b0292003-03-22 17:31:38 +00006612
ths0da46a62007-10-20 20:23:07 +00006613/* do_fork() Must return host values and target errnos (unlike most
6614 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01006615static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00006616 abi_ulong parent_tidptr, target_ulong newtls,
6617 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00006618{
Richard Henderson29a0af62019-03-22 16:07:18 -07006619 CPUState *cpu = env_cpu(env);
bellard1b6b0292003-03-22 17:31:38 +00006620 int ret;
bellard5cd43932003-03-29 16:54:36 +00006621 TaskState *ts;
Andreas Färber0429a972013-08-26 18:14:44 +02006622 CPUState *new_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01006623 CPUArchState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00006624 sigset_t sigmask;
ths3b46e622007-09-17 08:09:54 +00006625
Peter Maydell5ea2fc82016-08-02 18:41:27 +01006626 flags &= ~CLONE_IGNORED_FLAGS;
6627
balrog436d1242008-09-21 02:39:45 +00006628 /* Emulate vfork() with fork() */
6629 if (flags & CLONE_VFORK)
6630 flags &= ~(CLONE_VFORK | CLONE_VM);
6631
bellard1b6b0292003-03-22 17:31:38 +00006632 if (flags & CLONE_VM) {
Andreas Färber0429a972013-08-26 18:14:44 +02006633 TaskState *parent_ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00006634 new_thread_info info;
6635 pthread_attr_t attr;
Peter Maydell24cb36a2013-07-16 18:45:00 +01006636
Peter Maydell5ea2fc82016-08-02 18:41:27 +01006637 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6638 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6639 return -TARGET_EINVAL;
6640 }
6641
Markus Armbrusterc78d65e2015-09-14 13:53:03 +02006642 ts = g_new0(TaskState, 1);
pbrook624f7972008-05-31 16:11:38 +00006643 init_task_state(ts);
Max Filippov73a988d2018-03-30 06:19:58 -07006644
6645 /* Grab a mutex so that thread setup appears atomic. */
6646 pthread_mutex_lock(&clone_lock);
6647
Richard Henderson6cc9d672021-03-01 19:21:08 -08006648 /*
6649 * If this is our first additional thread, we need to ensure we
6650 * generate code for parallel execution and flush old translations.
6651 * Do this now so that the copy gets CF_PARALLEL too.
6652 */
6653 if (!(cpu->tcg_cflags & CF_PARALLEL)) {
6654 cpu->tcg_cflags |= CF_PARALLEL;
6655 tb_flush(cpu);
6656 }
6657
bellard1b6b0292003-03-22 17:31:38 +00006658 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00006659 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00006660 /* Init regs that differ from the parent. */
Richard Henderson608999d2019-11-06 12:33:15 +01006661 cpu_clone_regs_child(new_env, newsp, flags);
Richard Henderson07a6ecf2019-11-06 12:33:16 +01006662 cpu_clone_regs_parent(env, flags);
Richard Henderson29a0af62019-03-22 16:07:18 -07006663 new_cpu = env_cpu(new_env);
Andreas Färber0429a972013-08-26 18:14:44 +02006664 new_cpu->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03006665 ts->bprm = parent_ts->bprm;
6666 ts->info = parent_ts->info;
Peter Maydell3d3efba2016-05-27 15:51:49 +01006667 ts->signal_mask = parent_ts->signal_mask;
pbrookd865bab2008-06-07 22:12:17 +00006668
Peter Maydell7cfbd382016-08-02 18:41:26 +01006669 if (flags & CLONE_CHILD_CLEARTID) {
pbrookc2764712009-03-07 15:24:59 +00006670 ts->child_tidptr = child_tidptr;
6671 }
6672
Peter Maydell7cfbd382016-08-02 18:41:26 +01006673 if (flags & CLONE_SETTLS) {
pbrookd865bab2008-06-07 22:12:17 +00006674 cpu_set_tls (new_env, newtls);
Peter Maydell7cfbd382016-08-02 18:41:26 +01006675 }
pbrookd865bab2008-06-07 22:12:17 +00006676
pbrookd865bab2008-06-07 22:12:17 +00006677 memset(&info, 0, sizeof(info));
6678 pthread_mutex_init(&info.mutex, NULL);
6679 pthread_mutex_lock(&info.mutex);
6680 pthread_cond_init(&info.cond, NULL);
6681 info.env = new_env;
Peter Maydell7cfbd382016-08-02 18:41:26 +01006682 if (flags & CLONE_CHILD_SETTID) {
pbrookd865bab2008-06-07 22:12:17 +00006683 info.child_tidptr = child_tidptr;
Peter Maydell7cfbd382016-08-02 18:41:26 +01006684 }
6685 if (flags & CLONE_PARENT_SETTID) {
pbrookd865bab2008-06-07 22:12:17 +00006686 info.parent_tidptr = parent_tidptr;
Peter Maydell7cfbd382016-08-02 18:41:26 +01006687 }
pbrookd865bab2008-06-07 22:12:17 +00006688
6689 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07006690 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6691 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00006692 /* It is not safe to deliver signals until the child has finished
6693 initializing, so temporarily block all signals. */
6694 sigfillset(&sigmask);
6695 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
Richard Henderson5ebdd772019-03-14 13:10:53 -07006696 cpu->random_seed = qemu_guest_random_seed_thread_part1();
pbrookd865bab2008-06-07 22:12:17 +00006697
6698 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00006699 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00006700
6701 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6702 pthread_attr_destroy(&attr);
6703 if (ret == 0) {
6704 /* Wait for the child to initialize. */
6705 pthread_cond_wait(&info.cond, &info.mutex);
6706 ret = info.tid;
pbrookd865bab2008-06-07 22:12:17 +00006707 } else {
6708 ret = -1;
6709 }
6710 pthread_mutex_unlock(&info.mutex);
6711 pthread_cond_destroy(&info.cond);
6712 pthread_mutex_destroy(&info.mutex);
6713 pthread_mutex_unlock(&clone_lock);
bellard1b6b0292003-03-22 17:31:38 +00006714 } else {
6715 /* if no CLONE_VM, we consider it is a fork */
Peter Maydell5ea2fc82016-08-02 18:41:27 +01006716 if (flags & CLONE_INVALID_FORK_FLAGS) {
6717 return -TARGET_EINVAL;
6718 }
6719
6720 /* We can't support custom termination signals */
6721 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
Timothy E Baldwin93b4eff2015-08-31 00:26:21 +01006722 return -TARGET_EINVAL;
6723 }
Timothy E Baldwin7d92d342016-05-27 15:51:58 +01006724
6725 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01006726 return -QEMU_ERESTARTSYS;
Timothy E Baldwin7d92d342016-05-27 15:51:58 +01006727 }
6728
pbrookd865bab2008-06-07 22:12:17 +00006729 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00006730 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00006731 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00006732 /* Child Process. */
Richard Henderson608999d2019-11-06 12:33:15 +01006733 cpu_clone_regs_child(env, newsp, flags);
pbrookd865bab2008-06-07 22:12:17 +00006734 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00006735 /* There is a race condition here. The parent process could
6736 theoretically read the TID in the child process before the child
6737 tid is set. This would require using either ptrace
6738 (not implemented) or having *_tidptr to point at a shared memory
6739 mapping. We can't repeat the spinlock hack used above because
6740 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00006741 if (flags & CLONE_CHILD_SETTID)
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +00006742 put_user_u32(sys_gettid(), child_tidptr);
pbrookd865bab2008-06-07 22:12:17 +00006743 if (flags & CLONE_PARENT_SETTID)
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +00006744 put_user_u32(sys_gettid(), parent_tidptr);
Andreas Färber0429a972013-08-26 18:14:44 +02006745 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00006746 if (flags & CLONE_SETTLS)
6747 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00006748 if (flags & CLONE_CHILD_CLEARTID)
6749 ts->child_tidptr = child_tidptr;
pbrookd865bab2008-06-07 22:12:17 +00006750 } else {
Richard Henderson07a6ecf2019-11-06 12:33:16 +01006751 cpu_clone_regs_parent(env, flags);
pbrookd865bab2008-06-07 22:12:17 +00006752 fork_end(0);
6753 }
bellard1b6b0292003-03-22 17:31:38 +00006754 }
6755 return ret;
6756}
6757
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006758/* warning : doesn't handle linux specific flags... */
6759static int target_to_host_fcntl_cmd(int cmd)
6760{
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006761 int ret;
6762
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006763 switch(cmd) {
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006764 case TARGET_F_DUPFD:
6765 case TARGET_F_GETFD:
6766 case TARGET_F_SETFD:
6767 case TARGET_F_GETFL:
6768 case TARGET_F_SETFL:
Andreas Schwab2d92c682020-05-25 09:59:28 +02006769 case TARGET_F_OFD_GETLK:
6770 case TARGET_F_OFD_SETLK:
6771 case TARGET_F_OFD_SETLKW:
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006772 ret = cmd;
6773 break;
6774 case TARGET_F_GETLK:
6775 ret = F_GETLK64;
6776 break;
6777 case TARGET_F_SETLK:
6778 ret = F_SETLK64;
6779 break;
6780 case TARGET_F_SETLKW:
6781 ret = F_SETLKW64;
6782 break;
6783 case TARGET_F_GETOWN:
6784 ret = F_GETOWN;
6785 break;
6786 case TARGET_F_SETOWN:
6787 ret = F_SETOWN;
6788 break;
6789 case TARGET_F_GETSIG:
6790 ret = F_GETSIG;
6791 break;
6792 case TARGET_F_SETSIG:
6793 ret = F_SETSIG;
6794 break;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006795#if TARGET_ABI_BITS == 32
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006796 case TARGET_F_GETLK64:
6797 ret = F_GETLK64;
6798 break;
6799 case TARGET_F_SETLK64:
6800 ret = F_SETLK64;
6801 break;
6802 case TARGET_F_SETLKW64:
6803 ret = F_SETLKW64;
6804 break;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006805#endif
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006806 case TARGET_F_SETLEASE:
6807 ret = F_SETLEASE;
6808 break;
6809 case TARGET_F_GETLEASE:
6810 ret = F_GETLEASE;
6811 break;
malcfbd5de92009-09-06 06:31:59 +04006812#ifdef F_DUPFD_CLOEXEC
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006813 case TARGET_F_DUPFD_CLOEXEC:
6814 ret = F_DUPFD_CLOEXEC;
6815 break;
malcfbd5de92009-09-06 06:31:59 +04006816#endif
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006817 case TARGET_F_NOTIFY:
6818 ret = F_NOTIFY;
6819 break;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006820#ifdef F_GETOWN_EX
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006821 case TARGET_F_GETOWN_EX:
6822 ret = F_GETOWN_EX;
6823 break;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006824#endif
6825#ifdef F_SETOWN_EX
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006826 case TARGET_F_SETOWN_EX:
6827 ret = F_SETOWN_EX;
6828 break;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006829#endif
Peter Maydellddf31aa2016-06-30 17:33:49 +01006830#ifdef F_SETPIPE_SZ
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006831 case TARGET_F_SETPIPE_SZ:
6832 ret = F_SETPIPE_SZ;
6833 break;
6834 case TARGET_F_GETPIPE_SZ:
6835 ret = F_GETPIPE_SZ;
6836 break;
Peter Maydellddf31aa2016-06-30 17:33:49 +01006837#endif
Shu-Chun Weng2bb963f2020-12-18 11:32:10 -08006838#ifdef F_ADD_SEALS
6839 case TARGET_F_ADD_SEALS:
6840 ret = F_ADD_SEALS;
6841 break;
6842 case TARGET_F_GET_SEALS:
6843 ret = F_GET_SEALS;
6844 break;
6845#endif
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006846 default:
6847 ret = -TARGET_EINVAL;
6848 break;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006849 }
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006850
6851#if defined(__powerpc64__)
6852 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6853 * is not supported by kernel. The glibc fcntl call actually adjusts
6854 * them to 5, 6 and 7 before making the syscall(). Since we make the
6855 * syscall directly, adjust to what is supported by the kernel.
6856 */
6857 if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
6858 ret -= F_GETLK64 - 5;
6859 }
6860#endif
6861
6862 return ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006863}
6864
Laurent Vivierae68ad92018-05-10 01:11:21 +02006865#define FLOCK_TRANSTBL \
6866 switch (type) { \
6867 TRANSTBL_CONVERT(F_RDLCK); \
6868 TRANSTBL_CONVERT(F_WRLCK); \
6869 TRANSTBL_CONVERT(F_UNLCK); \
Laurent Vivierae68ad92018-05-10 01:11:21 +02006870 }
6871
6872static int target_to_host_flock(int type)
6873{
6874#define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6875 FLOCK_TRANSTBL
6876#undef TRANSTBL_CONVERT
6877 return -TARGET_EINVAL;
6878}
6879
6880static int host_to_target_flock(int type)
6881{
6882#define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6883 FLOCK_TRANSTBL
6884#undef TRANSTBL_CONVERT
6885 /* if we don't know how to convert the value coming
6886 * from the host we copy to the target field as-is
6887 */
6888 return type;
6889}
Laurent Vivier2ba7f732013-01-10 21:42:48 +01006890
Peter Maydell213d3e92016-06-13 11:22:05 +01006891static inline abi_long copy_from_user_flock(struct flock64 *fl,
6892 abi_ulong target_flock_addr)
6893{
6894 struct target_flock *target_fl;
Laurent Vivierae68ad92018-05-10 01:11:21 +02006895 int l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006896
6897 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6898 return -TARGET_EFAULT;
6899 }
6900
6901 __get_user(l_type, &target_fl->l_type);
Laurent Vivierae68ad92018-05-10 01:11:21 +02006902 l_type = target_to_host_flock(l_type);
6903 if (l_type < 0) {
6904 return l_type;
6905 }
6906 fl->l_type = l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006907 __get_user(fl->l_whence, &target_fl->l_whence);
6908 __get_user(fl->l_start, &target_fl->l_start);
6909 __get_user(fl->l_len, &target_fl->l_len);
6910 __get_user(fl->l_pid, &target_fl->l_pid);
6911 unlock_user_struct(target_fl, target_flock_addr, 0);
6912 return 0;
6913}
6914
6915static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6916 const struct flock64 *fl)
6917{
6918 struct target_flock *target_fl;
6919 short l_type;
6920
6921 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6922 return -TARGET_EFAULT;
6923 }
6924
Laurent Vivierae68ad92018-05-10 01:11:21 +02006925 l_type = host_to_target_flock(fl->l_type);
Peter Maydell213d3e92016-06-13 11:22:05 +01006926 __put_user(l_type, &target_fl->l_type);
6927 __put_user(fl->l_whence, &target_fl->l_whence);
6928 __put_user(fl->l_start, &target_fl->l_start);
6929 __put_user(fl->l_len, &target_fl->l_len);
6930 __put_user(fl->l_pid, &target_fl->l_pid);
6931 unlock_user_struct(target_fl, target_flock_addr, 1);
6932 return 0;
6933}
6934
6935typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6936typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6937
6938#if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
Richard Hendersonffe81d42022-01-06 20:25:59 -08006939struct target_oabi_flock64 {
6940 abi_short l_type;
6941 abi_short l_whence;
6942 abi_llong l_start;
6943 abi_llong l_len;
6944 abi_int l_pid;
6945} QEMU_PACKED;
6946
Laurent Vivier7f254c52018-05-02 23:57:30 +02006947static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
Peter Maydell213d3e92016-06-13 11:22:05 +01006948 abi_ulong target_flock_addr)
6949{
Laurent Vivier7f254c52018-05-02 23:57:30 +02006950 struct target_oabi_flock64 *target_fl;
Laurent Vivierae68ad92018-05-10 01:11:21 +02006951 int l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006952
6953 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6954 return -TARGET_EFAULT;
6955 }
6956
6957 __get_user(l_type, &target_fl->l_type);
Laurent Vivierae68ad92018-05-10 01:11:21 +02006958 l_type = target_to_host_flock(l_type);
6959 if (l_type < 0) {
6960 return l_type;
6961 }
6962 fl->l_type = l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006963 __get_user(fl->l_whence, &target_fl->l_whence);
6964 __get_user(fl->l_start, &target_fl->l_start);
6965 __get_user(fl->l_len, &target_fl->l_len);
6966 __get_user(fl->l_pid, &target_fl->l_pid);
6967 unlock_user_struct(target_fl, target_flock_addr, 0);
6968 return 0;
6969}
6970
Laurent Vivier7f254c52018-05-02 23:57:30 +02006971static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
Peter Maydell213d3e92016-06-13 11:22:05 +01006972 const struct flock64 *fl)
6973{
Laurent Vivier7f254c52018-05-02 23:57:30 +02006974 struct target_oabi_flock64 *target_fl;
Peter Maydell213d3e92016-06-13 11:22:05 +01006975 short l_type;
6976
6977 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6978 return -TARGET_EFAULT;
6979 }
6980
Laurent Vivierae68ad92018-05-10 01:11:21 +02006981 l_type = host_to_target_flock(fl->l_type);
Peter Maydell213d3e92016-06-13 11:22:05 +01006982 __put_user(l_type, &target_fl->l_type);
6983 __put_user(fl->l_whence, &target_fl->l_whence);
6984 __put_user(fl->l_start, &target_fl->l_start);
6985 __put_user(fl->l_len, &target_fl->l_len);
6986 __put_user(fl->l_pid, &target_fl->l_pid);
6987 unlock_user_struct(target_fl, target_flock_addr, 1);
6988 return 0;
6989}
6990#endif
6991
6992static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6993 abi_ulong target_flock_addr)
6994{
6995 struct target_flock64 *target_fl;
Laurent Vivierae68ad92018-05-10 01:11:21 +02006996 int l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006997
6998 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6999 return -TARGET_EFAULT;
7000 }
7001
7002 __get_user(l_type, &target_fl->l_type);
Laurent Vivierae68ad92018-05-10 01:11:21 +02007003 l_type = target_to_host_flock(l_type);
7004 if (l_type < 0) {
7005 return l_type;
7006 }
7007 fl->l_type = l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01007008 __get_user(fl->l_whence, &target_fl->l_whence);
7009 __get_user(fl->l_start, &target_fl->l_start);
7010 __get_user(fl->l_len, &target_fl->l_len);
7011 __get_user(fl->l_pid, &target_fl->l_pid);
7012 unlock_user_struct(target_fl, target_flock_addr, 0);
7013 return 0;
7014}
7015
7016static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
7017 const struct flock64 *fl)
7018{
7019 struct target_flock64 *target_fl;
7020 short l_type;
7021
7022 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
7023 return -TARGET_EFAULT;
7024 }
7025
Laurent Vivierae68ad92018-05-10 01:11:21 +02007026 l_type = host_to_target_flock(fl->l_type);
Peter Maydell213d3e92016-06-13 11:22:05 +01007027 __put_user(l_type, &target_fl->l_type);
7028 __put_user(fl->l_whence, &target_fl->l_whence);
7029 __put_user(fl->l_start, &target_fl->l_start);
7030 __put_user(fl->l_len, &target_fl->l_len);
7031 __put_user(fl->l_pid, &target_fl->l_pid);
7032 unlock_user_struct(target_fl, target_flock_addr, 1);
7033 return 0;
7034}
7035
blueswir1992f48a2007-10-14 16:27:31 +00007036static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00007037{
ths43f238d2007-01-05 20:55:49 +00007038 struct flock64 fl64;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007039#ifdef F_GETOWN_EX
7040 struct f_owner_ex fox;
7041 struct target_f_owner_ex *target_fox;
7042#endif
blueswir1992f48a2007-10-14 16:27:31 +00007043 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007044 int host_cmd = target_to_host_fcntl_cmd(cmd);
7045
7046 if (host_cmd == -TARGET_EINVAL)
7047 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00007048
bellard7775e9e2003-05-14 22:46:48 +00007049 switch(cmd) {
7050 case TARGET_F_GETLK:
Peter Maydell213d3e92016-06-13 11:22:05 +01007051 ret = copy_from_user_flock(&fl64, arg);
7052 if (ret) {
7053 return ret;
7054 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007055 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00007056 if (ret == 0) {
Peter Maydell213d3e92016-06-13 11:22:05 +01007057 ret = copy_to_user_flock(arg, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00007058 }
7059 break;
ths3b46e622007-09-17 08:09:54 +00007060
bellard7775e9e2003-05-14 22:46:48 +00007061 case TARGET_F_SETLK:
7062 case TARGET_F_SETLKW:
Peter Maydell213d3e92016-06-13 11:22:05 +01007063 ret = copy_from_user_flock(&fl64, arg);
7064 if (ret) {
7065 return ret;
7066 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007067 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00007068 break;
ths3b46e622007-09-17 08:09:54 +00007069
bellard7775e9e2003-05-14 22:46:48 +00007070 case TARGET_F_GETLK64:
Andreas Schwab2d92c682020-05-25 09:59:28 +02007071 case TARGET_F_OFD_GETLK:
Peter Maydell213d3e92016-06-13 11:22:05 +01007072 ret = copy_from_user_flock64(&fl64, arg);
7073 if (ret) {
7074 return ret;
7075 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007076 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00007077 if (ret == 0) {
Peter Maydell213d3e92016-06-13 11:22:05 +01007078 ret = copy_to_user_flock64(arg, &fl64);
ths43f238d2007-01-05 20:55:49 +00007079 }
bellard9ee1fa22007-11-11 15:11:19 +00007080 break;
bellard7775e9e2003-05-14 22:46:48 +00007081 case TARGET_F_SETLK64:
7082 case TARGET_F_SETLKW64:
Andreas Schwab2d92c682020-05-25 09:59:28 +02007083 case TARGET_F_OFD_SETLK:
7084 case TARGET_F_OFD_SETLKW:
Peter Maydell213d3e92016-06-13 11:22:05 +01007085 ret = copy_from_user_flock64(&fl64, arg);
7086 if (ret) {
7087 return ret;
7088 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007089 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00007090 break;
7091
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007092 case TARGET_F_GETFL:
Peter Maydell435da5e2016-06-13 11:22:05 +01007093 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00007094 if (ret >= 0) {
7095 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
7096 }
bellardffa65c32004-01-04 23:57:22 +00007097 break;
7098
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007099 case TARGET_F_SETFL:
Peter Maydell435da5e2016-06-13 11:22:05 +01007100 ret = get_errno(safe_fcntl(fd, host_cmd,
7101 target_to_host_bitmask(arg,
7102 fcntl_flags_tbl)));
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007103 break;
7104
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007105#ifdef F_GETOWN_EX
7106 case TARGET_F_GETOWN_EX:
Peter Maydell435da5e2016-06-13 11:22:05 +01007107 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007108 if (ret >= 0) {
7109 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
7110 return -TARGET_EFAULT;
7111 target_fox->type = tswap32(fox.type);
7112 target_fox->pid = tswap32(fox.pid);
7113 unlock_user_struct(target_fox, arg, 1);
7114 }
7115 break;
7116#endif
7117
7118#ifdef F_SETOWN_EX
7119 case TARGET_F_SETOWN_EX:
7120 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
7121 return -TARGET_EFAULT;
7122 fox.type = tswap32(target_fox->type);
7123 fox.pid = tswap32(target_fox->pid);
7124 unlock_user_struct(target_fox, arg, 0);
Peter Maydell435da5e2016-06-13 11:22:05 +01007125 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007126 break;
7127#endif
7128
Timothy Baldwina7b4c9b2020-07-29 21:06:44 +01007129 case TARGET_F_SETSIG:
7130 ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
7131 break;
7132
7133 case TARGET_F_GETSIG:
7134 ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
7135 break;
7136
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007137 case TARGET_F_SETOWN:
7138 case TARGET_F_GETOWN:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007139 case TARGET_F_SETLEASE:
7140 case TARGET_F_GETLEASE:
Peter Maydell7e3b92e2016-06-20 15:50:37 +01007141 case TARGET_F_SETPIPE_SZ:
7142 case TARGET_F_GETPIPE_SZ:
Shu-Chun Weng2bb963f2020-12-18 11:32:10 -08007143 case TARGET_F_ADD_SEALS:
7144 case TARGET_F_GET_SEALS:
Peter Maydell435da5e2016-06-13 11:22:05 +01007145 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00007146 break;
7147
bellard7775e9e2003-05-14 22:46:48 +00007148 default:
Peter Maydell435da5e2016-06-13 11:22:05 +01007149 ret = get_errno(safe_fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00007150 break;
7151 }
7152 return ret;
7153}
7154
bellard67867302003-11-23 17:05:30 +00007155#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00007156
bellard67867302003-11-23 17:05:30 +00007157static inline int high2lowuid(int uid)
7158{
7159 if (uid > 65535)
7160 return 65534;
7161 else
7162 return uid;
7163}
7164
7165static inline int high2lowgid(int gid)
7166{
7167 if (gid > 65535)
7168 return 65534;
7169 else
7170 return gid;
7171}
7172
7173static inline int low2highuid(int uid)
7174{
7175 if ((int16_t)uid == -1)
7176 return -1;
7177 else
7178 return uid;
7179}
7180
7181static inline int low2highgid(int gid)
7182{
7183 if ((int16_t)gid == -1)
7184 return -1;
7185 else
7186 return gid;
7187}
Riku Voipio0c866a72011-04-18 15:23:06 +03007188static inline int tswapid(int id)
7189{
7190 return tswap16(id);
7191}
Peter Maydell76ca3102014-03-02 19:36:41 +00007192
7193#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7194
Riku Voipio0c866a72011-04-18 15:23:06 +03007195#else /* !USE_UID16 */
7196static inline int high2lowuid(int uid)
7197{
7198 return uid;
7199}
7200static inline int high2lowgid(int gid)
7201{
7202 return gid;
7203}
7204static inline int low2highuid(int uid)
7205{
7206 return uid;
7207}
7208static inline int low2highgid(int gid)
7209{
7210 return gid;
7211}
7212static inline int tswapid(int id)
7213{
7214 return tswap32(id);
7215}
Peter Maydell76ca3102014-03-02 19:36:41 +00007216
7217#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7218
bellard67867302003-11-23 17:05:30 +00007219#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00007220
Peter Maydellfd6f7792016-03-01 16:33:02 +00007221/* We must do direct syscalls for setting UID/GID, because we want to
7222 * implement the Linux system call semantics of "change only for this thread",
7223 * not the libc/POSIX semantics of "change for all threads in process".
7224 * (See http://ewontfix.com/17/ for more details.)
7225 * We use the 32-bit version of the syscalls if present; if it is not
7226 * then either the host architecture supports 32-bit UIDs natively with
7227 * the standard syscall, or the 16-bit UID is the best we can do.
7228 */
7229#ifdef __NR_setuid32
7230#define __NR_sys_setuid __NR_setuid32
7231#else
7232#define __NR_sys_setuid __NR_setuid
7233#endif
7234#ifdef __NR_setgid32
7235#define __NR_sys_setgid __NR_setgid32
7236#else
7237#define __NR_sys_setgid __NR_setgid
7238#endif
7239#ifdef __NR_setresuid32
7240#define __NR_sys_setresuid __NR_setresuid32
7241#else
7242#define __NR_sys_setresuid __NR_setresuid
7243#endif
7244#ifdef __NR_setresgid32
7245#define __NR_sys_setresgid __NR_setresgid32
7246#else
7247#define __NR_sys_setresgid __NR_setresgid
7248#endif
7249
7250_syscall1(int, sys_setuid, uid_t, uid)
7251_syscall1(int, sys_setgid, gid_t, gid)
7252_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
7253_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
7254
bellard31e31b82003-02-18 22:55:36 +00007255void syscall_init(void)
7256{
bellard2ab83ea2003-06-15 19:56:46 +00007257 IOCTLEntry *ie;
7258 const argtype *arg_type;
7259 int size;
7260
Alexander Graf8be656b2015-05-06 23:47:32 +02007261 thunk_init(STRUCT_MAX);
7262
Blue Swirl001faf32009-05-13 17:53:17 +00007263#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00007264#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00007265#include "syscall_types.h"
7266#undef STRUCT
7267#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00007268
7269 /* we patch the ioctl size if necessary. We rely on the fact that
7270 no ioctl has all the bits at '1' in the size field */
7271 ie = ioctl_entries;
7272 while (ie->target_cmd != 0) {
7273 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
7274 TARGET_IOC_SIZEMASK) {
7275 arg_type = ie->arg_type;
7276 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00007277 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00007278 ie->target_cmd);
7279 exit(1);
7280 }
7281 arg_type++;
7282 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00007283 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00007284 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
7285 (size << TARGET_IOC_SIZESHIFT);
7286 }
thsb92c47c2007-11-01 00:07:38 +00007287
bellard2ab83ea2003-06-15 19:56:46 +00007288 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00007289#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7290 (defined(__x86_64__) && defined(TARGET_X86_64))
7291 if (unlikely(ie->target_cmd != ie->host_cmd)) {
7292 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7293 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00007294 }
7295#endif
7296 ie++;
7297 }
bellard31e31b82003-02-18 22:55:36 +00007298}
bellardc573ff62004-01-04 15:51:36 +00007299
pbrookce4defa2006-02-09 16:49:55 +00007300#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00007301static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
7302 abi_long arg2,
7303 abi_long arg3,
7304 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00007305{
James Clarke8bf8e9d2017-09-15 20:33:13 +01007306 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
pbrookce4defa2006-02-09 16:49:55 +00007307 arg2 = arg3;
7308 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03007309 }
pbrookce4defa2006-02-09 16:49:55 +00007310 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
7311}
7312#endif
7313
7314#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00007315static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
7316 abi_long arg2,
7317 abi_long arg3,
7318 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00007319{
James Clarke8bf8e9d2017-09-15 20:33:13 +01007320 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
pbrookce4defa2006-02-09 16:49:55 +00007321 arg2 = arg3;
7322 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03007323 }
pbrookce4defa2006-02-09 16:49:55 +00007324 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
7325}
7326#endif
7327
Alistair Francis859e8a82020-03-12 15:13:49 -07007328#if defined(TARGET_NR_timer_settime) || \
7329 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
Filip Bozuta2c86c902020-07-22 17:34:20 +02007330static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007331 abi_ulong target_addr)
7332{
Filip Bozuta2c86c902020-07-22 17:34:20 +02007333 if (target_to_host_timespec(&host_its->it_interval, target_addr +
7334 offsetof(struct target_itimerspec,
7335 it_interval)) ||
7336 target_to_host_timespec(&host_its->it_value, target_addr +
7337 offsetof(struct target_itimerspec,
7338 it_value))) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007339 return -TARGET_EFAULT;
7340 }
7341
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007342 return 0;
7343}
Alistair Francis859e8a82020-03-12 15:13:49 -07007344#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007345
Filip Bozuta828cb3a2020-07-22 17:34:21 +02007346#if defined(TARGET_NR_timer_settime64) || \
7347 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7348static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
7349 abi_ulong target_addr)
7350{
7351 if (target_to_host_timespec64(&host_its->it_interval, target_addr +
7352 offsetof(struct target__kernel_itimerspec,
7353 it_interval)) ||
7354 target_to_host_timespec64(&host_its->it_value, target_addr +
7355 offsetof(struct target__kernel_itimerspec,
7356 it_value))) {
7357 return -TARGET_EFAULT;
7358 }
7359
7360 return 0;
7361}
7362#endif
7363
Alistair Francis859e8a82020-03-12 15:13:49 -07007364#if ((defined(TARGET_NR_timerfd_gettime) || \
7365 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
Filip Bozuta2c86c902020-07-22 17:34:20 +02007366 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007367static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
Filip Bozuta2c86c902020-07-22 17:34:20 +02007368 struct itimerspec *host_its)
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007369{
Filip Bozuta2c86c902020-07-22 17:34:20 +02007370 if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7371 it_interval),
7372 &host_its->it_interval) ||
7373 host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7374 it_value),
7375 &host_its->it_value)) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007376 return -TARGET_EFAULT;
7377 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007378 return 0;
7379}
Alistair Francis859e8a82020-03-12 15:13:49 -07007380#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007381
Filip Bozuta828cb3a2020-07-22 17:34:21 +02007382#if ((defined(TARGET_NR_timerfd_gettime64) || \
7383 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7384 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7385static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
7386 struct itimerspec *host_its)
7387{
7388 if (host_to_target_timespec64(target_addr +
7389 offsetof(struct target__kernel_itimerspec,
7390 it_interval),
7391 &host_its->it_interval) ||
7392 host_to_target_timespec64(target_addr +
7393 offsetof(struct target__kernel_itimerspec,
7394 it_value),
7395 &host_its->it_value)) {
7396 return -TARGET_EFAULT;
7397 }
7398 return 0;
7399}
7400#endif
7401
Alistair Francis859e8a82020-03-12 15:13:49 -07007402#if defined(TARGET_NR_adjtimex) || \
7403 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +02007404static inline abi_long target_to_host_timex(struct timex *host_tx,
7405 abi_long target_addr)
7406{
7407 struct target_timex *target_tx;
7408
7409 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7410 return -TARGET_EFAULT;
7411 }
7412
7413 __get_user(host_tx->modes, &target_tx->modes);
7414 __get_user(host_tx->offset, &target_tx->offset);
7415 __get_user(host_tx->freq, &target_tx->freq);
7416 __get_user(host_tx->maxerror, &target_tx->maxerror);
7417 __get_user(host_tx->esterror, &target_tx->esterror);
7418 __get_user(host_tx->status, &target_tx->status);
7419 __get_user(host_tx->constant, &target_tx->constant);
7420 __get_user(host_tx->precision, &target_tx->precision);
7421 __get_user(host_tx->tolerance, &target_tx->tolerance);
7422 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7423 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7424 __get_user(host_tx->tick, &target_tx->tick);
7425 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7426 __get_user(host_tx->jitter, &target_tx->jitter);
7427 __get_user(host_tx->shift, &target_tx->shift);
7428 __get_user(host_tx->stabil, &target_tx->stabil);
7429 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7430 __get_user(host_tx->calcnt, &target_tx->calcnt);
7431 __get_user(host_tx->errcnt, &target_tx->errcnt);
7432 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7433 __get_user(host_tx->tai, &target_tx->tai);
7434
7435 unlock_user_struct(target_tx, target_addr, 0);
7436 return 0;
7437}
7438
7439static inline abi_long host_to_target_timex(abi_long target_addr,
7440 struct timex *host_tx)
7441{
7442 struct target_timex *target_tx;
7443
7444 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7445 return -TARGET_EFAULT;
7446 }
7447
7448 __put_user(host_tx->modes, &target_tx->modes);
7449 __put_user(host_tx->offset, &target_tx->offset);
7450 __put_user(host_tx->freq, &target_tx->freq);
7451 __put_user(host_tx->maxerror, &target_tx->maxerror);
7452 __put_user(host_tx->esterror, &target_tx->esterror);
7453 __put_user(host_tx->status, &target_tx->status);
7454 __put_user(host_tx->constant, &target_tx->constant);
7455 __put_user(host_tx->precision, &target_tx->precision);
7456 __put_user(host_tx->tolerance, &target_tx->tolerance);
7457 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7458 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7459 __put_user(host_tx->tick, &target_tx->tick);
7460 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7461 __put_user(host_tx->jitter, &target_tx->jitter);
7462 __put_user(host_tx->shift, &target_tx->shift);
7463 __put_user(host_tx->stabil, &target_tx->stabil);
7464 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7465 __put_user(host_tx->calcnt, &target_tx->calcnt);
7466 __put_user(host_tx->errcnt, &target_tx->errcnt);
7467 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7468 __put_user(host_tx->tai, &target_tx->tai);
7469
7470 unlock_user_struct(target_tx, target_addr, 1);
7471 return 0;
7472}
Alistair Francis859e8a82020-03-12 15:13:49 -07007473#endif
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +02007474
Filip Bozuta6ac03b22020-08-24 21:21:15 +02007475
7476#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7477static inline abi_long target_to_host_timex64(struct timex *host_tx,
7478 abi_long target_addr)
7479{
7480 struct target__kernel_timex *target_tx;
7481
7482 if (copy_from_user_timeval64(&host_tx->time, target_addr +
7483 offsetof(struct target__kernel_timex,
7484 time))) {
7485 return -TARGET_EFAULT;
7486 }
7487
7488 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7489 return -TARGET_EFAULT;
7490 }
7491
7492 __get_user(host_tx->modes, &target_tx->modes);
7493 __get_user(host_tx->offset, &target_tx->offset);
7494 __get_user(host_tx->freq, &target_tx->freq);
7495 __get_user(host_tx->maxerror, &target_tx->maxerror);
7496 __get_user(host_tx->esterror, &target_tx->esterror);
7497 __get_user(host_tx->status, &target_tx->status);
7498 __get_user(host_tx->constant, &target_tx->constant);
7499 __get_user(host_tx->precision, &target_tx->precision);
7500 __get_user(host_tx->tolerance, &target_tx->tolerance);
7501 __get_user(host_tx->tick, &target_tx->tick);
7502 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7503 __get_user(host_tx->jitter, &target_tx->jitter);
7504 __get_user(host_tx->shift, &target_tx->shift);
7505 __get_user(host_tx->stabil, &target_tx->stabil);
7506 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7507 __get_user(host_tx->calcnt, &target_tx->calcnt);
7508 __get_user(host_tx->errcnt, &target_tx->errcnt);
7509 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7510 __get_user(host_tx->tai, &target_tx->tai);
7511
7512 unlock_user_struct(target_tx, target_addr, 0);
7513 return 0;
7514}
7515
7516static inline abi_long host_to_target_timex64(abi_long target_addr,
7517 struct timex *host_tx)
7518{
7519 struct target__kernel_timex *target_tx;
7520
7521 if (copy_to_user_timeval64(target_addr +
7522 offsetof(struct target__kernel_timex, time),
7523 &host_tx->time)) {
7524 return -TARGET_EFAULT;
7525 }
7526
7527 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7528 return -TARGET_EFAULT;
7529 }
7530
7531 __put_user(host_tx->modes, &target_tx->modes);
7532 __put_user(host_tx->offset, &target_tx->offset);
7533 __put_user(host_tx->freq, &target_tx->freq);
7534 __put_user(host_tx->maxerror, &target_tx->maxerror);
7535 __put_user(host_tx->esterror, &target_tx->esterror);
7536 __put_user(host_tx->status, &target_tx->status);
7537 __put_user(host_tx->constant, &target_tx->constant);
7538 __put_user(host_tx->precision, &target_tx->precision);
7539 __put_user(host_tx->tolerance, &target_tx->tolerance);
7540 __put_user(host_tx->tick, &target_tx->tick);
7541 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7542 __put_user(host_tx->jitter, &target_tx->jitter);
7543 __put_user(host_tx->shift, &target_tx->shift);
7544 __put_user(host_tx->stabil, &target_tx->stabil);
7545 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7546 __put_user(host_tx->calcnt, &target_tx->calcnt);
7547 __put_user(host_tx->errcnt, &target_tx->errcnt);
7548 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7549 __put_user(host_tx->tai, &target_tx->tai);
7550
7551 unlock_user_struct(target_tx, target_addr, 1);
7552 return 0;
7553}
7554#endif
7555
Michael Forney96ff7582021-05-25 20:55:56 -07007556#ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7557#define sigev_notify_thread_id _sigev_un._tid
7558#endif
7559
Peter Maydellc0659762014-08-09 15:42:32 +01007560static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
7561 abi_ulong target_addr)
7562{
7563 struct target_sigevent *target_sevp;
7564
7565 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
7566 return -TARGET_EFAULT;
7567 }
7568
7569 /* This union is awkward on 64 bit systems because it has a 32 bit
7570 * integer and a pointer in it; we follow the conversion approach
7571 * used for handling sigval types in signal.c so the guest should get
7572 * the correct value back even if we did a 64 bit byteswap and it's
7573 * using the 32 bit integer.
7574 */
7575 host_sevp->sigev_value.sival_ptr =
7576 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
7577 host_sevp->sigev_signo =
7578 target_to_host_signal(tswap32(target_sevp->sigev_signo));
7579 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
Michael Forney96ff7582021-05-25 20:55:56 -07007580 host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
Peter Maydellc0659762014-08-09 15:42:32 +01007581
7582 unlock_user_struct(target_sevp, target_addr, 1);
7583 return 0;
7584}
7585
Tom Musta6f6a4032014-08-12 13:53:42 -05007586#if defined(TARGET_NR_mlockall)
7587static inline int target_to_host_mlockall_arg(int arg)
7588{
7589 int result = 0;
7590
Filip Bozuta02e5d7d2020-08-11 18:45:51 +02007591 if (arg & TARGET_MCL_CURRENT) {
Tom Musta6f6a4032014-08-12 13:53:42 -05007592 result |= MCL_CURRENT;
7593 }
Filip Bozuta02e5d7d2020-08-11 18:45:51 +02007594 if (arg & TARGET_MCL_FUTURE) {
Tom Musta6f6a4032014-08-12 13:53:42 -05007595 result |= MCL_FUTURE;
7596 }
Filip Bozuta02e5d7d2020-08-11 18:45:51 +02007597#ifdef MCL_ONFAULT
7598 if (arg & TARGET_MCL_ONFAULT) {
7599 result |= MCL_ONFAULT;
7600 }
7601#endif
7602
Tom Musta6f6a4032014-08-12 13:53:42 -05007603 return result;
7604}
7605#endif
7606
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02007607#if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7608 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7609 defined(TARGET_NR_newfstatat))
balrog6a24a772008-09-20 02:23:36 +00007610static inline abi_long host_to_target_stat64(void *cpu_env,
7611 abi_ulong target_addr,
7612 struct stat *host_st)
7613{
Alexander Graf09701192013-09-03 20:12:15 +01007614#if defined(TARGET_ARM) && defined(TARGET_ABI32)
balrog6a24a772008-09-20 02:23:36 +00007615 if (((CPUARMState *)cpu_env)->eabi) {
7616 struct target_eabi_stat64 *target_st;
7617
7618 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7619 return -TARGET_EFAULT;
7620 memset(target_st, 0, sizeof(struct target_eabi_stat64));
7621 __put_user(host_st->st_dev, &target_st->st_dev);
7622 __put_user(host_st->st_ino, &target_st->st_ino);
7623#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7624 __put_user(host_st->st_ino, &target_st->__st_ino);
7625#endif
7626 __put_user(host_st->st_mode, &target_st->st_mode);
7627 __put_user(host_st->st_nlink, &target_st->st_nlink);
7628 __put_user(host_st->st_uid, &target_st->st_uid);
7629 __put_user(host_st->st_gid, &target_st->st_gid);
7630 __put_user(host_st->st_rdev, &target_st->st_rdev);
7631 __put_user(host_st->st_size, &target_st->st_size);
7632 __put_user(host_st->st_blksize, &target_st->st_blksize);
7633 __put_user(host_st->st_blocks, &target_st->st_blocks);
7634 __put_user(host_st->st_atime, &target_st->target_st_atime);
7635 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7636 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
Michael Forneyfebf6fa2021-05-25 20:55:31 -07007637#ifdef HAVE_STRUCT_STAT_ST_ATIM
Chen-Yu Tsai5f992db2019-05-23 00:21:47 +08007638 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7639 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7640 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7641#endif
balrog6a24a772008-09-20 02:23:36 +00007642 unlock_user_struct(target_st, target_addr, 1);
7643 } else
7644#endif
7645 {
Stefan Weil20d155b2013-10-30 22:52:24 +01007646#if defined(TARGET_HAS_STRUCT_STAT64)
balrog6a24a772008-09-20 02:23:36 +00007647 struct target_stat64 *target_st;
Stefan Weil20d155b2013-10-30 22:52:24 +01007648#else
7649 struct target_stat *target_st;
aurel329d33b762009-04-08 23:07:05 +00007650#endif
balrog6a24a772008-09-20 02:23:36 +00007651
7652 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7653 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00007654 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00007655 __put_user(host_st->st_dev, &target_st->st_dev);
7656 __put_user(host_st->st_ino, &target_st->st_ino);
7657#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7658 __put_user(host_st->st_ino, &target_st->__st_ino);
7659#endif
7660 __put_user(host_st->st_mode, &target_st->st_mode);
7661 __put_user(host_st->st_nlink, &target_st->st_nlink);
7662 __put_user(host_st->st_uid, &target_st->st_uid);
7663 __put_user(host_st->st_gid, &target_st->st_gid);
7664 __put_user(host_st->st_rdev, &target_st->st_rdev);
7665 /* XXX: better use of kernel struct */
7666 __put_user(host_st->st_size, &target_st->st_size);
7667 __put_user(host_st->st_blksize, &target_st->st_blksize);
7668 __put_user(host_st->st_blocks, &target_st->st_blocks);
7669 __put_user(host_st->st_atime, &target_st->target_st_atime);
7670 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7671 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
Michael Forneyfebf6fa2021-05-25 20:55:31 -07007672#ifdef HAVE_STRUCT_STAT_ST_ATIM
Chen-Yu Tsai5f992db2019-05-23 00:21:47 +08007673 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7674 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7675 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7676#endif
balrog6a24a772008-09-20 02:23:36 +00007677 unlock_user_struct(target_st, target_addr, 1);
7678 }
7679
7680 return 0;
7681}
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02007682#endif
balrog6a24a772008-09-20 02:23:36 +00007683
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02007684#if defined(TARGET_NR_statx) && defined(__NR_statx)
7685static inline abi_long host_to_target_statx(struct target_statx *host_stx,
7686 abi_ulong target_addr)
7687{
7688 struct target_statx *target_stx;
7689
7690 if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
7691 return -TARGET_EFAULT;
7692 }
7693 memset(target_stx, 0, sizeof(*target_stx));
7694
7695 __put_user(host_stx->stx_mask, &target_stx->stx_mask);
7696 __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
7697 __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
7698 __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
7699 __put_user(host_stx->stx_uid, &target_stx->stx_uid);
7700 __put_user(host_stx->stx_gid, &target_stx->stx_gid);
7701 __put_user(host_stx->stx_mode, &target_stx->stx_mode);
7702 __put_user(host_stx->stx_ino, &target_stx->stx_ino);
7703 __put_user(host_stx->stx_size, &target_stx->stx_size);
7704 __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
7705 __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
7706 __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
7707 __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
Ariadne Conilld1e26702019-11-22 11:40:40 -06007708 __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
7709 __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
7710 __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
7711 __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
7712 __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
7713 __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02007714 __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
7715 __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
7716 __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
7717 __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
7718
7719 unlock_user_struct(target_stx, target_addr, 1);
7720
7721 return 0;
7722}
7723#endif
7724
Alistair Francis14690292020-03-18 15:47:01 -07007725static int do_sys_futex(int *uaddr, int op, int val,
7726 const struct timespec *timeout, int *uaddr2,
7727 int val3)
7728{
7729#if HOST_LONG_BITS == 64
7730#if defined(__NR_futex)
7731 /* always a 64-bit time_t, it doesn't define _time64 version */
7732 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7733
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 sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
7740 }
7741#endif
7742#if defined(__NR_futex)
7743 /* old function on 32bit arch */
7744 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7745#endif
7746#endif /* HOST_LONG_BITS == 64 */
7747 g_assert_not_reached();
7748}
7749
7750static int do_safe_futex(int *uaddr, int op, int val,
7751 const struct timespec *timeout, int *uaddr2,
7752 int val3)
7753{
7754#if HOST_LONG_BITS == 64
7755#if defined(__NR_futex)
7756 /* always a 64-bit time_t, it doesn't define _time64 version */
7757 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7758#endif
7759#else /* HOST_LONG_BITS == 64 */
7760#if defined(__NR_futex_time64)
7761 if (sizeof(timeout->tv_sec) == 8) {
7762 /* _time64 function on 32bit arch */
7763 return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
7764 val3));
7765 }
7766#endif
7767#if defined(__NR_futex)
7768 /* old function on 32bit arch */
7769 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7770#endif
7771#endif /* HOST_LONG_BITS == 64 */
7772 return -TARGET_ENOSYS;
7773}
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02007774
pbrookbd0c5662008-05-29 14:34:11 +00007775/* ??? Using host futex calls even when target atomic operations
7776 are not really atomic probably breaks things. However implementing
7777 futexes locally would make futexes shared between multiple processes
7778 tricky. However they're probably useless because guest atomic
7779 operations won't work either. */
Alistair Francis859e8a82020-03-12 15:13:49 -07007780#if defined(TARGET_NR_futex)
Richard Henderson3e8f1622021-02-12 10:48:43 -08007781static int do_futex(CPUState *cpu, target_ulong uaddr, int op, int val,
7782 target_ulong timeout, target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00007783{
7784 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07007785 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00007786
7787 /* ??? We assume FUTEX_* constants are the same on both host
7788 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03007789#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07007790 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03007791#else
Nathan Froyda16aae02009-08-03 08:43:29 -07007792 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03007793#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07007794 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00007795 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07007796 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00007797 if (timeout) {
7798 pts = &ts;
7799 target_to_host_timespec(pts, timeout);
7800 } else {
7801 pts = NULL;
7802 }
Richard Henderson3e8f1622021-02-12 10:48:43 -08007803 return do_safe_futex(g2h(cpu, uaddr),
7804 op, tswap32(val), pts, NULL, val3);
pbrookbd0c5662008-05-29 14:34:11 +00007805 case FUTEX_WAKE:
Richard Henderson3e8f1622021-02-12 10:48:43 -08007806 return do_safe_futex(g2h(cpu, uaddr),
7807 op, val, NULL, NULL, 0);
pbrookbd0c5662008-05-29 14:34:11 +00007808 case FUTEX_FD:
Richard Henderson3e8f1622021-02-12 10:48:43 -08007809 return do_safe_futex(g2h(cpu, uaddr),
7810 op, val, NULL, NULL, 0);
pbrookbd0c5662008-05-29 14:34:11 +00007811 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00007812 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07007813 case FUTEX_WAKE_OP:
7814 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
7815 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
7816 But the prototype takes a `struct timespec *'; insert casts
7817 to satisfy the compiler. We do not need to tswap TIMEOUT
7818 since it's not compared to guest memory. */
7819 pts = (struct timespec *)(uintptr_t) timeout;
Richard Henderson3e8f1622021-02-12 10:48:43 -08007820 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, g2h(cpu, uaddr2),
Alistair Francis14690292020-03-18 15:47:01 -07007821 (base_op == FUTEX_CMP_REQUEUE
Richard Henderson3e8f1622021-02-12 10:48:43 -08007822 ? tswap32(val3) : val3));
pbrookbd0c5662008-05-29 14:34:11 +00007823 default:
7824 return -TARGET_ENOSYS;
7825 }
7826}
Alistair Francis859e8a82020-03-12 15:13:49 -07007827#endif
Alistair Francis14690292020-03-18 15:47:01 -07007828
7829#if defined(TARGET_NR_futex_time64)
Richard Henderson3e8f1622021-02-12 10:48:43 -08007830static int do_futex_time64(CPUState *cpu, target_ulong uaddr, int op,
7831 int val, target_ulong timeout,
Alistair Francis14690292020-03-18 15:47:01 -07007832 target_ulong uaddr2, int val3)
7833{
7834 struct timespec ts, *pts;
7835 int base_op;
7836
7837 /* ??? We assume FUTEX_* constants are the same on both host
7838 and target. */
7839#ifdef FUTEX_CMD_MASK
7840 base_op = op & FUTEX_CMD_MASK;
7841#else
7842 base_op = op;
7843#endif
7844 switch (base_op) {
7845 case FUTEX_WAIT:
7846 case FUTEX_WAIT_BITSET:
7847 if (timeout) {
7848 pts = &ts;
Alistair Francise4ce1782020-11-03 11:48:44 -08007849 if (target_to_host_timespec64(pts, timeout)) {
7850 return -TARGET_EFAULT;
7851 }
Alistair Francis14690292020-03-18 15:47:01 -07007852 } else {
7853 pts = NULL;
7854 }
Richard Henderson3e8f1622021-02-12 10:48:43 -08007855 return do_safe_futex(g2h(cpu, uaddr), op,
7856 tswap32(val), pts, NULL, val3);
Alistair Francis14690292020-03-18 15:47:01 -07007857 case FUTEX_WAKE:
Richard Henderson3e8f1622021-02-12 10:48:43 -08007858 return do_safe_futex(g2h(cpu, uaddr), op, val, NULL, NULL, 0);
Alistair Francis14690292020-03-18 15:47:01 -07007859 case FUTEX_FD:
Richard Henderson3e8f1622021-02-12 10:48:43 -08007860 return do_safe_futex(g2h(cpu, uaddr), op, val, NULL, NULL, 0);
Alistair Francis14690292020-03-18 15:47:01 -07007861 case FUTEX_REQUEUE:
7862 case FUTEX_CMP_REQUEUE:
7863 case FUTEX_WAKE_OP:
7864 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
7865 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
7866 But the prototype takes a `struct timespec *'; insert casts
7867 to satisfy the compiler. We do not need to tswap TIMEOUT
7868 since it's not compared to guest memory. */
7869 pts = (struct timespec *)(uintptr_t) timeout;
Richard Henderson3e8f1622021-02-12 10:48:43 -08007870 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, g2h(cpu, uaddr2),
Alistair Francis14690292020-03-18 15:47:01 -07007871 (base_op == FUTEX_CMP_REQUEUE
Richard Henderson3e8f1622021-02-12 10:48:43 -08007872 ? tswap32(val3) : val3));
Alistair Francis14690292020-03-18 15:47:01 -07007873 default:
7874 return -TARGET_ENOSYS;
7875 }
7876}
7877#endif
7878
Laurent Vivier0f0426f2015-09-01 22:27:33 +02007879#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7880static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7881 abi_long handle, abi_long mount_id,
7882 abi_long flags)
7883{
7884 struct file_handle *target_fh;
7885 struct file_handle *fh;
7886 int mid = 0;
7887 abi_long ret;
7888 char *name;
7889 unsigned int size, total_size;
7890
7891 if (get_user_s32(size, handle)) {
7892 return -TARGET_EFAULT;
7893 }
7894
7895 name = lock_user_string(pathname);
7896 if (!name) {
7897 return -TARGET_EFAULT;
7898 }
7899
7900 total_size = sizeof(struct file_handle) + size;
7901 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7902 if (!target_fh) {
7903 unlock_user(name, pathname, 0);
7904 return -TARGET_EFAULT;
7905 }
7906
7907 fh = g_malloc0(total_size);
7908 fh->handle_bytes = size;
7909
7910 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7911 unlock_user(name, pathname, 0);
7912
7913 /* man name_to_handle_at(2):
7914 * Other than the use of the handle_bytes field, the caller should treat
7915 * the file_handle structure as an opaque data type
7916 */
7917
7918 memcpy(target_fh, fh, total_size);
7919 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7920 target_fh->handle_type = tswap32(fh->handle_type);
7921 g_free(fh);
7922 unlock_user(target_fh, handle, total_size);
7923
7924 if (put_user_s32(mid, mount_id)) {
7925 return -TARGET_EFAULT;
7926 }
7927
7928 return ret;
7929
7930}
7931#endif
7932
7933#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7934static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7935 abi_long flags)
7936{
7937 struct file_handle *target_fh;
7938 struct file_handle *fh;
7939 unsigned int size, total_size;
7940 abi_long ret;
7941
7942 if (get_user_s32(size, handle)) {
7943 return -TARGET_EFAULT;
7944 }
7945
7946 total_size = sizeof(struct file_handle) + size;
7947 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7948 if (!target_fh) {
7949 return -TARGET_EFAULT;
7950 }
7951
Thomas Huthe9d49d52015-10-09 17:56:38 +02007952 fh = g_memdup(target_fh, total_size);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02007953 fh->handle_bytes = size;
7954 fh->handle_type = tswap32(target_fh->handle_type);
7955
7956 ret = get_errno(open_by_handle_at(mount_fd, fh,
7957 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7958
7959 g_free(fh);
7960
7961 unlock_user(target_fh, handle, total_size);
7962
7963 return ret;
7964}
7965#endif
pbrookbd0c5662008-05-29 14:34:11 +00007966
Laurent Viviere36800c2015-10-02 14:48:09 +02007967#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7968
Laurent Viviere36800c2015-10-02 14:48:09 +02007969static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7970{
7971 int host_flags;
7972 target_sigset_t *target_mask;
7973 sigset_t host_mask;
7974 abi_long ret;
7975
Helge Deller78721302021-02-10 07:12:14 +01007976 if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
Laurent Viviere36800c2015-10-02 14:48:09 +02007977 return -TARGET_EINVAL;
7978 }
7979 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7980 return -TARGET_EFAULT;
7981 }
7982
7983 target_to_host_sigset(&host_mask, target_mask);
7984
7985 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7986
7987 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7988 if (ret >= 0) {
7989 fd_trans_register(ret, &target_signalfd_trans);
7990 }
7991
7992 unlock_user_struct(target_mask, mask, 0);
7993
7994 return ret;
7995}
7996#endif
7997
pbrook1d9d8b52009-04-16 15:17:02 +00007998/* Map host to target signal numbers for the wait family of syscalls.
7999 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07008000int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00008001{
8002 if (WIFSIGNALED(status)) {
8003 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
8004 }
8005 if (WIFSTOPPED(status)) {
8006 return (host_to_target_signal(WSTOPSIG(status)) << 8)
8007 | (status & 0xff);
8008 }
8009 return status;
8010}
8011
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008012static int open_self_cmdline(void *cpu_env, int fd)
8013{
Richard Henderson29a0af62019-03-22 16:07:18 -07008014 CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
Andreas Schwab58de8b92017-03-20 12:31:55 +01008015 struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
8016 int i;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008017
Andreas Schwab58de8b92017-03-20 12:31:55 +01008018 for (i = 0; i < bprm->argc; i++) {
8019 size_t len = strlen(bprm->argv[i]) + 1;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008020
Andreas Schwab58de8b92017-03-20 12:31:55 +01008021 if (write(fd, bprm->argv[i], len) != len) {
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008022 return -1;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008023 }
8024 }
8025
Andreas Schwab58de8b92017-03-20 12:31:55 +01008026 return 0;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008027}
8028
Alexander Graf36c08d42011-11-02 20:23:24 +01008029static int open_self_maps(void *cpu_env, int fd)
8030{
Richard Henderson29a0af62019-03-22 16:07:18 -07008031 CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
Andreas Färber0429a972013-08-26 18:14:44 +02008032 TaskState *ts = cpu->opaque;
Alex Bennée01ef6b92020-04-03 20:11:46 +01008033 GSList *map_info = read_self_maps();
8034 GSList *s;
Alex Bennéebb551732020-04-03 20:11:47 +01008035 int count;
Alexander Graf36c08d42011-11-02 20:23:24 +01008036
Alex Bennée01ef6b92020-04-03 20:11:46 +01008037 for (s = map_info; s; s = g_slist_next(s)) {
8038 MapInfo *e = (MapInfo *) s->data;
Alexander Graf1a49ef22012-05-01 16:30:28 +01008039
Alex Bennée01ef6b92020-04-03 20:11:46 +01008040 if (h2g_valid(e->start)) {
8041 unsigned long min = e->start;
8042 unsigned long max = e->end;
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04008043 int flags = page_get_flags(h2g(min));
Alex Bennée01ef6b92020-04-03 20:11:46 +01008044 const char *path;
8045
8046 max = h2g_valid(max - 1) ?
Richard Henderson3e8f1622021-02-12 10:48:43 -08008047 max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1;
Alex Bennée01ef6b92020-04-03 20:11:46 +01008048
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04008049 if (page_check_range(h2g(min), max - min, flags) == -1) {
8050 continue;
8051 }
Alex Bennée01ef6b92020-04-03 20:11:46 +01008052
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04008053 if (h2g(min) == ts->info->stack_limit) {
Alex Bennéebb551732020-04-03 20:11:47 +01008054 path = "[stack]";
Alex Bennée01ef6b92020-04-03 20:11:46 +01008055 } else {
8056 path = e->path;
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04008057 }
Alex Bennée01ef6b92020-04-03 20:11:46 +01008058
Alex Bennéebb551732020-04-03 20:11:47 +01008059 count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
8060 " %c%c%c%c %08" PRIx64 " %s %"PRId64,
8061 h2g(min), h2g(max - 1) + 1,
Nicolas Surbayrole08f3a962021-03-08 10:19:59 +01008062 (flags & PAGE_READ) ? 'r' : '-',
8063 (flags & PAGE_WRITE_ORG) ? 'w' : '-',
8064 (flags & PAGE_EXEC) ? 'x' : '-',
Andrey Kazmine13685a2021-12-27 15:50:48 +03008065 e->is_priv ? 'p' : 's',
Alex Bennéebb551732020-04-03 20:11:47 +01008066 (uint64_t) e->offset, e->dev, e->inode);
8067 if (path) {
8068 dprintf(fd, "%*s%s\n", 73 - count, "", path);
8069 } else {
8070 dprintf(fd, "\n");
8071 }
Alexander Graf1a49ef22012-05-01 16:30:28 +01008072 }
8073 }
8074
Alex Bennée01ef6b92020-04-03 20:11:46 +01008075 free_self_maps(map_info);
8076
Richard Hendersonbf02adc2020-02-12 19:22:22 -08008077#ifdef TARGET_VSYSCALL_PAGE
8078 /*
8079 * We only support execution from the vsyscall page.
8080 * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
8081 */
Alex Bennéebb551732020-04-03 20:11:47 +01008082 count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
8083 " --xp 00000000 00:00 0",
8084 TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
8085 dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
Richard Hendersonbf02adc2020-02-12 19:22:22 -08008086#endif
8087
Alexander Graf36c08d42011-11-02 20:23:24 +01008088 return 0;
8089}
8090
Alexander Graf480b8e72011-11-02 20:23:25 +01008091static int open_self_stat(void *cpu_env, int fd)
8092{
Richard Henderson29a0af62019-03-22 16:07:18 -07008093 CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
Andreas Färber0429a972013-08-26 18:14:44 +02008094 TaskState *ts = cpu->opaque;
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008095 g_autoptr(GString) buf = g_string_new(NULL);
Alexander Graf480b8e72011-11-02 20:23:25 +01008096 int i;
8097
8098 for (i = 0; i < 44; i++) {
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008099 if (i == 0) {
8100 /* pid */
8101 g_string_printf(buf, FMT_pid " ", getpid());
8102 } else if (i == 1) {
8103 /* app name */
8104 gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
8105 bin = bin ? bin + 1 : ts->bprm->argv[0];
8106 g_string_printf(buf, "(%.15s) ", bin);
Andreas Schwab7aa9fe32021-06-21 11:32:59 +02008107 } else if (i == 3) {
8108 /* ppid */
8109 g_string_printf(buf, FMT_pid " ", getppid());
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008110 } else if (i == 27) {
8111 /* stack bottom */
8112 g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
8113 } else {
8114 /* for the rest, there is MasterCard */
8115 g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
8116 }
Alexander Graf480b8e72011-11-02 20:23:25 +01008117
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008118 if (write(fd, buf->str, buf->len) != buf->len) {
8119 return -1;
8120 }
Alexander Graf480b8e72011-11-02 20:23:25 +01008121 }
8122
8123 return 0;
8124}
8125
Alexander Graf257450e2011-11-02 20:23:26 +01008126static int open_self_auxv(void *cpu_env, int fd)
8127{
Richard Henderson29a0af62019-03-22 16:07:18 -07008128 CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
Andreas Färber0429a972013-08-26 18:14:44 +02008129 TaskState *ts = cpu->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01008130 abi_ulong auxv = ts->info->saved_auxv;
8131 abi_ulong len = ts->info->auxv_len;
8132 char *ptr;
8133
8134 /*
8135 * Auxiliary vector is stored in target process stack.
8136 * read in whole auxv vector and copy it to file
8137 */
8138 ptr = lock_user(VERIFY_READ, auxv, len, 0);
8139 if (ptr != NULL) {
8140 while (len > 0) {
8141 ssize_t r;
8142 r = write(fd, ptr, len);
8143 if (r <= 0) {
8144 break;
8145 }
8146 len -= r;
8147 ptr += r;
8148 }
8149 lseek(fd, 0, SEEK_SET);
8150 unlock_user(ptr, auxv, len);
8151 }
8152
8153 return 0;
8154}
8155
Andreas Schwab463d8e72013-07-02 14:04:12 +01008156static int is_proc_myself(const char *filename, const char *entry)
8157{
8158 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
8159 filename += strlen("/proc/");
8160 if (!strncmp(filename, "self/", strlen("self/"))) {
8161 filename += strlen("self/");
8162 } else if (*filename >= '1' && *filename <= '9') {
8163 char myself[80];
8164 snprintf(myself, sizeof(myself), "%d/", getpid());
8165 if (!strncmp(filename, myself, strlen(myself))) {
8166 filename += strlen(myself);
8167 } else {
8168 return 0;
8169 }
8170 } else {
8171 return 0;
8172 }
8173 if (!strcmp(filename, entry)) {
8174 return 1;
8175 }
8176 }
8177 return 0;
8178}
8179
Laurent Vivierfff69382019-05-17 15:31:48 +02008180#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
Helge Deller93a56612020-04-24 23:06:48 +02008181 defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
Laurent Vivierde6b9932013-08-30 01:46:40 +02008182static int is_proc(const char *filename, const char *entry)
8183{
8184 return strcmp(filename, entry) == 0;
8185}
Laurent Vivierfff69382019-05-17 15:31:48 +02008186#endif
Laurent Vivierde6b9932013-08-30 01:46:40 +02008187
Laurent Vivierfff69382019-05-17 15:31:48 +02008188#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
Laurent Vivierde6b9932013-08-30 01:46:40 +02008189static int open_net_route(void *cpu_env, int fd)
8190{
8191 FILE *fp;
8192 char *line = NULL;
8193 size_t len = 0;
8194 ssize_t read;
8195
8196 fp = fopen("/proc/net/route", "r");
8197 if (fp == NULL) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01008198 return -1;
Laurent Vivierde6b9932013-08-30 01:46:40 +02008199 }
8200
8201 /* read header */
8202
8203 read = getline(&line, &len, fp);
8204 dprintf(fd, "%s", line);
8205
8206 /* read routes */
8207
8208 while ((read = getline(&line, &len, fp)) != -1) {
8209 char iface[16];
8210 uint32_t dest, gw, mask;
8211 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
Peter Maydell9d0bd0c2019-02-05 17:42:07 +00008212 int fields;
8213
8214 fields = sscanf(line,
8215 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8216 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
8217 &mask, &mtu, &window, &irtt);
8218 if (fields != 11) {
8219 continue;
8220 }
Laurent Vivierde6b9932013-08-30 01:46:40 +02008221 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8222 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
8223 metric, tswap32(mask), mtu, window, irtt);
8224 }
8225
8226 free(line);
8227 fclose(fp);
8228
8229 return 0;
8230}
8231#endif
8232
Laurent Vivierfff69382019-05-17 15:31:48 +02008233#if defined(TARGET_SPARC)
8234static int open_cpuinfo(void *cpu_env, int fd)
8235{
8236 dprintf(fd, "type\t\t: sun4u\n");
8237 return 0;
8238}
8239#endif
8240
Helge Deller93a56612020-04-24 23:06:48 +02008241#if defined(TARGET_HPPA)
8242static int open_cpuinfo(void *cpu_env, int fd)
8243{
8244 dprintf(fd, "cpu family\t: PA-RISC 1.1e\n");
8245 dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n");
8246 dprintf(fd, "capabilities\t: os32\n");
8247 dprintf(fd, "model\t\t: 9000/778/B160L\n");
8248 dprintf(fd, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n");
8249 return 0;
8250}
8251#endif
8252
Laurent Vivier4ab67132019-05-17 15:31:49 +02008253#if defined(TARGET_M68K)
8254static int open_hardware(void *cpu_env, int fd)
8255{
8256 dprintf(fd, "Model:\t\tqemu-m68k\n");
8257 return 0;
8258}
8259#endif
8260
Riku Voipio0b2effd2014-08-06 10:36:37 +03008261static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
Alexander Graf3be14d02011-11-02 20:23:23 +01008262{
8263 struct fake_open {
8264 const char *filename;
8265 int (*fill)(void *cpu_env, int fd);
Laurent Vivierde6b9932013-08-30 01:46:40 +02008266 int (*cmp)(const char *s1, const char *s2);
Alexander Graf3be14d02011-11-02 20:23:23 +01008267 };
8268 const struct fake_open *fake_open;
8269 static const struct fake_open fakes[] = {
Laurent Vivierde6b9932013-08-30 01:46:40 +02008270 { "maps", open_self_maps, is_proc_myself },
8271 { "stat", open_self_stat, is_proc_myself },
8272 { "auxv", open_self_auxv, is_proc_myself },
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008273 { "cmdline", open_self_cmdline, is_proc_myself },
Laurent Vivierde6b9932013-08-30 01:46:40 +02008274#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
8275 { "/proc/net/route", open_net_route, is_proc },
8276#endif
Helge Deller93a56612020-04-24 23:06:48 +02008277#if defined(TARGET_SPARC) || defined(TARGET_HPPA)
Laurent Vivierfff69382019-05-17 15:31:48 +02008278 { "/proc/cpuinfo", open_cpuinfo, is_proc },
8279#endif
Laurent Vivier4ab67132019-05-17 15:31:49 +02008280#if defined(TARGET_M68K)
8281 { "/proc/hardware", open_hardware, is_proc },
8282#endif
Laurent Vivierde6b9932013-08-30 01:46:40 +02008283 { NULL, NULL, NULL }
Alexander Graf3be14d02011-11-02 20:23:23 +01008284 };
8285
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03008286 if (is_proc_myself(pathname, "exe")) {
8287 int execfd = qemu_getauxval(AT_EXECFD);
Timothy E Baldwinc10a0732016-05-12 18:47:48 +01008288 return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03008289 }
8290
Alexander Graf3be14d02011-11-02 20:23:23 +01008291 for (fake_open = fakes; fake_open->filename; fake_open++) {
Laurent Vivierde6b9932013-08-30 01:46:40 +02008292 if (fake_open->cmp(pathname, fake_open->filename)) {
Alexander Graf3be14d02011-11-02 20:23:23 +01008293 break;
8294 }
8295 }
8296
8297 if (fake_open->filename) {
8298 const char *tmpdir;
8299 char filename[PATH_MAX];
8300 int fd, r;
8301
8302 /* create temporary file to map stat to */
8303 tmpdir = getenv("TMPDIR");
8304 if (!tmpdir)
8305 tmpdir = "/tmp";
8306 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
8307 fd = mkstemp(filename);
8308 if (fd < 0) {
8309 return fd;
8310 }
8311 unlink(filename);
8312
8313 if ((r = fake_open->fill(cpu_env, fd))) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01008314 int e = errno;
Alexander Graf3be14d02011-11-02 20:23:23 +01008315 close(fd);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01008316 errno = e;
Alexander Graf3be14d02011-11-02 20:23:23 +01008317 return r;
8318 }
8319 lseek(fd, 0, SEEK_SET);
8320
8321 return fd;
8322 }
8323
Timothy E Baldwinc10a0732016-05-12 18:47:48 +01008324 return safe_openat(dirfd, path(pathname), flags, mode);
Alexander Graf3be14d02011-11-02 20:23:23 +01008325}
8326
Alexander Grafaecc8862014-11-10 21:33:03 +01008327#define TIMER_MAGIC 0x0caf0000
8328#define TIMER_MAGIC_MASK 0xffff0000
8329
8330/* Convert QEMU provided timer ID back to internal 16bit index format */
8331static target_timer_t get_timer_id(abi_long arg)
8332{
8333 target_timer_t timerid = arg;
8334
8335 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
8336 return -TARGET_EINVAL;
8337 }
8338
8339 timerid &= 0xffff;
8340
8341 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
8342 return -TARGET_EINVAL;
8343 }
8344
8345 return timerid;
8346}
8347
Samuel Thibault2e0a8712018-01-09 21:16:43 +01008348static int target_to_host_cpu_mask(unsigned long *host_mask,
8349 size_t host_size,
8350 abi_ulong target_addr,
8351 size_t target_size)
8352{
8353 unsigned target_bits = sizeof(abi_ulong) * 8;
8354 unsigned host_bits = sizeof(*host_mask) * 8;
8355 abi_ulong *target_mask;
8356 unsigned i, j;
8357
8358 assert(host_size >= target_size);
8359
8360 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
8361 if (!target_mask) {
8362 return -TARGET_EFAULT;
8363 }
8364 memset(host_mask, 0, host_size);
8365
8366 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8367 unsigned bit = i * target_bits;
8368 abi_ulong val;
8369
8370 __get_user(val, &target_mask[i]);
8371 for (j = 0; j < target_bits; j++, bit++) {
8372 if (val & (1UL << j)) {
8373 host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
8374 }
8375 }
8376 }
8377
8378 unlock_user(target_mask, target_addr, 0);
8379 return 0;
8380}
8381
8382static int host_to_target_cpu_mask(const unsigned long *host_mask,
8383 size_t host_size,
8384 abi_ulong target_addr,
8385 size_t target_size)
8386{
8387 unsigned target_bits = sizeof(abi_ulong) * 8;
8388 unsigned host_bits = sizeof(*host_mask) * 8;
8389 abi_ulong *target_mask;
8390 unsigned i, j;
8391
8392 assert(host_size >= target_size);
8393
8394 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
8395 if (!target_mask) {
8396 return -TARGET_EFAULT;
8397 }
8398
8399 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8400 unsigned bit = i * target_bits;
8401 abi_ulong val = 0;
8402
8403 for (j = 0; j < target_bits; j++, bit++) {
8404 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
8405 val |= 1UL << j;
8406 }
8407 }
8408 __put_user(val, &target_mask[i]);
8409 }
8410
8411 unlock_user(target_mask, target_addr, target_size);
8412 return 0;
8413}
8414
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008415#ifdef TARGET_NR_getdents
Richard Hendersonaee14c72021-11-14 11:35:39 +01008416static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008417{
Richard Hendersonaee14c72021-11-14 11:35:39 +01008418 g_autofree void *hdirp = NULL;
8419 void *tdirp;
8420 int hlen, hoff, toff;
8421 int hreclen, treclen;
8422 off64_t prev_diroff = 0;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008423
Richard Hendersonaee14c72021-11-14 11:35:39 +01008424 hdirp = g_try_malloc(count);
8425 if (!hdirp) {
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008426 return -TARGET_ENOMEM;
8427 }
8428
Richard Hendersonaee14c72021-11-14 11:35:39 +01008429#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8430 hlen = sys_getdents(dirfd, hdirp, count);
8431#else
8432 hlen = sys_getdents64(dirfd, hdirp, count);
8433#endif
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008434
Richard Hendersonaee14c72021-11-14 11:35:39 +01008435 hlen = get_errno(hlen);
8436 if (is_error(hlen)) {
8437 return hlen;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008438 }
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008439
Richard Hendersonaee14c72021-11-14 11:35:39 +01008440 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8441 if (!tdirp) {
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008442 return -TARGET_EFAULT;
8443 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008444
8445 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8446#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8447 struct linux_dirent *hde = hdirp + hoff;
8448#else
8449 struct linux_dirent64 *hde = hdirp + hoff;
8450#endif
8451 struct target_dirent *tde = tdirp + toff;
8452 int namelen;
8453 uint8_t type;
8454
8455 namelen = strlen(hde->d_name);
8456 hreclen = hde->d_reclen;
8457 treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
8458 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
8459
8460 if (toff + treclen > count) {
8461 /*
8462 * If the host struct is smaller than the target struct, or
8463 * requires less alignment and thus packs into less space,
8464 * then the host can return more entries than we can pass
8465 * on to the guest.
8466 */
8467 if (toff == 0) {
8468 toff = -TARGET_EINVAL; /* result buffer is too small */
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008469 break;
8470 }
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008471 /*
Richard Hendersonaee14c72021-11-14 11:35:39 +01008472 * Return what we have, resetting the file pointer to the
8473 * location of the first record not returned.
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008474 */
Richard Hendersonaee14c72021-11-14 11:35:39 +01008475 lseek64(dirfd, prev_diroff, SEEK_SET);
8476 break;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008477 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008478
8479 prev_diroff = hde->d_off;
8480 tde->d_ino = tswapal(hde->d_ino);
8481 tde->d_off = tswapal(hde->d_off);
8482 tde->d_reclen = tswap16(treclen);
8483 memcpy(tde->d_name, hde->d_name, namelen + 1);
8484
8485 /*
8486 * The getdents type is in what was formerly a padding byte at the
8487 * end of the structure.
8488 */
8489#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8490 type = *((uint8_t *)hde + hreclen - 1);
8491#else
8492 type = hde->d_type;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008493#endif
Richard Hendersonaee14c72021-11-14 11:35:39 +01008494 *((uint8_t *)tde + treclen - 1) = type;
8495 }
8496
8497 unlock_user(tdirp, arg2, toff);
8498 return toff;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008499}
8500#endif /* TARGET_NR_getdents */
8501
8502#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
Richard Hendersonaee14c72021-11-14 11:35:39 +01008503static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008504{
Richard Hendersonaee14c72021-11-14 11:35:39 +01008505 g_autofree void *hdirp = NULL;
8506 void *tdirp;
8507 int hlen, hoff, toff;
8508 int hreclen, treclen;
8509 off64_t prev_diroff = 0;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008510
Richard Hendersonaee14c72021-11-14 11:35:39 +01008511 hdirp = g_try_malloc(count);
8512 if (!hdirp) {
8513 return -TARGET_ENOMEM;
8514 }
8515
8516 hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
8517 if (is_error(hlen)) {
8518 return hlen;
8519 }
8520
8521 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8522 if (!tdirp) {
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008523 return -TARGET_EFAULT;
8524 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008525
8526 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8527 struct linux_dirent64 *hde = hdirp + hoff;
8528 struct target_dirent64 *tde = tdirp + toff;
8529 int namelen;
8530
8531 namelen = strlen(hde->d_name) + 1;
8532 hreclen = hde->d_reclen;
8533 treclen = offsetof(struct target_dirent64, d_name) + namelen;
8534 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
8535
8536 if (toff + treclen > count) {
8537 /*
8538 * If the host struct is smaller than the target struct, or
8539 * requires less alignment and thus packs into less space,
8540 * then the host can return more entries than we can pass
8541 * on to the guest.
8542 */
8543 if (toff == 0) {
8544 toff = -TARGET_EINVAL; /* result buffer is too small */
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008545 break;
8546 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008547 /*
8548 * Return what we have, resetting the file pointer to the
8549 * location of the first record not returned.
8550 */
8551 lseek64(dirfd, prev_diroff, SEEK_SET);
8552 break;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008553 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008554
8555 prev_diroff = hde->d_off;
8556 tde->d_ino = tswap64(hde->d_ino);
8557 tde->d_off = tswap64(hde->d_off);
8558 tde->d_reclen = tswap16(treclen);
8559 tde->d_type = hde->d_type;
8560 memcpy(tde->d_name, hde->d_name, namelen);
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008561 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008562
8563 unlock_user(tdirp, arg2, toff);
8564 return toff;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008565}
8566#endif /* TARGET_NR_getdents64 */
8567
YAMAMOTO Takashie10fbe82021-05-31 14:50:12 +09008568#if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
8569_syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
8570#endif
8571
Richard Hendersondc1ce182018-08-18 12:01:04 -07008572/* This is an internal helper for do_syscall so that it is easier
8573 * to have a single return point, so that actions, such as logging
8574 * of syscall results, can be performed.
8575 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
8576 */
8577static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
8578 abi_long arg2, abi_long arg3, abi_long arg4,
8579 abi_long arg5, abi_long arg6, abi_long arg7,
8580 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00008581{
Richard Henderson29a0af62019-03-22 16:07:18 -07008582 CPUState *cpu = env_cpu(cpu_env);
blueswir1992f48a2007-10-14 16:27:31 +00008583 abi_long ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008584#if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
8585 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02008586 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
8587 || defined(TARGET_NR_statx)
bellard31e31b82003-02-18 22:55:36 +00008588 struct stat st;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008589#endif
8590#if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
8591 || defined(TARGET_NR_fstatfs)
bellard56c8f682005-11-28 22:28:41 +00008592 struct statfs stfs;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008593#endif
pbrook53a59602006-03-25 19:31:22 +00008594 void *p;
ths3b46e622007-09-17 08:09:54 +00008595
bellard31e31b82003-02-18 22:55:36 +00008596 switch(num) {
8597 case TARGET_NR_exit:
Andreas Färber9b056fc2013-06-24 23:53:10 +02008598 /* In old applications this may be used to implement _exit(2).
zhaolichang6f9ff552020-09-17 15:50:25 +08008599 However in threaded applications it is used for thread termination,
Andreas Färber9b056fc2013-06-24 23:53:10 +02008600 and _exit_group is used for application termination.
8601 Do thread termination if we have more then one thread. */
Timothy E Baldwina0995882016-05-27 15:51:56 +01008602
8603 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01008604 return -QEMU_ERESTARTSYS;
Timothy E Baldwina0995882016-05-27 15:51:56 +01008605 }
8606
Alex Bennée1f81ce92020-05-20 15:05:39 +01008607 pthread_mutex_lock(&clone_lock);
Alex Bennéedd1f6342016-09-30 22:31:01 +01008608
Andreas Färberbdc44642013-06-24 23:50:24 +02008609 if (CPU_NEXT(first_cpu)) {
Alex Bennée1f81ce92020-05-20 15:05:39 +01008610 TaskState *ts = cpu->opaque;
pbrookc2764712009-03-07 15:24:59 +00008611
Markus Armbruster5325cc32020-07-07 18:05:54 +02008612 object_property_set_bool(OBJECT(cpu), "realized", false, NULL);
Alex Bennée1f81ce92020-05-20 15:05:39 +01008613 object_unref(OBJECT(cpu));
8614 /*
8615 * At this point the CPU should be unrealized and removed
8616 * from cpu lists. We can clean-up the rest of the thread
8617 * data without the lock held.
8618 */
Alex Bennéedd1f6342016-09-30 22:31:01 +01008619
Alex Bennée1f81ce92020-05-20 15:05:39 +01008620 pthread_mutex_unlock(&clone_lock);
Alex Bennéedd1f6342016-09-30 22:31:01 +01008621
Andreas Färber9b056fc2013-06-24 23:53:10 +02008622 if (ts->child_tidptr) {
8623 put_user_u32(0, ts->child_tidptr);
Richard Henderson3e8f1622021-02-12 10:48:43 -08008624 do_sys_futex(g2h(cpu, ts->child_tidptr),
8625 FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
Andreas Färber9b056fc2013-06-24 23:53:10 +02008626 }
Andreas Färbera2247f82013-06-09 19:47:04 +02008627 thread_cpu = NULL;
Andreas Färber9b056fc2013-06-24 23:53:10 +02008628 g_free(ts);
Emilio G. Cota70903762015-08-23 20:23:41 -04008629 rcu_unregister_thread();
Andreas Färber9b056fc2013-06-24 23:53:10 +02008630 pthread_exit(NULL);
8631 }
Alex Bennéedd1f6342016-09-30 22:31:01 +01008632
Alex Bennée1f81ce92020-05-20 15:05:39 +01008633 pthread_mutex_unlock(&clone_lock);
Alex Bennée708b6a62018-06-22 17:09:10 +01008634 preexit_cleanup(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00008635 _exit(arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008636 return 0; /* avoid warning */
bellard31e31b82003-02-18 22:55:36 +00008637 case TARGET_NR_read:
Andreas Schwabba584f12019-03-05 17:45:05 +01008638 if (arg2 == 0 && arg3 == 0) {
8639 return get_errno(safe_read(arg1, 0, 0));
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008640 } else {
aurel3238d840e2009-01-30 19:48:17 +00008641 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008642 return -TARGET_EFAULT;
Timothy E Baldwin50afd022016-05-12 18:47:47 +01008643 ret = get_errno(safe_read(arg1, p, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02008644 if (ret >= 0 &&
Laurent Vivier5d4d3662015-10-28 21:40:43 +01008645 fd_trans_host_to_target_data(arg1)) {
8646 ret = fd_trans_host_to_target_data(arg1)(p, ret);
Laurent Viviere36800c2015-10-02 14:48:09 +02008647 }
aurel3238d840e2009-01-30 19:48:17 +00008648 unlock_user(p, arg2, ret);
8649 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008650 return ret;
bellard31e31b82003-02-18 22:55:36 +00008651 case TARGET_NR_write:
Tony Garnock-Jones58cfa6c2018-09-08 19:22:05 +01008652 if (arg2 == 0 && arg3 == 0) {
8653 return get_errno(safe_write(arg1, 0, 0));
8654 }
bellard579a97f2007-11-11 14:26:47 +00008655 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008656 return -TARGET_EFAULT;
Laurent Vivier04b9bcf2017-03-01 10:37:47 +01008657 if (fd_trans_target_to_host_data(arg1)) {
8658 void *copy = g_malloc(arg3);
8659 memcpy(copy, p, arg3);
8660 ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
8661 if (ret >= 0) {
8662 ret = get_errno(safe_write(arg1, copy, ret));
8663 }
8664 g_free(copy);
8665 } else {
8666 ret = get_errno(safe_write(arg1, p, arg3));
8667 }
pbrook53a59602006-03-25 19:31:22 +00008668 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008669 return ret;
8670
Chen Gang704eff62015-08-21 05:37:33 +08008671#ifdef TARGET_NR_open
bellard31e31b82003-02-18 22:55:36 +00008672 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00008673 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008674 return -TARGET_EFAULT;
Riku Voipio0b2effd2014-08-06 10:36:37 +03008675 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
8676 target_to_host_bitmask(arg2, fcntl_flags_tbl),
8677 arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02008678 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00008679 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008680 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08008681#endif
ths82424832007-09-24 09:21:55 +00008682 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00008683 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008684 return -TARGET_EFAULT;
Riku Voipio0b2effd2014-08-06 10:36:37 +03008685 ret = get_errno(do_openat(cpu_env, arg1, p,
8686 target_to_host_bitmask(arg3, fcntl_flags_tbl),
8687 arg4));
Laurent Viviere36800c2015-10-02 14:48:09 +02008688 fd_trans_unregister(ret);
bellard579a97f2007-11-11 14:26:47 +00008689 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008690 return ret;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02008691#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
8692 case TARGET_NR_name_to_handle_at:
8693 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008694 return ret;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02008695#endif
8696#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
8697 case TARGET_NR_open_by_handle_at:
8698 ret = do_open_by_handle_at(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02008699 fd_trans_unregister(ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008700 return ret;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02008701#endif
bellard31e31b82003-02-18 22:55:36 +00008702 case TARGET_NR_close:
Laurent Viviere36800c2015-10-02 14:48:09 +02008703 fd_trans_unregister(arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008704 return get_errno(close(arg1));
8705
bellard31e31b82003-02-18 22:55:36 +00008706 case TARGET_NR_brk:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008707 return do_brk(arg1);
Chen Gang704eff62015-08-21 05:37:33 +08008708#ifdef TARGET_NR_fork
bellard31e31b82003-02-18 22:55:36 +00008709 case TARGET_NR_fork:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008710 return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
Chen Gang704eff62015-08-21 05:37:33 +08008711#endif
thse5febef2007-04-01 18:31:35 +00008712#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00008713 case TARGET_NR_waitpid:
8714 {
pbrook53a59602006-03-25 19:31:22 +00008715 int status;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01008716 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
Alexander Graf53795572011-11-24 00:44:43 +01008717 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00008718 && put_user_s32(host_to_target_waitstatus(status), arg2))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008719 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00008720 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008721 return ret;
thse5febef2007-04-01 18:31:35 +00008722#endif
pbrookf0cbb612008-05-30 18:20:05 +00008723#ifdef TARGET_NR_waitid
8724 case TARGET_NR_waitid:
8725 {
8726 siginfo_t info;
8727 info.si_pid = 0;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01008728 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
pbrookf0cbb612008-05-30 18:20:05 +00008729 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05008730 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008731 return -TARGET_EFAULT;
pbrookf0cbb612008-05-30 18:20:05 +00008732 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05008733 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00008734 }
8735 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008736 return ret;
pbrookf0cbb612008-05-30 18:20:05 +00008737#endif
j_mayer7a3148a2007-04-05 07:13:51 +00008738#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00008739 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00008740 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008741 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00008742 ret = get_errno(creat(p, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02008743 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00008744 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008745 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00008746#endif
Chen Gang704eff62015-08-21 05:37:33 +08008747#ifdef TARGET_NR_link
bellard31e31b82003-02-18 22:55:36 +00008748 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00008749 {
8750 void * p2;
8751 p = lock_user_string(arg1);
8752 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00008753 if (!p || !p2)
8754 ret = -TARGET_EFAULT;
8755 else
8756 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00008757 unlock_user(p2, arg2, 0);
8758 unlock_user(p, arg1, 0);
8759 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008760 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08008761#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01008762#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00008763 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00008764 {
8765 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00008766 if (!arg2 || !arg4)
Richard Henderson2852aaf2018-08-18 12:01:06 -07008767 return -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00008768 p = lock_user_string(arg2);
8769 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00008770 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00008771 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00008772 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01008773 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00008774 unlock_user(p, arg2, 0);
8775 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00008776 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008777 return ret;
ths64f0ce42007-09-24 09:25:06 +00008778#endif
Chen Gang704eff62015-08-21 05:37:33 +08008779#ifdef TARGET_NR_unlink
bellard31e31b82003-02-18 22:55:36 +00008780 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00008781 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008782 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00008783 ret = get_errno(unlink(p));
8784 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008785 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08008786#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01008787#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00008788 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00008789 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008790 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008791 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00008792 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008793 return ret;
balrogb7d35e62007-12-12 00:40:24 +00008794#endif
bellard31e31b82003-02-18 22:55:36 +00008795 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00008796 {
8797 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00008798 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00008799 abi_ulong gp;
8800 abi_ulong guest_argp;
8801 abi_ulong guest_envp;
8802 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00008803 char **q;
8804
bellardf7341ff2003-03-30 21:00:25 +00008805 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00008806 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00008807 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00008808 if (get_user_ual(addr, gp))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008809 return -TARGET_EFAULT;
ths03aa1972007-12-02 06:28:08 +00008810 if (!addr)
bellard2f619692007-11-16 10:46:05 +00008811 break;
bellard7854b052003-03-29 17:22:23 +00008812 argc++;
bellard2f619692007-11-16 10:46:05 +00008813 }
bellardf7341ff2003-03-30 21:00:25 +00008814 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00008815 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00008816 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00008817 if (get_user_ual(addr, gp))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008818 return -TARGET_EFAULT;
ths03aa1972007-12-02 06:28:08 +00008819 if (!addr)
bellard2f619692007-11-16 10:46:05 +00008820 break;
bellard7854b052003-03-29 17:22:23 +00008821 envc++;
bellard2f619692007-11-16 10:46:05 +00008822 }
bellard7854b052003-03-29 17:22:23 +00008823
Prasad J Panditb936cb52017-03-07 12:51:47 +05308824 argp = g_new0(char *, argc + 1);
8825 envp = g_new0(char *, envc + 1);
bellard7854b052003-03-29 17:22:23 +00008826
pbrookda94d262008-05-30 18:24:00 +00008827 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00008828 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00008829 if (get_user_ual(addr, gp))
8830 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00008831 if (!addr)
8832 break;
bellard2f619692007-11-16 10:46:05 +00008833 if (!(*q = lock_user_string(addr)))
8834 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00008835 }
bellardf7341ff2003-03-30 21:00:25 +00008836 *q = NULL;
8837
pbrookda94d262008-05-30 18:24:00 +00008838 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00008839 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00008840 if (get_user_ual(addr, gp))
8841 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00008842 if (!addr)
8843 break;
bellard2f619692007-11-16 10:46:05 +00008844 if (!(*q = lock_user_string(addr)))
8845 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00008846 }
bellardf7341ff2003-03-30 21:00:25 +00008847 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00008848
bellard2f619692007-11-16 10:46:05 +00008849 if (!(p = lock_user_string(arg1)))
8850 goto execve_efault;
Timothy E Baldwinffdcbe22016-05-12 18:47:50 +01008851 /* Although execve() is not an interruptible syscall it is
8852 * a special case where we must use the safe_syscall wrapper:
8853 * if we allow a signal to happen before we make the host
8854 * syscall then we will 'lose' it, because at the point of
8855 * execve the process leaves QEMU's control. So we use the
8856 * safe syscall wrapper to ensure that we either take the
8857 * signal as a guest signal, or else it does not happen
8858 * before the execve completes and makes it the other
8859 * program's problem.
8860 */
8861 ret = get_errno(safe_execve(p, argp, envp));
pbrook53a59602006-03-25 19:31:22 +00008862 unlock_user(p, arg1, 0);
8863
bellard2f619692007-11-16 10:46:05 +00008864 goto execve_end;
8865
8866 execve_efault:
8867 ret = -TARGET_EFAULT;
8868
8869 execve_end:
pbrook53a59602006-03-25 19:31:22 +00008870 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00008871 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00008872 if (get_user_ual(addr, gp)
8873 || !addr)
8874 break;
pbrook53a59602006-03-25 19:31:22 +00008875 unlock_user(*q, addr, 0);
8876 }
8877 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00008878 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00008879 if (get_user_ual(addr, gp)
8880 || !addr)
8881 break;
pbrook53a59602006-03-25 19:31:22 +00008882 unlock_user(*q, addr, 0);
8883 }
Prasad J Panditb936cb52017-03-07 12:51:47 +05308884
8885 g_free(argp);
8886 g_free(envp);
bellard7854b052003-03-29 17:22:23 +00008887 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008888 return ret;
bellard31e31b82003-02-18 22:55:36 +00008889 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00008890 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008891 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00008892 ret = get_errno(chdir(p));
8893 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008894 return ret;
bellarda315a142005-01-30 22:59:18 +00008895#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00008896 case TARGET_NR_time:
8897 {
pbrook53a59602006-03-25 19:31:22 +00008898 time_t host_time;
8899 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00008900 if (!is_error(ret)
8901 && arg1
8902 && put_user_sal(host_time, arg1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008903 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00008904 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008905 return ret;
bellarda315a142005-01-30 22:59:18 +00008906#endif
Chen Gang704eff62015-08-21 05:37:33 +08008907#ifdef TARGET_NR_mknod
bellard31e31b82003-02-18 22:55:36 +00008908 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00008909 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008910 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00008911 ret = get_errno(mknod(p, arg2, arg3));
8912 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008913 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08008914#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01008915#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00008916 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00008917 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008918 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008919 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00008920 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008921 return ret;
ths75ac37a2007-09-24 09:23:05 +00008922#endif
Chen Gang704eff62015-08-21 05:37:33 +08008923#ifdef TARGET_NR_chmod
bellard31e31b82003-02-18 22:55:36 +00008924 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00008925 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07008926 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00008927 ret = get_errno(chmod(p, arg2));
8928 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008929 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08008930#endif
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008931#ifdef TARGET_NR_lseek
bellard31e31b82003-02-18 22:55:36 +00008932 case TARGET_NR_lseek:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008933 return get_errno(lseek(arg1, arg2, arg3));
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008934#endif
Richard Henderson92317332010-05-03 10:07:53 -07008935#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
8936 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00008937 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07008938 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008939 return get_errno(getpid());
Richard Henderson92317332010-05-03 10:07:53 -07008940#endif
8941#ifdef TARGET_NR_getpid
8942 case TARGET_NR_getpid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008943 return get_errno(getpid());
Richard Henderson92317332010-05-03 10:07:53 -07008944#endif
bellard31e31b82003-02-18 22:55:36 +00008945 case TARGET_NR_mount:
Paul Burton356d7712014-06-22 11:25:37 +01008946 {
8947 /* need to look at the data field */
8948 void *p2, *p3;
8949
8950 if (arg1) {
8951 p = lock_user_string(arg1);
8952 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07008953 return -TARGET_EFAULT;
Paul Burton356d7712014-06-22 11:25:37 +01008954 }
8955 } else {
8956 p = NULL;
8957 }
8958
8959 p2 = lock_user_string(arg2);
8960 if (!p2) {
8961 if (arg1) {
8962 unlock_user(p, arg1, 0);
8963 }
Richard Henderson2852aaf2018-08-18 12:01:06 -07008964 return -TARGET_EFAULT;
Paul Burton356d7712014-06-22 11:25:37 +01008965 }
8966
8967 if (arg3) {
8968 p3 = lock_user_string(arg3);
8969 if (!p3) {
8970 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00008971 unlock_user(p, arg1, 0);
Paul Burton356d7712014-06-22 11:25:37 +01008972 }
8973 unlock_user(p2, arg2, 0);
Richard Henderson2852aaf2018-08-18 12:01:06 -07008974 return -TARGET_EFAULT;
Paul Burton356d7712014-06-22 11:25:37 +01008975 }
8976 } else {
8977 p3 = NULL;
8978 }
8979
8980 /* FIXME - arg5 should be locked, but it isn't clear how to
8981 * do that since it's not guaranteed to be a NULL-terminated
8982 * string.
8983 */
8984 if (!arg5) {
8985 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
8986 } else {
Richard Henderson3e8f1622021-02-12 10:48:43 -08008987 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
Paul Burton356d7712014-06-22 11:25:37 +01008988 }
8989 ret = get_errno(ret);
8990
8991 if (arg1) {
8992 unlock_user(p, arg1, 0);
8993 }
8994 unlock_user(p2, arg2, 0);
8995 if (arg3) {
8996 unlock_user(p3, arg3, 0);
8997 }
8998 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07008999 return ret;
Laurent Vivier6eb9dbf2020-05-02 21:46:42 +02009000#if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9001#if defined(TARGET_NR_umount)
bellard31e31b82003-02-18 22:55:36 +00009002 case TARGET_NR_umount:
Laurent Vivier6eb9dbf2020-05-02 21:46:42 +02009003#endif
9004#if defined(TARGET_NR_oldumount)
9005 case TARGET_NR_oldumount:
9006#endif
bellard579a97f2007-11-11 14:26:47 +00009007 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009008 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009009 ret = get_errno(umount(p));
9010 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009011 return ret;
thse5febef2007-04-01 18:31:35 +00009012#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009013#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009014 case TARGET_NR_stime:
9015 {
Laurent Vivier0f1f2d42019-11-12 15:25:56 +01009016 struct timespec ts;
9017 ts.tv_nsec = 0;
9018 if (get_user_sal(ts.tv_sec, arg1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009019 return -TARGET_EFAULT;
Laurent Vivier0f1f2d42019-11-12 15:25:56 +01009020 }
9021 return get_errno(clock_settime(CLOCK_REALTIME, &ts));
bellard31e31b82003-02-18 22:55:36 +00009022 }
j_mayer7a3148a2007-04-05 07:13:51 +00009023#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009024#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009025 case TARGET_NR_alarm:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009026 return alarm(arg1);
j_mayer7a3148a2007-04-05 07:13:51 +00009027#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009028#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009029 case TARGET_NR_pause:
Timothy E Baldwinf59ec602016-05-27 15:51:55 +01009030 if (!block_signals()) {
9031 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
9032 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009033 return -TARGET_EINTR;
j_mayer7a3148a2007-04-05 07:13:51 +00009034#endif
thse5febef2007-04-01 18:31:35 +00009035#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00009036 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00009037 {
pbrook53a59602006-03-25 19:31:22 +00009038 struct utimbuf tbuf, *host_tbuf;
9039 struct target_utimbuf *target_tbuf;
9040 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00009041 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009042 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02009043 tbuf.actime = tswapal(target_tbuf->actime);
9044 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00009045 unlock_user_struct(target_tbuf, arg2, 0);
9046 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00009047 } else {
pbrook53a59602006-03-25 19:31:22 +00009048 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00009049 }
bellard579a97f2007-11-11 14:26:47 +00009050 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009051 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009052 ret = get_errno(utime(p, host_tbuf));
9053 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00009054 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009055 return ret;
thse5febef2007-04-01 18:31:35 +00009056#endif
Chen Gang704eff62015-08-21 05:37:33 +08009057#ifdef TARGET_NR_utimes
bellard978a66f2004-12-06 22:58:05 +00009058 case TARGET_NR_utimes:
9059 {
bellard978a66f2004-12-06 22:58:05 +00009060 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00009061 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00009062 if (copy_from_user_timeval(&tv[0], arg2)
9063 || copy_from_user_timeval(&tv[1],
9064 arg2 + sizeof(struct target_timeval)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009065 return -TARGET_EFAULT;
bellard978a66f2004-12-06 22:58:05 +00009066 tvp = tv;
9067 } else {
9068 tvp = NULL;
9069 }
bellard579a97f2007-11-11 14:26:47 +00009070 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009071 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009072 ret = get_errno(utimes(p, tvp));
9073 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00009074 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009075 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009076#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009077#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00009078 case TARGET_NR_futimesat:
9079 {
9080 struct timeval *tvp, tv[2];
9081 if (arg3) {
9082 if (copy_from_user_timeval(&tv[0], arg3)
9083 || copy_from_user_timeval(&tv[1],
9084 arg3 + sizeof(struct target_timeval)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009085 return -TARGET_EFAULT;
balrogac8a6552008-09-20 02:25:39 +00009086 tvp = tv;
9087 } else {
9088 tvp = NULL;
9089 }
Richard Henderson2852aaf2018-08-18 12:01:06 -07009090 if (!(p = lock_user_string(arg2))) {
9091 return -TARGET_EFAULT;
9092 }
Peter Maydellc0d472b2013-06-12 16:20:21 +01009093 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00009094 unlock_user(p, arg2, 0);
9095 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009096 return ret;
balrogac8a6552008-09-20 02:25:39 +00009097#endif
Chen Gang704eff62015-08-21 05:37:33 +08009098#ifdef TARGET_NR_access
bellard31e31b82003-02-18 22:55:36 +00009099 case TARGET_NR_access:
Richard Henderson2852aaf2018-08-18 12:01:06 -07009100 if (!(p = lock_user_string(arg1))) {
9101 return -TARGET_EFAULT;
9102 }
Ulrich Hecht719f9082009-07-03 17:09:29 +02009103 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00009104 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009105 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009106#endif
ths92a34c12007-09-24 09:27:49 +00009107#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9108 case TARGET_NR_faccessat:
Richard Henderson2852aaf2018-08-18 12:01:06 -07009109 if (!(p = lock_user_string(arg2))) {
9110 return -TARGET_EFAULT;
9111 }
Peter Maydellc0d472b2013-06-12 16:20:21 +01009112 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00009113 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009114 return ret;
ths92a34c12007-09-24 09:27:49 +00009115#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009116#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009117 case TARGET_NR_nice:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009118 return get_errno(nice(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +00009119#endif
bellard31e31b82003-02-18 22:55:36 +00009120 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00009121 sync();
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009122 return 0;
Aleksandar Markovic5a03cd02016-10-10 13:23:30 +02009123#if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9124 case TARGET_NR_syncfs:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009125 return get_errno(syncfs(arg1));
Aleksandar Markovic5a03cd02016-10-10 13:23:30 +02009126#endif
bellard31e31b82003-02-18 22:55:36 +00009127 case TARGET_NR_kill:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009128 return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
Chen Gang704eff62015-08-21 05:37:33 +08009129#ifdef TARGET_NR_rename
bellard31e31b82003-02-18 22:55:36 +00009130 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00009131 {
9132 void *p2;
9133 p = lock_user_string(arg1);
9134 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00009135 if (!p || !p2)
9136 ret = -TARGET_EFAULT;
9137 else
9138 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00009139 unlock_user(p2, arg2, 0);
9140 unlock_user(p, arg1, 0);
9141 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009142 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009143#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009144#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00009145 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00009146 {
bellard579a97f2007-11-11 14:26:47 +00009147 void *p2;
ths722183f2007-09-24 09:24:37 +00009148 p = lock_user_string(arg2);
9149 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00009150 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00009151 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00009152 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01009153 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00009154 unlock_user(p2, arg4, 0);
9155 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00009156 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009157 return ret;
ths722183f2007-09-24 09:24:37 +00009158#endif
Andreas Schwab95d03072018-01-23 11:53:31 +01009159#if defined(TARGET_NR_renameat2)
9160 case TARGET_NR_renameat2:
9161 {
9162 void *p2;
9163 p = lock_user_string(arg2);
9164 p2 = lock_user_string(arg4);
9165 if (!p || !p2) {
9166 ret = -TARGET_EFAULT;
9167 } else {
9168 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
9169 }
9170 unlock_user(p2, arg4, 0);
9171 unlock_user(p, arg2, 0);
9172 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009173 return ret;
Andreas Schwab95d03072018-01-23 11:53:31 +01009174#endif
Chen Gang704eff62015-08-21 05:37:33 +08009175#ifdef TARGET_NR_mkdir
bellard31e31b82003-02-18 22:55:36 +00009176 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00009177 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009178 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009179 ret = get_errno(mkdir(p, arg2));
9180 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009181 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009182#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009183#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00009184 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00009185 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009186 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009187 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00009188 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009189 return ret;
ths4472ad02007-09-24 09:22:32 +00009190#endif
Chen Gang704eff62015-08-21 05:37:33 +08009191#ifdef TARGET_NR_rmdir
bellard31e31b82003-02-18 22:55:36 +00009192 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00009193 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009194 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009195 ret = get_errno(rmdir(p));
9196 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009197 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009198#endif
bellard31e31b82003-02-18 22:55:36 +00009199 case TARGET_NR_dup:
9200 ret = get_errno(dup(arg1));
Laurent Viviere36800c2015-10-02 14:48:09 +02009201 if (ret >= 0) {
9202 fd_trans_dup(arg1, ret);
9203 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009204 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009205#ifdef TARGET_NR_pipe
bellard31e31b82003-02-18 22:55:36 +00009206 case TARGET_NR_pipe:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009207 return do_pipe(cpu_env, arg1, 0, 0);
Chen Gang704eff62015-08-21 05:37:33 +08009208#endif
Riku Voipio099d6b02009-05-05 12:10:04 +03009209#ifdef TARGET_NR_pipe2
9210 case TARGET_NR_pipe2:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009211 return do_pipe(cpu_env, arg1,
9212 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03009213#endif
bellard31e31b82003-02-18 22:55:36 +00009214 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00009215 {
pbrook53a59602006-03-25 19:31:22 +00009216 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00009217 struct tms tms;
9218 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00009219 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00009220 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
9221 if (!tmsp)
Richard Henderson2852aaf2018-08-18 12:01:06 -07009222 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02009223 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
9224 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
9225 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
9226 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00009227 }
bellardc596ed12003-07-13 17:32:31 +00009228 if (!is_error(ret))
9229 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00009230 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009231 return ret;
bellard31e31b82003-02-18 22:55:36 +00009232 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00009233 if (arg1 == 0) {
9234 ret = get_errno(acct(NULL));
9235 } else {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009236 if (!(p = lock_user_string(arg1))) {
9237 return -TARGET_EFAULT;
9238 }
aurel3238d840e2009-01-30 19:48:17 +00009239 ret = get_errno(acct(path(p)));
9240 unlock_user(p, arg1, 0);
9241 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009242 return ret;
Richard Henderson8070e7b2013-07-24 09:50:00 -10009243#ifdef TARGET_NR_umount2
bellard31e31b82003-02-18 22:55:36 +00009244 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00009245 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009246 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009247 ret = get_errno(umount2(p, arg2));
9248 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009249 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009250#endif
bellard31e31b82003-02-18 22:55:36 +00009251 case TARGET_NR_ioctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009252 return do_ioctl(arg1, arg2, arg3);
Michael Clark47ae93c2018-03-03 01:31:11 +13009253#ifdef TARGET_NR_fcntl
bellard31e31b82003-02-18 22:55:36 +00009254 case TARGET_NR_fcntl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009255 return do_fcntl(arg1, arg2, arg3);
Michael Clark47ae93c2018-03-03 01:31:11 +13009256#endif
bellard31e31b82003-02-18 22:55:36 +00009257 case TARGET_NR_setpgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009258 return get_errno(setpgid(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00009259 case TARGET_NR_umask:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009260 return get_errno(umask(arg1));
bellard31e31b82003-02-18 22:55:36 +00009261 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00009262 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009263 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009264 ret = get_errno(chroot(p));
9265 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009266 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009267#ifdef TARGET_NR_dup2
bellard31e31b82003-02-18 22:55:36 +00009268 case TARGET_NR_dup2:
9269 ret = get_errno(dup2(arg1, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02009270 if (ret >= 0) {
9271 fd_trans_dup(arg1, arg2);
9272 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009273 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009274#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03009275#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9276 case TARGET_NR_dup3:
Peter Maydell10fa9932017-12-15 15:18:00 +00009277 {
9278 int host_flags;
9279
9280 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
9281 return -EINVAL;
9282 }
9283 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
9284 ret = get_errno(dup3(arg1, arg2, host_flags));
Laurent Viviere36800c2015-10-02 14:48:09 +02009285 if (ret >= 0) {
9286 fd_trans_dup(arg1, arg2);
9287 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009288 return ret;
Peter Maydell10fa9932017-12-15 15:18:00 +00009289 }
Ulrich Hechtd0927932009-09-17 20:22:14 +03009290#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009291#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009292 case TARGET_NR_getppid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009293 return get_errno(getppid());
j_mayer7a3148a2007-04-05 07:13:51 +00009294#endif
Chen Gang704eff62015-08-21 05:37:33 +08009295#ifdef TARGET_NR_getpgrp
bellard31e31b82003-02-18 22:55:36 +00009296 case TARGET_NR_getpgrp:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009297 return get_errno(getpgrp());
Chen Gang704eff62015-08-21 05:37:33 +08009298#endif
bellard31e31b82003-02-18 22:55:36 +00009299 case TARGET_NR_setsid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009300 return get_errno(setsid());
thse5febef2007-04-01 18:31:35 +00009301#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00009302 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00009303 {
Richard Henderson02d0de12021-04-22 16:02:26 -07009304#if defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00009305 struct target_sigaction act, oact, *pact, *old_act;
9306
9307 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00009308 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009309 return -TARGET_EFAULT;
bellard106ec872006-06-27 21:08:10 +00009310 act._sa_handler = old_act->_sa_handler;
9311 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
9312 act.sa_flags = old_act->sa_flags;
9313 unlock_user_struct(old_act, arg2, 0);
9314 pact = &act;
9315 } else {
9316 pact = NULL;
9317 }
9318
Richard Henderson02fb28e2021-04-22 16:02:23 -07009319 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
bellard106ec872006-06-27 21:08:10 +00009320
9321 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00009322 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009323 return -TARGET_EFAULT;
bellard106ec872006-06-27 21:08:10 +00009324 old_act->_sa_handler = oact._sa_handler;
9325 old_act->sa_flags = oact.sa_flags;
9326 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
9327 old_act->sa_mask.sig[1] = 0;
9328 old_act->sa_mask.sig[2] = 0;
9329 old_act->sa_mask.sig[3] = 0;
9330 unlock_user_struct(old_act, arg3, 1);
9331 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08009332#else
9333 struct target_old_sigaction *old_act;
9334 struct target_sigaction act, oact, *pact;
9335 if (arg2) {
9336 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009337 return -TARGET_EFAULT;
Richard Henderson6049f4f2009-12-27 18:30:03 -08009338 act._sa_handler = old_act->_sa_handler;
9339 target_siginitset(&act.sa_mask, old_act->sa_mask);
9340 act.sa_flags = old_act->sa_flags;
Richard Hendersonca192272021-04-22 16:02:24 -07009341#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Henderson6049f4f2009-12-27 18:30:03 -08009342 act.sa_restorer = old_act->sa_restorer;
Richard Hendersonca192272021-04-22 16:02:24 -07009343#endif
Richard Henderson6049f4f2009-12-27 18:30:03 -08009344 unlock_user_struct(old_act, arg2, 0);
9345 pact = &act;
9346 } else {
9347 pact = NULL;
9348 }
Richard Henderson02fb28e2021-04-22 16:02:23 -07009349 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
Richard Henderson6049f4f2009-12-27 18:30:03 -08009350 if (!is_error(ret) && arg3) {
9351 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009352 return -TARGET_EFAULT;
Richard Henderson6049f4f2009-12-27 18:30:03 -08009353 old_act->_sa_handler = oact._sa_handler;
9354 old_act->sa_mask = oact.sa_mask.sig[0];
9355 old_act->sa_flags = oact.sa_flags;
Richard Hendersonca192272021-04-22 16:02:24 -07009356#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Henderson6049f4f2009-12-27 18:30:03 -08009357 old_act->sa_restorer = oact.sa_restorer;
Richard Hendersonca192272021-04-22 16:02:24 -07009358#endif
Richard Henderson6049f4f2009-12-27 18:30:03 -08009359 unlock_user_struct(old_act, arg3, 1);
9360 }
ths388bb212007-05-13 13:58:00 +00009361#endif
bellard31e31b82003-02-18 22:55:36 +00009362 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009363 return ret;
thse5febef2007-04-01 18:31:35 +00009364#endif
bellard66fb9762003-03-23 01:06:05 +00009365 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00009366 {
Richard Henderson0f6f9902021-04-22 16:02:25 -07009367 /*
9368 * For Alpha and SPARC this is a 5 argument syscall, with
Peter Maydell78bfef72017-11-06 18:33:26 +00009369 * a 'restorer' parameter which must be copied into the
9370 * sa_restorer field of the sigaction struct.
9371 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9372 * and arg5 is the sigsetsize.
Peter Maydell78bfef72017-11-06 18:33:26 +00009373 */
Richard Henderson0f6f9902021-04-22 16:02:25 -07009374#if defined(TARGET_ALPHA)
9375 target_ulong sigsetsize = arg4;
9376 target_ulong restorer = arg5;
9377#elif defined(TARGET_SPARC)
Peter Maydell78bfef72017-11-06 18:33:26 +00009378 target_ulong restorer = arg4;
9379 target_ulong sigsetsize = arg5;
9380#else
9381 target_ulong sigsetsize = arg4;
Richard Henderson02fb28e2021-04-22 16:02:23 -07009382 target_ulong restorer = 0;
Peter Maydell78bfef72017-11-06 18:33:26 +00009383#endif
Richard Hendersonfb804392021-04-22 16:02:27 -07009384 struct target_sigaction *act = NULL;
9385 struct target_sigaction *oact = NULL;
pbrook53a59602006-03-25 19:31:22 +00009386
Peter Maydell78bfef72017-11-06 18:33:26 +00009387 if (sigsetsize != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009388 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009389 }
Richard Hendersonfb804392021-04-22 16:02:27 -07009390 if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
9391 return -TARGET_EFAULT;
Peter Maydell78bfef72017-11-06 18:33:26 +00009392 }
Richard Hendersonfb804392021-04-22 16:02:27 -07009393 if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
9394 ret = -TARGET_EFAULT;
9395 } else {
9396 ret = get_errno(do_sigaction(arg1, act, oact, restorer));
9397 if (oact) {
9398 unlock_user_struct(oact, arg3, 1);
bellard579a97f2007-11-11 14:26:47 +00009399 }
Richard Hendersonfb804392021-04-22 16:02:27 -07009400 }
9401 if (act) {
pbrook53a59602006-03-25 19:31:22 +00009402 unlock_user_struct(act, arg2, 0);
Richard Hendersonfb804392021-04-22 16:02:27 -07009403 }
pbrook53a59602006-03-25 19:31:22 +00009404 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009405 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009406#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009407 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00009408 {
9409 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00009410 abi_ulong target_set;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009411 ret = do_sigprocmask(0, NULL, &cur_set);
9412 if (!ret) {
9413 host_to_target_old_sigset(&target_set, &cur_set);
9414 ret = target_set;
9415 }
bellard66fb9762003-03-23 01:06:05 +00009416 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009417 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009418#endif
9419#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009420 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00009421 {
Miloš Stojanovića8617d82017-05-15 16:59:43 +02009422 sigset_t set, oset;
blueswir1992f48a2007-10-14 16:27:31 +00009423 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00009424 target_to_host_old_sigset(&set, &target_set);
Peter Maydell3d3efba2016-05-27 15:51:49 +01009425 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
9426 if (!ret) {
9427 host_to_target_old_sigset(&target_set, &oset);
9428 ret = target_set;
9429 }
bellard66fb9762003-03-23 01:06:05 +00009430 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009431 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009432#endif
thse5febef2007-04-01 18:31:35 +00009433#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00009434 case TARGET_NR_sigprocmask:
9435 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07009436#if defined(TARGET_ALPHA)
9437 sigset_t set, oldset;
9438 abi_ulong mask;
9439 int how;
9440
9441 switch (arg1) {
9442 case TARGET_SIG_BLOCK:
9443 how = SIG_BLOCK;
9444 break;
9445 case TARGET_SIG_UNBLOCK:
9446 how = SIG_UNBLOCK;
9447 break;
9448 case TARGET_SIG_SETMASK:
9449 how = SIG_SETMASK;
9450 break;
9451 default:
Richard Henderson259841c2018-08-18 12:01:09 -07009452 return -TARGET_EINVAL;
Richard Hendersona5b3b132010-05-03 10:07:55 -07009453 }
9454 mask = arg2;
9455 target_to_host_old_sigset(&set, &mask);
9456
Peter Maydell3d3efba2016-05-27 15:51:49 +01009457 ret = do_sigprocmask(how, &set, &oldset);
Richard Hendersona5b3b132010-05-03 10:07:55 -07009458 if (!is_error(ret)) {
9459 host_to_target_old_sigset(&mask, &oldset);
9460 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07009461 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07009462 }
9463#else
bellard66fb9762003-03-23 01:06:05 +00009464 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07009465 int how;
ths3b46e622007-09-17 08:09:54 +00009466
pbrook53a59602006-03-25 19:31:22 +00009467 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07009468 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00009469 case TARGET_SIG_BLOCK:
9470 how = SIG_BLOCK;
9471 break;
9472 case TARGET_SIG_UNBLOCK:
9473 how = SIG_UNBLOCK;
9474 break;
9475 case TARGET_SIG_SETMASK:
9476 how = SIG_SETMASK;
9477 break;
9478 default:
Richard Henderson259841c2018-08-18 12:01:09 -07009479 return -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00009480 }
Anthony Liguoric227f092009-10-01 16:12:16 -05009481 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009482 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009483 target_to_host_old_sigset(&set, p);
9484 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00009485 set_ptr = &set;
9486 } else {
9487 how = 0;
9488 set_ptr = NULL;
9489 }
Peter Maydell3d3efba2016-05-27 15:51:49 +01009490 ret = do_sigprocmask(how, set_ptr, &oldset);
pbrook53a59602006-03-25 19:31:22 +00009491 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009492 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009493 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009494 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05009495 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009496 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07009497#endif
bellard66fb9762003-03-23 01:06:05 +00009498 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009499 return ret;
thse5febef2007-04-01 18:31:35 +00009500#endif
bellard66fb9762003-03-23 01:06:05 +00009501 case TARGET_NR_rt_sigprocmask:
9502 {
9503 int how = arg1;
9504 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00009505
Peter Maydellc8157012016-06-30 14:23:24 +01009506 if (arg4 != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009507 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009508 }
9509
pbrook53a59602006-03-25 19:31:22 +00009510 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00009511 switch(how) {
9512 case TARGET_SIG_BLOCK:
9513 how = SIG_BLOCK;
9514 break;
9515 case TARGET_SIG_UNBLOCK:
9516 how = SIG_UNBLOCK;
9517 break;
9518 case TARGET_SIG_SETMASK:
9519 how = SIG_SETMASK;
9520 break;
9521 default:
Richard Henderson259841c2018-08-18 12:01:09 -07009522 return -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00009523 }
Anthony Liguoric227f092009-10-01 16:12:16 -05009524 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009525 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009526 target_to_host_sigset(&set, p);
9527 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00009528 set_ptr = &set;
9529 } else {
9530 how = 0;
9531 set_ptr = NULL;
9532 }
Peter Maydell3d3efba2016-05-27 15:51:49 +01009533 ret = do_sigprocmask(how, set_ptr, &oldset);
pbrook53a59602006-03-25 19:31:22 +00009534 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009535 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009536 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009537 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05009538 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009539 }
9540 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009541 return ret;
thse5febef2007-04-01 18:31:35 +00009542#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00009543 case TARGET_NR_sigpending:
9544 {
9545 sigset_t set;
9546 ret = get_errno(sigpending(&set));
9547 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009548 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009549 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009550 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05009551 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009552 }
9553 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009554 return ret;
thse5febef2007-04-01 18:31:35 +00009555#endif
bellard66fb9762003-03-23 01:06:05 +00009556 case TARGET_NR_rt_sigpending:
9557 {
9558 sigset_t set;
Peter Maydellc8157012016-06-30 14:23:24 +01009559
9560 /* Yes, this check is >, not != like most. We follow the kernel's
9561 * logic and it does it like this because it implements
9562 * NR_sigpending through the same code path, and in that case
9563 * the old_sigset_t is smaller in size.
9564 */
9565 if (arg2 > sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009566 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009567 }
9568
bellard66fb9762003-03-23 01:06:05 +00009569 ret = get_errno(sigpending(&set));
9570 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009571 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009572 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009573 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05009574 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009575 }
9576 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009577 return ret;
thse5febef2007-04-01 18:31:35 +00009578#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00009579 case TARGET_NR_sigsuspend:
9580 {
Peter Maydell3d3efba2016-05-27 15:51:49 +01009581 TaskState *ts = cpu->opaque;
Richard Hendersonf43ce122010-05-03 10:07:54 -07009582#if defined(TARGET_ALPHA)
9583 abi_ulong mask = arg1;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009584 target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
Richard Hendersonf43ce122010-05-03 10:07:54 -07009585#else
Anthony Liguoric227f092009-10-01 16:12:16 -05009586 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009587 return -TARGET_EFAULT;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009588 target_to_host_old_sigset(&ts->sigsuspend_mask, p);
pbrook53a59602006-03-25 19:31:22 +00009589 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07009590#endif
Peter Maydell3d3efba2016-05-27 15:51:49 +01009591 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
9592 SIGSET_T_SIZE));
Richard Hendersonaf254a22021-11-22 19:47:33 +01009593 if (ret != -QEMU_ERESTARTSYS) {
Peter Maydell3d3efba2016-05-27 15:51:49 +01009594 ts->in_sigsuspend = 1;
9595 }
bellard66fb9762003-03-23 01:06:05 +00009596 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009597 return ret;
thse5febef2007-04-01 18:31:35 +00009598#endif
bellard66fb9762003-03-23 01:06:05 +00009599 case TARGET_NR_rt_sigsuspend:
9600 {
Peter Maydell3d3efba2016-05-27 15:51:49 +01009601 TaskState *ts = cpu->opaque;
Peter Maydellc8157012016-06-30 14:23:24 +01009602
9603 if (arg2 != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009604 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009605 }
Anthony Liguoric227f092009-10-01 16:12:16 -05009606 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009607 return -TARGET_EFAULT;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009608 target_to_host_sigset(&ts->sigsuspend_mask, p);
pbrook53a59602006-03-25 19:31:22 +00009609 unlock_user(p, arg1, 0);
Peter Maydell3d3efba2016-05-27 15:51:49 +01009610 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
9611 SIGSET_T_SIZE));
Richard Hendersonaf254a22021-11-22 19:47:33 +01009612 if (ret != -QEMU_ERESTARTSYS) {
Peter Maydell3d3efba2016-05-27 15:51:49 +01009613 ts->in_sigsuspend = 1;
9614 }
bellard66fb9762003-03-23 01:06:05 +00009615 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009616 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -07009617#ifdef TARGET_NR_rt_sigtimedwait
bellard66fb9762003-03-23 01:06:05 +00009618 case TARGET_NR_rt_sigtimedwait:
9619 {
bellard66fb9762003-03-23 01:06:05 +00009620 sigset_t set;
9621 struct timespec uts, *puts;
9622 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00009623
Peter Maydellc8157012016-06-30 14:23:24 +01009624 if (arg4 != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009625 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009626 }
9627
Anthony Liguoric227f092009-10-01 16:12:16 -05009628 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009629 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009630 target_to_host_sigset(&set, p);
9631 unlock_user(p, arg1, 0);
9632 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00009633 puts = &uts;
Filip Bozuta4d213002020-07-24 20:16:51 +02009634 if (target_to_host_timespec(puts, arg3)) {
9635 return -TARGET_EFAULT;
9636 }
bellard66fb9762003-03-23 01:06:05 +00009637 } else {
9638 puts = NULL;
9639 }
Peter Maydellb3f82332016-06-06 19:58:08 +01009640 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
9641 SIGSET_T_SIZE));
Petar Jovanovic974a1962014-03-03 15:07:41 +01009642 if (!is_error(ret)) {
9643 if (arg2) {
9644 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
9645 0);
9646 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009647 return -TARGET_EFAULT;
Petar Jovanovic974a1962014-03-03 15:07:41 +01009648 }
9649 host_to_target_siginfo(p, &uinfo);
9650 unlock_user(p, arg2, sizeof(target_siginfo_t));
9651 }
9652 ret = host_to_target_signal(ret);
bellard66fb9762003-03-23 01:06:05 +00009653 }
9654 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009655 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -07009656#endif
Filip Bozutaddcbde12020-08-24 21:21:16 +02009657#ifdef TARGET_NR_rt_sigtimedwait_time64
9658 case TARGET_NR_rt_sigtimedwait_time64:
9659 {
9660 sigset_t set;
9661 struct timespec uts, *puts;
9662 siginfo_t uinfo;
9663
9664 if (arg4 != sizeof(target_sigset_t)) {
9665 return -TARGET_EINVAL;
9666 }
9667
9668 p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
9669 if (!p) {
9670 return -TARGET_EFAULT;
9671 }
9672 target_to_host_sigset(&set, p);
9673 unlock_user(p, arg1, 0);
9674 if (arg3) {
9675 puts = &uts;
9676 if (target_to_host_timespec64(puts, arg3)) {
9677 return -TARGET_EFAULT;
9678 }
9679 } else {
9680 puts = NULL;
9681 }
9682 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
9683 SIGSET_T_SIZE));
9684 if (!is_error(ret)) {
9685 if (arg2) {
9686 p = lock_user(VERIFY_WRITE, arg2,
9687 sizeof(target_siginfo_t), 0);
9688 if (!p) {
9689 return -TARGET_EFAULT;
9690 }
9691 host_to_target_siginfo(p, &uinfo);
9692 unlock_user(p, arg2, sizeof(target_siginfo_t));
9693 }
9694 ret = host_to_target_signal(ret);
9695 }
9696 }
9697 return ret;
9698#endif
bellard66fb9762003-03-23 01:06:05 +00009699 case TARGET_NR_rt_sigqueueinfo:
9700 {
9701 siginfo_t uinfo;
Peter Maydell4debae62016-06-20 15:50:36 +01009702
9703 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
9704 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009705 return -TARGET_EFAULT;
Peter Maydell4debae62016-06-20 15:50:36 +01009706 }
pbrook53a59602006-03-25 19:31:22 +00009707 target_to_host_siginfo(&uinfo, p);
Miloš Stojanovićd8b6d892017-05-15 16:59:44 +02009708 unlock_user(p, arg3, 0);
bellard66fb9762003-03-23 01:06:05 +00009709 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
9710 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009711 return ret;
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +02009712 case TARGET_NR_rt_tgsigqueueinfo:
9713 {
9714 siginfo_t uinfo;
9715
9716 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
9717 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009718 return -TARGET_EFAULT;
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +02009719 }
9720 target_to_host_siginfo(&uinfo, p);
9721 unlock_user(p, arg4, 0);
9722 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo));
9723 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009724 return ret;
thse5febef2007-04-01 18:31:35 +00009725#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00009726 case TARGET_NR_sigreturn:
Peter Maydell3d3efba2016-05-27 15:51:49 +01009727 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01009728 return -QEMU_ERESTARTSYS;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009729 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009730 return do_sigreturn(cpu_env);
thse5febef2007-04-01 18:31:35 +00009731#endif
bellard66fb9762003-03-23 01:06:05 +00009732 case TARGET_NR_rt_sigreturn:
Peter Maydell3d3efba2016-05-27 15:51:49 +01009733 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01009734 return -QEMU_ERESTARTSYS;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009735 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009736 return do_rt_sigreturn(cpu_env);
bellard31e31b82003-02-18 22:55:36 +00009737 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00009738 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009739 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009740 ret = get_errno(sethostname(p, arg2));
9741 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009742 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02009743#ifdef TARGET_NR_setrlimit
bellard31e31b82003-02-18 22:55:36 +00009744 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00009745 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03009746 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00009747 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00009748 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00009749 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009750 return -TARGET_EFAULT;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09009751 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
9752 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00009753 unlock_user_struct(target_rlim, arg2, 0);
Max Filippov5dfa88f2018-09-17 11:13:14 -07009754 /*
9755 * If we just passed through resource limit settings for memory then
9756 * they would also apply to QEMU's own allocations, and QEMU will
9757 * crash or hang or die if its allocations fail. Ideally we would
9758 * track the guest allocations in QEMU and apply the limits ourselves.
9759 * For now, just tell the guest the call succeeded but don't actually
9760 * limit anything.
9761 */
9762 if (resource != RLIMIT_AS &&
9763 resource != RLIMIT_DATA &&
9764 resource != RLIMIT_STACK) {
9765 return get_errno(setrlimit(resource, &rlim));
9766 } else {
9767 return 0;
9768 }
bellard9de5e442003-03-23 16:49:39 +00009769 }
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02009770#endif
9771#ifdef TARGET_NR_getrlimit
bellard31e31b82003-02-18 22:55:36 +00009772 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00009773 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03009774 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00009775 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00009776 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00009777
bellard9de5e442003-03-23 16:49:39 +00009778 ret = get_errno(getrlimit(resource, &rlim));
9779 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00009780 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009781 return -TARGET_EFAULT;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09009782 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
9783 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00009784 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00009785 }
9786 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009787 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02009788#endif
bellard31e31b82003-02-18 22:55:36 +00009789 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00009790 {
9791 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00009792 ret = get_errno(getrusage(arg1, &rusage));
9793 if (!is_error(ret)) {
Petar Jovanovica39fb272014-04-08 19:24:30 +02009794 ret = host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00009795 }
9796 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009797 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -07009798#if defined(TARGET_NR_gettimeofday)
bellard31e31b82003-02-18 22:55:36 +00009799 case TARGET_NR_gettimeofday:
9800 {
bellard31e31b82003-02-18 22:55:36 +00009801 struct timeval tv;
Richard Hendersona52f5f82020-02-12 19:22:23 -08009802 struct timezone tz;
9803
9804 ret = get_errno(gettimeofday(&tv, &tz));
bellard31e31b82003-02-18 22:55:36 +00009805 if (!is_error(ret)) {
Richard Hendersona52f5f82020-02-12 19:22:23 -08009806 if (arg1 && copy_to_user_timeval(arg1, &tv)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009807 return -TARGET_EFAULT;
Richard Hendersona52f5f82020-02-12 19:22:23 -08009808 }
9809 if (arg2 && copy_to_user_timezone(arg2, &tz)) {
9810 return -TARGET_EFAULT;
9811 }
bellard31e31b82003-02-18 22:55:36 +00009812 }
9813 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009814 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -07009815#endif
9816#if defined(TARGET_NR_settimeofday)
bellard31e31b82003-02-18 22:55:36 +00009817 case TARGET_NR_settimeofday:
9818 {
Paul Burtonb67d8032014-06-22 11:25:41 +01009819 struct timeval tv, *ptv = NULL;
Paul Burtonef4467e2014-06-22 11:25:40 +01009820 struct timezone tz, *ptz = NULL;
9821
Paul Burtonb67d8032014-06-22 11:25:41 +01009822 if (arg1) {
9823 if (copy_from_user_timeval(&tv, arg1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009824 return -TARGET_EFAULT;
Paul Burtonb67d8032014-06-22 11:25:41 +01009825 }
9826 ptv = &tv;
9827 }
Paul Burtonef4467e2014-06-22 11:25:40 +01009828
9829 if (arg2) {
9830 if (copy_from_user_timezone(&tz, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009831 return -TARGET_EFAULT;
Paul Burtonef4467e2014-06-22 11:25:40 +01009832 }
9833 ptz = &tz;
9834 }
9835
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009836 return get_errno(settimeofday(ptv, ptz));
bellard31e31b82003-02-18 22:55:36 +00009837 }
Alistair Francis859e8a82020-03-12 15:13:49 -07009838#endif
Laurent Vivier9468a5d2013-01-10 22:30:50 +01009839#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00009840 case TARGET_NR_select:
Laurent Vivier5457dc92016-07-08 01:17:27 +02009841#if defined(TARGET_WANT_NI_OLD_SELECT)
9842 /* some architectures used to have old_select here
9843 * but now ENOSYS it.
9844 */
9845 ret = -TARGET_ENOSYS;
9846#elif defined(TARGET_WANT_OLD_SYS_SELECT)
9847 ret = do_old_select(arg1);
Laurent Vivier9468a5d2013-01-10 22:30:50 +01009848#else
Laurent Vivier5457dc92016-07-08 01:17:27 +02009849 ret = do_select(arg1, arg2, arg3, arg4, arg5);
Laurent Vivier9468a5d2013-01-10 22:30:50 +01009850#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009851 return ret;
bellard048f6b42005-11-26 18:47:20 +00009852#endif
Riku Voipio9e423822010-05-07 12:28:05 +00009853#ifdef TARGET_NR_pselect6
9854 case TARGET_NR_pselect6:
Filip Bozutae5ce9682020-08-25 00:30:49 +02009855 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
9856#endif
9857#ifdef TARGET_NR_pselect6_time64
9858 case TARGET_NR_pselect6_time64:
9859 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
Riku Voipio9e423822010-05-07 12:28:05 +00009860#endif
Chen Gang704eff62015-08-21 05:37:33 +08009861#ifdef TARGET_NR_symlink
bellard31e31b82003-02-18 22:55:36 +00009862 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00009863 {
9864 void *p2;
9865 p = lock_user_string(arg1);
9866 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00009867 if (!p || !p2)
9868 ret = -TARGET_EFAULT;
9869 else
9870 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00009871 unlock_user(p2, arg2, 0);
9872 unlock_user(p, arg1, 0);
9873 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009874 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009875#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009876#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +00009877 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00009878 {
bellard579a97f2007-11-11 14:26:47 +00009879 void *p2;
thsf0b62432007-09-24 09:25:40 +00009880 p = lock_user_string(arg1);
9881 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00009882 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00009883 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00009884 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01009885 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00009886 unlock_user(p2, arg3, 0);
9887 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00009888 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009889 return ret;
thsf0b62432007-09-24 09:25:40 +00009890#endif
Chen Gang704eff62015-08-21 05:37:33 +08009891#ifdef TARGET_NR_readlink
bellard31e31b82003-02-18 22:55:36 +00009892 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00009893 {
Andreas Schwab463d8e72013-07-02 14:04:12 +01009894 void *p2;
pbrook53a59602006-03-25 19:31:22 +00009895 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00009896 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01009897 if (!p || !p2) {
bellard579a97f2007-11-11 14:26:47 +00009898 ret = -TARGET_EFAULT;
Mike Frysingerf17f4982014-08-08 09:40:25 +09009899 } else if (!arg3) {
9900 /* Short circuit this for the magic exe check. */
9901 ret = -TARGET_EINVAL;
Andreas Schwab463d8e72013-07-02 14:04:12 +01009902 } else if (is_proc_myself((const char *)p, "exe")) {
9903 char real[PATH_MAX], *temp;
9904 temp = realpath(exec_path, real);
Mike Frysingerf17f4982014-08-08 09:40:25 +09009905 /* Return value is # of bytes that we wrote to the buffer. */
9906 if (temp == NULL) {
9907 ret = get_errno(-1);
9908 } else {
9909 /* Don't worry about sign mismatch as earlier mapping
9910 * logic would have thrown a bad address error. */
9911 ret = MIN(strlen(real), arg3);
9912 /* We cannot NUL terminate the string. */
9913 memcpy(p2, real, ret);
9914 }
Andreas Schwab463d8e72013-07-02 14:04:12 +01009915 } else {
9916 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00009917 }
pbrook53a59602006-03-25 19:31:22 +00009918 unlock_user(p2, arg2, ret);
9919 unlock_user(p, arg1, 0);
9920 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009921 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009922#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009923#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +00009924 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00009925 {
bellard579a97f2007-11-11 14:26:47 +00009926 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00009927 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00009928 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01009929 if (!p || !p2) {
9930 ret = -TARGET_EFAULT;
9931 } else if (is_proc_myself((const char *)p, "exe")) {
9932 char real[PATH_MAX], *temp;
9933 temp = realpath(exec_path, real);
9934 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
9935 snprintf((char *)p2, arg4, "%s", real);
9936 } else {
Peter Maydellc0d472b2013-06-12 16:20:21 +01009937 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
Andreas Schwab463d8e72013-07-02 14:04:12 +01009938 }
bellard579a97f2007-11-11 14:26:47 +00009939 unlock_user(p2, arg3, ret);
9940 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00009941 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009942 return ret;
ths5e0ccb12007-09-24 09:26:10 +00009943#endif
thse5febef2007-04-01 18:31:35 +00009944#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00009945 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00009946 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009947 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009948 ret = get_errno(swapon(p, arg2));
9949 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009950 return ret;
thse5febef2007-04-01 18:31:35 +00009951#endif
bellard31e31b82003-02-18 22:55:36 +00009952 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00009953 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
9954 /* arg4 must be ignored in all other cases */
9955 p = lock_user_string(arg4);
9956 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009957 return -TARGET_EFAULT;
Laurent Vivierc07ecc62013-01-07 11:40:06 +00009958 }
9959 ret = get_errno(reboot(arg1, arg2, arg3, p));
9960 unlock_user(p, arg4, 0);
9961 } else {
9962 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
9963 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009964 return ret;
thse5febef2007-04-01 18:31:35 +00009965#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00009966 case TARGET_NR_mmap:
Alexander Graf09701192013-09-03 20:12:15 +01009967#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
9968 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +02009969 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
9970 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00009971 {
blueswir1992f48a2007-10-14 16:27:31 +00009972 abi_ulong *v;
9973 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00009974 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009975 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02009976 v1 = tswapal(v[0]);
9977 v2 = tswapal(v[1]);
9978 v3 = tswapal(v[2]);
9979 v4 = tswapal(v[3]);
9980 v5 = tswapal(v[4]);
9981 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00009982 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00009983 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00009984 target_to_host_bitmask(v4, mmap_flags_tbl),
9985 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00009986 }
bellard31e31b82003-02-18 22:55:36 +00009987#else
Richard Hendersonee1bf832021-02-12 10:48:44 -08009988 /* mmap pointers are always untagged */
ths5fafdf22007-09-16 21:08:06 +00009989 ret = get_errno(target_mmap(arg1, arg2, arg3,
9990 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00009991 arg5,
9992 arg6));
bellard31e31b82003-02-18 22:55:36 +00009993#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009994 return ret;
thse5febef2007-04-01 18:31:35 +00009995#endif
bellarda315a142005-01-30 22:59:18 +00009996#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00009997 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00009998#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00009999#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +000010000#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010001 ret = target_mmap(arg1, arg2, arg3,
10002 target_to_host_bitmask(arg4, mmap_flags_tbl),
10003 arg5, arg6 << MMAP_SHIFT);
10004 return get_errno(ret);
bellarda315a142005-01-30 22:59:18 +000010005#endif
bellard31e31b82003-02-18 22:55:36 +000010006 case TARGET_NR_munmap:
Richard Hendersonee1bf832021-02-12 10:48:44 -080010007 arg1 = cpu_untagged_addr(cpu, arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010008 return get_errno(target_munmap(arg1, arg2));
bellard9de5e442003-03-23 16:49:39 +000010009 case TARGET_NR_mprotect:
Richard Hendersonee1bf832021-02-12 10:48:44 -080010010 arg1 = cpu_untagged_addr(cpu, arg1);
Paul Brook97374d32010-06-16 13:03:51 +010010011 {
Andreas Färber0429a972013-08-26 18:14:44 +020010012 TaskState *ts = cpu->opaque;
Paul Brook97374d32010-06-16 13:03:51 +010010013 /* Special hack to detect libc making the stack executable. */
10014 if ((arg3 & PROT_GROWSDOWN)
10015 && arg1 >= ts->info->stack_limit
10016 && arg1 <= ts->info->start_stack) {
10017 arg3 &= ~PROT_GROWSDOWN;
10018 arg2 = arg2 + arg1 - ts->info->stack_limit;
10019 arg1 = ts->info->stack_limit;
10020 }
10021 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010022 return get_errno(target_mprotect(arg1, arg2, arg3));
thse5febef2007-04-01 18:31:35 +000010023#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +000010024 case TARGET_NR_mremap:
Richard Hendersonee1bf832021-02-12 10:48:44 -080010025 arg1 = cpu_untagged_addr(cpu, arg1);
10026 /* mremap new_addr (arg5) is always untagged */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010027 return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
thse5febef2007-04-01 18:31:35 +000010028#endif
pbrook53a59602006-03-25 19:31:22 +000010029 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +000010030#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +000010031 case TARGET_NR_msync:
Richard Henderson3e8f1622021-02-12 10:48:43 -080010032 return get_errno(msync(g2h(cpu, arg1), arg2, arg3));
thse5febef2007-04-01 18:31:35 +000010033#endif
10034#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +000010035 case TARGET_NR_mlock:
Richard Henderson3e8f1622021-02-12 10:48:43 -080010036 return get_errno(mlock(g2h(cpu, arg1), arg2));
thse5febef2007-04-01 18:31:35 +000010037#endif
10038#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +000010039 case TARGET_NR_munlock:
Richard Henderson3e8f1622021-02-12 10:48:43 -080010040 return get_errno(munlock(g2h(cpu, arg1), arg2));
thse5febef2007-04-01 18:31:35 +000010041#endif
10042#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +000010043 case TARGET_NR_mlockall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010044 return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
thse5febef2007-04-01 18:31:35 +000010045#endif
10046#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +000010047 case TARGET_NR_munlockall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010048 return get_errno(munlockall());
thse5febef2007-04-01 18:31:35 +000010049#endif
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010050#ifdef TARGET_NR_truncate
bellard31e31b82003-02-18 22:55:36 +000010051 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +000010052 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010053 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010054 ret = get_errno(truncate(p, arg2));
10055 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010056 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010057#endif
10058#ifdef TARGET_NR_ftruncate
bellard31e31b82003-02-18 22:55:36 +000010059 case TARGET_NR_ftruncate:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010060 return get_errno(ftruncate(arg1, arg2));
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010061#endif
bellard31e31b82003-02-18 22:55:36 +000010062 case TARGET_NR_fchmod:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010063 return get_errno(fchmod(arg1, arg2));
Peter Maydellc0d472b2013-06-12 16:20:21 +010010064#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +000010065 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +000010066 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010067 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +010010068 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +000010069 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010070 return ret;
ths814d7972007-09-24 09:26:51 +000010071#endif
bellard31e31b82003-02-18 22:55:36 +000010072 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -070010073 /* Note that negative values are valid for getpriority, so we must
10074 differentiate based on errno settings. */
10075 errno = 0;
10076 ret = getpriority(arg1, arg2);
10077 if (ret == -1 && errno != 0) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010078 return -host_to_target_errno(errno);
Richard Henderson95c09822012-06-07 15:14:50 -070010079 }
10080#ifdef TARGET_ALPHA
10081 /* Return value is the unbiased priority. Signal no error. */
10082 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
10083#else
10084 /* Return value is a biased priority to avoid negative numbers. */
10085 ret = 20 - ret;
10086#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010087 return ret;
bellard31e31b82003-02-18 22:55:36 +000010088 case TARGET_NR_setpriority:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010089 return get_errno(setpriority(arg1, arg2, arg3));
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010090#ifdef TARGET_NR_statfs
bellard31e31b82003-02-18 22:55:36 +000010091 case TARGET_NR_statfs:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010092 if (!(p = lock_user_string(arg1))) {
10093 return -TARGET_EFAULT;
10094 }
pbrook53a59602006-03-25 19:31:22 +000010095 ret = get_errno(statfs(path(p), &stfs));
10096 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +000010097 convert_statfs:
10098 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000010099 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +000010100
bellard579a97f2007-11-11 14:26:47 +000010101 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010102 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000010103 __put_user(stfs.f_type, &target_stfs->f_type);
10104 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10105 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10106 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10107 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10108 __put_user(stfs.f_files, &target_stfs->f_files);
10109 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10110 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10111 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10112 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +020010113 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
Shea Levyd4247ec2018-03-01 06:15:00 -050010114#ifdef _STATFS_F_FLAGS
10115 __put_user(stfs.f_flags, &target_stfs->f_flags);
10116#else
10117 __put_user(0, &target_stfs->f_flags);
10118#endif
Alexander Graf229d3372012-09-19 04:39:53 +020010119 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +000010120 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +000010121 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010122 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010123#endif
10124#ifdef TARGET_NR_fstatfs
bellard31e31b82003-02-18 22:55:36 +000010125 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +000010126 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +000010127 goto convert_statfs;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010128#endif
bellard56c8f682005-11-28 22:28:41 +000010129#ifdef TARGET_NR_statfs64
10130 case TARGET_NR_statfs64:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010131 if (!(p = lock_user_string(arg1))) {
10132 return -TARGET_EFAULT;
10133 }
pbrook53a59602006-03-25 19:31:22 +000010134 ret = get_errno(statfs(path(p), &stfs));
10135 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +000010136 convert_statfs64:
10137 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000010138 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +000010139
bellard579a97f2007-11-11 14:26:47 +000010140 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010141 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000010142 __put_user(stfs.f_type, &target_stfs->f_type);
10143 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10144 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10145 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10146 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10147 __put_user(stfs.f_files, &target_stfs->f_files);
10148 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10149 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10150 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10151 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +020010152 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
Frajob94e2b42020-01-30 15:00:30 +020010153#ifdef _STATFS_F_FLAGS
10154 __put_user(stfs.f_flags, &target_stfs->f_flags);
10155#else
10156 __put_user(0, &target_stfs->f_flags);
10157#endif
Alexander Graf229d3372012-09-19 04:39:53 +020010158 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +000010159 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +000010160 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010161 return ret;
bellard56c8f682005-11-28 22:28:41 +000010162 case TARGET_NR_fstatfs64:
10163 ret = get_errno(fstatfs(arg1, &stfs));
10164 goto convert_statfs64;
10165#endif
thse5febef2007-04-01 18:31:35 +000010166#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +000010167 case TARGET_NR_socketcall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010168 return do_socketcall(arg1, arg2);
thse5febef2007-04-01 18:31:35 +000010169#endif
bellard3532fa72006-06-24 15:06:03 +000010170#ifdef TARGET_NR_accept
10171 case TARGET_NR_accept:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010172 return do_accept4(arg1, arg2, arg3, 0);
Peter Maydella94b4982013-02-08 04:35:04 +000010173#endif
10174#ifdef TARGET_NR_accept4
10175 case TARGET_NR_accept4:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010176 return do_accept4(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +000010177#endif
10178#ifdef TARGET_NR_bind
10179 case TARGET_NR_bind:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010180 return do_bind(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010181#endif
10182#ifdef TARGET_NR_connect
10183 case TARGET_NR_connect:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010184 return do_connect(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010185#endif
10186#ifdef TARGET_NR_getpeername
10187 case TARGET_NR_getpeername:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010188 return do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010189#endif
10190#ifdef TARGET_NR_getsockname
10191 case TARGET_NR_getsockname:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010192 return do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010193#endif
10194#ifdef TARGET_NR_getsockopt
10195 case TARGET_NR_getsockopt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010196 return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
bellard3532fa72006-06-24 15:06:03 +000010197#endif
10198#ifdef TARGET_NR_listen
10199 case TARGET_NR_listen:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010200 return get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +000010201#endif
10202#ifdef TARGET_NR_recv
10203 case TARGET_NR_recv:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010204 return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +000010205#endif
10206#ifdef TARGET_NR_recvfrom
10207 case TARGET_NR_recvfrom:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010208 return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +000010209#endif
10210#ifdef TARGET_NR_recvmsg
10211 case TARGET_NR_recvmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010212 return do_sendrecvmsg(arg1, arg2, arg3, 0);
bellard3532fa72006-06-24 15:06:03 +000010213#endif
10214#ifdef TARGET_NR_send
10215 case TARGET_NR_send:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010216 return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +000010217#endif
10218#ifdef TARGET_NR_sendmsg
10219 case TARGET_NR_sendmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010220 return do_sendrecvmsg(arg1, arg2, arg3, 1);
bellard3532fa72006-06-24 15:06:03 +000010221#endif
Alexander Graff19e00d2014-03-02 19:36:42 +000010222#ifdef TARGET_NR_sendmmsg
10223 case TARGET_NR_sendmmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010224 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
Alistair Francis859e8a82020-03-12 15:13:49 -070010225#endif
10226#ifdef TARGET_NR_recvmmsg
Alexander Graff19e00d2014-03-02 19:36:42 +000010227 case TARGET_NR_recvmmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010228 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
Alexander Graff19e00d2014-03-02 19:36:42 +000010229#endif
bellard3532fa72006-06-24 15:06:03 +000010230#ifdef TARGET_NR_sendto
10231 case TARGET_NR_sendto:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010232 return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +000010233#endif
10234#ifdef TARGET_NR_shutdown
10235 case TARGET_NR_shutdown:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010236 return get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +000010237#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +010010238#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10239 case TARGET_NR_getrandom:
10240 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10241 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010242 return -TARGET_EFAULT;
Laurent Vivierf894efd2016-02-21 10:56:23 +010010243 }
10244 ret = get_errno(getrandom(p, arg2, arg3));
10245 unlock_user(p, arg1, ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010246 return ret;
Laurent Vivierf894efd2016-02-21 10:56:23 +010010247#endif
bellard3532fa72006-06-24 15:06:03 +000010248#ifdef TARGET_NR_socket
10249 case TARGET_NR_socket:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010250 return do_socket(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010251#endif
10252#ifdef TARGET_NR_socketpair
10253 case TARGET_NR_socketpair:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010254 return do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +000010255#endif
10256#ifdef TARGET_NR_setsockopt
10257 case TARGET_NR_setsockopt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010258 return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
bellard3532fa72006-06-24 15:06:03 +000010259#endif
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010260#if defined(TARGET_NR_syslog)
bellard31e31b82003-02-18 22:55:36 +000010261 case TARGET_NR_syslog:
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010262 {
10263 int len = arg2;
ths7494b0f2007-02-11 18:26:53 +000010264
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010265 switch (arg1) {
10266 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
10267 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
10268 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
10269 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
10270 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
10271 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
10272 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
10273 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010274 return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010275 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
10276 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
10277 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
10278 {
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010279 if (len < 0) {
Richard Henderson259841c2018-08-18 12:01:09 -070010280 return -TARGET_EINVAL;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010281 }
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010282 if (len == 0) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010283 return 0;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010284 }
10285 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10286 if (!p) {
Richard Henderson259841c2018-08-18 12:01:09 -070010287 return -TARGET_EFAULT;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010288 }
10289 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
10290 unlock_user(p, arg2, arg3);
10291 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010292 return ret;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010293 default:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010294 return -TARGET_EINVAL;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010295 }
10296 }
10297 break;
10298#endif
bellard31e31b82003-02-18 22:55:36 +000010299 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +000010300 {
bellard66fb9762003-03-23 01:06:05 +000010301 struct itimerval value, ovalue, *pvalue;
10302
pbrook53a59602006-03-25 19:31:22 +000010303 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +000010304 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +000010305 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
10306 || copy_from_user_timeval(&pvalue->it_value,
10307 arg2 + sizeof(struct target_timeval)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010308 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +000010309 } else {
10310 pvalue = NULL;
10311 }
10312 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +000010313 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +000010314 if (copy_to_user_timeval(arg3,
10315 &ovalue.it_interval)
10316 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
10317 &ovalue.it_value))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010318 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +000010319 }
10320 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010321 return ret;
bellard31e31b82003-02-18 22:55:36 +000010322 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +000010323 {
bellard66fb9762003-03-23 01:06:05 +000010324 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +000010325
bellard66fb9762003-03-23 01:06:05 +000010326 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +000010327 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +000010328 if (copy_to_user_timeval(arg2,
10329 &value.it_interval)
10330 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
10331 &value.it_value))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010332 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +000010333 }
10334 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010335 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080010336#ifdef TARGET_NR_stat
bellard31e31b82003-02-18 22:55:36 +000010337 case TARGET_NR_stat:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010338 if (!(p = lock_user_string(arg1))) {
10339 return -TARGET_EFAULT;
10340 }
pbrook53a59602006-03-25 19:31:22 +000010341 ret = get_errno(stat(path(p), &st));
10342 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +000010343 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +080010344#endif
10345#ifdef TARGET_NR_lstat
bellard31e31b82003-02-18 22:55:36 +000010346 case TARGET_NR_lstat:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010347 if (!(p = lock_user_string(arg1))) {
10348 return -TARGET_EFAULT;
10349 }
pbrook53a59602006-03-25 19:31:22 +000010350 ret = get_errno(lstat(path(p), &st));
10351 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +000010352 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +080010353#endif
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010354#ifdef TARGET_NR_fstat
bellard31e31b82003-02-18 22:55:36 +000010355 case TARGET_NR_fstat:
10356 {
10357 ret = get_errno(fstat(arg1, &st));
Chen Gang704eff62015-08-21 05:37:33 +080010358#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
bellard31e31b82003-02-18 22:55:36 +000010359 do_stat:
Chen Gang704eff62015-08-21 05:37:33 +080010360#endif
bellard31e31b82003-02-18 22:55:36 +000010361 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000010362 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +000010363
bellard579a97f2007-11-11 14:26:47 +000010364 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010365 return -TARGET_EFAULT;
Ulrich Hecht12727912009-07-24 19:10:32 +020010366 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +000010367 __put_user(st.st_dev, &target_st->st_dev);
10368 __put_user(st.st_ino, &target_st->st_ino);
10369 __put_user(st.st_mode, &target_st->st_mode);
10370 __put_user(st.st_uid, &target_st->st_uid);
10371 __put_user(st.st_gid, &target_st->st_gid);
10372 __put_user(st.st_nlink, &target_st->st_nlink);
10373 __put_user(st.st_rdev, &target_st->st_rdev);
10374 __put_user(st.st_size, &target_st->st_size);
10375 __put_user(st.st_blksize, &target_st->st_blksize);
10376 __put_user(st.st_blocks, &target_st->st_blocks);
10377 __put_user(st.st_atime, &target_st->target_st_atime);
10378 __put_user(st.st_mtime, &target_st->target_st_mtime);
10379 __put_user(st.st_ctime, &target_st->target_st_ctime);
Michael Forneyfebf6fa2021-05-25 20:55:31 -070010380#if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
Chen-Yu Tsai5f992db2019-05-23 00:21:47 +080010381 __put_user(st.st_atim.tv_nsec,
10382 &target_st->target_st_atime_nsec);
10383 __put_user(st.st_mtim.tv_nsec,
10384 &target_st->target_st_mtime_nsec);
10385 __put_user(st.st_ctim.tv_nsec,
10386 &target_st->target_st_ctime_nsec);
10387#endif
pbrook53a59602006-03-25 19:31:22 +000010388 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +000010389 }
10390 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010391 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010392#endif
bellard31e31b82003-02-18 22:55:36 +000010393 case TARGET_NR_vhangup:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010394 return get_errno(vhangup());
bellard42ad6ae2005-01-03 22:48:11 +000010395#ifdef TARGET_NR_syscall
10396 case TARGET_NR_syscall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010397 return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
10398 arg6, arg7, arg8, 0);
bellard42ad6ae2005-01-03 22:48:11 +000010399#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070010400#if defined(TARGET_NR_wait4)
bellard31e31b82003-02-18 22:55:36 +000010401 case TARGET_NR_wait4:
10402 {
10403 int status;
blueswir1992f48a2007-10-14 16:27:31 +000010404 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +000010405 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +000010406 abi_ulong target_rusage = arg4;
Petar Jovanovica39fb272014-04-08 19:24:30 +020010407 abi_long rusage_err;
bellard31e31b82003-02-18 22:55:36 +000010408 if (target_rusage)
10409 rusage_ptr = &rusage;
10410 else
10411 rusage_ptr = NULL;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +010010412 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
bellard31e31b82003-02-18 22:55:36 +000010413 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +010010414 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +000010415 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +000010416 if (put_user_s32(status, status_ptr))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010417 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +000010418 }
Petar Jovanovica39fb272014-04-08 19:24:30 +020010419 if (target_rusage) {
10420 rusage_err = host_to_target_rusage(target_rusage, &rusage);
10421 if (rusage_err) {
10422 ret = rusage_err;
10423 }
10424 }
bellard31e31b82003-02-18 22:55:36 +000010425 }
10426 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010427 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010428#endif
thse5febef2007-04-01 18:31:35 +000010429#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +000010430 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +000010431 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010432 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010433 ret = get_errno(swapoff(p));
10434 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010435 return ret;
thse5febef2007-04-01 18:31:35 +000010436#endif
bellard31e31b82003-02-18 22:55:36 +000010437 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +000010438 {
pbrook53a59602006-03-25 19:31:22 +000010439 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +000010440 struct sysinfo value;
10441 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +000010442 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +000010443 {
bellard579a97f2007-11-11 14:26:47 +000010444 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010445 return -TARGET_EFAULT;
bellarda5448a72004-06-19 16:59:03 +000010446 __put_user(value.uptime, &target_value->uptime);
10447 __put_user(value.loads[0], &target_value->loads[0]);
10448 __put_user(value.loads[1], &target_value->loads[1]);
10449 __put_user(value.loads[2], &target_value->loads[2]);
10450 __put_user(value.totalram, &target_value->totalram);
10451 __put_user(value.freeram, &target_value->freeram);
10452 __put_user(value.sharedram, &target_value->sharedram);
10453 __put_user(value.bufferram, &target_value->bufferram);
10454 __put_user(value.totalswap, &target_value->totalswap);
10455 __put_user(value.freeswap, &target_value->freeswap);
10456 __put_user(value.procs, &target_value->procs);
10457 __put_user(value.totalhigh, &target_value->totalhigh);
10458 __put_user(value.freehigh, &target_value->freehigh);
10459 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +000010460 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +000010461 }
10462 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010463 return ret;
thse5febef2007-04-01 18:31:35 +000010464#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +000010465 case TARGET_NR_ipc:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010466 return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
thse5febef2007-04-01 18:31:35 +000010467#endif
aurel32e5289082009-04-18 16:16:12 +000010468#ifdef TARGET_NR_semget
10469 case TARGET_NR_semget:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010470 return get_errno(semget(arg1, arg2, arg3));
aurel32e5289082009-04-18 16:16:12 +000010471#endif
10472#ifdef TARGET_NR_semop
10473 case TARGET_NR_semop:
Filip Bozutacac46eb2020-08-25 00:30:50 +020010474 return do_semtimedop(arg1, arg2, arg3, 0, false);
Matus Kyseld8c08b12020-06-26 14:46:11 +020010475#endif
10476#ifdef TARGET_NR_semtimedop
10477 case TARGET_NR_semtimedop:
Filip Bozutacac46eb2020-08-25 00:30:50 +020010478 return do_semtimedop(arg1, arg2, arg3, arg4, false);
10479#endif
10480#ifdef TARGET_NR_semtimedop_time64
10481 case TARGET_NR_semtimedop_time64:
10482 return do_semtimedop(arg1, arg2, arg3, arg4, true);
aurel32e5289082009-04-18 16:16:12 +000010483#endif
10484#ifdef TARGET_NR_semctl
10485 case TARGET_NR_semctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010486 return do_semctl(arg1, arg2, arg3, arg4);
aurel32e5289082009-04-18 16:16:12 +000010487#endif
aurel32eeb438c2008-10-13 21:08:55 +000010488#ifdef TARGET_NR_msgctl
10489 case TARGET_NR_msgctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010490 return do_msgctl(arg1, arg2, arg3);
aurel32eeb438c2008-10-13 21:08:55 +000010491#endif
10492#ifdef TARGET_NR_msgget
10493 case TARGET_NR_msgget:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010494 return get_errno(msgget(arg1, arg2));
aurel32eeb438c2008-10-13 21:08:55 +000010495#endif
10496#ifdef TARGET_NR_msgrcv
10497 case TARGET_NR_msgrcv:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010498 return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
aurel32eeb438c2008-10-13 21:08:55 +000010499#endif
10500#ifdef TARGET_NR_msgsnd
10501 case TARGET_NR_msgsnd:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010502 return do_msgsnd(arg1, arg2, arg3, arg4);
aurel32eeb438c2008-10-13 21:08:55 +000010503#endif
Riku Voipio88a8c982009-04-03 10:42:00 +030010504#ifdef TARGET_NR_shmget
10505 case TARGET_NR_shmget:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010506 return get_errno(shmget(arg1, arg2, arg3));
Riku Voipio88a8c982009-04-03 10:42:00 +030010507#endif
10508#ifdef TARGET_NR_shmctl
10509 case TARGET_NR_shmctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010510 return do_shmctl(arg1, arg2, arg3);
Riku Voipio88a8c982009-04-03 10:42:00 +030010511#endif
10512#ifdef TARGET_NR_shmat
10513 case TARGET_NR_shmat:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010514 return do_shmat(cpu_env, arg1, arg2, arg3);
Riku Voipio88a8c982009-04-03 10:42:00 +030010515#endif
10516#ifdef TARGET_NR_shmdt
10517 case TARGET_NR_shmdt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010518 return do_shmdt(arg1);
Riku Voipio88a8c982009-04-03 10:42:00 +030010519#endif
bellard31e31b82003-02-18 22:55:36 +000010520 case TARGET_NR_fsync:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010521 return get_errno(fsync(arg1));
bellard31e31b82003-02-18 22:55:36 +000010522 case TARGET_NR_clone:
Peter Maydell4ce62432013-07-16 18:44:57 +010010523 /* Linux manages to have three different orderings for its
10524 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10525 * match the kernel's CONFIG_CLONE_* settings.
10526 * Microblaze is further special in that it uses a sixth
10527 * implicit argument to clone for the TLS pointer.
10528 */
10529#if defined(TARGET_MICROBLAZE)
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +020010530 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Peter Maydell4ce62432013-07-16 18:44:57 +010010531#elif defined(TARGET_CLONE_BACKWARDS)
10532 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
10533#elif defined(TARGET_CLONE_BACKWARDS2)
Ulrich Hechta4c075f2009-07-24 16:57:31 +020010534 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +000010535#else
Peter Maydell4ce62432013-07-16 18:44:57 +010010536 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +000010537#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010538 return ret;
bellardec86b0f2003-04-11 00:15:04 +000010539#ifdef __NR_exit_group
10540 /* new thread calls */
10541 case TARGET_NR_exit_group:
Alex Bennée708b6a62018-06-22 17:09:10 +010010542 preexit_cleanup(cpu_env, arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010543 return get_errno(exit_group(arg1));
bellardec86b0f2003-04-11 00:15:04 +000010544#endif
bellard31e31b82003-02-18 22:55:36 +000010545 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +000010546 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010547 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010548 ret = get_errno(setdomainname(p, arg2));
10549 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010550 return ret;
bellard31e31b82003-02-18 22:55:36 +000010551 case TARGET_NR_uname:
10552 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +000010553 {
10554 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +000010555
bellard579a97f2007-11-11 14:26:47 +000010556 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010557 return -TARGET_EFAULT;
bellard29e619b2004-09-13 21:41:04 +000010558 ret = get_errno(sys_uname(buf));
10559 if (!is_error(ret)) {
Peter Maydell332c9782016-07-12 13:02:16 +010010560 /* Overwrite the native machine name with whatever is being
bellard29e619b2004-09-13 21:41:04 +000010561 emulated. */
Philippe Mathieu-Daudé871f95c2017-07-24 15:27:47 -030010562 g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
10563 sizeof(buf->machine));
pbrookc5937222006-05-14 11:30:38 +000010564 /* Allow the user to override the reported release. */
Peter Maydell332c9782016-07-12 13:02:16 +010010565 if (qemu_uname_release && *qemu_uname_release) {
10566 g_strlcpy(buf->release, qemu_uname_release,
10567 sizeof(buf->release));
10568 }
bellard29e619b2004-09-13 21:41:04 +000010569 }
pbrook53a59602006-03-25 19:31:22 +000010570 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +000010571 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010572 return ret;
bellard6dbad632003-03-16 18:05:05 +000010573#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +000010574 case TARGET_NR_modify_ldt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010575 return do_modify_ldt(cpu_env, arg1, arg2, arg3);
j_mayer84409dd2007-04-06 08:56:50 +000010576#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +000010577 case TARGET_NR_vm86:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010578 return do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +000010579#endif
j_mayer84409dd2007-04-06 08:56:50 +000010580#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070010581#if defined(TARGET_NR_adjtimex)
bellard31e31b82003-02-18 22:55:36 +000010582 case TARGET_NR_adjtimex:
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020010583 {
10584 struct timex host_buf;
10585
10586 if (target_to_host_timex(&host_buf, arg1) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010587 return -TARGET_EFAULT;
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020010588 }
10589 ret = get_errno(adjtimex(&host_buf));
10590 if (!is_error(ret)) {
10591 if (host_to_target_timex(arg1, &host_buf) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010592 return -TARGET_EFAULT;
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020010593 }
10594 }
10595 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010596 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010597#endif
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010598#if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10599 case TARGET_NR_clock_adjtime:
10600 {
10601 struct timex htx, *phtx = &htx;
10602
10603 if (target_to_host_timex(phtx, arg2) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010604 return -TARGET_EFAULT;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010605 }
10606 ret = get_errno(clock_adjtime(arg1, phtx));
10607 if (!is_error(ret) && phtx) {
10608 if (host_to_target_timex(arg2, phtx) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010609 return -TARGET_EFAULT;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010610 }
10611 }
10612 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010613 return ret;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010614#endif
Filip Bozuta6ac03b22020-08-24 21:21:15 +020010615#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
10616 case TARGET_NR_clock_adjtime64:
10617 {
10618 struct timex htx;
10619
10620 if (target_to_host_timex64(&htx, arg2) != 0) {
10621 return -TARGET_EFAULT;
10622 }
10623 ret = get_errno(clock_adjtime(arg1, &htx));
10624 if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
10625 return -TARGET_EFAULT;
10626 }
10627 }
10628 return ret;
10629#endif
bellard31e31b82003-02-18 22:55:36 +000010630 case TARGET_NR_getpgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010631 return get_errno(getpgid(arg1));
bellard31e31b82003-02-18 22:55:36 +000010632 case TARGET_NR_fchdir:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010633 return get_errno(fchdir(arg1));
bellard31e31b82003-02-18 22:55:36 +000010634 case TARGET_NR_personality:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010635 return get_errno(personality(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +000010636#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +000010637 case TARGET_NR__llseek:
10638 {
10639 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +000010640#if !defined(__NR_llseek)
Peter Maydell9fea2732016-08-11 18:59:39 +010010641 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
Peter Maydell0c1592d2011-02-22 13:02:26 +000010642 if (res == -1) {
10643 ret = get_errno(res);
10644 } else {
10645 ret = 0;
10646 }
10647#else
bellard31e31b82003-02-18 22:55:36 +000010648 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +000010649#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +000010650 if ((ret == 0) && put_user_s64(res, arg4)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010651 return -TARGET_EFAULT;
Peter Maydell0c1592d2011-02-22 13:02:26 +000010652 }
bellard31e31b82003-02-18 22:55:36 +000010653 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010654 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +000010655#endif
Chen Gang704eff62015-08-21 05:37:33 +080010656#ifdef TARGET_NR_getdents
bellard31e31b82003-02-18 22:55:36 +000010657 case TARGET_NR_getdents:
Richard Hendersonfd08ddb2021-11-14 11:35:36 +010010658 return do_getdents(arg1, arg2, arg3);
Chen Gang704eff62015-08-21 05:37:33 +080010659#endif /* TARGET_NR_getdents */
ths3ae43202007-09-16 21:39:48 +000010660#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +000010661 case TARGET_NR_getdents64:
Richard Hendersonfd08ddb2021-11-14 11:35:36 +010010662 return do_getdents64(arg1, arg2, arg3);
bellarda541f292004-04-12 20:39:29 +000010663#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +010010664#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +000010665 case TARGET_NR__newselect:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010666 return do_select(arg1, arg2, arg3, arg4, arg5);
thse5febef2007-04-01 18:31:35 +000010667#endif
Filip Bozutae5ce9682020-08-25 00:30:49 +020010668#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +000010669 case TARGET_NR_poll:
Filip Bozutae5ce9682020-08-25 00:30:49 +020010670 return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
10671#endif
10672#ifdef TARGET_NR_ppoll
Mike Frysingerd8035d42011-02-07 01:05:51 -050010673 case TARGET_NR_ppoll:
Filip Bozutae5ce9682020-08-25 00:30:49 +020010674 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
10675#endif
10676#ifdef TARGET_NR_ppoll_time64
10677 case TARGET_NR_ppoll_time64:
10678 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
thse5febef2007-04-01 18:31:35 +000010679#endif
bellard31e31b82003-02-18 22:55:36 +000010680 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +000010681 /* NOTE: the flock constant seems to be the same for every
10682 Linux platform */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010683 return get_errno(safe_flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +000010684 case TARGET_NR_readv:
10685 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070010686 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
10687 if (vec != NULL) {
Peter Maydell918c03e2016-06-06 19:58:02 +010010688 ret = get_errno(safe_readv(arg1, vec, arg3));
Richard Hendersonf287b2c2012-09-15 13:20:25 -070010689 unlock_iovec(vec, arg2, arg3, 1);
10690 } else {
10691 ret = -host_to_target_errno(errno);
10692 }
bellard31e31b82003-02-18 22:55:36 +000010693 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010694 return ret;
bellard31e31b82003-02-18 22:55:36 +000010695 case TARGET_NR_writev:
10696 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070010697 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10698 if (vec != NULL) {
Peter Maydell918c03e2016-06-06 19:58:02 +010010699 ret = get_errno(safe_writev(arg1, vec, arg3));
Richard Hendersonf287b2c2012-09-15 13:20:25 -070010700 unlock_iovec(vec, arg2, arg3, 0);
10701 } else {
10702 ret = -host_to_target_errno(errno);
10703 }
bellard31e31b82003-02-18 22:55:36 +000010704 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010705 return ret;
Dejan Jovicevic0f263862016-10-11 11:52:46 +020010706#if defined(TARGET_NR_preadv)
10707 case TARGET_NR_preadv:
10708 {
10709 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
10710 if (vec != NULL) {
Max Filippov9ac22512018-04-04 17:30:41 -070010711 unsigned long low, high;
10712
10713 target_to_host_low_high(arg4, arg5, &low, &high);
10714 ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
Dejan Jovicevic0f263862016-10-11 11:52:46 +020010715 unlock_iovec(vec, arg2, arg3, 1);
10716 } else {
10717 ret = -host_to_target_errno(errno);
10718 }
10719 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010720 return ret;
Dejan Jovicevic0f263862016-10-11 11:52:46 +020010721#endif
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +020010722#if defined(TARGET_NR_pwritev)
10723 case TARGET_NR_pwritev:
10724 {
10725 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10726 if (vec != NULL) {
Max Filippov9ac22512018-04-04 17:30:41 -070010727 unsigned long low, high;
10728
10729 target_to_host_low_high(arg4, arg5, &low, &high);
10730 ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +020010731 unlock_iovec(vec, arg2, arg3, 0);
10732 } else {
10733 ret = -host_to_target_errno(errno);
10734 }
10735 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010736 return ret;
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +020010737#endif
bellard31e31b82003-02-18 22:55:36 +000010738 case TARGET_NR_getsid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010739 return get_errno(getsid(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +000010740#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +000010741 case TARGET_NR_fdatasync:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010742 return get_errno(fdatasync(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +000010743#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -050010744 case TARGET_NR_sched_getaffinity:
10745 {
10746 unsigned int mask_size;
10747 unsigned long *mask;
10748
10749 /*
10750 * sched_getaffinity needs multiples of ulong, so need to take
10751 * care of mismatches between target ulong and host ulong sizes.
10752 */
10753 if (arg2 & (sizeof(abi_ulong) - 1)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010754 return -TARGET_EINVAL;
Mike Frysinger737de1d2011-02-07 01:05:55 -050010755 }
10756 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
10757
10758 mask = alloca(mask_size);
Samuel Thibault2e0a8712018-01-09 21:16:43 +010010759 memset(mask, 0, mask_size);
Mike Frysinger737de1d2011-02-07 01:05:55 -050010760 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
10761
10762 if (!is_error(ret)) {
Peter Maydellbe3bd282014-05-15 14:40:23 +010010763 if (ret > arg2) {
10764 /* More data returned than the caller's buffer will fit.
10765 * This only happens if sizeof(abi_long) < sizeof(long)
10766 * and the caller passed us a buffer holding an odd number
10767 * of abi_longs. If the host kernel is actually using the
10768 * extra 4 bytes then fail EINVAL; otherwise we can just
10769 * ignore them and only copy the interesting part.
10770 */
10771 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
10772 if (numcpus > arg2 * 8) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010773 return -TARGET_EINVAL;
Peter Maydellbe3bd282014-05-15 14:40:23 +010010774 }
10775 ret = arg2;
10776 }
10777
Samuel Thibault5fdefcf2018-02-11 18:47:04 +010010778 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010779 return -TARGET_EFAULT;
Samuel Thibault5fdefcf2018-02-11 18:47:04 +010010780 }
Mike Frysinger737de1d2011-02-07 01:05:55 -050010781 }
10782 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010783 return ret;
Mike Frysinger737de1d2011-02-07 01:05:55 -050010784 case TARGET_NR_sched_setaffinity:
10785 {
10786 unsigned int mask_size;
10787 unsigned long *mask;
10788
10789 /*
10790 * sched_setaffinity needs multiples of ulong, so need to take
10791 * care of mismatches between target ulong and host ulong sizes.
10792 */
10793 if (arg2 & (sizeof(abi_ulong) - 1)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010794 return -TARGET_EINVAL;
Mike Frysinger737de1d2011-02-07 01:05:55 -050010795 }
10796 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
Mike Frysinger737de1d2011-02-07 01:05:55 -050010797 mask = alloca(mask_size);
Samuel Thibault2e0a8712018-01-09 21:16:43 +010010798
10799 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
10800 if (ret) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010801 return ret;
Mike Frysinger737de1d2011-02-07 01:05:55 -050010802 }
Mike Frysinger737de1d2011-02-07 01:05:55 -050010803
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010804 return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
Mike Frysinger737de1d2011-02-07 01:05:55 -050010805 }
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010010806 case TARGET_NR_getcpu:
10807 {
10808 unsigned cpu, node;
10809 ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
10810 arg2 ? &node : NULL,
10811 NULL));
10812 if (is_error(ret)) {
Richard Henderson259841c2018-08-18 12:01:09 -070010813 return ret;
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010010814 }
10815 if (arg1 && put_user_u32(cpu, arg1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010816 return -TARGET_EFAULT;
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010010817 }
10818 if (arg2 && put_user_u32(node, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010819 return -TARGET_EFAULT;
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010010820 }
10821 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010822 return ret;
bellard31e31b82003-02-18 22:55:36 +000010823 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +000010824 {
Tonis Tiigi407a1192022-01-04 20:18:19 -080010825 struct target_sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +000010826 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +000010827
Tom Mustaa1d5c5b2014-08-12 13:53:38 -050010828 if (arg2 == 0) {
10829 return -TARGET_EINVAL;
10830 }
Tonis Tiigi407a1192022-01-04 20:18:19 -080010831 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010832 return -TARGET_EFAULT;
Tonis Tiigi407a1192022-01-04 20:18:19 -080010833 }
bellard5cd43932003-03-29 16:54:36 +000010834 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +000010835 unlock_user_struct(target_schp, arg2, 0);
Tonis Tiigi407a1192022-01-04 20:18:19 -080010836 return get_errno(sys_sched_setparam(arg1, &schp));
bellard5cd43932003-03-29 16:54:36 +000010837 }
bellard31e31b82003-02-18 22:55:36 +000010838 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +000010839 {
Tonis Tiigi407a1192022-01-04 20:18:19 -080010840 struct target_sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +000010841 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -050010842
10843 if (arg2 == 0) {
10844 return -TARGET_EINVAL;
10845 }
Tonis Tiigi407a1192022-01-04 20:18:19 -080010846 ret = get_errno(sys_sched_getparam(arg1, &schp));
bellard5cd43932003-03-29 16:54:36 +000010847 if (!is_error(ret)) {
Tonis Tiigi407a1192022-01-04 20:18:19 -080010848 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010849 return -TARGET_EFAULT;
Tonis Tiigi407a1192022-01-04 20:18:19 -080010850 }
bellard5cd43932003-03-29 16:54:36 +000010851 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +000010852 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +000010853 }
10854 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010855 return ret;
bellard31e31b82003-02-18 22:55:36 +000010856 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +000010857 {
Tonis Tiigi407a1192022-01-04 20:18:19 -080010858 struct target_sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +000010859 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -050010860 if (arg3 == 0) {
10861 return -TARGET_EINVAL;
10862 }
Tonis Tiigi407a1192022-01-04 20:18:19 -080010863 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010864 return -TARGET_EFAULT;
Tonis Tiigi407a1192022-01-04 20:18:19 -080010865 }
bellard5cd43932003-03-29 16:54:36 +000010866 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +000010867 unlock_user_struct(target_schp, arg3, 0);
Tonis Tiigi407a1192022-01-04 20:18:19 -080010868 return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
bellard5cd43932003-03-29 16:54:36 +000010869 }
bellard31e31b82003-02-18 22:55:36 +000010870 case TARGET_NR_sched_getscheduler:
Tonis Tiigi407a1192022-01-04 20:18:19 -080010871 return get_errno(sys_sched_getscheduler(arg1));
Tonis Tiigi45ad7612022-01-04 20:18:18 -080010872 case TARGET_NR_sched_getattr:
10873 {
10874 struct target_sched_attr *target_scha;
10875 struct sched_attr scha;
10876 if (arg2 == 0) {
10877 return -TARGET_EINVAL;
10878 }
10879 if (arg3 > sizeof(scha)) {
10880 arg3 = sizeof(scha);
10881 }
10882 ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
10883 if (!is_error(ret)) {
10884 target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10885 if (!target_scha) {
10886 return -TARGET_EFAULT;
10887 }
10888 target_scha->size = tswap32(scha.size);
10889 target_scha->sched_policy = tswap32(scha.sched_policy);
10890 target_scha->sched_flags = tswap64(scha.sched_flags);
10891 target_scha->sched_nice = tswap32(scha.sched_nice);
10892 target_scha->sched_priority = tswap32(scha.sched_priority);
10893 target_scha->sched_runtime = tswap64(scha.sched_runtime);
10894 target_scha->sched_deadline = tswap64(scha.sched_deadline);
10895 target_scha->sched_period = tswap64(scha.sched_period);
10896 if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
10897 target_scha->sched_util_min = tswap32(scha.sched_util_min);
10898 target_scha->sched_util_max = tswap32(scha.sched_util_max);
10899 }
10900 unlock_user(target_scha, arg2, arg3);
10901 }
10902 return ret;
10903 }
10904 case TARGET_NR_sched_setattr:
10905 {
10906 struct target_sched_attr *target_scha;
10907 struct sched_attr scha;
10908 uint32_t size;
10909 int zeroed;
10910 if (arg2 == 0) {
10911 return -TARGET_EINVAL;
10912 }
10913 if (get_user_u32(size, arg2)) {
10914 return -TARGET_EFAULT;
10915 }
10916 if (!size) {
10917 size = offsetof(struct target_sched_attr, sched_util_min);
10918 }
10919 if (size < offsetof(struct target_sched_attr, sched_util_min)) {
10920 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
10921 return -TARGET_EFAULT;
10922 }
10923 return -TARGET_E2BIG;
10924 }
10925
10926 zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
10927 if (zeroed < 0) {
10928 return zeroed;
10929 } else if (zeroed == 0) {
10930 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
10931 return -TARGET_EFAULT;
10932 }
10933 return -TARGET_E2BIG;
10934 }
10935 if (size > sizeof(struct target_sched_attr)) {
10936 size = sizeof(struct target_sched_attr);
10937 }
10938
10939 target_scha = lock_user(VERIFY_READ, arg2, size, 1);
10940 if (!target_scha) {
10941 return -TARGET_EFAULT;
10942 }
10943 scha.size = size;
10944 scha.sched_policy = tswap32(target_scha->sched_policy);
10945 scha.sched_flags = tswap64(target_scha->sched_flags);
10946 scha.sched_nice = tswap32(target_scha->sched_nice);
10947 scha.sched_priority = tswap32(target_scha->sched_priority);
10948 scha.sched_runtime = tswap64(target_scha->sched_runtime);
10949 scha.sched_deadline = tswap64(target_scha->sched_deadline);
10950 scha.sched_period = tswap64(target_scha->sched_period);
10951 if (size > offsetof(struct target_sched_attr, sched_util_min)) {
10952 scha.sched_util_min = tswap32(target_scha->sched_util_min);
10953 scha.sched_util_max = tswap32(target_scha->sched_util_max);
10954 }
10955 unlock_user(target_scha, arg2, 0);
10956 return get_errno(sys_sched_setattr(arg1, &scha, arg3));
10957 }
bellard31e31b82003-02-18 22:55:36 +000010958 case TARGET_NR_sched_yield:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010959 return get_errno(sched_yield());
bellard31e31b82003-02-18 22:55:36 +000010960 case TARGET_NR_sched_get_priority_max:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010961 return get_errno(sched_get_priority_max(arg1));
bellard31e31b82003-02-18 22:55:36 +000010962 case TARGET_NR_sched_get_priority_min:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010963 return get_errno(sched_get_priority_min(arg1));
Alistair Francis859e8a82020-03-12 15:13:49 -070010964#ifdef TARGET_NR_sched_rr_get_interval
bellard31e31b82003-02-18 22:55:36 +000010965 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +000010966 {
bellard5cd43932003-03-29 16:54:36 +000010967 struct timespec ts;
10968 ret = get_errno(sched_rr_get_interval(arg1, &ts));
10969 if (!is_error(ret)) {
Tom Mustad4290c42014-08-12 13:53:39 -050010970 ret = host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +000010971 }
10972 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010973 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010974#endif
Filip Bozutaddcbde12020-08-24 21:21:16 +020010975#ifdef TARGET_NR_sched_rr_get_interval_time64
10976 case TARGET_NR_sched_rr_get_interval_time64:
10977 {
10978 struct timespec ts;
10979 ret = get_errno(sched_rr_get_interval(arg1, &ts));
10980 if (!is_error(ret)) {
10981 ret = host_to_target_timespec64(arg2, &ts);
10982 }
10983 }
10984 return ret;
10985#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070010986#if defined(TARGET_NR_nanosleep)
bellard31e31b82003-02-18 22:55:36 +000010987 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +000010988 {
bellard1b6b0292003-03-22 17:31:38 +000010989 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +000010990 target_to_host_timespec(&req, arg1);
Peter Maydell9e518222016-06-06 19:58:09 +010010991 ret = get_errno(safe_nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +000010992 if (is_error(ret) && arg2) {
10993 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +000010994 }
10995 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010996 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010997#endif
bellard31e31b82003-02-18 22:55:36 +000010998 case TARGET_NR_prctl:
Richard Henderson87e9bf22021-12-27 07:01:22 -080010999 return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
ths39b9aae2007-02-11 18:36:44 +000011000 break;
bellardd2fd1af2007-11-14 18:08:56 +000011001#ifdef TARGET_NR_arch_prctl
11002 case TARGET_NR_arch_prctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011003 return do_arch_prctl(cpu_env, arg1, arg2);
bellardd2fd1af2007-11-14 18:08:56 +000011004#endif
aurel32f2c7ba12008-03-28 22:32:06 +000011005#ifdef TARGET_NR_pread64
11006 case TARGET_NR_pread64:
James Clarke8bf8e9d2017-09-15 20:33:13 +010011007 if (regpairs_aligned(cpu_env, num)) {
Alexander Grafae017a52012-09-29 15:32:39 +000011008 arg4 = arg5;
11009 arg5 = arg6;
11010 }
Peter Maydell2bd3f892019-01-08 18:49:00 +000011011 if (arg2 == 0 && arg3 == 0) {
11012 /* Special-case NULL buffer and zero length, which should succeed */
11013 p = 0;
11014 } else {
11015 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11016 if (!p) {
11017 return -TARGET_EFAULT;
11018 }
11019 }
aurel32f2c7ba12008-03-28 22:32:06 +000011020 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
11021 unlock_user(p, arg2, ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011022 return ret;
aurel32f2c7ba12008-03-28 22:32:06 +000011023 case TARGET_NR_pwrite64:
James Clarke8bf8e9d2017-09-15 20:33:13 +010011024 if (regpairs_aligned(cpu_env, num)) {
Alexander Grafae017a52012-09-29 15:32:39 +000011025 arg4 = arg5;
11026 arg5 = arg6;
11027 }
Peter Maydell2bd3f892019-01-08 18:49:00 +000011028 if (arg2 == 0 && arg3 == 0) {
11029 /* Special-case NULL buffer and zero length, which should succeed */
11030 p = 0;
11031 } else {
11032 p = lock_user(VERIFY_READ, arg2, arg3, 1);
11033 if (!p) {
11034 return -TARGET_EFAULT;
11035 }
11036 }
aurel32f2c7ba12008-03-28 22:32:06 +000011037 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
11038 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011039 return ret;
aurel32f2c7ba12008-03-28 22:32:06 +000011040#endif
bellard31e31b82003-02-18 22:55:36 +000011041 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +000011042 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011043 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011044 ret = get_errno(sys_getcwd1(p, arg2));
11045 unlock_user(p, arg1, ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011046 return ret;
bellard31e31b82003-02-18 22:55:36 +000011047 case TARGET_NR_capget:
11048 case TARGET_NR_capset:
Peter Maydelle0eb2102014-03-17 12:15:35 +000011049 {
11050 struct target_user_cap_header *target_header;
11051 struct target_user_cap_data *target_data = NULL;
11052 struct __user_cap_header_struct header;
11053 struct __user_cap_data_struct data[2];
11054 struct __user_cap_data_struct *dataptr = NULL;
11055 int i, target_datalen;
11056 int data_items = 1;
11057
11058 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011059 return -TARGET_EFAULT;
Peter Maydelle0eb2102014-03-17 12:15:35 +000011060 }
11061 header.version = tswap32(target_header->version);
11062 header.pid = tswap32(target_header->pid);
11063
Peter Maydellec864872014-03-19 16:07:30 +000011064 if (header.version != _LINUX_CAPABILITY_VERSION) {
Peter Maydelle0eb2102014-03-17 12:15:35 +000011065 /* Version 2 and up takes pointer to two user_data structs */
11066 data_items = 2;
11067 }
11068
11069 target_datalen = sizeof(*target_data) * data_items;
11070
11071 if (arg2) {
11072 if (num == TARGET_NR_capget) {
11073 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
11074 } else {
11075 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
11076 }
11077 if (!target_data) {
11078 unlock_user_struct(target_header, arg1, 0);
Richard Henderson2852aaf2018-08-18 12:01:06 -070011079 return -TARGET_EFAULT;
Peter Maydelle0eb2102014-03-17 12:15:35 +000011080 }
11081
11082 if (num == TARGET_NR_capset) {
11083 for (i = 0; i < data_items; i++) {
11084 data[i].effective = tswap32(target_data[i].effective);
11085 data[i].permitted = tswap32(target_data[i].permitted);
11086 data[i].inheritable = tswap32(target_data[i].inheritable);
11087 }
11088 }
11089
11090 dataptr = data;
11091 }
11092
11093 if (num == TARGET_NR_capget) {
11094 ret = get_errno(capget(&header, dataptr));
11095 } else {
11096 ret = get_errno(capset(&header, dataptr));
11097 }
11098
11099 /* The kernel always updates version for both capget and capset */
11100 target_header->version = tswap32(header.version);
11101 unlock_user_struct(target_header, arg1, 1);
11102
11103 if (arg2) {
11104 if (num == TARGET_NR_capget) {
11105 for (i = 0; i < data_items; i++) {
11106 target_data[i].effective = tswap32(data[i].effective);
11107 target_data[i].permitted = tswap32(data[i].permitted);
11108 target_data[i].inheritable = tswap32(data[i].inheritable);
11109 }
11110 unlock_user(target_data, arg2, target_datalen);
11111 } else {
11112 unlock_user(target_data, arg2, 0);
11113 }
11114 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011115 return ret;
Peter Maydelle0eb2102014-03-17 12:15:35 +000011116 }
bellard31e31b82003-02-18 22:55:36 +000011117 case TARGET_NR_sigaltstack:
Richard Henderson6b208752021-04-25 19:53:12 -070011118 return do_sigaltstack(arg1, arg2, cpu_env);
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011119
11120#ifdef CONFIG_SENDFILE
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020011121#ifdef TARGET_NR_sendfile
bellard31e31b82003-02-18 22:55:36 +000011122 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011123 {
11124 off_t *offp = NULL;
11125 off_t off;
11126 if (arg3) {
11127 ret = get_user_sal(off, arg3);
11128 if (is_error(ret)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011129 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011130 }
11131 offp = &off;
11132 }
11133 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11134 if (!is_error(ret) && arg3) {
11135 abi_long ret2 = put_user_sal(off, arg3);
11136 if (is_error(ret2)) {
11137 ret = ret2;
11138 }
11139 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011140 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011141 }
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020011142#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011143#ifdef TARGET_NR_sendfile64
11144 case TARGET_NR_sendfile64:
11145 {
11146 off_t *offp = NULL;
11147 off_t off;
11148 if (arg3) {
11149 ret = get_user_s64(off, arg3);
11150 if (is_error(ret)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011151 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011152 }
11153 offp = &off;
11154 }
11155 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11156 if (!is_error(ret) && arg3) {
11157 abi_long ret2 = put_user_s64(off, arg3);
11158 if (is_error(ret2)) {
11159 ret = ret2;
11160 }
11161 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011162 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011163 }
11164#endif
bellardebc05482003-09-30 21:08:41 +000011165#endif
bellard048f6b42005-11-26 18:47:20 +000011166#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +000011167 case TARGET_NR_vfork:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011168 return get_errno(do_fork(cpu_env,
11169 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
11170 0, 0, 0, 0));
bellard048f6b42005-11-26 18:47:20 +000011171#endif
bellardebc05482003-09-30 21:08:41 +000011172#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +000011173 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +000011174 {
11175 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +030011176 int resource = target_to_host_resource(arg1);
11177 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +000011178 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000011179 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +000011180 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011181 return -TARGET_EFAULT;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +090011182 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
11183 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +000011184 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +000011185 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011186 return ret;
bellard728584b2003-04-29 20:43:36 +000011187 }
bellardebc05482003-09-30 21:08:41 +000011188#endif
bellarda315a142005-01-30 22:59:18 +000011189#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +000011190 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +000011191 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011192 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011193 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
11194 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011195 return ret;
bellarda315a142005-01-30 22:59:18 +000011196#endif
11197#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +000011198 case TARGET_NR_ftruncate64:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011199 return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellarda315a142005-01-30 22:59:18 +000011200#endif
11201#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +000011202 case TARGET_NR_stat64:
Richard Henderson2852aaf2018-08-18 12:01:06 -070011203 if (!(p = lock_user_string(arg1))) {
11204 return -TARGET_EFAULT;
11205 }
pbrook53a59602006-03-25 19:31:22 +000011206 ret = get_errno(stat(path(p), &st));
11207 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +000011208 if (!is_error(ret))
11209 ret = host_to_target_stat64(cpu_env, arg2, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011210 return ret;
bellarda315a142005-01-30 22:59:18 +000011211#endif
11212#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +000011213 case TARGET_NR_lstat64:
Richard Henderson2852aaf2018-08-18 12:01:06 -070011214 if (!(p = lock_user_string(arg1))) {
11215 return -TARGET_EFAULT;
11216 }
pbrook53a59602006-03-25 19:31:22 +000011217 ret = get_errno(lstat(path(p), &st));
11218 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +000011219 if (!is_error(ret))
11220 ret = host_to_target_stat64(cpu_env, arg2, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011221 return ret;
bellarda315a142005-01-30 22:59:18 +000011222#endif
11223#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +000011224 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +000011225 ret = get_errno(fstat(arg1, &st));
11226 if (!is_error(ret))
11227 ret = host_to_target_stat64(cpu_env, arg2, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011228 return ret;
bellardec86b0f2003-04-11 00:15:04 +000011229#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +010011230#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +000011231#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +000011232 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +000011233#endif
11234#ifdef TARGET_NR_newfstatat
11235 case TARGET_NR_newfstatat:
11236#endif
Richard Henderson2852aaf2018-08-18 12:01:06 -070011237 if (!(p = lock_user_string(arg2))) {
11238 return -TARGET_EFAULT;
11239 }
Peter Maydellc0d472b2013-06-12 16:20:21 +010011240 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
Richard Henderson2852aaf2018-08-18 12:01:06 -070011241 unlock_user(p, arg2, 0);
balrog6a24a772008-09-20 02:23:36 +000011242 if (!is_error(ret))
11243 ret = host_to_target_stat64(cpu_env, arg3, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011244 return ret;
bellarda315a142005-01-30 22:59:18 +000011245#endif
Aleksandar Rikaloefa92182019-06-28 12:43:34 +020011246#if defined(TARGET_NR_statx)
11247 case TARGET_NR_statx:
11248 {
11249 struct target_statx *target_stx;
11250 int dirfd = arg1;
11251 int flags = arg3;
11252
11253 p = lock_user_string(arg2);
11254 if (p == NULL) {
11255 return -TARGET_EFAULT;
11256 }
11257#if defined(__NR_statx)
11258 {
11259 /*
11260 * It is assumed that struct statx is architecture independent.
11261 */
11262 struct target_statx host_stx;
11263 int mask = arg4;
11264
11265 ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
11266 if (!is_error(ret)) {
11267 if (host_to_target_statx(&host_stx, arg5) != 0) {
11268 unlock_user(p, arg2, 0);
11269 return -TARGET_EFAULT;
11270 }
11271 }
11272
11273 if (ret != -TARGET_ENOSYS) {
11274 unlock_user(p, arg2, 0);
11275 return ret;
11276 }
11277 }
11278#endif
11279 ret = get_errno(fstatat(dirfd, path(p), &st, flags));
11280 unlock_user(p, arg2, 0);
11281
11282 if (!is_error(ret)) {
11283 if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
11284 return -TARGET_EFAULT;
11285 }
11286 memset(target_stx, 0, sizeof(*target_stx));
11287 __put_user(major(st.st_dev), &target_stx->stx_dev_major);
11288 __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
11289 __put_user(st.st_ino, &target_stx->stx_ino);
11290 __put_user(st.st_mode, &target_stx->stx_mode);
11291 __put_user(st.st_uid, &target_stx->stx_uid);
11292 __put_user(st.st_gid, &target_stx->stx_gid);
11293 __put_user(st.st_nlink, &target_stx->stx_nlink);
11294 __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
11295 __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
11296 __put_user(st.st_size, &target_stx->stx_size);
11297 __put_user(st.st_blksize, &target_stx->stx_blksize);
11298 __put_user(st.st_blocks, &target_stx->stx_blocks);
11299 __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
11300 __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
11301 __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
11302 unlock_user_struct(target_stx, arg5, 1);
11303 }
11304 }
11305 return ret;
11306#endif
Chen Gang704eff62015-08-21 05:37:33 +080011307#ifdef TARGET_NR_lchown
bellard67867302003-11-23 17:05:30 +000011308 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +000011309 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011310 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011311 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
11312 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011313 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080011314#endif
Riku Voipio0c866a72011-04-18 15:23:06 +030011315#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +000011316 case TARGET_NR_getuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011317 return get_errno(high2lowuid(getuid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011318#endif
11319#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +000011320 case TARGET_NR_getgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011321 return get_errno(high2lowgid(getgid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011322#endif
11323#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +000011324 case TARGET_NR_geteuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011325 return get_errno(high2lowuid(geteuid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011326#endif
11327#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +000011328 case TARGET_NR_getegid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011329 return get_errno(high2lowgid(getegid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011330#endif
bellard67867302003-11-23 17:05:30 +000011331 case TARGET_NR_setreuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011332 return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
bellard67867302003-11-23 17:05:30 +000011333 case TARGET_NR_setregid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011334 return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
bellard67867302003-11-23 17:05:30 +000011335 case TARGET_NR_getgroups:
11336 {
11337 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +030011338 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +000011339 gid_t *grouplist;
11340 int i;
11341
11342 grouplist = alloca(gidsetsize * sizeof(gid_t));
11343 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +000011344 if (gidsetsize == 0)
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011345 return ret;
bellard67867302003-11-23 17:05:30 +000011346 if (!is_error(ret)) {
Andreas Schwab03903ff2013-04-09 05:41:33 +000011347 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
bellard579a97f2007-11-11 14:26:47 +000011348 if (!target_grouplist)
Richard Henderson2852aaf2018-08-18 12:01:06 -070011349 return -TARGET_EFAULT;
balroga2155fc2008-09-20 02:12:08 +000011350 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +030011351 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
Andreas Schwab03903ff2013-04-09 05:41:33 +000011352 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
bellard67867302003-11-23 17:05:30 +000011353 }
11354 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011355 return ret;
bellard67867302003-11-23 17:05:30 +000011356 case TARGET_NR_setgroups:
11357 {
11358 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +030011359 target_id *target_grouplist;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -050011360 gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +000011361 int i;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -050011362 if (gidsetsize) {
11363 grouplist = alloca(gidsetsize * sizeof(gid_t));
Andreas Schwab03903ff2013-04-09 05:41:33 +000011364 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -050011365 if (!target_grouplist) {
Richard Henderson259841c2018-08-18 12:01:09 -070011366 return -TARGET_EFAULT;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -050011367 }
11368 for (i = 0; i < gidsetsize; i++) {
11369 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
11370 }
11371 unlock_user(target_grouplist, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +000011372 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011373 return get_errno(setgroups(gidsetsize, grouplist));
bellard67867302003-11-23 17:05:30 +000011374 }
bellard67867302003-11-23 17:05:30 +000011375 case TARGET_NR_fchown:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011376 return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
Peter Maydellc0d472b2013-06-12 16:20:21 +010011377#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +000011378 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +000011379 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011380 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +010011381 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
11382 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +000011383 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011384 return ret;
thsccfa72b2007-09-24 09:23:34 +000011385#endif
bellard67867302003-11-23 17:05:30 +000011386#ifdef TARGET_NR_setresuid
11387 case TARGET_NR_setresuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011388 return get_errno(sys_setresuid(low2highuid(arg1),
11389 low2highuid(arg2),
11390 low2highuid(arg3)));
bellard67867302003-11-23 17:05:30 +000011391#endif
11392#ifdef TARGET_NR_getresuid
11393 case TARGET_NR_getresuid:
11394 {
pbrook53a59602006-03-25 19:31:22 +000011395 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +000011396 ret = get_errno(getresuid(&ruid, &euid, &suid));
11397 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +000011398 if (put_user_id(high2lowuid(ruid), arg1)
11399 || put_user_id(high2lowuid(euid), arg2)
11400 || put_user_id(high2lowuid(suid), arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011401 return -TARGET_EFAULT;
bellard67867302003-11-23 17:05:30 +000011402 }
11403 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011404 return ret;
bellard67867302003-11-23 17:05:30 +000011405#endif
11406#ifdef TARGET_NR_getresgid
11407 case TARGET_NR_setresgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011408 return get_errno(sys_setresgid(low2highgid(arg1),
11409 low2highgid(arg2),
11410 low2highgid(arg3)));
bellard67867302003-11-23 17:05:30 +000011411#endif
11412#ifdef TARGET_NR_getresgid
11413 case TARGET_NR_getresgid:
11414 {
pbrook53a59602006-03-25 19:31:22 +000011415 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +000011416 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11417 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +000011418 if (put_user_id(high2lowgid(rgid), arg1)
11419 || put_user_id(high2lowgid(egid), arg2)
11420 || put_user_id(high2lowgid(sgid), arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011421 return -TARGET_EFAULT;
bellard67867302003-11-23 17:05:30 +000011422 }
11423 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011424 return ret;
bellard67867302003-11-23 17:05:30 +000011425#endif
Chen Gang704eff62015-08-21 05:37:33 +080011426#ifdef TARGET_NR_chown
bellard67867302003-11-23 17:05:30 +000011427 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +000011428 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011429 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011430 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
11431 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011432 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080011433#endif
bellard67867302003-11-23 17:05:30 +000011434 case TARGET_NR_setuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011435 return get_errno(sys_setuid(low2highuid(arg1)));
bellard67867302003-11-23 17:05:30 +000011436 case TARGET_NR_setgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011437 return get_errno(sys_setgid(low2highgid(arg1)));
bellard67867302003-11-23 17:05:30 +000011438 case TARGET_NR_setfsuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011439 return get_errno(setfsuid(arg1));
bellard67867302003-11-23 17:05:30 +000011440 case TARGET_NR_setfsgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011441 return get_errno(setfsgid(arg1));
bellard67867302003-11-23 17:05:30 +000011442
bellarda315a142005-01-30 22:59:18 +000011443#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +000011444 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +000011445 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011446 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011447 ret = get_errno(lchown(p, arg2, arg3));
11448 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011449 return ret;
bellarda315a142005-01-30 22:59:18 +000011450#endif
11451#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +000011452 case TARGET_NR_getuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011453 return get_errno(getuid());
bellarda315a142005-01-30 22:59:18 +000011454#endif
aurel3264b4d282008-11-14 17:20:15 +000011455
11456#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11457 /* Alpha specific */
11458 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -080011459 {
11460 uid_t euid;
11461 euid=geteuid();
11462 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
11463 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011464 return get_errno(getuid());
aurel3264b4d282008-11-14 17:20:15 +000011465#endif
11466#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11467 /* Alpha specific */
11468 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -080011469 {
11470 uid_t egid;
11471 egid=getegid();
11472 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
11473 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011474 return get_errno(getgid());
aurel3264b4d282008-11-14 17:20:15 +000011475#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -080011476#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11477 /* Alpha specific */
11478 case TARGET_NR_osf_getsysinfo:
11479 ret = -TARGET_EOPNOTSUPP;
11480 switch (arg1) {
11481 case TARGET_GSI_IEEE_FP_CONTROL:
11482 {
Richard Henderson21ba8562019-04-26 15:20:51 -070011483 uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
11484 uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011485
Richard Henderson21ba8562019-04-26 15:20:51 -070011486 swcr &= ~SWCR_STATUS_MASK;
11487 swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011488
11489 if (put_user_u64 (swcr, arg2))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011490 return -TARGET_EFAULT;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011491 ret = 0;
11492 }
11493 break;
11494
11495 /* case GSI_IEEE_STATE_AT_SIGNAL:
11496 -- Not implemented in linux kernel.
11497 case GSI_UACPROC:
11498 -- Retrieves current unaligned access state; not much used.
11499 case GSI_PROC_TYPE:
11500 -- Retrieves implver information; surely not used.
11501 case GSI_GET_HWRPB:
11502 -- Grabs a copy of the HWRPB; surely not used.
11503 */
11504 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011505 return ret;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011506#endif
11507#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11508 /* Alpha specific */
11509 case TARGET_NR_osf_setsysinfo:
11510 ret = -TARGET_EOPNOTSUPP;
11511 switch (arg1) {
11512 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -080011513 {
Richard Henderson21ba8562019-04-26 15:20:51 -070011514 uint64_t swcr, fpcr;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011515
Richard Henderson6e06d512012-06-01 09:08:21 -070011516 if (get_user_u64 (swcr, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011517 return -TARGET_EFAULT;
Richard Henderson6e06d512012-06-01 09:08:21 -070011518 }
Richard Hendersonba0e2762009-12-09 15:56:29 -080011519
Richard Henderson21ba8562019-04-26 15:20:51 -070011520 /*
11521 * The kernel calls swcr_update_status to update the
11522 * status bits from the fpcr at every point that it
11523 * could be queried. Therefore, we store the status
11524 * bits only in FPCR.
11525 */
11526 ((CPUAlphaState *)cpu_env)->swcr
11527 = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -080011528
Richard Henderson21ba8562019-04-26 15:20:51 -070011529 fpcr = cpu_alpha_load_fpcr(cpu_env);
11530 fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
11531 fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
Richard Henderson6e06d512012-06-01 09:08:21 -070011532 cpu_alpha_store_fpcr(cpu_env, fpcr);
11533 ret = 0;
11534 }
11535 break;
11536
11537 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
11538 {
Richard Henderson21ba8562019-04-26 15:20:51 -070011539 uint64_t exc, fpcr, fex;
Richard Henderson6e06d512012-06-01 09:08:21 -070011540
11541 if (get_user_u64(exc, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011542 return -TARGET_EFAULT;
Richard Henderson6e06d512012-06-01 09:08:21 -070011543 }
Richard Henderson21ba8562019-04-26 15:20:51 -070011544 exc &= SWCR_STATUS_MASK;
11545 fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -080011546
Richard Henderson6e06d512012-06-01 09:08:21 -070011547 /* Old exceptions are not signaled. */
Richard Henderson21ba8562019-04-26 15:20:51 -070011548 fex = alpha_ieee_fpcr_to_swcr(fpcr);
11549 fex = exc & ~fex;
11550 fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
11551 fex &= ((CPUArchState *)cpu_env)->swcr;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011552
Richard Henderson21ba8562019-04-26 15:20:51 -070011553 /* Update the hardware fpcr. */
11554 fpcr |= alpha_ieee_swcr_to_fpcr(exc);
11555 cpu_alpha_store_fpcr(cpu_env, fpcr);
11556
11557 if (fex) {
11558 int si_code = TARGET_FPE_FLTUNK;
Richard Henderson6e06d512012-06-01 09:08:21 -070011559 target_siginfo_t info;
Richard Henderson21ba8562019-04-26 15:20:51 -070011560
11561 if (fex & SWCR_TRAP_ENABLE_DNO) {
11562 si_code = TARGET_FPE_FLTUND;
11563 }
11564 if (fex & SWCR_TRAP_ENABLE_INE) {
11565 si_code = TARGET_FPE_FLTRES;
11566 }
11567 if (fex & SWCR_TRAP_ENABLE_UNF) {
11568 si_code = TARGET_FPE_FLTUND;
11569 }
11570 if (fex & SWCR_TRAP_ENABLE_OVF) {
11571 si_code = TARGET_FPE_FLTOVF;
11572 }
11573 if (fex & SWCR_TRAP_ENABLE_DZE) {
11574 si_code = TARGET_FPE_FLTDIV;
11575 }
11576 if (fex & SWCR_TRAP_ENABLE_INV) {
11577 si_code = TARGET_FPE_FLTINV;
11578 }
11579
Richard Henderson6e06d512012-06-01 09:08:21 -070011580 info.si_signo = SIGFPE;
11581 info.si_errno = 0;
11582 info.si_code = si_code;
11583 info._sifields._sigfault._addr
11584 = ((CPUArchState *)cpu_env)->pc;
Peter Maydell9d2803f2016-07-28 16:44:46 +010011585 queue_signal((CPUArchState *)cpu_env, info.si_signo,
11586 QEMU_SI_FAULT, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -080011587 }
Richard Henderson21ba8562019-04-26 15:20:51 -070011588 ret = 0;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011589 }
11590 break;
11591
11592 /* case SSI_NVPAIRS:
11593 -- Used with SSIN_UACPROC to enable unaligned accesses.
11594 case SSI_IEEE_STATE_AT_SIGNAL:
11595 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11596 -- Not implemented in linux kernel
11597 */
11598 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011599 return ret;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011600#endif
11601#ifdef TARGET_NR_osf_sigprocmask
11602 /* Alpha specific. */
11603 case TARGET_NR_osf_sigprocmask:
11604 {
11605 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +010011606 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011607 sigset_t set, oldset;
11608
11609 switch(arg1) {
11610 case TARGET_SIG_BLOCK:
11611 how = SIG_BLOCK;
11612 break;
11613 case TARGET_SIG_UNBLOCK:
11614 how = SIG_UNBLOCK;
11615 break;
11616 case TARGET_SIG_SETMASK:
11617 how = SIG_SETMASK;
11618 break;
11619 default:
Richard Henderson259841c2018-08-18 12:01:09 -070011620 return -TARGET_EINVAL;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011621 }
11622 mask = arg2;
11623 target_to_host_old_sigset(&set, &mask);
Peter Maydell3d3efba2016-05-27 15:51:49 +010011624 ret = do_sigprocmask(how, &set, &oldset);
11625 if (!ret) {
11626 host_to_target_old_sigset(&mask, &oldset);
11627 ret = mask;
11628 }
Richard Hendersonba0e2762009-12-09 15:56:29 -080011629 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011630 return ret;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011631#endif
aurel3264b4d282008-11-14 17:20:15 +000011632
bellarda315a142005-01-30 22:59:18 +000011633#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +000011634 case TARGET_NR_getgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011635 return get_errno(getgid());
bellarda315a142005-01-30 22:59:18 +000011636#endif
11637#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +000011638 case TARGET_NR_geteuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011639 return get_errno(geteuid());
bellarda315a142005-01-30 22:59:18 +000011640#endif
11641#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +000011642 case TARGET_NR_getegid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011643 return get_errno(getegid());
bellarda315a142005-01-30 22:59:18 +000011644#endif
11645#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +000011646 case TARGET_NR_setreuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011647 return get_errno(setreuid(arg1, arg2));
bellarda315a142005-01-30 22:59:18 +000011648#endif
11649#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +000011650 case TARGET_NR_setregid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011651 return get_errno(setregid(arg1, arg2));
bellarda315a142005-01-30 22:59:18 +000011652#endif
11653#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +000011654 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +000011655 {
11656 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +000011657 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +000011658 gid_t *grouplist;
11659 int i;
11660
11661 grouplist = alloca(gidsetsize * sizeof(gid_t));
11662 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +000011663 if (gidsetsize == 0)
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011664 return ret;
bellard99c475a2005-01-31 20:45:13 +000011665 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +000011666 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
11667 if (!target_grouplist) {
Richard Henderson259841c2018-08-18 12:01:09 -070011668 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000011669 }
balroga2155fc2008-09-20 02:12:08 +000011670 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +000011671 target_grouplist[i] = tswap32(grouplist[i]);
11672 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +000011673 }
11674 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011675 return ret;
bellarda315a142005-01-30 22:59:18 +000011676#endif
11677#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +000011678 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +000011679 {
11680 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +000011681 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +000011682 gid_t *grouplist;
11683 int i;
ths3b46e622007-09-17 08:09:54 +000011684
bellard99c475a2005-01-31 20:45:13 +000011685 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +000011686 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
11687 if (!target_grouplist) {
Richard Henderson259841c2018-08-18 12:01:09 -070011688 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000011689 }
bellard99c475a2005-01-31 20:45:13 +000011690 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +000011691 grouplist[i] = tswap32(target_grouplist[i]);
11692 unlock_user(target_grouplist, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011693 return get_errno(setgroups(gidsetsize, grouplist));
bellard99c475a2005-01-31 20:45:13 +000011694 }
bellarda315a142005-01-30 22:59:18 +000011695#endif
11696#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +000011697 case TARGET_NR_fchown32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011698 return get_errno(fchown(arg1, arg2, arg3));
bellarda315a142005-01-30 22:59:18 +000011699#endif
11700#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +000011701 case TARGET_NR_setresuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011702 return get_errno(sys_setresuid(arg1, arg2, arg3));
bellarda315a142005-01-30 22:59:18 +000011703#endif
11704#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +000011705 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +000011706 {
pbrook53a59602006-03-25 19:31:22 +000011707 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +000011708 ret = get_errno(getresuid(&ruid, &euid, &suid));
11709 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +000011710 if (put_user_u32(ruid, arg1)
11711 || put_user_u32(euid, arg2)
11712 || put_user_u32(suid, arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011713 return -TARGET_EFAULT;
bellardb03c60f2003-03-23 17:19:56 +000011714 }
11715 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011716 return ret;
bellarda315a142005-01-30 22:59:18 +000011717#endif
11718#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +000011719 case TARGET_NR_setresgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011720 return get_errno(sys_setresgid(arg1, arg2, arg3));
bellarda315a142005-01-30 22:59:18 +000011721#endif
11722#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +000011723 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +000011724 {
pbrook53a59602006-03-25 19:31:22 +000011725 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +000011726 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11727 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +000011728 if (put_user_u32(rgid, arg1)
11729 || put_user_u32(egid, arg2)
11730 || put_user_u32(sgid, arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011731 return -TARGET_EFAULT;
bellardb03c60f2003-03-23 17:19:56 +000011732 }
11733 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011734 return ret;
bellarda315a142005-01-30 22:59:18 +000011735#endif
11736#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +000011737 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +000011738 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011739 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011740 ret = get_errno(chown(p, arg2, arg3));
11741 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011742 return ret;
bellarda315a142005-01-30 22:59:18 +000011743#endif
11744#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +000011745 case TARGET_NR_setuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011746 return get_errno(sys_setuid(arg1));
bellarda315a142005-01-30 22:59:18 +000011747#endif
11748#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +000011749 case TARGET_NR_setgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011750 return get_errno(sys_setgid(arg1));
bellarda315a142005-01-30 22:59:18 +000011751#endif
11752#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +000011753 case TARGET_NR_setfsuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011754 return get_errno(setfsuid(arg1));
bellarda315a142005-01-30 22:59:18 +000011755#endif
11756#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +000011757 case TARGET_NR_setfsgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011758 return get_errno(setfsgid(arg1));
bellarda315a142005-01-30 22:59:18 +000011759#endif
bellardffa65c32004-01-04 23:57:22 +000011760#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +000011761 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +000011762 {
Richard Henderson259841c2018-08-18 12:01:09 -070011763 void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000011764 if (!a) {
Richard Henderson259841c2018-08-18 12:01:09 -070011765 return -TARGET_ENOMEM;
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000011766 }
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000011767 p = lock_user_string(arg3);
11768 if (!p) {
Richard Henderson259841c2018-08-18 12:01:09 -070011769 ret = -TARGET_EFAULT;
11770 } else {
11771 ret = get_errno(mincore(a, arg2, p));
11772 unlock_user(p, arg3, ret);
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000011773 }
aurel3204bb9ac2008-10-01 21:46:41 +000011774 unlock_user(a, arg1, 0);
11775 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011776 return ret;
bellardffa65c32004-01-04 23:57:22 +000011777#endif
aurel32408321b2008-10-01 21:46:32 +000011778#ifdef TARGET_NR_arm_fadvise64_64
11779 case TARGET_NR_arm_fadvise64_64:
Peter Maydelle0156a92016-05-31 15:45:09 +010011780 /* arm_fadvise64_64 looks like fadvise64_64 but
11781 * with different argument order: fd, advice, offset, len
11782 * rather than the usual fd, offset, len, advice.
11783 * Note that offset and len are both 64-bit so appear as
11784 * pairs of 32-bit registers.
11785 */
11786 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
11787 target_offset64(arg5, arg6), arg2);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011788 return -host_to_target_errno(ret);
aurel32408321b2008-10-01 21:46:32 +000011789#endif
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011790
11791#if TARGET_ABI_BITS == 32
11792
11793#ifdef TARGET_NR_fadvise64_64
11794 case TARGET_NR_fadvise64_64:
Max Filippov64a563d2018-04-01 15:02:34 -070011795#if defined(TARGET_PPC) || defined(TARGET_XTENSA)
Laurent Vivier43046b52017-03-02 01:11:45 +010011796 /* 6 args: fd, advice, offset (high, low), len (high, low) */
11797 ret = arg2;
11798 arg2 = arg3;
11799 arg3 = arg4;
11800 arg4 = arg5;
11801 arg5 = arg6;
11802 arg6 = ret;
11803#else
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011804 /* 6 args: fd, offset (high, low), len (high, low), advice */
James Clarke8bf8e9d2017-09-15 20:33:13 +010011805 if (regpairs_aligned(cpu_env, num)) {
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011806 /* offset is in (3,4), len in (5,6) and advice in 7 */
11807 arg2 = arg3;
11808 arg3 = arg4;
11809 arg4 = arg5;
11810 arg5 = arg6;
11811 arg6 = arg7;
11812 }
Laurent Vivier43046b52017-03-02 01:11:45 +010011813#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011814 ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
11815 target_offset64(arg4, arg5), arg6);
11816 return -host_to_target_errno(ret);
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011817#endif
11818
11819#ifdef TARGET_NR_fadvise64
11820 case TARGET_NR_fadvise64:
11821 /* 5 args: fd, offset (high, low), len, advice */
James Clarke8bf8e9d2017-09-15 20:33:13 +010011822 if (regpairs_aligned(cpu_env, num)) {
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011823 /* offset is in (3,4), len in 5 and advice in 6 */
11824 arg2 = arg3;
11825 arg3 = arg4;
11826 arg4 = arg5;
11827 arg5 = arg6;
11828 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011829 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
11830 return -host_to_target_errno(ret);
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011831#endif
11832
11833#else /* not a 32-bit ABI */
Peter Maydelle0156a92016-05-31 15:45:09 +010011834#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +000011835#ifdef TARGET_NR_fadvise64_64
11836 case TARGET_NR_fadvise64_64:
11837#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +020011838#ifdef TARGET_NR_fadvise64
11839 case TARGET_NR_fadvise64:
11840#endif
11841#ifdef TARGET_S390X
11842 switch (arg4) {
11843 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
11844 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
11845 case 6: arg4 = POSIX_FADV_DONTNEED; break;
11846 case 7: arg4 = POSIX_FADV_NOREUSE; break;
11847 default: break;
11848 }
11849#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011850 return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
aurel32408321b2008-10-01 21:46:32 +000011851#endif
Peter Maydellbadd3cd2016-05-31 15:45:10 +010011852#endif /* end of 64-bit ABI fadvise handling */
11853
bellardffa65c32004-01-04 23:57:22 +000011854#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +000011855 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +000011856 /* A straight passthrough may not be safe because qemu sometimes
Lei Lid2d6b852013-05-20 17:20:50 +080011857 turns private file-backed mappings into anonymous mappings.
pbrook24836682006-04-16 14:14:53 +000011858 This will break MADV_DONTNEED.
11859 This is a hint, so ignoring and returning success is ok. */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011860 return 0;
bellardffa65c32004-01-04 23:57:22 +000011861#endif
Alex Bennéebbf5f2a2020-04-03 20:11:40 +010011862#ifdef TARGET_NR_fcntl64
bellard31e31b82003-02-18 22:55:36 +000011863 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +000011864 {
Alex Bennéebbf5f2a2020-04-03 20:11:40 +010011865 int cmd;
11866 struct flock64 fl;
Peter Maydell213d3e92016-06-13 11:22:05 +010011867 from_flock64_fn *copyfrom = copy_from_user_flock64;
11868 to_flock64_fn *copyto = copy_to_user_flock64;
11869
pbrookce4defa2006-02-09 16:49:55 +000011870#ifdef TARGET_ARM
Laurent Vivier7f254c52018-05-02 23:57:30 +020011871 if (!((CPUARMState *)cpu_env)->eabi) {
11872 copyfrom = copy_from_user_oabi_flock64;
11873 copyto = copy_to_user_oabi_flock64;
Peter Maydell213d3e92016-06-13 11:22:05 +010011874 }
pbrookce4defa2006-02-09 16:49:55 +000011875#endif
bellard77e46722003-04-29 20:39:06 +000011876
Alex Bennéebbf5f2a2020-04-03 20:11:40 +010011877 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +000011878 if (cmd == -TARGET_EINVAL) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011879 return cmd;
Peter Maydell31b63192011-12-05 23:11:50 +000011880 }
thsb1e341e2007-03-20 21:50:52 +000011881
bellard60cd49d2003-03-16 22:53:56 +000011882 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +000011883 case TARGET_F_GETLK64:
Peter Maydell213d3e92016-06-13 11:22:05 +010011884 ret = copyfrom(&fl, arg3);
11885 if (ret) {
11886 break;
ths58134272007-03-31 18:59:32 +000011887 }
Laurent Vivieraf8ab2b2018-07-13 14:58:05 +020011888 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
Peter Maydell213d3e92016-06-13 11:22:05 +010011889 if (ret == 0) {
11890 ret = copyto(arg3, &fl);
11891 }
bellard77e46722003-04-29 20:39:06 +000011892 break;
11893
thsb1e341e2007-03-20 21:50:52 +000011894 case TARGET_F_SETLK64:
11895 case TARGET_F_SETLKW64:
Peter Maydell213d3e92016-06-13 11:22:05 +010011896 ret = copyfrom(&fl, arg3);
11897 if (ret) {
11898 break;
pbrookce4defa2006-02-09 16:49:55 +000011899 }
Peter Maydell435da5e2016-06-13 11:22:05 +010011900 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +000011901 break;
bellard60cd49d2003-03-16 22:53:56 +000011902 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +020011903 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +000011904 break;
11905 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011906 return ret;
bellard77e46722003-04-29 20:39:06 +000011907 }
bellard60cd49d2003-03-16 22:53:56 +000011908#endif
ths7d600c82006-12-08 01:32:58 +000011909#ifdef TARGET_NR_cacheflush
11910 case TARGET_NR_cacheflush:
11911 /* self-modifying code is handled automatically, so nothing needed */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011912 return 0;
ths7d600c82006-12-08 01:32:58 +000011913#endif
bellardc573ff62004-01-04 15:51:36 +000011914#ifdef TARGET_NR_getpagesize
11915 case TARGET_NR_getpagesize:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011916 return TARGET_PAGE_SIZE;
bellardc573ff62004-01-04 15:51:36 +000011917#endif
bellard31e31b82003-02-18 22:55:36 +000011918 case TARGET_NR_gettid:
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +000011919 return get_errno(sys_gettid());
thse5febef2007-04-01 18:31:35 +000011920#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +000011921 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +000011922#if TARGET_ABI_BITS == 32
James Clarke8bf8e9d2017-09-15 20:33:13 +010011923 if (regpairs_aligned(cpu_env, num)) {
aurel322054ac92008-10-13 21:08:07 +000011924 arg2 = arg3;
11925 arg3 = arg4;
11926 arg4 = arg5;
11927 }
Lena Djokic77c68502016-11-24 17:08:56 +010011928 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
aurel322054ac92008-10-13 21:08:07 +000011929#else
11930 ret = get_errno(readahead(arg1, arg2, arg3));
11931#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011932 return ret;
thse5febef2007-04-01 18:31:35 +000011933#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070011934#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +000011935#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +000011936 case TARGET_NR_listxattr:
11937 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +000011938 {
11939 void *p, *b = 0;
11940 if (arg2) {
11941 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11942 if (!b) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011943 return -TARGET_EFAULT;
Peter Maydellfb5590f2011-12-14 15:37:19 +000011944 }
11945 }
11946 p = lock_user_string(arg1);
11947 if (p) {
11948 if (num == TARGET_NR_listxattr) {
11949 ret = get_errno(listxattr(p, b, arg3));
11950 } else {
11951 ret = get_errno(llistxattr(p, b, arg3));
11952 }
11953 } else {
11954 ret = -TARGET_EFAULT;
11955 }
11956 unlock_user(p, arg1, 0);
11957 unlock_user(b, arg2, arg3);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011958 return ret;
Peter Maydellfb5590f2011-12-14 15:37:19 +000011959 }
11960 case TARGET_NR_flistxattr:
11961 {
11962 void *b = 0;
11963 if (arg2) {
11964 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11965 if (!b) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011966 return -TARGET_EFAULT;
Peter Maydellfb5590f2011-12-14 15:37:19 +000011967 }
11968 }
11969 ret = get_errno(flistxattr(arg1, b, arg3));
11970 unlock_user(b, arg2, arg3);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011971 return ret;
Peter Maydellfb5590f2011-12-14 15:37:19 +000011972 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070011973 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +000011974 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070011975 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +000011976 void *p, *n, *v = 0;
11977 if (arg3) {
11978 v = lock_user(VERIFY_READ, arg3, arg4, 1);
11979 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011980 return -TARGET_EFAULT;
Peter Maydelle3c33ec2011-12-14 15:37:17 +000011981 }
11982 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070011983 p = lock_user_string(arg1);
11984 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +000011985 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000011986 if (num == TARGET_NR_setxattr) {
11987 ret = get_errno(setxattr(p, n, v, arg4, arg5));
11988 } else {
11989 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
11990 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070011991 } else {
11992 ret = -TARGET_EFAULT;
11993 }
11994 unlock_user(p, arg1, 0);
11995 unlock_user(n, arg2, 0);
11996 unlock_user(v, arg3, 0);
11997 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011998 return ret;
Peter Maydell30297b52011-12-14 15:37:18 +000011999 case TARGET_NR_fsetxattr:
12000 {
12001 void *n, *v = 0;
12002 if (arg3) {
12003 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12004 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012005 return -TARGET_EFAULT;
Peter Maydell30297b52011-12-14 15:37:18 +000012006 }
12007 }
12008 n = lock_user_string(arg2);
12009 if (n) {
12010 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
12011 } else {
12012 ret = -TARGET_EFAULT;
12013 }
12014 unlock_user(n, arg2, 0);
12015 unlock_user(v, arg3, 0);
12016 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012017 return ret;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012018 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +000012019 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012020 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012021 void *p, *n, *v = 0;
12022 if (arg3) {
12023 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12024 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012025 return -TARGET_EFAULT;
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012026 }
12027 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012028 p = lock_user_string(arg1);
12029 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012030 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000012031 if (num == TARGET_NR_getxattr) {
12032 ret = get_errno(getxattr(p, n, v, arg4));
12033 } else {
12034 ret = get_errno(lgetxattr(p, n, v, arg4));
12035 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012036 } else {
12037 ret = -TARGET_EFAULT;
12038 }
12039 unlock_user(p, arg1, 0);
12040 unlock_user(n, arg2, 0);
12041 unlock_user(v, arg3, arg4);
12042 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012043 return ret;
Peter Maydell30297b52011-12-14 15:37:18 +000012044 case TARGET_NR_fgetxattr:
12045 {
12046 void *n, *v = 0;
12047 if (arg3) {
12048 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12049 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012050 return -TARGET_EFAULT;
Peter Maydell30297b52011-12-14 15:37:18 +000012051 }
12052 }
12053 n = lock_user_string(arg2);
12054 if (n) {
12055 ret = get_errno(fgetxattr(arg1, n, v, arg4));
12056 } else {
12057 ret = -TARGET_EFAULT;
12058 }
12059 unlock_user(n, arg2, 0);
12060 unlock_user(v, arg3, arg4);
12061 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012062 return ret;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012063 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +000012064 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012065 {
12066 void *p, *n;
12067 p = lock_user_string(arg1);
12068 n = lock_user_string(arg2);
12069 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000012070 if (num == TARGET_NR_removexattr) {
12071 ret = get_errno(removexattr(p, n));
12072 } else {
12073 ret = get_errno(lremovexattr(p, n));
12074 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012075 } else {
12076 ret = -TARGET_EFAULT;
12077 }
12078 unlock_user(p, arg1, 0);
12079 unlock_user(n, arg2, 0);
12080 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012081 return ret;
Peter Maydell30297b52011-12-14 15:37:18 +000012082 case TARGET_NR_fremovexattr:
12083 {
12084 void *n;
12085 n = lock_user_string(arg2);
12086 if (n) {
12087 ret = get_errno(fremovexattr(arg1, n));
12088 } else {
12089 ret = -TARGET_EFAULT;
12090 }
12091 unlock_user(n, arg2, 0);
12092 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012093 return ret;
bellardebc05482003-09-30 21:08:41 +000012094#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012095#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +000012096#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +000012097 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +000012098#if defined(TARGET_MIPS)
Petar Jovanovicd2792792014-06-18 17:48:20 +020012099 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012100 return 0;
edgar_iglef967792009-01-07 14:19:38 +000012101#elif defined(TARGET_CRIS)
12102 if (arg1 & 0xff)
12103 ret = -TARGET_EINVAL;
12104 else {
12105 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
12106 ret = 0;
12107 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012108 return ret;
bellard8d18e892007-11-14 15:18:40 +000012109#elif defined(TARGET_I386) && defined(TARGET_ABI32)
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012110 return do_set_thread_area(cpu_env, arg1);
Peter Maydell1ccd9372013-07-16 18:44:55 +010012111#elif defined(TARGET_M68K)
12112 {
Andreas Färber0429a972013-08-26 18:14:44 +020012113 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +010012114 ts->tp_value = arg1;
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012115 return 0;
Peter Maydell1ccd9372013-07-16 18:44:55 +010012116 }
ths6f5b89a2007-03-02 20:48:00 +000012117#else
Richard Henderson10f45d92018-08-18 12:01:07 -070012118 return -TARGET_ENOSYS;
ths6f5b89a2007-03-02 20:48:00 +000012119#endif
12120#endif
12121#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +000012122 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +000012123#if defined(TARGET_I386) && defined(TARGET_ABI32)
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012124 return do_get_thread_area(cpu_env, arg1);
Peter Maydell1ccd9372013-07-16 18:44:55 +010012125#elif defined(TARGET_M68K)
12126 {
Andreas Färber0429a972013-08-26 18:14:44 +020012127 TaskState *ts = cpu->opaque;
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012128 return ts->tp_value;
Peter Maydell1ccd9372013-07-16 18:44:55 +010012129 }
bellard8d18e892007-11-14 15:18:40 +000012130#else
Richard Henderson10f45d92018-08-18 12:01:07 -070012131 return -TARGET_ENOSYS;
bellardebc05482003-09-30 21:08:41 +000012132#endif
bellard8d18e892007-11-14 15:18:40 +000012133#endif
bellard48dc41e2006-06-21 18:15:50 +000012134#ifdef TARGET_NR_getdomainname
12135 case TARGET_NR_getdomainname:
Richard Henderson10f45d92018-08-18 12:01:07 -070012136 return -TARGET_ENOSYS;
bellard48dc41e2006-06-21 18:15:50 +000012137#endif
ths6f5b89a2007-03-02 20:48:00 +000012138
Max Filippov12e33402018-04-01 13:13:49 -070012139#ifdef TARGET_NR_clock_settime
12140 case TARGET_NR_clock_settime:
12141 {
12142 struct timespec ts;
12143
12144 ret = target_to_host_timespec(&ts, arg2);
12145 if (!is_error(ret)) {
12146 ret = get_errno(clock_settime(arg1, &ts));
12147 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012148 return ret;
Max Filippov12e33402018-04-01 13:13:49 -070012149 }
12150#endif
Alistair Francisc6c8d102020-03-12 15:13:53 -070012151#ifdef TARGET_NR_clock_settime64
12152 case TARGET_NR_clock_settime64:
12153 {
12154 struct timespec ts;
12155
12156 ret = target_to_host_timespec64(&ts, arg2);
12157 if (!is_error(ret)) {
12158 ret = get_errno(clock_settime(arg1, &ts));
12159 }
12160 return ret;
12161 }
12162#endif
thsb5906f92007-03-19 13:32:45 +000012163#ifdef TARGET_NR_clock_gettime
12164 case TARGET_NR_clock_gettime:
12165 {
12166 struct timespec ts;
12167 ret = get_errno(clock_gettime(arg1, &ts));
12168 if (!is_error(ret)) {
Max Filippovb9f99082018-04-01 13:14:04 -070012169 ret = host_to_target_timespec(arg2, &ts);
thsb5906f92007-03-19 13:32:45 +000012170 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012171 return ret;
thsb5906f92007-03-19 13:32:45 +000012172 }
12173#endif
Alistair Francisc6c8d102020-03-12 15:13:53 -070012174#ifdef TARGET_NR_clock_gettime64
12175 case TARGET_NR_clock_gettime64:
12176 {
12177 struct timespec ts;
12178 ret = get_errno(clock_gettime(arg1, &ts));
12179 if (!is_error(ret)) {
12180 ret = host_to_target_timespec64(arg2, &ts);
12181 }
12182 return ret;
12183 }
12184#endif
thsb5906f92007-03-19 13:32:45 +000012185#ifdef TARGET_NR_clock_getres
12186 case TARGET_NR_clock_getres:
12187 {
12188 struct timespec ts;
12189 ret = get_errno(clock_getres(arg1, &ts));
12190 if (!is_error(ret)) {
12191 host_to_target_timespec(arg2, &ts);
12192 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012193 return ret;
thsb5906f92007-03-19 13:32:45 +000012194 }
12195#endif
Filip Bozuta828cb3a2020-07-22 17:34:21 +020012196#ifdef TARGET_NR_clock_getres_time64
12197 case TARGET_NR_clock_getres_time64:
12198 {
12199 struct timespec ts;
12200 ret = get_errno(clock_getres(arg1, &ts));
12201 if (!is_error(ret)) {
12202 host_to_target_timespec64(arg2, &ts);
12203 }
12204 return ret;
12205 }
12206#endif
pbrook63d76512008-05-29 13:43:29 +000012207#ifdef TARGET_NR_clock_nanosleep
12208 case TARGET_NR_clock_nanosleep:
12209 {
12210 struct timespec ts;
Filip Bozutab09d6402020-07-27 22:13:26 +020012211 if (target_to_host_timespec(&ts, arg3)) {
12212 return -TARGET_EFAULT;
12213 }
Peter Maydell9e518222016-06-06 19:58:09 +010012214 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12215 &ts, arg4 ? &ts : NULL));
Laurent Vivier8ec68a02020-07-24 07:45:05 +010012216 /*
12217 * if the call is interrupted by a signal handler, it fails
12218 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12219 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12220 */
Filip Bozutab09d6402020-07-27 22:13:26 +020012221 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12222 host_to_target_timespec(arg4, &ts)) {
12223 return -TARGET_EFAULT;
Laurent Vivier8ec68a02020-07-24 07:45:05 +010012224 }
Tom Musta8fbe8fd2014-08-12 13:53:41 -050012225
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012226 return ret;
pbrook63d76512008-05-29 13:43:29 +000012227 }
12228#endif
Filip Bozuta6ac03b22020-08-24 21:21:15 +020012229#ifdef TARGET_NR_clock_nanosleep_time64
12230 case TARGET_NR_clock_nanosleep_time64:
12231 {
12232 struct timespec ts;
12233
12234 if (target_to_host_timespec64(&ts, arg3)) {
12235 return -TARGET_EFAULT;
12236 }
12237
12238 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12239 &ts, arg4 ? &ts : NULL));
12240
12241 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12242 host_to_target_timespec64(arg4, &ts)) {
12243 return -TARGET_EFAULT;
12244 }
12245 return ret;
12246 }
12247#endif
thsb5906f92007-03-19 13:32:45 +000012248
ths6f5b89a2007-03-02 20:48:00 +000012249#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
12250 case TARGET_NR_set_tid_address:
Richard Henderson3e8f1622021-02-12 10:48:43 -080012251 return get_errno(set_tid_address((int *)g2h(cpu, arg1)));
ths6f5b89a2007-03-02 20:48:00 +000012252#endif
12253
ths4cae1d12007-07-12 11:06:53 +000012254 case TARGET_NR_tkill:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012255 return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +000012256
ths71455572007-06-21 21:45:30 +000012257 case TARGET_NR_tgkill:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012258 return get_errno(safe_tgkill((int)arg1, (int)arg2,
12259 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +000012260
ths4f2b1fe2007-06-21 21:57:12 +000012261#ifdef TARGET_NR_set_robust_list
12262 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +000012263 case TARGET_NR_get_robust_list:
12264 /* The ABI for supporting robust futexes has userspace pass
12265 * the kernel a pointer to a linked list which is updated by
12266 * userspace after the syscall; the list is walked by the kernel
12267 * when the thread exits. Since the linked list in QEMU guest
12268 * memory isn't a valid linked list for the host and we have
12269 * no way to reliably intercept the thread-death event, we can't
12270 * support these. Silently return ENOSYS so that guest userspace
12271 * falls back to a non-robust futex implementation (which should
12272 * be OK except in the corner case of the guest crashing while
12273 * holding a mutex that is shared with another process via
12274 * shared memory).
12275 */
Richard Henderson10f45d92018-08-18 12:01:07 -070012276 return -TARGET_ENOSYS;
ths4f2b1fe2007-06-21 21:57:12 +000012277#endif
12278
Peter Maydell1acae9f2013-07-02 14:04:12 +010012279#if defined(TARGET_NR_utimensat)
ths9007f0e2007-09-25 17:50:37 +000012280 case TARGET_NR_utimensat:
12281 {
Riku Voipioebc996f2009-04-21 15:01:51 +030012282 struct timespec *tsp, ts[2];
12283 if (!arg3) {
12284 tsp = NULL;
12285 } else {
Filip Bozutab3a3af72020-08-11 13:31:01 +020012286 if (target_to_host_timespec(ts, arg3)) {
12287 return -TARGET_EFAULT;
12288 }
12289 if (target_to_host_timespec(ts + 1, arg3 +
12290 sizeof(struct target_timespec))) {
12291 return -TARGET_EFAULT;
12292 }
Riku Voipioebc996f2009-04-21 15:01:51 +030012293 tsp = ts;
12294 }
ths9007f0e2007-09-25 17:50:37 +000012295 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +030012296 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +000012297 else {
bellard579a97f2007-11-11 14:26:47 +000012298 if (!(p = lock_user_string(arg2))) {
Richard Henderson259841c2018-08-18 12:01:09 -070012299 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000012300 }
Riku Voipioebc996f2009-04-21 15:01:51 +030012301 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +000012302 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +000012303 }
12304 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012305 return ret;
ths9007f0e2007-09-25 17:50:37 +000012306#endif
Filip Bozutacac46eb2020-08-25 00:30:50 +020012307#ifdef TARGET_NR_utimensat_time64
12308 case TARGET_NR_utimensat_time64:
12309 {
12310 struct timespec *tsp, ts[2];
12311 if (!arg3) {
12312 tsp = NULL;
12313 } else {
12314 if (target_to_host_timespec64(ts, arg3)) {
12315 return -TARGET_EFAULT;
12316 }
12317 if (target_to_host_timespec64(ts + 1, arg3 +
12318 sizeof(struct target__kernel_timespec))) {
12319 return -TARGET_EFAULT;
12320 }
12321 tsp = ts;
12322 }
12323 if (!arg2)
12324 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
12325 else {
12326 p = lock_user_string(arg2);
12327 if (!p) {
12328 return -TARGET_EFAULT;
12329 }
12330 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
12331 unlock_user(p, arg2, 0);
12332 }
12333 }
12334 return ret;
12335#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070012336#ifdef TARGET_NR_futex
pbrookbd0c5662008-05-29 14:34:11 +000012337 case TARGET_NR_futex:
Richard Henderson3e8f1622021-02-12 10:48:43 -080012338 return do_futex(cpu, arg1, arg2, arg3, arg4, arg5, arg6);
Alistair Francis859e8a82020-03-12 15:13:49 -070012339#endif
Alistair Francis14690292020-03-18 15:47:01 -070012340#ifdef TARGET_NR_futex_time64
12341 case TARGET_NR_futex_time64:
Richard Henderson3e8f1622021-02-12 10:48:43 -080012342 return do_futex_time64(cpu, arg1, arg2, arg3, arg4, arg5, arg6);
Alistair Francis14690292020-03-18 15:47:01 -070012343#endif
aurel32dbfe4c32009-04-08 21:29:30 +000012344#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +000012345 case TARGET_NR_inotify_init:
12346 ret = get_errno(sys_inotify_init());
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012347 if (ret >= 0) {
12348 fd_trans_register(ret, &target_inotify_trans);
12349 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012350 return ret;
aurel3239b59762008-10-01 21:46:50 +000012351#endif
Stefan Weila1606b02010-03-28 11:44:41 +020012352#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +000012353#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
12354 case TARGET_NR_inotify_init1:
Lena Djokicfea243e2016-11-24 17:08:53 +010012355 ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1,
12356 fcntl_flags_tbl)));
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012357 if (ret >= 0) {
12358 fd_trans_register(ret, &target_inotify_trans);
12359 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012360 return ret;
Riku Voipioc05c7a72010-03-26 15:25:11 +000012361#endif
Stefan Weila1606b02010-03-28 11:44:41 +020012362#endif
aurel32dbfe4c32009-04-08 21:29:30 +000012363#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +000012364 case TARGET_NR_inotify_add_watch:
12365 p = lock_user_string(arg2);
12366 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
12367 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012368 return ret;
aurel3239b59762008-10-01 21:46:50 +000012369#endif
aurel32dbfe4c32009-04-08 21:29:30 +000012370#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +000012371 case TARGET_NR_inotify_rm_watch:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012372 return get_errno(sys_inotify_rm_watch(arg1, arg2));
aurel3239b59762008-10-01 21:46:50 +000012373#endif
ths9007f0e2007-09-25 17:50:37 +000012374
Nathan Froyd8ec9cf82009-07-22 09:14:36 -070012375#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +000012376 case TARGET_NR_mq_open:
12377 {
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012378 struct mq_attr posix_mq_attr;
Lena Djokic26400772016-11-24 17:08:58 +010012379 struct mq_attr *pposix_mq_attr;
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012380 int host_flags;
aurel3224e10032009-04-15 16:11:43 +000012381
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012382 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
Lena Djokic26400772016-11-24 17:08:58 +010012383 pposix_mq_attr = NULL;
12384 if (arg4) {
12385 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012386 return -TARGET_EFAULT;
Lena Djokic26400772016-11-24 17:08:58 +010012387 }
12388 pposix_mq_attr = &posix_mq_attr;
Tom Mustab6ce1f62014-08-12 13:53:36 -050012389 }
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012390 p = lock_user_string(arg1 - 1);
12391 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012392 return -TARGET_EFAULT;
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012393 }
Lena Djokic26400772016-11-24 17:08:58 +010012394 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
aurel3224e10032009-04-15 16:11:43 +000012395 unlock_user (p, arg1, 0);
12396 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012397 return ret;
aurel3224e10032009-04-15 16:11:43 +000012398
12399 case TARGET_NR_mq_unlink:
12400 p = lock_user_string(arg1 - 1);
Peter Maydell32112152016-07-12 13:02:13 +010012401 if (!p) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012402 return -TARGET_EFAULT;
Peter Maydell32112152016-07-12 13:02:13 +010012403 }
aurel3224e10032009-04-15 16:11:43 +000012404 ret = get_errno(mq_unlink(p));
12405 unlock_user (p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012406 return ret;
aurel3224e10032009-04-15 16:11:43 +000012407
Alistair Francis859e8a82020-03-12 15:13:49 -070012408#ifdef TARGET_NR_mq_timedsend
aurel3224e10032009-04-15 16:11:43 +000012409 case TARGET_NR_mq_timedsend:
12410 {
12411 struct timespec ts;
12412
12413 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12414 if (arg5 != 0) {
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012415 if (target_to_host_timespec(&ts, arg5)) {
12416 return -TARGET_EFAULT;
12417 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012418 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012419 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12420 return -TARGET_EFAULT;
12421 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012422 } else {
12423 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
aurel3224e10032009-04-15 16:11:43 +000012424 }
aurel3224e10032009-04-15 16:11:43 +000012425 unlock_user (p, arg2, arg3);
12426 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012427 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070012428#endif
Filip Bozutad107e372020-08-24 21:37:52 +020012429#ifdef TARGET_NR_mq_timedsend_time64
12430 case TARGET_NR_mq_timedsend_time64:
12431 {
12432 struct timespec ts;
12433
12434 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12435 if (arg5 != 0) {
12436 if (target_to_host_timespec64(&ts, arg5)) {
12437 return -TARGET_EFAULT;
12438 }
12439 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
12440 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12441 return -TARGET_EFAULT;
12442 }
12443 } else {
12444 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
12445 }
12446 unlock_user(p, arg2, arg3);
12447 }
12448 return ret;
12449#endif
aurel3224e10032009-04-15 16:11:43 +000012450
Alistair Francis859e8a82020-03-12 15:13:49 -070012451#ifdef TARGET_NR_mq_timedreceive
aurel3224e10032009-04-15 16:11:43 +000012452 case TARGET_NR_mq_timedreceive:
12453 {
12454 struct timespec ts;
12455 unsigned int prio;
12456
12457 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12458 if (arg5 != 0) {
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012459 if (target_to_host_timespec(&ts, arg5)) {
12460 return -TARGET_EFAULT;
12461 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012462 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12463 &prio, &ts));
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012464 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12465 return -TARGET_EFAULT;
12466 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012467 } else {
12468 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12469 &prio, NULL));
aurel3224e10032009-04-15 16:11:43 +000012470 }
aurel3224e10032009-04-15 16:11:43 +000012471 unlock_user (p, arg2, arg3);
12472 if (arg4 != 0)
12473 put_user_u32(prio, arg4);
12474 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012475 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070012476#endif
Filip Bozutad107e372020-08-24 21:37:52 +020012477#ifdef TARGET_NR_mq_timedreceive_time64
12478 case TARGET_NR_mq_timedreceive_time64:
12479 {
12480 struct timespec ts;
12481 unsigned int prio;
12482
12483 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12484 if (arg5 != 0) {
12485 if (target_to_host_timespec64(&ts, arg5)) {
12486 return -TARGET_EFAULT;
12487 }
12488 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12489 &prio, &ts));
12490 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12491 return -TARGET_EFAULT;
12492 }
12493 } else {
12494 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12495 &prio, NULL));
12496 }
12497 unlock_user(p, arg2, arg3);
12498 if (arg4 != 0) {
12499 put_user_u32(prio, arg4);
12500 }
12501 }
12502 return ret;
12503#endif
aurel3224e10032009-04-15 16:11:43 +000012504
12505 /* Not implemented for now... */
12506/* case TARGET_NR_mq_notify: */
12507/* break; */
12508
12509 case TARGET_NR_mq_getsetattr:
12510 {
12511 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
12512 ret = 0;
aurel3224e10032009-04-15 16:11:43 +000012513 if (arg2 != 0) {
12514 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
Max Filippova23ea402018-03-31 08:20:15 -070012515 ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
12516 &posix_mq_attr_out));
12517 } else if (arg3 != 0) {
12518 ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
aurel3224e10032009-04-15 16:11:43 +000012519 }
Max Filippova23ea402018-03-31 08:20:15 -070012520 if (ret == 0 && arg3 != 0) {
12521 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
12522 }
aurel3224e10032009-04-15 16:11:43 +000012523 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012524 return ret;
aurel3224e10032009-04-15 16:11:43 +000012525#endif
12526
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012527#ifdef CONFIG_SPLICE
12528#ifdef TARGET_NR_tee
12529 case TARGET_NR_tee:
12530 {
12531 ret = get_errno(tee(arg1,arg2,arg3,arg4));
12532 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012533 return ret;
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012534#endif
12535#ifdef TARGET_NR_splice
12536 case TARGET_NR_splice:
12537 {
12538 loff_t loff_in, loff_out;
12539 loff_t *ploff_in = NULL, *ploff_out = NULL;
Andreas Schwab17644b32015-03-10 17:11:35 +010012540 if (arg2) {
12541 if (get_user_u64(loff_in, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012542 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012543 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012544 ploff_in = &loff_in;
12545 }
Andreas Schwab17644b32015-03-10 17:11:35 +010012546 if (arg4) {
12547 if (get_user_u64(loff_out, arg4)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012548 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012549 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012550 ploff_out = &loff_out;
12551 }
12552 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
Andreas Schwab17644b32015-03-10 17:11:35 +010012553 if (arg2) {
12554 if (put_user_u64(loff_in, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012555 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012556 }
12557 }
12558 if (arg4) {
12559 if (put_user_u64(loff_out, arg4)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012560 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012561 }
12562 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012563 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012564 return ret;
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012565#endif
12566#ifdef TARGET_NR_vmsplice
12567 case TARGET_NR_vmsplice:
12568 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070012569 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
12570 if (vec != NULL) {
12571 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
12572 unlock_iovec(vec, arg2, arg3, 0);
12573 } else {
12574 ret = -host_to_target_errno(errno);
12575 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012576 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012577 return ret;
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012578#endif
12579#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +030012580#ifdef CONFIG_EVENTFD
12581#if defined(TARGET_NR_eventfd)
12582 case TARGET_NR_eventfd:
12583 ret = get_errno(eventfd(arg1, 0));
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012584 if (ret >= 0) {
12585 fd_trans_register(ret, &target_eventfd_trans);
12586 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012587 return ret;
Riku Voipioc2882b92009-08-12 15:08:24 +030012588#endif
12589#if defined(TARGET_NR_eventfd2)
12590 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +020012591 {
Helge Deller78721302021-02-10 07:12:14 +010012592 int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
Petar Jovanovic5947c692013-04-08 20:26:10 +020012593 if (arg2 & TARGET_O_NONBLOCK) {
12594 host_flags |= O_NONBLOCK;
12595 }
12596 if (arg2 & TARGET_O_CLOEXEC) {
12597 host_flags |= O_CLOEXEC;
12598 }
12599 ret = get_errno(eventfd(arg1, host_flags));
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012600 if (ret >= 0) {
12601 fd_trans_register(ret, &target_eventfd_trans);
12602 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012603 return ret;
Petar Jovanovic5947c692013-04-08 20:26:10 +020012604 }
Riku Voipioc2882b92009-08-12 15:08:24 +030012605#endif
12606#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +030012607#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
12608 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +010012609#if TARGET_ABI_BITS == 32
12610 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
12611 target_offset64(arg5, arg6)));
12612#else
Ulrich Hechtd0927932009-09-17 20:22:14 +030012613 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +010012614#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012615 return ret;
Ulrich Hechtd0927932009-09-17 20:22:14 +030012616#endif
Peter Maydellc727f472011-01-06 11:05:10 +000012617#if defined(CONFIG_SYNC_FILE_RANGE)
12618#if defined(TARGET_NR_sync_file_range)
12619 case TARGET_NR_sync_file_range:
12620#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +030012621#if defined(TARGET_MIPS)
12622 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
12623 target_offset64(arg5, arg6), arg7));
12624#else
Peter Maydellc727f472011-01-06 11:05:10 +000012625 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
12626 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +030012627#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +000012628#else
12629 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
12630#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012631 return ret;
Peter Maydellc727f472011-01-06 11:05:10 +000012632#endif
Laurent Vivier5bcb4982020-03-10 11:33:50 +010012633#if defined(TARGET_NR_sync_file_range2) || \
12634 defined(TARGET_NR_arm_sync_file_range)
Peter Maydellc727f472011-01-06 11:05:10 +000012635#if defined(TARGET_NR_sync_file_range2)
12636 case TARGET_NR_sync_file_range2:
Laurent Vivier5bcb4982020-03-10 11:33:50 +010012637#endif
12638#if defined(TARGET_NR_arm_sync_file_range)
12639 case TARGET_NR_arm_sync_file_range:
12640#endif
Peter Maydellc727f472011-01-06 11:05:10 +000012641 /* This is like sync_file_range but the arguments are reordered */
12642#if TARGET_ABI_BITS == 32
12643 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
12644 target_offset64(arg5, arg6), arg2));
12645#else
12646 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
12647#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012648 return ret;
Peter Maydellc727f472011-01-06 11:05:10 +000012649#endif
12650#endif
Laurent Viviere36800c2015-10-02 14:48:09 +020012651#if defined(TARGET_NR_signalfd4)
12652 case TARGET_NR_signalfd4:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012653 return do_signalfd4(arg1, arg2, arg4);
Laurent Viviere36800c2015-10-02 14:48:09 +020012654#endif
12655#if defined(TARGET_NR_signalfd)
12656 case TARGET_NR_signalfd:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012657 return do_signalfd4(arg1, arg2, 0);
Laurent Viviere36800c2015-10-02 14:48:09 +020012658#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000012659#if defined(CONFIG_EPOLL)
12660#if defined(TARGET_NR_epoll_create)
12661 case TARGET_NR_epoll_create:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012662 return get_errno(epoll_create(arg1));
Peter Maydell3b6edd12011-02-15 18:35:05 +000012663#endif
12664#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
12665 case TARGET_NR_epoll_create1:
Sergei Trofimovich386d3862020-04-15 23:05:08 +010012666 return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
Peter Maydell3b6edd12011-02-15 18:35:05 +000012667#endif
12668#if defined(TARGET_NR_epoll_ctl)
12669 case TARGET_NR_epoll_ctl:
12670 {
12671 struct epoll_event ep;
12672 struct epoll_event *epp = 0;
12673 if (arg4) {
LemonBoyc7811022020-04-17 17:34:54 +020012674 if (arg2 != EPOLL_CTL_DEL) {
12675 struct target_epoll_event *target_ep;
12676 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
12677 return -TARGET_EFAULT;
12678 }
12679 ep.events = tswap32(target_ep->events);
12680 /*
12681 * The epoll_data_t union is just opaque data to the kernel,
12682 * so we transfer all 64 bits across and need not worry what
12683 * actual data type it is.
12684 */
12685 ep.data.u64 = tswap64(target_ep->data.u64);
12686 unlock_user_struct(target_ep, arg4, 0);
Peter Maydell3b6edd12011-02-15 18:35:05 +000012687 }
LemonBoyc7811022020-04-17 17:34:54 +020012688 /*
12689 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
12690 * non-null pointer, even though this argument is ignored.
12691 *
Peter Maydell3b6edd12011-02-15 18:35:05 +000012692 */
Peter Maydell3b6edd12011-02-15 18:35:05 +000012693 epp = &ep;
12694 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012695 return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
Peter Maydell3b6edd12011-02-15 18:35:05 +000012696 }
12697#endif
12698
Peter Maydell227f0212016-06-06 19:58:11 +010012699#if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
Peter Maydell3b6edd12011-02-15 18:35:05 +000012700#if defined(TARGET_NR_epoll_wait)
12701 case TARGET_NR_epoll_wait:
12702#endif
Peter Maydell227f0212016-06-06 19:58:11 +010012703#if defined(TARGET_NR_epoll_pwait)
Peter Maydell3b6edd12011-02-15 18:35:05 +000012704 case TARGET_NR_epoll_pwait:
12705#endif
12706 {
12707 struct target_epoll_event *target_ep;
12708 struct epoll_event *ep;
12709 int epfd = arg1;
12710 int maxevents = arg3;
12711 int timeout = arg4;
12712
Peter Maydell2ba7fae32016-07-18 15:35:59 +010012713 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012714 return -TARGET_EINVAL;
Peter Maydell2ba7fae32016-07-18 15:35:59 +010012715 }
12716
Peter Maydell3b6edd12011-02-15 18:35:05 +000012717 target_ep = lock_user(VERIFY_WRITE, arg2,
12718 maxevents * sizeof(struct target_epoll_event), 1);
12719 if (!target_ep) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012720 return -TARGET_EFAULT;
Peter Maydell3b6edd12011-02-15 18:35:05 +000012721 }
12722
Peter Maydell04c95f42016-07-18 15:36:00 +010012723 ep = g_try_new(struct epoll_event, maxevents);
12724 if (!ep) {
12725 unlock_user(target_ep, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012726 return -TARGET_ENOMEM;
Peter Maydell04c95f42016-07-18 15:36:00 +010012727 }
Peter Maydell3b6edd12011-02-15 18:35:05 +000012728
12729 switch (num) {
Peter Maydell227f0212016-06-06 19:58:11 +010012730#if defined(TARGET_NR_epoll_pwait)
Peter Maydell3b6edd12011-02-15 18:35:05 +000012731 case TARGET_NR_epoll_pwait:
12732 {
12733 target_sigset_t *target_set;
12734 sigset_t _set, *set = &_set;
12735
12736 if (arg5) {
Peter Maydellc8157012016-06-30 14:23:24 +010012737 if (arg6 != sizeof(target_sigset_t)) {
12738 ret = -TARGET_EINVAL;
12739 break;
12740 }
12741
Peter Maydell3b6edd12011-02-15 18:35:05 +000012742 target_set = lock_user(VERIFY_READ, arg5,
12743 sizeof(target_sigset_t), 1);
12744 if (!target_set) {
Peter Maydell04c95f42016-07-18 15:36:00 +010012745 ret = -TARGET_EFAULT;
12746 break;
Peter Maydell3b6edd12011-02-15 18:35:05 +000012747 }
12748 target_to_host_sigset(set, target_set);
12749 unlock_user(target_set, arg5, 0);
12750 } else {
12751 set = NULL;
12752 }
12753
Peter Maydell227f0212016-06-06 19:58:11 +010012754 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
12755 set, SIGSET_T_SIZE));
Peter Maydell3b6edd12011-02-15 18:35:05 +000012756 break;
12757 }
12758#endif
12759#if defined(TARGET_NR_epoll_wait)
12760 case TARGET_NR_epoll_wait:
Peter Maydell227f0212016-06-06 19:58:11 +010012761 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
12762 NULL, 0));
Peter Maydell3b6edd12011-02-15 18:35:05 +000012763 break;
12764#endif
12765 default:
12766 ret = -TARGET_ENOSYS;
12767 }
12768 if (!is_error(ret)) {
12769 int i;
12770 for (i = 0; i < ret; i++) {
12771 target_ep[i].events = tswap32(ep[i].events);
12772 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
12773 }
Peter Maydell04c95f42016-07-18 15:36:00 +010012774 unlock_user(target_ep, arg2,
12775 ret * sizeof(struct target_epoll_event));
12776 } else {
12777 unlock_user(target_ep, arg2, 0);
Peter Maydell3b6edd12011-02-15 18:35:05 +000012778 }
Peter Maydell04c95f42016-07-18 15:36:00 +010012779 g_free(ep);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012780 return ret;
Peter Maydell3b6edd12011-02-15 18:35:05 +000012781 }
12782#endif
12783#endif
Peter Maydell163a05a2011-06-27 17:44:52 +010012784#ifdef TARGET_NR_prlimit64
12785 case TARGET_NR_prlimit64:
12786 {
12787 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
12788 struct target_rlimit64 *target_rnew, *target_rold;
12789 struct host_rlimit64 rnew, rold, *rnewp = 0;
Felix Janda95018012014-12-02 22:11:17 +010012790 int resource = target_to_host_resource(arg2);
Tobias Koch055d92f2020-03-05 21:24:00 +010012791
12792 if (arg3 && (resource != RLIMIT_AS &&
12793 resource != RLIMIT_DATA &&
12794 resource != RLIMIT_STACK)) {
Peter Maydell163a05a2011-06-27 17:44:52 +010012795 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012796 return -TARGET_EFAULT;
Peter Maydell163a05a2011-06-27 17:44:52 +010012797 }
12798 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
12799 rnew.rlim_max = tswap64(target_rnew->rlim_max);
12800 unlock_user_struct(target_rnew, arg3, 0);
12801 rnewp = &rnew;
12802 }
12803
Felix Janda95018012014-12-02 22:11:17 +010012804 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
Peter Maydell163a05a2011-06-27 17:44:52 +010012805 if (!is_error(ret) && arg4) {
12806 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012807 return -TARGET_EFAULT;
Peter Maydell163a05a2011-06-27 17:44:52 +010012808 }
12809 target_rold->rlim_cur = tswap64(rold.rlim_cur);
12810 target_rold->rlim_max = tswap64(rold.rlim_max);
12811 unlock_user_struct(target_rold, arg4, 1);
12812 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012813 return ret;
Peter Maydell163a05a2011-06-27 17:44:52 +010012814 }
12815#endif
Richard Henderson3d21d292012-09-15 13:20:46 -070012816#ifdef TARGET_NR_gethostname
12817 case TARGET_NR_gethostname:
12818 {
12819 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
12820 if (name) {
12821 ret = get_errno(gethostname(name, arg2));
12822 unlock_user(name, arg1, arg2);
12823 } else {
12824 ret = -TARGET_EFAULT;
12825 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012826 return ret;
Richard Henderson3d21d292012-09-15 13:20:46 -070012827 }
12828#endif
Riku Voipio89aaf1a2013-07-24 09:44:26 +030012829#ifdef TARGET_NR_atomic_cmpxchg_32
12830 case TARGET_NR_atomic_cmpxchg_32:
12831 {
12832 /* should use start_exclusive from main.c */
12833 abi_ulong mem_value;
12834 if (get_user_u32(mem_value, arg6)) {
12835 target_siginfo_t info;
12836 info.si_signo = SIGSEGV;
12837 info.si_errno = 0;
12838 info.si_code = TARGET_SEGV_MAPERR;
12839 info._sifields._sigfault._addr = arg6;
Peter Maydell9d2803f2016-07-28 16:44:46 +010012840 queue_signal((CPUArchState *)cpu_env, info.si_signo,
12841 QEMU_SI_FAULT, &info);
Riku Voipio89aaf1a2013-07-24 09:44:26 +030012842 ret = 0xdeadbeef;
12843
12844 }
12845 if (mem_value == arg2)
12846 put_user_u32(arg1, arg6);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012847 return mem_value;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030012848 }
12849#endif
12850#ifdef TARGET_NR_atomic_barrier
12851 case TARGET_NR_atomic_barrier:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012852 /* Like the kernel implementation and the
12853 qemu arm barrier, no-op this? */
12854 return 0;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030012855#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012856
12857#ifdef TARGET_NR_timer_create
12858 case TARGET_NR_timer_create:
12859 {
12860 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
12861
12862 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012863
12864 int clkid = arg1;
12865 int timer_index = next_free_host_timer();
12866
12867 if (timer_index < 0) {
12868 ret = -TARGET_EAGAIN;
12869 } else {
12870 timer_t *phtimer = g_posix_timers + timer_index;
12871
12872 if (arg2) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012873 phost_sevp = &host_sevp;
Peter Maydellc0659762014-08-09 15:42:32 +010012874 ret = target_to_host_sigevent(phost_sevp, arg2);
12875 if (ret != 0) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012876 return ret;
Peter Maydellc0659762014-08-09 15:42:32 +010012877 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012878 }
12879
12880 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
12881 if (ret) {
12882 phtimer = NULL;
12883 } else {
Alexander Grafaecc8862014-11-10 21:33:03 +010012884 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012885 return -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012886 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012887 }
12888 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012889 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012890 }
12891#endif
12892
12893#ifdef TARGET_NR_timer_settime
12894 case TARGET_NR_timer_settime:
12895 {
12896 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
12897 * struct itimerspec * old_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010012898 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020012899
Alexander Grafaecc8862014-11-10 21:33:03 +010012900 if (timerid < 0) {
12901 ret = timerid;
12902 } else if (arg3 == 0) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012903 ret = -TARGET_EINVAL;
12904 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020012905 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012906 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
12907
Marc-André Lureau40c80b52017-01-19 10:15:32 -050012908 if (target_to_host_itimerspec(&hspec_new, arg3)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012909 return -TARGET_EFAULT;
Marc-André Lureau40c80b52017-01-19 10:15:32 -050012910 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012911 ret = get_errno(
12912 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
Marc-André Lureau40c80b52017-01-19 10:15:32 -050012913 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012914 return -TARGET_EFAULT;
Marc-André Lureau40c80b52017-01-19 10:15:32 -050012915 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012916 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012917 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012918 }
12919#endif
12920
Filip Bozuta828cb3a2020-07-22 17:34:21 +020012921#ifdef TARGET_NR_timer_settime64
12922 case TARGET_NR_timer_settime64:
12923 {
12924 target_timer_t timerid = get_timer_id(arg1);
12925
12926 if (timerid < 0) {
12927 ret = timerid;
12928 } else if (arg3 == 0) {
12929 ret = -TARGET_EINVAL;
12930 } else {
12931 timer_t htimer = g_posix_timers[timerid];
12932 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
12933
12934 if (target_to_host_itimerspec64(&hspec_new, arg3)) {
12935 return -TARGET_EFAULT;
12936 }
12937 ret = get_errno(
12938 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
12939 if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
12940 return -TARGET_EFAULT;
12941 }
12942 }
12943 return ret;
12944 }
12945#endif
12946
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012947#ifdef TARGET_NR_timer_gettime
12948 case TARGET_NR_timer_gettime:
12949 {
12950 /* args: timer_t timerid, struct itimerspec *curr_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010012951 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020012952
Alexander Grafaecc8862014-11-10 21:33:03 +010012953 if (timerid < 0) {
12954 ret = timerid;
12955 } else if (!arg2) {
12956 ret = -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012957 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020012958 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012959 struct itimerspec hspec;
12960 ret = get_errno(timer_gettime(htimer, &hspec));
12961
12962 if (host_to_target_itimerspec(arg2, &hspec)) {
12963 ret = -TARGET_EFAULT;
12964 }
12965 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012966 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012967 }
12968#endif
12969
Filip Bozuta828cb3a2020-07-22 17:34:21 +020012970#ifdef TARGET_NR_timer_gettime64
12971 case TARGET_NR_timer_gettime64:
12972 {
12973 /* args: timer_t timerid, struct itimerspec64 *curr_value */
12974 target_timer_t timerid = get_timer_id(arg1);
12975
12976 if (timerid < 0) {
12977 ret = timerid;
12978 } else if (!arg2) {
12979 ret = -TARGET_EFAULT;
12980 } else {
12981 timer_t htimer = g_posix_timers[timerid];
12982 struct itimerspec hspec;
12983 ret = get_errno(timer_gettime(htimer, &hspec));
12984
12985 if (host_to_target_itimerspec64(arg2, &hspec)) {
12986 ret = -TARGET_EFAULT;
12987 }
12988 }
12989 return ret;
12990 }
12991#endif
12992
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110012993#ifdef TARGET_NR_timer_getoverrun
12994 case TARGET_NR_timer_getoverrun:
12995 {
12996 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010012997 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020012998
Alexander Grafaecc8862014-11-10 21:33:03 +010012999 if (timerid < 0) {
13000 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013001 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020013002 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013003 ret = get_errno(timer_getoverrun(htimer));
13004 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013005 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013006 }
13007#endif
13008
13009#ifdef TARGET_NR_timer_delete
13010 case TARGET_NR_timer_delete:
13011 {
13012 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010013013 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020013014
Alexander Grafaecc8862014-11-10 21:33:03 +010013015 if (timerid < 0) {
13016 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013017 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020013018 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013019 ret = get_errno(timer_delete(htimer));
Alexander Grafe52a99f2014-08-22 13:56:18 +020013020 g_posix_timers[timerid] = 0;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013021 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013022 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013023 }
13024#endif
13025
Riku Voipio51834342014-06-22 11:25:42 +010013026#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13027 case TARGET_NR_timerfd_create:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013028 return get_errno(timerfd_create(arg1,
13029 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
Riku Voipio51834342014-06-22 11:25:42 +010013030#endif
13031
13032#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13033 case TARGET_NR_timerfd_gettime:
13034 {
13035 struct itimerspec its_curr;
13036
13037 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13038
13039 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013040 return -TARGET_EFAULT;
Riku Voipio51834342014-06-22 11:25:42 +010013041 }
13042 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013043 return ret;
Riku Voipio51834342014-06-22 11:25:42 +010013044#endif
13045
Filip Bozuta828cb3a2020-07-22 17:34:21 +020013046#if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13047 case TARGET_NR_timerfd_gettime64:
13048 {
13049 struct itimerspec its_curr;
13050
13051 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13052
13053 if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
13054 return -TARGET_EFAULT;
13055 }
13056 }
13057 return ret;
13058#endif
13059
Riku Voipio51834342014-06-22 11:25:42 +010013060#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13061 case TARGET_NR_timerfd_settime:
13062 {
13063 struct itimerspec its_new, its_old, *p_new;
13064
13065 if (arg3) {
13066 if (target_to_host_itimerspec(&its_new, arg3)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013067 return -TARGET_EFAULT;
Riku Voipio51834342014-06-22 11:25:42 +010013068 }
13069 p_new = &its_new;
13070 } else {
13071 p_new = NULL;
13072 }
13073
13074 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13075
13076 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013077 return -TARGET_EFAULT;
Riku Voipio51834342014-06-22 11:25:42 +010013078 }
13079 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013080 return ret;
Riku Voipio51834342014-06-22 11:25:42 +010013081#endif
13082
Filip Bozuta828cb3a2020-07-22 17:34:21 +020013083#if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13084 case TARGET_NR_timerfd_settime64:
13085 {
13086 struct itimerspec its_new, its_old, *p_new;
13087
13088 if (arg3) {
13089 if (target_to_host_itimerspec64(&its_new, arg3)) {
13090 return -TARGET_EFAULT;
13091 }
13092 p_new = &its_new;
13093 } else {
13094 p_new = NULL;
13095 }
13096
13097 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13098
13099 if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
13100 return -TARGET_EFAULT;
13101 }
13102 }
13103 return ret;
13104#endif
13105
Paul Burtonab31cda2014-06-22 11:25:43 +010013106#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13107 case TARGET_NR_ioprio_get:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013108 return get_errno(ioprio_get(arg1, arg2));
Paul Burtonab31cda2014-06-22 11:25:43 +010013109#endif
13110
13111#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13112 case TARGET_NR_ioprio_set:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013113 return get_errno(ioprio_set(arg1, arg2, arg3));
Paul Burtonab31cda2014-06-22 11:25:43 +010013114#endif
13115
Riku Voipio9af5c902014-08-12 15:58:57 +030013116#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13117 case TARGET_NR_setns:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013118 return get_errno(setns(arg1, arg2));
Riku Voipio9af5c902014-08-12 15:58:57 +030013119#endif
13120#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13121 case TARGET_NR_unshare:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013122 return get_errno(unshare(arg1));
Riku Voipio9af5c902014-08-12 15:58:57 +030013123#endif
Laurent Vivier2f147882016-09-25 22:20:20 +020013124#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13125 case TARGET_NR_kcmp:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013126 return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
Laurent Vivier2f147882016-09-25 22:20:20 +020013127#endif
Richard Hendersonfa97e382018-07-18 13:06:48 -070013128#ifdef TARGET_NR_swapcontext
13129 case TARGET_NR_swapcontext:
13130 /* PowerPC specific. */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013131 return do_swapcontext(cpu_env, arg1, arg2, arg3);
Richard Hendersonfa97e382018-07-18 13:06:48 -070013132#endif
Shu-Chun Weng9bdfa4d2019-08-19 11:09:47 -070013133#ifdef TARGET_NR_memfd_create
13134 case TARGET_NR_memfd_create:
13135 p = lock_user_string(arg1);
13136 if (!p) {
13137 return -TARGET_EFAULT;
13138 }
13139 ret = get_errno(memfd_create(p, arg2));
13140 fd_trans_unregister(ret);
13141 unlock_user(p, arg1, 0);
13142 return ret;
13143#endif
Andreas Schwab85004762019-05-13 11:02:53 +020013144#if defined TARGET_NR_membarrier && defined __NR_membarrier
13145 case TARGET_NR_membarrier:
13146 return get_errno(membarrier(arg1, arg2));
13147#endif
Riku Voipio9af5c902014-08-12 15:58:57 +030013148
Andreas Schwab84946452020-11-12 12:45:16 +010013149#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13150 case TARGET_NR_copy_file_range:
13151 {
13152 loff_t inoff, outoff;
13153 loff_t *pinoff = NULL, *poutoff = NULL;
13154
13155 if (arg2) {
13156 if (get_user_u64(inoff, arg2)) {
13157 return -TARGET_EFAULT;
13158 }
13159 pinoff = &inoff;
13160 }
13161 if (arg4) {
13162 if (get_user_u64(outoff, arg4)) {
13163 return -TARGET_EFAULT;
13164 }
13165 poutoff = &outoff;
13166 }
Giuseppe Musacchio0fa259d2021-05-03 19:41:59 +020013167 /* Do not sign-extend the count parameter. */
Andreas Schwab84946452020-11-12 12:45:16 +010013168 ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
Giuseppe Musacchio0fa259d2021-05-03 19:41:59 +020013169 (abi_ulong)arg5, arg6));
Andreas Schwab84946452020-11-12 12:45:16 +010013170 if (!is_error(ret) && ret > 0) {
13171 if (arg2) {
13172 if (put_user_u64(inoff, arg2)) {
13173 return -TARGET_EFAULT;
13174 }
13175 }
13176 if (arg4) {
13177 if (put_user_u64(outoff, arg4)) {
13178 return -TARGET_EFAULT;
13179 }
13180 }
13181 }
13182 }
13183 return ret;
13184#endif
13185
YAMAMOTO Takashie10fbe82021-05-31 14:50:12 +090013186#if defined(TARGET_NR_pivot_root)
13187 case TARGET_NR_pivot_root:
13188 {
13189 void *p2;
13190 p = lock_user_string(arg1); /* new_root */
13191 p2 = lock_user_string(arg2); /* put_old */
13192 if (!p || !p2) {
13193 ret = -TARGET_EFAULT;
13194 } else {
13195 ret = get_errno(pivot_root(p, p2));
13196 }
13197 unlock_user(p2, arg2, 0);
13198 unlock_user(p, arg1, 0);
13199 }
13200 return ret;
13201#endif
13202
bellard31e31b82003-02-18 22:55:36 +000013203 default:
Philippe Mathieu-Daudé122f9c82018-07-06 12:51:25 -030013204 qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013205 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +000013206 }
bellard31e31b82003-02-18 22:55:36 +000013207 return ret;
13208}
Richard Hendersondc1ce182018-08-18 12:01:04 -070013209
13210abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
13211 abi_long arg2, abi_long arg3, abi_long arg4,
13212 abi_long arg5, abi_long arg6, abi_long arg7,
13213 abi_long arg8)
13214{
Richard Henderson29a0af62019-03-22 16:07:18 -070013215 CPUState *cpu = env_cpu(cpu_env);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013216 abi_long ret;
13217
13218#ifdef DEBUG_ERESTARTSYS
13219 /* Debug-only code for exercising the syscall-restart code paths
13220 * in the per-architecture cpu main loops: restart every syscall
13221 * the guest makes once before letting it through.
13222 */
13223 {
13224 static bool flag;
13225 flag = !flag;
13226 if (flag) {
Richard Hendersonaf254a22021-11-22 19:47:33 +010013227 return -QEMU_ERESTARTSYS;
Richard Hendersondc1ce182018-08-18 12:01:04 -070013228 }
13229 }
13230#endif
13231
Emilio G. Cotac36f7a62018-10-21 13:27:44 -040013232 record_syscall_start(cpu, num, arg1,
13233 arg2, arg3, arg4, arg5, arg6, arg7, arg8);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013234
Josh Kunz4b25a502020-02-03 18:54:14 -080013235 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
Filip Bozutae400e112020-08-11 18:45:49 +020013236 print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
Josh Kunz4b25a502020-02-03 18:54:14 -080013237 }
13238
13239 ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
13240 arg5, arg6, arg7, arg8);
13241
13242 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
Filip Bozutae400e112020-08-11 18:45:49 +020013243 print_syscall_ret(cpu_env, num, ret, arg1, arg2,
13244 arg3, arg4, arg5, arg6);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013245 }
13246
Emilio G. Cotac36f7a62018-10-21 13:27:44 -040013247 record_syscall_return(cpu, num, ret);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013248 return ret;
13249}