blob: 16ca5ea7b6ce68fb04a10b0f48fd3aa485b33207 [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"
Richard Henderson720ace22023-03-15 17:43:14 +000025#include "qemu/plugin.h"
Richard Hendersond7ec12f2023-09-29 19:54:54 -070026#include "tcg/startup.h"
Helge Dellerfe080592022-12-15 08:27:46 +010027#include "target_mman.h"
bellard31e31b82003-02-18 22:55:36 +000028#include <elf.h>
29#include <endian.h>
John Spencerc56dc772012-12-10 07:59:46 +010030#include <grp.h>
thsd08d3bb2007-03-19 13:09:22 +000031#include <sys/ipc.h>
32#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000033#include <sys/wait.h>
bellard31e31b82003-02-18 22:55:36 +000034#include <sys/mount.h>
John Spencer586b0be2012-12-26 00:49:49 +010035#include <sys/file.h>
36#include <sys/fsuid.h>
37#include <sys/personality.h>
ths39b9aae2007-02-11 18:36:44 +000038#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000039#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000040#include <sys/swap.h>
Peter Maydelle0eb2102014-03-17 12:15:35 +000041#include <linux/capability.h>
bellard31e31b82003-02-18 22:55:36 +000042#include <sched.h>
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020043#include <sys/timex.h>
bellard31e31b82003-02-18 22:55:36 +000044#include <sys/socket.h>
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +020045#include <linux/sockios.h>
aurel32607175e2009-04-15 16:11:59 +000046#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000047#include <sys/uio.h>
Felix Janda0839f112016-09-30 19:40:21 -040048#include <poll.h>
bellard32f36bc2003-03-30 21:29:48 +000049#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000050#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000051#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000052#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000053#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000054#include <sys/sysinfo.h>
Laurent Viviere36800c2015-10-02 14:48:09 +020055#include <sys/signalfd.h>
bellard72f03902003-02-18 23:33:18 +000056//#include <sys/user.h>
Shu-Chun Weng22db1212020-12-18 11:32:12 -080057#include <netinet/in.h>
bellard8853f862004-02-22 14:57:26 +000058#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000059#include <netinet/tcp.h>
Shu-Chun Wengfe51b0a2020-12-18 11:32:11 -080060#include <netinet/udp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020061#include <linux/wireless.h>
Jing Huang920394d2012-07-24 13:59:23 +000062#include <linux/icmp.h>
Helge Delleree1ac3a2017-02-18 23:31:30 +010063#include <linux/icmpv6.h>
Shu-Chun Weng6addf062020-09-28 18:48:01 -070064#include <linux/if_tun.h>
Shu-Chun Weng22db1212020-12-18 11:32:12 -080065#include <linux/in6.h>
Helge Delleree1ac3a2017-02-18 23:31:30 +010066#include <linux/errqueue.h>
Marco A L Barbosad6d6d6f2017-10-05 10:55:30 -030067#include <linux/random.h>
Riku Voipiod80a1902014-10-01 16:05:46 +030068#ifdef CONFIG_TIMERFD
69#include <sys/timerfd.h>
70#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030071#ifdef CONFIG_EVENTFD
72#include <sys/eventfd.h>
73#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000074#ifdef CONFIG_EPOLL
75#include <sys/epoll.h>
76#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070077#ifdef CONFIG_ATTR
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010078#include "qemu/xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070079#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000080#ifdef CONFIG_SENDFILE
81#include <sys/sendfile.h>
82#endif
Thomas Huth4a9d5f82020-11-18 18:10:51 +010083#ifdef HAVE_SYS_KCOV_H
Aleksandar Markovicbd27e672020-01-16 23:49:50 +010084#include <sys/kcov.h>
85#endif
bellard31e31b82003-02-18 22:55:36 +000086
87#define termios host_termios
88#define winsize host_winsize
89#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000090#define sgttyb host_sgttyb /* same as target */
91#define tchars host_tchars /* same as target */
92#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000093
94#include <linux/termios.h>
95#include <linux/unistd.h>
bellard31e31b82003-02-18 22:55:36 +000096#include <linux/cdrom.h>
97#include <linux/hdreg.h>
98#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000099#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +0000100#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +0300101#include <linux/fs.h>
Yunqiang Suab22b4d2019-09-04 14:59:24 +0200102#include <linux/fd.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000103#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +0000104#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000105#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200106#include <linux/fb.h>
Cortland Tölva6c753a62018-10-08 09:35:20 -0700107#if defined(CONFIG_USBFS)
108#include <linux/usbdevice_fs.h>
Cortland Tölvaa1333672018-10-08 09:35:21 -0700109#include <linux/usb/ch9.h>
Cortland Tölva6c753a62018-10-08 09:35:20 -0700110#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200111#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +0100112#include <linux/dm-ioctl.h>
Laurent Vivierc07ecc62013-01-07 11:40:06 +0000113#include <linux/reboot.h>
Laurent Vivier7ff7b662013-07-02 14:04:12 +0100114#include <linux/route.h>
Laurent Vivierf57d4192013-08-30 01:46:41 +0200115#include <linux/filter.h>
Andreas Färberfff8c532014-01-18 07:38:30 +0100116#include <linux/blkpg.h>
Laurent Viviera82ea932016-06-27 00:18:22 +0200117#include <netpacket/packet.h>
Laurent Vivier6c5b5642016-05-22 18:56:19 +0200118#include <linux/netlink.h>
Yunqiang Suf31dddd2019-06-19 16:17:11 +0200119#include <linux/if_alg.h>
Filip Bozuta68365f92020-01-15 20:36:35 +0100120#include <linux/rtc.h>
Filip Bozuta1c4c6fc2020-01-15 20:36:41 +0100121#include <sound/asound.h>
Thomas Huth48f670e2020-11-18 18:10:52 +0100122#ifdef HAVE_BTRFS_H
Filip Bozutad6092e02020-08-23 21:50:07 +0200123#include <linux/btrfs.h>
124#endif
Chen Gange865b972020-06-05 09:32:21 +0800125#ifdef HAVE_DRM_H
126#include <libdrm/drm.h>
Chen Gang913b03c2020-08-02 21:39:38 +0800127#include <libdrm/i915_drm.h>
Chen Gange865b972020-06-05 09:32:21 +0800128#endif
pbrookd7e40362008-05-23 16:06:43 +0000129#include "linux_loop.h"
Riku Voipio18cb0082014-02-19 12:59:58 +0200130#include "uname.h"
bellard31e31b82003-02-18 22:55:36 +0000131
bellard3ef693a2003-03-23 20:17:16 +0000132#include "qemu.h"
Peter Maydell3b249d22021-09-08 16:44:03 +0100133#include "user-internals.h"
Peter Maydella44d57a2021-09-08 16:43:58 +0100134#include "strace.h"
Peter Maydell2113aed2021-09-08 16:43:59 +0100135#include "signal-common.h"
Peter Maydell3ad0a762021-09-08 16:44:00 +0100136#include "loader.h"
Peter Maydell5423e6d2021-09-08 16:44:01 +0100137#include "user-mmap.h"
Richard Hendersonbbf15aa2021-11-17 16:14:00 +0100138#include "user/safe-syscall.h"
Richard Henderson5ebdd772019-03-14 13:10:53 -0700139#include "qemu/guest-random.h"
Alex Bennée01ef6b92020-04-03 20:11:46 +0100140#include "qemu/selfmap.h"
Emilio G. Cotac36f7a62018-10-21 13:27:44 -0400141#include "user/syscall-trace.h"
Richard Henderson5da40632021-11-17 15:46:05 +0100142#include "special-errno.h"
Richard Henderson51977e22019-03-12 19:22:20 -0700143#include "qapi/error.h"
Laurent Vivierf7e6a402018-08-24 00:22:15 +0200144#include "fd-trans.h"
Helge Dellerbd5ccd62022-10-24 22:18:09 +0200145#include "cpu_loop-common.h"
bellard31e31b82003-02-18 22:55:36 +0000146
Peter Maydell5ea2fc82016-08-02 18:41:27 +0100147#ifndef CLONE_IO
148#define CLONE_IO 0x80000000 /* Clone io context */
149#endif
150
151/* We can't directly call the host clone syscall, because this will
152 * badly confuse libc (breaking mutexes, for example). So we must
153 * divide clone flags into:
154 * * flag combinations that look like pthread_create()
155 * * flag combinations that look like fork()
156 * * flags we can implement within QEMU itself
157 * * flags we can't support and will return an error for
158 */
159/* For thread creation, all these flags must be present; for
160 * fork, none must be present.
161 */
162#define CLONE_THREAD_FLAGS \
163 (CLONE_VM | CLONE_FS | CLONE_FILES | \
164 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
165
166/* These flags are ignored:
167 * CLONE_DETACHED is now ignored by the kernel;
168 * CLONE_IO is just an optimisation hint to the I/O scheduler
169 */
170#define CLONE_IGNORED_FLAGS \
171 (CLONE_DETACHED | CLONE_IO)
172
Helge Deller895ce8b2022-11-29 12:08:20 +0100173#ifndef CLONE_PIDFD
174# define CLONE_PIDFD 0x00001000
175#endif
176
Peter Maydell5ea2fc82016-08-02 18:41:27 +0100177/* Flags for fork which we can implement within QEMU itself */
178#define CLONE_OPTIONAL_FORK_FLAGS \
Helge Deller895ce8b2022-11-29 12:08:20 +0100179 (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
Peter Maydell5ea2fc82016-08-02 18:41:27 +0100180 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
181
182/* Flags for thread creation which we can implement within QEMU itself */
183#define CLONE_OPTIONAL_THREAD_FLAGS \
184 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
185 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
186
187#define CLONE_INVALID_FORK_FLAGS \
188 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
189
190#define CLONE_INVALID_THREAD_FLAGS \
191 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
192 CLONE_IGNORED_FLAGS))
193
194/* CLONE_VFORK is special cased early in do_fork(). The other flag bits
195 * have almost all been allocated. We cannot support any of
196 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
197 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
198 * The checks against the invalid thread masks above will catch these.
199 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
200 */
pbrook30813ce2008-06-02 15:45:44 +0000201
Timothy E Baldwin71a8f7f2016-05-12 18:47:45 +0100202/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
203 * once. This exercises the codepaths for restart.
204 */
205//#define DEBUG_ERESTARTSYS
bellard31e31b82003-02-18 22:55:36 +0000206
bellard1a9353d2003-03-16 20:28:50 +0000207//#include <linux/msdos_fs.h>
Richard Henderson540a7362021-11-14 11:35:37 +0100208#define VFAT_IOCTL_READDIR_BOTH \
209 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
210#define VFAT_IOCTL_READDIR_SHORT \
211 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
bellard1a9353d2003-03-16 20:28:50 +0000212
bellard70a194b2003-08-11 22:20:16 +0000213#undef _syscall0
214#undef _syscall1
215#undef _syscall2
216#undef _syscall3
217#undef _syscall4
218#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000219#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000220
bellard83fcb512006-06-14 13:37:16 +0000221#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000222static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000223{ \
224 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000225}
226
bellard83fcb512006-06-14 13:37:16 +0000227#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000228static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000229{ \
230 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000231}
232
bellard83fcb512006-06-14 13:37:16 +0000233#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000234static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000235{ \
236 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000237}
238
bellard83fcb512006-06-14 13:37:16 +0000239#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000240static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000241{ \
242 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000243}
244
bellard83fcb512006-06-14 13:37:16 +0000245#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000246static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000247{ \
248 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000249}
250
bellard83fcb512006-06-14 13:37:16 +0000251#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
252 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000253static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000254{ \
255 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000256}
bellard83fcb512006-06-14 13:37:16 +0000257
258
259#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
260 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000261static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
262 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000263{ \
264 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
265}
266
bellard70a194b2003-08-11 22:20:16 +0000267
bellard31e31b82003-02-18 22:55:36 +0000268#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000269#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000270#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000271#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000272#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000273#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +0200274#define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000275#define __NR_sys_syslog __NR_syslog
Alistair Francis14690292020-03-18 15:47:01 -0700276#if defined(__NR_futex)
277# define __NR_sys_futex __NR_futex
278#endif
279#if defined(__NR_futex_time64)
280# define __NR_sys_futex_time64 __NR_futex_time64
281#endif
Aleksandar Rikaloefa92182019-06-28 12:43:34 +0200282#define __NR_sys_statx __NR_statx
bellard31e31b82003-02-18 22:55:36 +0000283
Peter Maydellb1cef6d2018-01-25 16:19:49 +0000284#if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000285#define __NR__llseek __NR_lseek
286#endif
287
James Hogana29e5ba2014-03-25 21:51:08 +0000288/* Newer kernel ports have llseek() instead of _llseek() */
289#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
290#define TARGET_NR__llseek TARGET_NR_llseek
291#endif
292
Helge Deller78721302021-02-10 07:12:14 +0100293/* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
294#ifndef TARGET_O_NONBLOCK_MASK
295#define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
296#endif
297
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +0000298#define __NR_sys_gettid __NR_gettid
299_syscall0(int, sys_gettid)
Peter Maydell2b3f64c2018-04-19 13:57:40 +0100300
301/* For the 64-bit guest on 32-bit host case we must emulate
302 * getdents using getdents64, because otherwise the host
303 * might hand us back more dirent records than we can fit
304 * into the guest buffer after structure format conversion.
305 * Otherwise we emulate getdents with getdents if the host has it.
306 */
307#if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
308#define EMULATE_GETDENTS_WITH_GETDENTS
309#endif
310
311#if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
Juan Quintelaac42f442023-07-09 18:34:30 +0100312_syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100313#endif
Peter Maydell2b3f64c2018-04-19 13:57:40 +0100314#if (defined(TARGET_NR_getdents) && \
315 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
Peter Maydell3307e232013-06-12 16:20:21 +0100316 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
Juan Quintelaac42f442023-07-09 18:34:30 +0100317_syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count);
aurel323b3f24a2009-04-15 16:12:13 +0000318#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700319#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
Juan Quintelaac42f442023-07-09 18:34:30 +0100320_syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo,
321 loff_t *, res, unsigned int, wh);
aurel323b3f24a2009-04-15 16:12:13 +0000322#endif
Miloš Stojanovićc1a402a2017-05-15 16:59:45 +0200323_syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +0200324_syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
325 siginfo_t *, uinfo)
aurel323b3f24a2009-04-15 16:12:13 +0000326_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
aurel323b3f24a2009-04-15 16:12:13 +0000327#ifdef __NR_exit_group
328_syscall1(int,exit_group,int,error_code)
329#endif
Helge Delleraf804f32022-10-25 04:34:14 +0200330#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
331#define __NR_sys_close_range __NR_close_range
332_syscall3(int,sys_close_range,int,first,int,last,int,flags)
333#ifndef CLOSE_RANGE_CLOEXEC
334#define CLOSE_RANGE_CLOEXEC (1U << 2)
335#endif
336#endif
Alistair Francis14690292020-03-18 15:47:01 -0700337#if defined(__NR_futex)
aurel323b3f24a2009-04-15 16:12:13 +0000338_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
339 const struct timespec *,timeout,int *,uaddr2,int,val3)
340#endif
Alistair Francis14690292020-03-18 15:47:01 -0700341#if defined(__NR_futex_time64)
342_syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
343 const struct timespec *,timeout,int *,uaddr2,int,val3)
344#endif
Helge Dellercc054c62022-09-18 21:45:46 +0200345#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
346_syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
347#endif
348#if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
349_syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
350 unsigned int, flags);
351#endif
352#if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
353_syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
354#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500355#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
356_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
357 unsigned long *, user_mask_ptr);
358#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
359_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
360 unsigned long *, user_mask_ptr);
Tonis Tiigi45ad7612022-01-04 20:18:18 -0800361/* sched_attr is not defined in glibc */
362struct sched_attr {
363 uint32_t size;
364 uint32_t sched_policy;
365 uint64_t sched_flags;
366 int32_t sched_nice;
367 uint32_t sched_priority;
368 uint64_t sched_runtime;
369 uint64_t sched_deadline;
370 uint64_t sched_period;
371 uint32_t sched_util_min;
372 uint32_t sched_util_max;
373};
374#define __NR_sys_sched_getattr __NR_sched_getattr
375_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
376 unsigned int, size, unsigned int, flags);
377#define __NR_sys_sched_setattr __NR_sched_setattr
378_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
379 unsigned int, flags);
Tonis Tiigi407a1192022-01-04 20:18:19 -0800380#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
381_syscall1(int, sys_sched_getscheduler, pid_t, pid);
382#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
383_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
384 const struct sched_param *, param);
385#define __NR_sys_sched_getparam __NR_sched_getparam
386_syscall2(int, sys_sched_getparam, pid_t, pid,
387 struct sched_param *, param);
388#define __NR_sys_sched_setparam __NR_sched_setparam
389_syscall2(int, sys_sched_setparam, pid_t, pid,
390 const struct sched_param *, param);
Samuel Thibaultb827c3e2018-01-12 09:14:35 +0100391#define __NR_sys_getcpu __NR_getcpu
392_syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200393_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
394 void *, arg);
Peter Maydelle0eb2102014-03-17 12:15:35 +0000395_syscall2(int, capget, struct __user_cap_header_struct *, header,
396 struct __user_cap_data_struct *, data);
397_syscall2(int, capset, struct __user_cap_header_struct *, header,
398 struct __user_cap_data_struct *, data);
Paul Burtonab31cda2014-06-22 11:25:43 +0100399#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
400_syscall2(int, ioprio_get, int, which, int, who)
401#endif
402#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
403_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
404#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +0100405#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
406_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
407#endif
aurel323b3f24a2009-04-15 16:12:13 +0000408
Laurent Vivier2f147882016-09-25 22:20:20 +0200409#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
410_syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
411 unsigned long, idx1, unsigned long, idx2)
412#endif
413
Aleksandar Rikaloefa92182019-06-28 12:43:34 +0200414/*
415 * It is assumed that struct statx is architecture independent.
416 */
417#if defined(TARGET_NR_statx) && defined(__NR_statx)
418_syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
419 unsigned int, mask, struct target_statx *, statxbuf)
420#endif
Andreas Schwab85004762019-05-13 11:02:53 +0200421#if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
422_syscall2(int, membarrier, int, cmd, int, flags)
423#endif
Aleksandar Rikaloefa92182019-06-28 12:43:34 +0200424
Philippe Mathieu-Daudé180d4ef2021-05-17 07:52:43 +0200425static const bitmask_transtbl fcntl_flags_tbl[] = {
aurel323b3f24a2009-04-15 16:12:13 +0000426 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
427 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
428 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
429 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
430 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
431 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
432 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
433 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700434 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000435 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
436 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
437 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
438 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000439#if defined(O_DIRECT)
440 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
441#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700442#if defined(O_NOATIME)
443 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
444#endif
445#if defined(O_CLOEXEC)
446 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
447#endif
448#if defined(O_PATH)
449 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
450#endif
Riku Voipio5f9cee42017-08-08 16:01:19 +0300451#if defined(O_TMPFILE)
452 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
453#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700454 /* Don't terminate the list prematurely on 64-bit host+guest. */
455#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
456 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
457#endif
aurel323b3f24a2009-04-15 16:12:13 +0000458};
459
Andreas Schwab0f6bb192020-07-23 12:27:13 +0200460_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
aurel323b3f24a2009-04-15 16:12:13 +0000461
Filip Bozutacac46eb2020-08-25 00:30:50 +0200462#if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
Peter Maydell700fa582016-07-18 11:47:55 +0100463#if defined(__NR_utimensat)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100464#define __NR_sys_utimensat __NR_utimensat
ths9007f0e2007-09-25 17:50:37 +0000465_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
466 const struct timespec *,tsp,int,flags)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100467#else
468static int sys_utimensat(int dirfd, const char *pathname,
469 const struct timespec times[2], int flags)
470{
471 errno = ENOSYS;
472 return -1;
473}
ths9007f0e2007-09-25 17:50:37 +0000474#endif
Peter Maydell1acae9f2013-07-02 14:04:12 +0100475#endif /* TARGET_NR_utimensat */
aurel323b3f24a2009-04-15 16:12:13 +0000476
Andreas Schwab95d03072018-01-23 11:53:31 +0100477#ifdef TARGET_NR_renameat2
478#if defined(__NR_renameat2)
479#define __NR_sys_renameat2 __NR_renameat2
480_syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
481 const char *, new, unsigned int, flags)
482#else
483static int sys_renameat2(int oldfd, const char *old,
484 int newfd, const char *new, int flags)
485{
486 if (flags == 0) {
487 return renameat(oldfd, old, newfd, new);
488 }
489 errno = ENOSYS;
490 return -1;
491}
492#endif
493#endif /* TARGET_NR_renameat2 */
494
aurel323b3f24a2009-04-15 16:12:13 +0000495#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000496#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000497#else
498/* Userspace can usually survive runtime without inotify */
499#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000500#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000501#undef TARGET_NR_inotify_add_watch
502#undef TARGET_NR_inotify_rm_watch
503#endif /* CONFIG_INOTIFY */
504
Peter Maydell163a05a2011-06-27 17:44:52 +0100505#if defined(TARGET_NR_prlimit64)
506#ifndef __NR_prlimit64
507# define __NR_prlimit64 -1
508#endif
509#define __NR_sys_prlimit64 __NR_prlimit64
510/* The glibc rlimit structure may not be that used by the underlying syscall */
511struct host_rlimit64 {
512 uint64_t rlim_cur;
513 uint64_t rlim_max;
514};
515_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
516 const struct host_rlimit64 *, new_limit,
517 struct host_rlimit64 *, old_limit)
518#endif
519
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100520
521#if defined(TARGET_NR_timer_create)
zhaolichang6f9ff552020-09-17 15:50:25 +0800522/* Maximum of 32 active POSIX timers allowed at any one time. */
Peter Maydell9e598992022-07-25 12:00:35 +0100523#define GUEST_TIMER_MAX 32
524static timer_t g_posix_timers[GUEST_TIMER_MAX];
525static int g_posix_timer_allocated[GUEST_TIMER_MAX];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100526
527static inline int next_free_host_timer(void)
528{
Peter Maydell9e598992022-07-25 12:00:35 +0100529 int k;
530 for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
531 if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100532 return k;
533 }
534 }
535 return -1;
536}
Peter Maydell9e598992022-07-25 12:00:35 +0100537
538static inline void free_host_timer_slot(int id)
539{
540 qatomic_store_release(g_posix_timer_allocated + id, 0);
541}
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100542#endif
543
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200544static inline int host_to_target_errno(int host_errno)
ths637947f2007-06-01 12:09:19 +0000545{
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200546 switch (host_errno) {
547#define E(X) case X: return TARGET_##X;
548#include "errnos.c.inc"
549#undef E
550 default:
551 return host_errno;
Timothy E Baldwin24661192016-05-12 18:47:25 +0100552 }
ths637947f2007-06-01 12:09:19 +0000553}
554
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200555static inline int target_to_host_errno(int target_errno)
thsb92c47c2007-11-01 00:07:38 +0000556{
Philippe Mathieu-Daudé3ffe3262021-07-08 19:05:49 +0200557 switch (target_errno) {
558#define E(X) case TARGET_##X: return X;
559#include "errnos.c.inc"
560#undef E
561 default:
562 return target_errno;
Timothy E Baldwin24661192016-05-12 18:47:25 +0100563 }
thsb92c47c2007-11-01 00:07:38 +0000564}
565
Ilya Leoshkevich892a4f62022-06-21 16:42:05 +0200566abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000567{
568 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000569 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000570 else
571 return ret;
572}
573
Peter Maydell7dcdaea2016-06-06 19:58:18 +0100574const char *target_strerror(int err)
thsb92c47c2007-11-01 00:07:38 +0000575{
Richard Hendersonaf254a22021-11-22 19:47:33 +0100576 if (err == QEMU_ERESTARTSYS) {
Peter Maydellda2a34f2016-06-06 19:58:19 +0100577 return "To be restarted";
578 }
Richard Henderson57a0c932021-11-17 05:14:52 -0800579 if (err == QEMU_ESIGRETURN) {
Peter Maydellda2a34f2016-06-06 19:58:19 +0100580 return "Successful exit from sigreturn";
581 }
582
thsb92c47c2007-11-01 00:07:38 +0000583 return strerror(target_to_host_errno(err));
584}
585
Tonis Tiigi45ad7612022-01-04 20:18:18 -0800586static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
587{
588 int i;
589 uint8_t b;
590 if (usize <= ksize) {
591 return 1;
592 }
593 for (i = ksize; i < usize; i++) {
594 if (get_user_u8(b, addr + i)) {
595 return -TARGET_EFAULT;
596 }
597 if (b != 0) {
598 return 0;
599 }
600 }
601 return 1;
602}
603
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100604#define safe_syscall0(type, name) \
605static type safe_##name(void) \
606{ \
607 return safe_syscall(__NR_##name); \
608}
609
610#define safe_syscall1(type, name, type1, arg1) \
611static type safe_##name(type1 arg1) \
612{ \
613 return safe_syscall(__NR_##name, arg1); \
614}
615
616#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
617static type safe_##name(type1 arg1, type2 arg2) \
618{ \
619 return safe_syscall(__NR_##name, arg1, arg2); \
620}
621
622#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
623static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
624{ \
625 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
626}
627
628#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
629 type4, arg4) \
630static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
631{ \
632 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
633}
634
635#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
636 type4, arg4, type5, arg5) \
637static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
638 type5 arg5) \
639{ \
640 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
641}
642
643#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
644 type4, arg4, type5, arg5, type6, arg6) \
645static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
646 type5 arg5, type6 arg6) \
647{ \
648 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
649}
650
Timothy E Baldwin50afd022016-05-12 18:47:47 +0100651safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
652safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
Timothy E Baldwinc10a0732016-05-12 18:47:48 +0100653safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
654 int, flags, mode_t, mode)
Alistair Francis859e8a82020-03-12 15:13:49 -0700655#if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
Timothy E Baldwin4af80a32016-05-12 18:47:49 +0100656safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
657 struct rusage *, rusage)
Alistair Francis859e8a82020-03-12 15:13:49 -0700658#endif
Timothy E Baldwin4af80a32016-05-12 18:47:49 +0100659safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
660 int, options, struct rusage *, rusage)
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +0200661safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
Drew DeVault55bbe4d2022-11-04 18:36:32 +0100662safe_syscall5(int, execveat, int, dirfd, const char *, filename,
663 char **, argv, char **, envp, int, flags)
Alistair Francis859e8a82020-03-12 15:13:49 -0700664#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
Filip Bozutae5ce9682020-08-25 00:30:49 +0200665 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
Peter Maydell6df9d382016-05-12 18:47:51 +0100666safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
667 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
Alistair Francis859e8a82020-03-12 15:13:49 -0700668#endif
Filip Bozutae5ce9682020-08-25 00:30:49 +0200669#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
Peter Maydella6130232016-06-06 19:58:10 +0100670safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
671 struct timespec *, tsp, const sigset_t *, sigmask,
672 size_t, sigsetsize)
Alistair Francis859e8a82020-03-12 15:13:49 -0700673#endif
Peter Maydell227f0212016-06-06 19:58:11 +0100674safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
675 int, maxevents, int, timeout, const sigset_t *, sigmask,
676 size_t, sigsetsize)
Alistair Francis14690292020-03-18 15:47:01 -0700677#if defined(__NR_futex)
Peter Maydelld509eeb2016-05-12 18:47:52 +0100678safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
679 const struct timespec *,timeout,int *,uaddr2,int,val3)
Alistair Francis859e8a82020-03-12 15:13:49 -0700680#endif
Alistair Francis14690292020-03-18 15:47:01 -0700681#if defined(__NR_futex_time64)
682safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
683 const struct timespec *,timeout,int *,uaddr2,int,val3)
684#endif
Peter Maydell2fe4fba2016-05-27 15:51:48 +0100685safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
Peter Maydellbef653d2016-05-27 15:51:57 +0100686safe_syscall2(int, kill, pid_t, pid, int, sig)
687safe_syscall2(int, tkill, int, tid, int, sig)
688safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
Peter Maydell918c03e2016-06-06 19:58:02 +0100689safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
690safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
Dejan Jovicevic0f263862016-10-11 11:52:46 +0200691safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
692 unsigned long, pos_l, unsigned long, pos_h)
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +0200693safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
694 unsigned long, pos_l, unsigned long, pos_h)
Peter Maydell2a3c7612016-06-06 19:58:03 +0100695safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
696 socklen_t, addrlen)
Peter Maydell66687532016-06-06 19:58:04 +0100697safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
698 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
699safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
700 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
701safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
702safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
Peter Maydell2a845982016-06-06 19:58:07 +0100703safe_syscall2(int, flock, int, fd, int, operation)
Filip Bozutaddcbde12020-08-24 21:21:16 +0200704#if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
Peter Maydellb3f82332016-06-06 19:58:08 +0100705safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
706 const struct timespec *, uts, size_t, sigsetsize)
Alistair Francis859e8a82020-03-12 15:13:49 -0700707#endif
Peter Maydellff6dc132016-06-06 19:58:13 +0100708safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
709 int, flags)
Alistair Francis859e8a82020-03-12 15:13:49 -0700710#if defined(TARGET_NR_nanosleep)
Peter Maydell9e518222016-06-06 19:58:09 +0100711safe_syscall2(int, nanosleep, const struct timespec *, req,
712 struct timespec *, rem)
Alistair Francis859e8a82020-03-12 15:13:49 -0700713#endif
Filip Bozuta6ac03b22020-08-24 21:21:15 +0200714#if defined(TARGET_NR_clock_nanosleep) || \
715 defined(TARGET_NR_clock_nanosleep_time64)
Peter Maydell9e518222016-06-06 19:58:09 +0100716safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
717 const struct timespec *, req, struct timespec *, rem)
718#endif
Laurent Vivier524fa342019-05-29 10:48:04 +0200719#ifdef __NR_ipc
Matus Kyseld8c08b12020-06-26 14:46:11 +0200720#ifdef __s390x__
721safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
722 void *, ptr)
723#else
Peter Maydell89f9fe42016-06-06 19:58:05 +0100724safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
725 void *, ptr, long, fifth)
Laurent Vivier86e63692019-05-23 19:54:13 +0200726#endif
Matus Kyseld8c08b12020-06-26 14:46:11 +0200727#endif
Laurent Vivier86e63692019-05-23 19:54:13 +0200728#ifdef __NR_msgsnd
729safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
730 int, flags)
Laurent Vivier86e63692019-05-23 19:54:13 +0200731#endif
732#ifdef __NR_msgrcv
733safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
734 long, msgtype, int, flags)
Laurent Vivier86e63692019-05-23 19:54:13 +0200735#endif
736#ifdef __NR_semtimedop
737safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
738 unsigned, nsops, const struct timespec *, timeout)
Peter Maydell89f9fe42016-06-06 19:58:05 +0100739#endif
Filip Bozutad107e372020-08-24 21:37:52 +0200740#if defined(TARGET_NR_mq_timedsend) || \
741 defined(TARGET_NR_mq_timedsend_time64)
Peter Maydelld40ecd62016-06-06 19:58:06 +0100742safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
743 size_t, len, unsigned, prio, const struct timespec *, timeout)
Alistair Francis859e8a82020-03-12 15:13:49 -0700744#endif
Filip Bozutad107e372020-08-24 21:37:52 +0200745#if defined(TARGET_NR_mq_timedreceive) || \
746 defined(TARGET_NR_mq_timedreceive_time64)
Peter Maydelld40ecd62016-06-06 19:58:06 +0100747safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
748 size_t, len, unsigned *, prio, const struct timespec *, timeout)
749#endif
Andreas Schwab84946452020-11-12 12:45:16 +0100750#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
751safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
752 int, outfd, loff_t *, poutoff, size_t, length,
753 unsigned int, flags)
754#endif
755
Peter Maydell49ca6f32016-06-06 19:58:14 +0100756/* We do ioctl like this rather than via safe_syscall3 to preserve the
757 * "third argument might be integer or pointer or not present" behaviour of
758 * the libc function.
759 */
760#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
Peter Maydell435da5e2016-06-13 11:22:05 +0100761/* Similarly for fcntl. Note that callers must always:
762 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
763 * use the flock64 struct rather than unsuffixed flock
764 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
765 */
766#ifdef __NR_fcntl64
767#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
768#else
769#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
770#endif
Timothy E Baldwin50afd022016-05-12 18:47:47 +0100771
Paul Burton8289d112014-06-22 11:25:33 +0100772static inline int host_to_target_sock_type(int host_type)
773{
774 int target_type;
775
776 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
777 case SOCK_DGRAM:
778 target_type = TARGET_SOCK_DGRAM;
779 break;
780 case SOCK_STREAM:
781 target_type = TARGET_SOCK_STREAM;
782 break;
783 default:
784 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
785 break;
786 }
787
788#if defined(SOCK_CLOEXEC)
789 if (host_type & SOCK_CLOEXEC) {
790 target_type |= TARGET_SOCK_CLOEXEC;
791 }
792#endif
793
794#if defined(SOCK_NONBLOCK)
795 if (host_type & SOCK_NONBLOCK) {
796 target_type |= TARGET_SOCK_NONBLOCK;
797 }
798#endif
799
800 return target_type;
801}
802
Helge Dellerdfe49862023-07-17 12:27:13 +0200803static abi_ulong target_brk, initial_target_brk;
bellard31e31b82003-02-18 22:55:36 +0000804
blueswir1992f48a2007-10-14 16:27:31 +0000805void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000806{
Andreas Schwabd28b3c92023-07-06 13:34:19 +0200807 target_brk = TARGET_PAGE_ALIGN(new_brk);
Helge Dellerdfe49862023-07-17 12:27:13 +0200808 initial_target_brk = target_brk;
bellard31e31b82003-02-18 22:55:36 +0000809}
810
ths0da46a62007-10-20 20:23:07 +0000811/* do_brk() must return target values and target errnos. */
Helge Deller86f04732022-12-25 09:23:19 +0100812abi_long do_brk(abi_ulong brk_val)
bellard31e31b82003-02-18 22:55:36 +0000813{
blueswir1992f48a2007-10-14 16:27:31 +0000814 abi_long mapped_addr;
Akihiko Odaki2aea1372023-08-02 16:17:52 +0900815 abi_ulong new_brk;
816 abi_ulong old_brk;
bellard31e31b82003-02-18 22:55:36 +0000817
Richard Hendersonee1bf832021-02-12 10:48:44 -0800818 /* brk pointers are always untagged */
819
Helge Dellerdfe49862023-07-17 12:27:13 +0200820 /* do not allow to shrink below initial brk value */
821 if (brk_val < initial_target_brk) {
Akihiko Odakicb9d5d12023-08-02 16:17:51 +0900822 return target_brk;
Helge Dellerdfe49862023-07-17 12:27:13 +0200823 }
824
Helge Deller86f04732022-12-25 09:23:19 +0100825 new_brk = TARGET_PAGE_ALIGN(brk_val);
Akihiko Odaki2aea1372023-08-02 16:17:52 +0900826 old_brk = TARGET_PAGE_ALIGN(target_brk);
Helge Deller86f04732022-12-25 09:23:19 +0100827
Akihiko Odaki2aea1372023-08-02 16:17:52 +0900828 /* new and old target_brk might be on the same page */
829 if (new_brk == old_brk) {
Helge Deller86f04732022-12-25 09:23:19 +0100830 target_brk = brk_val;
831 return target_brk;
832 }
833
Michael Tokarev2cf91b92023-11-14 19:07:15 +0300834 /* Release heap if necessary */
Akihiko Odaki2aea1372023-08-02 16:17:52 +0900835 if (new_brk < old_brk) {
836 target_munmap(new_brk, old_brk - new_brk);
Helge Deller86f04732022-12-25 09:23:19 +0100837
838 target_brk = brk_val;
839 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000840 }
841
Akihiko Odaki2aea1372023-08-02 16:17:52 +0900842 mapped_addr = target_mmap(old_brk, new_brk - old_brk,
843 PROT_READ | PROT_WRITE,
844 MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
845 -1, 0);
Peter Maydell00faf082011-04-18 16:34:24 +0100846
Akihiko Odaki2aea1372023-08-02 16:17:52 +0900847 if (mapped_addr == old_brk) {
Helge Deller86f04732022-12-25 09:23:19 +0100848 target_brk = brk_val;
Peter Maydell00faf082011-04-18 16:34:24 +0100849 return target_brk;
Peter Maydell00faf082011-04-18 16:34:24 +0100850 }
balrog7ab240a2008-04-26 12:17:34 +0000851
Richard Henderson7dd46c02010-05-03 10:07:49 -0700852#if defined(TARGET_ALPHA)
853 /* We (partially) emulate OSF/1 on Alpha, which requires we
854 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100855 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700856#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100857 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000858 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000859}
860
Alistair Francis859e8a82020-03-12 15:13:49 -0700861#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
Filip Bozutae5ce9682020-08-25 00:30:49 +0200862 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
ths26edcf42007-12-09 02:25:24 +0000863static inline abi_long copy_from_user_fdset(fd_set *fds,
864 abi_ulong target_fds_addr,
865 int n)
bellard31e31b82003-02-18 22:55:36 +0000866{
ths26edcf42007-12-09 02:25:24 +0000867 int i, nw, j, k;
868 abi_ulong b, *target_fds;
869
Laurent Vivierb1b2db22016-05-31 18:36:02 +0200870 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
ths26edcf42007-12-09 02:25:24 +0000871 if (!(target_fds = lock_user(VERIFY_READ,
872 target_fds_addr,
873 sizeof(abi_ulong) * nw,
874 1)))
875 return -TARGET_EFAULT;
876
877 FD_ZERO(fds);
878 k = 0;
879 for (i = 0; i < nw; i++) {
880 /* grab the abi_ulong */
881 __get_user(b, &target_fds[i]);
882 for (j = 0; j < TARGET_ABI_BITS; j++) {
883 /* check the bit inside the abi_ulong */
884 if ((b >> j) & 1)
885 FD_SET(k, fds);
886 k++;
bellard31e31b82003-02-18 22:55:36 +0000887 }
bellard31e31b82003-02-18 22:55:36 +0000888 }
ths26edcf42007-12-09 02:25:24 +0000889
890 unlock_user(target_fds, target_fds_addr, 0);
891
892 return 0;
bellard31e31b82003-02-18 22:55:36 +0000893}
894
Mike Frysinger055e0902011-06-03 17:01:49 -0400895static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
896 abi_ulong target_fds_addr,
897 int n)
898{
899 if (target_fds_addr) {
900 if (copy_from_user_fdset(fds, target_fds_addr, n))
901 return -TARGET_EFAULT;
902 *fds_ptr = fds;
903 } else {
904 *fds_ptr = NULL;
905 }
906 return 0;
907}
908
ths26edcf42007-12-09 02:25:24 +0000909static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
910 const fd_set *fds,
911 int n)
bellard31e31b82003-02-18 22:55:36 +0000912{
bellard31e31b82003-02-18 22:55:36 +0000913 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000914 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000915 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000916
Laurent Vivierb1b2db22016-05-31 18:36:02 +0200917 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
ths26edcf42007-12-09 02:25:24 +0000918 if (!(target_fds = lock_user(VERIFY_WRITE,
919 target_fds_addr,
920 sizeof(abi_ulong) * nw,
921 0)))
922 return -TARGET_EFAULT;
923
924 k = 0;
925 for (i = 0; i < nw; i++) {
926 v = 0;
927 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000928 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000929 k++;
bellard31e31b82003-02-18 22:55:36 +0000930 }
ths26edcf42007-12-09 02:25:24 +0000931 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000932 }
ths26edcf42007-12-09 02:25:24 +0000933
934 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
935
936 return 0;
bellard31e31b82003-02-18 22:55:36 +0000937}
Alistair Francis859e8a82020-03-12 15:13:49 -0700938#endif
bellard31e31b82003-02-18 22:55:36 +0000939
bellardc596ed12003-07-13 17:32:31 +0000940#if defined(__alpha__)
941#define HOST_HZ 1024
942#else
943#define HOST_HZ 100
944#endif
945
blueswir1992f48a2007-10-14 16:27:31 +0000946static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000947{
948#if HOST_HZ == TARGET_HZ
949 return ticks;
950#else
951 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
952#endif
953}
954
bellard579a97f2007-11-11 14:26:47 +0000955static inline abi_long host_to_target_rusage(abi_ulong target_addr,
956 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000957{
pbrook53a59602006-03-25 19:31:22 +0000958 struct target_rusage *target_rusage;
959
bellard579a97f2007-11-11 14:26:47 +0000960 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
961 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200962 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
963 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
964 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
965 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
966 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
967 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
968 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
969 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
970 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
971 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
972 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
973 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
974 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
975 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
976 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
977 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
978 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
979 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000980 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000981
982 return 0;
bellardb4091862003-05-16 15:39:34 +0000983}
984
Alistair Francis859e8a82020-03-12 15:13:49 -0700985#ifdef TARGET_NR_setrlimit
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200986static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900987{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200988 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300989 rlim_t result;
990
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200991 target_rlim_swap = tswapal(target_rlim);
992 if (target_rlim_swap == TARGET_RLIM_INFINITY)
993 return RLIM_INFINITY;
994
995 result = target_rlim_swap;
996 if (target_rlim_swap != (rlim_t)result)
997 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300998
999 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001000}
Alistair Francis859e8a82020-03-12 15:13:49 -07001001#endif
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001002
Alistair Francis859e8a82020-03-12 15:13:49 -07001003#if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001004static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001005{
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001006 abi_ulong target_rlim_swap;
1007 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001008
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001009 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001010 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001011 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001012 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001013 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +03001014
1015 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001016}
Alistair Francis859e8a82020-03-12 15:13:49 -07001017#endif
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09001018
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03001019static inline int target_to_host_resource(int code)
1020{
1021 switch (code) {
1022 case TARGET_RLIMIT_AS:
1023 return RLIMIT_AS;
1024 case TARGET_RLIMIT_CORE:
1025 return RLIMIT_CORE;
1026 case TARGET_RLIMIT_CPU:
1027 return RLIMIT_CPU;
1028 case TARGET_RLIMIT_DATA:
1029 return RLIMIT_DATA;
1030 case TARGET_RLIMIT_FSIZE:
1031 return RLIMIT_FSIZE;
1032 case TARGET_RLIMIT_LOCKS:
1033 return RLIMIT_LOCKS;
1034 case TARGET_RLIMIT_MEMLOCK:
1035 return RLIMIT_MEMLOCK;
1036 case TARGET_RLIMIT_MSGQUEUE:
1037 return RLIMIT_MSGQUEUE;
1038 case TARGET_RLIMIT_NICE:
1039 return RLIMIT_NICE;
1040 case TARGET_RLIMIT_NOFILE:
1041 return RLIMIT_NOFILE;
1042 case TARGET_RLIMIT_NPROC:
1043 return RLIMIT_NPROC;
1044 case TARGET_RLIMIT_RSS:
1045 return RLIMIT_RSS;
1046 case TARGET_RLIMIT_RTPRIO:
1047 return RLIMIT_RTPRIO;
Fabrice Fontainec3a28d72022-05-23 12:52:39 +02001048#ifdef RLIMIT_RTTIME
Serge Belyshev244fd082022-01-29 22:48:23 +03001049 case TARGET_RLIMIT_RTTIME:
1050 return RLIMIT_RTTIME;
Fabrice Fontainec3a28d72022-05-23 12:52:39 +02001051#endif
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03001052 case TARGET_RLIMIT_SIGPENDING:
1053 return RLIMIT_SIGPENDING;
1054 case TARGET_RLIMIT_STACK:
1055 return RLIMIT_STACK;
1056 default:
1057 return code;
1058 }
1059}
1060
ths788f5ec2007-12-09 02:37:05 +00001061static inline abi_long copy_from_user_timeval(struct timeval *tv,
1062 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001063{
pbrook53a59602006-03-25 19:31:22 +00001064 struct target_timeval *target_tv;
1065
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001066 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
bellard579a97f2007-11-11 14:26:47 +00001067 return -TARGET_EFAULT;
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001068 }
ths788f5ec2007-12-09 02:37:05 +00001069
1070 __get_user(tv->tv_sec, &target_tv->tv_sec);
1071 __get_user(tv->tv_usec, &target_tv->tv_usec);
1072
1073 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001074
1075 return 0;
bellard31e31b82003-02-18 22:55:36 +00001076}
1077
ths788f5ec2007-12-09 02:37:05 +00001078static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1079 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001080{
pbrook53a59602006-03-25 19:31:22 +00001081 struct target_timeval *target_tv;
1082
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001083 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
bellard579a97f2007-11-11 14:26:47 +00001084 return -TARGET_EFAULT;
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001085 }
ths788f5ec2007-12-09 02:37:05 +00001086
1087 __put_user(tv->tv_sec, &target_tv->tv_sec);
1088 __put_user(tv->tv_usec, &target_tv->tv_usec);
1089
1090 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001091
1092 return 0;
bellard31e31b82003-02-18 22:55:36 +00001093}
1094
Filip Bozuta6ac03b22020-08-24 21:21:15 +02001095#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1096static inline abi_long copy_from_user_timeval64(struct timeval *tv,
1097 abi_ulong target_tv_addr)
1098{
1099 struct target__kernel_sock_timeval *target_tv;
1100
1101 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
1102 return -TARGET_EFAULT;
1103 }
1104
1105 __get_user(tv->tv_sec, &target_tv->tv_sec);
1106 __get_user(tv->tv_usec, &target_tv->tv_usec);
1107
1108 unlock_user_struct(target_tv, target_tv_addr, 0);
1109
1110 return 0;
1111}
1112#endif
1113
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001114static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
Filip Bozuta6ac03b22020-08-24 21:21:15 +02001115 const struct timeval *tv)
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001116{
1117 struct target__kernel_sock_timeval *target_tv;
1118
1119 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
1120 return -TARGET_EFAULT;
1121 }
1122
1123 __put_user(tv->tv_sec, &target_tv->tv_sec);
1124 __put_user(tv->tv_usec, &target_tv->tv_usec);
1125
1126 unlock_user_struct(target_tv, target_tv_addr, 1);
1127
1128 return 0;
1129}
1130
Alistair Francis859e8a82020-03-12 15:13:49 -07001131#if defined(TARGET_NR_futex) || \
1132 defined(TARGET_NR_rt_sigtimedwait) || \
1133 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1134 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1135 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
Matus Kyseld8c08b12020-06-26 14:46:11 +02001136 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
Filip Bozuta2c86c902020-07-22 17:34:20 +02001137 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1138 defined(TARGET_NR_timer_settime) || \
1139 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001140static inline abi_long target_to_host_timespec(struct timespec *host_ts,
1141 abi_ulong target_addr)
1142{
1143 struct target_timespec *target_ts;
1144
1145 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1146 return -TARGET_EFAULT;
1147 }
1148 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1149 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1150 unlock_user_struct(target_ts, target_addr, 0);
1151 return 0;
1152}
Alistair Francis859e8a82020-03-12 15:13:49 -07001153#endif
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001154
Filip Bozuta828cb3a2020-07-22 17:34:21 +02001155#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1156 defined(TARGET_NR_timer_settime64) || \
Filip Bozutad107e372020-08-24 21:37:52 +02001157 defined(TARGET_NR_mq_timedsend_time64) || \
1158 defined(TARGET_NR_mq_timedreceive_time64) || \
Filip Bozuta6ac03b22020-08-24 21:21:15 +02001159 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
Filip Bozutaddcbde12020-08-24 21:21:16 +02001160 defined(TARGET_NR_clock_nanosleep_time64) || \
Filip Bozutacac46eb2020-08-25 00:30:50 +02001161 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1162 defined(TARGET_NR_utimensat) || \
1163 defined(TARGET_NR_utimensat_time64) || \
Filip Bozutae5ce9682020-08-25 00:30:49 +02001164 defined(TARGET_NR_semtimedop_time64) || \
1165 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
Alistair Francisc6c8d102020-03-12 15:13:53 -07001166static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
1167 abi_ulong target_addr)
1168{
1169 struct target__kernel_timespec *target_ts;
1170
1171 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1172 return -TARGET_EFAULT;
1173 }
1174 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1175 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
Laurent Vivier00576752020-08-27 09:04:49 +02001176 /* in 32bit mode, this drops the padding */
1177 host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
Alistair Francisc6c8d102020-03-12 15:13:53 -07001178 unlock_user_struct(target_ts, target_addr, 0);
1179 return 0;
1180}
1181#endif
1182
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02001183static inline abi_long host_to_target_timespec(abi_ulong target_addr,
1184 struct timespec *host_ts)
1185{
1186 struct target_timespec *target_ts;
1187
1188 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1189 return -TARGET_EFAULT;
1190 }
1191 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1192 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1193 unlock_user_struct(target_ts, target_addr, 1);
1194 return 0;
1195}
1196
1197static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
1198 struct timespec *host_ts)
1199{
1200 struct target__kernel_timespec *target_ts;
1201
1202 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1203 return -TARGET_EFAULT;
1204 }
1205 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1206 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1207 unlock_user_struct(target_ts, target_addr, 1);
1208 return 0;
1209}
1210
Richard Hendersona52f5f82020-02-12 19:22:23 -08001211#if defined(TARGET_NR_gettimeofday)
1212static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
1213 struct timezone *tz)
1214{
1215 struct target_timezone *target_tz;
1216
1217 if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
1218 return -TARGET_EFAULT;
1219 }
1220
1221 __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1222 __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1223
1224 unlock_user_struct(target_tz, target_tz_addr, 1);
1225
1226 return 0;
1227}
1228#endif
1229
Alistair Francis859e8a82020-03-12 15:13:49 -07001230#if defined(TARGET_NR_settimeofday)
Paul Burtonef4467e2014-06-22 11:25:40 +01001231static inline abi_long copy_from_user_timezone(struct timezone *tz,
1232 abi_ulong target_tz_addr)
1233{
1234 struct target_timezone *target_tz;
1235
1236 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1237 return -TARGET_EFAULT;
1238 }
1239
1240 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1241 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1242
1243 unlock_user_struct(target_tz, target_tz_addr, 0);
1244
1245 return 0;
1246}
Alistair Francis859e8a82020-03-12 15:13:49 -07001247#endif
Paul Burtonef4467e2014-06-22 11:25:40 +01001248
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001249#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1250#include <mqueue.h>
1251
aurel3224e10032009-04-15 16:11:43 +00001252static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1253 abi_ulong target_mq_attr_addr)
1254{
1255 struct target_mq_attr *target_mq_attr;
1256
1257 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1258 target_mq_attr_addr, 1))
1259 return -TARGET_EFAULT;
1260
1261 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1262 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1263 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1264 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1265
1266 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1267
1268 return 0;
1269}
1270
1271static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1272 const struct mq_attr *attr)
1273{
1274 struct target_mq_attr *target_mq_attr;
1275
1276 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1277 target_mq_attr_addr, 0))
1278 return -TARGET_EFAULT;
1279
1280 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1281 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1282 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1283 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1284
1285 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1286
1287 return 0;
1288}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001289#endif
bellard31e31b82003-02-18 22:55:36 +00001290
Mike Frysinger055e0902011-06-03 17:01:49 -04001291#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001292/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001293static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001294 abi_ulong rfd_addr, abi_ulong wfd_addr,
1295 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001296{
1297 fd_set rfds, wfds, efds;
1298 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
Peter Maydell6df9d382016-05-12 18:47:51 +01001299 struct timeval tv;
1300 struct timespec ts, *ts_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001301 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001302
Mike Frysinger055e0902011-06-03 17:01:49 -04001303 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1304 if (ret) {
1305 return ret;
pbrook53a59602006-03-25 19:31:22 +00001306 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001307 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1308 if (ret) {
1309 return ret;
pbrook53a59602006-03-25 19:31:22 +00001310 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001311 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1312 if (ret) {
1313 return ret;
pbrook53a59602006-03-25 19:31:22 +00001314 }
ths3b46e622007-09-17 08:09:54 +00001315
ths26edcf42007-12-09 02:25:24 +00001316 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001317 if (copy_from_user_timeval(&tv, target_tv_addr))
1318 return -TARGET_EFAULT;
Peter Maydell6df9d382016-05-12 18:47:51 +01001319 ts.tv_sec = tv.tv_sec;
1320 ts.tv_nsec = tv.tv_usec * 1000;
1321 ts_ptr = &ts;
bellard31e31b82003-02-18 22:55:36 +00001322 } else {
Peter Maydell6df9d382016-05-12 18:47:51 +01001323 ts_ptr = NULL;
bellard31e31b82003-02-18 22:55:36 +00001324 }
ths26edcf42007-12-09 02:25:24 +00001325
Peter Maydell6df9d382016-05-12 18:47:51 +01001326 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1327 ts_ptr, NULL));
pbrook53a59602006-03-25 19:31:22 +00001328
ths26edcf42007-12-09 02:25:24 +00001329 if (!is_error(ret)) {
1330 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1331 return -TARGET_EFAULT;
1332 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1333 return -TARGET_EFAULT;
1334 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1335 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001336
Peter Maydell6df9d382016-05-12 18:47:51 +01001337 if (target_tv_addr) {
1338 tv.tv_sec = ts.tv_sec;
1339 tv.tv_usec = ts.tv_nsec / 1000;
1340 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1341 return -TARGET_EFAULT;
1342 }
1343 }
bellard31e31b82003-02-18 22:55:36 +00001344 }
bellard579a97f2007-11-11 14:26:47 +00001345
bellard31e31b82003-02-18 22:55:36 +00001346 return ret;
1347}
Laurent Vivier5457dc92016-07-08 01:17:27 +02001348
1349#if defined(TARGET_WANT_OLD_SYS_SELECT)
1350static abi_long do_old_select(abi_ulong arg1)
1351{
1352 struct target_sel_arg_struct *sel;
1353 abi_ulong inp, outp, exp, tvp;
1354 long nsel;
1355
1356 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1357 return -TARGET_EFAULT;
1358 }
1359
1360 nsel = tswapal(sel->n);
1361 inp = tswapal(sel->inp);
1362 outp = tswapal(sel->outp);
1363 exp = tswapal(sel->exp);
1364 tvp = tswapal(sel->tvp);
1365
1366 unlock_user_struct(sel, arg1, 0);
1367
1368 return do_select(nsel, inp, outp, exp, tvp);
1369}
1370#endif
Mike Frysinger055e0902011-06-03 17:01:49 -04001371#endif
bellard31e31b82003-02-18 22:55:36 +00001372
Filip Bozutae5ce9682020-08-25 00:30:49 +02001373#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1374static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
1375 abi_long arg4, abi_long arg5, abi_long arg6,
1376 bool time64)
1377{
1378 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
1379 fd_set rfds, wfds, efds;
1380 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1381 struct timespec ts, *ts_ptr;
1382 abi_long ret;
1383
1384 /*
1385 * The 6th arg is actually two args smashed together,
1386 * so we cannot use the C library.
1387 */
Filip Bozutae5ce9682020-08-25 00:30:49 +02001388 struct {
1389 sigset_t *set;
1390 size_t size;
1391 } sig, *sig_ptr;
1392
1393 abi_ulong arg_sigset, arg_sigsize, *arg7;
Filip Bozutae5ce9682020-08-25 00:30:49 +02001394
1395 n = arg1;
1396 rfd_addr = arg2;
1397 wfd_addr = arg3;
1398 efd_addr = arg4;
1399 ts_addr = arg5;
1400
1401 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1402 if (ret) {
1403 return ret;
1404 }
1405 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1406 if (ret) {
1407 return ret;
1408 }
1409 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1410 if (ret) {
1411 return ret;
1412 }
1413
1414 /*
1415 * This takes a timespec, and not a timeval, so we cannot
1416 * use the do_select() helper ...
1417 */
1418 if (ts_addr) {
1419 if (time64) {
1420 if (target_to_host_timespec64(&ts, ts_addr)) {
1421 return -TARGET_EFAULT;
1422 }
1423 } else {
1424 if (target_to_host_timespec(&ts, ts_addr)) {
1425 return -TARGET_EFAULT;
1426 }
1427 }
1428 ts_ptr = &ts;
1429 } else {
1430 ts_ptr = NULL;
1431 }
1432
1433 /* Extract the two packed args for the sigset */
Richard Hendersoncb226032022-03-15 01:43:06 -07001434 sig_ptr = NULL;
Filip Bozutae5ce9682020-08-25 00:30:49 +02001435 if (arg6) {
Filip Bozutae5ce9682020-08-25 00:30:49 +02001436 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
1437 if (!arg7) {
1438 return -TARGET_EFAULT;
1439 }
1440 arg_sigset = tswapal(arg7[0]);
1441 arg_sigsize = tswapal(arg7[1]);
1442 unlock_user(arg7, arg6, 0);
1443
1444 if (arg_sigset) {
Richard Hendersoncb226032022-03-15 01:43:06 -07001445 ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
1446 if (ret != 0) {
1447 return ret;
Filip Bozutae5ce9682020-08-25 00:30:49 +02001448 }
Richard Hendersoncb226032022-03-15 01:43:06 -07001449 sig_ptr = &sig;
1450 sig.size = SIGSET_T_SIZE;
Filip Bozutae5ce9682020-08-25 00:30:49 +02001451 }
Filip Bozutae5ce9682020-08-25 00:30:49 +02001452 }
1453
1454 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1455 ts_ptr, sig_ptr));
1456
Richard Hendersoncb226032022-03-15 01:43:06 -07001457 if (sig_ptr) {
1458 finish_sigsuspend_mask(ret);
1459 }
1460
Filip Bozutae5ce9682020-08-25 00:30:49 +02001461 if (!is_error(ret)) {
1462 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
1463 return -TARGET_EFAULT;
1464 }
1465 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
1466 return -TARGET_EFAULT;
1467 }
1468 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
1469 return -TARGET_EFAULT;
1470 }
1471 if (time64) {
1472 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
1473 return -TARGET_EFAULT;
1474 }
1475 } else {
1476 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
1477 return -TARGET_EFAULT;
1478 }
1479 }
1480 }
1481 return ret;
1482}
1483#endif
1484
1485#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1486 defined(TARGET_NR_ppoll_time64)
1487static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
1488 abi_long arg4, abi_long arg5, bool ppoll, bool time64)
1489{
1490 struct target_pollfd *target_pfd;
1491 unsigned int nfds = arg2;
1492 struct pollfd *pfd;
1493 unsigned int i;
1494 abi_long ret;
1495
1496 pfd = NULL;
1497 target_pfd = NULL;
1498 if (nfds) {
1499 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
1500 return -TARGET_EINVAL;
1501 }
1502 target_pfd = lock_user(VERIFY_WRITE, arg1,
1503 sizeof(struct target_pollfd) * nfds, 1);
1504 if (!target_pfd) {
1505 return -TARGET_EFAULT;
1506 }
1507
1508 pfd = alloca(sizeof(struct pollfd) * nfds);
1509 for (i = 0; i < nfds; i++) {
1510 pfd[i].fd = tswap32(target_pfd[i].fd);
1511 pfd[i].events = tswap16(target_pfd[i].events);
1512 }
1513 }
1514 if (ppoll) {
1515 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
Richard Hendersondb36aa72022-03-15 01:43:08 -07001516 sigset_t *set = NULL;
Filip Bozutae5ce9682020-08-25 00:30:49 +02001517
1518 if (arg3) {
1519 if (time64) {
1520 if (target_to_host_timespec64(timeout_ts, arg3)) {
1521 unlock_user(target_pfd, arg1, 0);
1522 return -TARGET_EFAULT;
1523 }
1524 } else {
1525 if (target_to_host_timespec(timeout_ts, arg3)) {
1526 unlock_user(target_pfd, arg1, 0);
1527 return -TARGET_EFAULT;
1528 }
1529 }
1530 } else {
1531 timeout_ts = NULL;
1532 }
1533
1534 if (arg4) {
Richard Hendersondb36aa72022-03-15 01:43:08 -07001535 ret = process_sigsuspend_mask(&set, arg4, arg5);
1536 if (ret != 0) {
Filip Bozutae5ce9682020-08-25 00:30:49 +02001537 unlock_user(target_pfd, arg1, 0);
Richard Hendersondb36aa72022-03-15 01:43:08 -07001538 return ret;
Filip Bozutae5ce9682020-08-25 00:30:49 +02001539 }
Filip Bozutae5ce9682020-08-25 00:30:49 +02001540 }
1541
1542 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
1543 set, SIGSET_T_SIZE));
1544
Richard Hendersondb36aa72022-03-15 01:43:08 -07001545 if (set) {
1546 finish_sigsuspend_mask(ret);
1547 }
Filip Bozutae5ce9682020-08-25 00:30:49 +02001548 if (!is_error(ret) && arg3) {
1549 if (time64) {
1550 if (host_to_target_timespec64(arg3, timeout_ts)) {
1551 return -TARGET_EFAULT;
1552 }
1553 } else {
1554 if (host_to_target_timespec(arg3, timeout_ts)) {
1555 return -TARGET_EFAULT;
1556 }
1557 }
1558 }
Filip Bozutae5ce9682020-08-25 00:30:49 +02001559 } else {
1560 struct timespec ts, *pts;
1561
1562 if (arg3 >= 0) {
1563 /* Convert ms to secs, ns */
1564 ts.tv_sec = arg3 / 1000;
1565 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
1566 pts = &ts;
1567 } else {
1568 /* -ve poll() timeout means "infinite" */
1569 pts = NULL;
1570 }
1571 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
1572 }
1573
1574 if (!is_error(ret)) {
1575 for (i = 0; i < nfds; i++) {
1576 target_pfd[i].revents = tswap16(pfd[i].revents);
1577 }
1578 }
1579 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
1580 return ret;
1581}
1582#endif
1583
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02001584static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
Richard Hendersonfb41a662010-05-03 10:07:52 -07001585 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001586{
1587 int host_pipe[2];
1588 abi_long ret;
Helge Deller499d8052022-07-19 18:20:42 +02001589 ret = pipe2(host_pipe, flags);
Riku Voipio099d6b02009-05-05 12:10:04 +03001590
1591 if (is_error(ret))
1592 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001593
1594 /* Several targets have special calling conventions for the original
1595 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1596 if (!is_pipe2) {
1597#if defined(TARGET_ALPHA)
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02001598 cpu_env->ir[IR_A4] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001599 return host_pipe[0];
1600#elif defined(TARGET_MIPS)
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02001601 cpu_env->active_tc.gpr[3] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001602 return host_pipe[0];
1603#elif defined(TARGET_SH4)
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02001604 cpu_env->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001605 return host_pipe[0];
Peter Maydell82f05b62013-07-06 17:39:48 +01001606#elif defined(TARGET_SPARC)
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02001607 cpu_env->regwptr[1] = host_pipe[1];
Peter Maydell82f05b62013-07-06 17:39:48 +01001608 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001609#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001610 }
1611
Riku Voipio099d6b02009-05-05 12:10:04 +03001612 if (put_user_s32(host_pipe[0], pipedes)
Helge Deller6f200f52022-07-17 18:21:53 +02001613 || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
Riku Voipio099d6b02009-05-05 12:10:04 +03001614 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001615 return get_errno(ret);
1616}
1617
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001618static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1619 abi_ulong target_addr,
1620 socklen_t len)
1621{
1622 struct target_ip_mreqn *target_smreqn;
1623
1624 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1625 if (!target_smreqn)
1626 return -TARGET_EFAULT;
1627 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1628 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1629 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001630 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001631 unlock_user(target_smreqn, target_addr, 0);
1632
1633 return 0;
1634}
1635
Laurent Vivier7b36f782015-10-28 21:40:44 +01001636static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
bellard579a97f2007-11-11 14:26:47 +00001637 abi_ulong target_addr,
1638 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001639{
aurel32607175e2009-04-15 16:11:59 +00001640 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1641 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001642 struct target_sockaddr *target_saddr;
1643
Laurent Vivier7b36f782015-10-28 21:40:44 +01001644 if (fd_trans_target_to_host_addr(fd)) {
1645 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1646 }
1647
bellard579a97f2007-11-11 14:26:47 +00001648 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1649 if (!target_saddr)
1650 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001651
1652 sa_family = tswap16(target_saddr->sa_family);
1653
1654 /* Oops. The caller might send a incomplete sun_path; sun_path
1655 * must be terminated by \0 (see the manual page), but
1656 * unfortunately it is quite common to specify sockaddr_un
1657 * length as "strlen(x->sun_path)" while it should be
1658 * "strlen(...) + 1". We'll fix that here if needed.
1659 * Linux kernel has a similar feature.
1660 */
1661
1662 if (sa_family == AF_UNIX) {
1663 if (len < unix_maxlen && len > 0) {
1664 char *cp = (char*)target_saddr;
1665
1666 if ( cp[len-1] && !cp[len] )
1667 len++;
1668 }
1669 if (len > unix_maxlen)
1670 len = unix_maxlen;
1671 }
1672
pbrook53a59602006-03-25 19:31:22 +00001673 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001674 addr->sa_family = sa_family;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001675 if (sa_family == AF_NETLINK) {
1676 struct sockaddr_nl *nladdr;
1677
1678 nladdr = (struct sockaddr_nl *)addr;
1679 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1680 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1681 } else if (sa_family == AF_PACKET) {
Joakim Tjernlund33a29b52014-07-12 15:47:07 +02001682 struct target_sockaddr_ll *lladdr;
1683
1684 lladdr = (struct target_sockaddr_ll *)addr;
1685 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1686 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
Mathis Marion44cf6732023-03-07 16:42:55 +01001687 } else if (sa_family == AF_INET6) {
1688 struct sockaddr_in6 *in6addr;
1689
1690 in6addr = (struct sockaddr_in6 *)addr;
1691 in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id);
Joakim Tjernlund33a29b52014-07-12 15:47:07 +02001692 }
pbrook53a59602006-03-25 19:31:22 +00001693 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001694
1695 return 0;
bellard7854b052003-03-29 17:22:23 +00001696}
1697
bellard579a97f2007-11-11 14:26:47 +00001698static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1699 struct sockaddr *addr,
1700 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001701{
pbrook53a59602006-03-25 19:31:22 +00001702 struct target_sockaddr *target_saddr;
1703
Peter Maydella1e22192016-07-07 15:44:43 +01001704 if (len == 0) {
1705 return 0;
1706 }
Philippe Mathieu-Daudé68607102017-07-26 23:42:18 -03001707 assert(addr);
Peter Maydella1e22192016-07-07 15:44:43 +01001708
bellard579a97f2007-11-11 14:26:47 +00001709 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1710 if (!target_saddr)
1711 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001712 memcpy(target_saddr, addr, len);
Peter Maydella1e22192016-07-07 15:44:43 +01001713 if (len >= offsetof(struct target_sockaddr, sa_family) +
1714 sizeof(target_saddr->sa_family)) {
1715 target_saddr->sa_family = tswap16(addr->sa_family);
1716 }
Philippe Mathieu-Daudéa47401b2019-10-21 13:48:52 +02001717 if (addr->sa_family == AF_NETLINK &&
1718 len >= sizeof(struct target_sockaddr_nl)) {
1719 struct target_sockaddr_nl *target_nl =
1720 (struct target_sockaddr_nl *)target_saddr;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001721 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1722 target_nl->nl_groups = tswap32(target_nl->nl_groups);
Laurent Viviera82ea932016-06-27 00:18:22 +02001723 } else if (addr->sa_family == AF_PACKET) {
1724 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1725 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1726 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
Helge Delleree1ac3a2017-02-18 23:31:30 +01001727 } else if (addr->sa_family == AF_INET6 &&
1728 len >= sizeof(struct target_sockaddr_in6)) {
1729 struct target_sockaddr_in6 *target_in6 =
1730 (struct target_sockaddr_in6 *)target_saddr;
1731 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001732 }
pbrook53a59602006-03-25 19:31:22 +00001733 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001734
1735 return 0;
bellard7854b052003-03-29 17:22:23 +00001736}
1737
bellard5a4a8982007-11-11 17:39:18 +00001738static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1739 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001740{
1741 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001742 abi_long msg_controllen;
1743 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001744 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001745 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001746
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001747 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001748 if (msg_controllen < sizeof (struct target_cmsghdr))
1749 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001750 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001751 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001752 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001753 if (!target_cmsg)
1754 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001755
1756 while (cmsg && target_cmsg) {
1757 void *data = CMSG_DATA(cmsg);
1758 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1759
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001760 int len = tswapal(target_cmsg->cmsg_len)
Peter Maydellad762b92017-12-15 13:52:56 +00001761 - sizeof(struct target_cmsghdr);
bellard7854b052003-03-29 17:22:23 +00001762
1763 space += CMSG_SPACE(len);
1764 if (space > msgh->msg_controllen) {
1765 space -= CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001766 /* This is a QEMU bug, since we allocated the payload
1767 * area ourselves (unlike overflow in host-to-target
1768 * conversion, which is just the guest giving us a buffer
1769 * that's too small). It can't happen for the payload types
1770 * we currently support; if it becomes an issue in future
1771 * we would need to improve our allocation strategy to
1772 * something more intelligent than "twice the size of the
1773 * target buffer we're reading from".
1774 */
Josh Kunz39be5352020-02-03 18:54:13 -08001775 qemu_log_mask(LOG_UNIMP,
1776 ("Unsupported ancillary data %d/%d: "
1777 "unhandled msg size\n"),
1778 tswap32(target_cmsg->cmsg_level),
1779 tswap32(target_cmsg->cmsg_type));
bellard7854b052003-03-29 17:22:23 +00001780 break;
1781 }
1782
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001783 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1784 cmsg->cmsg_level = SOL_SOCKET;
1785 } else {
1786 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1787 }
bellard7854b052003-03-29 17:22:23 +00001788 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1789 cmsg->cmsg_len = CMSG_LEN(len);
1790
Alex Suykov30b8b682014-12-23 07:52:58 +02001791 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001792 int *fd = (int *)data;
1793 int *target_fd = (int *)target_data;
1794 int i, numfds = len / sizeof(int);
1795
Peter Maydell876e23c2015-05-26 19:46:32 +01001796 for (i = 0; i < numfds; i++) {
1797 __get_user(fd[i], target_fd + i);
1798 }
Alex Suykov30b8b682014-12-23 07:52:58 +02001799 } else if (cmsg->cmsg_level == SOL_SOCKET
1800 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1801 struct ucred *cred = (struct ucred *)data;
1802 struct target_ucred *target_cred =
1803 (struct target_ucred *)target_data;
1804
Peter Maydell876e23c2015-05-26 19:46:32 +01001805 __get_user(cred->pid, &target_cred->pid);
1806 __get_user(cred->uid, &target_cred->uid);
1807 __get_user(cred->gid, &target_cred->gid);
Helge Deller27404b62022-12-12 18:34:15 +01001808 } else if (cmsg->cmsg_level == SOL_ALG) {
1809 uint32_t *dst = (uint32_t *)data;
1810
1811 memcpy(dst, target_data, len);
Michael Tokarev669dcb62023-08-23 08:53:30 +02001812 /* fix endianness of first 32-bit word */
Helge Deller27404b62022-12-12 18:34:15 +01001813 if (len >= sizeof(uint32_t)) {
1814 *dst = tswap32(*dst);
1815 }
Alex Suykov30b8b682014-12-23 07:52:58 +02001816 } else {
Josh Kunz39be5352020-02-03 18:54:13 -08001817 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
1818 cmsg->cmsg_level, cmsg->cmsg_type);
Alex Suykov30b8b682014-12-23 07:52:58 +02001819 memcpy(data, target_data, len);
bellard7854b052003-03-29 17:22:23 +00001820 }
1821
1822 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001823 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1824 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001825 }
bellard5a4a8982007-11-11 17:39:18 +00001826 unlock_user(target_cmsg, target_cmsg_addr, 0);
1827 the_end:
bellard7854b052003-03-29 17:22:23 +00001828 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001829 return 0;
bellard7854b052003-03-29 17:22:23 +00001830}
1831
bellard5a4a8982007-11-11 17:39:18 +00001832static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1833 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001834{
1835 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001836 abi_long msg_controllen;
1837 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001838 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001839 socklen_t space = 0;
1840
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001841 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001842 if (msg_controllen < sizeof (struct target_cmsghdr))
1843 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001844 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001845 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001846 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001847 if (!target_cmsg)
1848 return -TARGET_EFAULT;
1849
bellard7854b052003-03-29 17:22:23 +00001850 while (cmsg && target_cmsg) {
1851 void *data = CMSG_DATA(cmsg);
1852 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1853
Peter Maydellad762b92017-12-15 13:52:56 +00001854 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001855 int tgt_len, tgt_space;
bellard7854b052003-03-29 17:22:23 +00001856
Peter Maydellc2aeb252015-05-26 19:46:31 +01001857 /* We never copy a half-header but may copy half-data;
1858 * this is Linux's behaviour in put_cmsg(). Note that
1859 * truncation here is a guest problem (which we report
1860 * to the guest via the CTRUNC bit), unlike truncation
1861 * in target_to_host_cmsg, which is a QEMU bug.
1862 */
Peter Maydell71749702017-12-15 13:52:55 +00001863 if (msg_controllen < sizeof(struct target_cmsghdr)) {
Peter Maydellc2aeb252015-05-26 19:46:31 +01001864 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
bellard7854b052003-03-29 17:22:23 +00001865 break;
1866 }
1867
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001868 if (cmsg->cmsg_level == SOL_SOCKET) {
1869 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1870 } else {
1871 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1872 }
bellard7854b052003-03-29 17:22:23 +00001873 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
bellard7854b052003-03-29 17:22:23 +00001874
Peter Maydellc2aeb252015-05-26 19:46:31 +01001875 /* Payload types which need a different size of payload on
1876 * the target must adjust tgt_len here.
1877 */
Peter Maydell309786c2018-05-18 19:47:15 +01001878 tgt_len = len;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001879 switch (cmsg->cmsg_level) {
1880 case SOL_SOCKET:
1881 switch (cmsg->cmsg_type) {
1882 case SO_TIMESTAMP:
1883 tgt_len = sizeof(struct target_timeval);
1884 break;
1885 default:
1886 break;
1887 }
Peter Maydell309786c2018-05-18 19:47:15 +01001888 break;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001889 default:
1890 break;
1891 }
1892
Peter Maydell71749702017-12-15 13:52:55 +00001893 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
Peter Maydellc2aeb252015-05-26 19:46:31 +01001894 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
Peter Maydell71749702017-12-15 13:52:55 +00001895 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001896 }
1897
1898 /* We must now copy-and-convert len bytes of payload
1899 * into tgt_len bytes of destination space. Bear in mind
1900 * that in both source and destination we may be dealing
1901 * with a truncated value!
1902 */
Huw Davies52b65492014-04-17 14:02:47 +01001903 switch (cmsg->cmsg_level) {
1904 case SOL_SOCKET:
1905 switch (cmsg->cmsg_type) {
1906 case SCM_RIGHTS:
1907 {
1908 int *fd = (int *)data;
1909 int *target_fd = (int *)target_data;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001910 int i, numfds = tgt_len / sizeof(int);
bellard7854b052003-03-29 17:22:23 +00001911
Peter Maydell876e23c2015-05-26 19:46:32 +01001912 for (i = 0; i < numfds; i++) {
1913 __put_user(fd[i], target_fd + i);
1914 }
Huw Davies52b65492014-04-17 14:02:47 +01001915 break;
1916 }
1917 case SO_TIMESTAMP:
1918 {
1919 struct timeval *tv = (struct timeval *)data;
1920 struct target_timeval *target_tv =
1921 (struct target_timeval *)target_data;
Jing Huangaebf5bc2012-07-24 14:01:42 +00001922
Peter Maydellc2aeb252015-05-26 19:46:31 +01001923 if (len != sizeof(struct timeval) ||
1924 tgt_len != sizeof(struct target_timeval)) {
Huw Davies52b65492014-04-17 14:02:47 +01001925 goto unimplemented;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001926 }
Huw Davies52b65492014-04-17 14:02:47 +01001927
1928 /* copy struct timeval to target */
Peter Maydell876e23c2015-05-26 19:46:32 +01001929 __put_user(tv->tv_sec, &target_tv->tv_sec);
1930 __put_user(tv->tv_usec, &target_tv->tv_usec);
Huw Davies52b65492014-04-17 14:02:47 +01001931 break;
1932 }
Huw Davies4bc29752014-04-17 14:02:48 +01001933 case SCM_CREDENTIALS:
1934 {
1935 struct ucred *cred = (struct ucred *)data;
1936 struct target_ucred *target_cred =
1937 (struct target_ucred *)target_data;
1938
1939 __put_user(cred->pid, &target_cred->pid);
1940 __put_user(cred->uid, &target_cred->uid);
1941 __put_user(cred->gid, &target_cred->gid);
1942 break;
1943 }
Huw Davies52b65492014-04-17 14:02:47 +01001944 default:
1945 goto unimplemented;
1946 }
1947 break;
1948
Helge Delleree1ac3a2017-02-18 23:31:30 +01001949 case SOL_IP:
1950 switch (cmsg->cmsg_type) {
1951 case IP_TTL:
1952 {
1953 uint32_t *v = (uint32_t *)data;
1954 uint32_t *t_int = (uint32_t *)target_data;
1955
Peter Maydell71749702017-12-15 13:52:55 +00001956 if (len != sizeof(uint32_t) ||
1957 tgt_len != sizeof(uint32_t)) {
1958 goto unimplemented;
1959 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01001960 __put_user(*v, t_int);
1961 break;
1962 }
1963 case IP_RECVERR:
1964 {
1965 struct errhdr_t {
1966 struct sock_extended_err ee;
1967 struct sockaddr_in offender;
1968 };
1969 struct errhdr_t *errh = (struct errhdr_t *)data;
1970 struct errhdr_t *target_errh =
1971 (struct errhdr_t *)target_data;
1972
Peter Maydell71749702017-12-15 13:52:55 +00001973 if (len != sizeof(struct errhdr_t) ||
1974 tgt_len != sizeof(struct errhdr_t)) {
1975 goto unimplemented;
1976 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01001977 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1978 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1979 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1980 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1981 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1982 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1983 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1984 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1985 (void *) &errh->offender, sizeof(errh->offender));
1986 break;
1987 }
1988 default:
1989 goto unimplemented;
1990 }
1991 break;
1992
1993 case SOL_IPV6:
1994 switch (cmsg->cmsg_type) {
1995 case IPV6_HOPLIMIT:
1996 {
1997 uint32_t *v = (uint32_t *)data;
1998 uint32_t *t_int = (uint32_t *)target_data;
1999
Peter Maydell71749702017-12-15 13:52:55 +00002000 if (len != sizeof(uint32_t) ||
2001 tgt_len != sizeof(uint32_t)) {
2002 goto unimplemented;
2003 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002004 __put_user(*v, t_int);
2005 break;
2006 }
2007 case IPV6_RECVERR:
2008 {
2009 struct errhdr6_t {
2010 struct sock_extended_err ee;
2011 struct sockaddr_in6 offender;
2012 };
2013 struct errhdr6_t *errh = (struct errhdr6_t *)data;
2014 struct errhdr6_t *target_errh =
2015 (struct errhdr6_t *)target_data;
2016
Peter Maydell71749702017-12-15 13:52:55 +00002017 if (len != sizeof(struct errhdr6_t) ||
2018 tgt_len != sizeof(struct errhdr6_t)) {
2019 goto unimplemented;
2020 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002021 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
2022 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
2023 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
2024 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
2025 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
2026 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
2027 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
2028 host_to_target_sockaddr((unsigned long) &target_errh->offender,
2029 (void *) &errh->offender, sizeof(errh->offender));
2030 break;
2031 }
2032 default:
2033 goto unimplemented;
2034 }
2035 break;
2036
Huw Davies52b65492014-04-17 14:02:47 +01002037 default:
2038 unimplemented:
Josh Kunz39be5352020-02-03 18:54:13 -08002039 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
2040 cmsg->cmsg_level, cmsg->cmsg_type);
Peter Maydellc2aeb252015-05-26 19:46:31 +01002041 memcpy(target_data, data, MIN(len, tgt_len));
2042 if (tgt_len > len) {
2043 memset(target_data + len, 0, tgt_len - len);
2044 }
bellard7854b052003-03-29 17:22:23 +00002045 }
2046
Peter Maydell71749702017-12-15 13:52:55 +00002047 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
2048 tgt_space = TARGET_CMSG_SPACE(tgt_len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01002049 if (msg_controllen < tgt_space) {
2050 tgt_space = msg_controllen;
2051 }
2052 msg_controllen -= tgt_space;
2053 space += tgt_space;
bellard7854b052003-03-29 17:22:23 +00002054 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02002055 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
2056 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00002057 }
bellard5a4a8982007-11-11 17:39:18 +00002058 unlock_user(target_cmsg, target_cmsg_addr, space);
2059 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002060 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00002061 return 0;
bellard7854b052003-03-29 17:22:23 +00002062}
2063
ths0da46a62007-10-20 20:23:07 +00002064/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002065static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00002066 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00002067{
blueswir1992f48a2007-10-14 16:27:31 +00002068 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00002069 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02002070 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002071 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00002072
bellard8853f862004-02-22 14:57:26 +00002073 switch(level) {
2074 case SOL_TCP:
Shu-Chun Wengfe51b0a2020-12-18 11:32:11 -08002075 case SOL_UDP:
2076 /* TCP and UDP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00002077 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00002078 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00002079
bellard2f619692007-11-16 10:46:05 +00002080 if (get_user_u32(val, optval_addr))
2081 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002082 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2083 break;
2084 case SOL_IP:
2085 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00002086 case IP_TOS:
2087 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00002088 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00002089 case IP_ROUTER_ALERT:
2090 case IP_RECVOPTS:
2091 case IP_RETOPTS:
2092 case IP_PKTINFO:
2093 case IP_MTU_DISCOVER:
2094 case IP_RECVERR:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002095 case IP_RECVTTL:
bellard2efbe912005-07-23 15:10:20 +00002096 case IP_RECVTOS:
2097#ifdef IP_FREEBIND
2098 case IP_FREEBIND:
2099#endif
2100 case IP_MULTICAST_TTL:
2101 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00002102 val = 0;
2103 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00002104 if (get_user_u32(val, optval_addr))
2105 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002106 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00002107 if (get_user_u8(val, optval_addr))
2108 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002109 }
2110 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2111 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02002112 case IP_ADD_MEMBERSHIP:
2113 case IP_DROP_MEMBERSHIP:
2114 if (optlen < sizeof (struct target_ip_mreq) ||
2115 optlen > sizeof (struct target_ip_mreqn))
2116 return -TARGET_EINVAL;
2117
2118 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2119 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2120 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2121 break;
2122
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002123 case IP_BLOCK_SOURCE:
2124 case IP_UNBLOCK_SOURCE:
2125 case IP_ADD_SOURCE_MEMBERSHIP:
2126 case IP_DROP_SOURCE_MEMBERSHIP:
2127 if (optlen != sizeof (struct target_ip_mreq_source))
2128 return -TARGET_EINVAL;
2129
2130 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
Peter Maydell74e43b02021-08-09 16:54:24 +01002131 if (!ip_mreq_source) {
2132 return -TARGET_EFAULT;
2133 }
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002134 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2135 unlock_user (ip_mreq_source, optval_addr, 0);
2136 break;
2137
bellard8853f862004-02-22 14:57:26 +00002138 default:
2139 goto unimplemented;
2140 }
2141 break;
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002142 case SOL_IPV6:
2143 switch (optname) {
2144 case IPV6_MTU_DISCOVER:
2145 case IPV6_MTU:
2146 case IPV6_V6ONLY:
2147 case IPV6_RECVPKTINFO:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002148 case IPV6_UNICAST_HOPS:
Laurent Vivier21749c42018-06-27 23:21:52 +02002149 case IPV6_MULTICAST_HOPS:
2150 case IPV6_MULTICAST_LOOP:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002151 case IPV6_RECVERR:
2152 case IPV6_RECVHOPLIMIT:
2153 case IPV6_2292HOPLIMIT:
2154 case IPV6_CHECKSUM:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002155 case IPV6_ADDRFORM:
2156 case IPV6_2292PKTINFO:
2157 case IPV6_RECVTCLASS:
2158 case IPV6_RECVRTHDR:
2159 case IPV6_2292RTHDR:
2160 case IPV6_RECVHOPOPTS:
2161 case IPV6_2292HOPOPTS:
2162 case IPV6_RECVDSTOPTS:
2163 case IPV6_2292DSTOPTS:
2164 case IPV6_TCLASS:
Shu-Chun Weng22db1212020-12-18 11:32:12 -08002165 case IPV6_ADDR_PREFERENCES:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002166#ifdef IPV6_RECVPATHMTU
2167 case IPV6_RECVPATHMTU:
2168#endif
2169#ifdef IPV6_TRANSPARENT
2170 case IPV6_TRANSPARENT:
2171#endif
2172#ifdef IPV6_FREEBIND
2173 case IPV6_FREEBIND:
2174#endif
2175#ifdef IPV6_RECVORIGDSTADDR
2176 case IPV6_RECVORIGDSTADDR:
2177#endif
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002178 val = 0;
2179 if (optlen < sizeof(uint32_t)) {
2180 return -TARGET_EINVAL;
2181 }
2182 if (get_user_u32(val, optval_addr)) {
2183 return -TARGET_EFAULT;
2184 }
2185 ret = get_errno(setsockopt(sockfd, level, optname,
2186 &val, sizeof(val)));
2187 break;
Helge Delleree1ac3a2017-02-18 23:31:30 +01002188 case IPV6_PKTINFO:
2189 {
2190 struct in6_pktinfo pki;
2191
2192 if (optlen < sizeof(pki)) {
2193 return -TARGET_EINVAL;
2194 }
2195
2196 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
2197 return -TARGET_EFAULT;
2198 }
2199
2200 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
2201
2202 ret = get_errno(setsockopt(sockfd, level, optname,
2203 &pki, sizeof(pki)));
2204 break;
2205 }
Neng Chen22bf4ee2019-06-19 16:17:10 +02002206 case IPV6_ADD_MEMBERSHIP:
2207 case IPV6_DROP_MEMBERSHIP:
2208 {
2209 struct ipv6_mreq ipv6mreq;
2210
2211 if (optlen < sizeof(ipv6mreq)) {
2212 return -TARGET_EINVAL;
2213 }
2214
2215 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
2216 return -TARGET_EFAULT;
2217 }
2218
2219 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
2220
2221 ret = get_errno(setsockopt(sockfd, level, optname,
2222 &ipv6mreq, sizeof(ipv6mreq)));
2223 break;
2224 }
Helge Delleree1ac3a2017-02-18 23:31:30 +01002225 default:
2226 goto unimplemented;
2227 }
2228 break;
2229 case SOL_ICMPV6:
2230 switch (optname) {
2231 case ICMPV6_FILTER:
2232 {
2233 struct icmp6_filter icmp6f;
2234
2235 if (optlen > sizeof(icmp6f)) {
2236 optlen = sizeof(icmp6f);
2237 }
2238
2239 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
2240 return -TARGET_EFAULT;
2241 }
2242
2243 for (val = 0; val < 8; val++) {
2244 icmp6f.data[val] = tswap32(icmp6f.data[val]);
2245 }
2246
2247 ret = get_errno(setsockopt(sockfd, level, optname,
2248 &icmp6f, optlen));
2249 break;
2250 }
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002251 default:
2252 goto unimplemented;
2253 }
2254 break;
Jing Huang920394d2012-07-24 13:59:23 +00002255 case SOL_RAW:
2256 switch (optname) {
2257 case ICMP_FILTER:
Helge Delleree1ac3a2017-02-18 23:31:30 +01002258 case IPV6_CHECKSUM:
2259 /* those take an u32 value */
Jing Huang920394d2012-07-24 13:59:23 +00002260 if (optlen < sizeof(uint32_t)) {
2261 return -TARGET_EINVAL;
2262 }
2263
2264 if (get_user_u32(val, optval_addr)) {
2265 return -TARGET_EFAULT;
2266 }
2267 ret = get_errno(setsockopt(sockfd, level, optname,
2268 &val, sizeof(val)));
2269 break;
2270
2271 default:
2272 goto unimplemented;
2273 }
2274 break;
Yunqiang Suf31dddd2019-06-19 16:17:11 +02002275#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2276 case SOL_ALG:
2277 switch (optname) {
2278 case ALG_SET_KEY:
2279 {
2280 char *alg_key = g_malloc(optlen);
2281
2282 if (!alg_key) {
2283 return -TARGET_ENOMEM;
2284 }
2285 if (copy_from_user(alg_key, optval_addr, optlen)) {
2286 g_free(alg_key);
2287 return -TARGET_EFAULT;
2288 }
2289 ret = get_errno(setsockopt(sockfd, level, optname,
2290 alg_key, optlen));
2291 g_free(alg_key);
2292 break;
2293 }
2294 case ALG_SET_AEAD_AUTHSIZE:
2295 {
2296 ret = get_errno(setsockopt(sockfd, level, optname,
2297 NULL, optlen));
2298 break;
2299 }
2300 default:
2301 goto unimplemented;
2302 }
2303 break;
2304#endif
bellard3532fa72006-06-24 15:06:03 +00002305 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00002306 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00002307 case TARGET_SO_RCVTIMEO:
2308 {
2309 struct timeval tv;
2310
2311 optname = SO_RCVTIMEO;
2312
2313set_timeout:
2314 if (optlen != sizeof(struct target_timeval)) {
2315 return -TARGET_EINVAL;
2316 }
2317
2318 if (copy_from_user_timeval(&tv, optval_addr)) {
2319 return -TARGET_EFAULT;
2320 }
2321
2322 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2323 &tv, sizeof(tv)));
2324 return ret;
2325 }
2326 case TARGET_SO_SNDTIMEO:
2327 optname = SO_SNDTIMEO;
2328 goto set_timeout;
Laurent Vivierf57d4192013-08-30 01:46:41 +02002329 case TARGET_SO_ATTACH_FILTER:
2330 {
2331 struct target_sock_fprog *tfprog;
2332 struct target_sock_filter *tfilter;
2333 struct sock_fprog fprog;
2334 struct sock_filter *filter;
2335 int i;
2336
2337 if (optlen != sizeof(*tfprog)) {
2338 return -TARGET_EINVAL;
2339 }
2340 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2341 return -TARGET_EFAULT;
2342 }
2343 if (!lock_user_struct(VERIFY_READ, tfilter,
2344 tswapal(tfprog->filter), 0)) {
2345 unlock_user_struct(tfprog, optval_addr, 1);
2346 return -TARGET_EFAULT;
2347 }
2348
2349 fprog.len = tswap16(tfprog->len);
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302350 filter = g_try_new(struct sock_filter, fprog.len);
Laurent Vivierf57d4192013-08-30 01:46:41 +02002351 if (filter == NULL) {
2352 unlock_user_struct(tfilter, tfprog->filter, 1);
2353 unlock_user_struct(tfprog, optval_addr, 1);
2354 return -TARGET_ENOMEM;
2355 }
2356 for (i = 0; i < fprog.len; i++) {
2357 filter[i].code = tswap16(tfilter[i].code);
2358 filter[i].jt = tfilter[i].jt;
2359 filter[i].jf = tfilter[i].jf;
2360 filter[i].k = tswap32(tfilter[i].k);
2361 }
2362 fprog.filter = filter;
2363
2364 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2365 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302366 g_free(filter);
Laurent Vivierf57d4192013-08-30 01:46:41 +02002367
2368 unlock_user_struct(tfilter, tfprog->filter, 1);
2369 unlock_user_struct(tfprog, optval_addr, 1);
2370 return ret;
2371 }
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02002372 case TARGET_SO_BINDTODEVICE:
2373 {
2374 char *dev_ifname, *addr_ifname;
2375
2376 if (optlen > IFNAMSIZ - 1) {
2377 optlen = IFNAMSIZ - 1;
2378 }
2379 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2380 if (!dev_ifname) {
2381 return -TARGET_EFAULT;
2382 }
2383 optname = SO_BINDTODEVICE;
2384 addr_ifname = alloca(IFNAMSIZ);
2385 memcpy(addr_ifname, dev_ifname, optlen);
2386 addr_ifname[optlen] = 0;
Chen Gangfad6c582016-01-11 13:58:50 +08002387 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2388 addr_ifname, optlen));
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02002389 unlock_user (dev_ifname, optval_addr, 0);
2390 return ret;
2391 }
Carlo Marcelo Arenas Belón83eb6e52018-08-24 01:56:01 -07002392 case TARGET_SO_LINGER:
2393 {
2394 struct linger lg;
2395 struct target_linger *tlg;
2396
2397 if (optlen != sizeof(struct target_linger)) {
2398 return -TARGET_EINVAL;
2399 }
2400 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
2401 return -TARGET_EFAULT;
2402 }
2403 __get_user(lg.l_onoff, &tlg->l_onoff);
2404 __get_user(lg.l_linger, &tlg->l_linger);
2405 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
2406 &lg, sizeof(lg)));
2407 unlock_user_struct(tlg, optval_addr, 0);
2408 return ret;
2409 }
bellard8853f862004-02-22 14:57:26 +00002410 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00002411 case TARGET_SO_DEBUG:
2412 optname = SO_DEBUG;
2413 break;
2414 case TARGET_SO_REUSEADDR:
2415 optname = SO_REUSEADDR;
2416 break;
Yunqiang Su113a9dd2018-10-30 13:55:08 +01002417#ifdef SO_REUSEPORT
2418 case TARGET_SO_REUSEPORT:
2419 optname = SO_REUSEPORT;
2420 break;
2421#endif
bellard3532fa72006-06-24 15:06:03 +00002422 case TARGET_SO_TYPE:
2423 optname = SO_TYPE;
2424 break;
2425 case TARGET_SO_ERROR:
2426 optname = SO_ERROR;
2427 break;
2428 case TARGET_SO_DONTROUTE:
2429 optname = SO_DONTROUTE;
2430 break;
2431 case TARGET_SO_BROADCAST:
2432 optname = SO_BROADCAST;
2433 break;
2434 case TARGET_SO_SNDBUF:
2435 optname = SO_SNDBUF;
2436 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01002437 case TARGET_SO_SNDBUFFORCE:
2438 optname = SO_SNDBUFFORCE;
2439 break;
bellard3532fa72006-06-24 15:06:03 +00002440 case TARGET_SO_RCVBUF:
2441 optname = SO_RCVBUF;
2442 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01002443 case TARGET_SO_RCVBUFFORCE:
2444 optname = SO_RCVBUFFORCE;
2445 break;
bellard3532fa72006-06-24 15:06:03 +00002446 case TARGET_SO_KEEPALIVE:
2447 optname = SO_KEEPALIVE;
2448 break;
2449 case TARGET_SO_OOBINLINE:
2450 optname = SO_OOBINLINE;
2451 break;
2452 case TARGET_SO_NO_CHECK:
2453 optname = SO_NO_CHECK;
2454 break;
2455 case TARGET_SO_PRIORITY:
2456 optname = SO_PRIORITY;
2457 break;
bellard5e83e8e2005-03-01 22:32:06 +00002458#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00002459 case TARGET_SO_BSDCOMPAT:
2460 optname = SO_BSDCOMPAT;
2461 break;
bellard5e83e8e2005-03-01 22:32:06 +00002462#endif
bellard3532fa72006-06-24 15:06:03 +00002463 case TARGET_SO_PASSCRED:
2464 optname = SO_PASSCRED;
2465 break;
Paul Burton82d0fe62014-06-22 11:25:36 +01002466 case TARGET_SO_PASSSEC:
2467 optname = SO_PASSSEC;
2468 break;
bellard3532fa72006-06-24 15:06:03 +00002469 case TARGET_SO_TIMESTAMP:
2470 optname = SO_TIMESTAMP;
2471 break;
2472 case TARGET_SO_RCVLOWAT:
2473 optname = SO_RCVLOWAT;
2474 break;
bellard8853f862004-02-22 14:57:26 +00002475 default:
2476 goto unimplemented;
2477 }
bellard3532fa72006-06-24 15:06:03 +00002478 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00002479 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00002480
bellard2f619692007-11-16 10:46:05 +00002481 if (get_user_u32(val, optval_addr))
2482 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002483 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00002484 break;
Josh Kunza2d86682019-10-29 15:43:10 -07002485#ifdef SOL_NETLINK
2486 case SOL_NETLINK:
2487 switch (optname) {
2488 case NETLINK_PKTINFO:
2489 case NETLINK_ADD_MEMBERSHIP:
2490 case NETLINK_DROP_MEMBERSHIP:
2491 case NETLINK_BROADCAST_ERROR:
2492 case NETLINK_NO_ENOBUFS:
2493#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2494 case NETLINK_LISTEN_ALL_NSID:
2495 case NETLINK_CAP_ACK:
2496#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2497#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2498 case NETLINK_EXT_ACK:
2499#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2500#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2501 case NETLINK_GET_STRICT_CHK:
2502#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2503 break;
2504 default:
2505 goto unimplemented;
2506 }
2507 val = 0;
2508 if (optlen < sizeof(uint32_t)) {
2509 return -TARGET_EINVAL;
2510 }
2511 if (get_user_u32(val, optval_addr)) {
2512 return -TARGET_EFAULT;
2513 }
2514 ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
2515 sizeof(val)));
2516 break;
2517#endif /* SOL_NETLINK */
bellard7854b052003-03-29 17:22:23 +00002518 default:
bellard8853f862004-02-22 14:57:26 +00002519 unimplemented:
Josh Kunz39be5352020-02-03 18:54:13 -08002520 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
2521 level, optname);
ths6fa13c12007-12-18 02:41:04 +00002522 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00002523 }
bellard8853f862004-02-22 14:57:26 +00002524 return ret;
bellard7854b052003-03-29 17:22:23 +00002525}
2526
ths0da46a62007-10-20 20:23:07 +00002527/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002528static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00002529 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00002530{
blueswir1992f48a2007-10-14 16:27:31 +00002531 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00002532 int len, val;
2533 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00002534
2535 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00002536 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002537 level = SOL_SOCKET;
2538 switch (optname) {
2539 /* These don't just return a single integer */
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002540 case TARGET_SO_PEERNAME:
2541 goto unimplemented;
Andreas Schwab405dc4c2019-05-13 11:06:26 +02002542 case TARGET_SO_RCVTIMEO: {
2543 struct timeval tv;
2544 socklen_t tvlen;
2545
2546 optname = SO_RCVTIMEO;
2547
2548get_timeout:
2549 if (get_user_u32(len, optlen)) {
2550 return -TARGET_EFAULT;
2551 }
2552 if (len < 0) {
2553 return -TARGET_EINVAL;
2554 }
2555
2556 tvlen = sizeof(tv);
2557 ret = get_errno(getsockopt(sockfd, level, optname,
2558 &tv, &tvlen));
2559 if (ret < 0) {
2560 return ret;
2561 }
2562 if (len > sizeof(struct target_timeval)) {
2563 len = sizeof(struct target_timeval);
2564 }
2565 if (copy_to_user_timeval(optval_addr, &tv)) {
2566 return -TARGET_EFAULT;
2567 }
2568 if (put_user_u32(len, optlen)) {
2569 return -TARGET_EFAULT;
2570 }
2571 break;
2572 }
2573 case TARGET_SO_SNDTIMEO:
2574 optname = SO_SNDTIMEO;
2575 goto get_timeout;
Akos PASZTORY583359a2011-11-14 15:09:49 +02002576 case TARGET_SO_PEERCRED: {
2577 struct ucred cr;
2578 socklen_t crlen;
2579 struct target_ucred *tcr;
2580
2581 if (get_user_u32(len, optlen)) {
2582 return -TARGET_EFAULT;
2583 }
2584 if (len < 0) {
2585 return -TARGET_EINVAL;
2586 }
2587
2588 crlen = sizeof(cr);
2589 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2590 &cr, &crlen));
2591 if (ret < 0) {
2592 return ret;
2593 }
2594 if (len > crlen) {
2595 len = crlen;
2596 }
2597 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2598 return -TARGET_EFAULT;
2599 }
2600 __put_user(cr.pid, &tcr->pid);
2601 __put_user(cr.uid, &tcr->uid);
2602 __put_user(cr.gid, &tcr->gid);
2603 unlock_user_struct(tcr, optval_addr, 1);
2604 if (put_user_u32(len, optlen)) {
2605 return -TARGET_EFAULT;
2606 }
2607 break;
2608 }
Laurent Vivier6d485a52020-02-04 22:19:01 +01002609 case TARGET_SO_PEERSEC: {
2610 char *name;
2611
2612 if (get_user_u32(len, optlen)) {
2613 return -TARGET_EFAULT;
2614 }
2615 if (len < 0) {
2616 return -TARGET_EINVAL;
2617 }
2618 name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
2619 if (!name) {
2620 return -TARGET_EFAULT;
2621 }
2622 lv = len;
2623 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
2624 name, &lv));
2625 if (put_user_u32(lv, optlen)) {
2626 ret = -TARGET_EFAULT;
2627 }
2628 unlock_user(name, optval_addr, lv);
2629 break;
2630 }
Carlo Marcelo Arenas Belón83eb6e52018-08-24 01:56:01 -07002631 case TARGET_SO_LINGER:
2632 {
2633 struct linger lg;
2634 socklen_t lglen;
2635 struct target_linger *tlg;
2636
2637 if (get_user_u32(len, optlen)) {
2638 return -TARGET_EFAULT;
2639 }
2640 if (len < 0) {
2641 return -TARGET_EINVAL;
2642 }
2643
2644 lglen = sizeof(lg);
2645 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2646 &lg, &lglen));
2647 if (ret < 0) {
2648 return ret;
2649 }
2650 if (len > lglen) {
2651 len = lglen;
2652 }
2653 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
2654 return -TARGET_EFAULT;
2655 }
2656 __put_user(lg.l_onoff, &tlg->l_onoff);
2657 __put_user(lg.l_linger, &tlg->l_linger);
2658 unlock_user_struct(tlg, optval_addr, 1);
2659 if (put_user_u32(len, optlen)) {
2660 return -TARGET_EFAULT;
2661 }
2662 break;
2663 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002664 /* Options with 'int' argument. */
2665 case TARGET_SO_DEBUG:
2666 optname = SO_DEBUG;
2667 goto int_case;
2668 case TARGET_SO_REUSEADDR:
2669 optname = SO_REUSEADDR;
2670 goto int_case;
Yunqiang Su113a9dd2018-10-30 13:55:08 +01002671#ifdef SO_REUSEPORT
2672 case TARGET_SO_REUSEPORT:
2673 optname = SO_REUSEPORT;
2674 goto int_case;
2675#endif
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002676 case TARGET_SO_TYPE:
2677 optname = SO_TYPE;
2678 goto int_case;
2679 case TARGET_SO_ERROR:
2680 optname = SO_ERROR;
2681 goto int_case;
2682 case TARGET_SO_DONTROUTE:
2683 optname = SO_DONTROUTE;
2684 goto int_case;
2685 case TARGET_SO_BROADCAST:
2686 optname = SO_BROADCAST;
2687 goto int_case;
2688 case TARGET_SO_SNDBUF:
2689 optname = SO_SNDBUF;
2690 goto int_case;
2691 case TARGET_SO_RCVBUF:
2692 optname = SO_RCVBUF;
2693 goto int_case;
2694 case TARGET_SO_KEEPALIVE:
2695 optname = SO_KEEPALIVE;
2696 goto int_case;
2697 case TARGET_SO_OOBINLINE:
2698 optname = SO_OOBINLINE;
2699 goto int_case;
2700 case TARGET_SO_NO_CHECK:
2701 optname = SO_NO_CHECK;
2702 goto int_case;
2703 case TARGET_SO_PRIORITY:
2704 optname = SO_PRIORITY;
2705 goto int_case;
2706#ifdef SO_BSDCOMPAT
2707 case TARGET_SO_BSDCOMPAT:
2708 optname = SO_BSDCOMPAT;
2709 goto int_case;
2710#endif
2711 case TARGET_SO_PASSCRED:
2712 optname = SO_PASSCRED;
2713 goto int_case;
2714 case TARGET_SO_TIMESTAMP:
2715 optname = SO_TIMESTAMP;
2716 goto int_case;
2717 case TARGET_SO_RCVLOWAT:
2718 optname = SO_RCVLOWAT;
2719 goto int_case;
Paul Burtonaec1ca42014-06-22 11:25:34 +01002720 case TARGET_SO_ACCEPTCONN:
2721 optname = SO_ACCEPTCONN;
2722 goto int_case;
Jason A. Donenfeldec63e062021-02-04 16:39:25 +01002723 case TARGET_SO_PROTOCOL:
2724 optname = SO_PROTOCOL;
2725 goto int_case;
2726 case TARGET_SO_DOMAIN:
2727 optname = SO_DOMAIN;
2728 goto int_case;
bellard8853f862004-02-22 14:57:26 +00002729 default:
bellard2efbe912005-07-23 15:10:20 +00002730 goto int_case;
2731 }
2732 break;
2733 case SOL_TCP:
Shu-Chun Wengfe51b0a2020-12-18 11:32:11 -08002734 case SOL_UDP:
2735 /* TCP and UDP options all take an 'int' value. */
bellard2efbe912005-07-23 15:10:20 +00002736 int_case:
bellard2f619692007-11-16 10:46:05 +00002737 if (get_user_u32(len, optlen))
2738 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002739 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00002740 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05002741 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00002742 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2743 if (ret < 0)
2744 return ret;
Helge Dellercb88b7c2023-01-27 21:25:27 +01002745 switch (optname) {
2746 case SO_TYPE:
Paul Burton8289d112014-06-22 11:25:33 +01002747 val = host_to_target_sock_type(val);
Helge Dellercb88b7c2023-01-27 21:25:27 +01002748 break;
2749 case SO_ERROR:
2750 val = host_to_target_errno(val);
2751 break;
Paul Burton8289d112014-06-22 11:25:33 +01002752 }
bellard2efbe912005-07-23 15:10:20 +00002753 if (len > lv)
2754 len = lv;
bellard2f619692007-11-16 10:46:05 +00002755 if (len == 4) {
2756 if (put_user_u32(val, optval_addr))
2757 return -TARGET_EFAULT;
2758 } else {
2759 if (put_user_u8(val, optval_addr))
2760 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002761 }
bellard2f619692007-11-16 10:46:05 +00002762 if (put_user_u32(len, optlen))
2763 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002764 break;
2765 case SOL_IP:
2766 switch(optname) {
2767 case IP_TOS:
2768 case IP_TTL:
2769 case IP_HDRINCL:
2770 case IP_ROUTER_ALERT:
2771 case IP_RECVOPTS:
2772 case IP_RETOPTS:
2773 case IP_PKTINFO:
2774 case IP_MTU_DISCOVER:
2775 case IP_RECVERR:
2776 case IP_RECVTOS:
2777#ifdef IP_FREEBIND
2778 case IP_FREEBIND:
2779#endif
2780 case IP_MULTICAST_TTL:
2781 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00002782 if (get_user_u32(len, optlen))
2783 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002784 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00002785 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05002786 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00002787 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2788 if (ret < 0)
2789 return ret;
bellard2efbe912005-07-23 15:10:20 +00002790 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00002791 len = 1;
bellard2f619692007-11-16 10:46:05 +00002792 if (put_user_u32(len, optlen)
2793 || put_user_u8(val, optval_addr))
2794 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002795 } else {
bellard2efbe912005-07-23 15:10:20 +00002796 if (len > sizeof(int))
2797 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00002798 if (put_user_u32(len, optlen)
2799 || put_user_u32(val, optval_addr))
2800 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002801 }
bellard8853f862004-02-22 14:57:26 +00002802 break;
bellard2efbe912005-07-23 15:10:20 +00002803 default:
thsc02f4992007-12-18 02:39:59 +00002804 ret = -TARGET_ENOPROTOOPT;
2805 break;
bellard8853f862004-02-22 14:57:26 +00002806 }
2807 break;
Tom Deseynbd8ed482018-12-13 14:06:11 +01002808 case SOL_IPV6:
2809 switch (optname) {
2810 case IPV6_MTU_DISCOVER:
2811 case IPV6_MTU:
2812 case IPV6_V6ONLY:
2813 case IPV6_RECVPKTINFO:
2814 case IPV6_UNICAST_HOPS:
2815 case IPV6_MULTICAST_HOPS:
2816 case IPV6_MULTICAST_LOOP:
2817 case IPV6_RECVERR:
2818 case IPV6_RECVHOPLIMIT:
2819 case IPV6_2292HOPLIMIT:
2820 case IPV6_CHECKSUM:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002821 case IPV6_ADDRFORM:
2822 case IPV6_2292PKTINFO:
2823 case IPV6_RECVTCLASS:
2824 case IPV6_RECVRTHDR:
2825 case IPV6_2292RTHDR:
2826 case IPV6_RECVHOPOPTS:
2827 case IPV6_2292HOPOPTS:
2828 case IPV6_RECVDSTOPTS:
2829 case IPV6_2292DSTOPTS:
2830 case IPV6_TCLASS:
Shu-Chun Weng22db1212020-12-18 11:32:12 -08002831 case IPV6_ADDR_PREFERENCES:
Helge Dellerb9cce6d2019-03-10 12:12:16 +01002832#ifdef IPV6_RECVPATHMTU
2833 case IPV6_RECVPATHMTU:
2834#endif
2835#ifdef IPV6_TRANSPARENT
2836 case IPV6_TRANSPARENT:
2837#endif
2838#ifdef IPV6_FREEBIND
2839 case IPV6_FREEBIND:
2840#endif
2841#ifdef IPV6_RECVORIGDSTADDR
2842 case IPV6_RECVORIGDSTADDR:
2843#endif
Tom Deseynbd8ed482018-12-13 14:06:11 +01002844 if (get_user_u32(len, optlen))
2845 return -TARGET_EFAULT;
2846 if (len < 0)
2847 return -TARGET_EINVAL;
2848 lv = sizeof(lv);
2849 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2850 if (ret < 0)
2851 return ret;
2852 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2853 len = 1;
2854 if (put_user_u32(len, optlen)
2855 || put_user_u8(val, optval_addr))
2856 return -TARGET_EFAULT;
2857 } else {
2858 if (len > sizeof(int))
2859 len = sizeof(int);
2860 if (put_user_u32(len, optlen)
2861 || put_user_u32(val, optval_addr))
2862 return -TARGET_EFAULT;
2863 }
2864 break;
2865 default:
2866 ret = -TARGET_ENOPROTOOPT;
2867 break;
2868 }
2869 break;
Josh Kunza2d86682019-10-29 15:43:10 -07002870#ifdef SOL_NETLINK
2871 case SOL_NETLINK:
2872 switch (optname) {
2873 case NETLINK_PKTINFO:
2874 case NETLINK_BROADCAST_ERROR:
2875 case NETLINK_NO_ENOBUFS:
2876#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2877 case NETLINK_LISTEN_ALL_NSID:
2878 case NETLINK_CAP_ACK:
2879#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2880#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2881 case NETLINK_EXT_ACK:
2882#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2883#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2884 case NETLINK_GET_STRICT_CHK:
2885#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2886 if (get_user_u32(len, optlen)) {
2887 return -TARGET_EFAULT;
2888 }
2889 if (len != sizeof(val)) {
2890 return -TARGET_EINVAL;
2891 }
2892 lv = len;
2893 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2894 if (ret < 0) {
2895 return ret;
2896 }
2897 if (put_user_u32(lv, optlen)
2898 || put_user_u32(val, optval_addr)) {
2899 return -TARGET_EFAULT;
2900 }
2901 break;
2902#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2903 case NETLINK_LIST_MEMBERSHIPS:
2904 {
2905 uint32_t *results;
2906 int i;
2907 if (get_user_u32(len, optlen)) {
2908 return -TARGET_EFAULT;
2909 }
2910 if (len < 0) {
2911 return -TARGET_EINVAL;
2912 }
2913 results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
Frédéric Fortier13e340c2021-03-28 14:01:35 -04002914 if (!results && len > 0) {
Josh Kunza2d86682019-10-29 15:43:10 -07002915 return -TARGET_EFAULT;
2916 }
2917 lv = len;
2918 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
2919 if (ret < 0) {
2920 unlock_user(results, optval_addr, 0);
2921 return ret;
2922 }
Michael Tokarev669dcb62023-08-23 08:53:30 +02002923 /* swap host endianness to target endianness. */
Josh Kunza2d86682019-10-29 15:43:10 -07002924 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
2925 results[i] = tswap32(results[i]);
2926 }
2927 if (put_user_u32(lv, optlen)) {
2928 return -TARGET_EFAULT;
2929 }
2930 unlock_user(results, optval_addr, 0);
2931 break;
2932 }
2933#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2934 default:
2935 goto unimplemented;
2936 }
Laurent Vivierc0cb8802019-11-12 11:50:55 +01002937 break;
Josh Kunza2d86682019-10-29 15:43:10 -07002938#endif /* SOL_NETLINK */
bellard8853f862004-02-22 14:57:26 +00002939 default:
2940 unimplemented:
Josh Kunz39be5352020-02-03 18:54:13 -08002941 qemu_log_mask(LOG_UNIMP,
2942 "getsockopt level=%d optname=%d not yet supported\n",
2943 level, optname);
thsc02f4992007-12-18 02:39:59 +00002944 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00002945 break;
2946 }
2947 return ret;
bellard7854b052003-03-29 17:22:23 +00002948}
2949
Max Filippov9ac22512018-04-04 17:30:41 -07002950/* Convert target low/high pair representing file offset into the host
2951 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2952 * as the kernel doesn't handle them either.
2953 */
2954static void target_to_host_low_high(abi_ulong tlow,
2955 abi_ulong thigh,
2956 unsigned long *hlow,
2957 unsigned long *hhigh)
2958{
2959 uint64_t off = tlow |
2960 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
2961 TARGET_LONG_BITS / 2;
2962
2963 *hlow = off;
2964 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
2965}
2966
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002967static struct iovec *lock_iovec(int type, abi_ulong target_addr,
Peter Maydelldab32b32016-07-15 14:57:26 +01002968 abi_ulong count, int copy)
pbrook53a59602006-03-25 19:31:22 +00002969{
2970 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002971 struct iovec *vec;
2972 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00002973 int i;
Peter Maydell501bb4b2014-02-17 18:55:33 +00002974 int err = 0;
Tom Musta29560a62014-08-12 13:53:43 -05002975 bool bad_address = false;
pbrook53a59602006-03-25 19:31:22 +00002976
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002977 if (count == 0) {
2978 errno = 0;
2979 return NULL;
pbrook53a59602006-03-25 19:31:22 +00002980 }
Peter Maydelldab32b32016-07-15 14:57:26 +01002981 if (count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002982 errno = EINVAL;
2983 return NULL;
2984 }
2985
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302986 vec = g_try_new0(struct iovec, count);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002987 if (vec == NULL) {
2988 errno = ENOMEM;
2989 return NULL;
2990 }
2991
2992 target_vec = lock_user(VERIFY_READ, target_addr,
2993 count * sizeof(struct target_iovec), 1);
2994 if (target_vec == NULL) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00002995 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002996 goto fail2;
2997 }
2998
2999 /* ??? If host page size > target page size, this will result in a
3000 value larger than what we can actually support. */
3001 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3002 total_len = 0;
3003
3004 for (i = 0; i < count; i++) {
3005 abi_ulong base = tswapal(target_vec[i].iov_base);
3006 abi_long len = tswapal(target_vec[i].iov_len);
3007
3008 if (len < 0) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00003009 err = EINVAL;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003010 goto fail;
3011 } else if (len == 0) {
3012 /* Zero length pointer is ignored. */
3013 vec[i].iov_base = 0;
3014 } else {
3015 vec[i].iov_base = lock_user(type, base, len, copy);
Tom Musta29560a62014-08-12 13:53:43 -05003016 /* If the first buffer pointer is bad, this is a fault. But
3017 * subsequent bad buffers will result in a partial write; this
3018 * is realized by filling the vector with null pointers and
3019 * zero lengths. */
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003020 if (!vec[i].iov_base) {
Tom Musta29560a62014-08-12 13:53:43 -05003021 if (i == 0) {
3022 err = EFAULT;
3023 goto fail;
3024 } else {
3025 bad_address = true;
3026 }
3027 }
3028 if (bad_address) {
3029 len = 0;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003030 }
3031 if (len > max_len - total_len) {
3032 len = max_len - total_len;
3033 }
3034 }
3035 vec[i].iov_len = len;
3036 total_len += len;
3037 }
3038
3039 unlock_user(target_vec, target_addr, 0);
3040 return vec;
3041
3042 fail:
Chen Gang S7eff5182015-01-23 18:01:09 +08003043 while (--i >= 0) {
3044 if (tswapal(target_vec[i].iov_len) > 0) {
3045 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3046 }
3047 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003048 unlock_user(target_vec, target_addr, 0);
Peter Maydell501bb4b2014-02-17 18:55:33 +00003049 fail2:
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303050 g_free(vec);
Peter Maydell501bb4b2014-02-17 18:55:33 +00003051 errno = err;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003052 return NULL;
pbrook53a59602006-03-25 19:31:22 +00003053}
3054
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003055static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
Peter Maydelldab32b32016-07-15 14:57:26 +01003056 abi_ulong count, int copy)
pbrook53a59602006-03-25 19:31:22 +00003057{
3058 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00003059 int i;
3060
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003061 target_vec = lock_user(VERIFY_READ, target_addr,
3062 count * sizeof(struct target_iovec), 1);
3063 if (target_vec) {
3064 for (i = 0; i < count; i++) {
3065 abi_ulong base = tswapal(target_vec[i].iov_base);
Chen Gang S71ec7ce2015-01-23 18:07:50 +08003066 abi_long len = tswapal(target_vec[i].iov_len);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003067 if (len < 0) {
3068 break;
3069 }
balrogd732dcb2008-10-28 10:21:03 +00003070 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3071 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003072 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00003073 }
bellard579a97f2007-11-11 14:26:47 +00003074
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303075 g_free(vec);
pbrook53a59602006-03-25 19:31:22 +00003076}
3077
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003078static inline int target_to_host_sock_type(int *type)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003079{
3080 int host_type = 0;
3081 int target_type = *type;
3082
3083 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3084 case TARGET_SOCK_DGRAM:
3085 host_type = SOCK_DGRAM;
3086 break;
3087 case TARGET_SOCK_STREAM:
3088 host_type = SOCK_STREAM;
3089 break;
3090 default:
3091 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3092 break;
3093 }
3094 if (target_type & TARGET_SOCK_CLOEXEC) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003095#if defined(SOCK_CLOEXEC)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003096 host_type |= SOCK_CLOEXEC;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003097#else
3098 return -TARGET_EINVAL;
3099#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003100 }
3101 if (target_type & TARGET_SOCK_NONBLOCK) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003102#if defined(SOCK_NONBLOCK)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003103 host_type |= SOCK_NONBLOCK;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003104#elif !defined(O_NONBLOCK)
3105 return -TARGET_EINVAL;
3106#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003107 }
3108 *type = host_type;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003109 return 0;
3110}
3111
3112/* Try to emulate socket type flags after socket creation. */
3113static int sock_flags_fixup(int fd, int target_type)
3114{
3115#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3116 if (target_type & TARGET_SOCK_NONBLOCK) {
3117 int flags = fcntl(fd, F_GETFL);
3118 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3119 close(fd);
3120 return -TARGET_EINVAL;
3121 }
3122 }
3123#endif
3124 return fd;
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003125}
3126
ths0da46a62007-10-20 20:23:07 +00003127/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003128static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00003129{
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003130 int target_type = type;
3131 int ret;
3132
3133 ret = target_to_host_sock_type(&type);
3134 if (ret) {
3135 return ret;
3136 }
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003137
Laurent Vivier575b22b2016-06-02 22:14:15 +02003138 if (domain == PF_NETLINK && !(
3139#ifdef CONFIG_RTNETLINK
3140 protocol == NETLINK_ROUTE ||
3141#endif
3142 protocol == NETLINK_KOBJECT_UEVENT ||
3143 protocol == NETLINK_AUDIT)) {
Josh Kunz71e24432020-07-06 17:10:36 -07003144 return -TARGET_EPROTONOSUPPORT;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003145 }
Laurent Vivierff626f22015-10-28 21:40:42 +01003146
3147 if (domain == AF_PACKET ||
3148 (domain == AF_INET && type == SOCK_PACKET)) {
3149 protocol = tswap16(protocol);
3150 }
3151
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003152 ret = get_errno(socket(domain, type, protocol));
3153 if (ret >= 0) {
3154 ret = sock_flags_fixup(ret, target_type);
Laurent Vivier0cf22722015-10-28 21:40:45 +01003155 if (type == SOCK_PACKET) {
3156 /* Manage an obsolete case :
3157 * if socket type is SOCK_PACKET, bind by name
3158 */
3159 fd_trans_register(ret, &target_packet_trans);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003160 } else if (domain == PF_NETLINK) {
3161 switch (protocol) {
Laurent Vivier575b22b2016-06-02 22:14:15 +02003162#ifdef CONFIG_RTNETLINK
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003163 case NETLINK_ROUTE:
3164 fd_trans_register(ret, &target_netlink_route_trans);
3165 break;
Laurent Vivier575b22b2016-06-02 22:14:15 +02003166#endif
Laurent Vivierb2656202016-05-22 18:56:20 +02003167 case NETLINK_KOBJECT_UEVENT:
3168 /* nothing to do: messages are strings */
3169 break;
Laurent Vivier5ce9bb52016-05-22 18:56:21 +02003170 case NETLINK_AUDIT:
3171 fd_trans_register(ret, &target_netlink_audit_trans);
3172 break;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003173 default:
3174 g_assert_not_reached();
3175 }
Laurent Vivier0cf22722015-10-28 21:40:45 +01003176 }
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02003177 }
3178 return ret;
bellard3532fa72006-06-24 15:06:03 +00003179}
3180
ths0da46a62007-10-20 20:23:07 +00003181/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003182static abi_long do_bind(int sockfd, abi_ulong target_addr,
3183 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00003184{
aurel328f7aeaf2009-01-30 19:47:57 +00003185 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03003186 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00003187
Blue Swirl38724252010-09-18 05:53:14 +00003188 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003189 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003190 }
aurel328f7aeaf2009-01-30 19:47:57 +00003191
aurel32607175e2009-04-15 16:11:59 +00003192 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00003193
Laurent Vivier7b36f782015-10-28 21:40:44 +01003194 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003195 if (ret)
3196 return ret;
3197
bellard3532fa72006-06-24 15:06:03 +00003198 return get_errno(bind(sockfd, addr, addrlen));
3199}
3200
ths0da46a62007-10-20 20:23:07 +00003201/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003202static abi_long do_connect(int sockfd, abi_ulong target_addr,
3203 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00003204{
aurel328f7aeaf2009-01-30 19:47:57 +00003205 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03003206 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00003207
Blue Swirl38724252010-09-18 05:53:14 +00003208 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003209 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003210 }
aurel328f7aeaf2009-01-30 19:47:57 +00003211
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003212 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00003213
Laurent Vivier7b36f782015-10-28 21:40:44 +01003214 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003215 if (ret)
3216 return ret;
3217
Peter Maydell2a3c7612016-06-06 19:58:03 +01003218 return get_errno(safe_connect(sockfd, addr, addrlen));
bellard3532fa72006-06-24 15:06:03 +00003219}
3220
Alexander Graff19e00d2014-03-02 19:36:42 +00003221/* do_sendrecvmsg_locked() Must return target values and target errnos. */
3222static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3223 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00003224{
balrog6de645c2008-10-28 10:26:29 +00003225 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00003226 struct msghdr msg;
Peter Maydelldab32b32016-07-15 14:57:26 +01003227 abi_ulong count;
bellard3532fa72006-06-24 15:06:03 +00003228 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00003229 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00003230
bellard3532fa72006-06-24 15:06:03 +00003231 if (msgp->msg_name) {
3232 msg.msg_namelen = tswap32(msgp->msg_namelen);
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003233 msg.msg_name = alloca(msg.msg_namelen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01003234 ret = target_to_host_sockaddr(fd, msg.msg_name,
3235 tswapal(msgp->msg_name),
3236 msg.msg_namelen);
Peter Maydell26a6fc92016-07-15 14:57:28 +01003237 if (ret == -TARGET_EFAULT) {
3238 /* For connected sockets msg_name and msg_namelen must
3239 * be ignored, so returning EFAULT immediately is wrong.
3240 * Instead, pass a bad msg_name to the host kernel, and
3241 * let it decide whether to return EFAULT or not.
3242 */
3243 msg.msg_name = (void *)-1;
3244 } else if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003245 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03003246 }
bellard3532fa72006-06-24 15:06:03 +00003247 } else {
3248 msg.msg_name = NULL;
3249 msg.msg_namelen = 0;
3250 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003251 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00003252 msg.msg_control = alloca(msg.msg_controllen);
Jonas Schievink1d3d1b22018-07-12 00:12:44 +02003253 memset(msg.msg_control, 0, msg.msg_controllen);
3254
bellard3532fa72006-06-24 15:06:03 +00003255 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00003256
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003257 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003258 target_vec = tswapal(msgp->msg_iov);
Peter Maydell97b07972016-07-15 14:57:27 +01003259
3260 if (count > IOV_MAX) {
3261 /* sendrcvmsg returns a different errno for this condition than
3262 * readv/writev, so we must catch it here before lock_iovec() does.
3263 */
3264 ret = -TARGET_EMSGSIZE;
3265 goto out2;
3266 }
3267
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003268 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3269 target_vec, count, send);
3270 if (vec == NULL) {
3271 ret = -host_to_target_errno(errno);
Helge Deller3f0744f2022-12-12 18:34:16 +01003272 /* allow sending packet without any iov, e.g. with MSG_MORE flag */
3273 if (!send || ret) {
3274 goto out2;
3275 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003276 }
bellard3532fa72006-06-24 15:06:03 +00003277 msg.msg_iovlen = count;
3278 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00003279
bellard3532fa72006-06-24 15:06:03 +00003280 if (send) {
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003281 if (fd_trans_target_to_host_data(fd)) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003282 void *host_msg;
3283
3284 host_msg = g_malloc(msg.msg_iov->iov_len);
3285 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3286 ret = fd_trans_target_to_host_data(fd)(host_msg,
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003287 msg.msg_iov->iov_len);
Laurent Vivier7d61d892016-06-21 19:51:14 +02003288 if (ret >= 0) {
3289 msg.msg_iov->iov_base = host_msg;
3290 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3291 }
3292 g_free(host_msg);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003293 } else {
3294 ret = target_to_host_cmsg(&msg, msgp);
Laurent Vivier7d61d892016-06-21 19:51:14 +02003295 if (ret == 0) {
3296 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3297 }
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003298 }
bellard3532fa72006-06-24 15:06:03 +00003299 } else {
Peter Maydell66687532016-06-06 19:58:04 +01003300 ret = get_errno(safe_recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00003301 if (!is_error(ret)) {
3302 len = ret;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003303 if (fd_trans_host_to_target_data(fd)) {
3304 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
Laurent Vivier2a03d3e2018-08-20 19:15:54 +02003305 MIN(msg.msg_iov->iov_len, len));
Icenowy Zheng16c81dd2022-10-28 16:12:20 +08003306 }
3307 if (!is_error(ret)) {
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003308 ret = host_to_target_cmsg(msgp, &msg);
3309 }
Jing Huangca619062012-07-24 13:58:02 +00003310 if (!is_error(ret)) {
3311 msgp->msg_namelen = tswap32(msg.msg_namelen);
Andreas Schwab24894f32019-02-12 17:34:35 +01003312 msgp->msg_flags = tswap32(msg.msg_flags);
Peter Maydell26a6fc92016-07-15 14:57:28 +01003313 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
Jing Huangca619062012-07-24 13:58:02 +00003314 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3315 msg.msg_name, msg.msg_namelen);
3316 if (ret) {
3317 goto out;
3318 }
3319 }
3320
balrog6de645c2008-10-28 10:26:29 +00003321 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00003322 }
balrog6de645c2008-10-28 10:26:29 +00003323 }
bellard3532fa72006-06-24 15:06:03 +00003324 }
Jing Huangca619062012-07-24 13:58:02 +00003325
3326out:
Helge Deller3f0744f2022-12-12 18:34:16 +01003327 if (vec) {
3328 unlock_iovec(vec, target_vec, count, !send);
3329 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07003330out2:
Alexander Graff19e00d2014-03-02 19:36:42 +00003331 return ret;
3332}
3333
3334static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3335 int flags, int send)
3336{
3337 abi_long ret;
3338 struct target_msghdr *msgp;
3339
3340 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3341 msgp,
3342 target_msg,
3343 send ? 1 : 0)) {
3344 return -TARGET_EFAULT;
3345 }
3346 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
bellard579a97f2007-11-11 14:26:47 +00003347 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00003348 return ret;
3349}
3350
Alexander Graff19e00d2014-03-02 19:36:42 +00003351/* We don't rely on the C library to have sendmmsg/recvmmsg support,
3352 * so it might not have this *mmsg-specific flag either.
3353 */
3354#ifndef MSG_WAITFORONE
3355#define MSG_WAITFORONE 0x10000
3356#endif
3357
3358static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3359 unsigned int vlen, unsigned int flags,
3360 int send)
3361{
3362 struct target_mmsghdr *mmsgp;
3363 abi_long ret = 0;
3364 int i;
3365
3366 if (vlen > UIO_MAXIOV) {
3367 vlen = UIO_MAXIOV;
3368 }
3369
3370 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3371 if (!mmsgp) {
3372 return -TARGET_EFAULT;
3373 }
3374
3375 for (i = 0; i < vlen; i++) {
3376 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3377 if (is_error(ret)) {
3378 break;
3379 }
3380 mmsgp[i].msg_len = tswap32(ret);
3381 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3382 if (flags & MSG_WAITFORONE) {
3383 flags |= MSG_DONTWAIT;
3384 }
3385 }
3386
3387 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3388
3389 /* Return number of datagrams sent if we sent any at all;
3390 * otherwise return the error.
3391 */
3392 if (i) {
3393 return i;
3394 }
3395 return ret;
3396}
Alexander Graff19e00d2014-03-02 19:36:42 +00003397
Peter Maydella94b4982013-02-08 04:35:04 +00003398/* do_accept4() Must return target values and target errnos. */
3399static abi_long do_accept4(int fd, abi_ulong target_addr,
3400 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00003401{
Andreas Schwabcd813362019-02-14 12:43:40 +01003402 socklen_t addrlen, ret_addrlen;
bellard2f619692007-11-16 10:46:05 +00003403 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003404 abi_long ret;
Petar Jovanovicd25295d2014-03-31 17:41:23 +02003405 int host_flags;
3406
Helge Dellerdca4c832023-07-08 07:24:00 +02003407 if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
3408 return -TARGET_EINVAL;
3409 }
3410
3411 host_flags = 0;
3412 if (flags & TARGET_SOCK_NONBLOCK) {
3413 host_flags |= SOCK_NONBLOCK;
3414 }
3415 if (flags & TARGET_SOCK_CLOEXEC) {
3416 host_flags |= SOCK_CLOEXEC;
3417 }
pbrook1be9e1d2006-11-19 15:26:04 +00003418
Peter Maydella94b4982013-02-08 04:35:04 +00003419 if (target_addr == 0) {
Peter Maydellff6dc132016-06-06 19:58:13 +01003420 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
Peter Maydella94b4982013-02-08 04:35:04 +00003421 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003422
Matus Kysele554eb42020-09-30 17:16:16 +02003423 /* linux returns EFAULT if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00003424 if (get_user_u32(addrlen, target_addrlen_addr))
Matus Kysele554eb42020-09-30 17:16:16 +02003425 return -TARGET_EFAULT;
bellard2f619692007-11-16 10:46:05 +00003426
Blue Swirl38724252010-09-18 05:53:14 +00003427 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003428 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003429 }
aurel328f7aeaf2009-01-30 19:47:57 +00003430
Richard Hendersonc7169b02021-02-12 10:48:47 -08003431 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
Matus Kysele554eb42020-09-30 17:16:16 +02003432 return -TARGET_EFAULT;
Richard Hendersonc7169b02021-02-12 10:48:47 -08003433 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003434
bellard2f619692007-11-16 10:46:05 +00003435 addr = alloca(addrlen);
3436
Andreas Schwabcd813362019-02-14 12:43:40 +01003437 ret_addrlen = addrlen;
3438 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
pbrook1be9e1d2006-11-19 15:26:04 +00003439 if (!is_error(ret)) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003440 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3441 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
bellard2f619692007-11-16 10:46:05 +00003442 ret = -TARGET_EFAULT;
Andreas Schwabcd813362019-02-14 12:43:40 +01003443 }
pbrook1be9e1d2006-11-19 15:26:04 +00003444 }
3445 return ret;
3446}
3447
ths0da46a62007-10-20 20:23:07 +00003448/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003449static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00003450 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003451{
Andreas Schwabcd813362019-02-14 12:43:40 +01003452 socklen_t addrlen, ret_addrlen;
bellard2f619692007-11-16 10:46:05 +00003453 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003454 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003455
bellard2f619692007-11-16 10:46:05 +00003456 if (get_user_u32(addrlen, target_addrlen_addr))
3457 return -TARGET_EFAULT;
3458
Blue Swirl38724252010-09-18 05:53:14 +00003459 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003460 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003461 }
aurel328f7aeaf2009-01-30 19:47:57 +00003462
Richard Hendersonc7169b02021-02-12 10:48:47 -08003463 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
Arnaud Patard917507b2009-06-19 10:44:45 +03003464 return -TARGET_EFAULT;
Richard Hendersonc7169b02021-02-12 10:48:47 -08003465 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003466
bellard2f619692007-11-16 10:46:05 +00003467 addr = alloca(addrlen);
3468
Andreas Schwabcd813362019-02-14 12:43:40 +01003469 ret_addrlen = addrlen;
3470 ret = get_errno(getpeername(fd, addr, &ret_addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003471 if (!is_error(ret)) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003472 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3473 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
bellard2f619692007-11-16 10:46:05 +00003474 ret = -TARGET_EFAULT;
Andreas Schwabcd813362019-02-14 12:43:40 +01003475 }
pbrook1be9e1d2006-11-19 15:26:04 +00003476 }
3477 return ret;
3478}
3479
ths0da46a62007-10-20 20:23:07 +00003480/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003481static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00003482 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003483{
Andreas Schwabcd813362019-02-14 12:43:40 +01003484 socklen_t addrlen, ret_addrlen;
bellard2f619692007-11-16 10:46:05 +00003485 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003486 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003487
bellard2f619692007-11-16 10:46:05 +00003488 if (get_user_u32(addrlen, target_addrlen_addr))
3489 return -TARGET_EFAULT;
3490
Blue Swirl38724252010-09-18 05:53:14 +00003491 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003492 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003493 }
aurel328f7aeaf2009-01-30 19:47:57 +00003494
Richard Hendersonc7169b02021-02-12 10:48:47 -08003495 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
Arnaud Patard917507b2009-06-19 10:44:45 +03003496 return -TARGET_EFAULT;
Richard Hendersonc7169b02021-02-12 10:48:47 -08003497 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003498
bellard2f619692007-11-16 10:46:05 +00003499 addr = alloca(addrlen);
3500
Andreas Schwabcd813362019-02-14 12:43:40 +01003501 ret_addrlen = addrlen;
3502 ret = get_errno(getsockname(fd, addr, &ret_addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003503 if (!is_error(ret)) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003504 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3505 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
bellard2f619692007-11-16 10:46:05 +00003506 ret = -TARGET_EFAULT;
Andreas Schwabcd813362019-02-14 12:43:40 +01003507 }
pbrook1be9e1d2006-11-19 15:26:04 +00003508 }
3509 return ret;
3510}
3511
ths0da46a62007-10-20 20:23:07 +00003512/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003513static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00003514 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003515{
3516 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00003517 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003518
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003519 target_to_host_sock_type(&type);
3520
pbrook1be9e1d2006-11-19 15:26:04 +00003521 ret = get_errno(socketpair(domain, type, protocol, tab));
3522 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00003523 if (put_user_s32(tab[0], target_tab_addr)
3524 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3525 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003526 }
3527 return ret;
3528}
3529
ths0da46a62007-10-20 20:23:07 +00003530/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003531static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3532 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00003533{
3534 void *addr;
3535 void *host_msg;
Laurent Vivier7d61d892016-06-21 19:51:14 +02003536 void *copy_msg = NULL;
blueswir1992f48a2007-10-14 16:27:31 +00003537 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003538
Blue Swirl38724252010-09-18 05:53:14 +00003539 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003540 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003541 }
aurel328f7aeaf2009-01-30 19:47:57 +00003542
bellard579a97f2007-11-11 14:26:47 +00003543 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3544 if (!host_msg)
3545 return -TARGET_EFAULT;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003546 if (fd_trans_target_to_host_data(fd)) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003547 copy_msg = host_msg;
3548 host_msg = g_malloc(len);
3549 memcpy(host_msg, copy_msg, len);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003550 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3551 if (ret < 0) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003552 goto fail;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003553 }
3554 }
pbrook1be9e1d2006-11-19 15:26:04 +00003555 if (target_addr) {
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003556 addr = alloca(addrlen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01003557 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003558 if (ret) {
Laurent Vivier7d61d892016-06-21 19:51:14 +02003559 goto fail;
Arnaud Patard917507b2009-06-19 10:44:45 +03003560 }
Peter Maydell66687532016-06-06 19:58:04 +01003561 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003562 } else {
Peter Maydell66687532016-06-06 19:58:04 +01003563 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
pbrook1be9e1d2006-11-19 15:26:04 +00003564 }
Laurent Vivier7d61d892016-06-21 19:51:14 +02003565fail:
3566 if (copy_msg) {
3567 g_free(host_msg);
3568 host_msg = copy_msg;
3569 }
pbrook1be9e1d2006-11-19 15:26:04 +00003570 unlock_user(host_msg, msg, 0);
3571 return ret;
3572}
3573
ths0da46a62007-10-20 20:23:07 +00003574/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003575static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3576 abi_ulong target_addr,
3577 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00003578{
Andreas Schwabcd813362019-02-14 12:43:40 +01003579 socklen_t addrlen, ret_addrlen;
pbrook1be9e1d2006-11-19 15:26:04 +00003580 void *addr;
3581 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00003582 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003583
Zach Reizner4a1e6bc2021-03-26 22:11:16 -04003584 if (!msg) {
3585 host_msg = NULL;
3586 } else {
3587 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3588 if (!host_msg) {
3589 return -TARGET_EFAULT;
3590 }
3591 }
pbrook1be9e1d2006-11-19 15:26:04 +00003592 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00003593 if (get_user_u32(addrlen, target_addrlen)) {
3594 ret = -TARGET_EFAULT;
3595 goto fail;
3596 }
Blue Swirl38724252010-09-18 05:53:14 +00003597 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003598 ret = -TARGET_EINVAL;
3599 goto fail;
3600 }
pbrook1be9e1d2006-11-19 15:26:04 +00003601 addr = alloca(addrlen);
Andreas Schwabcd813362019-02-14 12:43:40 +01003602 ret_addrlen = addrlen;
Peter Maydell66687532016-06-06 19:58:04 +01003603 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
Andreas Schwabcd813362019-02-14 12:43:40 +01003604 addr, &ret_addrlen));
pbrook1be9e1d2006-11-19 15:26:04 +00003605 } else {
3606 addr = NULL; /* To keep compiler quiet. */
Andreas Schwabcd813362019-02-14 12:43:40 +01003607 addrlen = 0; /* To keep compiler quiet. */
Peter Maydell66687532016-06-06 19:58:04 +01003608 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
pbrook1be9e1d2006-11-19 15:26:04 +00003609 }
3610 if (!is_error(ret)) {
Laurent Vivierc35e1f92016-06-21 19:51:15 +02003611 if (fd_trans_host_to_target_data(fd)) {
Laurent Vivier2a03d3e2018-08-20 19:15:54 +02003612 abi_long trans;
3613 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3614 if (is_error(trans)) {
3615 ret = trans;
3616 goto fail;
3617 }
Laurent Vivierc35e1f92016-06-21 19:51:15 +02003618 }
pbrook1be9e1d2006-11-19 15:26:04 +00003619 if (target_addr) {
Andreas Schwabcd813362019-02-14 12:43:40 +01003620 host_to_target_sockaddr(target_addr, addr,
3621 MIN(addrlen, ret_addrlen));
3622 if (put_user_u32(ret_addrlen, target_addrlen)) {
bellard2f619692007-11-16 10:46:05 +00003623 ret = -TARGET_EFAULT;
3624 goto fail;
3625 }
pbrook1be9e1d2006-11-19 15:26:04 +00003626 }
3627 unlock_user(host_msg, msg, len);
3628 } else {
bellard2f619692007-11-16 10:46:05 +00003629fail:
pbrook1be9e1d2006-11-19 15:26:04 +00003630 unlock_user(host_msg, msg, 0);
3631 }
3632 return ret;
3633}
3634
j_mayer32407102007-09-26 23:01:49 +00003635#ifdef TARGET_NR_socketcall
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003636/* do_socketcall() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003637static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00003638{
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003639 static const unsigned nargs[] = { /* number of arguments per operation */
3640 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3641 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3642 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3643 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3644 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3645 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3646 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3647 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3648 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3649 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3650 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3651 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3652 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3653 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3654 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3655 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3656 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3657 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3658 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3659 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003660 };
3661 abi_long a[6]; /* max 6 args */
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003662 unsigned i;
bellard31e31b82003-02-18 22:55:36 +00003663
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003664 /* check the range of the first argument num */
3665 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3666 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3667 return -TARGET_EINVAL;
3668 }
3669 /* ensure we have space for args */
3670 if (nargs[num] > ARRAY_SIZE(a)) {
3671 return -TARGET_EINVAL;
3672 }
3673 /* collect the arguments in a[] according to nargs[] */
3674 for (i = 0; i < nargs[num]; ++i) {
3675 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3676 return -TARGET_EFAULT;
André Hentschelb9d36eb2014-01-06 20:18:52 +01003677 }
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003678 }
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003679 /* now when we have the args, invoke the appropriate underlying function */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003680 switch (num) {
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003681 case TARGET_SYS_SOCKET: /* domain, type, protocol */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003682 return do_socket(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003683 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003684 return do_bind(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003685 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003686 return do_connect(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003687 case TARGET_SYS_LISTEN: /* sockfd, backlog */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003688 return get_errno(listen(a[0], a[1]));
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003689 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003690 return do_accept4(a[0], a[1], a[2], 0);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003691 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003692 return do_getsockname(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003693 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003694 return do_getpeername(a[0], a[1], a[2]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003695 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003696 return do_socketpair(a[0], a[1], a[2], a[3]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003697 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003698 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003699 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003700 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003701 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003702 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003703 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003704 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003705 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003706 return get_errno(shutdown(a[0], a[1]));
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003707 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003708 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003709 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003710 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003711 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
3712 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3713 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
3714 return do_sendrecvmsg(a[0], a[1], a[2], 0);
3715 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3716 return do_accept4(a[0], a[1], a[2], a[3]);
3717 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
3718 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
3719 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3720 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
bellard31e31b82003-02-18 22:55:36 +00003721 default:
Josh Kunz39be5352020-02-03 18:54:13 -08003722 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
Aleksandar Markovicff71a452016-09-22 18:56:57 +02003723 return -TARGET_EINVAL;
bellard31e31b82003-02-18 22:55:36 +00003724 }
bellard31e31b82003-02-18 22:55:36 +00003725}
j_mayer32407102007-09-26 23:01:49 +00003726#endif
bellard31e31b82003-02-18 22:55:36 +00003727
Peter Maydell005eb2a2016-07-15 16:50:47 +01003728#ifndef TARGET_SEMID64_DS
3729/* asm-generic version of this struct */
3730struct target_semid64_ds
ths3eb6b042007-06-03 14:26:27 +00003731{
3732 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00003733 abi_ulong sem_otime;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003734#if TARGET_ABI_BITS == 32
blueswir1992f48a2007-10-14 16:27:31 +00003735 abi_ulong __unused1;
Tom Musta03527342014-08-12 13:53:32 -05003736#endif
blueswir1992f48a2007-10-14 16:27:31 +00003737 abi_ulong sem_ctime;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003738#if TARGET_ABI_BITS == 32
blueswir1992f48a2007-10-14 16:27:31 +00003739 abi_ulong __unused2;
Tom Musta03527342014-08-12 13:53:32 -05003740#endif
blueswir1992f48a2007-10-14 16:27:31 +00003741 abi_ulong sem_nsems;
3742 abi_ulong __unused3;
3743 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00003744};
Peter Maydell005eb2a2016-07-15 16:50:47 +01003745#endif
ths3eb6b042007-06-03 14:26:27 +00003746
bellard579a97f2007-11-11 14:26:47 +00003747static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3748 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003749{
3750 struct target_ipc_perm *target_ip;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003751 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003752
bellard579a97f2007-11-11 14:26:47 +00003753 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3754 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00003755 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003756 host_ip->__key = tswap32(target_ip->__key);
3757 host_ip->uid = tswap32(target_ip->uid);
3758 host_ip->gid = tswap32(target_ip->gid);
3759 host_ip->cuid = tswap32(target_ip->cuid);
3760 host_ip->cgid = tswap32(target_ip->cgid);
3761#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3762 host_ip->mode = tswap32(target_ip->mode);
3763#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003764 host_ip->mode = tswap16(target_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003765#endif
3766#if defined(TARGET_PPC)
3767 host_ip->__seq = tswap32(target_ip->__seq);
3768#else
3769 host_ip->__seq = tswap16(target_ip->__seq);
3770#endif
ths3eb6b042007-06-03 14:26:27 +00003771 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003772 return 0;
ths3eb6b042007-06-03 14:26:27 +00003773}
3774
bellard579a97f2007-11-11 14:26:47 +00003775static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3776 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00003777{
3778 struct target_ipc_perm *target_ip;
Peter Maydell005eb2a2016-07-15 16:50:47 +01003779 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003780
bellard579a97f2007-11-11 14:26:47 +00003781 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3782 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00003783 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003784 target_ip->__key = tswap32(host_ip->__key);
3785 target_ip->uid = tswap32(host_ip->uid);
3786 target_ip->gid = tswap32(host_ip->gid);
3787 target_ip->cuid = tswap32(host_ip->cuid);
3788 target_ip->cgid = tswap32(host_ip->cgid);
3789#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3790 target_ip->mode = tswap32(host_ip->mode);
3791#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003792 target_ip->mode = tswap16(host_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003793#endif
3794#if defined(TARGET_PPC)
3795 target_ip->__seq = tswap32(host_ip->__seq);
3796#else
3797 target_ip->__seq = tswap16(host_ip->__seq);
3798#endif
ths3eb6b042007-06-03 14:26:27 +00003799 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003800 return 0;
ths3eb6b042007-06-03 14:26:27 +00003801}
3802
bellard579a97f2007-11-11 14:26:47 +00003803static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3804 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003805{
Peter Maydell005eb2a2016-07-15 16:50:47 +01003806 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003807
bellard579a97f2007-11-11 14:26:47 +00003808 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3809 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00003810 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3811 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003812 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3813 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3814 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00003815 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003816 return 0;
ths3eb6b042007-06-03 14:26:27 +00003817}
3818
bellard579a97f2007-11-11 14:26:47 +00003819static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3820 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00003821{
Peter Maydell005eb2a2016-07-15 16:50:47 +01003822 struct target_semid64_ds *target_sd;
ths3eb6b042007-06-03 14:26:27 +00003823
bellard579a97f2007-11-11 14:26:47 +00003824 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3825 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00003826 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08003827 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003828 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3829 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3830 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00003831 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003832 return 0;
ths3eb6b042007-06-03 14:26:27 +00003833}
3834
aurel32e5289082009-04-18 16:16:12 +00003835struct target_seminfo {
3836 int semmap;
3837 int semmni;
3838 int semmns;
3839 int semmnu;
3840 int semmsl;
3841 int semopm;
3842 int semume;
3843 int semusz;
3844 int semvmx;
3845 int semaem;
3846};
3847
3848static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3849 struct seminfo *host_seminfo)
3850{
3851 struct target_seminfo *target_seminfo;
3852 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3853 return -TARGET_EFAULT;
3854 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3855 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3856 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3857 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3858 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3859 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3860 __put_user(host_seminfo->semume, &target_seminfo->semume);
3861 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3862 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3863 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3864 unlock_user_struct(target_seminfo, target_addr, 1);
3865 return 0;
3866}
3867
thsfa294812007-02-02 22:05:00 +00003868union semun {
3869 int val;
ths3eb6b042007-06-03 14:26:27 +00003870 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00003871 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00003872 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00003873};
3874
ths3eb6b042007-06-03 14:26:27 +00003875union target_semun {
3876 int val;
aurel32e5289082009-04-18 16:16:12 +00003877 abi_ulong buf;
3878 abi_ulong array;
3879 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00003880};
3881
aurel32e5289082009-04-18 16:16:12 +00003882static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3883 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003884{
aurel32e5289082009-04-18 16:16:12 +00003885 int nsems;
3886 unsigned short *array;
3887 union semun semun;
3888 struct semid_ds semid_ds;
3889 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00003890
aurel32e5289082009-04-18 16:16:12 +00003891 semun.buf = &semid_ds;
3892
3893 ret = semctl(semid, 0, IPC_STAT, semun);
3894 if (ret == -1)
3895 return get_errno(ret);
3896
3897 nsems = semid_ds.sem_nsems;
3898
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303899 *host_array = g_try_new(unsigned short, nsems);
Peter Maydell69d4c702014-02-17 18:55:34 +00003900 if (!*host_array) {
3901 return -TARGET_ENOMEM;
3902 }
aurel32e5289082009-04-18 16:16:12 +00003903 array = lock_user(VERIFY_READ, target_addr,
3904 nsems*sizeof(unsigned short), 1);
Peter Maydell69d4c702014-02-17 18:55:34 +00003905 if (!array) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303906 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00003907 return -TARGET_EFAULT;
Peter Maydell69d4c702014-02-17 18:55:34 +00003908 }
aurel32e5289082009-04-18 16:16:12 +00003909
3910 for(i=0; i<nsems; i++) {
3911 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00003912 }
aurel32e5289082009-04-18 16:16:12 +00003913 unlock_user(array, target_addr, 0);
3914
bellard579a97f2007-11-11 14:26:47 +00003915 return 0;
ths3eb6b042007-06-03 14:26:27 +00003916}
3917
aurel32e5289082009-04-18 16:16:12 +00003918static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3919 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00003920{
aurel32e5289082009-04-18 16:16:12 +00003921 int nsems;
3922 unsigned short *array;
3923 union semun semun;
3924 struct semid_ds semid_ds;
3925 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00003926
aurel32e5289082009-04-18 16:16:12 +00003927 semun.buf = &semid_ds;
3928
3929 ret = semctl(semid, 0, IPC_STAT, semun);
3930 if (ret == -1)
3931 return get_errno(ret);
3932
3933 nsems = semid_ds.sem_nsems;
3934
3935 array = lock_user(VERIFY_WRITE, target_addr,
3936 nsems*sizeof(unsigned short), 0);
3937 if (!array)
3938 return -TARGET_EFAULT;
3939
3940 for(i=0; i<nsems; i++) {
3941 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00003942 }
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303943 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00003944 unlock_user(array, target_addr, 1);
3945
bellard579a97f2007-11-11 14:26:47 +00003946 return 0;
ths3eb6b042007-06-03 14:26:27 +00003947}
3948
aurel32e5289082009-04-18 16:16:12 +00003949static inline abi_long do_semctl(int semid, int semnum, int cmd,
Stefan Weild1c002b2015-02-08 15:40:58 +01003950 abi_ulong target_arg)
ths3eb6b042007-06-03 14:26:27 +00003951{
Stefan Weild1c002b2015-02-08 15:40:58 +01003952 union target_semun target_su = { .buf = target_arg };
ths3eb6b042007-06-03 14:26:27 +00003953 union semun arg;
3954 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05303955 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00003956 struct seminfo seminfo;
3957 abi_long ret = -TARGET_EINVAL;
3958 abi_long err;
3959 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00003960
3961 switch( cmd ) {
3962 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00003963 case SETVAL:
Tom Musta5464bae2014-08-12 13:53:34 -05003964 /* In 64 bit cross-endian situations, we will erroneously pick up
3965 * the wrong half of the union for the "val" element. To rectify
3966 * this, the entire 8-byte structure is byteswapped, followed by
3967 * a swap of the 4 byte val field. In other cases, the data is
3968 * already in proper host byte order. */
3969 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3970 target_su.buf = tswapal(target_su.buf);
3971 arg.val = tswap32(target_su.val);
3972 } else {
3973 arg.val = target_su.val;
3974 }
aurel32e5289082009-04-18 16:16:12 +00003975 ret = get_errno(semctl(semid, semnum, cmd, arg));
ths3eb6b042007-06-03 14:26:27 +00003976 break;
3977 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00003978 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00003979 err = target_to_host_semarray(semid, &array, target_su.array);
3980 if (err)
3981 return err;
3982 arg.array = array;
3983 ret = get_errno(semctl(semid, semnum, cmd, arg));
3984 err = host_to_target_semarray(semid, target_su.array, &array);
3985 if (err)
3986 return err;
ths3eb6b042007-06-03 14:26:27 +00003987 break;
3988 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00003989 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00003990 case SEM_STAT:
3991 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3992 if (err)
3993 return err;
3994 arg.buf = &dsarg;
3995 ret = get_errno(semctl(semid, semnum, cmd, arg));
3996 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3997 if (err)
3998 return err;
ths3eb6b042007-06-03 14:26:27 +00003999 break;
aurel32e5289082009-04-18 16:16:12 +00004000 case IPC_INFO:
4001 case SEM_INFO:
4002 arg.__buf = &seminfo;
4003 ret = get_errno(semctl(semid, semnum, cmd, arg));
4004 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4005 if (err)
4006 return err;
4007 break;
4008 case IPC_RMID:
4009 case GETPID:
4010 case GETNCNT:
4011 case GETZCNT:
4012 ret = get_errno(semctl(semid, semnum, cmd, NULL));
4013 break;
ths3eb6b042007-06-03 14:26:27 +00004014 }
4015
4016 return ret;
4017}
4018
aurel32e5289082009-04-18 16:16:12 +00004019struct target_sembuf {
4020 unsigned short sem_num;
4021 short sem_op;
4022 short sem_flg;
4023};
4024
4025static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4026 abi_ulong target_addr,
4027 unsigned nsops)
4028{
4029 struct target_sembuf *target_sembuf;
4030 int i;
4031
4032 target_sembuf = lock_user(VERIFY_READ, target_addr,
4033 nsops*sizeof(struct target_sembuf), 1);
4034 if (!target_sembuf)
4035 return -TARGET_EFAULT;
4036
4037 for(i=0; i<nsops; i++) {
4038 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4039 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4040 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4041 }
4042
4043 unlock_user(target_sembuf, target_addr, 0);
4044
4045 return 0;
4046}
4047
Matus Kyseld8c08b12020-06-26 14:46:11 +02004048#if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
Filip Bozutacac46eb2020-08-25 00:30:50 +02004049 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
Matus Kyseld8c08b12020-06-26 14:46:11 +02004050
4051/*
4052 * This macro is required to handle the s390 variants, which passes the
4053 * arguments in a different order than default.
4054 */
4055#ifdef __s390x__
4056#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4057 (__nsops), (__timeout), (__sops)
4058#else
4059#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4060 (__nsops), 0, (__sops), (__timeout)
4061#endif
4062
4063static inline abi_long do_semtimedop(int semid,
4064 abi_long ptr,
4065 unsigned nsops,
Filip Bozutacac46eb2020-08-25 00:30:50 +02004066 abi_long timeout, bool time64)
aurel32e5289082009-04-18 16:16:12 +00004067{
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004068 struct sembuf *sops;
Matus Kyseld8c08b12020-06-26 14:46:11 +02004069 struct timespec ts, *pts = NULL;
Laurent Vivier524fa342019-05-29 10:48:04 +02004070 abi_long ret;
aurel32e5289082009-04-18 16:16:12 +00004071
Matus Kyseld8c08b12020-06-26 14:46:11 +02004072 if (timeout) {
4073 pts = &ts;
Filip Bozutacac46eb2020-08-25 00:30:50 +02004074 if (time64) {
4075 if (target_to_host_timespec64(pts, timeout)) {
4076 return -TARGET_EFAULT;
4077 }
4078 } else {
4079 if (target_to_host_timespec(pts, timeout)) {
4080 return -TARGET_EFAULT;
4081 }
Matus Kyseld8c08b12020-06-26 14:46:11 +02004082 }
4083 }
4084
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004085 if (nsops > TARGET_SEMOPM) {
4086 return -TARGET_E2BIG;
4087 }
4088
4089 sops = g_new(struct sembuf, nsops);
4090
4091 if (target_to_host_sembuf(sops, ptr, nsops)) {
4092 g_free(sops);
aurel32e5289082009-04-18 16:16:12 +00004093 return -TARGET_EFAULT;
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004094 }
aurel32e5289082009-04-18 16:16:12 +00004095
Laurent Vivier524fa342019-05-29 10:48:04 +02004096 ret = -TARGET_ENOSYS;
4097#ifdef __NR_semtimedop
Matus Kyseld8c08b12020-06-26 14:46:11 +02004098 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
Laurent Vivier524fa342019-05-29 10:48:04 +02004099#endif
4100#ifdef __NR_ipc
4101 if (ret == -TARGET_ENOSYS) {
Matus Kyseld8c08b12020-06-26 14:46:11 +02004102 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
4103 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
Laurent Vivier524fa342019-05-29 10:48:04 +02004104 }
4105#endif
Filip Bozuta0a7ec842020-08-18 20:07:22 +02004106 g_free(sops);
Laurent Vivier524fa342019-05-29 10:48:04 +02004107 return ret;
aurel32e5289082009-04-18 16:16:12 +00004108}
Matus Kyseld8c08b12020-06-26 14:46:11 +02004109#endif
aurel32e5289082009-04-18 16:16:12 +00004110
ths1bc012f2007-06-03 14:27:49 +00004111struct target_msqid_ds
4112{
aurel321c54ff92008-10-13 21:08:44 +00004113 struct target_ipc_perm msg_perm;
4114 abi_ulong msg_stime;
4115#if TARGET_ABI_BITS == 32
4116 abi_ulong __unused1;
4117#endif
4118 abi_ulong msg_rtime;
4119#if TARGET_ABI_BITS == 32
4120 abi_ulong __unused2;
4121#endif
4122 abi_ulong msg_ctime;
4123#if TARGET_ABI_BITS == 32
4124 abi_ulong __unused3;
4125#endif
4126 abi_ulong __msg_cbytes;
4127 abi_ulong msg_qnum;
4128 abi_ulong msg_qbytes;
4129 abi_ulong msg_lspid;
4130 abi_ulong msg_lrpid;
4131 abi_ulong __unused4;
4132 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00004133};
4134
bellard579a97f2007-11-11 14:26:47 +00004135static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4136 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00004137{
4138 struct target_msqid_ds *target_md;
4139
bellard579a97f2007-11-11 14:26:47 +00004140 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4141 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00004142 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4143 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004144 host_md->msg_stime = tswapal(target_md->msg_stime);
4145 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4146 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4147 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4148 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4149 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4150 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4151 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00004152 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00004153 return 0;
ths1bc012f2007-06-03 14:27:49 +00004154}
4155
bellard579a97f2007-11-11 14:26:47 +00004156static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4157 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00004158{
4159 struct target_msqid_ds *target_md;
4160
bellard579a97f2007-11-11 14:26:47 +00004161 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4162 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00004163 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4164 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004165 target_md->msg_stime = tswapal(host_md->msg_stime);
4166 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4167 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4168 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4169 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4170 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4171 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4172 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00004173 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00004174 return 0;
ths1bc012f2007-06-03 14:27:49 +00004175}
4176
aurel321c54ff92008-10-13 21:08:44 +00004177struct target_msginfo {
4178 int msgpool;
4179 int msgmap;
4180 int msgmax;
4181 int msgmnb;
4182 int msgmni;
4183 int msgssz;
4184 int msgtql;
4185 unsigned short int msgseg;
4186};
4187
4188static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4189 struct msginfo *host_msginfo)
4190{
4191 struct target_msginfo *target_msginfo;
4192 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4193 return -TARGET_EFAULT;
4194 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4195 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4196 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4197 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4198 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4199 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4200 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4201 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4202 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00004203 return 0;
aurel321c54ff92008-10-13 21:08:44 +00004204}
4205
4206static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00004207{
4208 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00004209 struct msginfo msginfo;
4210 abi_long ret = -TARGET_EINVAL;
4211
4212 cmd &= 0xff;
4213
4214 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00004215 case IPC_STAT:
4216 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00004217 case MSG_STAT:
4218 if (target_to_host_msqid_ds(&dsarg,ptr))
4219 return -TARGET_EFAULT;
4220 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4221 if (host_to_target_msqid_ds(ptr,&dsarg))
4222 return -TARGET_EFAULT;
4223 break;
4224 case IPC_RMID:
4225 ret = get_errno(msgctl(msgid, cmd, NULL));
4226 break;
4227 case IPC_INFO:
4228 case MSG_INFO:
4229 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4230 if (host_to_target_msginfo(ptr, &msginfo))
4231 return -TARGET_EFAULT;
4232 break;
ths1bc012f2007-06-03 14:27:49 +00004233 }
aurel321c54ff92008-10-13 21:08:44 +00004234
ths1bc012f2007-06-03 14:27:49 +00004235 return ret;
4236}
4237
4238struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00004239 abi_long mtype;
4240 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00004241};
4242
blueswir1992f48a2007-10-14 16:27:31 +00004243static inline abi_long do_msgsnd(int msqid, abi_long msgp,
Tom Mustaedcc5f92014-08-12 13:53:37 -05004244 ssize_t msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00004245{
4246 struct target_msgbuf *target_mb;
4247 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00004248 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00004249
Tom Mustaedcc5f92014-08-12 13:53:37 -05004250 if (msgsz < 0) {
4251 return -TARGET_EINVAL;
4252 }
4253
bellard579a97f2007-11-11 14:26:47 +00004254 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4255 return -TARGET_EFAULT;
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304256 host_mb = g_try_malloc(msgsz + sizeof(long));
zhanghailiang29e03fc2014-08-14 15:29:18 +08004257 if (!host_mb) {
4258 unlock_user_struct(target_mb, msgp, 0);
4259 return -TARGET_ENOMEM;
4260 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004261 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00004262 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
Laurent Vivier524fa342019-05-29 10:48:04 +02004263 ret = -TARGET_ENOSYS;
4264#ifdef __NR_msgsnd
Peter Maydell89f9fe42016-06-06 19:58:05 +01004265 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
Laurent Vivier524fa342019-05-29 10:48:04 +02004266#endif
4267#ifdef __NR_ipc
4268 if (ret == -TARGET_ENOSYS) {
Matus Kyseld8c08b12020-06-26 14:46:11 +02004269#ifdef __s390x__
4270 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4271 host_mb));
4272#else
Laurent Vivier524fa342019-05-29 10:48:04 +02004273 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4274 host_mb, 0));
Matus Kyseld8c08b12020-06-26 14:46:11 +02004275#endif
Laurent Vivier524fa342019-05-29 10:48:04 +02004276 }
4277#endif
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304278 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00004279 unlock_user_struct(target_mb, msgp, 0);
4280
4281 return ret;
4282}
4283
Matus Kyseld8c08b12020-06-26 14:46:11 +02004284#ifdef __NR_ipc
4285#if defined(__sparc__)
4286/* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4287#define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4288#elif defined(__s390x__)
4289/* The s390 sys_ipc variant has only five parameters. */
4290#define MSGRCV_ARGS(__msgp, __msgtyp) \
4291 ((long int[]){(long int)__msgp, __msgtyp})
4292#else
4293#define MSGRCV_ARGS(__msgp, __msgtyp) \
4294 ((long int[]){(long int)__msgp, __msgtyp}), 0
4295#endif
4296#endif
4297
blueswir1992f48a2007-10-14 16:27:31 +00004298static inline abi_long do_msgrcv(int msqid, abi_long msgp,
Peter Maydell99874f62016-05-20 19:00:56 +01004299 ssize_t msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00004300 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00004301{
4302 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00004303 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00004304 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00004305 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00004306
Peter Maydell99874f62016-05-20 19:00:56 +01004307 if (msgsz < 0) {
4308 return -TARGET_EINVAL;
4309 }
4310
bellard579a97f2007-11-11 14:26:47 +00004311 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4312 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00004313
Peter Maydell415d8472016-05-20 19:00:57 +01004314 host_mb = g_try_malloc(msgsz + sizeof(long));
4315 if (!host_mb) {
4316 ret = -TARGET_ENOMEM;
4317 goto end;
4318 }
Laurent Vivier524fa342019-05-29 10:48:04 +02004319 ret = -TARGET_ENOSYS;
4320#ifdef __NR_msgrcv
Peter Maydell89f9fe42016-06-06 19:58:05 +01004321 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
Laurent Vivier524fa342019-05-29 10:48:04 +02004322#endif
4323#ifdef __NR_ipc
4324 if (ret == -TARGET_ENOSYS) {
4325 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
Matus Kyseld8c08b12020-06-26 14:46:11 +02004326 msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
Laurent Vivier524fa342019-05-29 10:48:04 +02004327 }
4328#endif
aurel321c54ff92008-10-13 21:08:44 +00004329
bellard579a97f2007-11-11 14:26:47 +00004330 if (ret > 0) {
4331 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4332 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4333 if (!target_mtext) {
4334 ret = -TARGET_EFAULT;
4335 goto end;
4336 }
aurel321c54ff92008-10-13 21:08:44 +00004337 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00004338 unlock_user(target_mtext, target_mtext_addr, ret);
4339 }
aurel321c54ff92008-10-13 21:08:44 +00004340
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004341 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00004342
bellard579a97f2007-11-11 14:26:47 +00004343end:
4344 if (target_mb)
4345 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02004346 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00004347 return ret;
4348}
4349
Riku Voipio88a8c982009-04-03 10:42:00 +03004350static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4351 abi_ulong target_addr)
4352{
4353 struct target_shmid_ds *target_sd;
4354
4355 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4356 return -TARGET_EFAULT;
4357 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4358 return -TARGET_EFAULT;
4359 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4360 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4361 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4362 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4363 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4364 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4365 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4366 unlock_user_struct(target_sd, target_addr, 0);
4367 return 0;
4368}
4369
4370static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4371 struct shmid_ds *host_sd)
4372{
4373 struct target_shmid_ds *target_sd;
4374
4375 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4376 return -TARGET_EFAULT;
4377 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
4378 return -TARGET_EFAULT;
4379 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4380 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
4381 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4382 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4383 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4384 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4385 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4386 unlock_user_struct(target_sd, target_addr, 1);
4387 return 0;
4388}
4389
4390struct target_shminfo {
4391 abi_ulong shmmax;
4392 abi_ulong shmmin;
4393 abi_ulong shmmni;
4394 abi_ulong shmseg;
4395 abi_ulong shmall;
4396};
4397
4398static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
4399 struct shminfo *host_shminfo)
4400{
4401 struct target_shminfo *target_shminfo;
4402 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
4403 return -TARGET_EFAULT;
4404 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
4405 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
4406 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
4407 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
4408 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
4409 unlock_user_struct(target_shminfo, target_addr, 1);
4410 return 0;
4411}
4412
4413struct target_shm_info {
4414 int used_ids;
4415 abi_ulong shm_tot;
4416 abi_ulong shm_rss;
4417 abi_ulong shm_swp;
4418 abi_ulong swap_attempts;
4419 abi_ulong swap_successes;
4420};
4421
4422static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
4423 struct shm_info *host_shm_info)
4424{
4425 struct target_shm_info *target_shm_info;
4426 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
4427 return -TARGET_EFAULT;
4428 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
4429 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
4430 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
4431 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
4432 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
4433 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
4434 unlock_user_struct(target_shm_info, target_addr, 1);
4435 return 0;
4436}
4437
4438static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4439{
4440 struct shmid_ds dsarg;
4441 struct shminfo shminfo;
4442 struct shm_info shm_info;
4443 abi_long ret = -TARGET_EINVAL;
4444
4445 cmd &= 0xff;
4446
4447 switch(cmd) {
4448 case IPC_STAT:
4449 case IPC_SET:
4450 case SHM_STAT:
4451 if (target_to_host_shmid_ds(&dsarg, buf))
4452 return -TARGET_EFAULT;
4453 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4454 if (host_to_target_shmid_ds(buf, &dsarg))
4455 return -TARGET_EFAULT;
4456 break;
4457 case IPC_INFO:
4458 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4459 if (host_to_target_shminfo(buf, &shminfo))
4460 return -TARGET_EFAULT;
4461 break;
4462 case SHM_INFO:
4463 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4464 if (host_to_target_shm_info(buf, &shm_info))
4465 return -TARGET_EFAULT;
4466 break;
4467 case IPC_RMID:
4468 case SHM_LOCK:
4469 case SHM_UNLOCK:
4470 ret = get_errno(shmctl(shmid, cmd, NULL));
4471 break;
4472 }
4473
4474 return ret;
4475}
4476
aurel321c54ff92008-10-13 21:08:44 +00004477#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00004478/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00004479/* do_ipc() must return target values and target errnos. */
Peter Maydellee8e7612016-07-11 16:48:11 +01004480static abi_long do_ipc(CPUArchState *cpu_env,
4481 unsigned int call, abi_long first,
Tom Musta37ed0952014-08-12 13:53:35 -05004482 abi_long second, abi_long third,
blueswir1992f48a2007-10-14 16:27:31 +00004483 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00004484{
4485 int version;
blueswir1992f48a2007-10-14 16:27:31 +00004486 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00004487
4488 version = call >> 16;
4489 call &= 0xffff;
4490
4491 switch (call) {
thsfa294812007-02-02 22:05:00 +00004492 case IPCOP_semop:
Filip Bozutacac46eb2020-08-25 00:30:50 +02004493 ret = do_semtimedop(first, ptr, second, 0, false);
Matus Kyseld8c08b12020-06-26 14:46:11 +02004494 break;
4495 case IPCOP_semtimedop:
4496 /*
4497 * The s390 sys_ipc variant has only five parameters instead of six
4498 * (as for default variant) and the only difference is the handling of
4499 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4500 * to a struct timespec where the generic variant uses fifth parameter.
4501 */
4502#if defined(TARGET_S390X)
Filip Bozutacac46eb2020-08-25 00:30:50 +02004503 ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
Matus Kyseld8c08b12020-06-26 14:46:11 +02004504#else
Filip Bozutacac46eb2020-08-25 00:30:50 +02004505 ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
Matus Kyseld8c08b12020-06-26 14:46:11 +02004506#endif
thsfa294812007-02-02 22:05:00 +00004507 break;
4508
4509 case IPCOP_semget:
4510 ret = get_errno(semget(first, second, third));
4511 break;
4512
Tom Musta5d2fa8e2014-08-12 13:53:33 -05004513 case IPCOP_semctl: {
4514 /* The semun argument to semctl is passed by value, so dereference the
4515 * ptr argument. */
4516 abi_ulong atptr;
Tom Musta37ed0952014-08-12 13:53:35 -05004517 get_user_ual(atptr, ptr);
Stefan Weild1c002b2015-02-08 15:40:58 +01004518 ret = do_semctl(first, second, third, atptr);
thsfa294812007-02-02 22:05:00 +00004519 break;
Tom Musta5d2fa8e2014-08-12 13:53:33 -05004520 }
thsd96372e2007-02-02 22:05:44 +00004521
aurel321c54ff92008-10-13 21:08:44 +00004522 case IPCOP_msgget:
4523 ret = get_errno(msgget(first, second));
4524 break;
thsd96372e2007-02-02 22:05:44 +00004525
aurel321c54ff92008-10-13 21:08:44 +00004526 case IPCOP_msgsnd:
4527 ret = do_msgsnd(first, ptr, second, third);
4528 break;
thsd96372e2007-02-02 22:05:44 +00004529
aurel321c54ff92008-10-13 21:08:44 +00004530 case IPCOP_msgctl:
4531 ret = do_msgctl(first, second, ptr);
4532 break;
thsd96372e2007-02-02 22:05:44 +00004533
aurel321c54ff92008-10-13 21:08:44 +00004534 case IPCOP_msgrcv:
4535 switch (version) {
4536 case 0:
4537 {
4538 struct target_ipc_kludge {
4539 abi_long msgp;
4540 abi_long msgtyp;
4541 } *tmp;
thsd96372e2007-02-02 22:05:44 +00004542
aurel321c54ff92008-10-13 21:08:44 +00004543 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4544 ret = -TARGET_EFAULT;
4545 break;
ths1bc012f2007-06-03 14:27:49 +00004546 }
aurel321c54ff92008-10-13 21:08:44 +00004547
Laurent Vivier79dd77d2012-12-20 11:00:11 +00004548 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00004549
4550 unlock_user_struct(tmp, ptr, 0);
4551 break;
4552 }
4553 default:
4554 ret = do_msgrcv(first, ptr, second, fifth, third);
4555 }
4556 break;
thsd96372e2007-02-02 22:05:44 +00004557
bellard8853f862004-02-22 14:57:26 +00004558 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03004559 switch (version) {
4560 default:
bellard5a4a8982007-11-11 17:39:18 +00004561 {
4562 abi_ulong raddr;
Richard Henderson225a2062023-08-20 09:24:14 -07004563 raddr = target_shmat(cpu_env, first, ptr, second);
Riku Voipio88a8c982009-04-03 10:42:00 +03004564 if (is_error(raddr))
4565 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00004566 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00004567 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03004568 break;
4569 }
4570 case 1:
4571 ret = -TARGET_EINVAL;
4572 break;
bellard5a4a8982007-11-11 17:39:18 +00004573 }
bellard8853f862004-02-22 14:57:26 +00004574 break;
4575 case IPCOP_shmdt:
Richard Henderson225a2062023-08-20 09:24:14 -07004576 ret = target_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00004577 break;
4578
4579 case IPCOP_shmget:
4580 /* IPC_* flag values are the same on all linux platforms */
4581 ret = get_errno(shmget(first, second, third));
4582 break;
4583
4584 /* IPC_* and SHM_* command values are the same on all linux platforms */
4585 case IPCOP_shmctl:
Petar Jovanovica2926782013-10-30 14:46:32 +01004586 ret = do_shmctl(first, second, ptr);
bellard8853f862004-02-22 14:57:26 +00004587 break;
4588 default:
Josh Kunz39be5352020-02-03 18:54:13 -08004589 qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
4590 call, version);
ths0da46a62007-10-20 20:23:07 +00004591 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00004592 break;
4593 }
4594 return ret;
4595}
j_mayer32407102007-09-26 23:01:49 +00004596#endif
bellard8853f862004-02-22 14:57:26 +00004597
bellard31e31b82003-02-18 22:55:36 +00004598/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00004599
Blue Swirl001faf32009-05-13 17:53:17 +00004600#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00004601#define STRUCT_SPECIAL(name) STRUCT_ ## name,
4602enum {
4603#include "syscall_types.h"
Alexander Graf8be656b2015-05-06 23:47:32 +02004604STRUCT_MAX
bellard31e31b82003-02-18 22:55:36 +00004605};
4606#undef STRUCT
4607#undef STRUCT_SPECIAL
4608
Blue Swirl001faf32009-05-13 17:53:17 +00004609#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00004610#define STRUCT_SPECIAL(name)
4611#include "syscall_types.h"
4612#undef STRUCT
4613#undef STRUCT_SPECIAL
4614
bellard31e31b82003-02-18 22:55:36 +00004615#define MAX_STRUCT_SIZE 4096
4616
Peter Maydelldace20d2011-01-10 13:11:24 +00004617#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00004618/* So fiemap access checks don't overflow on 32 bit systems.
4619 * This is very slightly smaller than the limit imposed by
4620 * the underlying kernel.
4621 */
4622#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4623 / sizeof(struct fiemap_extent))
4624
4625static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004626 int fd, int cmd, abi_long arg)
Peter Maydell285da2b2011-01-06 15:04:18 +00004627{
4628 /* The parameter for this ioctl is a struct fiemap followed
4629 * by an array of struct fiemap_extent whose size is set
4630 * in fiemap->fm_extent_count. The array is filled in by the
4631 * ioctl.
4632 */
4633 int target_size_in, target_size_out;
4634 struct fiemap *fm;
4635 const argtype *arg_type = ie->arg_type;
4636 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4637 void *argptr, *p;
4638 abi_long ret;
4639 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4640 uint32_t outbufsz;
4641 int free_fm = 0;
4642
4643 assert(arg_type[0] == TYPE_PTR);
4644 assert(ie->access == IOC_RW);
4645 arg_type++;
4646 target_size_in = thunk_type_size(arg_type, 0);
4647 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4648 if (!argptr) {
4649 return -TARGET_EFAULT;
4650 }
4651 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4652 unlock_user(argptr, arg, 0);
4653 fm = (struct fiemap *)buf_temp;
4654 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4655 return -TARGET_EINVAL;
4656 }
4657
4658 outbufsz = sizeof (*fm) +
4659 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4660
4661 if (outbufsz > MAX_STRUCT_SIZE) {
4662 /* We can't fit all the extents into the fixed size buffer.
4663 * Allocate one that is large enough and use it instead.
4664 */
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304665 fm = g_try_malloc(outbufsz);
Peter Maydell285da2b2011-01-06 15:04:18 +00004666 if (!fm) {
4667 return -TARGET_ENOMEM;
4668 }
4669 memcpy(fm, buf_temp, sizeof(struct fiemap));
4670 free_fm = 1;
4671 }
Peter Maydell49ca6f32016-06-06 19:58:14 +01004672 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
Peter Maydell285da2b2011-01-06 15:04:18 +00004673 if (!is_error(ret)) {
4674 target_size_out = target_size_in;
4675 /* An extent_count of 0 means we were only counting the extents
4676 * so there are no structs to copy
4677 */
4678 if (fm->fm_extent_count != 0) {
4679 target_size_out += fm->fm_mapped_extents * extent_size;
4680 }
4681 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4682 if (!argptr) {
4683 ret = -TARGET_EFAULT;
4684 } else {
4685 /* Convert the struct fiemap */
4686 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4687 if (fm->fm_extent_count != 0) {
4688 p = argptr + target_size_in;
4689 /* ...and then all the struct fiemap_extents */
4690 for (i = 0; i < fm->fm_mapped_extents; i++) {
4691 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4692 THUNK_TARGET);
4693 p += extent_size;
4694 }
4695 }
4696 unlock_user(argptr, arg, target_size_out);
4697 }
4698 }
4699 if (free_fm) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304700 g_free(fm);
Peter Maydell285da2b2011-01-06 15:04:18 +00004701 }
4702 return ret;
4703}
Peter Maydelldace20d2011-01-10 13:11:24 +00004704#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00004705
Laurent Vivier059c2f22011-03-30 00:12:12 +02004706static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004707 int fd, int cmd, abi_long arg)
Laurent Vivier059c2f22011-03-30 00:12:12 +02004708{
4709 const argtype *arg_type = ie->arg_type;
4710 int target_size;
4711 void *argptr;
4712 int ret;
4713 struct ifconf *host_ifconf;
4714 uint32_t outbufsz;
4715 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
Stefan4df7b7f2021-01-09 19:59:42 +01004716 const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
Laurent Vivier059c2f22011-03-30 00:12:12 +02004717 int target_ifreq_size;
4718 int nb_ifreq;
4719 int free_buf = 0;
4720 int i;
4721 int target_ifc_len;
4722 abi_long target_ifc_buf;
4723 int host_ifc_len;
4724 char *host_ifc_buf;
4725
4726 assert(arg_type[0] == TYPE_PTR);
4727 assert(ie->access == IOC_RW);
4728
4729 arg_type++;
4730 target_size = thunk_type_size(arg_type, 0);
4731
4732 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4733 if (!argptr)
4734 return -TARGET_EFAULT;
4735 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4736 unlock_user(argptr, arg, 0);
4737
4738 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
Laurent Vivier059c2f22011-03-30 00:12:12 +02004739 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
Stefan4df7b7f2021-01-09 19:59:42 +01004740 target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004741
Kan Li22e4a2672018-10-24 20:13:03 +00004742 if (target_ifc_buf != 0) {
4743 target_ifc_len = host_ifconf->ifc_len;
4744 nb_ifreq = target_ifc_len / target_ifreq_size;
4745 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4746
4747 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4748 if (outbufsz > MAX_STRUCT_SIZE) {
4749 /*
4750 * We can't fit all the extents into the fixed size buffer.
4751 * Allocate one that is large enough and use it instead.
4752 */
Ahmed Abouzied7a5626a2022-01-04 16:38:41 +02004753 host_ifconf = g_try_malloc(outbufsz);
Kan Li22e4a2672018-10-24 20:13:03 +00004754 if (!host_ifconf) {
4755 return -TARGET_ENOMEM;
4756 }
4757 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4758 free_buf = 1;
Laurent Vivier059c2f22011-03-30 00:12:12 +02004759 }
Kan Li22e4a2672018-10-24 20:13:03 +00004760 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004761
Kan Li22e4a2672018-10-24 20:13:03 +00004762 host_ifconf->ifc_len = host_ifc_len;
4763 } else {
4764 host_ifc_buf = NULL;
4765 }
Laurent Vivier059c2f22011-03-30 00:12:12 +02004766 host_ifconf->ifc_buf = host_ifc_buf;
4767
Peter Maydell49ca6f32016-06-06 19:58:14 +01004768 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
Laurent Vivier059c2f22011-03-30 00:12:12 +02004769 if (!is_error(ret)) {
4770 /* convert host ifc_len to target ifc_len */
4771
4772 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4773 target_ifc_len = nb_ifreq * target_ifreq_size;
4774 host_ifconf->ifc_len = target_ifc_len;
4775
4776 /* restore target ifc_buf */
4777
4778 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4779
4780 /* copy struct ifconf to target user */
4781
4782 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4783 if (!argptr)
4784 return -TARGET_EFAULT;
4785 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4786 unlock_user(argptr, arg, target_size);
4787
Kan Li22e4a2672018-10-24 20:13:03 +00004788 if (target_ifc_buf != 0) {
4789 /* copy ifreq[] to target user */
4790 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4791 for (i = 0; i < nb_ifreq ; i++) {
4792 thunk_convert(argptr + i * target_ifreq_size,
4793 host_ifc_buf + i * sizeof(struct ifreq),
4794 ifreq_arg_type, THUNK_TARGET);
4795 }
4796 unlock_user(argptr, target_ifc_buf, target_ifc_len);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004797 }
Laurent Vivier059c2f22011-03-30 00:12:12 +02004798 }
4799
4800 if (free_buf) {
Ahmed Abouzied7a5626a2022-01-04 16:38:41 +02004801 g_free(host_ifconf);
Laurent Vivier059c2f22011-03-30 00:12:12 +02004802 }
4803
4804 return ret;
4805}
4806
Cortland Tölvaa1333672018-10-08 09:35:21 -07004807#if defined(CONFIG_USBFS)
4808#if HOST_LONG_BITS > 64
4809#error USBDEVFS thunks do not support >64 bit hosts yet.
4810#endif
4811struct live_urb {
4812 uint64_t target_urb_adr;
4813 uint64_t target_buf_adr;
4814 char *target_buf_ptr;
4815 struct usbdevfs_urb host_urb;
4816};
4817
4818static GHashTable *usbdevfs_urb_hashtable(void)
4819{
4820 static GHashTable *urb_hashtable;
4821
4822 if (!urb_hashtable) {
4823 urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
4824 }
4825 return urb_hashtable;
4826}
4827
4828static void urb_hashtable_insert(struct live_urb *urb)
4829{
4830 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4831 g_hash_table_insert(urb_hashtable, urb, urb);
4832}
4833
4834static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
4835{
4836 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4837 return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
4838}
4839
4840static void urb_hashtable_remove(struct live_urb *urb)
4841{
4842 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4843 g_hash_table_remove(urb_hashtable, urb);
4844}
4845
4846static abi_long
4847do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
4848 int fd, int cmd, abi_long arg)
4849{
4850 const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
4851 const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
4852 struct live_urb *lurb;
4853 void *argptr;
4854 uint64_t hurb;
4855 int target_size;
4856 uintptr_t target_urb_adr;
4857 abi_long ret;
4858
4859 target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
4860
4861 memset(buf_temp, 0, sizeof(uint64_t));
4862 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
4863 if (is_error(ret)) {
4864 return ret;
4865 }
4866
4867 memcpy(&hurb, buf_temp, sizeof(uint64_t));
4868 lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
4869 if (!lurb->target_urb_adr) {
4870 return -TARGET_EFAULT;
4871 }
4872 urb_hashtable_remove(lurb);
4873 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
4874 lurb->host_urb.buffer_length);
4875 lurb->target_buf_ptr = NULL;
4876
4877 /* restore the guest buffer pointer */
4878 lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
4879
4880 /* update the guest urb struct */
4881 argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
4882 if (!argptr) {
4883 g_free(lurb);
4884 return -TARGET_EFAULT;
4885 }
4886 thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
4887 unlock_user(argptr, lurb->target_urb_adr, target_size);
4888
4889 target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
4890 /* write back the urb handle */
4891 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4892 if (!argptr) {
4893 g_free(lurb);
4894 return -TARGET_EFAULT;
4895 }
4896
4897 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
4898 target_urb_adr = lurb->target_urb_adr;
4899 thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
4900 unlock_user(argptr, arg, target_size);
4901
4902 g_free(lurb);
4903 return ret;
4904}
4905
4906static abi_long
4907do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
4908 uint8_t *buf_temp __attribute__((unused)),
4909 int fd, int cmd, abi_long arg)
4910{
4911 struct live_urb *lurb;
4912
4913 /* map target address back to host URB with metadata. */
4914 lurb = urb_hashtable_lookup(arg);
4915 if (!lurb) {
4916 return -TARGET_EFAULT;
4917 }
4918 return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4919}
4920
4921static abi_long
4922do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
4923 int fd, int cmd, abi_long arg)
4924{
4925 const argtype *arg_type = ie->arg_type;
4926 int target_size;
4927 abi_long ret;
4928 void *argptr;
4929 int rw_dir;
4930 struct live_urb *lurb;
4931
4932 /*
4933 * each submitted URB needs to map to a unique ID for the
4934 * kernel, and that unique ID needs to be a pointer to
4935 * host memory. hence, we need to malloc for each URB.
4936 * isochronous transfers have a variable length struct.
4937 */
4938 arg_type++;
4939 target_size = thunk_type_size(arg_type, THUNK_TARGET);
4940
4941 /* construct host copy of urb and metadata */
Markus Armbrusterb21e2382022-03-15 15:41:56 +01004942 lurb = g_try_new0(struct live_urb, 1);
Cortland Tölvaa1333672018-10-08 09:35:21 -07004943 if (!lurb) {
4944 return -TARGET_ENOMEM;
4945 }
4946
4947 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4948 if (!argptr) {
4949 g_free(lurb);
4950 return -TARGET_EFAULT;
4951 }
4952 thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
4953 unlock_user(argptr, arg, 0);
4954
4955 lurb->target_urb_adr = arg;
4956 lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
4957
4958 /* buffer space used depends on endpoint type so lock the entire buffer */
4959 /* control type urbs should check the buffer contents for true direction */
4960 rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
4961 lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
4962 lurb->host_urb.buffer_length, 1);
4963 if (lurb->target_buf_ptr == NULL) {
4964 g_free(lurb);
4965 return -TARGET_EFAULT;
4966 }
4967
4968 /* update buffer pointer in host copy */
4969 lurb->host_urb.buffer = lurb->target_buf_ptr;
4970
4971 ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4972 if (is_error(ret)) {
4973 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
4974 g_free(lurb);
4975 } else {
4976 urb_hashtable_insert(lurb);
4977 }
4978
4979 return ret;
4980}
4981#endif /* CONFIG_USBFS */
4982
Alexander Graf56e904e2012-01-31 18:42:06 +01004983static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004984 int cmd, abi_long arg)
Alexander Graf56e904e2012-01-31 18:42:06 +01004985{
4986 void *argptr;
4987 struct dm_ioctl *host_dm;
4988 abi_long guest_data;
4989 uint32_t guest_data_size;
4990 int target_size;
4991 const argtype *arg_type = ie->arg_type;
4992 abi_long ret;
4993 void *big_buf = NULL;
4994 char *host_data;
4995
4996 arg_type++;
4997 target_size = thunk_type_size(arg_type, 0);
4998 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4999 if (!argptr) {
5000 ret = -TARGET_EFAULT;
5001 goto out;
5002 }
5003 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5004 unlock_user(argptr, arg, 0);
5005
5006 /* buf_temp is too small, so fetch things into a bigger buffer */
5007 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
5008 memcpy(big_buf, buf_temp, target_size);
5009 buf_temp = big_buf;
5010 host_dm = big_buf;
5011
5012 guest_data = arg + host_dm->data_start;
5013 if ((guest_data - arg) < 0) {
Peter Maydellf9757b12016-07-12 13:02:14 +01005014 ret = -TARGET_EINVAL;
Alexander Graf56e904e2012-01-31 18:42:06 +01005015 goto out;
5016 }
5017 guest_data_size = host_dm->data_size - host_dm->data_start;
5018 host_data = (char*)host_dm + host_dm->data_start;
5019
5020 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
Peter Maydell32112152016-07-12 13:02:13 +01005021 if (!argptr) {
5022 ret = -TARGET_EFAULT;
5023 goto out;
5024 }
5025
Alexander Graf56e904e2012-01-31 18:42:06 +01005026 switch (ie->host_cmd) {
5027 case DM_REMOVE_ALL:
5028 case DM_LIST_DEVICES:
5029 case DM_DEV_CREATE:
5030 case DM_DEV_REMOVE:
5031 case DM_DEV_SUSPEND:
5032 case DM_DEV_STATUS:
5033 case DM_DEV_WAIT:
5034 case DM_TABLE_STATUS:
5035 case DM_TABLE_CLEAR:
5036 case DM_TABLE_DEPS:
5037 case DM_LIST_VERSIONS:
5038 /* no input data */
5039 break;
5040 case DM_DEV_RENAME:
5041 case DM_DEV_SET_GEOMETRY:
5042 /* data contains only strings */
5043 memcpy(host_data, argptr, guest_data_size);
5044 break;
5045 case DM_TARGET_MSG:
5046 memcpy(host_data, argptr, guest_data_size);
5047 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5048 break;
5049 case DM_TABLE_LOAD:
5050 {
5051 void *gspec = argptr;
5052 void *cur_data = host_data;
Laurent Viviereb2929c2023-09-25 17:10:27 +02005053 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5054 int spec_size = thunk_type_size(dm_arg_type, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005055 int i;
5056
5057 for (i = 0; i < host_dm->target_count; i++) {
5058 struct dm_target_spec *spec = cur_data;
5059 uint32_t next;
5060 int slen;
5061
Laurent Viviereb2929c2023-09-25 17:10:27 +02005062 thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST);
Alexander Graf56e904e2012-01-31 18:42:06 +01005063 slen = strlen((char*)gspec + spec_size) + 1;
5064 next = spec->next;
5065 spec->next = sizeof(*spec) + slen;
5066 strcpy((char*)&spec[1], gspec + spec_size);
5067 gspec += next;
5068 cur_data += spec->next;
5069 }
5070 break;
5071 }
5072 default:
5073 ret = -TARGET_EINVAL;
Chen Gang Sdec04732015-01-25 08:00:42 +08005074 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005075 goto out;
5076 }
5077 unlock_user(argptr, guest_data, 0);
5078
Peter Maydell49ca6f32016-06-06 19:58:14 +01005079 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
Alexander Graf56e904e2012-01-31 18:42:06 +01005080 if (!is_error(ret)) {
5081 guest_data = arg + host_dm->data_start;
5082 guest_data_size = host_dm->data_size - host_dm->data_start;
5083 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
5084 switch (ie->host_cmd) {
5085 case DM_REMOVE_ALL:
5086 case DM_DEV_CREATE:
5087 case DM_DEV_REMOVE:
5088 case DM_DEV_RENAME:
5089 case DM_DEV_SUSPEND:
5090 case DM_DEV_STATUS:
5091 case DM_TABLE_LOAD:
5092 case DM_TABLE_CLEAR:
5093 case DM_TARGET_MSG:
5094 case DM_DEV_SET_GEOMETRY:
5095 /* no return data */
5096 break;
5097 case DM_LIST_DEVICES:
5098 {
5099 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
5100 uint32_t remaining_data = guest_data_size;
5101 void *cur_data = argptr;
Laurent Viviereb2929c2023-09-25 17:10:27 +02005102 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
Alexander Graf56e904e2012-01-31 18:42:06 +01005103 int nl_size = 12; /* can't use thunk_size due to alignment */
5104
5105 while (1) {
5106 uint32_t next = nl->next;
5107 if (next) {
5108 nl->next = nl_size + (strlen(nl->name) + 1);
5109 }
5110 if (remaining_data < nl->next) {
5111 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5112 break;
5113 }
Laurent Viviereb2929c2023-09-25 17:10:27 +02005114 thunk_convert(cur_data, nl, dm_arg_type, THUNK_TARGET);
Alexander Graf56e904e2012-01-31 18:42:06 +01005115 strcpy(cur_data + nl_size, nl->name);
5116 cur_data += nl->next;
5117 remaining_data -= nl->next;
5118 if (!next) {
5119 break;
5120 }
5121 nl = (void*)nl + next;
5122 }
5123 break;
5124 }
5125 case DM_DEV_WAIT:
5126 case DM_TABLE_STATUS:
5127 {
5128 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
5129 void *cur_data = argptr;
Laurent Viviereb2929c2023-09-25 17:10:27 +02005130 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5131 int spec_size = thunk_type_size(dm_arg_type, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005132 int i;
5133
5134 for (i = 0; i < host_dm->target_count; i++) {
5135 uint32_t next = spec->next;
5136 int slen = strlen((char*)&spec[1]) + 1;
5137 spec->next = (cur_data - argptr) + spec_size + slen;
5138 if (guest_data_size < spec->next) {
5139 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5140 break;
5141 }
Laurent Viviereb2929c2023-09-25 17:10:27 +02005142 thunk_convert(cur_data, spec, dm_arg_type, THUNK_TARGET);
Alexander Graf56e904e2012-01-31 18:42:06 +01005143 strcpy(cur_data + spec_size, (char*)&spec[1]);
5144 cur_data = argptr + spec->next;
5145 spec = (void*)host_dm + host_dm->data_start + next;
5146 }
5147 break;
5148 }
5149 case DM_TABLE_DEPS:
5150 {
5151 void *hdata = (void*)host_dm + host_dm->data_start;
5152 int count = *(uint32_t*)hdata;
5153 uint64_t *hdev = hdata + 8;
5154 uint64_t *gdev = argptr + 8;
5155 int i;
5156
5157 *(uint32_t*)argptr = tswap32(count);
5158 for (i = 0; i < count; i++) {
5159 *gdev = tswap64(*hdev);
5160 gdev++;
5161 hdev++;
5162 }
5163 break;
5164 }
5165 case DM_LIST_VERSIONS:
5166 {
5167 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
5168 uint32_t remaining_data = guest_data_size;
5169 void *cur_data = argptr;
Laurent Viviereb2929c2023-09-25 17:10:27 +02005170 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
5171 int vers_size = thunk_type_size(dm_arg_type, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005172
5173 while (1) {
5174 uint32_t next = vers->next;
5175 if (next) {
5176 vers->next = vers_size + (strlen(vers->name) + 1);
5177 }
5178 if (remaining_data < vers->next) {
5179 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5180 break;
5181 }
Laurent Viviereb2929c2023-09-25 17:10:27 +02005182 thunk_convert(cur_data, vers, dm_arg_type, THUNK_TARGET);
Alexander Graf56e904e2012-01-31 18:42:06 +01005183 strcpy(cur_data + vers_size, vers->name);
5184 cur_data += vers->next;
5185 remaining_data -= vers->next;
5186 if (!next) {
5187 break;
5188 }
5189 vers = (void*)vers + next;
5190 }
5191 break;
5192 }
5193 default:
Chen Gang Sdec04732015-01-25 08:00:42 +08005194 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01005195 ret = -TARGET_EINVAL;
5196 goto out;
5197 }
5198 unlock_user(argptr, guest_data, guest_data_size);
5199
5200 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5201 if (!argptr) {
5202 ret = -TARGET_EFAULT;
5203 goto out;
5204 }
5205 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5206 unlock_user(argptr, arg, target_size);
5207 }
5208out:
Stefan Weilad11ad72012-09-04 22:14:19 +02005209 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01005210 return ret;
5211}
5212
Alexander Grafa59b5e32014-08-22 13:15:50 +02005213static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005214 int cmd, abi_long arg)
Alexander Grafa59b5e32014-08-22 13:15:50 +02005215{
5216 void *argptr;
5217 int target_size;
5218 const argtype *arg_type = ie->arg_type;
5219 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5220 abi_long ret;
5221
5222 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5223 struct blkpg_partition host_part;
5224
5225 /* Read and convert blkpg */
5226 arg_type++;
5227 target_size = thunk_type_size(arg_type, 0);
5228 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5229 if (!argptr) {
5230 ret = -TARGET_EFAULT;
5231 goto out;
5232 }
5233 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5234 unlock_user(argptr, arg, 0);
5235
5236 switch (host_blkpg->op) {
5237 case BLKPG_ADD_PARTITION:
5238 case BLKPG_DEL_PARTITION:
5239 /* payload is struct blkpg_partition */
5240 break;
5241 default:
5242 /* Unknown opcode */
5243 ret = -TARGET_EINVAL;
5244 goto out;
5245 }
5246
5247 /* Read and convert blkpg->data */
5248 arg = (abi_long)(uintptr_t)host_blkpg->data;
5249 target_size = thunk_type_size(part_arg_type, 0);
5250 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5251 if (!argptr) {
5252 ret = -TARGET_EFAULT;
5253 goto out;
5254 }
5255 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
5256 unlock_user(argptr, arg, 0);
5257
5258 /* Swizzle the data pointer to our local copy and call! */
5259 host_blkpg->data = &host_part;
Peter Maydell49ca6f32016-06-06 19:58:14 +01005260 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
Alexander Grafa59b5e32014-08-22 13:15:50 +02005261
5262out:
5263 return ret;
5264}
5265
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005266static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005267 int fd, int cmd, abi_long arg)
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005268{
5269 const argtype *arg_type = ie->arg_type;
5270 const StructEntry *se;
5271 const argtype *field_types;
5272 const int *dst_offsets, *src_offsets;
5273 int target_size;
5274 void *argptr;
Marc-André Lureaub78c5222019-03-05 16:15:00 +01005275 abi_ulong *target_rt_dev_ptr = NULL;
5276 unsigned long *host_rt_dev_ptr = NULL;
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005277 abi_long ret;
5278 int i;
5279
5280 assert(ie->access == IOC_W);
5281 assert(*arg_type == TYPE_PTR);
5282 arg_type++;
5283 assert(*arg_type == TYPE_STRUCT);
5284 target_size = thunk_type_size(arg_type, 0);
5285 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5286 if (!argptr) {
5287 return -TARGET_EFAULT;
5288 }
5289 arg_type++;
5290 assert(*arg_type == (int)STRUCT_rtentry);
5291 se = struct_entries + *arg_type++;
5292 assert(se->convert[0] == NULL);
5293 /* convert struct here to be able to catch rt_dev string */
5294 field_types = se->field_types;
5295 dst_offsets = se->field_offsets[THUNK_HOST];
5296 src_offsets = se->field_offsets[THUNK_TARGET];
5297 for (i = 0; i < se->nb_fields; i++) {
5298 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
5299 assert(*field_types == TYPE_PTRVOID);
Markus Armbruster3d558332022-11-23 14:38:11 +01005300 target_rt_dev_ptr = argptr + src_offsets[i];
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005301 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
5302 if (*target_rt_dev_ptr != 0) {
5303 *host_rt_dev_ptr = (unsigned long)lock_user_string(
5304 tswapal(*target_rt_dev_ptr));
5305 if (!*host_rt_dev_ptr) {
5306 unlock_user(argptr, arg, 0);
5307 return -TARGET_EFAULT;
5308 }
5309 } else {
5310 *host_rt_dev_ptr = 0;
5311 }
5312 field_types++;
5313 continue;
5314 }
5315 field_types = thunk_convert(buf_temp + dst_offsets[i],
5316 argptr + src_offsets[i],
5317 field_types, THUNK_HOST);
5318 }
5319 unlock_user(argptr, arg, 0);
5320
Peter Maydell49ca6f32016-06-06 19:58:14 +01005321 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
Marc-André Lureaub78c5222019-03-05 16:15:00 +01005322
5323 assert(host_rt_dev_ptr != NULL);
5324 assert(target_rt_dev_ptr != NULL);
Laurent Vivier7ff7b662013-07-02 14:04:12 +01005325 if (*host_rt_dev_ptr != 0) {
5326 unlock_user((void *)*host_rt_dev_ptr,
5327 *target_rt_dev_ptr, 0);
5328 }
5329 return ret;
5330}
5331
Paul Burtonca56f5b2014-06-22 11:25:47 +01005332static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02005333 int fd, int cmd, abi_long arg)
Paul Burtonca56f5b2014-06-22 11:25:47 +01005334{
5335 int sig = target_to_host_signal(arg);
Peter Maydell49ca6f32016-06-06 19:58:14 +01005336 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
Paul Burtonca56f5b2014-06-22 11:25:47 +01005337}
5338
Daniel P. Berrangé6d5d5dd2019-07-18 15:06:41 +02005339static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
5340 int fd, int cmd, abi_long arg)
5341{
5342 struct timeval tv;
5343 abi_long ret;
5344
5345 ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
5346 if (is_error(ret)) {
5347 return ret;
5348 }
5349
5350 if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
5351 if (copy_to_user_timeval(arg, &tv)) {
5352 return -TARGET_EFAULT;
5353 }
5354 } else {
5355 if (copy_to_user_timeval64(arg, &tv)) {
5356 return -TARGET_EFAULT;
5357 }
5358 }
5359
5360 return ret;
5361}
5362
5363static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
5364 int fd, int cmd, abi_long arg)
5365{
5366 struct timespec ts;
5367 abi_long ret;
5368
5369 ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
5370 if (is_error(ret)) {
5371 return ret;
5372 }
5373
5374 if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
5375 if (host_to_target_timespec(arg, &ts)) {
5376 return -TARGET_EFAULT;
5377 }
5378 } else{
5379 if (host_to_target_timespec64(arg, &ts)) {
5380 return -TARGET_EFAULT;
5381 }
5382 }
5383
5384 return ret;
5385}
5386
Andreas Schwab2b74f622018-01-29 11:47:06 +01005387#ifdef TIOCGPTPEER
5388static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
5389 int fd, int cmd, abi_long arg)
5390{
5391 int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
5392 return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
5393}
5394#endif
5395
Chen Gange865b972020-06-05 09:32:21 +08005396#ifdef HAVE_DRM_H
5397
5398static void unlock_drm_version(struct drm_version *host_ver,
5399 struct target_drm_version *target_ver,
5400 bool copy)
5401{
5402 unlock_user(host_ver->name, target_ver->name,
5403 copy ? host_ver->name_len : 0);
5404 unlock_user(host_ver->date, target_ver->date,
5405 copy ? host_ver->date_len : 0);
5406 unlock_user(host_ver->desc, target_ver->desc,
5407 copy ? host_ver->desc_len : 0);
5408}
5409
5410static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
5411 struct target_drm_version *target_ver)
5412{
5413 memset(host_ver, 0, sizeof(*host_ver));
5414
5415 __get_user(host_ver->name_len, &target_ver->name_len);
5416 if (host_ver->name_len) {
5417 host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
5418 target_ver->name_len, 0);
5419 if (!host_ver->name) {
5420 return -EFAULT;
5421 }
5422 }
5423
5424 __get_user(host_ver->date_len, &target_ver->date_len);
5425 if (host_ver->date_len) {
5426 host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
5427 target_ver->date_len, 0);
5428 if (!host_ver->date) {
5429 goto err;
5430 }
5431 }
5432
5433 __get_user(host_ver->desc_len, &target_ver->desc_len);
5434 if (host_ver->desc_len) {
5435 host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
5436 target_ver->desc_len, 0);
5437 if (!host_ver->desc) {
5438 goto err;
5439 }
5440 }
5441
5442 return 0;
5443err:
5444 unlock_drm_version(host_ver, target_ver, false);
5445 return -EFAULT;
5446}
5447
5448static inline void host_to_target_drmversion(
5449 struct target_drm_version *target_ver,
5450 struct drm_version *host_ver)
5451{
5452 __put_user(host_ver->version_major, &target_ver->version_major);
5453 __put_user(host_ver->version_minor, &target_ver->version_minor);
5454 __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
5455 __put_user(host_ver->name_len, &target_ver->name_len);
5456 __put_user(host_ver->date_len, &target_ver->date_len);
5457 __put_user(host_ver->desc_len, &target_ver->desc_len);
5458 unlock_drm_version(host_ver, target_ver, true);
5459}
5460
5461static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
5462 int fd, int cmd, abi_long arg)
5463{
5464 struct drm_version *ver;
5465 struct target_drm_version *target_ver;
5466 abi_long ret;
5467
5468 switch (ie->host_cmd) {
5469 case DRM_IOCTL_VERSION:
5470 if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
5471 return -TARGET_EFAULT;
5472 }
5473 ver = (struct drm_version *)buf_temp;
5474 ret = target_to_host_drmversion(ver, target_ver);
5475 if (!is_error(ret)) {
5476 ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
5477 if (is_error(ret)) {
5478 unlock_drm_version(ver, target_ver, false);
5479 } else {
5480 host_to_target_drmversion(target_ver, ver);
5481 }
5482 }
5483 unlock_user_struct(target_ver, arg, 0);
5484 return ret;
5485 }
5486 return -TARGET_ENOSYS;
5487}
5488
Chen Gang913b03c2020-08-02 21:39:38 +08005489static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
5490 struct drm_i915_getparam *gparam,
5491 int fd, abi_long arg)
5492{
5493 abi_long ret;
5494 int value;
5495 struct target_drm_i915_getparam *target_gparam;
5496
5497 if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
5498 return -TARGET_EFAULT;
5499 }
5500
5501 __get_user(gparam->param, &target_gparam->param);
5502 gparam->value = &value;
5503 ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
5504 put_user_s32(value, target_gparam->value);
5505
5506 unlock_user_struct(target_gparam, arg, 0);
5507 return ret;
5508}
5509
5510static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
5511 int fd, int cmd, abi_long arg)
5512{
5513 switch (ie->host_cmd) {
5514 case DRM_IOCTL_I915_GETPARAM:
5515 return do_ioctl_drm_i915_getparam(ie,
5516 (struct drm_i915_getparam *)buf_temp,
5517 fd, arg);
5518 default:
5519 return -TARGET_ENOSYS;
5520 }
5521}
5522
Chen Gange865b972020-06-05 09:32:21 +08005523#endif
5524
Shu-Chun Weng6addf062020-09-28 18:48:01 -07005525static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
5526 int fd, int cmd, abi_long arg)
5527{
5528 struct tun_filter *filter = (struct tun_filter *)buf_temp;
5529 struct tun_filter *target_filter;
5530 char *target_addr;
5531
5532 assert(ie->access == IOC_W);
5533
5534 target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
5535 if (!target_filter) {
5536 return -TARGET_EFAULT;
5537 }
5538 filter->flags = tswap16(target_filter->flags);
5539 filter->count = tswap16(target_filter->count);
5540 unlock_user(target_filter, arg, 0);
5541
5542 if (filter->count) {
5543 if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
5544 MAX_STRUCT_SIZE) {
5545 return -TARGET_EFAULT;
5546 }
5547
5548 target_addr = lock_user(VERIFY_READ,
5549 arg + offsetof(struct tun_filter, addr),
5550 filter->count * ETH_ALEN, 1);
5551 if (!target_addr) {
5552 return -TARGET_EFAULT;
5553 }
5554 memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
5555 unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
5556 }
5557
5558 return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
5559}
5560
Filip Bozuta79482e592020-06-19 14:47:27 +02005561IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00005562#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00005563 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5564#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5565 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
Helge Deller8cb762f2016-12-06 16:24:03 +01005566#define IOCTL_IGNORE(cmd) \
5567 { TARGET_ ## cmd, 0, #cmd },
bellard31e31b82003-02-18 22:55:36 +00005568#include "ioctls.h"
5569 { 0, 0, },
5570};
5571
pbrook53a59602006-03-25 19:31:22 +00005572/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00005573/* do_ioctl() Must return target values and target errnos. */
Laurent Vivier45c874e2015-06-16 00:35:28 +02005574static abi_long do_ioctl(int fd, int cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00005575{
5576 const IOCTLEntry *ie;
5577 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00005578 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005579 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00005580 int target_size;
5581 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00005582
5583 ie = ioctl_entries;
5584 for(;;) {
5585 if (ie->target_cmd == 0) {
Josh Kunz39be5352020-02-03 18:54:13 -08005586 qemu_log_mask(
5587 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
Thomas Weißschuh59d11722023-04-26 09:06:59 +02005588 return -TARGET_ENOTTY;
bellard31e31b82003-02-18 22:55:36 +00005589 }
5590 if (ie->target_cmd == cmd)
5591 break;
5592 ie++;
5593 }
5594 arg_type = ie->arg_type;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00005595 if (ie->do_ioctl) {
5596 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
Helge Deller8cb762f2016-12-06 16:24:03 +01005597 } else if (!ie->host_cmd) {
5598 /* Some architectures define BSD ioctls in their headers
5599 that are not implemented in Linux. */
Thomas Weißschuh59d11722023-04-26 09:06:59 +02005600 return -TARGET_ENOTTY;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00005601 }
5602
bellard31e31b82003-02-18 22:55:36 +00005603 switch(arg_type[0]) {
5604 case TYPE_NULL:
5605 /* no argument */
Peter Maydell49ca6f32016-06-06 19:58:14 +01005606 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
bellard31e31b82003-02-18 22:55:36 +00005607 break;
5608 case TYPE_PTRVOID:
5609 case TYPE_INT:
Filip Bozutac858e532020-01-15 20:36:47 +01005610 case TYPE_LONG:
5611 case TYPE_ULONG:
Peter Maydell49ca6f32016-06-06 19:58:14 +01005612 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
bellard31e31b82003-02-18 22:55:36 +00005613 break;
5614 case TYPE_PTR:
5615 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00005616 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00005617 switch(ie->access) {
5618 case IOC_R:
Peter Maydell49ca6f32016-06-06 19:58:14 +01005619 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
bellard31e31b82003-02-18 22:55:36 +00005620 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005621 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5622 if (!argptr)
5623 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005624 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5625 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00005626 }
5627 break;
5628 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00005629 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5630 if (!argptr)
5631 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005632 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5633 unlock_user(argptr, arg, 0);
Peter Maydell49ca6f32016-06-06 19:58:14 +01005634 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
bellard31e31b82003-02-18 22:55:36 +00005635 break;
5636 default:
5637 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00005638 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5639 if (!argptr)
5640 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005641 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5642 unlock_user(argptr, arg, 0);
Peter Maydell49ca6f32016-06-06 19:58:14 +01005643 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
bellard31e31b82003-02-18 22:55:36 +00005644 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005645 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5646 if (!argptr)
5647 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005648 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5649 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00005650 }
5651 break;
5652 }
5653 break;
5654 default:
Josh Kunz39be5352020-02-03 18:54:13 -08005655 qemu_log_mask(LOG_UNIMP,
5656 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5657 (long)cmd, arg_type[0]);
Thomas Weißschuh59d11722023-04-26 09:06:59 +02005658 ret = -TARGET_ENOTTY;
bellard31e31b82003-02-18 22:55:36 +00005659 break;
5660 }
5661 return ret;
5662}
5663
blueswir1b39bc502008-10-05 10:51:10 +00005664static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00005665 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
5666 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
5667 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
5668 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
5669 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
5670 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
5671 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
5672 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
5673 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
5674 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
5675 { TARGET_IXON, TARGET_IXON, IXON, IXON },
5676 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
5677 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
5678 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
Filip Bozutac218b4e2020-07-23 23:02:32 +02005679 { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
bellard31e31b82003-02-18 22:55:36 +00005680};
5681
blueswir1b39bc502008-10-05 10:51:10 +00005682static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00005683 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
5684 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
5685 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
5686 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
5687 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
5688 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
5689 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
5690 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
5691 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
5692 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
5693 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
5694 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
5695 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
5696 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
5697 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
5698 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
5699 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
5700 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
5701 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
5702 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
5703 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
5704 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
5705 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
5706 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
bellard31e31b82003-02-18 22:55:36 +00005707};
5708
blueswir1b39bc502008-10-05 10:51:10 +00005709static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00005710 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
5711 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
5712 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
5713 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
5714 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
5715 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
5716 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
5717 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
5718 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
5719 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
5720 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
5721 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
5722 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
5723 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
5724 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
5725 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
5726 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
5727 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
5728 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
5729 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
5730 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
5731 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
5732 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
5733 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
5734 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
5735 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
5736 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
5737 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
5738 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
5739 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
5740 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
bellard31e31b82003-02-18 22:55:36 +00005741};
5742
blueswir1b39bc502008-10-05 10:51:10 +00005743static const bitmask_transtbl lflag_tbl[] = {
Filip Bozutac218b4e2020-07-23 23:02:32 +02005744 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
5745 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
5746 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
5747 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
5748 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
5749 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
5750 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
5751 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
5752 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
5753 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
5754 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
5755 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
5756 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
5757 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
5758 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
5759 { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
bellard31e31b82003-02-18 22:55:36 +00005760};
5761
5762static void target_to_host_termios (void *dst, const void *src)
5763{
5764 struct host_termios *host = dst;
5765 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00005766
ths5fafdf22007-09-16 21:08:06 +00005767 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00005768 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00005769 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00005770 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00005771 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00005772 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00005773 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00005774 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
5775 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00005776
Arnaud Patard44607122009-04-21 17:39:08 +03005777 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00005778 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
5779 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00005780 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00005781 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00005782 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00005783 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00005784 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00005785 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00005786 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00005787 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5788 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00005789 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5790 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5791 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5792 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5793 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00005794 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00005795}
ths3b46e622007-09-17 08:09:54 +00005796
bellard31e31b82003-02-18 22:55:36 +00005797static void host_to_target_termios (void *dst, const void *src)
5798{
5799 struct target_termios *target = dst;
5800 const struct host_termios *host = src;
5801
ths5fafdf22007-09-16 21:08:06 +00005802 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00005803 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00005804 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00005805 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00005806 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00005807 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00005808 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00005809 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5810 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00005811
Arnaud Patard44607122009-04-21 17:39:08 +03005812 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00005813 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5814 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5815 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5816 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5817 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5818 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5819 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5820 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5821 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5822 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5823 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5824 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5825 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5826 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5827 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5828 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5829 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5830}
5831
blueswir18e853dc2008-10-05 10:49:32 +00005832static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00005833 .convert = { host_to_target_termios, target_to_host_termios },
5834 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
5835 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
Filip Bozuta888468d2020-07-23 23:02:33 +02005836 .print = print_termios,
bellard31e31b82003-02-18 22:55:36 +00005837};
5838
Richard Henderson4b840f92023-07-07 21:40:36 +01005839/* If the host does not provide these bits, they may be safely discarded. */
5840#ifndef MAP_SYNC
5841#define MAP_SYNC 0
5842#endif
5843#ifndef MAP_UNINITIALIZED
5844#define MAP_UNINITIALIZED 0
5845#endif
5846
Philippe Mathieu-Daudé180d4ef2021-05-17 07:52:43 +02005847static const bitmask_transtbl mmap_flags_tbl[] = {
Helge Deller541e1692017-10-31 13:53:58 +01005848 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
5849 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
5850 MAP_ANONYMOUS, MAP_ANONYMOUS },
5851 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
5852 MAP_GROWSDOWN, MAP_GROWSDOWN },
5853 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
5854 MAP_DENYWRITE, MAP_DENYWRITE },
5855 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
5856 MAP_EXECUTABLE, MAP_EXECUTABLE },
5857 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
5858 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
5859 MAP_NORESERVE, MAP_NORESERVE },
5860 { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
5861 /* MAP_STACK had been ignored by the kernel for quite some time.
5862 Recognize it for the target insofar as we do not want to pass
5863 it through to the host. */
5864 { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
Richard Henderson4b840f92023-07-07 21:40:36 +01005865 { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK },
5866 { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE },
5867 { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE,
5868 MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE },
5869 { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED,
5870 MAP_UNINITIALIZED, MAP_UNINITIALIZED },
bellard5286db72003-06-05 00:57:30 +00005871};
5872
Laurent Vivier9d12f792020-03-10 11:33:55 +01005873/*
Richard Henderson9ab8d0712023-08-07 18:22:35 -07005874 * Arrange for legacy / undefined architecture specific flags to be
5875 * ignored by mmap handling code.
5876 */
5877#ifndef TARGET_MAP_32BIT
5878#define TARGET_MAP_32BIT 0
5879#endif
5880#ifndef TARGET_MAP_HUGE_2MB
5881#define TARGET_MAP_HUGE_2MB 0
5882#endif
5883#ifndef TARGET_MAP_HUGE_1GB
5884#define TARGET_MAP_HUGE_1GB 0
5885#endif
5886
5887static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot,
5888 int target_flags, int fd, off_t offset)
5889{
5890 /*
5891 * The historical set of flags that all mmap types implicitly support.
5892 */
5893 enum {
5894 TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
5895 | TARGET_MAP_PRIVATE
5896 | TARGET_MAP_FIXED
5897 | TARGET_MAP_ANONYMOUS
5898 | TARGET_MAP_DENYWRITE
5899 | TARGET_MAP_EXECUTABLE
5900 | TARGET_MAP_UNINITIALIZED
5901 | TARGET_MAP_GROWSDOWN
5902 | TARGET_MAP_LOCKED
5903 | TARGET_MAP_NORESERVE
5904 | TARGET_MAP_POPULATE
5905 | TARGET_MAP_NONBLOCK
5906 | TARGET_MAP_STACK
5907 | TARGET_MAP_HUGETLB
5908 | TARGET_MAP_32BIT
5909 | TARGET_MAP_HUGE_2MB
5910 | TARGET_MAP_HUGE_1GB
5911 };
5912 int host_flags;
5913
5914 switch (target_flags & TARGET_MAP_TYPE) {
5915 case TARGET_MAP_PRIVATE:
5916 host_flags = MAP_PRIVATE;
5917 break;
5918 case TARGET_MAP_SHARED:
5919 host_flags = MAP_SHARED;
5920 break;
5921 case TARGET_MAP_SHARED_VALIDATE:
5922 /*
5923 * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
5924 * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
5925 */
5926 if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) {
5927 return -TARGET_EOPNOTSUPP;
5928 }
5929 host_flags = MAP_SHARED_VALIDATE;
5930 if (target_flags & TARGET_MAP_SYNC) {
5931 host_flags |= MAP_SYNC;
5932 }
5933 break;
5934 default:
5935 return -TARGET_EINVAL;
5936 }
5937 host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl);
5938
5939 return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset));
5940}
5941
5942/*
Laurent Vivier9d12f792020-03-10 11:33:55 +01005943 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
5944 * TARGET_I386 is defined if TARGET_X86_64 is defined
5945 */
bellard2ab83ea2003-06-15 19:56:46 +00005946#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00005947
5948/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00005949static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00005950
bellard03acab62007-11-11 14:57:14 +00005951static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00005952{
5953 int size;
pbrook53a59602006-03-25 19:31:22 +00005954 void *p;
bellard6dbad632003-03-16 18:05:05 +00005955
5956 if (!ldt_table)
5957 return 0;
5958 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5959 if (size > bytecount)
5960 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00005961 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5962 if (!p)
bellard03acab62007-11-11 14:57:14 +00005963 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005964 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00005965 memcpy(p, ldt_table, size);
5966 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00005967 return size;
5968}
5969
5970/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00005971static abi_long write_ldt(CPUX86State *env,
5972 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00005973{
5974 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00005975 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00005976 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00005977 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00005978 uint32_t *lp, entry_1, entry_2;
5979
5980 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00005981 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00005982 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00005983 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005984 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005985 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00005986 ldt_info.limit = tswap32(target_ldt_info->limit);
5987 ldt_info.flags = tswap32(target_ldt_info->flags);
5988 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00005989
bellard6dbad632003-03-16 18:05:05 +00005990 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00005991 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00005992 seg_32bit = ldt_info.flags & 1;
5993 contents = (ldt_info.flags >> 1) & 3;
5994 read_exec_only = (ldt_info.flags >> 3) & 1;
5995 limit_in_pages = (ldt_info.flags >> 4) & 1;
5996 seg_not_present = (ldt_info.flags >> 5) & 1;
5997 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00005998#ifdef TARGET_ABI32
5999 lm = 0;
6000#else
6001 lm = (ldt_info.flags >> 7) & 1;
6002#endif
bellard6dbad632003-03-16 18:05:05 +00006003 if (contents == 3) {
6004 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00006005 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00006006 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00006007 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00006008 }
6009 /* allocate the LDT */
6010 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00006011 env->ldt.base = target_mmap(0,
6012 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
6013 PROT_READ|PROT_WRITE,
6014 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
6015 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00006016 return -TARGET_ENOMEM;
Richard Henderson3e8f1622021-02-12 10:48:43 -08006017 memset(g2h_untagged(env->ldt.base), 0,
balroge4415702008-11-10 02:55:33 +00006018 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00006019 env->ldt.limit = 0xffff;
Richard Henderson3e8f1622021-02-12 10:48:43 -08006020 ldt_table = g2h_untagged(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00006021 }
6022
6023 /* NOTE: same code as Linux kernel */
6024 /* Allow LDTs to be cleared by the user. */
6025 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6026 if (oldmode ||
6027 (contents == 0 &&
6028 read_exec_only == 1 &&
6029 seg_32bit == 0 &&
6030 limit_in_pages == 0 &&
6031 seg_not_present == 1 &&
6032 useable == 0 )) {
6033 entry_1 = 0;
6034 entry_2 = 0;
6035 goto install;
6036 }
6037 }
ths3b46e622007-09-17 08:09:54 +00006038
bellard6dbad632003-03-16 18:05:05 +00006039 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6040 (ldt_info.limit & 0x0ffff);
6041 entry_2 = (ldt_info.base_addr & 0xff000000) |
6042 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6043 (ldt_info.limit & 0xf0000) |
6044 ((read_exec_only ^ 1) << 9) |
6045 (contents << 10) |
6046 ((seg_not_present ^ 1) << 15) |
6047 (seg_32bit << 22) |
6048 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00006049 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00006050 0x7000;
6051 if (!oldmode)
6052 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00006053
bellard6dbad632003-03-16 18:05:05 +00006054 /* Install the new entry ... */
6055install:
6056 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
6057 lp[0] = tswap32(entry_1);
6058 lp[1] = tswap32(entry_2);
6059 return 0;
6060}
6061
6062/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00006063static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
6064 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00006065{
bellard03acab62007-11-11 14:57:14 +00006066 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00006067
bellard6dbad632003-03-16 18:05:05 +00006068 switch (func) {
6069 case 0:
6070 ret = read_ldt(ptr, bytecount);
6071 break;
6072 case 1:
6073 ret = write_ldt(env, ptr, bytecount, 1);
6074 break;
6075 case 0x11:
6076 ret = write_ldt(env, ptr, bytecount, 0);
6077 break;
bellard03acab62007-11-11 14:57:14 +00006078 default:
6079 ret = -TARGET_ENOSYS;
6080 break;
bellard6dbad632003-03-16 18:05:05 +00006081 }
6082 return ret;
6083}
bellard1b6b0292003-03-22 17:31:38 +00006084
Laurent Vivier9d12f792020-03-10 11:33:55 +01006085#if defined(TARGET_ABI32)
Alexander Grafbc22eb42013-07-16 18:44:58 +01006086abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00006087{
Richard Henderson3e8f1622021-02-12 10:48:43 -08006088 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
bellard8d18e892007-11-14 15:18:40 +00006089 struct target_modify_ldt_ldt_s ldt_info;
6090 struct target_modify_ldt_ldt_s *target_ldt_info;
6091 int seg_32bit, contents, read_exec_only, limit_in_pages;
6092 int seg_not_present, useable, lm;
6093 uint32_t *lp, entry_1, entry_2;
6094 int i;
6095
6096 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6097 if (!target_ldt_info)
6098 return -TARGET_EFAULT;
6099 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006100 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00006101 ldt_info.limit = tswap32(target_ldt_info->limit);
6102 ldt_info.flags = tswap32(target_ldt_info->flags);
6103 if (ldt_info.entry_number == -1) {
6104 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
6105 if (gdt_table[i] == 0) {
6106 ldt_info.entry_number = i;
6107 target_ldt_info->entry_number = tswap32(i);
6108 break;
6109 }
6110 }
6111 }
6112 unlock_user_struct(target_ldt_info, ptr, 1);
6113
6114 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
6115 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
6116 return -TARGET_EINVAL;
6117 seg_32bit = ldt_info.flags & 1;
6118 contents = (ldt_info.flags >> 1) & 3;
6119 read_exec_only = (ldt_info.flags >> 3) & 1;
6120 limit_in_pages = (ldt_info.flags >> 4) & 1;
6121 seg_not_present = (ldt_info.flags >> 5) & 1;
6122 useable = (ldt_info.flags >> 6) & 1;
6123#ifdef TARGET_ABI32
6124 lm = 0;
6125#else
6126 lm = (ldt_info.flags >> 7) & 1;
6127#endif
6128
6129 if (contents == 3) {
6130 if (seg_not_present == 0)
6131 return -TARGET_EINVAL;
6132 }
6133
6134 /* NOTE: same code as Linux kernel */
6135 /* Allow LDTs to be cleared by the user. */
6136 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6137 if ((contents == 0 &&
6138 read_exec_only == 1 &&
6139 seg_32bit == 0 &&
6140 limit_in_pages == 0 &&
6141 seg_not_present == 1 &&
6142 useable == 0 )) {
6143 entry_1 = 0;
6144 entry_2 = 0;
6145 goto install;
6146 }
6147 }
6148
6149 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6150 (ldt_info.limit & 0x0ffff);
6151 entry_2 = (ldt_info.base_addr & 0xff000000) |
6152 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6153 (ldt_info.limit & 0xf0000) |
6154 ((read_exec_only ^ 1) << 9) |
6155 (contents << 10) |
6156 ((seg_not_present ^ 1) << 15) |
6157 (seg_32bit << 22) |
6158 (limit_in_pages << 23) |
6159 (useable << 20) |
6160 (lm << 21) |
6161 0x7000;
6162
6163 /* Install the new entry ... */
6164install:
6165 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
6166 lp[0] = tswap32(entry_1);
6167 lp[1] = tswap32(entry_2);
6168 return 0;
6169}
6170
blueswir18fcd3692008-08-17 20:26:25 +00006171static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00006172{
6173 struct target_modify_ldt_ldt_s *target_ldt_info;
Richard Henderson3e8f1622021-02-12 10:48:43 -08006174 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
bellard8d18e892007-11-14 15:18:40 +00006175 uint32_t base_addr, limit, flags;
6176 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
6177 int seg_not_present, useable, lm;
6178 uint32_t *lp, entry_1, entry_2;
6179
6180 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6181 if (!target_ldt_info)
6182 return -TARGET_EFAULT;
6183 idx = tswap32(target_ldt_info->entry_number);
6184 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
6185 idx > TARGET_GDT_ENTRY_TLS_MAX) {
6186 unlock_user_struct(target_ldt_info, ptr, 1);
6187 return -TARGET_EINVAL;
6188 }
6189 lp = (uint32_t *)(gdt_table + idx);
6190 entry_1 = tswap32(lp[0]);
6191 entry_2 = tswap32(lp[1]);
6192
6193 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
6194 contents = (entry_2 >> 10) & 3;
6195 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
6196 seg_32bit = (entry_2 >> 22) & 1;
6197 limit_in_pages = (entry_2 >> 23) & 1;
6198 useable = (entry_2 >> 20) & 1;
6199#ifdef TARGET_ABI32
6200 lm = 0;
6201#else
6202 lm = (entry_2 >> 21) & 1;
6203#endif
6204 flags = (seg_32bit << 0) | (contents << 1) |
6205 (read_exec_only << 3) | (limit_in_pages << 4) |
6206 (seg_not_present << 5) | (useable << 6) | (lm << 7);
6207 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
6208 base_addr = (entry_1 >> 16) |
6209 (entry_2 & 0xff000000) |
6210 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006211 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00006212 target_ldt_info->limit = tswap32(limit);
6213 target_ldt_info->flags = tswap32(flags);
6214 unlock_user_struct(target_ldt_info, ptr, 1);
6215 return 0;
6216}
6217
Laurent Vivier9d12f792020-03-10 11:33:55 +01006218abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6219{
Helge Deller538fabc2020-04-25 00:00:33 +02006220 return -TARGET_ENOSYS;
Laurent Vivier9d12f792020-03-10 11:33:55 +01006221}
6222#else
Peter Maydell2667e712013-07-16 18:44:59 +01006223abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00006224{
Juan Quintela1add8692011-06-16 17:37:09 +01006225 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00006226 abi_ulong val;
6227 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01006228
bellardd2fd1af2007-11-14 18:08:56 +00006229 switch(code) {
6230 case TARGET_ARCH_SET_GS:
6231 case TARGET_ARCH_SET_FS:
6232 if (code == TARGET_ARCH_SET_GS)
6233 idx = R_GS;
6234 else
6235 idx = R_FS;
6236 cpu_x86_load_seg(env, idx, 0);
6237 env->segs[idx].base = addr;
6238 break;
6239 case TARGET_ARCH_GET_GS:
6240 case TARGET_ARCH_GET_FS:
6241 if (code == TARGET_ARCH_GET_GS)
6242 idx = R_GS;
6243 else
6244 idx = R_FS;
6245 val = env->segs[idx].base;
6246 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01006247 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00006248 break;
6249 default:
6250 ret = -TARGET_EINVAL;
6251 break;
6252 }
Juan Quintela1add8692011-06-16 17:37:09 +01006253 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00006254}
Laurent Vivier9d12f792020-03-10 11:33:55 +01006255#endif /* defined(TARGET_ABI32 */
bellard2ab83ea2003-06-15 19:56:46 +00006256#endif /* defined(TARGET_I386) */
6257
Richard Henderson87e9bf22021-12-27 07:01:22 -08006258/*
6259 * These constants are generic. Supply any that are missing from the host.
6260 */
6261#ifndef PR_SET_NAME
6262# define PR_SET_NAME 15
6263# define PR_GET_NAME 16
6264#endif
6265#ifndef PR_SET_FP_MODE
6266# define PR_SET_FP_MODE 45
6267# define PR_GET_FP_MODE 46
6268# define PR_FP_MODE_FR (1 << 0)
6269# define PR_FP_MODE_FRE (1 << 1)
6270#endif
6271#ifndef PR_SVE_SET_VL
6272# define PR_SVE_SET_VL 50
6273# define PR_SVE_GET_VL 51
6274# define PR_SVE_VL_LEN_MASK 0xffff
6275# define PR_SVE_VL_INHERIT (1 << 17)
6276#endif
6277#ifndef PR_PAC_RESET_KEYS
6278# define PR_PAC_RESET_KEYS 54
6279# define PR_PAC_APIAKEY (1 << 0)
6280# define PR_PAC_APIBKEY (1 << 1)
6281# define PR_PAC_APDAKEY (1 << 2)
6282# define PR_PAC_APDBKEY (1 << 3)
6283# define PR_PAC_APGAKEY (1 << 4)
6284#endif
6285#ifndef PR_SET_TAGGED_ADDR_CTRL
6286# define PR_SET_TAGGED_ADDR_CTRL 55
6287# define PR_GET_TAGGED_ADDR_CTRL 56
6288# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6289#endif
6290#ifndef PR_MTE_TCF_SHIFT
6291# define PR_MTE_TCF_SHIFT 1
6292# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6293# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6294# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6295# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6296# define PR_MTE_TAG_SHIFT 3
6297# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6298#endif
Richard Henderson220717a2021-12-27 07:01:23 -08006299#ifndef PR_SET_IO_FLUSHER
6300# define PR_SET_IO_FLUSHER 57
6301# define PR_GET_IO_FLUSHER 58
6302#endif
6303#ifndef PR_SET_SYSCALL_USER_DISPATCH
6304# define PR_SET_SYSCALL_USER_DISPATCH 59
6305#endif
Richard Henderson24d87c12022-07-08 20:45:37 +05306306#ifndef PR_SME_SET_VL
6307# define PR_SME_SET_VL 63
6308# define PR_SME_GET_VL 64
6309# define PR_SME_VL_LEN_MASK 0xffff
6310# define PR_SME_VL_INHERIT (1 << 17)
6311#endif
Richard Henderson87e9bf22021-12-27 07:01:22 -08006312
6313#include "target_prctl.h"
6314
6315static abi_long do_prctl_inval0(CPUArchState *env)
6316{
6317 return -TARGET_EINVAL;
6318}
6319
6320static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
6321{
6322 return -TARGET_EINVAL;
6323}
6324
6325#ifndef do_prctl_get_fp_mode
6326#define do_prctl_get_fp_mode do_prctl_inval0
6327#endif
6328#ifndef do_prctl_set_fp_mode
6329#define do_prctl_set_fp_mode do_prctl_inval1
6330#endif
Richard Hendersonfd72f5d2022-07-08 20:45:36 +05306331#ifndef do_prctl_sve_get_vl
6332#define do_prctl_sve_get_vl do_prctl_inval0
Richard Henderson87e9bf22021-12-27 07:01:22 -08006333#endif
Richard Hendersonfd72f5d2022-07-08 20:45:36 +05306334#ifndef do_prctl_sve_set_vl
6335#define do_prctl_sve_set_vl do_prctl_inval1
Richard Henderson87e9bf22021-12-27 07:01:22 -08006336#endif
6337#ifndef do_prctl_reset_keys
6338#define do_prctl_reset_keys do_prctl_inval1
6339#endif
6340#ifndef do_prctl_set_tagged_addr_ctrl
6341#define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6342#endif
6343#ifndef do_prctl_get_tagged_addr_ctrl
6344#define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6345#endif
Richard Henderson6e8dcac2021-12-27 07:01:24 -08006346#ifndef do_prctl_get_unalign
6347#define do_prctl_get_unalign do_prctl_inval1
6348#endif
6349#ifndef do_prctl_set_unalign
6350#define do_prctl_set_unalign do_prctl_inval1
6351#endif
Richard Henderson24d87c12022-07-08 20:45:37 +05306352#ifndef do_prctl_sme_get_vl
6353#define do_prctl_sme_get_vl do_prctl_inval0
6354#endif
6355#ifndef do_prctl_sme_set_vl
6356#define do_prctl_sme_set_vl do_prctl_inval1
6357#endif
Richard Henderson87e9bf22021-12-27 07:01:22 -08006358
6359static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
6360 abi_long arg3, abi_long arg4, abi_long arg5)
6361{
6362 abi_long ret;
6363
6364 switch (option) {
6365 case PR_GET_PDEATHSIG:
6366 {
6367 int deathsig;
6368 ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
6369 arg3, arg4, arg5));
Richard Henderson1edebb32022-01-06 14:57:36 -08006370 if (!is_error(ret) &&
6371 put_user_s32(host_to_target_signal(deathsig), arg2)) {
Richard Henderson87e9bf22021-12-27 07:01:22 -08006372 return -TARGET_EFAULT;
6373 }
6374 return ret;
6375 }
Richard Hendersonf746c652022-01-06 14:57:37 -08006376 case PR_SET_PDEATHSIG:
6377 return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
6378 arg3, arg4, arg5));
Richard Henderson87e9bf22021-12-27 07:01:22 -08006379 case PR_GET_NAME:
6380 {
6381 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
6382 if (!name) {
6383 return -TARGET_EFAULT;
6384 }
6385 ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
6386 arg3, arg4, arg5));
6387 unlock_user(name, arg2, 16);
6388 return ret;
6389 }
6390 case PR_SET_NAME:
6391 {
6392 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
6393 if (!name) {
6394 return -TARGET_EFAULT;
6395 }
6396 ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
6397 arg3, arg4, arg5));
6398 unlock_user(name, arg2, 0);
6399 return ret;
6400 }
6401 case PR_GET_FP_MODE:
6402 return do_prctl_get_fp_mode(env);
6403 case PR_SET_FP_MODE:
6404 return do_prctl_set_fp_mode(env, arg2);
6405 case PR_SVE_GET_VL:
Richard Hendersonfd72f5d2022-07-08 20:45:36 +05306406 return do_prctl_sve_get_vl(env);
Richard Henderson87e9bf22021-12-27 07:01:22 -08006407 case PR_SVE_SET_VL:
Richard Hendersonfd72f5d2022-07-08 20:45:36 +05306408 return do_prctl_sve_set_vl(env, arg2);
Richard Henderson24d87c12022-07-08 20:45:37 +05306409 case PR_SME_GET_VL:
6410 return do_prctl_sme_get_vl(env);
6411 case PR_SME_SET_VL:
6412 return do_prctl_sme_set_vl(env, arg2);
Richard Henderson87e9bf22021-12-27 07:01:22 -08006413 case PR_PAC_RESET_KEYS:
6414 if (arg3 || arg4 || arg5) {
6415 return -TARGET_EINVAL;
6416 }
6417 return do_prctl_reset_keys(env, arg2);
6418 case PR_SET_TAGGED_ADDR_CTRL:
6419 if (arg3 || arg4 || arg5) {
6420 return -TARGET_EINVAL;
6421 }
6422 return do_prctl_set_tagged_addr_ctrl(env, arg2);
6423 case PR_GET_TAGGED_ADDR_CTRL:
6424 if (arg2 || arg3 || arg4 || arg5) {
6425 return -TARGET_EINVAL;
6426 }
6427 return do_prctl_get_tagged_addr_ctrl(env);
Richard Henderson220717a2021-12-27 07:01:23 -08006428
Richard Henderson6e8dcac2021-12-27 07:01:24 -08006429 case PR_GET_UNALIGN:
6430 return do_prctl_get_unalign(env, arg2);
6431 case PR_SET_UNALIGN:
6432 return do_prctl_set_unalign(env, arg2);
6433
Richard Henderson4f4e5562022-01-06 14:57:38 -08006434 case PR_CAP_AMBIENT:
6435 case PR_CAPBSET_READ:
6436 case PR_CAPBSET_DROP:
Richard Henderson220717a2021-12-27 07:01:23 -08006437 case PR_GET_DUMPABLE:
6438 case PR_SET_DUMPABLE:
6439 case PR_GET_KEEPCAPS:
6440 case PR_SET_KEEPCAPS:
Richard Henderson4f4e5562022-01-06 14:57:38 -08006441 case PR_GET_SECUREBITS:
6442 case PR_SET_SECUREBITS:
Richard Henderson220717a2021-12-27 07:01:23 -08006443 case PR_GET_TIMING:
6444 case PR_SET_TIMING:
6445 case PR_GET_TIMERSLACK:
6446 case PR_SET_TIMERSLACK:
6447 case PR_MCE_KILL:
6448 case PR_MCE_KILL_GET:
6449 case PR_GET_NO_NEW_PRIVS:
6450 case PR_SET_NO_NEW_PRIVS:
6451 case PR_GET_IO_FLUSHER:
6452 case PR_SET_IO_FLUSHER:
6453 /* Some prctl options have no pointer arguments and we can pass on. */
6454 return get_errno(prctl(option, arg2, arg3, arg4, arg5));
6455
6456 case PR_GET_CHILD_SUBREAPER:
6457 case PR_SET_CHILD_SUBREAPER:
6458 case PR_GET_SPECULATION_CTRL:
6459 case PR_SET_SPECULATION_CTRL:
6460 case PR_GET_TID_ADDRESS:
6461 /* TODO */
6462 return -TARGET_EINVAL;
6463
6464 case PR_GET_FPEXC:
6465 case PR_SET_FPEXC:
6466 /* Was used for SPE on PowerPC. */
6467 return -TARGET_EINVAL;
6468
6469 case PR_GET_ENDIAN:
6470 case PR_SET_ENDIAN:
6471 case PR_GET_FPEMU:
6472 case PR_SET_FPEMU:
6473 case PR_SET_MM:
Richard Henderson87e9bf22021-12-27 07:01:22 -08006474 case PR_GET_SECCOMP:
6475 case PR_SET_SECCOMP:
Richard Henderson220717a2021-12-27 07:01:23 -08006476 case PR_SET_SYSCALL_USER_DISPATCH:
6477 case PR_GET_THP_DISABLE:
6478 case PR_SET_THP_DISABLE:
6479 case PR_GET_TSC:
6480 case PR_SET_TSC:
Richard Henderson220717a2021-12-27 07:01:23 -08006481 /* Disable to prevent the target disabling stuff we need. */
Richard Henderson87e9bf22021-12-27 07:01:22 -08006482 return -TARGET_EINVAL;
Richard Henderson220717a2021-12-27 07:01:23 -08006483
Richard Henderson87e9bf22021-12-27 07:01:22 -08006484 default:
Richard Henderson220717a2021-12-27 07:01:23 -08006485 qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
6486 option);
6487 return -TARGET_EINVAL;
Richard Henderson87e9bf22021-12-27 07:01:22 -08006488 }
6489}
6490
Riku Voipio05098a92011-03-04 15:27:29 +02006491#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00006492
pbrookd865bab2008-06-07 22:12:17 +00006493
6494static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6495typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006496 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00006497 pthread_mutex_t mutex;
6498 pthread_cond_t cond;
6499 pthread_t thread;
6500 uint32_t tid;
6501 abi_ulong child_tidptr;
6502 abi_ulong parent_tidptr;
6503 sigset_t sigmask;
6504} new_thread_info;
6505
6506static void *clone_func(void *arg)
6507{
6508 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01006509 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01006510 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03006511 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00006512
Emilio G. Cota70903762015-08-23 20:23:41 -04006513 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04006514 tcg_register_thread();
pbrookd865bab2008-06-07 22:12:17 +00006515 env = info->env;
Richard Henderson29a0af62019-03-22 16:07:18 -07006516 cpu = env_cpu(env);
Andreas Färbera2247f82013-06-09 19:47:04 +02006517 thread_cpu = cpu;
Andreas Färber0429a972013-08-26 18:14:44 +02006518 ts = (TaskState *)cpu->opaque;
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +00006519 info->tid = sys_gettid();
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03006520 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00006521 if (info->child_tidptr)
6522 put_user_u32(info->tid, info->child_tidptr);
6523 if (info->parent_tidptr)
6524 put_user_u32(info->tid, info->parent_tidptr);
Richard Henderson5ebdd772019-03-14 13:10:53 -07006525 qemu_guest_random_seed_thread_part2(cpu->random_seed);
pbrookd865bab2008-06-07 22:12:17 +00006526 /* Enable signals. */
6527 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6528 /* Signal to the parent that we're ready. */
6529 pthread_mutex_lock(&info->mutex);
6530 pthread_cond_broadcast(&info->cond);
6531 pthread_mutex_unlock(&info->mutex);
Emilio G. Cotaa4dd3d52017-10-18 18:01:41 -04006532 /* Wait until the parent has finished initializing the tls state. */
pbrookd865bab2008-06-07 22:12:17 +00006533 pthread_mutex_lock(&clone_lock);
6534 pthread_mutex_unlock(&clone_lock);
6535 cpu_loop(env);
6536 /* never exits */
6537 return NULL;
6538}
bellard1b6b0292003-03-22 17:31:38 +00006539
ths0da46a62007-10-20 20:23:07 +00006540/* do_fork() Must return host values and target errnos (unlike most
6541 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01006542static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00006543 abi_ulong parent_tidptr, target_ulong newtls,
6544 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00006545{
Richard Henderson29a0af62019-03-22 16:07:18 -07006546 CPUState *cpu = env_cpu(env);
bellard1b6b0292003-03-22 17:31:38 +00006547 int ret;
bellard5cd43932003-03-29 16:54:36 +00006548 TaskState *ts;
Andreas Färber0429a972013-08-26 18:14:44 +02006549 CPUState *new_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01006550 CPUArchState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00006551 sigset_t sigmask;
ths3b46e622007-09-17 08:09:54 +00006552
Peter Maydell5ea2fc82016-08-02 18:41:27 +01006553 flags &= ~CLONE_IGNORED_FLAGS;
6554
balrog436d1242008-09-21 02:39:45 +00006555 /* Emulate vfork() with fork() */
6556 if (flags & CLONE_VFORK)
6557 flags &= ~(CLONE_VFORK | CLONE_VM);
6558
bellard1b6b0292003-03-22 17:31:38 +00006559 if (flags & CLONE_VM) {
Andreas Färber0429a972013-08-26 18:14:44 +02006560 TaskState *parent_ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00006561 new_thread_info info;
6562 pthread_attr_t attr;
Peter Maydell24cb36a2013-07-16 18:45:00 +01006563
Peter Maydell5ea2fc82016-08-02 18:41:27 +01006564 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6565 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6566 return -TARGET_EINVAL;
6567 }
6568
Markus Armbrusterc78d65e2015-09-14 13:53:03 +02006569 ts = g_new0(TaskState, 1);
pbrook624f7972008-05-31 16:11:38 +00006570 init_task_state(ts);
Max Filippov73a988d2018-03-30 06:19:58 -07006571
6572 /* Grab a mutex so that thread setup appears atomic. */
6573 pthread_mutex_lock(&clone_lock);
6574
Richard Henderson6cc9d672021-03-01 19:21:08 -08006575 /*
6576 * If this is our first additional thread, we need to ensure we
6577 * generate code for parallel execution and flush old translations.
6578 * Do this now so that the copy gets CF_PARALLEL too.
6579 */
6580 if (!(cpu->tcg_cflags & CF_PARALLEL)) {
6581 cpu->tcg_cflags |= CF_PARALLEL;
6582 tb_flush(cpu);
6583 }
6584
bellard1b6b0292003-03-22 17:31:38 +00006585 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00006586 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00006587 /* Init regs that differ from the parent. */
Richard Henderson608999d2019-11-06 12:33:15 +01006588 cpu_clone_regs_child(new_env, newsp, flags);
Richard Henderson07a6ecf2019-11-06 12:33:16 +01006589 cpu_clone_regs_parent(env, flags);
Richard Henderson29a0af62019-03-22 16:07:18 -07006590 new_cpu = env_cpu(new_env);
Andreas Färber0429a972013-08-26 18:14:44 +02006591 new_cpu->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03006592 ts->bprm = parent_ts->bprm;
6593 ts->info = parent_ts->info;
Peter Maydell3d3efba2016-05-27 15:51:49 +01006594 ts->signal_mask = parent_ts->signal_mask;
pbrookd865bab2008-06-07 22:12:17 +00006595
Peter Maydell7cfbd382016-08-02 18:41:26 +01006596 if (flags & CLONE_CHILD_CLEARTID) {
pbrookc2764712009-03-07 15:24:59 +00006597 ts->child_tidptr = child_tidptr;
6598 }
6599
Peter Maydell7cfbd382016-08-02 18:41:26 +01006600 if (flags & CLONE_SETTLS) {
pbrookd865bab2008-06-07 22:12:17 +00006601 cpu_set_tls (new_env, newtls);
Peter Maydell7cfbd382016-08-02 18:41:26 +01006602 }
pbrookd865bab2008-06-07 22:12:17 +00006603
pbrookd865bab2008-06-07 22:12:17 +00006604 memset(&info, 0, sizeof(info));
6605 pthread_mutex_init(&info.mutex, NULL);
6606 pthread_mutex_lock(&info.mutex);
6607 pthread_cond_init(&info.cond, NULL);
6608 info.env = new_env;
Peter Maydell7cfbd382016-08-02 18:41:26 +01006609 if (flags & CLONE_CHILD_SETTID) {
pbrookd865bab2008-06-07 22:12:17 +00006610 info.child_tidptr = child_tidptr;
Peter Maydell7cfbd382016-08-02 18:41:26 +01006611 }
6612 if (flags & CLONE_PARENT_SETTID) {
pbrookd865bab2008-06-07 22:12:17 +00006613 info.parent_tidptr = parent_tidptr;
Peter Maydell7cfbd382016-08-02 18:41:26 +01006614 }
pbrookd865bab2008-06-07 22:12:17 +00006615
6616 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07006617 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6618 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00006619 /* It is not safe to deliver signals until the child has finished
6620 initializing, so temporarily block all signals. */
6621 sigfillset(&sigmask);
6622 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
Richard Henderson5ebdd772019-03-14 13:10:53 -07006623 cpu->random_seed = qemu_guest_random_seed_thread_part1();
pbrookd865bab2008-06-07 22:12:17 +00006624
6625 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00006626 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00006627
6628 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6629 pthread_attr_destroy(&attr);
6630 if (ret == 0) {
6631 /* Wait for the child to initialize. */
6632 pthread_cond_wait(&info.cond, &info.mutex);
6633 ret = info.tid;
pbrookd865bab2008-06-07 22:12:17 +00006634 } else {
6635 ret = -1;
6636 }
6637 pthread_mutex_unlock(&info.mutex);
6638 pthread_cond_destroy(&info.cond);
6639 pthread_mutex_destroy(&info.mutex);
6640 pthread_mutex_unlock(&clone_lock);
bellard1b6b0292003-03-22 17:31:38 +00006641 } else {
6642 /* if no CLONE_VM, we consider it is a fork */
Peter Maydell5ea2fc82016-08-02 18:41:27 +01006643 if (flags & CLONE_INVALID_FORK_FLAGS) {
6644 return -TARGET_EINVAL;
6645 }
6646
6647 /* We can't support custom termination signals */
6648 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
Timothy E Baldwin93b4eff2015-08-31 00:26:21 +01006649 return -TARGET_EINVAL;
6650 }
Timothy E Baldwin7d92d342016-05-27 15:51:58 +01006651
Helge Deller895ce8b2022-11-29 12:08:20 +01006652#if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
6653 if (flags & CLONE_PIDFD) {
6654 return -TARGET_EINVAL;
6655 }
6656#endif
6657
6658 /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
6659 if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
6660 return -TARGET_EINVAL;
6661 }
6662
Timothy E Baldwin7d92d342016-05-27 15:51:58 +01006663 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01006664 return -QEMU_ERESTARTSYS;
Timothy E Baldwin7d92d342016-05-27 15:51:58 +01006665 }
6666
pbrookd865bab2008-06-07 22:12:17 +00006667 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00006668 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00006669 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00006670 /* Child Process. */
Richard Henderson608999d2019-11-06 12:33:15 +01006671 cpu_clone_regs_child(env, newsp, flags);
pbrookd865bab2008-06-07 22:12:17 +00006672 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00006673 /* There is a race condition here. The parent process could
6674 theoretically read the TID in the child process before the child
6675 tid is set. This would require using either ptrace
6676 (not implemented) or having *_tidptr to point at a shared memory
6677 mapping. We can't repeat the spinlock hack used above because
6678 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00006679 if (flags & CLONE_CHILD_SETTID)
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +00006680 put_user_u32(sys_gettid(), child_tidptr);
pbrookd865bab2008-06-07 22:12:17 +00006681 if (flags & CLONE_PARENT_SETTID)
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +00006682 put_user_u32(sys_gettid(), parent_tidptr);
Andreas Färber0429a972013-08-26 18:14:44 +02006683 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00006684 if (flags & CLONE_SETTLS)
6685 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00006686 if (flags & CLONE_CHILD_CLEARTID)
6687 ts->child_tidptr = child_tidptr;
pbrookd865bab2008-06-07 22:12:17 +00006688 } else {
Richard Henderson07a6ecf2019-11-06 12:33:16 +01006689 cpu_clone_regs_parent(env, flags);
Helge Deller895ce8b2022-11-29 12:08:20 +01006690 if (flags & CLONE_PIDFD) {
6691 int pid_fd = 0;
6692#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
6693 int pid_child = ret;
6694 pid_fd = pidfd_open(pid_child, 0);
6695 if (pid_fd >= 0) {
6696 fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
6697 | FD_CLOEXEC);
6698 } else {
6699 pid_fd = 0;
6700 }
6701#endif
6702 put_user_u32(pid_fd, parent_tidptr);
6703 }
pbrookd865bab2008-06-07 22:12:17 +00006704 fork_end(0);
6705 }
Ilya Leoshkevich7de08162023-02-14 15:08:26 +01006706 g_assert(!cpu_in_exclusive_context(cpu));
bellard1b6b0292003-03-22 17:31:38 +00006707 }
6708 return ret;
6709}
6710
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006711/* warning : doesn't handle linux specific flags... */
6712static int target_to_host_fcntl_cmd(int cmd)
6713{
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006714 int ret;
6715
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006716 switch(cmd) {
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006717 case TARGET_F_DUPFD:
6718 case TARGET_F_GETFD:
6719 case TARGET_F_SETFD:
6720 case TARGET_F_GETFL:
6721 case TARGET_F_SETFL:
Andreas Schwab2d92c682020-05-25 09:59:28 +02006722 case TARGET_F_OFD_GETLK:
6723 case TARGET_F_OFD_SETLK:
6724 case TARGET_F_OFD_SETLKW:
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006725 ret = cmd;
6726 break;
6727 case TARGET_F_GETLK:
6728 ret = F_GETLK64;
6729 break;
6730 case TARGET_F_SETLK:
6731 ret = F_SETLK64;
6732 break;
6733 case TARGET_F_SETLKW:
6734 ret = F_SETLKW64;
6735 break;
6736 case TARGET_F_GETOWN:
6737 ret = F_GETOWN;
6738 break;
6739 case TARGET_F_SETOWN:
6740 ret = F_SETOWN;
6741 break;
6742 case TARGET_F_GETSIG:
6743 ret = F_GETSIG;
6744 break;
6745 case TARGET_F_SETSIG:
6746 ret = F_SETSIG;
6747 break;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006748#if TARGET_ABI_BITS == 32
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006749 case TARGET_F_GETLK64:
6750 ret = F_GETLK64;
6751 break;
6752 case TARGET_F_SETLK64:
6753 ret = F_SETLK64;
6754 break;
6755 case TARGET_F_SETLKW64:
6756 ret = F_SETLKW64;
6757 break;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006758#endif
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006759 case TARGET_F_SETLEASE:
6760 ret = F_SETLEASE;
6761 break;
6762 case TARGET_F_GETLEASE:
6763 ret = F_GETLEASE;
6764 break;
malcfbd5de92009-09-06 06:31:59 +04006765#ifdef F_DUPFD_CLOEXEC
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006766 case TARGET_F_DUPFD_CLOEXEC:
6767 ret = F_DUPFD_CLOEXEC;
6768 break;
malcfbd5de92009-09-06 06:31:59 +04006769#endif
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006770 case TARGET_F_NOTIFY:
6771 ret = F_NOTIFY;
6772 break;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006773#ifdef F_GETOWN_EX
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006774 case TARGET_F_GETOWN_EX:
6775 ret = F_GETOWN_EX;
6776 break;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006777#endif
6778#ifdef F_SETOWN_EX
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006779 case TARGET_F_SETOWN_EX:
6780 ret = F_SETOWN_EX;
6781 break;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006782#endif
Peter Maydellddf31aa2016-06-30 17:33:49 +01006783#ifdef F_SETPIPE_SZ
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006784 case TARGET_F_SETPIPE_SZ:
6785 ret = F_SETPIPE_SZ;
6786 break;
6787 case TARGET_F_GETPIPE_SZ:
6788 ret = F_GETPIPE_SZ;
6789 break;
Peter Maydellddf31aa2016-06-30 17:33:49 +01006790#endif
Shu-Chun Weng2bb963f2020-12-18 11:32:10 -08006791#ifdef F_ADD_SEALS
6792 case TARGET_F_ADD_SEALS:
6793 ret = F_ADD_SEALS;
6794 break;
6795 case TARGET_F_GET_SEALS:
6796 ret = F_GET_SEALS;
6797 break;
6798#endif
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006799 default:
6800 ret = -TARGET_EINVAL;
6801 break;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006802 }
Shivaprasad G Bhat4a545762018-07-13 07:34:46 -05006803
6804#if defined(__powerpc64__)
6805 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6806 * is not supported by kernel. The glibc fcntl call actually adjusts
6807 * them to 5, 6 and 7 before making the syscall(). Since we make the
6808 * syscall directly, adjust to what is supported by the kernel.
6809 */
6810 if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
6811 ret -= F_GETLK64 - 5;
6812 }
6813#endif
6814
6815 return ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006816}
6817
Laurent Vivierae68ad92018-05-10 01:11:21 +02006818#define FLOCK_TRANSTBL \
6819 switch (type) { \
6820 TRANSTBL_CONVERT(F_RDLCK); \
6821 TRANSTBL_CONVERT(F_WRLCK); \
6822 TRANSTBL_CONVERT(F_UNLCK); \
Laurent Vivierae68ad92018-05-10 01:11:21 +02006823 }
6824
6825static int target_to_host_flock(int type)
6826{
6827#define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6828 FLOCK_TRANSTBL
6829#undef TRANSTBL_CONVERT
6830 return -TARGET_EINVAL;
6831}
6832
6833static int host_to_target_flock(int type)
6834{
6835#define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6836 FLOCK_TRANSTBL
6837#undef TRANSTBL_CONVERT
6838 /* if we don't know how to convert the value coming
6839 * from the host we copy to the target field as-is
6840 */
6841 return type;
6842}
Laurent Vivier2ba7f732013-01-10 21:42:48 +01006843
Peter Maydell213d3e92016-06-13 11:22:05 +01006844static inline abi_long copy_from_user_flock(struct flock64 *fl,
6845 abi_ulong target_flock_addr)
6846{
6847 struct target_flock *target_fl;
Laurent Vivierae68ad92018-05-10 01:11:21 +02006848 int l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006849
6850 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6851 return -TARGET_EFAULT;
6852 }
6853
6854 __get_user(l_type, &target_fl->l_type);
Laurent Vivierae68ad92018-05-10 01:11:21 +02006855 l_type = target_to_host_flock(l_type);
6856 if (l_type < 0) {
6857 return l_type;
6858 }
6859 fl->l_type = l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006860 __get_user(fl->l_whence, &target_fl->l_whence);
6861 __get_user(fl->l_start, &target_fl->l_start);
6862 __get_user(fl->l_len, &target_fl->l_len);
6863 __get_user(fl->l_pid, &target_fl->l_pid);
6864 unlock_user_struct(target_fl, target_flock_addr, 0);
6865 return 0;
6866}
6867
6868static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6869 const struct flock64 *fl)
6870{
6871 struct target_flock *target_fl;
6872 short l_type;
6873
6874 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6875 return -TARGET_EFAULT;
6876 }
6877
Laurent Vivierae68ad92018-05-10 01:11:21 +02006878 l_type = host_to_target_flock(fl->l_type);
Peter Maydell213d3e92016-06-13 11:22:05 +01006879 __put_user(l_type, &target_fl->l_type);
6880 __put_user(fl->l_whence, &target_fl->l_whence);
6881 __put_user(fl->l_start, &target_fl->l_start);
6882 __put_user(fl->l_len, &target_fl->l_len);
6883 __put_user(fl->l_pid, &target_fl->l_pid);
6884 unlock_user_struct(target_fl, target_flock_addr, 1);
6885 return 0;
6886}
6887
6888typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6889typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6890
6891#if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
Richard Hendersonffe81d42022-01-06 20:25:59 -08006892struct target_oabi_flock64 {
6893 abi_short l_type;
6894 abi_short l_whence;
6895 abi_llong l_start;
6896 abi_llong l_len;
6897 abi_int l_pid;
6898} QEMU_PACKED;
6899
Laurent Vivier7f254c52018-05-02 23:57:30 +02006900static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
Peter Maydell213d3e92016-06-13 11:22:05 +01006901 abi_ulong target_flock_addr)
6902{
Laurent Vivier7f254c52018-05-02 23:57:30 +02006903 struct target_oabi_flock64 *target_fl;
Laurent Vivierae68ad92018-05-10 01:11:21 +02006904 int l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006905
6906 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6907 return -TARGET_EFAULT;
6908 }
6909
6910 __get_user(l_type, &target_fl->l_type);
Laurent Vivierae68ad92018-05-10 01:11:21 +02006911 l_type = target_to_host_flock(l_type);
6912 if (l_type < 0) {
6913 return l_type;
6914 }
6915 fl->l_type = l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006916 __get_user(fl->l_whence, &target_fl->l_whence);
6917 __get_user(fl->l_start, &target_fl->l_start);
6918 __get_user(fl->l_len, &target_fl->l_len);
6919 __get_user(fl->l_pid, &target_fl->l_pid);
6920 unlock_user_struct(target_fl, target_flock_addr, 0);
6921 return 0;
6922}
6923
Laurent Vivier7f254c52018-05-02 23:57:30 +02006924static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
Peter Maydell213d3e92016-06-13 11:22:05 +01006925 const struct flock64 *fl)
6926{
Laurent Vivier7f254c52018-05-02 23:57:30 +02006927 struct target_oabi_flock64 *target_fl;
Peter Maydell213d3e92016-06-13 11:22:05 +01006928 short l_type;
6929
6930 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6931 return -TARGET_EFAULT;
6932 }
6933
Laurent Vivierae68ad92018-05-10 01:11:21 +02006934 l_type = host_to_target_flock(fl->l_type);
Peter Maydell213d3e92016-06-13 11:22:05 +01006935 __put_user(l_type, &target_fl->l_type);
6936 __put_user(fl->l_whence, &target_fl->l_whence);
6937 __put_user(fl->l_start, &target_fl->l_start);
6938 __put_user(fl->l_len, &target_fl->l_len);
6939 __put_user(fl->l_pid, &target_fl->l_pid);
6940 unlock_user_struct(target_fl, target_flock_addr, 1);
6941 return 0;
6942}
6943#endif
6944
6945static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6946 abi_ulong target_flock_addr)
6947{
6948 struct target_flock64 *target_fl;
Laurent Vivierae68ad92018-05-10 01:11:21 +02006949 int l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006950
6951 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6952 return -TARGET_EFAULT;
6953 }
6954
6955 __get_user(l_type, &target_fl->l_type);
Laurent Vivierae68ad92018-05-10 01:11:21 +02006956 l_type = target_to_host_flock(l_type);
6957 if (l_type < 0) {
6958 return l_type;
6959 }
6960 fl->l_type = l_type;
Peter Maydell213d3e92016-06-13 11:22:05 +01006961 __get_user(fl->l_whence, &target_fl->l_whence);
6962 __get_user(fl->l_start, &target_fl->l_start);
6963 __get_user(fl->l_len, &target_fl->l_len);
6964 __get_user(fl->l_pid, &target_fl->l_pid);
6965 unlock_user_struct(target_fl, target_flock_addr, 0);
6966 return 0;
6967}
6968
6969static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
6970 const struct flock64 *fl)
6971{
6972 struct target_flock64 *target_fl;
6973 short l_type;
6974
6975 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6976 return -TARGET_EFAULT;
6977 }
6978
Laurent Vivierae68ad92018-05-10 01:11:21 +02006979 l_type = host_to_target_flock(fl->l_type);
Peter Maydell213d3e92016-06-13 11:22:05 +01006980 __put_user(l_type, &target_fl->l_type);
6981 __put_user(fl->l_whence, &target_fl->l_whence);
6982 __put_user(fl->l_start, &target_fl->l_start);
6983 __put_user(fl->l_len, &target_fl->l_len);
6984 __put_user(fl->l_pid, &target_fl->l_pid);
6985 unlock_user_struct(target_fl, target_flock_addr, 1);
6986 return 0;
6987}
6988
blueswir1992f48a2007-10-14 16:27:31 +00006989static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00006990{
ths43f238d2007-01-05 20:55:49 +00006991 struct flock64 fl64;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01006992#ifdef F_GETOWN_EX
6993 struct f_owner_ex fox;
6994 struct target_f_owner_ex *target_fox;
6995#endif
blueswir1992f48a2007-10-14 16:27:31 +00006996 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006997 int host_cmd = target_to_host_fcntl_cmd(cmd);
6998
6999 if (host_cmd == -TARGET_EINVAL)
7000 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00007001
bellard7775e9e2003-05-14 22:46:48 +00007002 switch(cmd) {
7003 case TARGET_F_GETLK:
Peter Maydell213d3e92016-06-13 11:22:05 +01007004 ret = copy_from_user_flock(&fl64, arg);
7005 if (ret) {
7006 return ret;
7007 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007008 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00007009 if (ret == 0) {
Peter Maydell213d3e92016-06-13 11:22:05 +01007010 ret = copy_to_user_flock(arg, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00007011 }
7012 break;
ths3b46e622007-09-17 08:09:54 +00007013
bellard7775e9e2003-05-14 22:46:48 +00007014 case TARGET_F_SETLK:
7015 case TARGET_F_SETLKW:
Peter Maydell213d3e92016-06-13 11:22:05 +01007016 ret = copy_from_user_flock(&fl64, arg);
7017 if (ret) {
7018 return ret;
7019 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007020 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00007021 break;
ths3b46e622007-09-17 08:09:54 +00007022
bellard7775e9e2003-05-14 22:46:48 +00007023 case TARGET_F_GETLK64:
Andreas Schwab2d92c682020-05-25 09:59:28 +02007024 case TARGET_F_OFD_GETLK:
Peter Maydell213d3e92016-06-13 11:22:05 +01007025 ret = copy_from_user_flock64(&fl64, arg);
7026 if (ret) {
7027 return ret;
7028 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007029 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00007030 if (ret == 0) {
Peter Maydell213d3e92016-06-13 11:22:05 +01007031 ret = copy_to_user_flock64(arg, &fl64);
ths43f238d2007-01-05 20:55:49 +00007032 }
bellard9ee1fa22007-11-11 15:11:19 +00007033 break;
bellard7775e9e2003-05-14 22:46:48 +00007034 case TARGET_F_SETLK64:
7035 case TARGET_F_SETLKW64:
Andreas Schwab2d92c682020-05-25 09:59:28 +02007036 case TARGET_F_OFD_SETLK:
7037 case TARGET_F_OFD_SETLKW:
Peter Maydell213d3e92016-06-13 11:22:05 +01007038 ret = copy_from_user_flock64(&fl64, arg);
7039 if (ret) {
7040 return ret;
7041 }
Peter Maydell435da5e2016-06-13 11:22:05 +01007042 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00007043 break;
7044
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007045 case TARGET_F_GETFL:
Peter Maydell435da5e2016-06-13 11:22:05 +01007046 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00007047 if (ret >= 0) {
7048 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
Helge Dellere0ddf8e2023-07-08 07:00:25 +02007049 /* tell 32-bit guests it uses largefile on 64-bit hosts: */
7050 if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
7051 ret |= TARGET_O_LARGEFILE;
7052 }
bellard9ee1fa22007-11-11 15:11:19 +00007053 }
bellardffa65c32004-01-04 23:57:22 +00007054 break;
7055
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007056 case TARGET_F_SETFL:
Peter Maydell435da5e2016-06-13 11:22:05 +01007057 ret = get_errno(safe_fcntl(fd, host_cmd,
7058 target_to_host_bitmask(arg,
7059 fcntl_flags_tbl)));
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007060 break;
7061
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007062#ifdef F_GETOWN_EX
7063 case TARGET_F_GETOWN_EX:
Peter Maydell435da5e2016-06-13 11:22:05 +01007064 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007065 if (ret >= 0) {
7066 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
7067 return -TARGET_EFAULT;
7068 target_fox->type = tswap32(fox.type);
7069 target_fox->pid = tswap32(fox.pid);
7070 unlock_user_struct(target_fox, arg, 1);
7071 }
7072 break;
7073#endif
7074
7075#ifdef F_SETOWN_EX
7076 case TARGET_F_SETOWN_EX:
7077 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
7078 return -TARGET_EFAULT;
7079 fox.type = tswap32(target_fox->type);
7080 fox.pid = tswap32(target_fox->pid);
7081 unlock_user_struct(target_fox, arg, 0);
Peter Maydell435da5e2016-06-13 11:22:05 +01007082 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
Andreas Schwab8d5d3002014-03-07 15:24:08 +01007083 break;
7084#endif
7085
Timothy Baldwina7b4c9b2020-07-29 21:06:44 +01007086 case TARGET_F_SETSIG:
7087 ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
7088 break;
7089
7090 case TARGET_F_GETSIG:
7091 ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
7092 break;
7093
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007094 case TARGET_F_SETOWN:
7095 case TARGET_F_GETOWN:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007096 case TARGET_F_SETLEASE:
7097 case TARGET_F_GETLEASE:
Peter Maydell7e3b92e2016-06-20 15:50:37 +01007098 case TARGET_F_SETPIPE_SZ:
7099 case TARGET_F_GETPIPE_SZ:
Shu-Chun Weng2bb963f2020-12-18 11:32:10 -08007100 case TARGET_F_ADD_SEALS:
7101 case TARGET_F_GET_SEALS:
Peter Maydell435da5e2016-06-13 11:22:05 +01007102 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00007103 break;
7104
bellard7775e9e2003-05-14 22:46:48 +00007105 default:
Peter Maydell435da5e2016-06-13 11:22:05 +01007106 ret = get_errno(safe_fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00007107 break;
7108 }
7109 return ret;
7110}
7111
bellard67867302003-11-23 17:05:30 +00007112#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00007113
bellard67867302003-11-23 17:05:30 +00007114static inline int high2lowuid(int uid)
7115{
7116 if (uid > 65535)
7117 return 65534;
7118 else
7119 return uid;
7120}
7121
7122static inline int high2lowgid(int gid)
7123{
7124 if (gid > 65535)
7125 return 65534;
7126 else
7127 return gid;
7128}
7129
7130static inline int low2highuid(int uid)
7131{
7132 if ((int16_t)uid == -1)
7133 return -1;
7134 else
7135 return uid;
7136}
7137
7138static inline int low2highgid(int gid)
7139{
7140 if ((int16_t)gid == -1)
7141 return -1;
7142 else
7143 return gid;
7144}
Riku Voipio0c866a72011-04-18 15:23:06 +03007145static inline int tswapid(int id)
7146{
7147 return tswap16(id);
7148}
Peter Maydell76ca3102014-03-02 19:36:41 +00007149
7150#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7151
Riku Voipio0c866a72011-04-18 15:23:06 +03007152#else /* !USE_UID16 */
7153static inline int high2lowuid(int uid)
7154{
7155 return uid;
7156}
7157static inline int high2lowgid(int gid)
7158{
7159 return gid;
7160}
7161static inline int low2highuid(int uid)
7162{
7163 return uid;
7164}
7165static inline int low2highgid(int gid)
7166{
7167 return gid;
7168}
7169static inline int tswapid(int id)
7170{
7171 return tswap32(id);
7172}
Peter Maydell76ca3102014-03-02 19:36:41 +00007173
7174#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7175
bellard67867302003-11-23 17:05:30 +00007176#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00007177
Peter Maydellfd6f7792016-03-01 16:33:02 +00007178/* We must do direct syscalls for setting UID/GID, because we want to
7179 * implement the Linux system call semantics of "change only for this thread",
7180 * not the libc/POSIX semantics of "change for all threads in process".
7181 * (See http://ewontfix.com/17/ for more details.)
7182 * We use the 32-bit version of the syscalls if present; if it is not
7183 * then either the host architecture supports 32-bit UIDs natively with
7184 * the standard syscall, or the 16-bit UID is the best we can do.
7185 */
7186#ifdef __NR_setuid32
7187#define __NR_sys_setuid __NR_setuid32
7188#else
7189#define __NR_sys_setuid __NR_setuid
7190#endif
7191#ifdef __NR_setgid32
7192#define __NR_sys_setgid __NR_setgid32
7193#else
7194#define __NR_sys_setgid __NR_setgid
7195#endif
7196#ifdef __NR_setresuid32
7197#define __NR_sys_setresuid __NR_setresuid32
7198#else
7199#define __NR_sys_setresuid __NR_setresuid
7200#endif
7201#ifdef __NR_setresgid32
7202#define __NR_sys_setresgid __NR_setresgid32
7203#else
7204#define __NR_sys_setresgid __NR_setresgid
7205#endif
7206
7207_syscall1(int, sys_setuid, uid_t, uid)
7208_syscall1(int, sys_setgid, gid_t, gid)
7209_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
7210_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
7211
bellard31e31b82003-02-18 22:55:36 +00007212void syscall_init(void)
7213{
bellard2ab83ea2003-06-15 19:56:46 +00007214 IOCTLEntry *ie;
7215 const argtype *arg_type;
7216 int size;
7217
Alexander Graf8be656b2015-05-06 23:47:32 +02007218 thunk_init(STRUCT_MAX);
7219
Blue Swirl001faf32009-05-13 17:53:17 +00007220#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00007221#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00007222#include "syscall_types.h"
7223#undef STRUCT
7224#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00007225
7226 /* we patch the ioctl size if necessary. We rely on the fact that
7227 no ioctl has all the bits at '1' in the size field */
7228 ie = ioctl_entries;
7229 while (ie->target_cmd != 0) {
7230 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
7231 TARGET_IOC_SIZEMASK) {
7232 arg_type = ie->arg_type;
7233 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00007234 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00007235 ie->target_cmd);
7236 exit(1);
7237 }
7238 arg_type++;
7239 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00007240 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00007241 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
7242 (size << TARGET_IOC_SIZESHIFT);
7243 }
thsb92c47c2007-11-01 00:07:38 +00007244
bellard2ab83ea2003-06-15 19:56:46 +00007245 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00007246#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7247 (defined(__x86_64__) && defined(TARGET_X86_64))
7248 if (unlikely(ie->target_cmd != ie->host_cmd)) {
7249 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7250 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00007251 }
7252#endif
7253 ie++;
7254 }
bellard31e31b82003-02-18 22:55:36 +00007255}
bellardc573ff62004-01-04 15:51:36 +00007256
pbrookce4defa2006-02-09 16:49:55 +00007257#ifdef TARGET_NR_truncate64
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02007258static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
blueswir1992f48a2007-10-14 16:27:31 +00007259 abi_long arg2,
7260 abi_long arg3,
7261 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00007262{
James Clarke8bf8e9d2017-09-15 20:33:13 +01007263 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
pbrookce4defa2006-02-09 16:49:55 +00007264 arg2 = arg3;
7265 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03007266 }
pbrookce4defa2006-02-09 16:49:55 +00007267 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
7268}
7269#endif
7270
7271#ifdef TARGET_NR_ftruncate64
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02007272static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
blueswir1992f48a2007-10-14 16:27:31 +00007273 abi_long arg2,
7274 abi_long arg3,
7275 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00007276{
James Clarke8bf8e9d2017-09-15 20:33:13 +01007277 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
pbrookce4defa2006-02-09 16:49:55 +00007278 arg2 = arg3;
7279 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03007280 }
pbrookce4defa2006-02-09 16:49:55 +00007281 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
7282}
7283#endif
7284
Alistair Francis859e8a82020-03-12 15:13:49 -07007285#if defined(TARGET_NR_timer_settime) || \
7286 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
Filip Bozuta2c86c902020-07-22 17:34:20 +02007287static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007288 abi_ulong target_addr)
7289{
Filip Bozuta2c86c902020-07-22 17:34:20 +02007290 if (target_to_host_timespec(&host_its->it_interval, target_addr +
7291 offsetof(struct target_itimerspec,
7292 it_interval)) ||
7293 target_to_host_timespec(&host_its->it_value, target_addr +
7294 offsetof(struct target_itimerspec,
7295 it_value))) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007296 return -TARGET_EFAULT;
7297 }
7298
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007299 return 0;
7300}
Alistair Francis859e8a82020-03-12 15:13:49 -07007301#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007302
Filip Bozuta828cb3a2020-07-22 17:34:21 +02007303#if defined(TARGET_NR_timer_settime64) || \
7304 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7305static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
7306 abi_ulong target_addr)
7307{
7308 if (target_to_host_timespec64(&host_its->it_interval, target_addr +
7309 offsetof(struct target__kernel_itimerspec,
7310 it_interval)) ||
7311 target_to_host_timespec64(&host_its->it_value, target_addr +
7312 offsetof(struct target__kernel_itimerspec,
7313 it_value))) {
7314 return -TARGET_EFAULT;
7315 }
7316
7317 return 0;
7318}
7319#endif
7320
Alistair Francis859e8a82020-03-12 15:13:49 -07007321#if ((defined(TARGET_NR_timerfd_gettime) || \
7322 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
Filip Bozuta2c86c902020-07-22 17:34:20 +02007323 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007324static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
Filip Bozuta2c86c902020-07-22 17:34:20 +02007325 struct itimerspec *host_its)
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007326{
Filip Bozuta2c86c902020-07-22 17:34:20 +02007327 if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7328 it_interval),
7329 &host_its->it_interval) ||
7330 host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7331 it_value),
7332 &host_its->it_value)) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007333 return -TARGET_EFAULT;
7334 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007335 return 0;
7336}
Alistair Francis859e8a82020-03-12 15:13:49 -07007337#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11007338
Filip Bozuta828cb3a2020-07-22 17:34:21 +02007339#if ((defined(TARGET_NR_timerfd_gettime64) || \
7340 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7341 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7342static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
7343 struct itimerspec *host_its)
7344{
7345 if (host_to_target_timespec64(target_addr +
7346 offsetof(struct target__kernel_itimerspec,
7347 it_interval),
7348 &host_its->it_interval) ||
7349 host_to_target_timespec64(target_addr +
7350 offsetof(struct target__kernel_itimerspec,
7351 it_value),
7352 &host_its->it_value)) {
7353 return -TARGET_EFAULT;
7354 }
7355 return 0;
7356}
7357#endif
7358
Alistair Francis859e8a82020-03-12 15:13:49 -07007359#if defined(TARGET_NR_adjtimex) || \
7360 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +02007361static inline abi_long target_to_host_timex(struct timex *host_tx,
7362 abi_long target_addr)
7363{
7364 struct target_timex *target_tx;
7365
7366 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7367 return -TARGET_EFAULT;
7368 }
7369
7370 __get_user(host_tx->modes, &target_tx->modes);
7371 __get_user(host_tx->offset, &target_tx->offset);
7372 __get_user(host_tx->freq, &target_tx->freq);
7373 __get_user(host_tx->maxerror, &target_tx->maxerror);
7374 __get_user(host_tx->esterror, &target_tx->esterror);
7375 __get_user(host_tx->status, &target_tx->status);
7376 __get_user(host_tx->constant, &target_tx->constant);
7377 __get_user(host_tx->precision, &target_tx->precision);
7378 __get_user(host_tx->tolerance, &target_tx->tolerance);
7379 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7380 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7381 __get_user(host_tx->tick, &target_tx->tick);
7382 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7383 __get_user(host_tx->jitter, &target_tx->jitter);
7384 __get_user(host_tx->shift, &target_tx->shift);
7385 __get_user(host_tx->stabil, &target_tx->stabil);
7386 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7387 __get_user(host_tx->calcnt, &target_tx->calcnt);
7388 __get_user(host_tx->errcnt, &target_tx->errcnt);
7389 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7390 __get_user(host_tx->tai, &target_tx->tai);
7391
7392 unlock_user_struct(target_tx, target_addr, 0);
7393 return 0;
7394}
7395
7396static inline abi_long host_to_target_timex(abi_long target_addr,
7397 struct timex *host_tx)
7398{
7399 struct target_timex *target_tx;
7400
7401 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7402 return -TARGET_EFAULT;
7403 }
7404
7405 __put_user(host_tx->modes, &target_tx->modes);
7406 __put_user(host_tx->offset, &target_tx->offset);
7407 __put_user(host_tx->freq, &target_tx->freq);
7408 __put_user(host_tx->maxerror, &target_tx->maxerror);
7409 __put_user(host_tx->esterror, &target_tx->esterror);
7410 __put_user(host_tx->status, &target_tx->status);
7411 __put_user(host_tx->constant, &target_tx->constant);
7412 __put_user(host_tx->precision, &target_tx->precision);
7413 __put_user(host_tx->tolerance, &target_tx->tolerance);
7414 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7415 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7416 __put_user(host_tx->tick, &target_tx->tick);
7417 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7418 __put_user(host_tx->jitter, &target_tx->jitter);
7419 __put_user(host_tx->shift, &target_tx->shift);
7420 __put_user(host_tx->stabil, &target_tx->stabil);
7421 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7422 __put_user(host_tx->calcnt, &target_tx->calcnt);
7423 __put_user(host_tx->errcnt, &target_tx->errcnt);
7424 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7425 __put_user(host_tx->tai, &target_tx->tai);
7426
7427 unlock_user_struct(target_tx, target_addr, 1);
7428 return 0;
7429}
Alistair Francis859e8a82020-03-12 15:13:49 -07007430#endif
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +02007431
Filip Bozuta6ac03b22020-08-24 21:21:15 +02007432
7433#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7434static inline abi_long target_to_host_timex64(struct timex *host_tx,
7435 abi_long target_addr)
7436{
7437 struct target__kernel_timex *target_tx;
7438
7439 if (copy_from_user_timeval64(&host_tx->time, target_addr +
7440 offsetof(struct target__kernel_timex,
7441 time))) {
7442 return -TARGET_EFAULT;
7443 }
7444
7445 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7446 return -TARGET_EFAULT;
7447 }
7448
7449 __get_user(host_tx->modes, &target_tx->modes);
7450 __get_user(host_tx->offset, &target_tx->offset);
7451 __get_user(host_tx->freq, &target_tx->freq);
7452 __get_user(host_tx->maxerror, &target_tx->maxerror);
7453 __get_user(host_tx->esterror, &target_tx->esterror);
7454 __get_user(host_tx->status, &target_tx->status);
7455 __get_user(host_tx->constant, &target_tx->constant);
7456 __get_user(host_tx->precision, &target_tx->precision);
7457 __get_user(host_tx->tolerance, &target_tx->tolerance);
7458 __get_user(host_tx->tick, &target_tx->tick);
7459 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7460 __get_user(host_tx->jitter, &target_tx->jitter);
7461 __get_user(host_tx->shift, &target_tx->shift);
7462 __get_user(host_tx->stabil, &target_tx->stabil);
7463 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7464 __get_user(host_tx->calcnt, &target_tx->calcnt);
7465 __get_user(host_tx->errcnt, &target_tx->errcnt);
7466 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7467 __get_user(host_tx->tai, &target_tx->tai);
7468
7469 unlock_user_struct(target_tx, target_addr, 0);
7470 return 0;
7471}
7472
7473static inline abi_long host_to_target_timex64(abi_long target_addr,
7474 struct timex *host_tx)
7475{
7476 struct target__kernel_timex *target_tx;
7477
7478 if (copy_to_user_timeval64(target_addr +
7479 offsetof(struct target__kernel_timex, time),
7480 &host_tx->time)) {
7481 return -TARGET_EFAULT;
7482 }
7483
7484 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7485 return -TARGET_EFAULT;
7486 }
7487
7488 __put_user(host_tx->modes, &target_tx->modes);
7489 __put_user(host_tx->offset, &target_tx->offset);
7490 __put_user(host_tx->freq, &target_tx->freq);
7491 __put_user(host_tx->maxerror, &target_tx->maxerror);
7492 __put_user(host_tx->esterror, &target_tx->esterror);
7493 __put_user(host_tx->status, &target_tx->status);
7494 __put_user(host_tx->constant, &target_tx->constant);
7495 __put_user(host_tx->precision, &target_tx->precision);
7496 __put_user(host_tx->tolerance, &target_tx->tolerance);
7497 __put_user(host_tx->tick, &target_tx->tick);
7498 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7499 __put_user(host_tx->jitter, &target_tx->jitter);
7500 __put_user(host_tx->shift, &target_tx->shift);
7501 __put_user(host_tx->stabil, &target_tx->stabil);
7502 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7503 __put_user(host_tx->calcnt, &target_tx->calcnt);
7504 __put_user(host_tx->errcnt, &target_tx->errcnt);
7505 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7506 __put_user(host_tx->tai, &target_tx->tai);
7507
7508 unlock_user_struct(target_tx, target_addr, 1);
7509 return 0;
7510}
7511#endif
7512
Michael Forney96ff7582021-05-25 20:55:56 -07007513#ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7514#define sigev_notify_thread_id _sigev_un._tid
7515#endif
7516
Peter Maydellc0659762014-08-09 15:42:32 +01007517static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
7518 abi_ulong target_addr)
7519{
7520 struct target_sigevent *target_sevp;
7521
7522 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
7523 return -TARGET_EFAULT;
7524 }
7525
7526 /* This union is awkward on 64 bit systems because it has a 32 bit
7527 * integer and a pointer in it; we follow the conversion approach
7528 * used for handling sigval types in signal.c so the guest should get
7529 * the correct value back even if we did a 64 bit byteswap and it's
7530 * using the 32 bit integer.
7531 */
7532 host_sevp->sigev_value.sival_ptr =
7533 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
7534 host_sevp->sigev_signo =
7535 target_to_host_signal(tswap32(target_sevp->sigev_signo));
7536 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
Michael Forney96ff7582021-05-25 20:55:56 -07007537 host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
Peter Maydellc0659762014-08-09 15:42:32 +01007538
7539 unlock_user_struct(target_sevp, target_addr, 1);
7540 return 0;
7541}
7542
Tom Musta6f6a4032014-08-12 13:53:42 -05007543#if defined(TARGET_NR_mlockall)
7544static inline int target_to_host_mlockall_arg(int arg)
7545{
7546 int result = 0;
7547
Filip Bozuta02e5d7d2020-08-11 18:45:51 +02007548 if (arg & TARGET_MCL_CURRENT) {
Tom Musta6f6a4032014-08-12 13:53:42 -05007549 result |= MCL_CURRENT;
7550 }
Filip Bozuta02e5d7d2020-08-11 18:45:51 +02007551 if (arg & TARGET_MCL_FUTURE) {
Tom Musta6f6a4032014-08-12 13:53:42 -05007552 result |= MCL_FUTURE;
7553 }
Filip Bozuta02e5d7d2020-08-11 18:45:51 +02007554#ifdef MCL_ONFAULT
7555 if (arg & TARGET_MCL_ONFAULT) {
7556 result |= MCL_ONFAULT;
7557 }
7558#endif
7559
Tom Musta6f6a4032014-08-12 13:53:42 -05007560 return result;
7561}
7562#endif
7563
Helge Dellerfe080592022-12-15 08:27:46 +01007564static inline int target_to_host_msync_arg(abi_long arg)
7565{
7566 return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
7567 ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
7568 ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
7569 (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
7570}
7571
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02007572#if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7573 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7574 defined(TARGET_NR_newfstatat))
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02007575static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
balrog6a24a772008-09-20 02:23:36 +00007576 abi_ulong target_addr,
7577 struct stat *host_st)
7578{
Alexander Graf09701192013-09-03 20:12:15 +01007579#if defined(TARGET_ARM) && defined(TARGET_ABI32)
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02007580 if (cpu_env->eabi) {
balrog6a24a772008-09-20 02:23:36 +00007581 struct target_eabi_stat64 *target_st;
7582
7583 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7584 return -TARGET_EFAULT;
7585 memset(target_st, 0, sizeof(struct target_eabi_stat64));
7586 __put_user(host_st->st_dev, &target_st->st_dev);
7587 __put_user(host_st->st_ino, &target_st->st_ino);
7588#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7589 __put_user(host_st->st_ino, &target_st->__st_ino);
7590#endif
7591 __put_user(host_st->st_mode, &target_st->st_mode);
7592 __put_user(host_st->st_nlink, &target_st->st_nlink);
7593 __put_user(host_st->st_uid, &target_st->st_uid);
7594 __put_user(host_st->st_gid, &target_st->st_gid);
7595 __put_user(host_st->st_rdev, &target_st->st_rdev);
7596 __put_user(host_st->st_size, &target_st->st_size);
7597 __put_user(host_st->st_blksize, &target_st->st_blksize);
7598 __put_user(host_st->st_blocks, &target_st->st_blocks);
7599 __put_user(host_st->st_atime, &target_st->target_st_atime);
7600 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7601 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
Michael Forneyfebf6fa2021-05-25 20:55:31 -07007602#ifdef HAVE_STRUCT_STAT_ST_ATIM
Chen-Yu Tsai5f992db2019-05-23 00:21:47 +08007603 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7604 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7605 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7606#endif
balrog6a24a772008-09-20 02:23:36 +00007607 unlock_user_struct(target_st, target_addr, 1);
7608 } else
7609#endif
7610 {
Stefan Weil20d155b2013-10-30 22:52:24 +01007611#if defined(TARGET_HAS_STRUCT_STAT64)
balrog6a24a772008-09-20 02:23:36 +00007612 struct target_stat64 *target_st;
Stefan Weil20d155b2013-10-30 22:52:24 +01007613#else
7614 struct target_stat *target_st;
aurel329d33b762009-04-08 23:07:05 +00007615#endif
balrog6a24a772008-09-20 02:23:36 +00007616
7617 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7618 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00007619 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00007620 __put_user(host_st->st_dev, &target_st->st_dev);
7621 __put_user(host_st->st_ino, &target_st->st_ino);
7622#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7623 __put_user(host_st->st_ino, &target_st->__st_ino);
7624#endif
7625 __put_user(host_st->st_mode, &target_st->st_mode);
7626 __put_user(host_st->st_nlink, &target_st->st_nlink);
7627 __put_user(host_st->st_uid, &target_st->st_uid);
7628 __put_user(host_st->st_gid, &target_st->st_gid);
7629 __put_user(host_st->st_rdev, &target_st->st_rdev);
7630 /* XXX: better use of kernel struct */
7631 __put_user(host_st->st_size, &target_st->st_size);
7632 __put_user(host_st->st_blksize, &target_st->st_blksize);
7633 __put_user(host_st->st_blocks, &target_st->st_blocks);
7634 __put_user(host_st->st_atime, &target_st->target_st_atime);
7635 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7636 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
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 }
7644
7645 return 0;
7646}
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02007647#endif
balrog6a24a772008-09-20 02:23:36 +00007648
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02007649#if defined(TARGET_NR_statx) && defined(__NR_statx)
7650static inline abi_long host_to_target_statx(struct target_statx *host_stx,
7651 abi_ulong target_addr)
7652{
7653 struct target_statx *target_stx;
7654
7655 if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
7656 return -TARGET_EFAULT;
7657 }
7658 memset(target_stx, 0, sizeof(*target_stx));
7659
7660 __put_user(host_stx->stx_mask, &target_stx->stx_mask);
7661 __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
7662 __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
7663 __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
7664 __put_user(host_stx->stx_uid, &target_stx->stx_uid);
7665 __put_user(host_stx->stx_gid, &target_stx->stx_gid);
7666 __put_user(host_stx->stx_mode, &target_stx->stx_mode);
7667 __put_user(host_stx->stx_ino, &target_stx->stx_ino);
7668 __put_user(host_stx->stx_size, &target_stx->stx_size);
7669 __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
7670 __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
7671 __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
7672 __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
Ariadne Conilld1e26702019-11-22 11:40:40 -06007673 __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
7674 __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
7675 __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
7676 __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
7677 __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
7678 __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02007679 __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
7680 __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
7681 __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
7682 __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
7683
7684 unlock_user_struct(target_stx, target_addr, 1);
7685
7686 return 0;
7687}
7688#endif
7689
Alistair Francis14690292020-03-18 15:47:01 -07007690static int do_sys_futex(int *uaddr, int op, int val,
7691 const struct timespec *timeout, int *uaddr2,
7692 int val3)
7693{
7694#if HOST_LONG_BITS == 64
7695#if defined(__NR_futex)
7696 /* always a 64-bit time_t, it doesn't define _time64 version */
7697 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7698
7699#endif
7700#else /* HOST_LONG_BITS == 64 */
7701#if defined(__NR_futex_time64)
7702 if (sizeof(timeout->tv_sec) == 8) {
7703 /* _time64 function on 32bit arch */
7704 return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
7705 }
7706#endif
7707#if defined(__NR_futex)
7708 /* old function on 32bit arch */
7709 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7710#endif
7711#endif /* HOST_LONG_BITS == 64 */
7712 g_assert_not_reached();
7713}
7714
7715static int do_safe_futex(int *uaddr, int op, int val,
7716 const struct timespec *timeout, int *uaddr2,
7717 int val3)
7718{
7719#if HOST_LONG_BITS == 64
7720#if defined(__NR_futex)
7721 /* always a 64-bit time_t, it doesn't define _time64 version */
7722 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7723#endif
7724#else /* HOST_LONG_BITS == 64 */
7725#if defined(__NR_futex_time64)
7726 if (sizeof(timeout->tv_sec) == 8) {
7727 /* _time64 function on 32bit arch */
7728 return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
7729 val3));
7730 }
7731#endif
7732#if defined(__NR_futex)
7733 /* old function on 32bit arch */
7734 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7735#endif
7736#endif /* HOST_LONG_BITS == 64 */
7737 return -TARGET_ENOSYS;
7738}
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02007739
pbrookbd0c5662008-05-29 14:34:11 +00007740/* ??? Using host futex calls even when target atomic operations
7741 are not really atomic probably breaks things. However implementing
7742 futexes locally would make futexes shared between multiple processes
7743 tricky. However they're probably useless because guest atomic
7744 operations won't work either. */
Richard Henderson0fbc0f82022-08-28 19:09:59 -07007745#if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7746static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
7747 int op, int val, target_ulong timeout,
7748 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00007749{
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007750 struct timespec ts, *pts = NULL;
7751 void *haddr2 = NULL;
Nathan Froyda16aae02009-08-03 08:43:29 -07007752 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00007753
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007754 /* We assume FUTEX_* constants are the same on both host and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03007755#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07007756 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03007757#else
Nathan Froyda16aae02009-08-03 08:43:29 -07007758 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03007759#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07007760 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00007761 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07007762 case FUTEX_WAIT_BITSET:
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007763 val = tswap32(val);
7764 break;
Richard Hendersonc72a90d2022-08-28 19:10:03 -07007765 case FUTEX_WAIT_REQUEUE_PI:
7766 val = tswap32(val);
7767 haddr2 = g2h(cpu, uaddr2);
7768 break;
7769 case FUTEX_LOCK_PI:
7770 case FUTEX_LOCK_PI2:
7771 break;
pbrookbd0c5662008-05-29 14:34:11 +00007772 case FUTEX_WAKE:
Richard Hendersona6180f82022-08-28 19:10:01 -07007773 case FUTEX_WAKE_BITSET:
Richard Hendersonc72a90d2022-08-28 19:10:03 -07007774 case FUTEX_TRYLOCK_PI:
7775 case FUTEX_UNLOCK_PI:
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007776 timeout = 0;
7777 break;
pbrookbd0c5662008-05-29 14:34:11 +00007778 case FUTEX_FD:
Richard Henderson0f946732022-08-28 19:10:02 -07007779 val = target_to_host_signal(val);
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007780 timeout = 0;
7781 break;
pbrookbd0c5662008-05-29 14:34:11 +00007782 case FUTEX_CMP_REQUEUE:
Richard Hendersonc72a90d2022-08-28 19:10:03 -07007783 case FUTEX_CMP_REQUEUE_PI:
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007784 val3 = tswap32(val3);
7785 /* fall through */
7786 case FUTEX_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07007787 case FUTEX_WAKE_OP:
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007788 /*
7789 * For these, the 4th argument is not TIMEOUT, but VAL2.
7790 * But the prototype of do_safe_futex takes a pointer, so
7791 * insert casts to satisfy the compiler. We do not need
7792 * to tswap VAL2 since it's not compared to guest memory.
7793 */
7794 pts = (struct timespec *)(uintptr_t)timeout;
7795 timeout = 0;
7796 haddr2 = g2h(cpu, uaddr2);
7797 break;
pbrookbd0c5662008-05-29 14:34:11 +00007798 default:
7799 return -TARGET_ENOSYS;
7800 }
Richard Henderson57b9ccd2022-08-28 19:10:00 -07007801 if (timeout) {
7802 pts = &ts;
7803 if (time64
7804 ? target_to_host_timespec64(pts, timeout)
7805 : target_to_host_timespec(pts, timeout)) {
7806 return -TARGET_EFAULT;
7807 }
7808 }
7809 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
pbrookbd0c5662008-05-29 14:34:11 +00007810}
Alistair Francis859e8a82020-03-12 15:13:49 -07007811#endif
Alistair Francis14690292020-03-18 15:47:01 -07007812
Laurent Vivier0f0426f2015-09-01 22:27:33 +02007813#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7814static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7815 abi_long handle, abi_long mount_id,
7816 abi_long flags)
7817{
7818 struct file_handle *target_fh;
7819 struct file_handle *fh;
7820 int mid = 0;
7821 abi_long ret;
7822 char *name;
7823 unsigned int size, total_size;
7824
7825 if (get_user_s32(size, handle)) {
7826 return -TARGET_EFAULT;
7827 }
7828
7829 name = lock_user_string(pathname);
7830 if (!name) {
7831 return -TARGET_EFAULT;
7832 }
7833
7834 total_size = sizeof(struct file_handle) + size;
7835 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7836 if (!target_fh) {
7837 unlock_user(name, pathname, 0);
7838 return -TARGET_EFAULT;
7839 }
7840
7841 fh = g_malloc0(total_size);
7842 fh->handle_bytes = size;
7843
7844 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7845 unlock_user(name, pathname, 0);
7846
7847 /* man name_to_handle_at(2):
7848 * Other than the use of the handle_bytes field, the caller should treat
7849 * the file_handle structure as an opaque data type
7850 */
7851
7852 memcpy(target_fh, fh, total_size);
7853 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7854 target_fh->handle_type = tswap32(fh->handle_type);
7855 g_free(fh);
7856 unlock_user(target_fh, handle, total_size);
7857
7858 if (put_user_s32(mid, mount_id)) {
7859 return -TARGET_EFAULT;
7860 }
7861
7862 return ret;
7863
7864}
7865#endif
7866
7867#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7868static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7869 abi_long flags)
7870{
7871 struct file_handle *target_fh;
7872 struct file_handle *fh;
7873 unsigned int size, total_size;
7874 abi_long ret;
7875
7876 if (get_user_s32(size, handle)) {
7877 return -TARGET_EFAULT;
7878 }
7879
7880 total_size = sizeof(struct file_handle) + size;
7881 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7882 if (!target_fh) {
7883 return -TARGET_EFAULT;
7884 }
7885
Thomas Huthe9d49d52015-10-09 17:56:38 +02007886 fh = g_memdup(target_fh, total_size);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02007887 fh->handle_bytes = size;
7888 fh->handle_type = tswap32(target_fh->handle_type);
7889
7890 ret = get_errno(open_by_handle_at(mount_fd, fh,
7891 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7892
7893 g_free(fh);
7894
7895 unlock_user(target_fh, handle, total_size);
7896
7897 return ret;
7898}
7899#endif
pbrookbd0c5662008-05-29 14:34:11 +00007900
Laurent Viviere36800c2015-10-02 14:48:09 +02007901#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7902
Laurent Viviere36800c2015-10-02 14:48:09 +02007903static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7904{
7905 int host_flags;
7906 target_sigset_t *target_mask;
7907 sigset_t host_mask;
7908 abi_long ret;
7909
Helge Deller78721302021-02-10 07:12:14 +01007910 if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
Laurent Viviere36800c2015-10-02 14:48:09 +02007911 return -TARGET_EINVAL;
7912 }
7913 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7914 return -TARGET_EFAULT;
7915 }
7916
7917 target_to_host_sigset(&host_mask, target_mask);
7918
7919 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7920
7921 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7922 if (ret >= 0) {
7923 fd_trans_register(ret, &target_signalfd_trans);
7924 }
7925
7926 unlock_user_struct(target_mask, mask, 0);
7927
7928 return ret;
7929}
7930#endif
7931
pbrook1d9d8b52009-04-16 15:17:02 +00007932/* Map host to target signal numbers for the wait family of syscalls.
7933 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07007934int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00007935{
7936 if (WIFSIGNALED(status)) {
7937 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
7938 }
7939 if (WIFSTOPPED(status)) {
7940 return (host_to_target_signal(WSTOPSIG(status)) << 8)
7941 | (status & 0xff);
7942 }
7943 return status;
7944}
7945
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02007946static int open_self_cmdline(CPUArchState *cpu_env, int fd)
Wim Vander Schelden76b94242014-06-18 11:02:39 +02007947{
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02007948 CPUState *cpu = env_cpu(cpu_env);
Andreas Schwab58de8b92017-03-20 12:31:55 +01007949 struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
7950 int i;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02007951
Andreas Schwab58de8b92017-03-20 12:31:55 +01007952 for (i = 0; i < bprm->argc; i++) {
7953 size_t len = strlen(bprm->argv[i]) + 1;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02007954
Andreas Schwab58de8b92017-03-20 12:31:55 +01007955 if (write(fd, bprm->argv[i], len) != len) {
Wim Vander Schelden76b94242014-06-18 11:02:39 +02007956 return -1;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02007957 }
7958 }
7959
Andreas Schwab58de8b92017-03-20 12:31:55 +01007960 return 0;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02007961}
7962
Richard Henderson7b7a3362023-08-08 20:02:19 -07007963struct open_self_maps_data {
7964 TaskState *ts;
7965 IntervalTreeRoot *host_maps;
7966 int fd;
7967 bool smaps;
7968};
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01007969
Richard Henderson7b7a3362023-08-08 20:02:19 -07007970/*
7971 * Subroutine to output one line of /proc/self/maps,
7972 * or one region of /proc/self/smaps.
7973 */
7974
7975#ifdef TARGET_HPPA
7976# define test_stack(S, E, L) (E == L)
7977#else
7978# define test_stack(S, E, L) (S == L)
7979#endif
7980
7981static void open_self_maps_4(const struct open_self_maps_data *d,
7982 const MapInfo *mi, abi_ptr start,
7983 abi_ptr end, unsigned flags)
7984{
7985 const struct image_info *info = d->ts->info;
7986 const char *path = mi->path;
7987 uint64_t offset;
7988 int fd = d->fd;
7989 int count;
7990
7991 if (test_stack(start, end, info->stack_limit)) {
7992 path = "[stack]";
Richard Henderson92d2a032023-08-16 10:33:28 -07007993 } else if (start == info->brk) {
7994 path = "[heap]";
Richard Henderson5d94c2f2023-08-16 10:54:57 -07007995 } else if (start == info->vdso) {
7996 path = "[vdso]";
Richard Henderson7b7a3362023-08-08 20:02:19 -07007997 }
7998
7999 /* Except null device (MAP_ANON), adjust offset for this fragment. */
8000 offset = mi->offset;
8001 if (mi->dev) {
8002 uintptr_t hstart = (uintptr_t)g2h_untagged(start);
8003 offset += hstart - mi->itree.start;
8004 }
8005
8006 count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
8007 " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64,
8008 start, end,
8009 (flags & PAGE_READ) ? 'r' : '-',
8010 (flags & PAGE_WRITE_ORG) ? 'w' : '-',
8011 (flags & PAGE_EXEC) ? 'x' : '-',
8012 mi->is_priv ? 'p' : 's',
8013 offset, major(mi->dev), minor(mi->dev),
8014 (uint64_t)mi->inode);
8015 if (path) {
8016 dprintf(fd, "%*s%s\n", 73 - count, "", path);
8017 } else {
8018 dprintf(fd, "\n");
8019 }
8020
8021 if (d->smaps) {
8022 unsigned long size = end - start;
8023 unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
8024 unsigned long size_kb = size >> 10;
8025
8026 dprintf(fd, "Size: %lu kB\n"
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01008027 "KernelPageSize: %lu kB\n"
8028 "MMUPageSize: %lu kB\n"
8029 "Rss: 0 kB\n"
8030 "Pss: 0 kB\n"
8031 "Pss_Dirty: 0 kB\n"
8032 "Shared_Clean: 0 kB\n"
8033 "Shared_Dirty: 0 kB\n"
8034 "Private_Clean: 0 kB\n"
8035 "Private_Dirty: 0 kB\n"
8036 "Referenced: 0 kB\n"
Ilya Leoshkevich6467d9e2023-08-23 17:55:33 -07008037 "Anonymous: %lu kB\n"
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01008038 "LazyFree: 0 kB\n"
8039 "AnonHugePages: 0 kB\n"
8040 "ShmemPmdMapped: 0 kB\n"
8041 "FilePmdMapped: 0 kB\n"
8042 "Shared_Hugetlb: 0 kB\n"
8043 "Private_Hugetlb: 0 kB\n"
8044 "Swap: 0 kB\n"
8045 "SwapPss: 0 kB\n"
8046 "Locked: 0 kB\n"
Richard Henderson7b7a3362023-08-08 20:02:19 -07008047 "THPeligible: 0\n"
8048 "VmFlags:%s%s%s%s%s%s%s%s\n",
8049 size_kb, page_size_kb, page_size_kb,
Ilya Leoshkevich6467d9e2023-08-23 17:55:33 -07008050 (flags & PAGE_ANON ? size_kb : 0),
Richard Henderson7b7a3362023-08-08 20:02:19 -07008051 (flags & PAGE_READ) ? " rd" : "",
8052 (flags & PAGE_WRITE_ORG) ? " wr" : "",
8053 (flags & PAGE_EXEC) ? " ex" : "",
8054 mi->is_priv ? "" : " sh",
8055 (flags & PAGE_READ) ? " mr" : "",
8056 (flags & PAGE_WRITE_ORG) ? " mw" : "",
8057 (flags & PAGE_EXEC) ? " me" : "",
8058 mi->is_priv ? "" : " ms");
8059 }
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01008060}
8061
Richard Henderson7b7a3362023-08-08 20:02:19 -07008062/*
8063 * Callback for walk_memory_regions, when read_self_maps() fails.
8064 * Proceed without the benefit of host /proc/self/maps cross-check.
8065 */
8066static int open_self_maps_3(void *opaque, target_ulong guest_start,
8067 target_ulong guest_end, unsigned long flags)
Alexander Graf36c08d42011-11-02 20:23:24 +01008068{
Richard Henderson7b7a3362023-08-08 20:02:19 -07008069 static const MapInfo mi = { .is_priv = true };
Alexander Graf36c08d42011-11-02 20:23:24 +01008070
Richard Henderson7b7a3362023-08-08 20:02:19 -07008071 open_self_maps_4(opaque, &mi, guest_start, guest_end, flags);
8072 return 0;
8073}
Alexander Graf1a49ef22012-05-01 16:30:28 +01008074
Richard Henderson7b7a3362023-08-08 20:02:19 -07008075/*
8076 * Callback for walk_memory_regions, when read_self_maps() succeeds.
8077 */
8078static int open_self_maps_2(void *opaque, target_ulong guest_start,
8079 target_ulong guest_end, unsigned long flags)
8080{
8081 const struct open_self_maps_data *d = opaque;
8082 uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start);
8083 uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1);
Alex Bennée01ef6b92020-04-03 20:11:46 +01008084
Richard Henderson7b7a3362023-08-08 20:02:19 -07008085 while (1) {
8086 IntervalTreeNode *n =
8087 interval_tree_iter_first(d->host_maps, host_start, host_start);
8088 MapInfo *mi = container_of(n, MapInfo, itree);
8089 uintptr_t this_hlast = MIN(host_last, n->last);
8090 target_ulong this_gend = h2g(this_hlast) + 1;
Alex Bennée01ef6b92020-04-03 20:11:46 +01008091
Richard Henderson7b7a3362023-08-08 20:02:19 -07008092 open_self_maps_4(d, mi, guest_start, this_gend, flags);
Alex Bennée01ef6b92020-04-03 20:11:46 +01008093
Richard Henderson7b7a3362023-08-08 20:02:19 -07008094 if (this_hlast == host_last) {
8095 return 0;
Alexander Graf1a49ef22012-05-01 16:30:28 +01008096 }
Richard Henderson7b7a3362023-08-08 20:02:19 -07008097 host_start = this_hlast + 1;
8098 guest_start = h2g(host_start);
Alexander Graf1a49ef22012-05-01 16:30:28 +01008099 }
Richard Henderson7b7a3362023-08-08 20:02:19 -07008100}
Alexander Graf1a49ef22012-05-01 16:30:28 +01008101
Richard Henderson7b7a3362023-08-08 20:02:19 -07008102static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
8103{
8104 struct open_self_maps_data d = {
8105 .ts = env_cpu(env)->opaque,
8106 .host_maps = read_self_maps(),
8107 .fd = fd,
8108 .smaps = smaps
8109 };
Alex Bennée01ef6b92020-04-03 20:11:46 +01008110
Richard Henderson7b7a3362023-08-08 20:02:19 -07008111 if (d.host_maps) {
8112 walk_memory_regions(&d, open_self_maps_2);
8113 free_self_maps(d.host_maps);
8114 } else {
8115 walk_memory_regions(&d, open_self_maps_3);
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01008116 }
Alexander Graf36c08d42011-11-02 20:23:24 +01008117 return 0;
8118}
8119
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01008120static int open_self_maps(CPUArchState *cpu_env, int fd)
8121{
8122 return open_self_maps_1(cpu_env, fd, false);
8123}
8124
8125static int open_self_smaps(CPUArchState *cpu_env, int fd)
8126{
8127 return open_self_maps_1(cpu_env, fd, true);
8128}
8129
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02008130static int open_self_stat(CPUArchState *cpu_env, int fd)
Alexander Graf480b8e72011-11-02 20:23:25 +01008131{
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02008132 CPUState *cpu = env_cpu(cpu_env);
Andreas Färber0429a972013-08-26 18:14:44 +02008133 TaskState *ts = cpu->opaque;
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008134 g_autoptr(GString) buf = g_string_new(NULL);
Alexander Graf480b8e72011-11-02 20:23:25 +01008135 int i;
8136
8137 for (i = 0; i < 44; i++) {
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008138 if (i == 0) {
8139 /* pid */
8140 g_string_printf(buf, FMT_pid " ", getpid());
8141 } else if (i == 1) {
8142 /* app name */
8143 gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
8144 bin = bin ? bin + 1 : ts->bprm->argv[0];
8145 g_string_printf(buf, "(%.15s) ", bin);
Andreas Schwab25bb27c2023-03-06 10:59:29 +01008146 } else if (i == 2) {
8147 /* task state */
8148 g_string_assign(buf, "R "); /* we are running right now */
Andreas Schwab7aa9fe32021-06-21 11:32:59 +02008149 } else if (i == 3) {
8150 /* ppid */
8151 g_string_printf(buf, FMT_pid " ", getppid());
Cameron Esfahanieb33cda2022-01-27 16:12:51 -08008152 } else if (i == 21) {
8153 /* starttime */
8154 g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008155 } else if (i == 27) {
8156 /* stack bottom */
8157 g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
8158 } else {
8159 /* for the rest, there is MasterCard */
8160 g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
8161 }
Alexander Graf480b8e72011-11-02 20:23:25 +01008162
Alex Bennée7ad4d5a2020-04-14 21:06:23 +01008163 if (write(fd, buf->str, buf->len) != buf->len) {
8164 return -1;
8165 }
Alexander Graf480b8e72011-11-02 20:23:25 +01008166 }
8167
8168 return 0;
8169}
8170
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02008171static int open_self_auxv(CPUArchState *cpu_env, int fd)
Alexander Graf257450e2011-11-02 20:23:26 +01008172{
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02008173 CPUState *cpu = env_cpu(cpu_env);
Andreas Färber0429a972013-08-26 18:14:44 +02008174 TaskState *ts = cpu->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01008175 abi_ulong auxv = ts->info->saved_auxv;
8176 abi_ulong len = ts->info->auxv_len;
8177 char *ptr;
8178
8179 /*
8180 * Auxiliary vector is stored in target process stack.
8181 * read in whole auxv vector and copy it to file
8182 */
8183 ptr = lock_user(VERIFY_READ, auxv, len, 0);
8184 if (ptr != NULL) {
8185 while (len > 0) {
8186 ssize_t r;
8187 r = write(fd, ptr, len);
8188 if (r <= 0) {
8189 break;
8190 }
8191 len -= r;
8192 ptr += r;
8193 }
8194 lseek(fd, 0, SEEK_SET);
8195 unlock_user(ptr, auxv, len);
8196 }
8197
8198 return 0;
8199}
8200
Andreas Schwab463d8e72013-07-02 14:04:12 +01008201static int is_proc_myself(const char *filename, const char *entry)
8202{
8203 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
8204 filename += strlen("/proc/");
8205 if (!strncmp(filename, "self/", strlen("self/"))) {
8206 filename += strlen("self/");
8207 } else if (*filename >= '1' && *filename <= '9') {
8208 char myself[80];
8209 snprintf(myself, sizeof(myself), "%d/", getpid());
8210 if (!strncmp(filename, myself, strlen(myself))) {
8211 filename += strlen(myself);
8212 } else {
8213 return 0;
8214 }
8215 } else {
8216 return 0;
8217 }
8218 if (!strcmp(filename, entry)) {
8219 return 1;
8220 }
8221 }
8222 return 0;
8223}
8224
Helge Dellerbd5ccd62022-10-24 22:18:09 +02008225static void excp_dump_file(FILE *logfile, CPUArchState *env,
8226 const char *fmt, int code)
8227{
8228 if (logfile) {
8229 CPUState *cs = env_cpu(env);
8230
8231 fprintf(logfile, fmt, code);
8232 fprintf(logfile, "Failing executable: %s\n", exec_path);
8233 cpu_dump_state(cs, logfile, 0);
8234 open_self_maps(env, fileno(logfile));
8235 }
8236}
8237
8238void target_exception_dump(CPUArchState *env, const char *fmt, int code)
8239{
8240 /* dump to console */
8241 excp_dump_file(stderr, env, fmt, code);
8242
8243 /* dump to log file */
8244 if (qemu_log_separate()) {
8245 FILE *logfile = qemu_log_trylock();
8246
8247 excp_dump_file(logfile, env, fmt, code);
8248 qemu_log_unlock(logfile);
8249 }
8250}
8251
Richard Henderson121c8dd2023-08-23 17:13:14 -07008252#include "target_proc.h"
8253
Marc-André Lureauee3eb3a2022-03-23 19:57:18 +04008254#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
Richard Henderson121c8dd2023-08-23 17:13:14 -07008255 defined(HAVE_ARCH_PROC_CPUINFO) || \
8256 defined(HAVE_ARCH_PROC_HARDWARE)
Laurent Vivierde6b9932013-08-30 01:46:40 +02008257static int is_proc(const char *filename, const char *entry)
8258{
8259 return strcmp(filename, entry) == 0;
8260}
Laurent Vivierfff69382019-05-17 15:31:48 +02008261#endif
Laurent Vivierde6b9932013-08-30 01:46:40 +02008262
Marc-André Lureauee3eb3a2022-03-23 19:57:18 +04008263#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02008264static int open_net_route(CPUArchState *cpu_env, int fd)
Laurent Vivierde6b9932013-08-30 01:46:40 +02008265{
8266 FILE *fp;
8267 char *line = NULL;
8268 size_t len = 0;
8269 ssize_t read;
8270
8271 fp = fopen("/proc/net/route", "r");
8272 if (fp == NULL) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01008273 return -1;
Laurent Vivierde6b9932013-08-30 01:46:40 +02008274 }
8275
8276 /* read header */
8277
8278 read = getline(&line, &len, fp);
8279 dprintf(fd, "%s", line);
8280
8281 /* read routes */
8282
8283 while ((read = getline(&line, &len, fp)) != -1) {
8284 char iface[16];
8285 uint32_t dest, gw, mask;
8286 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
Peter Maydell9d0bd0c2019-02-05 17:42:07 +00008287 int fields;
8288
8289 fields = sscanf(line,
8290 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8291 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
8292 &mask, &mtu, &window, &irtt);
8293 if (fields != 11) {
8294 continue;
8295 }
Laurent Vivierde6b9932013-08-30 01:46:40 +02008296 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8297 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
8298 metric, tswap32(mask), mtu, window, irtt);
8299 }
8300
8301 free(line);
8302 fclose(fp);
8303
8304 return 0;
8305}
8306#endif
8307
Helge Dellerb8002052023-08-03 23:44:47 +02008308int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
Ilya Leoshkevich35be8982023-06-30 19:04:17 +01008309 int flags, mode_t mode, bool safe)
Alexander Graf3be14d02011-11-02 20:23:23 +01008310{
Helge Dellerb8002052023-08-03 23:44:47 +02008311 g_autofree char *proc_name = NULL;
8312 const char *pathname;
Alexander Graf3be14d02011-11-02 20:23:23 +01008313 struct fake_open {
8314 const char *filename;
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02008315 int (*fill)(CPUArchState *cpu_env, int fd);
Laurent Vivierde6b9932013-08-30 01:46:40 +02008316 int (*cmp)(const char *s1, const char *s2);
Alexander Graf3be14d02011-11-02 20:23:23 +01008317 };
8318 const struct fake_open *fake_open;
8319 static const struct fake_open fakes[] = {
Laurent Vivierde6b9932013-08-30 01:46:40 +02008320 { "maps", open_self_maps, is_proc_myself },
Ilya Leoshkevich77ae5762023-06-30 19:04:18 +01008321 { "smaps", open_self_smaps, is_proc_myself },
Laurent Vivierde6b9932013-08-30 01:46:40 +02008322 { "stat", open_self_stat, is_proc_myself },
8323 { "auxv", open_self_auxv, is_proc_myself },
Wim Vander Schelden76b94242014-06-18 11:02:39 +02008324 { "cmdline", open_self_cmdline, is_proc_myself },
Marc-André Lureauee3eb3a2022-03-23 19:57:18 +04008325#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
Laurent Vivierde6b9932013-08-30 01:46:40 +02008326 { "/proc/net/route", open_net_route, is_proc },
8327#endif
Richard Henderson121c8dd2023-08-23 17:13:14 -07008328#if defined(HAVE_ARCH_PROC_CPUINFO)
Laurent Vivierfff69382019-05-17 15:31:48 +02008329 { "/proc/cpuinfo", open_cpuinfo, is_proc },
8330#endif
Richard Henderson121c8dd2023-08-23 17:13:14 -07008331#if defined(HAVE_ARCH_PROC_HARDWARE)
Laurent Vivier4ab67132019-05-17 15:31:49 +02008332 { "/proc/hardware", open_hardware, is_proc },
8333#endif
Laurent Vivierde6b9932013-08-30 01:46:40 +02008334 { NULL, NULL, NULL }
Alexander Graf3be14d02011-11-02 20:23:23 +01008335 };
8336
Helge Dellerb8002052023-08-03 23:44:47 +02008337 /* if this is a file from /proc/ filesystem, expand full name */
8338 proc_name = realpath(fname, NULL);
8339 if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
8340 pathname = proc_name;
8341 } else {
8342 pathname = fname;
8343 }
8344
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03008345 if (is_proc_myself(pathname, "exe")) {
Ilya Leoshkevich35be8982023-06-30 19:04:17 +01008346 if (safe) {
8347 return safe_openat(dirfd, exec_path, flags, mode);
8348 } else {
8349 return openat(dirfd, exec_path, flags, mode);
8350 }
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03008351 }
8352
Alexander Graf3be14d02011-11-02 20:23:23 +01008353 for (fake_open = fakes; fake_open->filename; fake_open++) {
Laurent Vivierde6b9932013-08-30 01:46:40 +02008354 if (fake_open->cmp(pathname, fake_open->filename)) {
Alexander Graf3be14d02011-11-02 20:23:23 +01008355 break;
8356 }
8357 }
8358
8359 if (fake_open->filename) {
8360 const char *tmpdir;
8361 char filename[PATH_MAX];
8362 int fd, r;
8363
Rainer Müller5b63de62022-07-29 17:49:51 +02008364 fd = memfd_create("qemu-open", 0);
Alexander Graf3be14d02011-11-02 20:23:23 +01008365 if (fd < 0) {
Rainer Müller5b63de62022-07-29 17:49:51 +02008366 if (errno != ENOSYS) {
8367 return fd;
8368 }
8369 /* create temporary file to map stat to */
8370 tmpdir = getenv("TMPDIR");
8371 if (!tmpdir)
8372 tmpdir = "/tmp";
8373 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
8374 fd = mkstemp(filename);
8375 if (fd < 0) {
8376 return fd;
8377 }
8378 unlink(filename);
Alexander Graf3be14d02011-11-02 20:23:23 +01008379 }
Alexander Graf3be14d02011-11-02 20:23:23 +01008380
8381 if ((r = fake_open->fill(cpu_env, fd))) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01008382 int e = errno;
Alexander Graf3be14d02011-11-02 20:23:23 +01008383 close(fd);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01008384 errno = e;
Alexander Graf3be14d02011-11-02 20:23:23 +01008385 return r;
8386 }
8387 lseek(fd, 0, SEEK_SET);
8388
8389 return fd;
8390 }
8391
Ilya Leoshkevich35be8982023-06-30 19:04:17 +01008392 if (safe) {
8393 return safe_openat(dirfd, path(pathname), flags, mode);
8394 } else {
8395 return openat(dirfd, path(pathname), flags, mode);
8396 }
Alexander Graf3be14d02011-11-02 20:23:23 +01008397}
8398
Ilya Leoshkevicha4dab0a2023-06-30 19:04:16 +01008399ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
8400{
8401 ssize_t ret;
8402
8403 if (!pathname || !buf) {
8404 errno = EFAULT;
8405 return -1;
8406 }
8407
8408 if (!bufsiz) {
8409 /* Short circuit this for the magic exe check. */
8410 errno = EINVAL;
8411 return -1;
8412 }
8413
8414 if (is_proc_myself((const char *)pathname, "exe")) {
8415 /*
8416 * Don't worry about sign mismatch as earlier mapping
8417 * logic would have thrown a bad address error.
8418 */
8419 ret = MIN(strlen(exec_path), bufsiz);
8420 /* We cannot NUL terminate the string. */
8421 memcpy(buf, exec_path, ret);
8422 } else {
8423 ret = readlink(path(pathname), buf, bufsiz);
8424 }
8425
8426 return ret;
8427}
8428
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +02008429static int do_execv(CPUArchState *cpu_env, int dirfd,
8430 abi_long pathname, abi_long guest_argp,
8431 abi_long guest_envp, int flags, bool is_execveat)
Drew DeVault156e1f62022-11-04 18:36:31 +01008432{
8433 int ret;
8434 char **argp, **envp;
8435 int argc, envc;
8436 abi_ulong gp;
8437 abi_ulong addr;
8438 char **q;
8439 void *p;
8440
8441 argc = 0;
8442
8443 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
8444 if (get_user_ual(addr, gp)) {
8445 return -TARGET_EFAULT;
8446 }
8447 if (!addr) {
8448 break;
8449 }
8450 argc++;
8451 }
8452 envc = 0;
8453 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
8454 if (get_user_ual(addr, gp)) {
8455 return -TARGET_EFAULT;
8456 }
8457 if (!addr) {
8458 break;
8459 }
8460 envc++;
8461 }
8462
8463 argp = g_new0(char *, argc + 1);
8464 envp = g_new0(char *, envc + 1);
8465
8466 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
8467 if (get_user_ual(addr, gp)) {
8468 goto execve_efault;
8469 }
8470 if (!addr) {
8471 break;
8472 }
8473 *q = lock_user_string(addr);
8474 if (!*q) {
8475 goto execve_efault;
8476 }
8477 }
8478 *q = NULL;
8479
8480 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
8481 if (get_user_ual(addr, gp)) {
8482 goto execve_efault;
8483 }
8484 if (!addr) {
8485 break;
8486 }
8487 *q = lock_user_string(addr);
8488 if (!*q) {
8489 goto execve_efault;
8490 }
8491 }
8492 *q = NULL;
8493
8494 /*
8495 * Although execve() is not an interruptible syscall it is
8496 * a special case where we must use the safe_syscall wrapper:
8497 * if we allow a signal to happen before we make the host
8498 * syscall then we will 'lose' it, because at the point of
8499 * execve the process leaves QEMU's control. So we use the
8500 * safe syscall wrapper to ensure that we either take the
8501 * signal as a guest signal, or else it does not happen
8502 * before the execve completes and makes it the other
8503 * program's problem.
8504 */
8505 p = lock_user_string(pathname);
8506 if (!p) {
8507 goto execve_efault;
8508 }
8509
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +02008510 const char *exe = p;
Drew DeVault156e1f62022-11-04 18:36:31 +01008511 if (is_proc_myself(p, "exe")) {
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +02008512 exe = exec_path;
Drew DeVault156e1f62022-11-04 18:36:31 +01008513 }
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +02008514 ret = is_execveat
8515 ? safe_execveat(dirfd, exe, argp, envp, flags)
8516 : safe_execve(exe, argp, envp);
8517 ret = get_errno(ret);
Drew DeVault156e1f62022-11-04 18:36:31 +01008518
8519 unlock_user(p, pathname, 0);
8520
8521 goto execve_end;
8522
8523execve_efault:
8524 ret = -TARGET_EFAULT;
8525
8526execve_end:
8527 for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
8528 if (get_user_ual(addr, gp) || !addr) {
8529 break;
8530 }
8531 unlock_user(*q, addr, 0);
8532 }
8533 for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
8534 if (get_user_ual(addr, gp) || !addr) {
8535 break;
8536 }
8537 unlock_user(*q, addr, 0);
8538 }
8539
8540 g_free(argp);
8541 g_free(envp);
8542 return ret;
8543}
8544
Alexander Grafaecc8862014-11-10 21:33:03 +01008545#define TIMER_MAGIC 0x0caf0000
8546#define TIMER_MAGIC_MASK 0xffff0000
8547
8548/* Convert QEMU provided timer ID back to internal 16bit index format */
8549static target_timer_t get_timer_id(abi_long arg)
8550{
8551 target_timer_t timerid = arg;
8552
8553 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
8554 return -TARGET_EINVAL;
8555 }
8556
8557 timerid &= 0xffff;
8558
8559 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
8560 return -TARGET_EINVAL;
8561 }
8562
8563 return timerid;
8564}
8565
Samuel Thibault2e0a8712018-01-09 21:16:43 +01008566static int target_to_host_cpu_mask(unsigned long *host_mask,
8567 size_t host_size,
8568 abi_ulong target_addr,
8569 size_t target_size)
8570{
8571 unsigned target_bits = sizeof(abi_ulong) * 8;
8572 unsigned host_bits = sizeof(*host_mask) * 8;
8573 abi_ulong *target_mask;
8574 unsigned i, j;
8575
8576 assert(host_size >= target_size);
8577
8578 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
8579 if (!target_mask) {
8580 return -TARGET_EFAULT;
8581 }
8582 memset(host_mask, 0, host_size);
8583
8584 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8585 unsigned bit = i * target_bits;
8586 abi_ulong val;
8587
8588 __get_user(val, &target_mask[i]);
8589 for (j = 0; j < target_bits; j++, bit++) {
8590 if (val & (1UL << j)) {
8591 host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
8592 }
8593 }
8594 }
8595
8596 unlock_user(target_mask, target_addr, 0);
8597 return 0;
8598}
8599
8600static int host_to_target_cpu_mask(const unsigned long *host_mask,
8601 size_t host_size,
8602 abi_ulong target_addr,
8603 size_t target_size)
8604{
8605 unsigned target_bits = sizeof(abi_ulong) * 8;
8606 unsigned host_bits = sizeof(*host_mask) * 8;
8607 abi_ulong *target_mask;
8608 unsigned i, j;
8609
8610 assert(host_size >= target_size);
8611
8612 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
8613 if (!target_mask) {
8614 return -TARGET_EFAULT;
8615 }
8616
8617 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8618 unsigned bit = i * target_bits;
8619 abi_ulong val = 0;
8620
8621 for (j = 0; j < target_bits; j++, bit++) {
8622 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
8623 val |= 1UL << j;
8624 }
8625 }
8626 __put_user(val, &target_mask[i]);
8627 }
8628
8629 unlock_user(target_mask, target_addr, target_size);
8630 return 0;
8631}
8632
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008633#ifdef TARGET_NR_getdents
Richard Hendersonaee14c72021-11-14 11:35:39 +01008634static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008635{
Richard Hendersonaee14c72021-11-14 11:35:39 +01008636 g_autofree void *hdirp = NULL;
8637 void *tdirp;
8638 int hlen, hoff, toff;
8639 int hreclen, treclen;
8640 off64_t prev_diroff = 0;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008641
Richard Hendersonaee14c72021-11-14 11:35:39 +01008642 hdirp = g_try_malloc(count);
8643 if (!hdirp) {
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008644 return -TARGET_ENOMEM;
8645 }
8646
Richard Hendersonaee14c72021-11-14 11:35:39 +01008647#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8648 hlen = sys_getdents(dirfd, hdirp, count);
8649#else
8650 hlen = sys_getdents64(dirfd, hdirp, count);
8651#endif
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008652
Richard Hendersonaee14c72021-11-14 11:35:39 +01008653 hlen = get_errno(hlen);
8654 if (is_error(hlen)) {
8655 return hlen;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008656 }
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008657
Richard Hendersonaee14c72021-11-14 11:35:39 +01008658 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8659 if (!tdirp) {
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008660 return -TARGET_EFAULT;
8661 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008662
8663 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8664#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8665 struct linux_dirent *hde = hdirp + hoff;
8666#else
8667 struct linux_dirent64 *hde = hdirp + hoff;
8668#endif
8669 struct target_dirent *tde = tdirp + toff;
8670 int namelen;
8671 uint8_t type;
8672
8673 namelen = strlen(hde->d_name);
8674 hreclen = hde->d_reclen;
8675 treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
8676 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
8677
8678 if (toff + treclen > count) {
8679 /*
8680 * If the host struct is smaller than the target struct, or
8681 * requires less alignment and thus packs into less space,
8682 * then the host can return more entries than we can pass
8683 * on to the guest.
8684 */
8685 if (toff == 0) {
8686 toff = -TARGET_EINVAL; /* result buffer is too small */
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008687 break;
8688 }
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008689 /*
Richard Hendersonaee14c72021-11-14 11:35:39 +01008690 * Return what we have, resetting the file pointer to the
8691 * location of the first record not returned.
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008692 */
Richard Hendersonaee14c72021-11-14 11:35:39 +01008693 lseek64(dirfd, prev_diroff, SEEK_SET);
8694 break;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008695 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008696
8697 prev_diroff = hde->d_off;
8698 tde->d_ino = tswapal(hde->d_ino);
8699 tde->d_off = tswapal(hde->d_off);
8700 tde->d_reclen = tswap16(treclen);
8701 memcpy(tde->d_name, hde->d_name, namelen + 1);
8702
8703 /*
8704 * The getdents type is in what was formerly a padding byte at the
8705 * end of the structure.
8706 */
8707#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8708 type = *((uint8_t *)hde + hreclen - 1);
8709#else
8710 type = hde->d_type;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008711#endif
Richard Hendersonaee14c72021-11-14 11:35:39 +01008712 *((uint8_t *)tde + treclen - 1) = type;
8713 }
8714
8715 unlock_user(tdirp, arg2, toff);
8716 return toff;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008717}
8718#endif /* TARGET_NR_getdents */
8719
8720#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
Richard Hendersonaee14c72021-11-14 11:35:39 +01008721static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008722{
Richard Hendersonaee14c72021-11-14 11:35:39 +01008723 g_autofree void *hdirp = NULL;
8724 void *tdirp;
8725 int hlen, hoff, toff;
8726 int hreclen, treclen;
8727 off64_t prev_diroff = 0;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008728
Richard Hendersonaee14c72021-11-14 11:35:39 +01008729 hdirp = g_try_malloc(count);
8730 if (!hdirp) {
8731 return -TARGET_ENOMEM;
8732 }
8733
8734 hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
8735 if (is_error(hlen)) {
8736 return hlen;
8737 }
8738
8739 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8740 if (!tdirp) {
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008741 return -TARGET_EFAULT;
8742 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008743
8744 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8745 struct linux_dirent64 *hde = hdirp + hoff;
8746 struct target_dirent64 *tde = tdirp + toff;
8747 int namelen;
8748
8749 namelen = strlen(hde->d_name) + 1;
8750 hreclen = hde->d_reclen;
8751 treclen = offsetof(struct target_dirent64, d_name) + namelen;
8752 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
8753
8754 if (toff + treclen > count) {
8755 /*
8756 * If the host struct is smaller than the target struct, or
8757 * requires less alignment and thus packs into less space,
8758 * then the host can return more entries than we can pass
8759 * on to the guest.
8760 */
8761 if (toff == 0) {
8762 toff = -TARGET_EINVAL; /* result buffer is too small */
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008763 break;
8764 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008765 /*
8766 * Return what we have, resetting the file pointer to the
8767 * location of the first record not returned.
8768 */
8769 lseek64(dirfd, prev_diroff, SEEK_SET);
8770 break;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008771 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008772
8773 prev_diroff = hde->d_off;
8774 tde->d_ino = tswap64(hde->d_ino);
8775 tde->d_off = tswap64(hde->d_off);
8776 tde->d_reclen = tswap16(treclen);
8777 tde->d_type = hde->d_type;
8778 memcpy(tde->d_name, hde->d_name, namelen);
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008779 }
Richard Hendersonaee14c72021-11-14 11:35:39 +01008780
8781 unlock_user(tdirp, arg2, toff);
8782 return toff;
Richard Hendersonfd08ddb2021-11-14 11:35:36 +01008783}
8784#endif /* TARGET_NR_getdents64 */
8785
Robbin Ehn9e1c7d92023-06-19 10:24:03 +02008786#if defined(TARGET_NR_riscv_hwprobe)
8787
8788#define RISCV_HWPROBE_KEY_MVENDORID 0
8789#define RISCV_HWPROBE_KEY_MARCHID 1
8790#define RISCV_HWPROBE_KEY_MIMPID 2
8791
8792#define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
8793#define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
8794
8795#define RISCV_HWPROBE_KEY_IMA_EXT_0 4
8796#define RISCV_HWPROBE_IMA_FD (1 << 0)
8797#define RISCV_HWPROBE_IMA_C (1 << 1)
Robbin Ehnbb0a45e2023-08-28 16:56:59 +02008798#define RISCV_HWPROBE_IMA_V (1 << 2)
8799#define RISCV_HWPROBE_EXT_ZBA (1 << 3)
8800#define RISCV_HWPROBE_EXT_ZBB (1 << 4)
8801#define RISCV_HWPROBE_EXT_ZBS (1 << 5)
Robbin Ehn9e1c7d92023-06-19 10:24:03 +02008802
8803#define RISCV_HWPROBE_KEY_CPUPERF_0 5
8804#define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
8805#define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
8806#define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
8807#define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
8808#define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
8809#define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
8810
8811struct riscv_hwprobe {
8812 abi_llong key;
8813 abi_ullong value;
8814};
8815
8816static void risc_hwprobe_fill_pairs(CPURISCVState *env,
8817 struct riscv_hwprobe *pair,
8818 size_t pair_count)
8819{
8820 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
8821
8822 for (; pair_count > 0; pair_count--, pair++) {
8823 abi_llong key;
8824 abi_ullong value;
8825 __put_user(0, &pair->value);
8826 __get_user(key, &pair->key);
8827 switch (key) {
8828 case RISCV_HWPROBE_KEY_MVENDORID:
8829 __put_user(cfg->mvendorid, &pair->value);
8830 break;
8831 case RISCV_HWPROBE_KEY_MARCHID:
8832 __put_user(cfg->marchid, &pair->value);
8833 break;
8834 case RISCV_HWPROBE_KEY_MIMPID:
8835 __put_user(cfg->mimpid, &pair->value);
8836 break;
8837 case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
8838 value = riscv_has_ext(env, RVI) &&
8839 riscv_has_ext(env, RVM) &&
8840 riscv_has_ext(env, RVA) ?
8841 RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
8842 __put_user(value, &pair->value);
8843 break;
8844 case RISCV_HWPROBE_KEY_IMA_EXT_0:
8845 value = riscv_has_ext(env, RVF) &&
8846 riscv_has_ext(env, RVD) ?
8847 RISCV_HWPROBE_IMA_FD : 0;
8848 value |= riscv_has_ext(env, RVC) ?
Robbin Ehnbb0a45e2023-08-28 16:56:59 +02008849 RISCV_HWPROBE_IMA_C : 0;
8850 value |= riscv_has_ext(env, RVV) ?
8851 RISCV_HWPROBE_IMA_V : 0;
8852 value |= cfg->ext_zba ?
8853 RISCV_HWPROBE_EXT_ZBA : 0;
8854 value |= cfg->ext_zbb ?
8855 RISCV_HWPROBE_EXT_ZBB : 0;
8856 value |= cfg->ext_zbs ?
8857 RISCV_HWPROBE_EXT_ZBS : 0;
Robbin Ehn9e1c7d92023-06-19 10:24:03 +02008858 __put_user(value, &pair->value);
8859 break;
8860 case RISCV_HWPROBE_KEY_CPUPERF_0:
8861 __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
8862 break;
8863 default:
8864 __put_user(-1, &pair->key);
8865 break;
8866 }
8867 }
8868}
8869
8870static int cpu_set_valid(abi_long arg3, abi_long arg4)
8871{
8872 int ret, i, tmp;
8873 size_t host_mask_size, target_mask_size;
8874 unsigned long *host_mask;
8875
8876 /*
8877 * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
8878 * arg3 contains the cpu count.
8879 */
8880 tmp = (8 * sizeof(abi_ulong));
8881 target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
8882 host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
8883 ~(sizeof(*host_mask) - 1);
8884
8885 host_mask = alloca(host_mask_size);
8886
8887 ret = target_to_host_cpu_mask(host_mask, host_mask_size,
8888 arg4, target_mask_size);
8889 if (ret != 0) {
8890 return ret;
8891 }
8892
8893 for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
8894 if (host_mask[i] != 0) {
8895 return 0;
8896 }
8897 }
8898 return -TARGET_EINVAL;
8899}
8900
8901static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
8902 abi_long arg2, abi_long arg3,
8903 abi_long arg4, abi_long arg5)
8904{
8905 int ret;
8906 struct riscv_hwprobe *host_pairs;
8907
8908 /* flags must be 0 */
8909 if (arg5 != 0) {
8910 return -TARGET_EINVAL;
8911 }
8912
8913 /* check cpu_set */
8914 if (arg3 != 0) {
8915 ret = cpu_set_valid(arg3, arg4);
8916 if (ret != 0) {
8917 return ret;
8918 }
8919 } else if (arg4 != 0) {
8920 return -TARGET_EINVAL;
8921 }
8922
8923 /* no pairs */
8924 if (arg2 == 0) {
8925 return 0;
8926 }
8927
8928 host_pairs = lock_user(VERIFY_WRITE, arg1,
8929 sizeof(*host_pairs) * (size_t)arg2, 0);
8930 if (host_pairs == NULL) {
8931 return -TARGET_EFAULT;
8932 }
8933 risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
8934 unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
8935 return 0;
8936}
8937#endif /* TARGET_NR_riscv_hwprobe */
8938
YAMAMOTO Takashie10fbe82021-05-31 14:50:12 +09008939#if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
8940_syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
8941#endif
8942
Thomas Weißschuh7f696cdd2023-04-24 17:34:29 +02008943#if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
8944#define __NR_sys_open_tree __NR_open_tree
8945_syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
8946 unsigned int, __flags)
8947#endif
8948
Thomas Weißschuh4b2d2752023-04-24 17:34:28 +02008949#if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
8950#define __NR_sys_move_mount __NR_move_mount
8951_syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
8952 int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
8953#endif
8954
Richard Hendersondc1ce182018-08-18 12:01:04 -07008955/* This is an internal helper for do_syscall so that it is easier
8956 * to have a single return point, so that actions, such as logging
8957 * of syscall results, can be performed.
8958 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
8959 */
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +02008960static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
Richard Hendersondc1ce182018-08-18 12:01:04 -07008961 abi_long arg2, abi_long arg3, abi_long arg4,
8962 abi_long arg5, abi_long arg6, abi_long arg7,
8963 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00008964{
Richard Henderson29a0af62019-03-22 16:07:18 -07008965 CPUState *cpu = env_cpu(cpu_env);
blueswir1992f48a2007-10-14 16:27:31 +00008966 abi_long ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008967#if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
8968 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
Aleksandar Rikaloefa92182019-06-28 12:43:34 +02008969 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
8970 || defined(TARGET_NR_statx)
bellard31e31b82003-02-18 22:55:36 +00008971 struct stat st;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008972#endif
8973#if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
8974 || defined(TARGET_NR_fstatfs)
bellard56c8f682005-11-28 22:28:41 +00008975 struct statfs stfs;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02008976#endif
pbrook53a59602006-03-25 19:31:22 +00008977 void *p;
ths3b46e622007-09-17 08:09:54 +00008978
bellard31e31b82003-02-18 22:55:36 +00008979 switch(num) {
8980 case TARGET_NR_exit:
Andreas Färber9b056fc2013-06-24 23:53:10 +02008981 /* In old applications this may be used to implement _exit(2).
zhaolichang6f9ff552020-09-17 15:50:25 +08008982 However in threaded applications it is used for thread termination,
Andreas Färber9b056fc2013-06-24 23:53:10 +02008983 and _exit_group is used for application termination.
8984 Do thread termination if we have more then one thread. */
Timothy E Baldwina0995882016-05-27 15:51:56 +01008985
8986 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01008987 return -QEMU_ERESTARTSYS;
Timothy E Baldwina0995882016-05-27 15:51:56 +01008988 }
8989
Alex Bennée1f81ce92020-05-20 15:05:39 +01008990 pthread_mutex_lock(&clone_lock);
Alex Bennéedd1f6342016-09-30 22:31:01 +01008991
Andreas Färberbdc44642013-06-24 23:50:24 +02008992 if (CPU_NEXT(first_cpu)) {
Alex Bennée1f81ce92020-05-20 15:05:39 +01008993 TaskState *ts = cpu->opaque;
pbrookc2764712009-03-07 15:24:59 +00008994
Richard Henderson6490d9a2023-01-24 10:10:19 -10008995 if (ts->child_tidptr) {
8996 put_user_u32(0, ts->child_tidptr);
8997 do_sys_futex(g2h(cpu, ts->child_tidptr),
8998 FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
8999 }
9000
9001 object_unparent(OBJECT(cpu));
Alex Bennée1f81ce92020-05-20 15:05:39 +01009002 object_unref(OBJECT(cpu));
9003 /*
9004 * At this point the CPU should be unrealized and removed
9005 * from cpu lists. We can clean-up the rest of the thread
9006 * data without the lock held.
9007 */
Alex Bennéedd1f6342016-09-30 22:31:01 +01009008
Alex Bennée1f81ce92020-05-20 15:05:39 +01009009 pthread_mutex_unlock(&clone_lock);
Alex Bennéedd1f6342016-09-30 22:31:01 +01009010
Andreas Färbera2247f82013-06-09 19:47:04 +02009011 thread_cpu = NULL;
Andreas Färber9b056fc2013-06-24 23:53:10 +02009012 g_free(ts);
Emilio G. Cota70903762015-08-23 20:23:41 -04009013 rcu_unregister_thread();
Andreas Färber9b056fc2013-06-24 23:53:10 +02009014 pthread_exit(NULL);
9015 }
Alex Bennéedd1f6342016-09-30 22:31:01 +01009016
Alex Bennée1f81ce92020-05-20 15:05:39 +01009017 pthread_mutex_unlock(&clone_lock);
Alex Bennée708b6a62018-06-22 17:09:10 +01009018 preexit_cleanup(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00009019 _exit(arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009020 return 0; /* avoid warning */
bellard31e31b82003-02-18 22:55:36 +00009021 case TARGET_NR_read:
Andreas Schwabba584f12019-03-05 17:45:05 +01009022 if (arg2 == 0 && arg3 == 0) {
9023 return get_errno(safe_read(arg1, 0, 0));
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009024 } else {
aurel3238d840e2009-01-30 19:48:17 +00009025 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009026 return -TARGET_EFAULT;
Timothy E Baldwin50afd022016-05-12 18:47:47 +01009027 ret = get_errno(safe_read(arg1, p, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02009028 if (ret >= 0 &&
Laurent Vivier5d4d3662015-10-28 21:40:43 +01009029 fd_trans_host_to_target_data(arg1)) {
9030 ret = fd_trans_host_to_target_data(arg1)(p, ret);
Laurent Viviere36800c2015-10-02 14:48:09 +02009031 }
aurel3238d840e2009-01-30 19:48:17 +00009032 unlock_user(p, arg2, ret);
9033 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009034 return ret;
bellard31e31b82003-02-18 22:55:36 +00009035 case TARGET_NR_write:
Tony Garnock-Jones58cfa6c2018-09-08 19:22:05 +01009036 if (arg2 == 0 && arg3 == 0) {
9037 return get_errno(safe_write(arg1, 0, 0));
9038 }
bellard579a97f2007-11-11 14:26:47 +00009039 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009040 return -TARGET_EFAULT;
Laurent Vivier04b9bcf2017-03-01 10:37:47 +01009041 if (fd_trans_target_to_host_data(arg1)) {
9042 void *copy = g_malloc(arg3);
9043 memcpy(copy, p, arg3);
9044 ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
9045 if (ret >= 0) {
9046 ret = get_errno(safe_write(arg1, copy, ret));
9047 }
9048 g_free(copy);
9049 } else {
9050 ret = get_errno(safe_write(arg1, p, arg3));
9051 }
pbrook53a59602006-03-25 19:31:22 +00009052 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009053 return ret;
9054
Chen Gang704eff62015-08-21 05:37:33 +08009055#ifdef TARGET_NR_open
bellard31e31b82003-02-18 22:55:36 +00009056 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00009057 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009058 return -TARGET_EFAULT;
Ilya Leoshkevicha4dab0a2023-06-30 19:04:16 +01009059 ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
Riku Voipio0b2effd2014-08-06 10:36:37 +03009060 target_to_host_bitmask(arg2, fcntl_flags_tbl),
Ilya Leoshkevich35be8982023-06-30 19:04:17 +01009061 arg3, true));
Laurent Viviere36800c2015-10-02 14:48:09 +02009062 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00009063 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009064 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009065#endif
ths82424832007-09-24 09:21:55 +00009066 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00009067 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009068 return -TARGET_EFAULT;
Ilya Leoshkevicha4dab0a2023-06-30 19:04:16 +01009069 ret = get_errno(do_guest_openat(cpu_env, arg1, p,
Riku Voipio0b2effd2014-08-06 10:36:37 +03009070 target_to_host_bitmask(arg3, fcntl_flags_tbl),
Ilya Leoshkevich35be8982023-06-30 19:04:17 +01009071 arg4, true));
Laurent Viviere36800c2015-10-02 14:48:09 +02009072 fd_trans_unregister(ret);
bellard579a97f2007-11-11 14:26:47 +00009073 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009074 return ret;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02009075#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9076 case TARGET_NR_name_to_handle_at:
9077 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009078 return ret;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02009079#endif
9080#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9081 case TARGET_NR_open_by_handle_at:
9082 ret = do_open_by_handle_at(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02009083 fd_trans_unregister(ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009084 return ret;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02009085#endif
Helge Dellercc054c62022-09-18 21:45:46 +02009086#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
9087 case TARGET_NR_pidfd_open:
9088 return get_errno(pidfd_open(arg1, arg2));
9089#endif
9090#if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
9091 case TARGET_NR_pidfd_send_signal:
9092 {
Laurent Vivier46187d72022-10-05 18:38:26 +02009093 siginfo_t uinfo, *puinfo;
Helge Dellercc054c62022-09-18 21:45:46 +02009094
Laurent Vivier46187d72022-10-05 18:38:26 +02009095 if (arg3) {
9096 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
9097 if (!p) {
9098 return -TARGET_EFAULT;
9099 }
9100 target_to_host_siginfo(&uinfo, p);
9101 unlock_user(p, arg3, 0);
9102 puinfo = &uinfo;
9103 } else {
9104 puinfo = NULL;
Helge Dellercc054c62022-09-18 21:45:46 +02009105 }
Helge Dellercc054c62022-09-18 21:45:46 +02009106 ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
Laurent Vivier46187d72022-10-05 18:38:26 +02009107 puinfo, arg4));
Helge Dellercc054c62022-09-18 21:45:46 +02009108 }
9109 return ret;
9110#endif
9111#if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
9112 case TARGET_NR_pidfd_getfd:
9113 return get_errno(pidfd_getfd(arg1, arg2, arg3));
9114#endif
bellard31e31b82003-02-18 22:55:36 +00009115 case TARGET_NR_close:
Laurent Viviere36800c2015-10-02 14:48:09 +02009116 fd_trans_unregister(arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009117 return get_errno(close(arg1));
Helge Delleraf804f32022-10-25 04:34:14 +02009118#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
9119 case TARGET_NR_close_range:
9120 ret = get_errno(sys_close_range(arg1, arg2, arg3));
9121 if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
9122 abi_long fd, maxfd;
9123 maxfd = MIN(arg2, target_fd_max);
9124 for (fd = arg1; fd < maxfd; fd++) {
9125 fd_trans_unregister(fd);
9126 }
9127 }
9128 return ret;
9129#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009130
bellard31e31b82003-02-18 22:55:36 +00009131 case TARGET_NR_brk:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009132 return do_brk(arg1);
Chen Gang704eff62015-08-21 05:37:33 +08009133#ifdef TARGET_NR_fork
bellard31e31b82003-02-18 22:55:36 +00009134 case TARGET_NR_fork:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009135 return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
Chen Gang704eff62015-08-21 05:37:33 +08009136#endif
thse5febef2007-04-01 18:31:35 +00009137#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00009138 case TARGET_NR_waitpid:
9139 {
pbrook53a59602006-03-25 19:31:22 +00009140 int status;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01009141 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
Alexander Graf53795572011-11-24 00:44:43 +01009142 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00009143 && put_user_s32(host_to_target_waitstatus(status), arg2))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009144 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00009145 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009146 return ret;
thse5febef2007-04-01 18:31:35 +00009147#endif
pbrookf0cbb612008-05-30 18:20:05 +00009148#ifdef TARGET_NR_waitid
9149 case TARGET_NR_waitid:
9150 {
9151 siginfo_t info;
9152 info.si_pid = 0;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01009153 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
pbrookf0cbb612008-05-30 18:20:05 +00009154 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009155 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009156 return -TARGET_EFAULT;
pbrookf0cbb612008-05-30 18:20:05 +00009157 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05009158 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00009159 }
9160 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009161 return ret;
pbrookf0cbb612008-05-30 18:20:05 +00009162#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009163#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009164 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00009165 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009166 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009167 ret = get_errno(creat(p, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02009168 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00009169 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009170 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009171#endif
Chen Gang704eff62015-08-21 05:37:33 +08009172#ifdef TARGET_NR_link
bellard31e31b82003-02-18 22:55:36 +00009173 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00009174 {
9175 void * p2;
9176 p = lock_user_string(arg1);
9177 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00009178 if (!p || !p2)
9179 ret = -TARGET_EFAULT;
9180 else
9181 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00009182 unlock_user(p2, arg2, 0);
9183 unlock_user(p, arg1, 0);
9184 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009185 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009186#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009187#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00009188 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00009189 {
9190 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00009191 if (!arg2 || !arg4)
Richard Henderson2852aaf2018-08-18 12:01:06 -07009192 return -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00009193 p = lock_user_string(arg2);
9194 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00009195 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00009196 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00009197 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01009198 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00009199 unlock_user(p, arg2, 0);
9200 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00009201 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009202 return ret;
ths64f0ce42007-09-24 09:25:06 +00009203#endif
Chen Gang704eff62015-08-21 05:37:33 +08009204#ifdef TARGET_NR_unlink
bellard31e31b82003-02-18 22:55:36 +00009205 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00009206 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009207 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009208 ret = get_errno(unlink(p));
9209 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009210 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009211#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009212#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00009213 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00009214 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009215 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009216 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00009217 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009218 return ret;
balrogb7d35e62007-12-12 00:40:24 +00009219#endif
Drew DeVault55bbe4d2022-11-04 18:36:32 +01009220 case TARGET_NR_execveat:
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +02009221 return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
bellard31e31b82003-02-18 22:55:36 +00009222 case TARGET_NR_execve:
Pierrick Bouvier7a8d9f32023-07-05 14:10:23 +02009223 return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
bellard31e31b82003-02-18 22:55:36 +00009224 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00009225 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009226 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009227 ret = get_errno(chdir(p));
9228 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009229 return ret;
bellarda315a142005-01-30 22:59:18 +00009230#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00009231 case TARGET_NR_time:
9232 {
pbrook53a59602006-03-25 19:31:22 +00009233 time_t host_time;
9234 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00009235 if (!is_error(ret)
9236 && arg1
9237 && put_user_sal(host_time, arg1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009238 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00009239 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009240 return ret;
bellarda315a142005-01-30 22:59:18 +00009241#endif
Chen Gang704eff62015-08-21 05:37:33 +08009242#ifdef TARGET_NR_mknod
bellard31e31b82003-02-18 22:55:36 +00009243 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00009244 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009245 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009246 ret = get_errno(mknod(p, arg2, arg3));
9247 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009248 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009249#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009250#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00009251 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00009252 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009253 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009254 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00009255 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009256 return ret;
ths75ac37a2007-09-24 09:23:05 +00009257#endif
Chen Gang704eff62015-08-21 05:37:33 +08009258#ifdef TARGET_NR_chmod
bellard31e31b82003-02-18 22:55:36 +00009259 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00009260 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009261 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009262 ret = get_errno(chmod(p, arg2));
9263 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009264 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009265#endif
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02009266#ifdef TARGET_NR_lseek
bellard31e31b82003-02-18 22:55:36 +00009267 case TARGET_NR_lseek:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009268 return get_errno(lseek(arg1, arg2, arg3));
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +02009269#endif
Richard Henderson92317332010-05-03 10:07:53 -07009270#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
9271 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00009272 case TARGET_NR_getxpid:
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02009273 cpu_env->ir[IR_A4] = getppid();
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009274 return get_errno(getpid());
Richard Henderson92317332010-05-03 10:07:53 -07009275#endif
9276#ifdef TARGET_NR_getpid
9277 case TARGET_NR_getpid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009278 return get_errno(getpid());
Richard Henderson92317332010-05-03 10:07:53 -07009279#endif
bellard31e31b82003-02-18 22:55:36 +00009280 case TARGET_NR_mount:
Paul Burton356d7712014-06-22 11:25:37 +01009281 {
9282 /* need to look at the data field */
9283 void *p2, *p3;
9284
9285 if (arg1) {
9286 p = lock_user_string(arg1);
9287 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009288 return -TARGET_EFAULT;
Paul Burton356d7712014-06-22 11:25:37 +01009289 }
9290 } else {
9291 p = NULL;
9292 }
9293
9294 p2 = lock_user_string(arg2);
9295 if (!p2) {
9296 if (arg1) {
9297 unlock_user(p, arg1, 0);
9298 }
Richard Henderson2852aaf2018-08-18 12:01:06 -07009299 return -TARGET_EFAULT;
Paul Burton356d7712014-06-22 11:25:37 +01009300 }
9301
9302 if (arg3) {
9303 p3 = lock_user_string(arg3);
9304 if (!p3) {
9305 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00009306 unlock_user(p, arg1, 0);
Paul Burton356d7712014-06-22 11:25:37 +01009307 }
9308 unlock_user(p2, arg2, 0);
Richard Henderson2852aaf2018-08-18 12:01:06 -07009309 return -TARGET_EFAULT;
Paul Burton356d7712014-06-22 11:25:37 +01009310 }
9311 } else {
9312 p3 = NULL;
9313 }
9314
9315 /* FIXME - arg5 should be locked, but it isn't clear how to
9316 * do that since it's not guaranteed to be a NULL-terminated
9317 * string.
9318 */
9319 if (!arg5) {
9320 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
9321 } else {
Richard Henderson3e8f1622021-02-12 10:48:43 -08009322 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
Paul Burton356d7712014-06-22 11:25:37 +01009323 }
9324 ret = get_errno(ret);
9325
9326 if (arg1) {
9327 unlock_user(p, arg1, 0);
9328 }
9329 unlock_user(p2, arg2, 0);
9330 if (arg3) {
9331 unlock_user(p3, arg3, 0);
9332 }
9333 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009334 return ret;
Laurent Vivier6eb9dbf2020-05-02 21:46:42 +02009335#if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9336#if defined(TARGET_NR_umount)
bellard31e31b82003-02-18 22:55:36 +00009337 case TARGET_NR_umount:
Laurent Vivier6eb9dbf2020-05-02 21:46:42 +02009338#endif
9339#if defined(TARGET_NR_oldumount)
9340 case TARGET_NR_oldumount:
9341#endif
bellard579a97f2007-11-11 14:26:47 +00009342 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009343 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009344 ret = get_errno(umount(p));
9345 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009346 return ret;
thse5febef2007-04-01 18:31:35 +00009347#endif
Thomas Weißschuh4b2d2752023-04-24 17:34:28 +02009348#if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9349 case TARGET_NR_move_mount:
9350 {
9351 void *p2, *p4;
9352
9353 if (!arg2 || !arg4) {
9354 return -TARGET_EFAULT;
9355 }
9356
9357 p2 = lock_user_string(arg2);
9358 if (!p2) {
9359 return -TARGET_EFAULT;
9360 }
9361
9362 p4 = lock_user_string(arg4);
9363 if (!p4) {
9364 unlock_user(p2, arg2, 0);
9365 return -TARGET_EFAULT;
9366 }
9367 ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
9368
9369 unlock_user(p2, arg2, 0);
9370 unlock_user(p4, arg4, 0);
9371
9372 return ret;
9373 }
9374#endif
Thomas Weißschuh7f696cdd2023-04-24 17:34:29 +02009375#if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9376 case TARGET_NR_open_tree:
9377 {
9378 void *p2;
9379 int host_flags;
9380
9381 if (!arg2) {
9382 return -TARGET_EFAULT;
9383 }
9384
9385 p2 = lock_user_string(arg2);
9386 if (!p2) {
9387 return -TARGET_EFAULT;
9388 }
9389
9390 host_flags = arg3 & ~TARGET_O_CLOEXEC;
9391 if (arg3 & TARGET_O_CLOEXEC) {
9392 host_flags |= O_CLOEXEC;
9393 }
9394
9395 ret = get_errno(sys_open_tree(arg1, p2, host_flags));
9396
9397 unlock_user(p2, arg2, 0);
9398
9399 return ret;
9400 }
9401#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009402#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009403 case TARGET_NR_stime:
9404 {
Laurent Vivier0f1f2d42019-11-12 15:25:56 +01009405 struct timespec ts;
9406 ts.tv_nsec = 0;
9407 if (get_user_sal(ts.tv_sec, arg1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009408 return -TARGET_EFAULT;
Laurent Vivier0f1f2d42019-11-12 15:25:56 +01009409 }
9410 return get_errno(clock_settime(CLOCK_REALTIME, &ts));
bellard31e31b82003-02-18 22:55:36 +00009411 }
j_mayer7a3148a2007-04-05 07:13:51 +00009412#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009413#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009414 case TARGET_NR_alarm:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009415 return alarm(arg1);
j_mayer7a3148a2007-04-05 07:13:51 +00009416#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009417#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009418 case TARGET_NR_pause:
Timothy E Baldwinf59ec602016-05-27 15:51:55 +01009419 if (!block_signals()) {
9420 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
9421 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009422 return -TARGET_EINTR;
j_mayer7a3148a2007-04-05 07:13:51 +00009423#endif
thse5febef2007-04-01 18:31:35 +00009424#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00009425 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00009426 {
pbrook53a59602006-03-25 19:31:22 +00009427 struct utimbuf tbuf, *host_tbuf;
9428 struct target_utimbuf *target_tbuf;
9429 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00009430 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009431 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02009432 tbuf.actime = tswapal(target_tbuf->actime);
9433 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00009434 unlock_user_struct(target_tbuf, arg2, 0);
9435 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00009436 } else {
pbrook53a59602006-03-25 19:31:22 +00009437 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00009438 }
bellard579a97f2007-11-11 14:26:47 +00009439 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009440 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009441 ret = get_errno(utime(p, host_tbuf));
9442 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00009443 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009444 return ret;
thse5febef2007-04-01 18:31:35 +00009445#endif
Chen Gang704eff62015-08-21 05:37:33 +08009446#ifdef TARGET_NR_utimes
bellard978a66f2004-12-06 22:58:05 +00009447 case TARGET_NR_utimes:
9448 {
bellard978a66f2004-12-06 22:58:05 +00009449 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00009450 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00009451 if (copy_from_user_timeval(&tv[0], arg2)
9452 || copy_from_user_timeval(&tv[1],
9453 arg2 + sizeof(struct target_timeval)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009454 return -TARGET_EFAULT;
bellard978a66f2004-12-06 22:58:05 +00009455 tvp = tv;
9456 } else {
9457 tvp = NULL;
9458 }
bellard579a97f2007-11-11 14:26:47 +00009459 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009460 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009461 ret = get_errno(utimes(p, tvp));
9462 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00009463 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009464 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009465#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009466#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00009467 case TARGET_NR_futimesat:
9468 {
9469 struct timeval *tvp, tv[2];
9470 if (arg3) {
9471 if (copy_from_user_timeval(&tv[0], arg3)
9472 || copy_from_user_timeval(&tv[1],
9473 arg3 + sizeof(struct target_timeval)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009474 return -TARGET_EFAULT;
balrogac8a6552008-09-20 02:25:39 +00009475 tvp = tv;
9476 } else {
9477 tvp = NULL;
9478 }
Richard Henderson2852aaf2018-08-18 12:01:06 -07009479 if (!(p = lock_user_string(arg2))) {
9480 return -TARGET_EFAULT;
9481 }
Peter Maydellc0d472b2013-06-12 16:20:21 +01009482 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00009483 unlock_user(p, arg2, 0);
9484 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009485 return ret;
balrogac8a6552008-09-20 02:25:39 +00009486#endif
Chen Gang704eff62015-08-21 05:37:33 +08009487#ifdef TARGET_NR_access
bellard31e31b82003-02-18 22:55:36 +00009488 case TARGET_NR_access:
Richard Henderson2852aaf2018-08-18 12:01:06 -07009489 if (!(p = lock_user_string(arg1))) {
9490 return -TARGET_EFAULT;
9491 }
Ulrich Hecht719f9082009-07-03 17:09:29 +02009492 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00009493 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009494 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009495#endif
ths92a34c12007-09-24 09:27:49 +00009496#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9497 case TARGET_NR_faccessat:
Richard Henderson2852aaf2018-08-18 12:01:06 -07009498 if (!(p = lock_user_string(arg2))) {
9499 return -TARGET_EFAULT;
9500 }
Peter Maydellc0d472b2013-06-12 16:20:21 +01009501 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00009502 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009503 return ret;
ths92a34c12007-09-24 09:27:49 +00009504#endif
WANG Xuerui35a2c852022-10-09 14:08:13 +08009505#if defined(TARGET_NR_faccessat2)
9506 case TARGET_NR_faccessat2:
9507 if (!(p = lock_user_string(arg2))) {
9508 return -TARGET_EFAULT;
9509 }
9510 ret = get_errno(faccessat(arg1, p, arg3, arg4));
9511 unlock_user(p, arg2, 0);
9512 return ret;
9513#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009514#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009515 case TARGET_NR_nice:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009516 return get_errno(nice(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +00009517#endif
bellard31e31b82003-02-18 22:55:36 +00009518 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00009519 sync();
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009520 return 0;
Aleksandar Markovic5a03cd02016-10-10 13:23:30 +02009521#if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9522 case TARGET_NR_syncfs:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009523 return get_errno(syncfs(arg1));
Aleksandar Markovic5a03cd02016-10-10 13:23:30 +02009524#endif
bellard31e31b82003-02-18 22:55:36 +00009525 case TARGET_NR_kill:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009526 return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
Chen Gang704eff62015-08-21 05:37:33 +08009527#ifdef TARGET_NR_rename
bellard31e31b82003-02-18 22:55:36 +00009528 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00009529 {
9530 void *p2;
9531 p = lock_user_string(arg1);
9532 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00009533 if (!p || !p2)
9534 ret = -TARGET_EFAULT;
9535 else
9536 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00009537 unlock_user(p2, arg2, 0);
9538 unlock_user(p, arg1, 0);
9539 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009540 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009541#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009542#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00009543 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00009544 {
bellard579a97f2007-11-11 14:26:47 +00009545 void *p2;
ths722183f2007-09-24 09:24:37 +00009546 p = lock_user_string(arg2);
9547 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00009548 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00009549 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00009550 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01009551 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00009552 unlock_user(p2, arg4, 0);
9553 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00009554 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009555 return ret;
ths722183f2007-09-24 09:24:37 +00009556#endif
Andreas Schwab95d03072018-01-23 11:53:31 +01009557#if defined(TARGET_NR_renameat2)
9558 case TARGET_NR_renameat2:
9559 {
9560 void *p2;
9561 p = lock_user_string(arg2);
9562 p2 = lock_user_string(arg4);
9563 if (!p || !p2) {
9564 ret = -TARGET_EFAULT;
9565 } else {
9566 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
9567 }
9568 unlock_user(p2, arg4, 0);
9569 unlock_user(p, arg2, 0);
9570 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009571 return ret;
Andreas Schwab95d03072018-01-23 11:53:31 +01009572#endif
Chen Gang704eff62015-08-21 05:37:33 +08009573#ifdef TARGET_NR_mkdir
bellard31e31b82003-02-18 22:55:36 +00009574 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00009575 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009576 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009577 ret = get_errno(mkdir(p, arg2));
9578 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009579 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009580#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009581#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00009582 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00009583 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009584 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009585 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00009586 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009587 return ret;
ths4472ad02007-09-24 09:22:32 +00009588#endif
Chen Gang704eff62015-08-21 05:37:33 +08009589#ifdef TARGET_NR_rmdir
bellard31e31b82003-02-18 22:55:36 +00009590 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00009591 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009592 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009593 ret = get_errno(rmdir(p));
9594 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009595 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009596#endif
bellard31e31b82003-02-18 22:55:36 +00009597 case TARGET_NR_dup:
9598 ret = get_errno(dup(arg1));
Laurent Viviere36800c2015-10-02 14:48:09 +02009599 if (ret >= 0) {
9600 fd_trans_dup(arg1, ret);
9601 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009602 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009603#ifdef TARGET_NR_pipe
bellard31e31b82003-02-18 22:55:36 +00009604 case TARGET_NR_pipe:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009605 return do_pipe(cpu_env, arg1, 0, 0);
Chen Gang704eff62015-08-21 05:37:33 +08009606#endif
Riku Voipio099d6b02009-05-05 12:10:04 +03009607#ifdef TARGET_NR_pipe2
9608 case TARGET_NR_pipe2:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009609 return do_pipe(cpu_env, arg1,
9610 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03009611#endif
bellard31e31b82003-02-18 22:55:36 +00009612 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00009613 {
pbrook53a59602006-03-25 19:31:22 +00009614 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00009615 struct tms tms;
9616 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00009617 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00009618 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
9619 if (!tmsp)
Richard Henderson2852aaf2018-08-18 12:01:06 -07009620 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02009621 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
9622 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
9623 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
9624 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00009625 }
bellardc596ed12003-07-13 17:32:31 +00009626 if (!is_error(ret))
9627 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00009628 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009629 return ret;
bellard31e31b82003-02-18 22:55:36 +00009630 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00009631 if (arg1 == 0) {
9632 ret = get_errno(acct(NULL));
9633 } else {
Richard Henderson2852aaf2018-08-18 12:01:06 -07009634 if (!(p = lock_user_string(arg1))) {
9635 return -TARGET_EFAULT;
9636 }
aurel3238d840e2009-01-30 19:48:17 +00009637 ret = get_errno(acct(path(p)));
9638 unlock_user(p, arg1, 0);
9639 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009640 return ret;
Richard Henderson8070e7b2013-07-24 09:50:00 -10009641#ifdef TARGET_NR_umount2
bellard31e31b82003-02-18 22:55:36 +00009642 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00009643 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009644 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009645 ret = get_errno(umount2(p, arg2));
9646 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009647 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009648#endif
bellard31e31b82003-02-18 22:55:36 +00009649 case TARGET_NR_ioctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009650 return do_ioctl(arg1, arg2, arg3);
Michael Clark47ae93c2018-03-03 01:31:11 +13009651#ifdef TARGET_NR_fcntl
bellard31e31b82003-02-18 22:55:36 +00009652 case TARGET_NR_fcntl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009653 return do_fcntl(arg1, arg2, arg3);
Michael Clark47ae93c2018-03-03 01:31:11 +13009654#endif
bellard31e31b82003-02-18 22:55:36 +00009655 case TARGET_NR_setpgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009656 return get_errno(setpgid(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00009657 case TARGET_NR_umask:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009658 return get_errno(umask(arg1));
bellard31e31b82003-02-18 22:55:36 +00009659 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00009660 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009661 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009662 ret = get_errno(chroot(p));
9663 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009664 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009665#ifdef TARGET_NR_dup2
bellard31e31b82003-02-18 22:55:36 +00009666 case TARGET_NR_dup2:
9667 ret = get_errno(dup2(arg1, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02009668 if (ret >= 0) {
9669 fd_trans_dup(arg1, arg2);
9670 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009671 return ret;
Chen Gang704eff62015-08-21 05:37:33 +08009672#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03009673#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9674 case TARGET_NR_dup3:
Peter Maydell10fa9932017-12-15 15:18:00 +00009675 {
9676 int host_flags;
9677
9678 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
9679 return -EINVAL;
9680 }
9681 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
9682 ret = get_errno(dup3(arg1, arg2, host_flags));
Laurent Viviere36800c2015-10-02 14:48:09 +02009683 if (ret >= 0) {
9684 fd_trans_dup(arg1, arg2);
9685 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009686 return ret;
Peter Maydell10fa9932017-12-15 15:18:00 +00009687 }
Ulrich Hechtd0927932009-09-17 20:22:14 +03009688#endif
j_mayer7a3148a2007-04-05 07:13:51 +00009689#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009690 case TARGET_NR_getppid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009691 return get_errno(getppid());
j_mayer7a3148a2007-04-05 07:13:51 +00009692#endif
Chen Gang704eff62015-08-21 05:37:33 +08009693#ifdef TARGET_NR_getpgrp
bellard31e31b82003-02-18 22:55:36 +00009694 case TARGET_NR_getpgrp:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009695 return get_errno(getpgrp());
Chen Gang704eff62015-08-21 05:37:33 +08009696#endif
bellard31e31b82003-02-18 22:55:36 +00009697 case TARGET_NR_setsid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009698 return get_errno(setsid());
thse5febef2007-04-01 18:31:35 +00009699#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00009700 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00009701 {
Richard Henderson02d0de12021-04-22 16:02:26 -07009702#if defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00009703 struct target_sigaction act, oact, *pact, *old_act;
9704
9705 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00009706 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009707 return -TARGET_EFAULT;
bellard106ec872006-06-27 21:08:10 +00009708 act._sa_handler = old_act->_sa_handler;
9709 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
9710 act.sa_flags = old_act->sa_flags;
9711 unlock_user_struct(old_act, arg2, 0);
9712 pact = &act;
9713 } else {
9714 pact = NULL;
9715 }
9716
Richard Henderson02fb28e2021-04-22 16:02:23 -07009717 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
bellard106ec872006-06-27 21:08:10 +00009718
9719 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00009720 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009721 return -TARGET_EFAULT;
bellard106ec872006-06-27 21:08:10 +00009722 old_act->_sa_handler = oact._sa_handler;
9723 old_act->sa_flags = oact.sa_flags;
9724 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
9725 old_act->sa_mask.sig[1] = 0;
9726 old_act->sa_mask.sig[2] = 0;
9727 old_act->sa_mask.sig[3] = 0;
9728 unlock_user_struct(old_act, arg3, 1);
9729 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08009730#else
9731 struct target_old_sigaction *old_act;
9732 struct target_sigaction act, oact, *pact;
9733 if (arg2) {
9734 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009735 return -TARGET_EFAULT;
Richard Henderson6049f4f2009-12-27 18:30:03 -08009736 act._sa_handler = old_act->_sa_handler;
9737 target_siginitset(&act.sa_mask, old_act->sa_mask);
9738 act.sa_flags = old_act->sa_flags;
Richard Hendersonca192272021-04-22 16:02:24 -07009739#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Henderson6049f4f2009-12-27 18:30:03 -08009740 act.sa_restorer = old_act->sa_restorer;
Richard Hendersonca192272021-04-22 16:02:24 -07009741#endif
Richard Henderson6049f4f2009-12-27 18:30:03 -08009742 unlock_user_struct(old_act, arg2, 0);
9743 pact = &act;
9744 } else {
9745 pact = NULL;
9746 }
Richard Henderson02fb28e2021-04-22 16:02:23 -07009747 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
Richard Henderson6049f4f2009-12-27 18:30:03 -08009748 if (!is_error(ret) && arg3) {
9749 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009750 return -TARGET_EFAULT;
Richard Henderson6049f4f2009-12-27 18:30:03 -08009751 old_act->_sa_handler = oact._sa_handler;
9752 old_act->sa_mask = oact.sa_mask.sig[0];
9753 old_act->sa_flags = oact.sa_flags;
Richard Hendersonca192272021-04-22 16:02:24 -07009754#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Henderson6049f4f2009-12-27 18:30:03 -08009755 old_act->sa_restorer = oact.sa_restorer;
Richard Hendersonca192272021-04-22 16:02:24 -07009756#endif
Richard Henderson6049f4f2009-12-27 18:30:03 -08009757 unlock_user_struct(old_act, arg3, 1);
9758 }
ths388bb212007-05-13 13:58:00 +00009759#endif
bellard31e31b82003-02-18 22:55:36 +00009760 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009761 return ret;
thse5febef2007-04-01 18:31:35 +00009762#endif
bellard66fb9762003-03-23 01:06:05 +00009763 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00009764 {
Richard Henderson0f6f9902021-04-22 16:02:25 -07009765 /*
9766 * For Alpha and SPARC this is a 5 argument syscall, with
Peter Maydell78bfef72017-11-06 18:33:26 +00009767 * a 'restorer' parameter which must be copied into the
9768 * sa_restorer field of the sigaction struct.
9769 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9770 * and arg5 is the sigsetsize.
Peter Maydell78bfef72017-11-06 18:33:26 +00009771 */
Richard Henderson0f6f9902021-04-22 16:02:25 -07009772#if defined(TARGET_ALPHA)
9773 target_ulong sigsetsize = arg4;
9774 target_ulong restorer = arg5;
9775#elif defined(TARGET_SPARC)
Peter Maydell78bfef72017-11-06 18:33:26 +00009776 target_ulong restorer = arg4;
9777 target_ulong sigsetsize = arg5;
9778#else
9779 target_ulong sigsetsize = arg4;
Richard Henderson02fb28e2021-04-22 16:02:23 -07009780 target_ulong restorer = 0;
Peter Maydell78bfef72017-11-06 18:33:26 +00009781#endif
Richard Hendersonfb804392021-04-22 16:02:27 -07009782 struct target_sigaction *act = NULL;
9783 struct target_sigaction *oact = NULL;
pbrook53a59602006-03-25 19:31:22 +00009784
Peter Maydell78bfef72017-11-06 18:33:26 +00009785 if (sigsetsize != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009786 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009787 }
Richard Hendersonfb804392021-04-22 16:02:27 -07009788 if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
9789 return -TARGET_EFAULT;
Peter Maydell78bfef72017-11-06 18:33:26 +00009790 }
Richard Hendersonfb804392021-04-22 16:02:27 -07009791 if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
9792 ret = -TARGET_EFAULT;
9793 } else {
9794 ret = get_errno(do_sigaction(arg1, act, oact, restorer));
9795 if (oact) {
9796 unlock_user_struct(oact, arg3, 1);
bellard579a97f2007-11-11 14:26:47 +00009797 }
Richard Hendersonfb804392021-04-22 16:02:27 -07009798 }
9799 if (act) {
pbrook53a59602006-03-25 19:31:22 +00009800 unlock_user_struct(act, arg2, 0);
Richard Hendersonfb804392021-04-22 16:02:27 -07009801 }
pbrook53a59602006-03-25 19:31:22 +00009802 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009803 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009804#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009805 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00009806 {
9807 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00009808 abi_ulong target_set;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009809 ret = do_sigprocmask(0, NULL, &cur_set);
9810 if (!ret) {
9811 host_to_target_old_sigset(&target_set, &cur_set);
9812 ret = target_set;
9813 }
bellard66fb9762003-03-23 01:06:05 +00009814 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009815 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009816#endif
9817#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00009818 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00009819 {
Miloš Stojanovića8617d82017-05-15 16:59:43 +02009820 sigset_t set, oset;
blueswir1992f48a2007-10-14 16:27:31 +00009821 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00009822 target_to_host_old_sigset(&set, &target_set);
Peter Maydell3d3efba2016-05-27 15:51:49 +01009823 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
9824 if (!ret) {
9825 host_to_target_old_sigset(&target_set, &oset);
9826 ret = target_set;
9827 }
bellard66fb9762003-03-23 01:06:05 +00009828 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009829 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +00009830#endif
thse5febef2007-04-01 18:31:35 +00009831#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00009832 case TARGET_NR_sigprocmask:
9833 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07009834#if defined(TARGET_ALPHA)
9835 sigset_t set, oldset;
9836 abi_ulong mask;
9837 int how;
9838
9839 switch (arg1) {
9840 case TARGET_SIG_BLOCK:
9841 how = SIG_BLOCK;
9842 break;
9843 case TARGET_SIG_UNBLOCK:
9844 how = SIG_UNBLOCK;
9845 break;
9846 case TARGET_SIG_SETMASK:
9847 how = SIG_SETMASK;
9848 break;
9849 default:
Richard Henderson259841c2018-08-18 12:01:09 -07009850 return -TARGET_EINVAL;
Richard Hendersona5b3b132010-05-03 10:07:55 -07009851 }
9852 mask = arg2;
9853 target_to_host_old_sigset(&set, &mask);
9854
Peter Maydell3d3efba2016-05-27 15:51:49 +01009855 ret = do_sigprocmask(how, &set, &oldset);
Richard Hendersona5b3b132010-05-03 10:07:55 -07009856 if (!is_error(ret)) {
9857 host_to_target_old_sigset(&mask, &oldset);
9858 ret = mask;
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +02009859 cpu_env->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07009860 }
9861#else
bellard66fb9762003-03-23 01:06:05 +00009862 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07009863 int how;
ths3b46e622007-09-17 08:09:54 +00009864
pbrook53a59602006-03-25 19:31:22 +00009865 if (arg2) {
Patrick Ventureebce1712022-01-26 13:25:59 -08009866 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
9867 if (!p) {
9868 return -TARGET_EFAULT;
9869 }
9870 target_to_host_old_sigset(&set, p);
9871 unlock_user(p, arg2, 0);
9872 set_ptr = &set;
Richard Hendersona5b3b132010-05-03 10:07:55 -07009873 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00009874 case TARGET_SIG_BLOCK:
9875 how = SIG_BLOCK;
9876 break;
9877 case TARGET_SIG_UNBLOCK:
9878 how = SIG_UNBLOCK;
9879 break;
9880 case TARGET_SIG_SETMASK:
9881 how = SIG_SETMASK;
9882 break;
9883 default:
Richard Henderson259841c2018-08-18 12:01:09 -07009884 return -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00009885 }
bellard66fb9762003-03-23 01:06:05 +00009886 } else {
9887 how = 0;
9888 set_ptr = NULL;
9889 }
Peter Maydell3d3efba2016-05-27 15:51:49 +01009890 ret = do_sigprocmask(how, set_ptr, &oldset);
pbrook53a59602006-03-25 19:31:22 +00009891 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009892 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009893 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009894 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05009895 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009896 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07009897#endif
bellard66fb9762003-03-23 01:06:05 +00009898 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009899 return ret;
thse5febef2007-04-01 18:31:35 +00009900#endif
bellard66fb9762003-03-23 01:06:05 +00009901 case TARGET_NR_rt_sigprocmask:
9902 {
9903 int how = arg1;
9904 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00009905
Peter Maydellc8157012016-06-30 14:23:24 +01009906 if (arg4 != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009907 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009908 }
9909
pbrook53a59602006-03-25 19:31:22 +00009910 if (arg2) {
Shu-Chun Wengd3ced2a2022-01-26 13:25:58 -08009911 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
9912 if (!p) {
9913 return -TARGET_EFAULT;
9914 }
9915 target_to_host_sigset(&set, p);
9916 unlock_user(p, arg2, 0);
9917 set_ptr = &set;
bellard66fb9762003-03-23 01:06:05 +00009918 switch(how) {
9919 case TARGET_SIG_BLOCK:
9920 how = SIG_BLOCK;
9921 break;
9922 case TARGET_SIG_UNBLOCK:
9923 how = SIG_UNBLOCK;
9924 break;
9925 case TARGET_SIG_SETMASK:
9926 how = SIG_SETMASK;
9927 break;
9928 default:
Richard Henderson259841c2018-08-18 12:01:09 -07009929 return -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00009930 }
bellard66fb9762003-03-23 01:06:05 +00009931 } else {
9932 how = 0;
9933 set_ptr = NULL;
9934 }
Peter Maydell3d3efba2016-05-27 15:51:49 +01009935 ret = do_sigprocmask(how, set_ptr, &oldset);
pbrook53a59602006-03-25 19:31:22 +00009936 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009937 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009938 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009939 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05009940 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009941 }
9942 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009943 return ret;
thse5febef2007-04-01 18:31:35 +00009944#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00009945 case TARGET_NR_sigpending:
9946 {
9947 sigset_t set;
9948 ret = get_errno(sigpending(&set));
9949 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009950 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009951 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009952 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05009953 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009954 }
9955 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009956 return ret;
thse5febef2007-04-01 18:31:35 +00009957#endif
bellard66fb9762003-03-23 01:06:05 +00009958 case TARGET_NR_rt_sigpending:
9959 {
9960 sigset_t set;
Peter Maydellc8157012016-06-30 14:23:24 +01009961
9962 /* Yes, this check is >, not != like most. We follow the kernel's
9963 * logic and it does it like this because it implements
9964 * NR_sigpending through the same code path, and in that case
9965 * the old_sigset_t is smaller in size.
9966 */
9967 if (arg2 > sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009968 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +01009969 }
9970
bellard66fb9762003-03-23 01:06:05 +00009971 ret = get_errno(sigpending(&set));
9972 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05009973 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -07009974 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00009975 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05009976 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00009977 }
9978 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -07009979 return ret;
thse5febef2007-04-01 18:31:35 +00009980#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00009981 case TARGET_NR_sigsuspend:
9982 {
Richard Henderson0a99f092022-03-15 01:43:05 -07009983 sigset_t *set;
9984
Richard Hendersonf43ce122010-05-03 10:07:54 -07009985#if defined(TARGET_ALPHA)
Richard Henderson0a99f092022-03-15 01:43:05 -07009986 TaskState *ts = cpu->opaque;
Richard Henderson7fb5ef32022-03-15 01:43:04 -07009987 /* target_to_host_old_sigset will bswap back */
9988 abi_ulong mask = tswapal(arg1);
Richard Henderson0a99f092022-03-15 01:43:05 -07009989 set = &ts->sigsuspend_mask;
9990 target_to_host_old_sigset(set, &mask);
Richard Hendersonf43ce122010-05-03 10:07:54 -07009991#else
Richard Henderson0a99f092022-03-15 01:43:05 -07009992 ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
9993 if (ret != 0) {
9994 return ret;
Peter Maydell3d3efba2016-05-27 15:51:49 +01009995 }
Richard Henderson0a99f092022-03-15 01:43:05 -07009996#endif
9997 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
9998 finish_sigsuspend_mask(ret);
bellard66fb9762003-03-23 01:06:05 +00009999 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010000 return ret;
thse5febef2007-04-01 18:31:35 +000010001#endif
bellard66fb9762003-03-23 01:06:05 +000010002 case TARGET_NR_rt_sigsuspend:
10003 {
Richard Henderson0a99f092022-03-15 01:43:05 -070010004 sigset_t *set;
Peter Maydellc8157012016-06-30 14:23:24 +010010005
Richard Henderson0a99f092022-03-15 01:43:05 -070010006 ret = process_sigsuspend_mask(&set, arg1, arg2);
10007 if (ret != 0) {
10008 return ret;
Peter Maydellc8157012016-06-30 14:23:24 +010010009 }
Richard Henderson0a99f092022-03-15 01:43:05 -070010010 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10011 finish_sigsuspend_mask(ret);
bellard66fb9762003-03-23 01:06:05 +000010012 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010013 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010014#ifdef TARGET_NR_rt_sigtimedwait
bellard66fb9762003-03-23 01:06:05 +000010015 case TARGET_NR_rt_sigtimedwait:
10016 {
bellard66fb9762003-03-23 01:06:05 +000010017 sigset_t set;
10018 struct timespec uts, *puts;
10019 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +000010020
Peter Maydellc8157012016-06-30 14:23:24 +010010021 if (arg4 != sizeof(target_sigset_t)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010022 return -TARGET_EINVAL;
Peter Maydellc8157012016-06-30 14:23:24 +010010023 }
10024
Anthony Liguoric227f092009-10-01 16:12:16 -050010025 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010026 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010027 target_to_host_sigset(&set, p);
10028 unlock_user(p, arg1, 0);
10029 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +000010030 puts = &uts;
Filip Bozuta4d213002020-07-24 20:16:51 +020010031 if (target_to_host_timespec(puts, arg3)) {
10032 return -TARGET_EFAULT;
10033 }
bellard66fb9762003-03-23 01:06:05 +000010034 } else {
10035 puts = NULL;
10036 }
Peter Maydellb3f82332016-06-06 19:58:08 +010010037 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10038 SIGSET_T_SIZE));
Petar Jovanovic974a1962014-03-03 15:07:41 +010010039 if (!is_error(ret)) {
10040 if (arg2) {
10041 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
10042 0);
10043 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010044 return -TARGET_EFAULT;
Petar Jovanovic974a1962014-03-03 15:07:41 +010010045 }
10046 host_to_target_siginfo(p, &uinfo);
10047 unlock_user(p, arg2, sizeof(target_siginfo_t));
10048 }
10049 ret = host_to_target_signal(ret);
bellard66fb9762003-03-23 01:06:05 +000010050 }
10051 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010052 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010053#endif
Filip Bozutaddcbde12020-08-24 21:21:16 +020010054#ifdef TARGET_NR_rt_sigtimedwait_time64
10055 case TARGET_NR_rt_sigtimedwait_time64:
10056 {
10057 sigset_t set;
10058 struct timespec uts, *puts;
10059 siginfo_t uinfo;
10060
10061 if (arg4 != sizeof(target_sigset_t)) {
10062 return -TARGET_EINVAL;
10063 }
10064
10065 p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
10066 if (!p) {
10067 return -TARGET_EFAULT;
10068 }
10069 target_to_host_sigset(&set, p);
10070 unlock_user(p, arg1, 0);
10071 if (arg3) {
10072 puts = &uts;
10073 if (target_to_host_timespec64(puts, arg3)) {
10074 return -TARGET_EFAULT;
10075 }
10076 } else {
10077 puts = NULL;
10078 }
10079 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10080 SIGSET_T_SIZE));
10081 if (!is_error(ret)) {
10082 if (arg2) {
10083 p = lock_user(VERIFY_WRITE, arg2,
10084 sizeof(target_siginfo_t), 0);
10085 if (!p) {
10086 return -TARGET_EFAULT;
10087 }
10088 host_to_target_siginfo(p, &uinfo);
10089 unlock_user(p, arg2, sizeof(target_siginfo_t));
10090 }
10091 ret = host_to_target_signal(ret);
10092 }
10093 }
10094 return ret;
10095#endif
bellard66fb9762003-03-23 01:06:05 +000010096 case TARGET_NR_rt_sigqueueinfo:
10097 {
10098 siginfo_t uinfo;
Peter Maydell4debae62016-06-20 15:50:36 +010010099
10100 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
10101 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010102 return -TARGET_EFAULT;
Peter Maydell4debae62016-06-20 15:50:36 +010010103 }
pbrook53a59602006-03-25 19:31:22 +000010104 target_to_host_siginfo(&uinfo, p);
Miloš Stojanovićd8b6d892017-05-15 16:59:44 +020010105 unlock_user(p, arg3, 0);
fanwenjie9b9145f2022-08-31 11:55:25 +080010106 ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
bellard66fb9762003-03-23 01:06:05 +000010107 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010108 return ret;
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +020010109 case TARGET_NR_rt_tgsigqueueinfo:
10110 {
10111 siginfo_t uinfo;
10112
10113 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
10114 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010115 return -TARGET_EFAULT;
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +020010116 }
10117 target_to_host_siginfo(&uinfo, p);
10118 unlock_user(p, arg4, 0);
fanwenjie9b9145f2022-08-31 11:55:25 +080010119 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
Miloš Stojanovićcf8b8bf2017-05-15 16:59:46 +020010120 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010121 return ret;
thse5febef2007-04-01 18:31:35 +000010122#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +000010123 case TARGET_NR_sigreturn:
Peter Maydell3d3efba2016-05-27 15:51:49 +010010124 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +010010125 return -QEMU_ERESTARTSYS;
Peter Maydell3d3efba2016-05-27 15:51:49 +010010126 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010127 return do_sigreturn(cpu_env);
thse5febef2007-04-01 18:31:35 +000010128#endif
bellard66fb9762003-03-23 01:06:05 +000010129 case TARGET_NR_rt_sigreturn:
Peter Maydell3d3efba2016-05-27 15:51:49 +010010130 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +010010131 return -QEMU_ERESTARTSYS;
Peter Maydell3d3efba2016-05-27 15:51:49 +010010132 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010133 return do_rt_sigreturn(cpu_env);
bellard31e31b82003-02-18 22:55:36 +000010134 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +000010135 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010136 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010137 ret = get_errno(sethostname(p, arg2));
10138 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010139 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010140#ifdef TARGET_NR_setrlimit
bellard31e31b82003-02-18 22:55:36 +000010141 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +000010142 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +030010143 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +000010144 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +000010145 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +000010146 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010147 return -TARGET_EFAULT;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +090010148 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
10149 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +000010150 unlock_user_struct(target_rlim, arg2, 0);
Max Filippov5dfa88f2018-09-17 11:13:14 -070010151 /*
10152 * If we just passed through resource limit settings for memory then
10153 * they would also apply to QEMU's own allocations, and QEMU will
10154 * crash or hang or die if its allocations fail. Ideally we would
10155 * track the guest allocations in QEMU and apply the limits ourselves.
10156 * For now, just tell the guest the call succeeded but don't actually
10157 * limit anything.
10158 */
10159 if (resource != RLIMIT_AS &&
10160 resource != RLIMIT_DATA &&
10161 resource != RLIMIT_STACK) {
10162 return get_errno(setrlimit(resource, &rlim));
10163 } else {
10164 return 0;
10165 }
bellard9de5e442003-03-23 16:49:39 +000010166 }
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010167#endif
10168#ifdef TARGET_NR_getrlimit
bellard31e31b82003-02-18 22:55:36 +000010169 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +000010170 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +030010171 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +000010172 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +000010173 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +000010174
bellard9de5e442003-03-23 16:49:39 +000010175 ret = get_errno(getrlimit(resource, &rlim));
10176 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +000010177 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010178 return -TARGET_EFAULT;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +090010179 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
10180 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +000010181 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +000010182 }
10183 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010184 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010185#endif
bellard31e31b82003-02-18 22:55:36 +000010186 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +000010187 {
10188 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +000010189 ret = get_errno(getrusage(arg1, &rusage));
10190 if (!is_error(ret)) {
Petar Jovanovica39fb272014-04-08 19:24:30 +020010191 ret = host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +000010192 }
10193 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010194 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010195#if defined(TARGET_NR_gettimeofday)
bellard31e31b82003-02-18 22:55:36 +000010196 case TARGET_NR_gettimeofday:
10197 {
bellard31e31b82003-02-18 22:55:36 +000010198 struct timeval tv;
Richard Hendersona52f5f82020-02-12 19:22:23 -080010199 struct timezone tz;
10200
10201 ret = get_errno(gettimeofday(&tv, &tz));
bellard31e31b82003-02-18 22:55:36 +000010202 if (!is_error(ret)) {
Richard Hendersona52f5f82020-02-12 19:22:23 -080010203 if (arg1 && copy_to_user_timeval(arg1, &tv)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010204 return -TARGET_EFAULT;
Richard Hendersona52f5f82020-02-12 19:22:23 -080010205 }
10206 if (arg2 && copy_to_user_timezone(arg2, &tz)) {
10207 return -TARGET_EFAULT;
10208 }
bellard31e31b82003-02-18 22:55:36 +000010209 }
10210 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010211 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010212#endif
10213#if defined(TARGET_NR_settimeofday)
bellard31e31b82003-02-18 22:55:36 +000010214 case TARGET_NR_settimeofday:
10215 {
Paul Burtonb67d8032014-06-22 11:25:41 +010010216 struct timeval tv, *ptv = NULL;
Paul Burtonef4467e2014-06-22 11:25:40 +010010217 struct timezone tz, *ptz = NULL;
10218
Paul Burtonb67d8032014-06-22 11:25:41 +010010219 if (arg1) {
10220 if (copy_from_user_timeval(&tv, arg1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010221 return -TARGET_EFAULT;
Paul Burtonb67d8032014-06-22 11:25:41 +010010222 }
10223 ptv = &tv;
10224 }
Paul Burtonef4467e2014-06-22 11:25:40 +010010225
10226 if (arg2) {
10227 if (copy_from_user_timezone(&tz, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010228 return -TARGET_EFAULT;
Paul Burtonef4467e2014-06-22 11:25:40 +010010229 }
10230 ptz = &tz;
10231 }
10232
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010233 return get_errno(settimeofday(ptv, ptz));
bellard31e31b82003-02-18 22:55:36 +000010234 }
Alistair Francis859e8a82020-03-12 15:13:49 -070010235#endif
Laurent Vivier9468a5d2013-01-10 22:30:50 +010010236#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +000010237 case TARGET_NR_select:
Laurent Vivier5457dc92016-07-08 01:17:27 +020010238#if defined(TARGET_WANT_NI_OLD_SELECT)
10239 /* some architectures used to have old_select here
10240 * but now ENOSYS it.
10241 */
10242 ret = -TARGET_ENOSYS;
10243#elif defined(TARGET_WANT_OLD_SYS_SELECT)
10244 ret = do_old_select(arg1);
Laurent Vivier9468a5d2013-01-10 22:30:50 +010010245#else
Laurent Vivier5457dc92016-07-08 01:17:27 +020010246 ret = do_select(arg1, arg2, arg3, arg4, arg5);
Laurent Vivier9468a5d2013-01-10 22:30:50 +010010247#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010248 return ret;
bellard048f6b42005-11-26 18:47:20 +000010249#endif
Riku Voipio9e423822010-05-07 12:28:05 +000010250#ifdef TARGET_NR_pselect6
10251 case TARGET_NR_pselect6:
Filip Bozutae5ce9682020-08-25 00:30:49 +020010252 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
10253#endif
10254#ifdef TARGET_NR_pselect6_time64
10255 case TARGET_NR_pselect6_time64:
10256 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
Riku Voipio9e423822010-05-07 12:28:05 +000010257#endif
Chen Gang704eff62015-08-21 05:37:33 +080010258#ifdef TARGET_NR_symlink
bellard31e31b82003-02-18 22:55:36 +000010259 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +000010260 {
10261 void *p2;
10262 p = lock_user_string(arg1);
10263 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +000010264 if (!p || !p2)
10265 ret = -TARGET_EFAULT;
10266 else
10267 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +000010268 unlock_user(p2, arg2, 0);
10269 unlock_user(p, arg1, 0);
10270 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010271 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080010272#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +010010273#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +000010274 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +000010275 {
bellard579a97f2007-11-11 14:26:47 +000010276 void *p2;
thsf0b62432007-09-24 09:25:40 +000010277 p = lock_user_string(arg1);
10278 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +000010279 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +000010280 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +000010281 else
Peter Maydellc0d472b2013-06-12 16:20:21 +010010282 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +000010283 unlock_user(p2, arg3, 0);
10284 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +000010285 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010286 return ret;
thsf0b62432007-09-24 09:25:40 +000010287#endif
Chen Gang704eff62015-08-21 05:37:33 +080010288#ifdef TARGET_NR_readlink
bellard31e31b82003-02-18 22:55:36 +000010289 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +000010290 {
Andreas Schwab463d8e72013-07-02 14:04:12 +010010291 void *p2;
pbrook53a59602006-03-25 19:31:22 +000010292 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +000010293 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
Ilya Leoshkevicha4dab0a2023-06-30 19:04:16 +010010294 ret = get_errno(do_guest_readlink(p, p2, arg3));
pbrook53a59602006-03-25 19:31:22 +000010295 unlock_user(p2, arg2, ret);
10296 unlock_user(p, arg1, 0);
10297 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010298 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080010299#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +010010300#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +000010301 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +000010302 {
bellard579a97f2007-11-11 14:26:47 +000010303 void *p2;
ths5e0ccb12007-09-24 09:26:10 +000010304 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +000010305 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +010010306 if (!p || !p2) {
10307 ret = -TARGET_EFAULT;
Jameson Nash65d48302022-08-08 15:07:27 -040010308 } else if (!arg4) {
10309 /* Short circuit this for the magic exe check. */
10310 ret = -TARGET_EINVAL;
Andreas Schwab463d8e72013-07-02 14:04:12 +010010311 } else if (is_proc_myself((const char *)p, "exe")) {
Helge Deller258bec32022-12-05 12:38:25 +010010312 /*
10313 * Don't worry about sign mismatch as earlier mapping
10314 * logic would have thrown a bad address error.
10315 */
10316 ret = MIN(strlen(exec_path), arg4);
10317 /* We cannot NUL terminate the string. */
10318 memcpy(p2, exec_path, ret);
Andreas Schwab463d8e72013-07-02 14:04:12 +010010319 } else {
Peter Maydellc0d472b2013-06-12 16:20:21 +010010320 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
Andreas Schwab463d8e72013-07-02 14:04:12 +010010321 }
bellard579a97f2007-11-11 14:26:47 +000010322 unlock_user(p2, arg3, ret);
10323 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +000010324 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010325 return ret;
ths5e0ccb12007-09-24 09:26:10 +000010326#endif
thse5febef2007-04-01 18:31:35 +000010327#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +000010328 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +000010329 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010330 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010331 ret = get_errno(swapon(p, arg2));
10332 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010333 return ret;
thse5febef2007-04-01 18:31:35 +000010334#endif
bellard31e31b82003-02-18 22:55:36 +000010335 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +000010336 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
10337 /* arg4 must be ignored in all other cases */
10338 p = lock_user_string(arg4);
10339 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010340 return -TARGET_EFAULT;
Laurent Vivierc07ecc62013-01-07 11:40:06 +000010341 }
10342 ret = get_errno(reboot(arg1, arg2, arg3, p));
10343 unlock_user(p, arg4, 0);
10344 } else {
10345 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
10346 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010347 return ret;
thse5febef2007-04-01 18:31:35 +000010348#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +000010349 case TARGET_NR_mmap:
Alexander Graf09701192013-09-03 20:12:15 +010010350#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
10351 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +020010352 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
10353 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +000010354 {
blueswir1992f48a2007-10-14 16:27:31 +000010355 abi_ulong *v;
10356 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +000010357 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010358 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +020010359 v1 = tswapal(v[0]);
10360 v2 = tswapal(v[1]);
10361 v3 = tswapal(v[2]);
10362 v4 = tswapal(v[3]);
10363 v5 = tswapal(v[4]);
10364 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +000010365 unlock_user(v, arg1, 0);
Richard Henderson9ab8d0712023-08-07 18:22:35 -070010366 return do_mmap(v1, v2, v3, v4, v5, v6);
bellard31e31b82003-02-18 22:55:36 +000010367 }
bellard31e31b82003-02-18 22:55:36 +000010368#else
Richard Hendersonee1bf832021-02-12 10:48:44 -080010369 /* mmap pointers are always untagged */
Richard Henderson9ab8d0712023-08-07 18:22:35 -070010370 return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
bellard31e31b82003-02-18 22:55:36 +000010371#endif
thse5febef2007-04-01 18:31:35 +000010372#endif
bellarda315a142005-01-30 22:59:18 +000010373#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +000010374 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +000010375#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +000010376#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +000010377#endif
Richard Henderson9ab8d0712023-08-07 18:22:35 -070010378 return do_mmap(arg1, arg2, arg3, arg4, arg5,
10379 (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
bellarda315a142005-01-30 22:59:18 +000010380#endif
bellard31e31b82003-02-18 22:55:36 +000010381 case TARGET_NR_munmap:
Richard Hendersonee1bf832021-02-12 10:48:44 -080010382 arg1 = cpu_untagged_addr(cpu, arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010383 return get_errno(target_munmap(arg1, arg2));
bellard9de5e442003-03-23 16:49:39 +000010384 case TARGET_NR_mprotect:
Richard Hendersonee1bf832021-02-12 10:48:44 -080010385 arg1 = cpu_untagged_addr(cpu, arg1);
Paul Brook97374d32010-06-16 13:03:51 +010010386 {
Andreas Färber0429a972013-08-26 18:14:44 +020010387 TaskState *ts = cpu->opaque;
Paul Brook97374d32010-06-16 13:03:51 +010010388 /* Special hack to detect libc making the stack executable. */
10389 if ((arg3 & PROT_GROWSDOWN)
10390 && arg1 >= ts->info->stack_limit
10391 && arg1 <= ts->info->start_stack) {
10392 arg3 &= ~PROT_GROWSDOWN;
10393 arg2 = arg2 + arg1 - ts->info->stack_limit;
10394 arg1 = ts->info->stack_limit;
10395 }
10396 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010397 return get_errno(target_mprotect(arg1, arg2, arg3));
thse5febef2007-04-01 18:31:35 +000010398#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +000010399 case TARGET_NR_mremap:
Richard Hendersonee1bf832021-02-12 10:48:44 -080010400 arg1 = cpu_untagged_addr(cpu, arg1);
10401 /* mremap new_addr (arg5) is always untagged */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010402 return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
thse5febef2007-04-01 18:31:35 +000010403#endif
pbrook53a59602006-03-25 19:31:22 +000010404 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +000010405#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +000010406 case TARGET_NR_msync:
Helge Dellerfe080592022-12-15 08:27:46 +010010407 return get_errno(msync(g2h(cpu, arg1), arg2,
10408 target_to_host_msync_arg(arg3)));
thse5febef2007-04-01 18:31:35 +000010409#endif
10410#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +000010411 case TARGET_NR_mlock:
Richard Henderson3e8f1622021-02-12 10:48:43 -080010412 return get_errno(mlock(g2h(cpu, arg1), arg2));
thse5febef2007-04-01 18:31:35 +000010413#endif
10414#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +000010415 case TARGET_NR_munlock:
Richard Henderson3e8f1622021-02-12 10:48:43 -080010416 return get_errno(munlock(g2h(cpu, arg1), arg2));
thse5febef2007-04-01 18:31:35 +000010417#endif
10418#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +000010419 case TARGET_NR_mlockall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010420 return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
thse5febef2007-04-01 18:31:35 +000010421#endif
10422#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +000010423 case TARGET_NR_munlockall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010424 return get_errno(munlockall());
thse5febef2007-04-01 18:31:35 +000010425#endif
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010426#ifdef TARGET_NR_truncate
bellard31e31b82003-02-18 22:55:36 +000010427 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +000010428 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010429 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010430 ret = get_errno(truncate(p, arg2));
10431 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010432 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010433#endif
10434#ifdef TARGET_NR_ftruncate
bellard31e31b82003-02-18 22:55:36 +000010435 case TARGET_NR_ftruncate:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010436 return get_errno(ftruncate(arg1, arg2));
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010437#endif
bellard31e31b82003-02-18 22:55:36 +000010438 case TARGET_NR_fchmod:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010439 return get_errno(fchmod(arg1, arg2));
Peter Maydellc0d472b2013-06-12 16:20:21 +010010440#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +000010441 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +000010442 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010443 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +010010444 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +000010445 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010446 return ret;
ths814d7972007-09-24 09:26:51 +000010447#endif
bellard31e31b82003-02-18 22:55:36 +000010448 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -070010449 /* Note that negative values are valid for getpriority, so we must
10450 differentiate based on errno settings. */
10451 errno = 0;
10452 ret = getpriority(arg1, arg2);
10453 if (ret == -1 && errno != 0) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010454 return -host_to_target_errno(errno);
Richard Henderson95c09822012-06-07 15:14:50 -070010455 }
10456#ifdef TARGET_ALPHA
10457 /* Return value is the unbiased priority. Signal no error. */
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020010458 cpu_env->ir[IR_V0] = 0;
Richard Henderson95c09822012-06-07 15:14:50 -070010459#else
10460 /* Return value is a biased priority to avoid negative numbers. */
10461 ret = 20 - ret;
10462#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010463 return ret;
bellard31e31b82003-02-18 22:55:36 +000010464 case TARGET_NR_setpriority:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010465 return get_errno(setpriority(arg1, arg2, arg3));
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010466#ifdef TARGET_NR_statfs
bellard31e31b82003-02-18 22:55:36 +000010467 case TARGET_NR_statfs:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010468 if (!(p = lock_user_string(arg1))) {
10469 return -TARGET_EFAULT;
10470 }
pbrook53a59602006-03-25 19:31:22 +000010471 ret = get_errno(statfs(path(p), &stfs));
10472 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +000010473 convert_statfs:
10474 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000010475 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +000010476
bellard579a97f2007-11-11 14:26:47 +000010477 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010478 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000010479 __put_user(stfs.f_type, &target_stfs->f_type);
10480 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10481 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10482 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10483 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10484 __put_user(stfs.f_files, &target_stfs->f_files);
10485 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10486 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10487 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10488 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +020010489 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
Shea Levyd4247ec2018-03-01 06:15:00 -050010490#ifdef _STATFS_F_FLAGS
10491 __put_user(stfs.f_flags, &target_stfs->f_flags);
10492#else
10493 __put_user(0, &target_stfs->f_flags);
10494#endif
Alexander Graf229d3372012-09-19 04:39:53 +020010495 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +000010496 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +000010497 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010498 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010499#endif
10500#ifdef TARGET_NR_fstatfs
bellard31e31b82003-02-18 22:55:36 +000010501 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +000010502 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +000010503 goto convert_statfs;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010504#endif
bellard56c8f682005-11-28 22:28:41 +000010505#ifdef TARGET_NR_statfs64
10506 case TARGET_NR_statfs64:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010507 if (!(p = lock_user_string(arg1))) {
10508 return -TARGET_EFAULT;
10509 }
pbrook53a59602006-03-25 19:31:22 +000010510 ret = get_errno(statfs(path(p), &stfs));
10511 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +000010512 convert_statfs64:
10513 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000010514 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +000010515
bellard579a97f2007-11-11 14:26:47 +000010516 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010517 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000010518 __put_user(stfs.f_type, &target_stfs->f_type);
10519 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10520 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10521 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10522 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10523 __put_user(stfs.f_files, &target_stfs->f_files);
10524 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10525 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10526 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10527 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +020010528 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
Frajob94e2b42020-01-30 15:00:30 +020010529#ifdef _STATFS_F_FLAGS
10530 __put_user(stfs.f_flags, &target_stfs->f_flags);
10531#else
10532 __put_user(0, &target_stfs->f_flags);
10533#endif
Alexander Graf229d3372012-09-19 04:39:53 +020010534 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +000010535 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +000010536 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010537 return ret;
bellard56c8f682005-11-28 22:28:41 +000010538 case TARGET_NR_fstatfs64:
10539 ret = get_errno(fstatfs(arg1, &stfs));
10540 goto convert_statfs64;
10541#endif
thse5febef2007-04-01 18:31:35 +000010542#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +000010543 case TARGET_NR_socketcall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010544 return do_socketcall(arg1, arg2);
thse5febef2007-04-01 18:31:35 +000010545#endif
bellard3532fa72006-06-24 15:06:03 +000010546#ifdef TARGET_NR_accept
10547 case TARGET_NR_accept:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010548 return do_accept4(arg1, arg2, arg3, 0);
Peter Maydella94b4982013-02-08 04:35:04 +000010549#endif
10550#ifdef TARGET_NR_accept4
10551 case TARGET_NR_accept4:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010552 return do_accept4(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +000010553#endif
10554#ifdef TARGET_NR_bind
10555 case TARGET_NR_bind:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010556 return do_bind(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010557#endif
10558#ifdef TARGET_NR_connect
10559 case TARGET_NR_connect:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010560 return do_connect(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010561#endif
10562#ifdef TARGET_NR_getpeername
10563 case TARGET_NR_getpeername:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010564 return do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010565#endif
10566#ifdef TARGET_NR_getsockname
10567 case TARGET_NR_getsockname:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010568 return do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010569#endif
10570#ifdef TARGET_NR_getsockopt
10571 case TARGET_NR_getsockopt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010572 return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
bellard3532fa72006-06-24 15:06:03 +000010573#endif
10574#ifdef TARGET_NR_listen
10575 case TARGET_NR_listen:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010576 return get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +000010577#endif
10578#ifdef TARGET_NR_recv
10579 case TARGET_NR_recv:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010580 return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +000010581#endif
10582#ifdef TARGET_NR_recvfrom
10583 case TARGET_NR_recvfrom:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010584 return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +000010585#endif
10586#ifdef TARGET_NR_recvmsg
10587 case TARGET_NR_recvmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010588 return do_sendrecvmsg(arg1, arg2, arg3, 0);
bellard3532fa72006-06-24 15:06:03 +000010589#endif
10590#ifdef TARGET_NR_send
10591 case TARGET_NR_send:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010592 return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +000010593#endif
10594#ifdef TARGET_NR_sendmsg
10595 case TARGET_NR_sendmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010596 return do_sendrecvmsg(arg1, arg2, arg3, 1);
bellard3532fa72006-06-24 15:06:03 +000010597#endif
Alexander Graff19e00d2014-03-02 19:36:42 +000010598#ifdef TARGET_NR_sendmmsg
10599 case TARGET_NR_sendmmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010600 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
Alistair Francis859e8a82020-03-12 15:13:49 -070010601#endif
10602#ifdef TARGET_NR_recvmmsg
Alexander Graff19e00d2014-03-02 19:36:42 +000010603 case TARGET_NR_recvmmsg:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010604 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
Alexander Graff19e00d2014-03-02 19:36:42 +000010605#endif
bellard3532fa72006-06-24 15:06:03 +000010606#ifdef TARGET_NR_sendto
10607 case TARGET_NR_sendto:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010608 return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +000010609#endif
10610#ifdef TARGET_NR_shutdown
10611 case TARGET_NR_shutdown:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010612 return get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +000010613#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +010010614#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10615 case TARGET_NR_getrandom:
10616 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10617 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010618 return -TARGET_EFAULT;
Laurent Vivierf894efd2016-02-21 10:56:23 +010010619 }
10620 ret = get_errno(getrandom(p, arg2, arg3));
10621 unlock_user(p, arg1, ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010622 return ret;
Laurent Vivierf894efd2016-02-21 10:56:23 +010010623#endif
bellard3532fa72006-06-24 15:06:03 +000010624#ifdef TARGET_NR_socket
10625 case TARGET_NR_socket:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010626 return do_socket(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +000010627#endif
10628#ifdef TARGET_NR_socketpair
10629 case TARGET_NR_socketpair:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010630 return do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +000010631#endif
10632#ifdef TARGET_NR_setsockopt
10633 case TARGET_NR_setsockopt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010634 return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
bellard3532fa72006-06-24 15:06:03 +000010635#endif
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010636#if defined(TARGET_NR_syslog)
bellard31e31b82003-02-18 22:55:36 +000010637 case TARGET_NR_syslog:
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010638 {
10639 int len = arg2;
ths7494b0f2007-02-11 18:26:53 +000010640
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010641 switch (arg1) {
10642 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
10643 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
10644 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
10645 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
10646 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
10647 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
10648 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
10649 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010650 return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010651 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
10652 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
10653 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
10654 {
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010655 if (len < 0) {
Richard Henderson259841c2018-08-18 12:01:09 -070010656 return -TARGET_EINVAL;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010657 }
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010658 if (len == 0) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010659 return 0;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010660 }
10661 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10662 if (!p) {
Richard Henderson259841c2018-08-18 12:01:09 -070010663 return -TARGET_EFAULT;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010664 }
10665 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
10666 unlock_user(p, arg2, arg3);
10667 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010668 return ret;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010669 default:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010670 return -TARGET_EINVAL;
Aleksandar Markovicda2c8ad2016-09-22 18:56:58 +020010671 }
10672 }
10673 break;
10674#endif
bellard31e31b82003-02-18 22:55:36 +000010675 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +000010676 {
bellard66fb9762003-03-23 01:06:05 +000010677 struct itimerval value, ovalue, *pvalue;
10678
pbrook53a59602006-03-25 19:31:22 +000010679 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +000010680 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +000010681 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
10682 || copy_from_user_timeval(&pvalue->it_value,
10683 arg2 + sizeof(struct target_timeval)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010684 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +000010685 } else {
10686 pvalue = NULL;
10687 }
10688 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +000010689 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +000010690 if (copy_to_user_timeval(arg3,
10691 &ovalue.it_interval)
10692 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
10693 &ovalue.it_value))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010694 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +000010695 }
10696 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010697 return ret;
bellard31e31b82003-02-18 22:55:36 +000010698 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +000010699 {
bellard66fb9762003-03-23 01:06:05 +000010700 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +000010701
bellard66fb9762003-03-23 01:06:05 +000010702 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +000010703 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +000010704 if (copy_to_user_timeval(arg2,
10705 &value.it_interval)
10706 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
10707 &value.it_value))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010708 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +000010709 }
10710 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010711 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080010712#ifdef TARGET_NR_stat
bellard31e31b82003-02-18 22:55:36 +000010713 case TARGET_NR_stat:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010714 if (!(p = lock_user_string(arg1))) {
10715 return -TARGET_EFAULT;
10716 }
pbrook53a59602006-03-25 19:31:22 +000010717 ret = get_errno(stat(path(p), &st));
10718 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +000010719 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +080010720#endif
10721#ifdef TARGET_NR_lstat
bellard31e31b82003-02-18 22:55:36 +000010722 case TARGET_NR_lstat:
Richard Henderson2852aaf2018-08-18 12:01:06 -070010723 if (!(p = lock_user_string(arg1))) {
10724 return -TARGET_EFAULT;
10725 }
pbrook53a59602006-03-25 19:31:22 +000010726 ret = get_errno(lstat(path(p), &st));
10727 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +000010728 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +080010729#endif
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010730#ifdef TARGET_NR_fstat
bellard31e31b82003-02-18 22:55:36 +000010731 case TARGET_NR_fstat:
10732 {
10733 ret = get_errno(fstat(arg1, &st));
Chen Gang704eff62015-08-21 05:37:33 +080010734#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
bellard31e31b82003-02-18 22:55:36 +000010735 do_stat:
Chen Gang704eff62015-08-21 05:37:33 +080010736#endif
bellard31e31b82003-02-18 22:55:36 +000010737 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000010738 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +000010739
bellard579a97f2007-11-11 14:26:47 +000010740 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010741 return -TARGET_EFAULT;
Ulrich Hecht12727912009-07-24 19:10:32 +020010742 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +000010743 __put_user(st.st_dev, &target_st->st_dev);
10744 __put_user(st.st_ino, &target_st->st_ino);
10745 __put_user(st.st_mode, &target_st->st_mode);
10746 __put_user(st.st_uid, &target_st->st_uid);
10747 __put_user(st.st_gid, &target_st->st_gid);
10748 __put_user(st.st_nlink, &target_st->st_nlink);
10749 __put_user(st.st_rdev, &target_st->st_rdev);
10750 __put_user(st.st_size, &target_st->st_size);
10751 __put_user(st.st_blksize, &target_st->st_blksize);
10752 __put_user(st.st_blocks, &target_st->st_blocks);
10753 __put_user(st.st_atime, &target_st->target_st_atime);
10754 __put_user(st.st_mtime, &target_st->target_st_mtime);
10755 __put_user(st.st_ctime, &target_st->target_st_ctime);
Michael Forneyfebf6fa2021-05-25 20:55:31 -070010756#if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
Chen-Yu Tsai5f992db2019-05-23 00:21:47 +080010757 __put_user(st.st_atim.tv_nsec,
10758 &target_st->target_st_atime_nsec);
10759 __put_user(st.st_mtim.tv_nsec,
10760 &target_st->target_st_mtime_nsec);
10761 __put_user(st.st_ctim.tv_nsec,
10762 &target_st->target_st_ctime_nsec);
10763#endif
pbrook53a59602006-03-25 19:31:22 +000010764 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +000010765 }
10766 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010767 return ret;
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020010768#endif
bellard31e31b82003-02-18 22:55:36 +000010769 case TARGET_NR_vhangup:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010770 return get_errno(vhangup());
bellard42ad6ae2005-01-03 22:48:11 +000010771#ifdef TARGET_NR_syscall
10772 case TARGET_NR_syscall:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010773 return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
10774 arg6, arg7, arg8, 0);
bellard42ad6ae2005-01-03 22:48:11 +000010775#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070010776#if defined(TARGET_NR_wait4)
bellard31e31b82003-02-18 22:55:36 +000010777 case TARGET_NR_wait4:
10778 {
10779 int status;
blueswir1992f48a2007-10-14 16:27:31 +000010780 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +000010781 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +000010782 abi_ulong target_rusage = arg4;
Petar Jovanovica39fb272014-04-08 19:24:30 +020010783 abi_long rusage_err;
bellard31e31b82003-02-18 22:55:36 +000010784 if (target_rusage)
10785 rusage_ptr = &rusage;
10786 else
10787 rusage_ptr = NULL;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +010010788 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
bellard31e31b82003-02-18 22:55:36 +000010789 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +010010790 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +000010791 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +000010792 if (put_user_s32(status, status_ptr))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010793 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +000010794 }
Petar Jovanovica39fb272014-04-08 19:24:30 +020010795 if (target_rusage) {
10796 rusage_err = host_to_target_rusage(target_rusage, &rusage);
10797 if (rusage_err) {
10798 ret = rusage_err;
10799 }
10800 }
bellard31e31b82003-02-18 22:55:36 +000010801 }
10802 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010803 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010804#endif
thse5febef2007-04-01 18:31:35 +000010805#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +000010806 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +000010807 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010808 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010809 ret = get_errno(swapoff(p));
10810 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010811 return ret;
thse5febef2007-04-01 18:31:35 +000010812#endif
bellard31e31b82003-02-18 22:55:36 +000010813 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +000010814 {
pbrook53a59602006-03-25 19:31:22 +000010815 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +000010816 struct sysinfo value;
10817 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +000010818 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +000010819 {
bellard579a97f2007-11-11 14:26:47 +000010820 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010821 return -TARGET_EFAULT;
bellarda5448a72004-06-19 16:59:03 +000010822 __put_user(value.uptime, &target_value->uptime);
10823 __put_user(value.loads[0], &target_value->loads[0]);
10824 __put_user(value.loads[1], &target_value->loads[1]);
10825 __put_user(value.loads[2], &target_value->loads[2]);
10826 __put_user(value.totalram, &target_value->totalram);
10827 __put_user(value.freeram, &target_value->freeram);
10828 __put_user(value.sharedram, &target_value->sharedram);
10829 __put_user(value.bufferram, &target_value->bufferram);
10830 __put_user(value.totalswap, &target_value->totalswap);
10831 __put_user(value.freeswap, &target_value->freeswap);
10832 __put_user(value.procs, &target_value->procs);
10833 __put_user(value.totalhigh, &target_value->totalhigh);
10834 __put_user(value.freehigh, &target_value->freehigh);
10835 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +000010836 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +000010837 }
10838 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010839 return ret;
thse5febef2007-04-01 18:31:35 +000010840#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +000010841 case TARGET_NR_ipc:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010842 return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
thse5febef2007-04-01 18:31:35 +000010843#endif
aurel32e5289082009-04-18 16:16:12 +000010844#ifdef TARGET_NR_semget
10845 case TARGET_NR_semget:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010846 return get_errno(semget(arg1, arg2, arg3));
aurel32e5289082009-04-18 16:16:12 +000010847#endif
10848#ifdef TARGET_NR_semop
10849 case TARGET_NR_semop:
Filip Bozutacac46eb2020-08-25 00:30:50 +020010850 return do_semtimedop(arg1, arg2, arg3, 0, false);
Matus Kyseld8c08b12020-06-26 14:46:11 +020010851#endif
10852#ifdef TARGET_NR_semtimedop
10853 case TARGET_NR_semtimedop:
Filip Bozutacac46eb2020-08-25 00:30:50 +020010854 return do_semtimedop(arg1, arg2, arg3, arg4, false);
10855#endif
10856#ifdef TARGET_NR_semtimedop_time64
10857 case TARGET_NR_semtimedop_time64:
10858 return do_semtimedop(arg1, arg2, arg3, arg4, true);
aurel32e5289082009-04-18 16:16:12 +000010859#endif
10860#ifdef TARGET_NR_semctl
10861 case TARGET_NR_semctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010862 return do_semctl(arg1, arg2, arg3, arg4);
aurel32e5289082009-04-18 16:16:12 +000010863#endif
aurel32eeb438c2008-10-13 21:08:55 +000010864#ifdef TARGET_NR_msgctl
10865 case TARGET_NR_msgctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010866 return do_msgctl(arg1, arg2, arg3);
aurel32eeb438c2008-10-13 21:08:55 +000010867#endif
10868#ifdef TARGET_NR_msgget
10869 case TARGET_NR_msgget:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010870 return get_errno(msgget(arg1, arg2));
aurel32eeb438c2008-10-13 21:08:55 +000010871#endif
10872#ifdef TARGET_NR_msgrcv
10873 case TARGET_NR_msgrcv:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010874 return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
aurel32eeb438c2008-10-13 21:08:55 +000010875#endif
10876#ifdef TARGET_NR_msgsnd
10877 case TARGET_NR_msgsnd:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010878 return do_msgsnd(arg1, arg2, arg3, arg4);
aurel32eeb438c2008-10-13 21:08:55 +000010879#endif
Riku Voipio88a8c982009-04-03 10:42:00 +030010880#ifdef TARGET_NR_shmget
10881 case TARGET_NR_shmget:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010882 return get_errno(shmget(arg1, arg2, arg3));
Riku Voipio88a8c982009-04-03 10:42:00 +030010883#endif
10884#ifdef TARGET_NR_shmctl
10885 case TARGET_NR_shmctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010886 return do_shmctl(arg1, arg2, arg3);
Riku Voipio88a8c982009-04-03 10:42:00 +030010887#endif
10888#ifdef TARGET_NR_shmat
10889 case TARGET_NR_shmat:
Richard Henderson225a2062023-08-20 09:24:14 -070010890 return target_shmat(cpu_env, arg1, arg2, arg3);
Riku Voipio88a8c982009-04-03 10:42:00 +030010891#endif
10892#ifdef TARGET_NR_shmdt
10893 case TARGET_NR_shmdt:
Richard Henderson225a2062023-08-20 09:24:14 -070010894 return target_shmdt(arg1);
Riku Voipio88a8c982009-04-03 10:42:00 +030010895#endif
bellard31e31b82003-02-18 22:55:36 +000010896 case TARGET_NR_fsync:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010897 return get_errno(fsync(arg1));
bellard31e31b82003-02-18 22:55:36 +000010898 case TARGET_NR_clone:
Peter Maydell4ce62432013-07-16 18:44:57 +010010899 /* Linux manages to have three different orderings for its
10900 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10901 * match the kernel's CONFIG_CLONE_* settings.
10902 * Microblaze is further special in that it uses a sixth
10903 * implicit argument to clone for the TLS pointer.
10904 */
10905#if defined(TARGET_MICROBLAZE)
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +020010906 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Peter Maydell4ce62432013-07-16 18:44:57 +010010907#elif defined(TARGET_CLONE_BACKWARDS)
10908 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
10909#elif defined(TARGET_CLONE_BACKWARDS2)
Ulrich Hechta4c075f2009-07-24 16:57:31 +020010910 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +000010911#else
Peter Maydell4ce62432013-07-16 18:44:57 +010010912 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +000010913#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010914 return ret;
bellardec86b0f2003-04-11 00:15:04 +000010915#ifdef __NR_exit_group
10916 /* new thread calls */
10917 case TARGET_NR_exit_group:
Alex Bennée708b6a62018-06-22 17:09:10 +010010918 preexit_cleanup(cpu_env, arg1);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010919 return get_errno(exit_group(arg1));
bellardec86b0f2003-04-11 00:15:04 +000010920#endif
bellard31e31b82003-02-18 22:55:36 +000010921 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +000010922 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010923 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000010924 ret = get_errno(setdomainname(p, arg2));
10925 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010926 return ret;
bellard31e31b82003-02-18 22:55:36 +000010927 case TARGET_NR_uname:
10928 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +000010929 {
10930 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +000010931
bellard579a97f2007-11-11 14:26:47 +000010932 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070010933 return -TARGET_EFAULT;
bellard29e619b2004-09-13 21:41:04 +000010934 ret = get_errno(sys_uname(buf));
10935 if (!is_error(ret)) {
Peter Maydell332c9782016-07-12 13:02:16 +010010936 /* Overwrite the native machine name with whatever is being
bellard29e619b2004-09-13 21:41:04 +000010937 emulated. */
Philippe Mathieu-Daudé871f95c2017-07-24 15:27:47 -030010938 g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
10939 sizeof(buf->machine));
pbrookc5937222006-05-14 11:30:38 +000010940 /* Allow the user to override the reported release. */
Peter Maydell332c9782016-07-12 13:02:16 +010010941 if (qemu_uname_release && *qemu_uname_release) {
10942 g_strlcpy(buf->release, qemu_uname_release,
10943 sizeof(buf->release));
10944 }
bellard29e619b2004-09-13 21:41:04 +000010945 }
pbrook53a59602006-03-25 19:31:22 +000010946 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +000010947 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010948 return ret;
bellard6dbad632003-03-16 18:05:05 +000010949#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +000010950 case TARGET_NR_modify_ldt:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010951 return do_modify_ldt(cpu_env, arg1, arg2, arg3);
j_mayer84409dd2007-04-06 08:56:50 +000010952#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +000010953 case TARGET_NR_vm86:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010954 return do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +000010955#endif
j_mayer84409dd2007-04-06 08:56:50 +000010956#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070010957#if defined(TARGET_NR_adjtimex)
bellard31e31b82003-02-18 22:55:36 +000010958 case TARGET_NR_adjtimex:
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020010959 {
10960 struct timex host_buf;
10961
10962 if (target_to_host_timex(&host_buf, arg1) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010963 return -TARGET_EFAULT;
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020010964 }
10965 ret = get_errno(adjtimex(&host_buf));
10966 if (!is_error(ret)) {
10967 if (host_to_target_timex(arg1, &host_buf) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010968 return -TARGET_EFAULT;
Aleksandar Markovic19f59bc2016-09-22 18:56:50 +020010969 }
10970 }
10971 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010972 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070010973#endif
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010974#if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10975 case TARGET_NR_clock_adjtime:
10976 {
Peter Maydellaab74612023-07-17 11:05:07 +010010977 struct timex htx;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010978
Peter Maydellaab74612023-07-17 11:05:07 +010010979 if (target_to_host_timex(&htx, arg2) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070010980 return -TARGET_EFAULT;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010981 }
Peter Maydellaab74612023-07-17 11:05:07 +010010982 ret = get_errno(clock_adjtime(arg1, &htx));
10983 if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
10984 return -TARGET_EFAULT;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010985 }
10986 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070010987 return ret;
Aleksandar Markovic38860a02016-10-10 13:23:29 +020010988#endif
Filip Bozuta6ac03b22020-08-24 21:21:15 +020010989#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
10990 case TARGET_NR_clock_adjtime64:
10991 {
10992 struct timex htx;
10993
10994 if (target_to_host_timex64(&htx, arg2) != 0) {
10995 return -TARGET_EFAULT;
10996 }
10997 ret = get_errno(clock_adjtime(arg1, &htx));
10998 if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
10999 return -TARGET_EFAULT;
11000 }
11001 }
11002 return ret;
11003#endif
bellard31e31b82003-02-18 22:55:36 +000011004 case TARGET_NR_getpgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011005 return get_errno(getpgid(arg1));
bellard31e31b82003-02-18 22:55:36 +000011006 case TARGET_NR_fchdir:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011007 return get_errno(fchdir(arg1));
bellard31e31b82003-02-18 22:55:36 +000011008 case TARGET_NR_personality:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011009 return get_errno(personality(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +000011010#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +000011011 case TARGET_NR__llseek:
11012 {
11013 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +000011014#if !defined(__NR_llseek)
Peter Maydell9fea2732016-08-11 18:59:39 +010011015 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
Peter Maydell0c1592d2011-02-22 13:02:26 +000011016 if (res == -1) {
11017 ret = get_errno(res);
11018 } else {
11019 ret = 0;
11020 }
11021#else
bellard31e31b82003-02-18 22:55:36 +000011022 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +000011023#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +000011024 if ((ret == 0) && put_user_s64(res, arg4)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011025 return -TARGET_EFAULT;
Peter Maydell0c1592d2011-02-22 13:02:26 +000011026 }
bellard31e31b82003-02-18 22:55:36 +000011027 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011028 return ret;
j_mayer7a3148a2007-04-05 07:13:51 +000011029#endif
Chen Gang704eff62015-08-21 05:37:33 +080011030#ifdef TARGET_NR_getdents
bellard31e31b82003-02-18 22:55:36 +000011031 case TARGET_NR_getdents:
Richard Hendersonfd08ddb2021-11-14 11:35:36 +010011032 return do_getdents(arg1, arg2, arg3);
Chen Gang704eff62015-08-21 05:37:33 +080011033#endif /* TARGET_NR_getdents */
ths3ae43202007-09-16 21:39:48 +000011034#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +000011035 case TARGET_NR_getdents64:
Richard Hendersonfd08ddb2021-11-14 11:35:36 +010011036 return do_getdents64(arg1, arg2, arg3);
bellarda541f292004-04-12 20:39:29 +000011037#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +010011038#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +000011039 case TARGET_NR__newselect:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011040 return do_select(arg1, arg2, arg3, arg4, arg5);
thse5febef2007-04-01 18:31:35 +000011041#endif
Filip Bozutae5ce9682020-08-25 00:30:49 +020011042#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +000011043 case TARGET_NR_poll:
Filip Bozutae5ce9682020-08-25 00:30:49 +020011044 return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
11045#endif
11046#ifdef TARGET_NR_ppoll
Mike Frysingerd8035d42011-02-07 01:05:51 -050011047 case TARGET_NR_ppoll:
Filip Bozutae5ce9682020-08-25 00:30:49 +020011048 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
11049#endif
11050#ifdef TARGET_NR_ppoll_time64
11051 case TARGET_NR_ppoll_time64:
11052 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
thse5febef2007-04-01 18:31:35 +000011053#endif
bellard31e31b82003-02-18 22:55:36 +000011054 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +000011055 /* NOTE: the flock constant seems to be the same for every
11056 Linux platform */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011057 return get_errno(safe_flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +000011058 case TARGET_NR_readv:
11059 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070011060 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11061 if (vec != NULL) {
Peter Maydell918c03e2016-06-06 19:58:02 +010011062 ret = get_errno(safe_readv(arg1, vec, arg3));
Richard Hendersonf287b2c2012-09-15 13:20:25 -070011063 unlock_iovec(vec, arg2, arg3, 1);
11064 } else {
11065 ret = -host_to_target_errno(errno);
11066 }
bellard31e31b82003-02-18 22:55:36 +000011067 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011068 return ret;
bellard31e31b82003-02-18 22:55:36 +000011069 case TARGET_NR_writev:
11070 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070011071 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11072 if (vec != NULL) {
Peter Maydell918c03e2016-06-06 19:58:02 +010011073 ret = get_errno(safe_writev(arg1, vec, arg3));
Richard Hendersonf287b2c2012-09-15 13:20:25 -070011074 unlock_iovec(vec, arg2, arg3, 0);
11075 } else {
11076 ret = -host_to_target_errno(errno);
11077 }
bellard31e31b82003-02-18 22:55:36 +000011078 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011079 return ret;
Dejan Jovicevic0f263862016-10-11 11:52:46 +020011080#if defined(TARGET_NR_preadv)
11081 case TARGET_NR_preadv:
11082 {
11083 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11084 if (vec != NULL) {
Max Filippov9ac22512018-04-04 17:30:41 -070011085 unsigned long low, high;
11086
11087 target_to_host_low_high(arg4, arg5, &low, &high);
11088 ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
Dejan Jovicevic0f263862016-10-11 11:52:46 +020011089 unlock_iovec(vec, arg2, arg3, 1);
11090 } else {
11091 ret = -host_to_target_errno(errno);
11092 }
11093 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011094 return ret;
Dejan Jovicevic0f263862016-10-11 11:52:46 +020011095#endif
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +020011096#if defined(TARGET_NR_pwritev)
11097 case TARGET_NR_pwritev:
11098 {
11099 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11100 if (vec != NULL) {
Max Filippov9ac22512018-04-04 17:30:41 -070011101 unsigned long low, high;
11102
11103 target_to_host_low_high(arg4, arg5, &low, &high);
11104 ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +020011105 unlock_iovec(vec, arg2, arg3, 0);
11106 } else {
11107 ret = -host_to_target_errno(errno);
11108 }
11109 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011110 return ret;
Dejan Jovicevicf8d00fb2016-10-11 11:52:47 +020011111#endif
bellard31e31b82003-02-18 22:55:36 +000011112 case TARGET_NR_getsid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011113 return get_errno(getsid(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +000011114#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +000011115 case TARGET_NR_fdatasync:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011116 return get_errno(fdatasync(arg1));
j_mayer7a3148a2007-04-05 07:13:51 +000011117#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -050011118 case TARGET_NR_sched_getaffinity:
11119 {
11120 unsigned int mask_size;
11121 unsigned long *mask;
11122
11123 /*
11124 * sched_getaffinity needs multiples of ulong, so need to take
11125 * care of mismatches between target ulong and host ulong sizes.
11126 */
11127 if (arg2 & (sizeof(abi_ulong) - 1)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011128 return -TARGET_EINVAL;
Mike Frysinger737de1d2011-02-07 01:05:55 -050011129 }
11130 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
11131
11132 mask = alloca(mask_size);
Samuel Thibault2e0a8712018-01-09 21:16:43 +010011133 memset(mask, 0, mask_size);
Mike Frysinger737de1d2011-02-07 01:05:55 -050011134 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
11135
11136 if (!is_error(ret)) {
Peter Maydellbe3bd282014-05-15 14:40:23 +010011137 if (ret > arg2) {
11138 /* More data returned than the caller's buffer will fit.
11139 * This only happens if sizeof(abi_long) < sizeof(long)
11140 * and the caller passed us a buffer holding an odd number
11141 * of abi_longs. If the host kernel is actually using the
11142 * extra 4 bytes then fail EINVAL; otherwise we can just
11143 * ignore them and only copy the interesting part.
11144 */
11145 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
11146 if (numcpus > arg2 * 8) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011147 return -TARGET_EINVAL;
Peter Maydellbe3bd282014-05-15 14:40:23 +010011148 }
11149 ret = arg2;
11150 }
11151
Samuel Thibault5fdefcf2018-02-11 18:47:04 +010011152 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011153 return -TARGET_EFAULT;
Samuel Thibault5fdefcf2018-02-11 18:47:04 +010011154 }
Mike Frysinger737de1d2011-02-07 01:05:55 -050011155 }
11156 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011157 return ret;
Mike Frysinger737de1d2011-02-07 01:05:55 -050011158 case TARGET_NR_sched_setaffinity:
11159 {
11160 unsigned int mask_size;
11161 unsigned long *mask;
11162
11163 /*
11164 * sched_setaffinity needs multiples of ulong, so need to take
11165 * care of mismatches between target ulong and host ulong sizes.
11166 */
11167 if (arg2 & (sizeof(abi_ulong) - 1)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011168 return -TARGET_EINVAL;
Mike Frysinger737de1d2011-02-07 01:05:55 -050011169 }
11170 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
Mike Frysinger737de1d2011-02-07 01:05:55 -050011171 mask = alloca(mask_size);
Samuel Thibault2e0a8712018-01-09 21:16:43 +010011172
11173 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
11174 if (ret) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011175 return ret;
Mike Frysinger737de1d2011-02-07 01:05:55 -050011176 }
Mike Frysinger737de1d2011-02-07 01:05:55 -050011177
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011178 return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
Mike Frysinger737de1d2011-02-07 01:05:55 -050011179 }
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010011180 case TARGET_NR_getcpu:
11181 {
Laurent Vivier9962b7c2023-09-25 17:10:28 +020011182 unsigned cpuid, node;
11183 ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL,
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010011184 arg2 ? &node : NULL,
11185 NULL));
11186 if (is_error(ret)) {
Richard Henderson259841c2018-08-18 12:01:09 -070011187 return ret;
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010011188 }
Laurent Vivier9962b7c2023-09-25 17:10:28 +020011189 if (arg1 && put_user_u32(cpuid, arg1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011190 return -TARGET_EFAULT;
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010011191 }
11192 if (arg2 && put_user_u32(node, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011193 return -TARGET_EFAULT;
Samuel Thibaultb827c3e2018-01-12 09:14:35 +010011194 }
11195 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011196 return ret;
bellard31e31b82003-02-18 22:55:36 +000011197 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +000011198 {
Tonis Tiigi407a1192022-01-04 20:18:19 -080011199 struct target_sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +000011200 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +000011201
Tom Mustaa1d5c5b2014-08-12 13:53:38 -050011202 if (arg2 == 0) {
11203 return -TARGET_EINVAL;
11204 }
Tonis Tiigi407a1192022-01-04 20:18:19 -080011205 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011206 return -TARGET_EFAULT;
Tonis Tiigi407a1192022-01-04 20:18:19 -080011207 }
bellard5cd43932003-03-29 16:54:36 +000011208 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +000011209 unlock_user_struct(target_schp, arg2, 0);
Tonis Tiigi407a1192022-01-04 20:18:19 -080011210 return get_errno(sys_sched_setparam(arg1, &schp));
bellard5cd43932003-03-29 16:54:36 +000011211 }
bellard31e31b82003-02-18 22:55:36 +000011212 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +000011213 {
Tonis Tiigi407a1192022-01-04 20:18:19 -080011214 struct target_sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +000011215 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -050011216
11217 if (arg2 == 0) {
11218 return -TARGET_EINVAL;
11219 }
Tonis Tiigi407a1192022-01-04 20:18:19 -080011220 ret = get_errno(sys_sched_getparam(arg1, &schp));
bellard5cd43932003-03-29 16:54:36 +000011221 if (!is_error(ret)) {
Tonis Tiigi407a1192022-01-04 20:18:19 -080011222 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011223 return -TARGET_EFAULT;
Tonis Tiigi407a1192022-01-04 20:18:19 -080011224 }
bellard5cd43932003-03-29 16:54:36 +000011225 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +000011226 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +000011227 }
11228 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011229 return ret;
bellard31e31b82003-02-18 22:55:36 +000011230 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +000011231 {
Tonis Tiigi407a1192022-01-04 20:18:19 -080011232 struct target_sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +000011233 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -050011234 if (arg3 == 0) {
11235 return -TARGET_EINVAL;
11236 }
Tonis Tiigi407a1192022-01-04 20:18:19 -080011237 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011238 return -TARGET_EFAULT;
Tonis Tiigi407a1192022-01-04 20:18:19 -080011239 }
bellard5cd43932003-03-29 16:54:36 +000011240 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +000011241 unlock_user_struct(target_schp, arg3, 0);
Tonis Tiigi407a1192022-01-04 20:18:19 -080011242 return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
bellard5cd43932003-03-29 16:54:36 +000011243 }
bellard31e31b82003-02-18 22:55:36 +000011244 case TARGET_NR_sched_getscheduler:
Tonis Tiigi407a1192022-01-04 20:18:19 -080011245 return get_errno(sys_sched_getscheduler(arg1));
Tonis Tiigi45ad7612022-01-04 20:18:18 -080011246 case TARGET_NR_sched_getattr:
11247 {
11248 struct target_sched_attr *target_scha;
11249 struct sched_attr scha;
11250 if (arg2 == 0) {
11251 return -TARGET_EINVAL;
11252 }
11253 if (arg3 > sizeof(scha)) {
11254 arg3 = sizeof(scha);
11255 }
11256 ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
11257 if (!is_error(ret)) {
11258 target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11259 if (!target_scha) {
11260 return -TARGET_EFAULT;
11261 }
11262 target_scha->size = tswap32(scha.size);
11263 target_scha->sched_policy = tswap32(scha.sched_policy);
11264 target_scha->sched_flags = tswap64(scha.sched_flags);
11265 target_scha->sched_nice = tswap32(scha.sched_nice);
11266 target_scha->sched_priority = tswap32(scha.sched_priority);
11267 target_scha->sched_runtime = tswap64(scha.sched_runtime);
11268 target_scha->sched_deadline = tswap64(scha.sched_deadline);
11269 target_scha->sched_period = tswap64(scha.sched_period);
11270 if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
11271 target_scha->sched_util_min = tswap32(scha.sched_util_min);
11272 target_scha->sched_util_max = tswap32(scha.sched_util_max);
11273 }
11274 unlock_user(target_scha, arg2, arg3);
11275 }
11276 return ret;
11277 }
11278 case TARGET_NR_sched_setattr:
11279 {
11280 struct target_sched_attr *target_scha;
11281 struct sched_attr scha;
11282 uint32_t size;
11283 int zeroed;
11284 if (arg2 == 0) {
11285 return -TARGET_EINVAL;
11286 }
11287 if (get_user_u32(size, arg2)) {
11288 return -TARGET_EFAULT;
11289 }
11290 if (!size) {
11291 size = offsetof(struct target_sched_attr, sched_util_min);
11292 }
11293 if (size < offsetof(struct target_sched_attr, sched_util_min)) {
11294 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11295 return -TARGET_EFAULT;
11296 }
11297 return -TARGET_E2BIG;
11298 }
11299
11300 zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
11301 if (zeroed < 0) {
11302 return zeroed;
11303 } else if (zeroed == 0) {
11304 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11305 return -TARGET_EFAULT;
11306 }
11307 return -TARGET_E2BIG;
11308 }
11309 if (size > sizeof(struct target_sched_attr)) {
11310 size = sizeof(struct target_sched_attr);
11311 }
11312
11313 target_scha = lock_user(VERIFY_READ, arg2, size, 1);
11314 if (!target_scha) {
11315 return -TARGET_EFAULT;
11316 }
11317 scha.size = size;
11318 scha.sched_policy = tswap32(target_scha->sched_policy);
11319 scha.sched_flags = tswap64(target_scha->sched_flags);
11320 scha.sched_nice = tswap32(target_scha->sched_nice);
11321 scha.sched_priority = tswap32(target_scha->sched_priority);
11322 scha.sched_runtime = tswap64(target_scha->sched_runtime);
11323 scha.sched_deadline = tswap64(target_scha->sched_deadline);
11324 scha.sched_period = tswap64(target_scha->sched_period);
11325 if (size > offsetof(struct target_sched_attr, sched_util_min)) {
11326 scha.sched_util_min = tswap32(target_scha->sched_util_min);
11327 scha.sched_util_max = tswap32(target_scha->sched_util_max);
11328 }
11329 unlock_user(target_scha, arg2, 0);
11330 return get_errno(sys_sched_setattr(arg1, &scha, arg3));
11331 }
bellard31e31b82003-02-18 22:55:36 +000011332 case TARGET_NR_sched_yield:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011333 return get_errno(sched_yield());
bellard31e31b82003-02-18 22:55:36 +000011334 case TARGET_NR_sched_get_priority_max:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011335 return get_errno(sched_get_priority_max(arg1));
bellard31e31b82003-02-18 22:55:36 +000011336 case TARGET_NR_sched_get_priority_min:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011337 return get_errno(sched_get_priority_min(arg1));
Alistair Francis859e8a82020-03-12 15:13:49 -070011338#ifdef TARGET_NR_sched_rr_get_interval
bellard31e31b82003-02-18 22:55:36 +000011339 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +000011340 {
bellard5cd43932003-03-29 16:54:36 +000011341 struct timespec ts;
11342 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11343 if (!is_error(ret)) {
Tom Mustad4290c42014-08-12 13:53:39 -050011344 ret = host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +000011345 }
11346 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011347 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070011348#endif
Filip Bozutaddcbde12020-08-24 21:21:16 +020011349#ifdef TARGET_NR_sched_rr_get_interval_time64
11350 case TARGET_NR_sched_rr_get_interval_time64:
11351 {
11352 struct timespec ts;
11353 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11354 if (!is_error(ret)) {
11355 ret = host_to_target_timespec64(arg2, &ts);
11356 }
11357 }
11358 return ret;
11359#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070011360#if defined(TARGET_NR_nanosleep)
bellard31e31b82003-02-18 22:55:36 +000011361 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +000011362 {
bellard1b6b0292003-03-22 17:31:38 +000011363 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +000011364 target_to_host_timespec(&req, arg1);
Peter Maydell9e518222016-06-06 19:58:09 +010011365 ret = get_errno(safe_nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +000011366 if (is_error(ret) && arg2) {
11367 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +000011368 }
11369 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011370 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070011371#endif
bellard31e31b82003-02-18 22:55:36 +000011372 case TARGET_NR_prctl:
Richard Henderson87e9bf22021-12-27 07:01:22 -080011373 return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
ths39b9aae2007-02-11 18:36:44 +000011374 break;
bellardd2fd1af2007-11-14 18:08:56 +000011375#ifdef TARGET_NR_arch_prctl
11376 case TARGET_NR_arch_prctl:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011377 return do_arch_prctl(cpu_env, arg1, arg2);
bellardd2fd1af2007-11-14 18:08:56 +000011378#endif
aurel32f2c7ba12008-03-28 22:32:06 +000011379#ifdef TARGET_NR_pread64
11380 case TARGET_NR_pread64:
James Clarke8bf8e9d2017-09-15 20:33:13 +010011381 if (regpairs_aligned(cpu_env, num)) {
Alexander Grafae017a52012-09-29 15:32:39 +000011382 arg4 = arg5;
11383 arg5 = arg6;
11384 }
Peter Maydell2bd3f892019-01-08 18:49:00 +000011385 if (arg2 == 0 && arg3 == 0) {
11386 /* Special-case NULL buffer and zero length, which should succeed */
11387 p = 0;
11388 } else {
11389 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11390 if (!p) {
11391 return -TARGET_EFAULT;
11392 }
11393 }
aurel32f2c7ba12008-03-28 22:32:06 +000011394 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
11395 unlock_user(p, arg2, ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011396 return ret;
aurel32f2c7ba12008-03-28 22:32:06 +000011397 case TARGET_NR_pwrite64:
James Clarke8bf8e9d2017-09-15 20:33:13 +010011398 if (regpairs_aligned(cpu_env, num)) {
Alexander Grafae017a52012-09-29 15:32:39 +000011399 arg4 = arg5;
11400 arg5 = arg6;
11401 }
Peter Maydell2bd3f892019-01-08 18:49:00 +000011402 if (arg2 == 0 && arg3 == 0) {
11403 /* Special-case NULL buffer and zero length, which should succeed */
11404 p = 0;
11405 } else {
11406 p = lock_user(VERIFY_READ, arg2, arg3, 1);
11407 if (!p) {
11408 return -TARGET_EFAULT;
11409 }
11410 }
aurel32f2c7ba12008-03-28 22:32:06 +000011411 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
11412 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011413 return ret;
aurel32f2c7ba12008-03-28 22:32:06 +000011414#endif
bellard31e31b82003-02-18 22:55:36 +000011415 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +000011416 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011417 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011418 ret = get_errno(sys_getcwd1(p, arg2));
11419 unlock_user(p, arg1, ret);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011420 return ret;
bellard31e31b82003-02-18 22:55:36 +000011421 case TARGET_NR_capget:
11422 case TARGET_NR_capset:
Peter Maydelle0eb2102014-03-17 12:15:35 +000011423 {
11424 struct target_user_cap_header *target_header;
11425 struct target_user_cap_data *target_data = NULL;
11426 struct __user_cap_header_struct header;
11427 struct __user_cap_data_struct data[2];
11428 struct __user_cap_data_struct *dataptr = NULL;
11429 int i, target_datalen;
11430 int data_items = 1;
11431
11432 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011433 return -TARGET_EFAULT;
Peter Maydelle0eb2102014-03-17 12:15:35 +000011434 }
11435 header.version = tswap32(target_header->version);
11436 header.pid = tswap32(target_header->pid);
11437
Peter Maydellec864872014-03-19 16:07:30 +000011438 if (header.version != _LINUX_CAPABILITY_VERSION) {
Peter Maydelle0eb2102014-03-17 12:15:35 +000011439 /* Version 2 and up takes pointer to two user_data structs */
11440 data_items = 2;
11441 }
11442
11443 target_datalen = sizeof(*target_data) * data_items;
11444
11445 if (arg2) {
11446 if (num == TARGET_NR_capget) {
11447 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
11448 } else {
11449 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
11450 }
11451 if (!target_data) {
11452 unlock_user_struct(target_header, arg1, 0);
Richard Henderson2852aaf2018-08-18 12:01:06 -070011453 return -TARGET_EFAULT;
Peter Maydelle0eb2102014-03-17 12:15:35 +000011454 }
11455
11456 if (num == TARGET_NR_capset) {
11457 for (i = 0; i < data_items; i++) {
11458 data[i].effective = tswap32(target_data[i].effective);
11459 data[i].permitted = tswap32(target_data[i].permitted);
11460 data[i].inheritable = tswap32(target_data[i].inheritable);
11461 }
11462 }
11463
11464 dataptr = data;
11465 }
11466
11467 if (num == TARGET_NR_capget) {
11468 ret = get_errno(capget(&header, dataptr));
11469 } else {
11470 ret = get_errno(capset(&header, dataptr));
11471 }
11472
11473 /* The kernel always updates version for both capget and capset */
11474 target_header->version = tswap32(header.version);
11475 unlock_user_struct(target_header, arg1, 1);
11476
11477 if (arg2) {
11478 if (num == TARGET_NR_capget) {
11479 for (i = 0; i < data_items; i++) {
11480 target_data[i].effective = tswap32(data[i].effective);
11481 target_data[i].permitted = tswap32(data[i].permitted);
11482 target_data[i].inheritable = tswap32(data[i].inheritable);
11483 }
11484 unlock_user(target_data, arg2, target_datalen);
11485 } else {
11486 unlock_user(target_data, arg2, 0);
11487 }
11488 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011489 return ret;
Peter Maydelle0eb2102014-03-17 12:15:35 +000011490 }
bellard31e31b82003-02-18 22:55:36 +000011491 case TARGET_NR_sigaltstack:
Richard Henderson6b208752021-04-25 19:53:12 -070011492 return do_sigaltstack(arg1, arg2, cpu_env);
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011493
11494#ifdef CONFIG_SENDFILE
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020011495#ifdef TARGET_NR_sendfile
bellard31e31b82003-02-18 22:55:36 +000011496 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011497 {
11498 off_t *offp = NULL;
11499 off_t off;
11500 if (arg3) {
11501 ret = get_user_sal(off, arg3);
11502 if (is_error(ret)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011503 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011504 }
11505 offp = &off;
11506 }
11507 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11508 if (!is_error(ret) && arg3) {
11509 abi_long ret2 = put_user_sal(off, arg3);
11510 if (is_error(ret2)) {
11511 ret = ret2;
11512 }
11513 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011514 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011515 }
Aleksandar Rikalo4f7f8922018-08-02 16:16:00 +020011516#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011517#ifdef TARGET_NR_sendfile64
11518 case TARGET_NR_sendfile64:
11519 {
11520 off_t *offp = NULL;
11521 off_t off;
11522 if (arg3) {
11523 ret = get_user_s64(off, arg3);
11524 if (is_error(ret)) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011525 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011526 }
11527 offp = &off;
11528 }
11529 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11530 if (!is_error(ret) && arg3) {
11531 abi_long ret2 = put_user_s64(off, arg3);
11532 if (is_error(ret2)) {
11533 ret = ret2;
11534 }
11535 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011536 return ret;
Peter Maydella8fd1ab2013-02-08 07:31:55 +000011537 }
11538#endif
bellardebc05482003-09-30 21:08:41 +000011539#endif
bellard048f6b42005-11-26 18:47:20 +000011540#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +000011541 case TARGET_NR_vfork:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011542 return get_errno(do_fork(cpu_env,
11543 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
11544 0, 0, 0, 0));
bellard048f6b42005-11-26 18:47:20 +000011545#endif
bellardebc05482003-09-30 21:08:41 +000011546#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +000011547 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +000011548 {
11549 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +030011550 int resource = target_to_host_resource(arg1);
11551 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +000011552 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +000011553 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +000011554 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011555 return -TARGET_EFAULT;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +090011556 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
11557 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +000011558 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +000011559 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011560 return ret;
bellard728584b2003-04-29 20:43:36 +000011561 }
bellardebc05482003-09-30 21:08:41 +000011562#endif
bellarda315a142005-01-30 22:59:18 +000011563#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +000011564 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +000011565 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011566 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011567 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
11568 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011569 return ret;
bellarda315a142005-01-30 22:59:18 +000011570#endif
11571#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +000011572 case TARGET_NR_ftruncate64:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011573 return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellarda315a142005-01-30 22:59:18 +000011574#endif
11575#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +000011576 case TARGET_NR_stat64:
Richard Henderson2852aaf2018-08-18 12:01:06 -070011577 if (!(p = lock_user_string(arg1))) {
11578 return -TARGET_EFAULT;
11579 }
pbrook53a59602006-03-25 19:31:22 +000011580 ret = get_errno(stat(path(p), &st));
11581 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +000011582 if (!is_error(ret))
11583 ret = host_to_target_stat64(cpu_env, arg2, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011584 return ret;
bellarda315a142005-01-30 22:59:18 +000011585#endif
11586#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +000011587 case TARGET_NR_lstat64:
Richard Henderson2852aaf2018-08-18 12:01:06 -070011588 if (!(p = lock_user_string(arg1))) {
11589 return -TARGET_EFAULT;
11590 }
pbrook53a59602006-03-25 19:31:22 +000011591 ret = get_errno(lstat(path(p), &st));
11592 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +000011593 if (!is_error(ret))
11594 ret = host_to_target_stat64(cpu_env, arg2, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011595 return ret;
bellarda315a142005-01-30 22:59:18 +000011596#endif
11597#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +000011598 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +000011599 ret = get_errno(fstat(arg1, &st));
11600 if (!is_error(ret))
11601 ret = host_to_target_stat64(cpu_env, arg2, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011602 return ret;
bellardec86b0f2003-04-11 00:15:04 +000011603#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +010011604#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +000011605#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +000011606 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +000011607#endif
11608#ifdef TARGET_NR_newfstatat
11609 case TARGET_NR_newfstatat:
11610#endif
Richard Henderson2852aaf2018-08-18 12:01:06 -070011611 if (!(p = lock_user_string(arg2))) {
11612 return -TARGET_EFAULT;
11613 }
Peter Maydellc0d472b2013-06-12 16:20:21 +010011614 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
Richard Henderson2852aaf2018-08-18 12:01:06 -070011615 unlock_user(p, arg2, 0);
balrog6a24a772008-09-20 02:23:36 +000011616 if (!is_error(ret))
11617 ret = host_to_target_stat64(cpu_env, arg3, &st);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011618 return ret;
bellarda315a142005-01-30 22:59:18 +000011619#endif
Aleksandar Rikaloefa92182019-06-28 12:43:34 +020011620#if defined(TARGET_NR_statx)
11621 case TARGET_NR_statx:
11622 {
11623 struct target_statx *target_stx;
11624 int dirfd = arg1;
11625 int flags = arg3;
11626
11627 p = lock_user_string(arg2);
11628 if (p == NULL) {
11629 return -TARGET_EFAULT;
11630 }
11631#if defined(__NR_statx)
11632 {
11633 /*
11634 * It is assumed that struct statx is architecture independent.
11635 */
11636 struct target_statx host_stx;
11637 int mask = arg4;
11638
11639 ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
11640 if (!is_error(ret)) {
11641 if (host_to_target_statx(&host_stx, arg5) != 0) {
11642 unlock_user(p, arg2, 0);
11643 return -TARGET_EFAULT;
11644 }
11645 }
11646
11647 if (ret != -TARGET_ENOSYS) {
11648 unlock_user(p, arg2, 0);
11649 return ret;
11650 }
11651 }
11652#endif
11653 ret = get_errno(fstatat(dirfd, path(p), &st, flags));
11654 unlock_user(p, arg2, 0);
11655
11656 if (!is_error(ret)) {
11657 if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
11658 return -TARGET_EFAULT;
11659 }
11660 memset(target_stx, 0, sizeof(*target_stx));
11661 __put_user(major(st.st_dev), &target_stx->stx_dev_major);
11662 __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
11663 __put_user(st.st_ino, &target_stx->stx_ino);
11664 __put_user(st.st_mode, &target_stx->stx_mode);
11665 __put_user(st.st_uid, &target_stx->stx_uid);
11666 __put_user(st.st_gid, &target_stx->stx_gid);
11667 __put_user(st.st_nlink, &target_stx->stx_nlink);
11668 __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
11669 __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
11670 __put_user(st.st_size, &target_stx->stx_size);
11671 __put_user(st.st_blksize, &target_stx->stx_blksize);
11672 __put_user(st.st_blocks, &target_stx->stx_blocks);
11673 __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
11674 __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
11675 __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
11676 unlock_user_struct(target_stx, arg5, 1);
11677 }
11678 }
11679 return ret;
11680#endif
Chen Gang704eff62015-08-21 05:37:33 +080011681#ifdef TARGET_NR_lchown
bellard67867302003-11-23 17:05:30 +000011682 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +000011683 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011684 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011685 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
11686 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011687 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080011688#endif
Riku Voipio0c866a72011-04-18 15:23:06 +030011689#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +000011690 case TARGET_NR_getuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011691 return get_errno(high2lowuid(getuid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011692#endif
11693#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +000011694 case TARGET_NR_getgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011695 return get_errno(high2lowgid(getgid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011696#endif
11697#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +000011698 case TARGET_NR_geteuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011699 return get_errno(high2lowuid(geteuid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011700#endif
11701#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +000011702 case TARGET_NR_getegid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011703 return get_errno(high2lowgid(getegid()));
Riku Voipio0c866a72011-04-18 15:23:06 +030011704#endif
bellard67867302003-11-23 17:05:30 +000011705 case TARGET_NR_setreuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011706 return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
bellard67867302003-11-23 17:05:30 +000011707 case TARGET_NR_setregid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011708 return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
bellard67867302003-11-23 17:05:30 +000011709 case TARGET_NR_getgroups:
Michael Tokarev725160f2023-06-03 20:23:38 +030011710 { /* the same code as for TARGET_NR_getgroups32 */
bellard67867302003-11-23 17:05:30 +000011711 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +030011712 target_id *target_grouplist;
Michael Tokarev1e35d322023-04-09 13:53:27 +030011713 g_autofree gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +000011714 int i;
11715
Peter Maydell8fbf89a2023-06-09 17:29:15 +010011716 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
Michael Tokarev1e35d322023-04-09 13:53:27 +030011717 return -TARGET_EINVAL;
bellard67867302003-11-23 17:05:30 +000011718 }
Michael Tokarev1e35d322023-04-09 13:53:27 +030011719 if (gidsetsize > 0) {
11720 grouplist = g_try_new(gid_t, gidsetsize);
11721 if (!grouplist) {
11722 return -TARGET_ENOMEM;
11723 }
11724 }
11725 ret = get_errno(getgroups(gidsetsize, grouplist));
11726 if (!is_error(ret) && gidsetsize > 0) {
11727 target_grouplist = lock_user(VERIFY_WRITE, arg2,
11728 gidsetsize * sizeof(target_id), 0);
11729 if (!target_grouplist) {
11730 return -TARGET_EFAULT;
11731 }
11732 for (i = 0; i < ret; i++) {
11733 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
11734 }
11735 unlock_user(target_grouplist, arg2,
11736 gidsetsize * sizeof(target_id));
11737 }
11738 return ret;
bellard67867302003-11-23 17:05:30 +000011739 }
bellard67867302003-11-23 17:05:30 +000011740 case TARGET_NR_setgroups:
Michael Tokarev725160f2023-06-03 20:23:38 +030011741 { /* the same code as for TARGET_NR_setgroups32 */
bellard67867302003-11-23 17:05:30 +000011742 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +030011743 target_id *target_grouplist;
Michael Tokarev1e35d322023-04-09 13:53:27 +030011744 g_autofree gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +000011745 int i;
Michael Tokarev1e35d322023-04-09 13:53:27 +030011746
11747 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
11748 return -TARGET_EINVAL;
11749 }
11750 if (gidsetsize > 0) {
11751 grouplist = g_try_new(gid_t, gidsetsize);
11752 if (!grouplist) {
11753 return -TARGET_ENOMEM;
11754 }
11755 target_grouplist = lock_user(VERIFY_READ, arg2,
11756 gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -050011757 if (!target_grouplist) {
Richard Henderson259841c2018-08-18 12:01:09 -070011758 return -TARGET_EFAULT;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -050011759 }
11760 for (i = 0; i < gidsetsize; i++) {
11761 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
11762 }
Michael Tokarev1e35d322023-04-09 13:53:27 +030011763 unlock_user(target_grouplist, arg2,
11764 gidsetsize * sizeof(target_id));
bellard579a97f2007-11-11 14:26:47 +000011765 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011766 return get_errno(setgroups(gidsetsize, grouplist));
bellard67867302003-11-23 17:05:30 +000011767 }
bellard67867302003-11-23 17:05:30 +000011768 case TARGET_NR_fchown:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011769 return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
Peter Maydellc0d472b2013-06-12 16:20:21 +010011770#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +000011771 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +000011772 if (!(p = lock_user_string(arg2)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011773 return -TARGET_EFAULT;
Peter Maydellc0d472b2013-06-12 16:20:21 +010011774 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
11775 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +000011776 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011777 return ret;
thsccfa72b2007-09-24 09:23:34 +000011778#endif
bellard67867302003-11-23 17:05:30 +000011779#ifdef TARGET_NR_setresuid
11780 case TARGET_NR_setresuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011781 return get_errno(sys_setresuid(low2highuid(arg1),
11782 low2highuid(arg2),
11783 low2highuid(arg3)));
bellard67867302003-11-23 17:05:30 +000011784#endif
11785#ifdef TARGET_NR_getresuid
11786 case TARGET_NR_getresuid:
11787 {
pbrook53a59602006-03-25 19:31:22 +000011788 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +000011789 ret = get_errno(getresuid(&ruid, &euid, &suid));
11790 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +000011791 if (put_user_id(high2lowuid(ruid), arg1)
11792 || put_user_id(high2lowuid(euid), arg2)
11793 || put_user_id(high2lowuid(suid), arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011794 return -TARGET_EFAULT;
bellard67867302003-11-23 17:05:30 +000011795 }
11796 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011797 return ret;
bellard67867302003-11-23 17:05:30 +000011798#endif
11799#ifdef TARGET_NR_getresgid
11800 case TARGET_NR_setresgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011801 return get_errno(sys_setresgid(low2highgid(arg1),
11802 low2highgid(arg2),
11803 low2highgid(arg3)));
bellard67867302003-11-23 17:05:30 +000011804#endif
11805#ifdef TARGET_NR_getresgid
11806 case TARGET_NR_getresgid:
11807 {
pbrook53a59602006-03-25 19:31:22 +000011808 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +000011809 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11810 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +000011811 if (put_user_id(high2lowgid(rgid), arg1)
11812 || put_user_id(high2lowgid(egid), arg2)
11813 || put_user_id(high2lowgid(sgid), arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011814 return -TARGET_EFAULT;
bellard67867302003-11-23 17:05:30 +000011815 }
11816 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011817 return ret;
bellard67867302003-11-23 17:05:30 +000011818#endif
Chen Gang704eff62015-08-21 05:37:33 +080011819#ifdef TARGET_NR_chown
bellard67867302003-11-23 17:05:30 +000011820 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +000011821 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011822 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011823 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
11824 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011825 return ret;
Chen Gang704eff62015-08-21 05:37:33 +080011826#endif
bellard67867302003-11-23 17:05:30 +000011827 case TARGET_NR_setuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011828 return get_errno(sys_setuid(low2highuid(arg1)));
bellard67867302003-11-23 17:05:30 +000011829 case TARGET_NR_setgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011830 return get_errno(sys_setgid(low2highgid(arg1)));
bellard67867302003-11-23 17:05:30 +000011831 case TARGET_NR_setfsuid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011832 return get_errno(setfsuid(arg1));
bellard67867302003-11-23 17:05:30 +000011833 case TARGET_NR_setfsgid:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011834 return get_errno(setfsgid(arg1));
bellard67867302003-11-23 17:05:30 +000011835
bellarda315a142005-01-30 22:59:18 +000011836#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +000011837 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +000011838 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011839 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000011840 ret = get_errno(lchown(p, arg2, arg3));
11841 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011842 return ret;
bellarda315a142005-01-30 22:59:18 +000011843#endif
11844#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +000011845 case TARGET_NR_getuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011846 return get_errno(getuid());
bellarda315a142005-01-30 22:59:18 +000011847#endif
aurel3264b4d282008-11-14 17:20:15 +000011848
11849#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11850 /* Alpha specific */
11851 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -080011852 {
11853 uid_t euid;
11854 euid=geteuid();
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020011855 cpu_env->ir[IR_A4]=euid;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011856 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011857 return get_errno(getuid());
aurel3264b4d282008-11-14 17:20:15 +000011858#endif
11859#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11860 /* Alpha specific */
11861 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -080011862 {
11863 uid_t egid;
11864 egid=getegid();
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020011865 cpu_env->ir[IR_A4]=egid;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011866 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011867 return get_errno(getgid());
aurel3264b4d282008-11-14 17:20:15 +000011868#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -080011869#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11870 /* Alpha specific */
11871 case TARGET_NR_osf_getsysinfo:
11872 ret = -TARGET_EOPNOTSUPP;
11873 switch (arg1) {
11874 case TARGET_GSI_IEEE_FP_CONTROL:
11875 {
Richard Henderson21ba8562019-04-26 15:20:51 -070011876 uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020011877 uint64_t swcr = cpu_env->swcr;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011878
Richard Henderson21ba8562019-04-26 15:20:51 -070011879 swcr &= ~SWCR_STATUS_MASK;
11880 swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011881
11882 if (put_user_u64 (swcr, arg2))
Richard Henderson2852aaf2018-08-18 12:01:06 -070011883 return -TARGET_EFAULT;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011884 ret = 0;
11885 }
11886 break;
11887
11888 /* case GSI_IEEE_STATE_AT_SIGNAL:
11889 -- Not implemented in linux kernel.
11890 case GSI_UACPROC:
11891 -- Retrieves current unaligned access state; not much used.
11892 case GSI_PROC_TYPE:
11893 -- Retrieves implver information; surely not used.
11894 case GSI_GET_HWRPB:
11895 -- Grabs a copy of the HWRPB; surely not used.
11896 */
11897 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011898 return ret;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011899#endif
11900#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11901 /* Alpha specific */
11902 case TARGET_NR_osf_setsysinfo:
11903 ret = -TARGET_EOPNOTSUPP;
11904 switch (arg1) {
11905 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -080011906 {
Richard Henderson21ba8562019-04-26 15:20:51 -070011907 uint64_t swcr, fpcr;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011908
Richard Henderson6e06d512012-06-01 09:08:21 -070011909 if (get_user_u64 (swcr, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011910 return -TARGET_EFAULT;
Richard Henderson6e06d512012-06-01 09:08:21 -070011911 }
Richard Hendersonba0e2762009-12-09 15:56:29 -080011912
Richard Henderson21ba8562019-04-26 15:20:51 -070011913 /*
11914 * The kernel calls swcr_update_status to update the
11915 * status bits from the fpcr at every point that it
11916 * could be queried. Therefore, we store the status
11917 * bits only in FPCR.
11918 */
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020011919 cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -080011920
Richard Henderson21ba8562019-04-26 15:20:51 -070011921 fpcr = cpu_alpha_load_fpcr(cpu_env);
11922 fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
11923 fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
Richard Henderson6e06d512012-06-01 09:08:21 -070011924 cpu_alpha_store_fpcr(cpu_env, fpcr);
11925 ret = 0;
11926 }
11927 break;
11928
11929 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
11930 {
Richard Henderson21ba8562019-04-26 15:20:51 -070011931 uint64_t exc, fpcr, fex;
Richard Henderson6e06d512012-06-01 09:08:21 -070011932
11933 if (get_user_u64(exc, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070011934 return -TARGET_EFAULT;
Richard Henderson6e06d512012-06-01 09:08:21 -070011935 }
Richard Henderson21ba8562019-04-26 15:20:51 -070011936 exc &= SWCR_STATUS_MASK;
11937 fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -080011938
Richard Henderson6e06d512012-06-01 09:08:21 -070011939 /* Old exceptions are not signaled. */
Richard Henderson21ba8562019-04-26 15:20:51 -070011940 fex = alpha_ieee_fpcr_to_swcr(fpcr);
11941 fex = exc & ~fex;
11942 fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020011943 fex &= (cpu_env)->swcr;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011944
Richard Henderson21ba8562019-04-26 15:20:51 -070011945 /* Update the hardware fpcr. */
11946 fpcr |= alpha_ieee_swcr_to_fpcr(exc);
11947 cpu_alpha_store_fpcr(cpu_env, fpcr);
11948
11949 if (fex) {
11950 int si_code = TARGET_FPE_FLTUNK;
Richard Henderson6e06d512012-06-01 09:08:21 -070011951 target_siginfo_t info;
Richard Henderson21ba8562019-04-26 15:20:51 -070011952
11953 if (fex & SWCR_TRAP_ENABLE_DNO) {
11954 si_code = TARGET_FPE_FLTUND;
11955 }
11956 if (fex & SWCR_TRAP_ENABLE_INE) {
11957 si_code = TARGET_FPE_FLTRES;
11958 }
11959 if (fex & SWCR_TRAP_ENABLE_UNF) {
11960 si_code = TARGET_FPE_FLTUND;
11961 }
11962 if (fex & SWCR_TRAP_ENABLE_OVF) {
11963 si_code = TARGET_FPE_FLTOVF;
11964 }
11965 if (fex & SWCR_TRAP_ENABLE_DZE) {
11966 si_code = TARGET_FPE_FLTDIV;
11967 }
11968 if (fex & SWCR_TRAP_ENABLE_INV) {
11969 si_code = TARGET_FPE_FLTINV;
11970 }
11971
Richard Henderson6e06d512012-06-01 09:08:21 -070011972 info.si_signo = SIGFPE;
11973 info.si_errno = 0;
11974 info.si_code = si_code;
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020011975 info._sifields._sigfault._addr = (cpu_env)->pc;
11976 queue_signal(cpu_env, info.si_signo,
Peter Maydell9d2803f2016-07-28 16:44:46 +010011977 QEMU_SI_FAULT, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -080011978 }
Richard Henderson21ba8562019-04-26 15:20:51 -070011979 ret = 0;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011980 }
11981 break;
11982
11983 /* case SSI_NVPAIRS:
11984 -- Used with SSIN_UACPROC to enable unaligned accesses.
11985 case SSI_IEEE_STATE_AT_SIGNAL:
11986 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11987 -- Not implemented in linux kernel
11988 */
11989 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070011990 return ret;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011991#endif
11992#ifdef TARGET_NR_osf_sigprocmask
11993 /* Alpha specific. */
11994 case TARGET_NR_osf_sigprocmask:
11995 {
11996 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +010011997 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -080011998 sigset_t set, oldset;
11999
12000 switch(arg1) {
12001 case TARGET_SIG_BLOCK:
12002 how = SIG_BLOCK;
12003 break;
12004 case TARGET_SIG_UNBLOCK:
12005 how = SIG_UNBLOCK;
12006 break;
12007 case TARGET_SIG_SETMASK:
12008 how = SIG_SETMASK;
12009 break;
12010 default:
Richard Henderson259841c2018-08-18 12:01:09 -070012011 return -TARGET_EINVAL;
Richard Hendersonba0e2762009-12-09 15:56:29 -080012012 }
12013 mask = arg2;
12014 target_to_host_old_sigset(&set, &mask);
Peter Maydell3d3efba2016-05-27 15:51:49 +010012015 ret = do_sigprocmask(how, &set, &oldset);
12016 if (!ret) {
12017 host_to_target_old_sigset(&mask, &oldset);
12018 ret = mask;
12019 }
Richard Hendersonba0e2762009-12-09 15:56:29 -080012020 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012021 return ret;
Richard Hendersonba0e2762009-12-09 15:56:29 -080012022#endif
aurel3264b4d282008-11-14 17:20:15 +000012023
bellarda315a142005-01-30 22:59:18 +000012024#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +000012025 case TARGET_NR_getgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012026 return get_errno(getgid());
bellarda315a142005-01-30 22:59:18 +000012027#endif
12028#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +000012029 case TARGET_NR_geteuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012030 return get_errno(geteuid());
bellarda315a142005-01-30 22:59:18 +000012031#endif
12032#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +000012033 case TARGET_NR_getegid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012034 return get_errno(getegid());
bellarda315a142005-01-30 22:59:18 +000012035#endif
12036#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +000012037 case TARGET_NR_setreuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012038 return get_errno(setreuid(arg1, arg2));
bellarda315a142005-01-30 22:59:18 +000012039#endif
12040#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +000012041 case TARGET_NR_setregid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012042 return get_errno(setregid(arg1, arg2));
bellarda315a142005-01-30 22:59:18 +000012043#endif
12044#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +000012045 case TARGET_NR_getgroups32:
Michael Tokarev725160f2023-06-03 20:23:38 +030012046 { /* the same code as for TARGET_NR_getgroups */
bellard99c475a2005-01-31 20:45:13 +000012047 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +000012048 uint32_t *target_grouplist;
Michael Tokarev1e35d322023-04-09 13:53:27 +030012049 g_autofree gid_t *grouplist = NULL;
bellard99c475a2005-01-31 20:45:13 +000012050 int i;
12051
Peter Maydell8fbf89a2023-06-09 17:29:15 +010012052 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
Michael Tokarev1e35d322023-04-09 13:53:27 +030012053 return -TARGET_EINVAL;
12054 }
12055 if (gidsetsize > 0) {
12056 grouplist = g_try_new(gid_t, gidsetsize);
12057 if (!grouplist) {
12058 return -TARGET_ENOMEM;
12059 }
12060 }
bellard99c475a2005-01-31 20:45:13 +000012061 ret = get_errno(getgroups(gidsetsize, grouplist));
Michael Tokarev1e35d322023-04-09 13:53:27 +030012062 if (!is_error(ret) && gidsetsize > 0) {
12063 target_grouplist = lock_user(VERIFY_WRITE, arg2,
12064 gidsetsize * 4, 0);
bellard579a97f2007-11-11 14:26:47 +000012065 if (!target_grouplist) {
Richard Henderson259841c2018-08-18 12:01:09 -070012066 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000012067 }
Michael Tokarev1e35d322023-04-09 13:53:27 +030012068 for (i = 0; i < ret; i++) {
pbrook53a59602006-03-25 19:31:22 +000012069 target_grouplist[i] = tswap32(grouplist[i]);
Michael Tokarev1e35d322023-04-09 13:53:27 +030012070 }
pbrook53a59602006-03-25 19:31:22 +000012071 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +000012072 }
Michael Tokarev1e35d322023-04-09 13:53:27 +030012073 return ret;
bellard99c475a2005-01-31 20:45:13 +000012074 }
bellarda315a142005-01-30 22:59:18 +000012075#endif
12076#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +000012077 case TARGET_NR_setgroups32:
Michael Tokarev725160f2023-06-03 20:23:38 +030012078 { /* the same code as for TARGET_NR_setgroups */
bellard99c475a2005-01-31 20:45:13 +000012079 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +000012080 uint32_t *target_grouplist;
Michael Tokarev1e35d322023-04-09 13:53:27 +030012081 g_autofree gid_t *grouplist = NULL;
bellard99c475a2005-01-31 20:45:13 +000012082 int i;
ths3b46e622007-09-17 08:09:54 +000012083
Michael Tokarev1e35d322023-04-09 13:53:27 +030012084 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
12085 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +000012086 }
Michael Tokarev1e35d322023-04-09 13:53:27 +030012087 if (gidsetsize > 0) {
12088 grouplist = g_try_new(gid_t, gidsetsize);
12089 if (!grouplist) {
12090 return -TARGET_ENOMEM;
12091 }
12092 target_grouplist = lock_user(VERIFY_READ, arg2,
12093 gidsetsize * 4, 1);
12094 if (!target_grouplist) {
12095 return -TARGET_EFAULT;
12096 }
12097 for (i = 0; i < gidsetsize; i++) {
12098 grouplist[i] = tswap32(target_grouplist[i]);
12099 }
12100 unlock_user(target_grouplist, arg2, 0);
12101 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012102 return get_errno(setgroups(gidsetsize, grouplist));
bellard99c475a2005-01-31 20:45:13 +000012103 }
bellarda315a142005-01-30 22:59:18 +000012104#endif
12105#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +000012106 case TARGET_NR_fchown32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012107 return get_errno(fchown(arg1, arg2, arg3));
bellarda315a142005-01-30 22:59:18 +000012108#endif
12109#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +000012110 case TARGET_NR_setresuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012111 return get_errno(sys_setresuid(arg1, arg2, arg3));
bellarda315a142005-01-30 22:59:18 +000012112#endif
12113#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +000012114 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +000012115 {
pbrook53a59602006-03-25 19:31:22 +000012116 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +000012117 ret = get_errno(getresuid(&ruid, &euid, &suid));
12118 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +000012119 if (put_user_u32(ruid, arg1)
12120 || put_user_u32(euid, arg2)
12121 || put_user_u32(suid, arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070012122 return -TARGET_EFAULT;
bellardb03c60f2003-03-23 17:19:56 +000012123 }
12124 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012125 return ret;
bellarda315a142005-01-30 22:59:18 +000012126#endif
12127#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +000012128 case TARGET_NR_setresgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012129 return get_errno(sys_setresgid(arg1, arg2, arg3));
bellarda315a142005-01-30 22:59:18 +000012130#endif
12131#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +000012132 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +000012133 {
pbrook53a59602006-03-25 19:31:22 +000012134 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +000012135 ret = get_errno(getresgid(&rgid, &egid, &sgid));
12136 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +000012137 if (put_user_u32(rgid, arg1)
12138 || put_user_u32(egid, arg2)
12139 || put_user_u32(sgid, arg3))
Richard Henderson2852aaf2018-08-18 12:01:06 -070012140 return -TARGET_EFAULT;
bellardb03c60f2003-03-23 17:19:56 +000012141 }
12142 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012143 return ret;
bellarda315a142005-01-30 22:59:18 +000012144#endif
12145#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +000012146 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +000012147 if (!(p = lock_user_string(arg1)))
Richard Henderson2852aaf2018-08-18 12:01:06 -070012148 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +000012149 ret = get_errno(chown(p, arg2, arg3));
12150 unlock_user(p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012151 return ret;
bellarda315a142005-01-30 22:59:18 +000012152#endif
12153#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +000012154 case TARGET_NR_setuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012155 return get_errno(sys_setuid(arg1));
bellarda315a142005-01-30 22:59:18 +000012156#endif
12157#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +000012158 case TARGET_NR_setgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012159 return get_errno(sys_setgid(arg1));
bellarda315a142005-01-30 22:59:18 +000012160#endif
12161#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +000012162 case TARGET_NR_setfsuid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012163 return get_errno(setfsuid(arg1));
bellarda315a142005-01-30 22:59:18 +000012164#endif
12165#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +000012166 case TARGET_NR_setfsgid32:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012167 return get_errno(setfsgid(arg1));
bellarda315a142005-01-30 22:59:18 +000012168#endif
bellardffa65c32004-01-04 23:57:22 +000012169#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +000012170 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +000012171 {
Thomas Weißschuhf443a262023-04-22 12:03:14 +020012172 void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000012173 if (!a) {
Richard Henderson259841c2018-08-18 12:01:09 -070012174 return -TARGET_ENOMEM;
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000012175 }
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000012176 p = lock_user_string(arg3);
12177 if (!p) {
Richard Henderson259841c2018-08-18 12:01:09 -070012178 ret = -TARGET_EFAULT;
12179 } else {
12180 ret = get_errno(mincore(a, arg2, p));
12181 unlock_user(p, arg3, ret);
Franklin \"Snaipe\" Mathieu98a33312017-02-17 08:58:00 +000012182 }
aurel3204bb9ac2008-10-01 21:46:41 +000012183 unlock_user(a, arg1, 0);
12184 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012185 return ret;
bellardffa65c32004-01-04 23:57:22 +000012186#endif
aurel32408321b2008-10-01 21:46:32 +000012187#ifdef TARGET_NR_arm_fadvise64_64
12188 case TARGET_NR_arm_fadvise64_64:
Peter Maydelle0156a92016-05-31 15:45:09 +010012189 /* arm_fadvise64_64 looks like fadvise64_64 but
12190 * with different argument order: fd, advice, offset, len
12191 * rather than the usual fd, offset, len, advice.
12192 * Note that offset and len are both 64-bit so appear as
12193 * pairs of 32-bit registers.
12194 */
12195 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
12196 target_offset64(arg5, arg6), arg2);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012197 return -host_to_target_errno(ret);
aurel32408321b2008-10-01 21:46:32 +000012198#endif
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012199
WANG Xueruieeed2292022-10-06 16:55:00 +080012200#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012201
12202#ifdef TARGET_NR_fadvise64_64
12203 case TARGET_NR_fadvise64_64:
Max Filippov64a563d2018-04-01 15:02:34 -070012204#if defined(TARGET_PPC) || defined(TARGET_XTENSA)
Laurent Vivier43046b52017-03-02 01:11:45 +010012205 /* 6 args: fd, advice, offset (high, low), len (high, low) */
12206 ret = arg2;
12207 arg2 = arg3;
12208 arg3 = arg4;
12209 arg4 = arg5;
12210 arg5 = arg6;
12211 arg6 = ret;
12212#else
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012213 /* 6 args: fd, offset (high, low), len (high, low), advice */
James Clarke8bf8e9d2017-09-15 20:33:13 +010012214 if (regpairs_aligned(cpu_env, num)) {
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012215 /* offset is in (3,4), len in (5,6) and advice in 7 */
12216 arg2 = arg3;
12217 arg3 = arg4;
12218 arg4 = arg5;
12219 arg5 = arg6;
12220 arg6 = arg7;
12221 }
Laurent Vivier43046b52017-03-02 01:11:45 +010012222#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012223 ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
12224 target_offset64(arg4, arg5), arg6);
12225 return -host_to_target_errno(ret);
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012226#endif
12227
12228#ifdef TARGET_NR_fadvise64
12229 case TARGET_NR_fadvise64:
12230 /* 5 args: fd, offset (high, low), len, advice */
James Clarke8bf8e9d2017-09-15 20:33:13 +010012231 if (regpairs_aligned(cpu_env, num)) {
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012232 /* offset is in (3,4), len in 5 and advice in 6 */
12233 arg2 = arg3;
12234 arg3 = arg4;
12235 arg4 = arg5;
12236 arg5 = arg6;
12237 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012238 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
12239 return -host_to_target_errno(ret);
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012240#endif
12241
12242#else /* not a 32-bit ABI */
Peter Maydelle0156a92016-05-31 15:45:09 +010012243#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +000012244#ifdef TARGET_NR_fadvise64_64
12245 case TARGET_NR_fadvise64_64:
12246#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +020012247#ifdef TARGET_NR_fadvise64
12248 case TARGET_NR_fadvise64:
12249#endif
12250#ifdef TARGET_S390X
12251 switch (arg4) {
12252 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
12253 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
12254 case 6: arg4 = POSIX_FADV_DONTNEED; break;
12255 case 7: arg4 = POSIX_FADV_NOREUSE; break;
12256 default: break;
12257 }
12258#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012259 return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
aurel32408321b2008-10-01 21:46:32 +000012260#endif
Peter Maydellbadd3cd2016-05-31 15:45:10 +010012261#endif /* end of 64-bit ABI fadvise handling */
12262
bellardffa65c32004-01-04 23:57:22 +000012263#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +000012264 case TARGET_NR_madvise:
Ilya Leoshkevich892a4f62022-06-21 16:42:05 +020012265 return target_madvise(arg1, arg2, arg3);
bellardffa65c32004-01-04 23:57:22 +000012266#endif
Alex Bennéebbf5f2a2020-04-03 20:11:40 +010012267#ifdef TARGET_NR_fcntl64
bellard31e31b82003-02-18 22:55:36 +000012268 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +000012269 {
Alex Bennéebbf5f2a2020-04-03 20:11:40 +010012270 int cmd;
12271 struct flock64 fl;
Peter Maydell213d3e92016-06-13 11:22:05 +010012272 from_flock64_fn *copyfrom = copy_from_user_flock64;
12273 to_flock64_fn *copyto = copy_to_user_flock64;
12274
pbrookce4defa2006-02-09 16:49:55 +000012275#ifdef TARGET_ARM
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020012276 if (!cpu_env->eabi) {
Laurent Vivier7f254c52018-05-02 23:57:30 +020012277 copyfrom = copy_from_user_oabi_flock64;
12278 copyto = copy_to_user_oabi_flock64;
Peter Maydell213d3e92016-06-13 11:22:05 +010012279 }
pbrookce4defa2006-02-09 16:49:55 +000012280#endif
bellard77e46722003-04-29 20:39:06 +000012281
Alex Bennéebbf5f2a2020-04-03 20:11:40 +010012282 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +000012283 if (cmd == -TARGET_EINVAL) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012284 return cmd;
Peter Maydell31b63192011-12-05 23:11:50 +000012285 }
thsb1e341e2007-03-20 21:50:52 +000012286
bellard60cd49d2003-03-16 22:53:56 +000012287 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +000012288 case TARGET_F_GETLK64:
Peter Maydell213d3e92016-06-13 11:22:05 +010012289 ret = copyfrom(&fl, arg3);
12290 if (ret) {
12291 break;
ths58134272007-03-31 18:59:32 +000012292 }
Laurent Vivieraf8ab2b2018-07-13 14:58:05 +020012293 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
Peter Maydell213d3e92016-06-13 11:22:05 +010012294 if (ret == 0) {
12295 ret = copyto(arg3, &fl);
12296 }
bellard77e46722003-04-29 20:39:06 +000012297 break;
12298
thsb1e341e2007-03-20 21:50:52 +000012299 case TARGET_F_SETLK64:
12300 case TARGET_F_SETLKW64:
Peter Maydell213d3e92016-06-13 11:22:05 +010012301 ret = copyfrom(&fl, arg3);
12302 if (ret) {
12303 break;
pbrookce4defa2006-02-09 16:49:55 +000012304 }
Peter Maydell435da5e2016-06-13 11:22:05 +010012305 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +000012306 break;
bellard60cd49d2003-03-16 22:53:56 +000012307 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +020012308 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +000012309 break;
12310 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012311 return ret;
bellard77e46722003-04-29 20:39:06 +000012312 }
bellard60cd49d2003-03-16 22:53:56 +000012313#endif
ths7d600c82006-12-08 01:32:58 +000012314#ifdef TARGET_NR_cacheflush
12315 case TARGET_NR_cacheflush:
12316 /* self-modifying code is handled automatically, so nothing needed */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012317 return 0;
ths7d600c82006-12-08 01:32:58 +000012318#endif
bellardc573ff62004-01-04 15:51:36 +000012319#ifdef TARGET_NR_getpagesize
12320 case TARGET_NR_getpagesize:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012321 return TARGET_PAGE_SIZE;
bellardc573ff62004-01-04 15:51:36 +000012322#endif
bellard31e31b82003-02-18 22:55:36 +000012323 case TARGET_NR_gettid:
Daniel P. Berrangé71ba74f2019-03-20 16:18:42 +000012324 return get_errno(sys_gettid());
thse5febef2007-04-01 18:31:35 +000012325#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +000012326 case TARGET_NR_readahead:
WANG Xueruieeed2292022-10-06 16:55:00 +080012327#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
James Clarke8bf8e9d2017-09-15 20:33:13 +010012328 if (regpairs_aligned(cpu_env, num)) {
aurel322054ac92008-10-13 21:08:07 +000012329 arg2 = arg3;
12330 arg3 = arg4;
12331 arg4 = arg5;
12332 }
Lena Djokic77c68502016-11-24 17:08:56 +010012333 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
aurel322054ac92008-10-13 21:08:07 +000012334#else
12335 ret = get_errno(readahead(arg1, arg2, arg3));
12336#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012337 return ret;
thse5febef2007-04-01 18:31:35 +000012338#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012339#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +000012340#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +000012341 case TARGET_NR_listxattr:
12342 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +000012343 {
Laurent Vivier77c9f172023-09-25 17:10:29 +020012344 void *b = 0;
Peter Maydellfb5590f2011-12-14 15:37:19 +000012345 if (arg2) {
12346 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12347 if (!b) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012348 return -TARGET_EFAULT;
Peter Maydellfb5590f2011-12-14 15:37:19 +000012349 }
12350 }
12351 p = lock_user_string(arg1);
12352 if (p) {
12353 if (num == TARGET_NR_listxattr) {
12354 ret = get_errno(listxattr(p, b, arg3));
12355 } else {
12356 ret = get_errno(llistxattr(p, b, arg3));
12357 }
12358 } else {
12359 ret = -TARGET_EFAULT;
12360 }
12361 unlock_user(p, arg1, 0);
12362 unlock_user(b, arg2, arg3);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012363 return ret;
Peter Maydellfb5590f2011-12-14 15:37:19 +000012364 }
12365 case TARGET_NR_flistxattr:
12366 {
12367 void *b = 0;
12368 if (arg2) {
12369 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12370 if (!b) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012371 return -TARGET_EFAULT;
Peter Maydellfb5590f2011-12-14 15:37:19 +000012372 }
12373 }
12374 ret = get_errno(flistxattr(arg1, b, arg3));
12375 unlock_user(b, arg2, arg3);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012376 return ret;
Peter Maydellfb5590f2011-12-14 15:37:19 +000012377 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012378 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +000012379 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012380 {
Laurent Vivier77c9f172023-09-25 17:10:29 +020012381 void *n, *v = 0;
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012382 if (arg3) {
12383 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12384 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012385 return -TARGET_EFAULT;
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012386 }
12387 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012388 p = lock_user_string(arg1);
12389 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012390 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000012391 if (num == TARGET_NR_setxattr) {
12392 ret = get_errno(setxattr(p, n, v, arg4, arg5));
12393 } else {
12394 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
12395 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012396 } else {
12397 ret = -TARGET_EFAULT;
12398 }
12399 unlock_user(p, arg1, 0);
12400 unlock_user(n, arg2, 0);
12401 unlock_user(v, arg3, 0);
12402 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012403 return ret;
Peter Maydell30297b52011-12-14 15:37:18 +000012404 case TARGET_NR_fsetxattr:
12405 {
12406 void *n, *v = 0;
12407 if (arg3) {
12408 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12409 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012410 return -TARGET_EFAULT;
Peter Maydell30297b52011-12-14 15:37:18 +000012411 }
12412 }
12413 n = lock_user_string(arg2);
12414 if (n) {
12415 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
12416 } else {
12417 ret = -TARGET_EFAULT;
12418 }
12419 unlock_user(n, arg2, 0);
12420 unlock_user(v, arg3, 0);
12421 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012422 return ret;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012423 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +000012424 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012425 {
Laurent Vivier77c9f172023-09-25 17:10:29 +020012426 void *n, *v = 0;
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012427 if (arg3) {
12428 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12429 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012430 return -TARGET_EFAULT;
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012431 }
12432 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012433 p = lock_user_string(arg1);
12434 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +000012435 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000012436 if (num == TARGET_NR_getxattr) {
12437 ret = get_errno(getxattr(p, n, v, arg4));
12438 } else {
12439 ret = get_errno(lgetxattr(p, n, v, arg4));
12440 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012441 } else {
12442 ret = -TARGET_EFAULT;
12443 }
12444 unlock_user(p, arg1, 0);
12445 unlock_user(n, arg2, 0);
12446 unlock_user(v, arg3, arg4);
12447 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012448 return ret;
Peter Maydell30297b52011-12-14 15:37:18 +000012449 case TARGET_NR_fgetxattr:
12450 {
12451 void *n, *v = 0;
12452 if (arg3) {
12453 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12454 if (!v) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012455 return -TARGET_EFAULT;
Peter Maydell30297b52011-12-14 15:37:18 +000012456 }
12457 }
12458 n = lock_user_string(arg2);
12459 if (n) {
12460 ret = get_errno(fgetxattr(arg1, n, v, arg4));
12461 } else {
12462 ret = -TARGET_EFAULT;
12463 }
12464 unlock_user(n, arg2, 0);
12465 unlock_user(v, arg3, arg4);
12466 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012467 return ret;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012468 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +000012469 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012470 {
Laurent Vivier77c9f172023-09-25 17:10:29 +020012471 void *n;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012472 p = lock_user_string(arg1);
12473 n = lock_user_string(arg2);
12474 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000012475 if (num == TARGET_NR_removexattr) {
12476 ret = get_errno(removexattr(p, n));
12477 } else {
12478 ret = get_errno(lremovexattr(p, n));
12479 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012480 } else {
12481 ret = -TARGET_EFAULT;
12482 }
12483 unlock_user(p, arg1, 0);
12484 unlock_user(n, arg2, 0);
12485 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012486 return ret;
Peter Maydell30297b52011-12-14 15:37:18 +000012487 case TARGET_NR_fremovexattr:
12488 {
12489 void *n;
12490 n = lock_user_string(arg2);
12491 if (n) {
12492 ret = get_errno(fremovexattr(arg1, n));
12493 } else {
12494 ret = -TARGET_EFAULT;
12495 }
12496 unlock_user(n, arg2, 0);
12497 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012498 return ret;
bellardebc05482003-09-30 21:08:41 +000012499#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070012500#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +000012501#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +000012502 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +000012503#if defined(TARGET_MIPS)
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020012504 cpu_env->active_tc.CP0_UserLocal = arg1;
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012505 return 0;
edgar_iglef967792009-01-07 14:19:38 +000012506#elif defined(TARGET_CRIS)
12507 if (arg1 & 0xff)
12508 ret = -TARGET_EINVAL;
12509 else {
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020012510 cpu_env->pregs[PR_PID] = arg1;
edgar_iglef967792009-01-07 14:19:38 +000012511 ret = 0;
12512 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012513 return ret;
bellard8d18e892007-11-14 15:18:40 +000012514#elif defined(TARGET_I386) && defined(TARGET_ABI32)
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012515 return do_set_thread_area(cpu_env, arg1);
Peter Maydell1ccd9372013-07-16 18:44:55 +010012516#elif defined(TARGET_M68K)
12517 {
Andreas Färber0429a972013-08-26 18:14:44 +020012518 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +010012519 ts->tp_value = arg1;
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012520 return 0;
Peter Maydell1ccd9372013-07-16 18:44:55 +010012521 }
ths6f5b89a2007-03-02 20:48:00 +000012522#else
Richard Henderson10f45d92018-08-18 12:01:07 -070012523 return -TARGET_ENOSYS;
ths6f5b89a2007-03-02 20:48:00 +000012524#endif
12525#endif
12526#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +000012527 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +000012528#if defined(TARGET_I386) && defined(TARGET_ABI32)
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012529 return do_get_thread_area(cpu_env, arg1);
Peter Maydell1ccd9372013-07-16 18:44:55 +010012530#elif defined(TARGET_M68K)
12531 {
Andreas Färber0429a972013-08-26 18:14:44 +020012532 TaskState *ts = cpu->opaque;
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012533 return ts->tp_value;
Peter Maydell1ccd9372013-07-16 18:44:55 +010012534 }
bellard8d18e892007-11-14 15:18:40 +000012535#else
Richard Henderson10f45d92018-08-18 12:01:07 -070012536 return -TARGET_ENOSYS;
bellardebc05482003-09-30 21:08:41 +000012537#endif
bellard8d18e892007-11-14 15:18:40 +000012538#endif
bellard48dc41e2006-06-21 18:15:50 +000012539#ifdef TARGET_NR_getdomainname
12540 case TARGET_NR_getdomainname:
Richard Henderson10f45d92018-08-18 12:01:07 -070012541 return -TARGET_ENOSYS;
bellard48dc41e2006-06-21 18:15:50 +000012542#endif
ths6f5b89a2007-03-02 20:48:00 +000012543
Max Filippov12e33402018-04-01 13:13:49 -070012544#ifdef TARGET_NR_clock_settime
12545 case TARGET_NR_clock_settime:
12546 {
12547 struct timespec ts;
12548
12549 ret = target_to_host_timespec(&ts, arg2);
12550 if (!is_error(ret)) {
12551 ret = get_errno(clock_settime(arg1, &ts));
12552 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012553 return ret;
Max Filippov12e33402018-04-01 13:13:49 -070012554 }
12555#endif
Alistair Francisc6c8d102020-03-12 15:13:53 -070012556#ifdef TARGET_NR_clock_settime64
12557 case TARGET_NR_clock_settime64:
12558 {
12559 struct timespec ts;
12560
12561 ret = target_to_host_timespec64(&ts, arg2);
12562 if (!is_error(ret)) {
12563 ret = get_errno(clock_settime(arg1, &ts));
12564 }
12565 return ret;
12566 }
12567#endif
thsb5906f92007-03-19 13:32:45 +000012568#ifdef TARGET_NR_clock_gettime
12569 case TARGET_NR_clock_gettime:
12570 {
12571 struct timespec ts;
12572 ret = get_errno(clock_gettime(arg1, &ts));
12573 if (!is_error(ret)) {
Max Filippovb9f99082018-04-01 13:14:04 -070012574 ret = host_to_target_timespec(arg2, &ts);
thsb5906f92007-03-19 13:32:45 +000012575 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012576 return ret;
thsb5906f92007-03-19 13:32:45 +000012577 }
12578#endif
Alistair Francisc6c8d102020-03-12 15:13:53 -070012579#ifdef TARGET_NR_clock_gettime64
12580 case TARGET_NR_clock_gettime64:
12581 {
12582 struct timespec ts;
12583 ret = get_errno(clock_gettime(arg1, &ts));
12584 if (!is_error(ret)) {
12585 ret = host_to_target_timespec64(arg2, &ts);
12586 }
12587 return ret;
12588 }
12589#endif
thsb5906f92007-03-19 13:32:45 +000012590#ifdef TARGET_NR_clock_getres
12591 case TARGET_NR_clock_getres:
12592 {
12593 struct timespec ts;
12594 ret = get_errno(clock_getres(arg1, &ts));
12595 if (!is_error(ret)) {
12596 host_to_target_timespec(arg2, &ts);
12597 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012598 return ret;
thsb5906f92007-03-19 13:32:45 +000012599 }
12600#endif
Filip Bozuta828cb3a2020-07-22 17:34:21 +020012601#ifdef TARGET_NR_clock_getres_time64
12602 case TARGET_NR_clock_getres_time64:
12603 {
12604 struct timespec ts;
12605 ret = get_errno(clock_getres(arg1, &ts));
12606 if (!is_error(ret)) {
12607 host_to_target_timespec64(arg2, &ts);
12608 }
12609 return ret;
12610 }
12611#endif
pbrook63d76512008-05-29 13:43:29 +000012612#ifdef TARGET_NR_clock_nanosleep
12613 case TARGET_NR_clock_nanosleep:
12614 {
12615 struct timespec ts;
Filip Bozutab09d6402020-07-27 22:13:26 +020012616 if (target_to_host_timespec(&ts, arg3)) {
12617 return -TARGET_EFAULT;
12618 }
Peter Maydell9e518222016-06-06 19:58:09 +010012619 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12620 &ts, arg4 ? &ts : NULL));
Laurent Vivier8ec68a02020-07-24 07:45:05 +010012621 /*
12622 * if the call is interrupted by a signal handler, it fails
12623 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12624 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12625 */
Filip Bozutab09d6402020-07-27 22:13:26 +020012626 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12627 host_to_target_timespec(arg4, &ts)) {
12628 return -TARGET_EFAULT;
Laurent Vivier8ec68a02020-07-24 07:45:05 +010012629 }
Tom Musta8fbe8fd2014-08-12 13:53:41 -050012630
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012631 return ret;
pbrook63d76512008-05-29 13:43:29 +000012632 }
12633#endif
Filip Bozuta6ac03b22020-08-24 21:21:15 +020012634#ifdef TARGET_NR_clock_nanosleep_time64
12635 case TARGET_NR_clock_nanosleep_time64:
12636 {
12637 struct timespec ts;
12638
12639 if (target_to_host_timespec64(&ts, arg3)) {
12640 return -TARGET_EFAULT;
12641 }
12642
12643 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12644 &ts, arg4 ? &ts : NULL));
12645
12646 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12647 host_to_target_timespec64(arg4, &ts)) {
12648 return -TARGET_EFAULT;
12649 }
12650 return ret;
12651 }
12652#endif
thsb5906f92007-03-19 13:32:45 +000012653
Helge Deller9a7f6822022-05-28 12:52:10 +020012654#if defined(TARGET_NR_set_tid_address)
ths6f5b89a2007-03-02 20:48:00 +000012655 case TARGET_NR_set_tid_address:
Helge Deller9a7f6822022-05-28 12:52:10 +020012656 {
12657 TaskState *ts = cpu->opaque;
12658 ts->child_tidptr = arg1;
12659 /* do not call host set_tid_address() syscall, instead return tid() */
12660 return get_errno(sys_gettid());
12661 }
ths6f5b89a2007-03-02 20:48:00 +000012662#endif
12663
ths4cae1d12007-07-12 11:06:53 +000012664 case TARGET_NR_tkill:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012665 return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +000012666
ths71455572007-06-21 21:45:30 +000012667 case TARGET_NR_tgkill:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012668 return get_errno(safe_tgkill((int)arg1, (int)arg2,
12669 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +000012670
ths4f2b1fe2007-06-21 21:57:12 +000012671#ifdef TARGET_NR_set_robust_list
12672 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +000012673 case TARGET_NR_get_robust_list:
12674 /* The ABI for supporting robust futexes has userspace pass
12675 * the kernel a pointer to a linked list which is updated by
12676 * userspace after the syscall; the list is walked by the kernel
12677 * when the thread exits. Since the linked list in QEMU guest
12678 * memory isn't a valid linked list for the host and we have
12679 * no way to reliably intercept the thread-death event, we can't
12680 * support these. Silently return ENOSYS so that guest userspace
12681 * falls back to a non-robust futex implementation (which should
12682 * be OK except in the corner case of the guest crashing while
12683 * holding a mutex that is shared with another process via
12684 * shared memory).
12685 */
Richard Henderson10f45d92018-08-18 12:01:07 -070012686 return -TARGET_ENOSYS;
ths4f2b1fe2007-06-21 21:57:12 +000012687#endif
12688
Peter Maydell1acae9f2013-07-02 14:04:12 +010012689#if defined(TARGET_NR_utimensat)
ths9007f0e2007-09-25 17:50:37 +000012690 case TARGET_NR_utimensat:
12691 {
Riku Voipioebc996f2009-04-21 15:01:51 +030012692 struct timespec *tsp, ts[2];
12693 if (!arg3) {
12694 tsp = NULL;
12695 } else {
Filip Bozutab3a3af72020-08-11 13:31:01 +020012696 if (target_to_host_timespec(ts, arg3)) {
12697 return -TARGET_EFAULT;
12698 }
12699 if (target_to_host_timespec(ts + 1, arg3 +
12700 sizeof(struct target_timespec))) {
12701 return -TARGET_EFAULT;
12702 }
Riku Voipioebc996f2009-04-21 15:01:51 +030012703 tsp = ts;
12704 }
ths9007f0e2007-09-25 17:50:37 +000012705 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +030012706 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +000012707 else {
bellard579a97f2007-11-11 14:26:47 +000012708 if (!(p = lock_user_string(arg2))) {
Richard Henderson259841c2018-08-18 12:01:09 -070012709 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000012710 }
Riku Voipioebc996f2009-04-21 15:01:51 +030012711 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +000012712 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +000012713 }
12714 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012715 return ret;
ths9007f0e2007-09-25 17:50:37 +000012716#endif
Filip Bozutacac46eb2020-08-25 00:30:50 +020012717#ifdef TARGET_NR_utimensat_time64
12718 case TARGET_NR_utimensat_time64:
12719 {
12720 struct timespec *tsp, ts[2];
12721 if (!arg3) {
12722 tsp = NULL;
12723 } else {
12724 if (target_to_host_timespec64(ts, arg3)) {
12725 return -TARGET_EFAULT;
12726 }
12727 if (target_to_host_timespec64(ts + 1, arg3 +
12728 sizeof(struct target__kernel_timespec))) {
12729 return -TARGET_EFAULT;
12730 }
12731 tsp = ts;
12732 }
12733 if (!arg2)
12734 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
12735 else {
12736 p = lock_user_string(arg2);
12737 if (!p) {
12738 return -TARGET_EFAULT;
12739 }
12740 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
12741 unlock_user(p, arg2, 0);
12742 }
12743 }
12744 return ret;
12745#endif
Alistair Francis859e8a82020-03-12 15:13:49 -070012746#ifdef TARGET_NR_futex
pbrookbd0c5662008-05-29 14:34:11 +000012747 case TARGET_NR_futex:
Richard Henderson0fbc0f82022-08-28 19:09:59 -070012748 return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
Alistair Francis859e8a82020-03-12 15:13:49 -070012749#endif
Alistair Francis14690292020-03-18 15:47:01 -070012750#ifdef TARGET_NR_futex_time64
12751 case TARGET_NR_futex_time64:
Richard Henderson0fbc0f82022-08-28 19:09:59 -070012752 return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
Alistair Francis14690292020-03-18 15:47:01 -070012753#endif
Paul Brook33f53ac2022-01-26 20:26:36 +000012754#ifdef CONFIG_INOTIFY
12755#if defined(TARGET_NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +000012756 case TARGET_NR_inotify_init:
Paul Brook33f53ac2022-01-26 20:26:36 +000012757 ret = get_errno(inotify_init());
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012758 if (ret >= 0) {
12759 fd_trans_register(ret, &target_inotify_trans);
12760 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012761 return ret;
aurel3239b59762008-10-01 21:46:50 +000012762#endif
Paul Brook33f53ac2022-01-26 20:26:36 +000012763#if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
Riku Voipioc05c7a72010-03-26 15:25:11 +000012764 case TARGET_NR_inotify_init1:
Paul Brook33f53ac2022-01-26 20:26:36 +000012765 ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
Lena Djokicfea243e2016-11-24 17:08:53 +010012766 fcntl_flags_tbl)));
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012767 if (ret >= 0) {
12768 fd_trans_register(ret, &target_inotify_trans);
12769 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012770 return ret;
Riku Voipioc05c7a72010-03-26 15:25:11 +000012771#endif
Paul Brook33f53ac2022-01-26 20:26:36 +000012772#if defined(TARGET_NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +000012773 case TARGET_NR_inotify_add_watch:
12774 p = lock_user_string(arg2);
Paul Brook33f53ac2022-01-26 20:26:36 +000012775 ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
aurel3239b59762008-10-01 21:46:50 +000012776 unlock_user(p, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012777 return ret;
aurel3239b59762008-10-01 21:46:50 +000012778#endif
Paul Brook33f53ac2022-01-26 20:26:36 +000012779#if defined(TARGET_NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +000012780 case TARGET_NR_inotify_rm_watch:
Paul Brook33f53ac2022-01-26 20:26:36 +000012781 return get_errno(inotify_rm_watch(arg1, arg2));
12782#endif
aurel3239b59762008-10-01 21:46:50 +000012783#endif
ths9007f0e2007-09-25 17:50:37 +000012784
Nathan Froyd8ec9cf82009-07-22 09:14:36 -070012785#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +000012786 case TARGET_NR_mq_open:
12787 {
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012788 struct mq_attr posix_mq_attr;
Lena Djokic26400772016-11-24 17:08:58 +010012789 struct mq_attr *pposix_mq_attr;
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012790 int host_flags;
aurel3224e10032009-04-15 16:11:43 +000012791
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012792 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
Lena Djokic26400772016-11-24 17:08:58 +010012793 pposix_mq_attr = NULL;
12794 if (arg4) {
12795 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012796 return -TARGET_EFAULT;
Lena Djokic26400772016-11-24 17:08:58 +010012797 }
12798 pposix_mq_attr = &posix_mq_attr;
Tom Mustab6ce1f62014-08-12 13:53:36 -050012799 }
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012800 p = lock_user_string(arg1 - 1);
12801 if (!p) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012802 return -TARGET_EFAULT;
Aleksandar Markovicc7536ab2016-09-22 18:56:55 +020012803 }
Lena Djokic26400772016-11-24 17:08:58 +010012804 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
aurel3224e10032009-04-15 16:11:43 +000012805 unlock_user (p, arg1, 0);
12806 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012807 return ret;
aurel3224e10032009-04-15 16:11:43 +000012808
12809 case TARGET_NR_mq_unlink:
12810 p = lock_user_string(arg1 - 1);
Peter Maydell32112152016-07-12 13:02:13 +010012811 if (!p) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012812 return -TARGET_EFAULT;
Peter Maydell32112152016-07-12 13:02:13 +010012813 }
aurel3224e10032009-04-15 16:11:43 +000012814 ret = get_errno(mq_unlink(p));
12815 unlock_user (p, arg1, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012816 return ret;
aurel3224e10032009-04-15 16:11:43 +000012817
Alistair Francis859e8a82020-03-12 15:13:49 -070012818#ifdef TARGET_NR_mq_timedsend
aurel3224e10032009-04-15 16:11:43 +000012819 case TARGET_NR_mq_timedsend:
12820 {
12821 struct timespec ts;
12822
12823 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12824 if (arg5 != 0) {
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012825 if (target_to_host_timespec(&ts, arg5)) {
12826 return -TARGET_EFAULT;
12827 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012828 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012829 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12830 return -TARGET_EFAULT;
12831 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012832 } else {
12833 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
aurel3224e10032009-04-15 16:11:43 +000012834 }
aurel3224e10032009-04-15 16:11:43 +000012835 unlock_user (p, arg2, arg3);
12836 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012837 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070012838#endif
Filip Bozutad107e372020-08-24 21:37:52 +020012839#ifdef TARGET_NR_mq_timedsend_time64
12840 case TARGET_NR_mq_timedsend_time64:
12841 {
12842 struct timespec ts;
12843
12844 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12845 if (arg5 != 0) {
12846 if (target_to_host_timespec64(&ts, arg5)) {
12847 return -TARGET_EFAULT;
12848 }
12849 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
12850 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12851 return -TARGET_EFAULT;
12852 }
12853 } else {
12854 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
12855 }
12856 unlock_user(p, arg2, arg3);
12857 }
12858 return ret;
12859#endif
aurel3224e10032009-04-15 16:11:43 +000012860
Alistair Francis859e8a82020-03-12 15:13:49 -070012861#ifdef TARGET_NR_mq_timedreceive
aurel3224e10032009-04-15 16:11:43 +000012862 case TARGET_NR_mq_timedreceive:
12863 {
12864 struct timespec ts;
12865 unsigned int prio;
12866
12867 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12868 if (arg5 != 0) {
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012869 if (target_to_host_timespec(&ts, arg5)) {
12870 return -TARGET_EFAULT;
12871 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012872 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12873 &prio, &ts));
Filip Bozutadcbcf5c2020-08-24 21:37:51 +020012874 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12875 return -TARGET_EFAULT;
12876 }
Peter Maydelld40ecd62016-06-06 19:58:06 +010012877 } else {
12878 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12879 &prio, NULL));
aurel3224e10032009-04-15 16:11:43 +000012880 }
aurel3224e10032009-04-15 16:11:43 +000012881 unlock_user (p, arg2, arg3);
12882 if (arg4 != 0)
12883 put_user_u32(prio, arg4);
12884 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012885 return ret;
Alistair Francis859e8a82020-03-12 15:13:49 -070012886#endif
Filip Bozutad107e372020-08-24 21:37:52 +020012887#ifdef TARGET_NR_mq_timedreceive_time64
12888 case TARGET_NR_mq_timedreceive_time64:
12889 {
12890 struct timespec ts;
12891 unsigned int prio;
12892
12893 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12894 if (arg5 != 0) {
12895 if (target_to_host_timespec64(&ts, arg5)) {
12896 return -TARGET_EFAULT;
12897 }
12898 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12899 &prio, &ts));
12900 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12901 return -TARGET_EFAULT;
12902 }
12903 } else {
12904 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12905 &prio, NULL));
12906 }
12907 unlock_user(p, arg2, arg3);
12908 if (arg4 != 0) {
12909 put_user_u32(prio, arg4);
12910 }
12911 }
12912 return ret;
12913#endif
aurel3224e10032009-04-15 16:11:43 +000012914
12915 /* Not implemented for now... */
12916/* case TARGET_NR_mq_notify: */
12917/* break; */
12918
12919 case TARGET_NR_mq_getsetattr:
12920 {
12921 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
12922 ret = 0;
aurel3224e10032009-04-15 16:11:43 +000012923 if (arg2 != 0) {
12924 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
Max Filippova23ea402018-03-31 08:20:15 -070012925 ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
12926 &posix_mq_attr_out));
12927 } else if (arg3 != 0) {
12928 ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
aurel3224e10032009-04-15 16:11:43 +000012929 }
Max Filippova23ea402018-03-31 08:20:15 -070012930 if (ret == 0 && arg3 != 0) {
12931 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
12932 }
aurel3224e10032009-04-15 16:11:43 +000012933 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012934 return ret;
aurel3224e10032009-04-15 16:11:43 +000012935#endif
12936
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012937#ifdef CONFIG_SPLICE
12938#ifdef TARGET_NR_tee
12939 case TARGET_NR_tee:
12940 {
12941 ret = get_errno(tee(arg1,arg2,arg3,arg4));
12942 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012943 return ret;
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012944#endif
12945#ifdef TARGET_NR_splice
12946 case TARGET_NR_splice:
12947 {
12948 loff_t loff_in, loff_out;
12949 loff_t *ploff_in = NULL, *ploff_out = NULL;
Andreas Schwab17644b32015-03-10 17:11:35 +010012950 if (arg2) {
12951 if (get_user_u64(loff_in, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012952 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012953 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012954 ploff_in = &loff_in;
12955 }
Andreas Schwab17644b32015-03-10 17:11:35 +010012956 if (arg4) {
12957 if (get_user_u64(loff_out, arg4)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012958 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012959 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012960 ploff_out = &loff_out;
12961 }
12962 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
Andreas Schwab17644b32015-03-10 17:11:35 +010012963 if (arg2) {
12964 if (put_user_u64(loff_in, arg2)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012965 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012966 }
12967 }
12968 if (arg4) {
12969 if (put_user_u64(loff_out, arg4)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070012970 return -TARGET_EFAULT;
Andreas Schwab17644b32015-03-10 17:11:35 +010012971 }
12972 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012973 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012974 return ret;
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012975#endif
12976#ifdef TARGET_NR_vmsplice
12977 case TARGET_NR_vmsplice:
12978 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070012979 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
12980 if (vec != NULL) {
12981 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
12982 unlock_iovec(vec, arg2, arg3, 0);
12983 } else {
12984 ret = -host_to_target_errno(errno);
12985 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012986 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012987 return ret;
vibisreenivasan3ce34df2009-05-16 18:32:41 +053012988#endif
12989#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +030012990#ifdef CONFIG_EVENTFD
12991#if defined(TARGET_NR_eventfd)
12992 case TARGET_NR_eventfd:
12993 ret = get_errno(eventfd(arg1, 0));
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030012994 if (ret >= 0) {
12995 fd_trans_register(ret, &target_eventfd_trans);
12996 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070012997 return ret;
Riku Voipioc2882b92009-08-12 15:08:24 +030012998#endif
12999#if defined(TARGET_NR_eventfd2)
13000 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +020013001 {
Helge Deller78721302021-02-10 07:12:14 +010013002 int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
Petar Jovanovic5947c692013-04-08 20:26:10 +020013003 if (arg2 & TARGET_O_NONBLOCK) {
13004 host_flags |= O_NONBLOCK;
13005 }
13006 if (arg2 & TARGET_O_CLOEXEC) {
13007 host_flags |= O_CLOEXEC;
13008 }
13009 ret = get_errno(eventfd(arg1, host_flags));
Philippe Mathieu-Daudéb929f7e2017-07-26 23:42:19 -030013010 if (ret >= 0) {
13011 fd_trans_register(ret, &target_eventfd_trans);
13012 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013013 return ret;
Petar Jovanovic5947c692013-04-08 20:26:10 +020013014 }
Riku Voipioc2882b92009-08-12 15:08:24 +030013015#endif
13016#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +030013017#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
13018 case TARGET_NR_fallocate:
WANG Xueruieeed2292022-10-06 16:55:00 +080013019#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
Alexander Graf20249ae2012-02-06 21:37:07 +010013020 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
13021 target_offset64(arg5, arg6)));
13022#else
Ulrich Hechtd0927932009-09-17 20:22:14 +030013023 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +010013024#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013025 return ret;
Ulrich Hechtd0927932009-09-17 20:22:14 +030013026#endif
Peter Maydellc727f472011-01-06 11:05:10 +000013027#if defined(CONFIG_SYNC_FILE_RANGE)
13028#if defined(TARGET_NR_sync_file_range)
13029 case TARGET_NR_sync_file_range:
WANG Xueruieeed2292022-10-06 16:55:00 +080013030#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
Riku Voipiobfcedc52011-06-20 16:24:39 +030013031#if defined(TARGET_MIPS)
13032 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13033 target_offset64(arg5, arg6), arg7));
13034#else
Peter Maydellc727f472011-01-06 11:05:10 +000013035 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
13036 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +030013037#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +000013038#else
13039 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
13040#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013041 return ret;
Peter Maydellc727f472011-01-06 11:05:10 +000013042#endif
Laurent Vivier5bcb4982020-03-10 11:33:50 +010013043#if defined(TARGET_NR_sync_file_range2) || \
13044 defined(TARGET_NR_arm_sync_file_range)
Peter Maydellc727f472011-01-06 11:05:10 +000013045#if defined(TARGET_NR_sync_file_range2)
13046 case TARGET_NR_sync_file_range2:
Laurent Vivier5bcb4982020-03-10 11:33:50 +010013047#endif
13048#if defined(TARGET_NR_arm_sync_file_range)
13049 case TARGET_NR_arm_sync_file_range:
13050#endif
Peter Maydellc727f472011-01-06 11:05:10 +000013051 /* This is like sync_file_range but the arguments are reordered */
WANG Xueruieeed2292022-10-06 16:55:00 +080013052#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
Peter Maydellc727f472011-01-06 11:05:10 +000013053 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13054 target_offset64(arg5, arg6), arg2));
13055#else
13056 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
13057#endif
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013058 return ret;
Peter Maydellc727f472011-01-06 11:05:10 +000013059#endif
13060#endif
Laurent Viviere36800c2015-10-02 14:48:09 +020013061#if defined(TARGET_NR_signalfd4)
13062 case TARGET_NR_signalfd4:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013063 return do_signalfd4(arg1, arg2, arg4);
Laurent Viviere36800c2015-10-02 14:48:09 +020013064#endif
13065#if defined(TARGET_NR_signalfd)
13066 case TARGET_NR_signalfd:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013067 return do_signalfd4(arg1, arg2, 0);
Laurent Viviere36800c2015-10-02 14:48:09 +020013068#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000013069#if defined(CONFIG_EPOLL)
13070#if defined(TARGET_NR_epoll_create)
13071 case TARGET_NR_epoll_create:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013072 return get_errno(epoll_create(arg1));
Peter Maydell3b6edd12011-02-15 18:35:05 +000013073#endif
13074#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
13075 case TARGET_NR_epoll_create1:
Sergei Trofimovich386d3862020-04-15 23:05:08 +010013076 return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
Peter Maydell3b6edd12011-02-15 18:35:05 +000013077#endif
13078#if defined(TARGET_NR_epoll_ctl)
13079 case TARGET_NR_epoll_ctl:
13080 {
13081 struct epoll_event ep;
13082 struct epoll_event *epp = 0;
13083 if (arg4) {
LemonBoyc7811022020-04-17 17:34:54 +020013084 if (arg2 != EPOLL_CTL_DEL) {
13085 struct target_epoll_event *target_ep;
13086 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
13087 return -TARGET_EFAULT;
13088 }
13089 ep.events = tswap32(target_ep->events);
13090 /*
13091 * The epoll_data_t union is just opaque data to the kernel,
13092 * so we transfer all 64 bits across and need not worry what
13093 * actual data type it is.
13094 */
13095 ep.data.u64 = tswap64(target_ep->data.u64);
13096 unlock_user_struct(target_ep, arg4, 0);
Peter Maydell3b6edd12011-02-15 18:35:05 +000013097 }
LemonBoyc7811022020-04-17 17:34:54 +020013098 /*
13099 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
13100 * non-null pointer, even though this argument is ignored.
13101 *
Peter Maydell3b6edd12011-02-15 18:35:05 +000013102 */
Peter Maydell3b6edd12011-02-15 18:35:05 +000013103 epp = &ep;
13104 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013105 return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
Peter Maydell3b6edd12011-02-15 18:35:05 +000013106 }
13107#endif
13108
Peter Maydell227f0212016-06-06 19:58:11 +010013109#if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
Peter Maydell3b6edd12011-02-15 18:35:05 +000013110#if defined(TARGET_NR_epoll_wait)
13111 case TARGET_NR_epoll_wait:
13112#endif
Peter Maydell227f0212016-06-06 19:58:11 +010013113#if defined(TARGET_NR_epoll_pwait)
Peter Maydell3b6edd12011-02-15 18:35:05 +000013114 case TARGET_NR_epoll_pwait:
13115#endif
13116 {
13117 struct target_epoll_event *target_ep;
13118 struct epoll_event *ep;
13119 int epfd = arg1;
13120 int maxevents = arg3;
13121 int timeout = arg4;
13122
Peter Maydell2ba7fae32016-07-18 15:35:59 +010013123 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013124 return -TARGET_EINVAL;
Peter Maydell2ba7fae32016-07-18 15:35:59 +010013125 }
13126
Peter Maydell3b6edd12011-02-15 18:35:05 +000013127 target_ep = lock_user(VERIFY_WRITE, arg2,
13128 maxevents * sizeof(struct target_epoll_event), 1);
13129 if (!target_ep) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013130 return -TARGET_EFAULT;
Peter Maydell3b6edd12011-02-15 18:35:05 +000013131 }
13132
Peter Maydell04c95f42016-07-18 15:36:00 +010013133 ep = g_try_new(struct epoll_event, maxevents);
13134 if (!ep) {
13135 unlock_user(target_ep, arg2, 0);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013136 return -TARGET_ENOMEM;
Peter Maydell04c95f42016-07-18 15:36:00 +010013137 }
Peter Maydell3b6edd12011-02-15 18:35:05 +000013138
13139 switch (num) {
Peter Maydell227f0212016-06-06 19:58:11 +010013140#if defined(TARGET_NR_epoll_pwait)
Peter Maydell3b6edd12011-02-15 18:35:05 +000013141 case TARGET_NR_epoll_pwait:
13142 {
Richard Hendersoncd0e31a2022-03-15 01:43:07 -070013143 sigset_t *set = NULL;
Peter Maydell3b6edd12011-02-15 18:35:05 +000013144
13145 if (arg5) {
Richard Hendersoncd0e31a2022-03-15 01:43:07 -070013146 ret = process_sigsuspend_mask(&set, arg5, arg6);
13147 if (ret != 0) {
Peter Maydellc8157012016-06-30 14:23:24 +010013148 break;
13149 }
Peter Maydell3b6edd12011-02-15 18:35:05 +000013150 }
13151
Peter Maydell227f0212016-06-06 19:58:11 +010013152 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13153 set, SIGSET_T_SIZE));
Richard Hendersoncd0e31a2022-03-15 01:43:07 -070013154
13155 if (set) {
13156 finish_sigsuspend_mask(ret);
13157 }
Peter Maydell3b6edd12011-02-15 18:35:05 +000013158 break;
13159 }
13160#endif
13161#if defined(TARGET_NR_epoll_wait)
13162 case TARGET_NR_epoll_wait:
Peter Maydell227f0212016-06-06 19:58:11 +010013163 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13164 NULL, 0));
Peter Maydell3b6edd12011-02-15 18:35:05 +000013165 break;
13166#endif
13167 default:
13168 ret = -TARGET_ENOSYS;
13169 }
13170 if (!is_error(ret)) {
13171 int i;
13172 for (i = 0; i < ret; i++) {
13173 target_ep[i].events = tswap32(ep[i].events);
13174 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
13175 }
Peter Maydell04c95f42016-07-18 15:36:00 +010013176 unlock_user(target_ep, arg2,
13177 ret * sizeof(struct target_epoll_event));
13178 } else {
13179 unlock_user(target_ep, arg2, 0);
Peter Maydell3b6edd12011-02-15 18:35:05 +000013180 }
Peter Maydell04c95f42016-07-18 15:36:00 +010013181 g_free(ep);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013182 return ret;
Peter Maydell3b6edd12011-02-15 18:35:05 +000013183 }
13184#endif
13185#endif
Peter Maydell163a05a2011-06-27 17:44:52 +010013186#ifdef TARGET_NR_prlimit64
13187 case TARGET_NR_prlimit64:
13188 {
13189 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
13190 struct target_rlimit64 *target_rnew, *target_rold;
13191 struct host_rlimit64 rnew, rold, *rnewp = 0;
Felix Janda95018012014-12-02 22:11:17 +010013192 int resource = target_to_host_resource(arg2);
Tobias Koch055d92f2020-03-05 21:24:00 +010013193
13194 if (arg3 && (resource != RLIMIT_AS &&
13195 resource != RLIMIT_DATA &&
13196 resource != RLIMIT_STACK)) {
Peter Maydell163a05a2011-06-27 17:44:52 +010013197 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013198 return -TARGET_EFAULT;
Peter Maydell163a05a2011-06-27 17:44:52 +010013199 }
Ilya Leoshkevich9c1da8b2023-02-24 01:39:06 +010013200 __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
13201 __get_user(rnew.rlim_max, &target_rnew->rlim_max);
Peter Maydell163a05a2011-06-27 17:44:52 +010013202 unlock_user_struct(target_rnew, arg3, 0);
13203 rnewp = &rnew;
13204 }
13205
Felix Janda95018012014-12-02 22:11:17 +010013206 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
Peter Maydell163a05a2011-06-27 17:44:52 +010013207 if (!is_error(ret) && arg4) {
13208 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013209 return -TARGET_EFAULT;
Peter Maydell163a05a2011-06-27 17:44:52 +010013210 }
Ilya Leoshkevich9c1da8b2023-02-24 01:39:06 +010013211 __put_user(rold.rlim_cur, &target_rold->rlim_cur);
13212 __put_user(rold.rlim_max, &target_rold->rlim_max);
Peter Maydell163a05a2011-06-27 17:44:52 +010013213 unlock_user_struct(target_rold, arg4, 1);
13214 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013215 return ret;
Peter Maydell163a05a2011-06-27 17:44:52 +010013216 }
13217#endif
Richard Henderson3d21d292012-09-15 13:20:46 -070013218#ifdef TARGET_NR_gethostname
13219 case TARGET_NR_gethostname:
13220 {
13221 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
13222 if (name) {
13223 ret = get_errno(gethostname(name, arg2));
13224 unlock_user(name, arg1, arg2);
13225 } else {
13226 ret = -TARGET_EFAULT;
13227 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013228 return ret;
Richard Henderson3d21d292012-09-15 13:20:46 -070013229 }
13230#endif
Riku Voipio89aaf1a2013-07-24 09:44:26 +030013231#ifdef TARGET_NR_atomic_cmpxchg_32
13232 case TARGET_NR_atomic_cmpxchg_32:
13233 {
13234 /* should use start_exclusive from main.c */
13235 abi_ulong mem_value;
13236 if (get_user_u32(mem_value, arg6)) {
13237 target_siginfo_t info;
13238 info.si_signo = SIGSEGV;
13239 info.si_errno = 0;
13240 info.si_code = TARGET_SEGV_MAPERR;
13241 info._sifields._sigfault._addr = arg6;
Philippe Mathieu-Daudé0effdc22022-05-09 22:57:28 +020013242 queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
Riku Voipio89aaf1a2013-07-24 09:44:26 +030013243 ret = 0xdeadbeef;
13244
13245 }
13246 if (mem_value == arg2)
13247 put_user_u32(arg1, arg6);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013248 return mem_value;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030013249 }
13250#endif
13251#ifdef TARGET_NR_atomic_barrier
13252 case TARGET_NR_atomic_barrier:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013253 /* Like the kernel implementation and the
13254 qemu arm barrier, no-op this? */
13255 return 0;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030013256#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013257
13258#ifdef TARGET_NR_timer_create
13259 case TARGET_NR_timer_create:
13260 {
13261 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
13262
13263 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013264
13265 int clkid = arg1;
13266 int timer_index = next_free_host_timer();
13267
13268 if (timer_index < 0) {
13269 ret = -TARGET_EAGAIN;
13270 } else {
13271 timer_t *phtimer = g_posix_timers + timer_index;
13272
13273 if (arg2) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013274 phost_sevp = &host_sevp;
Peter Maydellc0659762014-08-09 15:42:32 +010013275 ret = target_to_host_sigevent(phost_sevp, arg2);
13276 if (ret != 0) {
Peter Maydell9e598992022-07-25 12:00:35 +010013277 free_host_timer_slot(timer_index);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013278 return ret;
Peter Maydellc0659762014-08-09 15:42:32 +010013279 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013280 }
13281
13282 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
13283 if (ret) {
Peter Maydell9e598992022-07-25 12:00:35 +010013284 free_host_timer_slot(timer_index);
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013285 } else {
Alexander Grafaecc8862014-11-10 21:33:03 +010013286 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
Peter Maydell9e598992022-07-25 12:00:35 +010013287 timer_delete(*phtimer);
13288 free_host_timer_slot(timer_index);
Richard Henderson2852aaf2018-08-18 12:01:06 -070013289 return -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013290 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013291 }
13292 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013293 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013294 }
13295#endif
13296
13297#ifdef TARGET_NR_timer_settime
13298 case TARGET_NR_timer_settime:
13299 {
13300 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
13301 * struct itimerspec * old_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010013302 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020013303
Alexander Grafaecc8862014-11-10 21:33:03 +010013304 if (timerid < 0) {
13305 ret = timerid;
13306 } else if (arg3 == 0) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013307 ret = -TARGET_EINVAL;
13308 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020013309 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013310 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13311
Marc-André Lureau40c80b52017-01-19 10:15:32 -050013312 if (target_to_host_itimerspec(&hspec_new, arg3)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013313 return -TARGET_EFAULT;
Marc-André Lureau40c80b52017-01-19 10:15:32 -050013314 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013315 ret = get_errno(
13316 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
Marc-André Lureau40c80b52017-01-19 10:15:32 -050013317 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013318 return -TARGET_EFAULT;
Marc-André Lureau40c80b52017-01-19 10:15:32 -050013319 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013320 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013321 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013322 }
13323#endif
13324
Filip Bozuta828cb3a2020-07-22 17:34:21 +020013325#ifdef TARGET_NR_timer_settime64
13326 case TARGET_NR_timer_settime64:
13327 {
13328 target_timer_t timerid = get_timer_id(arg1);
13329
13330 if (timerid < 0) {
13331 ret = timerid;
13332 } else if (arg3 == 0) {
13333 ret = -TARGET_EINVAL;
13334 } else {
13335 timer_t htimer = g_posix_timers[timerid];
13336 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13337
13338 if (target_to_host_itimerspec64(&hspec_new, arg3)) {
13339 return -TARGET_EFAULT;
13340 }
13341 ret = get_errno(
13342 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
13343 if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
13344 return -TARGET_EFAULT;
13345 }
13346 }
13347 return ret;
13348 }
13349#endif
13350
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013351#ifdef TARGET_NR_timer_gettime
13352 case TARGET_NR_timer_gettime:
13353 {
13354 /* args: timer_t timerid, struct itimerspec *curr_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010013355 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020013356
Alexander Grafaecc8862014-11-10 21:33:03 +010013357 if (timerid < 0) {
13358 ret = timerid;
13359 } else if (!arg2) {
13360 ret = -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013361 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020013362 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013363 struct itimerspec hspec;
13364 ret = get_errno(timer_gettime(htimer, &hspec));
13365
13366 if (host_to_target_itimerspec(arg2, &hspec)) {
13367 ret = -TARGET_EFAULT;
13368 }
13369 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013370 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013371 }
13372#endif
13373
Filip Bozuta828cb3a2020-07-22 17:34:21 +020013374#ifdef TARGET_NR_timer_gettime64
13375 case TARGET_NR_timer_gettime64:
13376 {
13377 /* args: timer_t timerid, struct itimerspec64 *curr_value */
13378 target_timer_t timerid = get_timer_id(arg1);
13379
13380 if (timerid < 0) {
13381 ret = timerid;
13382 } else if (!arg2) {
13383 ret = -TARGET_EFAULT;
13384 } else {
13385 timer_t htimer = g_posix_timers[timerid];
13386 struct itimerspec hspec;
13387 ret = get_errno(timer_gettime(htimer, &hspec));
13388
13389 if (host_to_target_itimerspec64(arg2, &hspec)) {
13390 ret = -TARGET_EFAULT;
13391 }
13392 }
13393 return ret;
13394 }
13395#endif
13396
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013397#ifdef TARGET_NR_timer_getoverrun
13398 case TARGET_NR_timer_getoverrun:
13399 {
13400 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010013401 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020013402
Alexander Grafaecc8862014-11-10 21:33:03 +010013403 if (timerid < 0) {
13404 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013405 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020013406 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013407 ret = get_errno(timer_getoverrun(htimer));
13408 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013409 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013410 }
13411#endif
13412
13413#ifdef TARGET_NR_timer_delete
13414 case TARGET_NR_timer_delete:
13415 {
13416 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010013417 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020013418
Alexander Grafaecc8862014-11-10 21:33:03 +010013419 if (timerid < 0) {
13420 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013421 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020013422 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013423 ret = get_errno(timer_delete(htimer));
Peter Maydell9e598992022-07-25 12:00:35 +010013424 free_host_timer_slot(timerid);
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013425 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013426 return ret;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110013427 }
13428#endif
13429
Riku Voipio51834342014-06-22 11:25:42 +010013430#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13431 case TARGET_NR_timerfd_create:
Mathis Mariond759a622023-02-20 09:58:19 +010013432 ret = get_errno(timerfd_create(arg1,
13433 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
13434 if (ret >= 0) {
13435 fd_trans_register(ret, &target_timerfd_trans);
13436 }
13437 return ret;
Riku Voipio51834342014-06-22 11:25:42 +010013438#endif
13439
13440#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13441 case TARGET_NR_timerfd_gettime:
13442 {
13443 struct itimerspec its_curr;
13444
13445 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13446
13447 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013448 return -TARGET_EFAULT;
Riku Voipio51834342014-06-22 11:25:42 +010013449 }
13450 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013451 return ret;
Riku Voipio51834342014-06-22 11:25:42 +010013452#endif
13453
Filip Bozuta828cb3a2020-07-22 17:34:21 +020013454#if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13455 case TARGET_NR_timerfd_gettime64:
13456 {
13457 struct itimerspec its_curr;
13458
13459 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13460
13461 if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
13462 return -TARGET_EFAULT;
13463 }
13464 }
13465 return ret;
13466#endif
13467
Riku Voipio51834342014-06-22 11:25:42 +010013468#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13469 case TARGET_NR_timerfd_settime:
13470 {
13471 struct itimerspec its_new, its_old, *p_new;
13472
13473 if (arg3) {
13474 if (target_to_host_itimerspec(&its_new, arg3)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013475 return -TARGET_EFAULT;
Riku Voipio51834342014-06-22 11:25:42 +010013476 }
13477 p_new = &its_new;
13478 } else {
13479 p_new = NULL;
13480 }
13481
13482 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13483
13484 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
Richard Henderson2852aaf2018-08-18 12:01:06 -070013485 return -TARGET_EFAULT;
Riku Voipio51834342014-06-22 11:25:42 +010013486 }
13487 }
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013488 return ret;
Riku Voipio51834342014-06-22 11:25:42 +010013489#endif
13490
Filip Bozuta828cb3a2020-07-22 17:34:21 +020013491#if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13492 case TARGET_NR_timerfd_settime64:
13493 {
13494 struct itimerspec its_new, its_old, *p_new;
13495
13496 if (arg3) {
13497 if (target_to_host_itimerspec64(&its_new, arg3)) {
13498 return -TARGET_EFAULT;
13499 }
13500 p_new = &its_new;
13501 } else {
13502 p_new = NULL;
13503 }
13504
13505 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13506
13507 if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
13508 return -TARGET_EFAULT;
13509 }
13510 }
13511 return ret;
13512#endif
13513
Paul Burtonab31cda2014-06-22 11:25:43 +010013514#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13515 case TARGET_NR_ioprio_get:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013516 return get_errno(ioprio_get(arg1, arg2));
Paul Burtonab31cda2014-06-22 11:25:43 +010013517#endif
13518
13519#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13520 case TARGET_NR_ioprio_set:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013521 return get_errno(ioprio_set(arg1, arg2, arg3));
Paul Burtonab31cda2014-06-22 11:25:43 +010013522#endif
13523
Riku Voipio9af5c902014-08-12 15:58:57 +030013524#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13525 case TARGET_NR_setns:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013526 return get_errno(setns(arg1, arg2));
Riku Voipio9af5c902014-08-12 15:58:57 +030013527#endif
13528#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13529 case TARGET_NR_unshare:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013530 return get_errno(unshare(arg1));
Riku Voipio9af5c902014-08-12 15:58:57 +030013531#endif
Laurent Vivier2f147882016-09-25 22:20:20 +020013532#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13533 case TARGET_NR_kcmp:
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013534 return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
Laurent Vivier2f147882016-09-25 22:20:20 +020013535#endif
Richard Hendersonfa97e382018-07-18 13:06:48 -070013536#ifdef TARGET_NR_swapcontext
13537 case TARGET_NR_swapcontext:
13538 /* PowerPC specific. */
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013539 return do_swapcontext(cpu_env, arg1, arg2, arg3);
Richard Hendersonfa97e382018-07-18 13:06:48 -070013540#endif
Shu-Chun Weng9bdfa4d2019-08-19 11:09:47 -070013541#ifdef TARGET_NR_memfd_create
13542 case TARGET_NR_memfd_create:
13543 p = lock_user_string(arg1);
13544 if (!p) {
13545 return -TARGET_EFAULT;
13546 }
13547 ret = get_errno(memfd_create(p, arg2));
13548 fd_trans_unregister(ret);
13549 unlock_user(p, arg1, 0);
13550 return ret;
13551#endif
Andreas Schwab85004762019-05-13 11:02:53 +020013552#if defined TARGET_NR_membarrier && defined __NR_membarrier
13553 case TARGET_NR_membarrier:
13554 return get_errno(membarrier(arg1, arg2));
13555#endif
Riku Voipio9af5c902014-08-12 15:58:57 +030013556
Andreas Schwab84946452020-11-12 12:45:16 +010013557#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13558 case TARGET_NR_copy_file_range:
13559 {
13560 loff_t inoff, outoff;
13561 loff_t *pinoff = NULL, *poutoff = NULL;
13562
13563 if (arg2) {
13564 if (get_user_u64(inoff, arg2)) {
13565 return -TARGET_EFAULT;
13566 }
13567 pinoff = &inoff;
13568 }
13569 if (arg4) {
13570 if (get_user_u64(outoff, arg4)) {
13571 return -TARGET_EFAULT;
13572 }
13573 poutoff = &outoff;
13574 }
Giuseppe Musacchio0fa259d2021-05-03 19:41:59 +020013575 /* Do not sign-extend the count parameter. */
Andreas Schwab84946452020-11-12 12:45:16 +010013576 ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
Giuseppe Musacchio0fa259d2021-05-03 19:41:59 +020013577 (abi_ulong)arg5, arg6));
Andreas Schwab84946452020-11-12 12:45:16 +010013578 if (!is_error(ret) && ret > 0) {
13579 if (arg2) {
13580 if (put_user_u64(inoff, arg2)) {
13581 return -TARGET_EFAULT;
13582 }
13583 }
13584 if (arg4) {
13585 if (put_user_u64(outoff, arg4)) {
13586 return -TARGET_EFAULT;
13587 }
13588 }
13589 }
13590 }
13591 return ret;
13592#endif
13593
YAMAMOTO Takashie10fbe82021-05-31 14:50:12 +090013594#if defined(TARGET_NR_pivot_root)
13595 case TARGET_NR_pivot_root:
13596 {
13597 void *p2;
13598 p = lock_user_string(arg1); /* new_root */
13599 p2 = lock_user_string(arg2); /* put_old */
13600 if (!p || !p2) {
13601 ret = -TARGET_EFAULT;
13602 } else {
13603 ret = get_errno(pivot_root(p, p2));
13604 }
13605 unlock_user(p2, arg2, 0);
13606 unlock_user(p, arg1, 0);
13607 }
13608 return ret;
13609#endif
13610
Robbin Ehn9e1c7d92023-06-19 10:24:03 +020013611#if defined(TARGET_NR_riscv_hwprobe)
13612 case TARGET_NR_riscv_hwprobe:
13613 return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
13614#endif
13615
bellard31e31b82003-02-18 22:55:36 +000013616 default:
Philippe Mathieu-Daudé122f9c82018-07-06 12:51:25 -030013617 qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
Richard Henderson72eb7ea2018-08-18 12:01:05 -070013618 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +000013619 }
bellard31e31b82003-02-18 22:55:36 +000013620 return ret;
13621}
Richard Hendersondc1ce182018-08-18 12:01:04 -070013622
Philippe Mathieu-Daudéa0939b82022-05-09 22:57:27 +020013623abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
Richard Hendersondc1ce182018-08-18 12:01:04 -070013624 abi_long arg2, abi_long arg3, abi_long arg4,
13625 abi_long arg5, abi_long arg6, abi_long arg7,
13626 abi_long arg8)
13627{
Richard Henderson29a0af62019-03-22 16:07:18 -070013628 CPUState *cpu = env_cpu(cpu_env);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013629 abi_long ret;
13630
13631#ifdef DEBUG_ERESTARTSYS
13632 /* Debug-only code for exercising the syscall-restart code paths
13633 * in the per-architecture cpu main loops: restart every syscall
13634 * the guest makes once before letting it through.
13635 */
13636 {
13637 static bool flag;
13638 flag = !flag;
13639 if (flag) {
Richard Hendersonaf254a22021-11-22 19:47:33 +010013640 return -QEMU_ERESTARTSYS;
Richard Hendersondc1ce182018-08-18 12:01:04 -070013641 }
13642 }
13643#endif
13644
Emilio G. Cotac36f7a62018-10-21 13:27:44 -040013645 record_syscall_start(cpu, num, arg1,
13646 arg2, arg3, arg4, arg5, arg6, arg7, arg8);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013647
Josh Kunz4b25a502020-02-03 18:54:14 -080013648 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
Filip Bozutae400e112020-08-11 18:45:49 +020013649 print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
Josh Kunz4b25a502020-02-03 18:54:14 -080013650 }
13651
13652 ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
13653 arg5, arg6, arg7, arg8);
13654
13655 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
Filip Bozutae400e112020-08-11 18:45:49 +020013656 print_syscall_ret(cpu_env, num, ret, arg1, arg2,
13657 arg3, arg4, arg5, arg6);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013658 }
13659
Emilio G. Cotac36f7a62018-10-21 13:27:44 -040013660 record_syscall_return(cpu, num, ret);
Richard Hendersondc1ce182018-08-18 12:01:04 -070013661 return ret;
13662}