bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Linux syscalls |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4 | * 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 Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 18 | */ |
Eduardo Habkost | d5b3a9b | 2009-06-09 18:26:31 -0300 | [diff] [blame] | 19 | #define _ATFILE_SOURCE |
Peter Maydell | d39594e | 2016-01-26 18:17:02 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 21 | #include "qemu/cutils.h" |
| 22 | #include "qemu/path.h" |
Shu-Chun Weng | 9bdfa4d | 2019-08-19 11:09:47 -0700 | [diff] [blame] | 23 | #include "qemu/memfd.h" |
Markus Armbruster | dc5e9ac | 2019-08-12 07:23:49 +0200 | [diff] [blame] | 24 | #include "qemu/queue.h" |
Richard Henderson | 720ace2 | 2023-03-15 17:43:14 +0000 | [diff] [blame] | 25 | #include "qemu/plugin.h" |
Richard Henderson | d7ec12f | 2023-09-29 19:54:54 -0700 | [diff] [blame] | 26 | #include "tcg/startup.h" |
Helge Deller | fe08059 | 2022-12-15 08:27:46 +0100 | [diff] [blame] | 27 | #include "target_mman.h" |
Philippe Mathieu-Daudé | 74781c0 | 2023-12-06 20:27:32 +0100 | [diff] [blame] | 28 | #include "exec/page-protection.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 29 | #include <elf.h> |
| 30 | #include <endian.h> |
John Spencer | c56dc77 | 2012-12-10 07:59:46 +0100 | [diff] [blame] | 31 | #include <grp.h> |
ths | d08d3bb | 2007-03-19 13:09:22 +0000 | [diff] [blame] | 32 | #include <sys/ipc.h> |
| 33 | #include <sys/msg.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 34 | #include <sys/wait.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 35 | #include <sys/mount.h> |
John Spencer | 586b0be | 2012-12-26 00:49:49 +0100 | [diff] [blame] | 36 | #include <sys/file.h> |
| 37 | #include <sys/fsuid.h> |
| 38 | #include <sys/personality.h> |
ths | 39b9aae | 2007-02-11 18:36:44 +0000 | [diff] [blame] | 39 | #include <sys/prctl.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 40 | #include <sys/resource.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 41 | #include <sys/swap.h> |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 42 | #include <linux/capability.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 43 | #include <sched.h> |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 44 | #include <sys/timex.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 45 | #include <sys/socket.h> |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 46 | #include <linux/sockios.h> |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 47 | #include <sys/un.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 48 | #include <sys/uio.h> |
Felix Janda | 0839f11 | 2016-09-30 19:40:21 -0400 | [diff] [blame] | 49 | #include <poll.h> |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 50 | #include <sys/times.h> |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 51 | #include <sys/shm.h> |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 52 | #include <sys/sem.h> |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 53 | #include <sys/statfs.h> |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 54 | #include <utime.h> |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 55 | #include <sys/sysinfo.h> |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 56 | #include <sys/signalfd.h> |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 57 | //#include <sys/user.h> |
Shu-Chun Weng | 22db121 | 2020-12-18 11:32:12 -0800 | [diff] [blame] | 58 | #include <netinet/in.h> |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 59 | #include <netinet/ip.h> |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 60 | #include <netinet/tcp.h> |
Shu-Chun Weng | fe51b0a | 2020-12-18 11:32:11 -0800 | [diff] [blame] | 61 | #include <netinet/udp.h> |
Laurent Vivier | 86fcd94 | 2011-03-30 01:35:23 +0200 | [diff] [blame] | 62 | #include <linux/wireless.h> |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 63 | #include <linux/icmp.h> |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 64 | #include <linux/icmpv6.h> |
Shu-Chun Weng | 6addf06 | 2020-09-28 18:48:01 -0700 | [diff] [blame] | 65 | #include <linux/if_tun.h> |
Shu-Chun Weng | 22db121 | 2020-12-18 11:32:12 -0800 | [diff] [blame] | 66 | #include <linux/in6.h> |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 67 | #include <linux/errqueue.h> |
Marco A L Barbosa | d6d6d6f | 2017-10-05 10:55:30 -0300 | [diff] [blame] | 68 | #include <linux/random.h> |
Riku Voipio | d80a190 | 2014-10-01 16:05:46 +0300 | [diff] [blame] | 69 | #ifdef CONFIG_TIMERFD |
| 70 | #include <sys/timerfd.h> |
| 71 | #endif |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 72 | #ifdef CONFIG_EVENTFD |
| 73 | #include <sys/eventfd.h> |
| 74 | #endif |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_EPOLL |
| 76 | #include <sys/epoll.h> |
| 77 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_ATTR |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 79 | #include "qemu/xattr.h" |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 80 | #endif |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 81 | #ifdef CONFIG_SENDFILE |
| 82 | #include <sys/sendfile.h> |
| 83 | #endif |
Thomas Huth | 4a9d5f8 | 2020-11-18 18:10:51 +0100 | [diff] [blame] | 84 | #ifdef HAVE_SYS_KCOV_H |
Aleksandar Markovic | bd27e67 | 2020-01-16 23:49:50 +0100 | [diff] [blame] | 85 | #include <sys/kcov.h> |
| 86 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 87 | |
| 88 | #define termios host_termios |
| 89 | #define winsize host_winsize |
| 90 | #define termio host_termio |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 91 | #define sgttyb host_sgttyb /* same as target */ |
| 92 | #define tchars host_tchars /* same as target */ |
| 93 | #define ltchars host_ltchars /* same as target */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 94 | |
| 95 | #include <linux/termios.h> |
| 96 | #include <linux/unistd.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 97 | #include <linux/cdrom.h> |
| 98 | #include <linux/hdreg.h> |
| 99 | #include <linux/soundcard.h> |
bellard | 19b84f3 | 2003-05-08 15:41:49 +0000 | [diff] [blame] | 100 | #include <linux/kd.h> |
balrog | 8fbd6b5 | 2008-09-20 03:03:09 +0000 | [diff] [blame] | 101 | #include <linux/mtio.h> |
Martin Mohring | 350d177 | 2009-05-04 21:21:41 +0300 | [diff] [blame] | 102 | #include <linux/fs.h> |
Yunqiang Su | ab22b4d | 2019-09-04 14:59:24 +0200 | [diff] [blame] | 103 | #include <linux/fd.h> |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 104 | #if defined(CONFIG_FIEMAP) |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 105 | #include <linux/fiemap.h> |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 106 | #endif |
Ulrich Hecht | f7680a5 | 2009-10-16 17:00:44 +0200 | [diff] [blame] | 107 | #include <linux/fb.h> |
Cortland Tölva | 6c753a6 | 2018-10-08 09:35:20 -0700 | [diff] [blame] | 108 | #if defined(CONFIG_USBFS) |
| 109 | #include <linux/usbdevice_fs.h> |
Cortland Tölva | a133367 | 2018-10-08 09:35:21 -0700 | [diff] [blame] | 110 | #include <linux/usb/ch9.h> |
Cortland Tölva | 6c753a6 | 2018-10-08 09:35:20 -0700 | [diff] [blame] | 111 | #endif |
Ulrich Hecht | f7680a5 | 2009-10-16 17:00:44 +0200 | [diff] [blame] | 112 | #include <linux/vt.h> |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 113 | #include <linux/dm-ioctl.h> |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 114 | #include <linux/reboot.h> |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 115 | #include <linux/route.h> |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 116 | #include <linux/filter.h> |
Andreas Färber | fff8c53 | 2014-01-18 07:38:30 +0100 | [diff] [blame] | 117 | #include <linux/blkpg.h> |
Laurent Vivier | a82ea93 | 2016-06-27 00:18:22 +0200 | [diff] [blame] | 118 | #include <netpacket/packet.h> |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 119 | #include <linux/netlink.h> |
Yunqiang Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 120 | #include <linux/if_alg.h> |
Filip Bozuta | 68365f9 | 2020-01-15 20:36:35 +0100 | [diff] [blame] | 121 | #include <linux/rtc.h> |
Filip Bozuta | 1c4c6fc | 2020-01-15 20:36:41 +0100 | [diff] [blame] | 122 | #include <sound/asound.h> |
Thomas Huth | 48f670e | 2020-11-18 18:10:52 +0100 | [diff] [blame] | 123 | #ifdef HAVE_BTRFS_H |
Filip Bozuta | d6092e0 | 2020-08-23 21:50:07 +0200 | [diff] [blame] | 124 | #include <linux/btrfs.h> |
| 125 | #endif |
Chen Gang | e865b97 | 2020-06-05 09:32:21 +0800 | [diff] [blame] | 126 | #ifdef HAVE_DRM_H |
| 127 | #include <libdrm/drm.h> |
Chen Gang | 913b03c | 2020-08-02 21:39:38 +0800 | [diff] [blame] | 128 | #include <libdrm/i915_drm.h> |
Chen Gang | e865b97 | 2020-06-05 09:32:21 +0800 | [diff] [blame] | 129 | #endif |
pbrook | d7e4036 | 2008-05-23 16:06:43 +0000 | [diff] [blame] | 130 | #include "linux_loop.h" |
Riku Voipio | 18cb008 | 2014-02-19 12:59:58 +0200 | [diff] [blame] | 131 | #include "uname.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 132 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 133 | #include "qemu.h" |
Peter Maydell | 3b249d2 | 2021-09-08 16:44:03 +0100 | [diff] [blame] | 134 | #include "user-internals.h" |
Peter Maydell | a44d57a | 2021-09-08 16:43:58 +0100 | [diff] [blame] | 135 | #include "strace.h" |
Peter Maydell | 2113aed | 2021-09-08 16:43:59 +0100 | [diff] [blame] | 136 | #include "signal-common.h" |
Peter Maydell | 3ad0a76 | 2021-09-08 16:44:00 +0100 | [diff] [blame] | 137 | #include "loader.h" |
Peter Maydell | 5423e6d | 2021-09-08 16:44:01 +0100 | [diff] [blame] | 138 | #include "user-mmap.h" |
Richard Henderson | bbf15aa | 2021-11-17 16:14:00 +0100 | [diff] [blame] | 139 | #include "user/safe-syscall.h" |
Richard Henderson | 5ebdd77 | 2019-03-14 13:10:53 -0700 | [diff] [blame] | 140 | #include "qemu/guest-random.h" |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 141 | #include "qemu/selfmap.h" |
Emilio G. Cota | c36f7a6 | 2018-10-21 13:27:44 -0400 | [diff] [blame] | 142 | #include "user/syscall-trace.h" |
Richard Henderson | 5da4063 | 2021-11-17 15:46:05 +0100 | [diff] [blame] | 143 | #include "special-errno.h" |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 144 | #include "qapi/error.h" |
Laurent Vivier | f7e6a40 | 2018-08-24 00:22:15 +0200 | [diff] [blame] | 145 | #include "fd-trans.h" |
Helge Deller | bd5ccd6 | 2022-10-24 22:18:09 +0200 | [diff] [blame] | 146 | #include "cpu_loop-common.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 147 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 148 | #ifndef CLONE_IO |
| 149 | #define CLONE_IO 0x80000000 /* Clone io context */ |
| 150 | #endif |
| 151 | |
| 152 | /* We can't directly call the host clone syscall, because this will |
| 153 | * badly confuse libc (breaking mutexes, for example). So we must |
| 154 | * divide clone flags into: |
| 155 | * * flag combinations that look like pthread_create() |
| 156 | * * flag combinations that look like fork() |
| 157 | * * flags we can implement within QEMU itself |
| 158 | * * flags we can't support and will return an error for |
| 159 | */ |
| 160 | /* For thread creation, all these flags must be present; for |
| 161 | * fork, none must be present. |
| 162 | */ |
| 163 | #define CLONE_THREAD_FLAGS \ |
| 164 | (CLONE_VM | CLONE_FS | CLONE_FILES | \ |
| 165 | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM) |
| 166 | |
| 167 | /* These flags are ignored: |
| 168 | * CLONE_DETACHED is now ignored by the kernel; |
| 169 | * CLONE_IO is just an optimisation hint to the I/O scheduler |
| 170 | */ |
| 171 | #define CLONE_IGNORED_FLAGS \ |
| 172 | (CLONE_DETACHED | CLONE_IO) |
| 173 | |
Helge Deller | 895ce8b | 2022-11-29 12:08:20 +0100 | [diff] [blame] | 174 | #ifndef CLONE_PIDFD |
| 175 | # define CLONE_PIDFD 0x00001000 |
| 176 | #endif |
| 177 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 178 | /* Flags for fork which we can implement within QEMU itself */ |
| 179 | #define CLONE_OPTIONAL_FORK_FLAGS \ |
Helge Deller | 895ce8b | 2022-11-29 12:08:20 +0100 | [diff] [blame] | 180 | (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \ |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 181 | CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID) |
| 182 | |
| 183 | /* Flags for thread creation which we can implement within QEMU itself */ |
| 184 | #define CLONE_OPTIONAL_THREAD_FLAGS \ |
| 185 | (CLONE_SETTLS | CLONE_PARENT_SETTID | \ |
| 186 | CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT) |
| 187 | |
| 188 | #define CLONE_INVALID_FORK_FLAGS \ |
| 189 | (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS)) |
| 190 | |
| 191 | #define CLONE_INVALID_THREAD_FLAGS \ |
| 192 | (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \ |
| 193 | CLONE_IGNORED_FLAGS)) |
| 194 | |
| 195 | /* CLONE_VFORK is special cased early in do_fork(). The other flag bits |
| 196 | * have almost all been allocated. We cannot support any of |
| 197 | * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC, |
| 198 | * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED. |
| 199 | * The checks against the invalid thread masks above will catch these. |
| 200 | * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.) |
| 201 | */ |
pbrook | 30813ce | 2008-06-02 15:45:44 +0000 | [diff] [blame] | 202 | |
Timothy E Baldwin | 71a8f7f | 2016-05-12 18:47:45 +0100 | [diff] [blame] | 203 | /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted |
| 204 | * once. This exercises the codepaths for restart. |
| 205 | */ |
| 206 | //#define DEBUG_ERESTARTSYS |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 207 | |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 208 | //#include <linux/msdos_fs.h> |
Richard Henderson | 540a736 | 2021-11-14 11:35:37 +0100 | [diff] [blame] | 209 | #define VFAT_IOCTL_READDIR_BOTH \ |
| 210 | _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2) |
| 211 | #define VFAT_IOCTL_READDIR_SHORT \ |
| 212 | _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2) |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 213 | |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 214 | #undef _syscall0 |
| 215 | #undef _syscall1 |
| 216 | #undef _syscall2 |
| 217 | #undef _syscall3 |
| 218 | #undef _syscall4 |
| 219 | #undef _syscall5 |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 220 | #undef _syscall6 |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 221 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 222 | #define _syscall0(type,name) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 223 | static type name (void) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 224 | { \ |
| 225 | return syscall(__NR_##name); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 226 | } |
| 227 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 228 | #define _syscall1(type,name,type1,arg1) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 229 | static type name (type1 arg1) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 230 | { \ |
| 231 | return syscall(__NR_##name, arg1); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 232 | } |
| 233 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 234 | #define _syscall2(type,name,type1,arg1,type2,arg2) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 235 | static type name (type1 arg1,type2 arg2) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 236 | { \ |
| 237 | return syscall(__NR_##name, arg1, arg2); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 238 | } |
| 239 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 240 | #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 241 | static type name (type1 arg1,type2 arg2,type3 arg3) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 242 | { \ |
| 243 | return syscall(__NR_##name, arg1, arg2, arg3); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 244 | } |
| 245 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 246 | #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 247 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 248 | { \ |
| 249 | return syscall(__NR_##name, arg1, arg2, arg3, arg4); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 250 | } |
| 251 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 252 | #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
| 253 | type5,arg5) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 254 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 255 | { \ |
| 256 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 257 | } |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
| 261 | type5,arg5,type6,arg6) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 262 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \ |
| 263 | type6 arg6) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 264 | { \ |
| 265 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ |
| 266 | } |
| 267 | |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 268 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 269 | #define __NR_sys_uname __NR_uname |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 270 | #define __NR_sys_getcwd1 __NR_getcwd |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 271 | #define __NR_sys_getdents __NR_getdents |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 272 | #define __NR_sys_getdents64 __NR_getdents64 |
ths | c6cda17 | 2007-10-09 03:42:34 +0000 | [diff] [blame] | 273 | #define __NR_sys_getpriority __NR_getpriority |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 274 | #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 275 | #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 276 | #define __NR_sys_syslog __NR_syslog |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 277 | #if defined(__NR_futex) |
| 278 | # define __NR_sys_futex __NR_futex |
| 279 | #endif |
| 280 | #if defined(__NR_futex_time64) |
| 281 | # define __NR_sys_futex_time64 __NR_futex_time64 |
| 282 | #endif |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 283 | #define __NR_sys_statx __NR_statx |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 284 | |
Peter Maydell | b1cef6d | 2018-01-25 16:19:49 +0000 | [diff] [blame] | 285 | #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__) |
bellard | 9af9eaa | 2003-04-07 21:34:41 +0000 | [diff] [blame] | 286 | #define __NR__llseek __NR_lseek |
| 287 | #endif |
| 288 | |
James Hogan | a29e5ba | 2014-03-25 21:51:08 +0000 | [diff] [blame] | 289 | /* Newer kernel ports have llseek() instead of _llseek() */ |
| 290 | #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek) |
| 291 | #define TARGET_NR__llseek TARGET_NR_llseek |
| 292 | #endif |
| 293 | |
Helge Deller | 7872130 | 2021-02-10 07:12:14 +0100 | [diff] [blame] | 294 | /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */ |
| 295 | #ifndef TARGET_O_NONBLOCK_MASK |
| 296 | #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK |
| 297 | #endif |
| 298 | |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 299 | #define __NR_sys_gettid __NR_gettid |
| 300 | _syscall0(int, sys_gettid) |
Peter Maydell | 2b3f64c | 2018-04-19 13:57:40 +0100 | [diff] [blame] | 301 | |
| 302 | /* For the 64-bit guest on 32-bit host case we must emulate |
| 303 | * getdents using getdents64, because otherwise the host |
| 304 | * might hand us back more dirent records than we can fit |
| 305 | * into the guest buffer after structure format conversion. |
| 306 | * Otherwise we emulate getdents with getdents if the host has it. |
| 307 | */ |
| 308 | #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS |
| 309 | #define EMULATE_GETDENTS_WITH_GETDENTS |
| 310 | #endif |
| 311 | |
| 312 | #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS) |
Juan Quintela | ac42f44 | 2023-07-09 18:34:30 +0100 | [diff] [blame] | 313 | _syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count); |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 314 | #endif |
Peter Maydell | 2b3f64c | 2018-04-19 13:57:40 +0100 | [diff] [blame] | 315 | #if (defined(TARGET_NR_getdents) && \ |
| 316 | !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \ |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 317 | (defined(TARGET_NR_getdents64) && defined(__NR_getdents64)) |
Juan Quintela | ac42f44 | 2023-07-09 18:34:30 +0100 | [diff] [blame] | 318 | _syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count); |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 319 | #endif |
Richard Henderson | d35b261 | 2010-06-04 12:14:10 -0700 | [diff] [blame] | 320 | #if defined(TARGET_NR__llseek) && defined(__NR_llseek) |
Juan Quintela | ac42f44 | 2023-07-09 18:34:30 +0100 | [diff] [blame] | 321 | _syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo, |
| 322 | loff_t *, res, unsigned int, wh); |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 323 | #endif |
Miloš Stojanović | c1a402a | 2017-05-15 16:59:45 +0200 | [diff] [blame] | 324 | _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo) |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 325 | _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig, |
| 326 | siginfo_t *, uinfo) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 327 | _syscall3(int,sys_syslog,int,type,char*,bufp,int,len) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 328 | #ifdef __NR_exit_group |
| 329 | _syscall1(int,exit_group,int,error_code) |
| 330 | #endif |
Helge Deller | af804f3 | 2022-10-25 04:34:14 +0200 | [diff] [blame] | 331 | #if defined(__NR_close_range) && defined(TARGET_NR_close_range) |
| 332 | #define __NR_sys_close_range __NR_close_range |
| 333 | _syscall3(int,sys_close_range,int,first,int,last,int,flags) |
| 334 | #ifndef CLOSE_RANGE_CLOEXEC |
| 335 | #define CLOSE_RANGE_CLOEXEC (1U << 2) |
| 336 | #endif |
| 337 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 338 | #if defined(__NR_futex) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 339 | _syscall6(int,sys_futex,int *,uaddr,int,op,int,val, |
| 340 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 341 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 342 | #if defined(__NR_futex_time64) |
| 343 | _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val, |
| 344 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 345 | #endif |
Helge Deller | cc054c6 | 2022-09-18 21:45:46 +0200 | [diff] [blame] | 346 | #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open) |
| 347 | _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags); |
| 348 | #endif |
| 349 | #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal) |
| 350 | _syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info, |
| 351 | unsigned int, flags); |
| 352 | #endif |
| 353 | #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd) |
| 354 | _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags); |
| 355 | #endif |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 356 | #define __NR_sys_sched_getaffinity __NR_sched_getaffinity |
| 357 | _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len, |
| 358 | unsigned long *, user_mask_ptr); |
| 359 | #define __NR_sys_sched_setaffinity __NR_sched_setaffinity |
| 360 | _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len, |
| 361 | unsigned long *, user_mask_ptr); |
Tonis Tiigi | 45ad761 | 2022-01-04 20:18:18 -0800 | [diff] [blame] | 362 | /* sched_attr is not defined in glibc */ |
| 363 | struct sched_attr { |
| 364 | uint32_t size; |
| 365 | uint32_t sched_policy; |
| 366 | uint64_t sched_flags; |
| 367 | int32_t sched_nice; |
| 368 | uint32_t sched_priority; |
| 369 | uint64_t sched_runtime; |
| 370 | uint64_t sched_deadline; |
| 371 | uint64_t sched_period; |
| 372 | uint32_t sched_util_min; |
| 373 | uint32_t sched_util_max; |
| 374 | }; |
| 375 | #define __NR_sys_sched_getattr __NR_sched_getattr |
| 376 | _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr, |
| 377 | unsigned int, size, unsigned int, flags); |
| 378 | #define __NR_sys_sched_setattr __NR_sched_setattr |
| 379 | _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr, |
| 380 | unsigned int, flags); |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 381 | #define __NR_sys_sched_getscheduler __NR_sched_getscheduler |
| 382 | _syscall1(int, sys_sched_getscheduler, pid_t, pid); |
| 383 | #define __NR_sys_sched_setscheduler __NR_sched_setscheduler |
| 384 | _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy, |
| 385 | const struct sched_param *, param); |
| 386 | #define __NR_sys_sched_getparam __NR_sched_getparam |
| 387 | _syscall2(int, sys_sched_getparam, pid_t, pid, |
| 388 | struct sched_param *, param); |
| 389 | #define __NR_sys_sched_setparam __NR_sched_setparam |
| 390 | _syscall2(int, sys_sched_setparam, pid_t, pid, |
| 391 | const struct sched_param *, param); |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 392 | #define __NR_sys_getcpu __NR_getcpu |
| 393 | _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache); |
Alexander Graf | 0f6b4d2 | 2011-09-27 14:39:42 +0200 | [diff] [blame] | 394 | _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, |
| 395 | void *, arg); |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 396 | _syscall2(int, capget, struct __user_cap_header_struct *, header, |
| 397 | struct __user_cap_data_struct *, data); |
| 398 | _syscall2(int, capset, struct __user_cap_header_struct *, header, |
| 399 | struct __user_cap_data_struct *, data); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 400 | #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) |
| 401 | _syscall2(int, ioprio_get, int, which, int, who) |
| 402 | #endif |
| 403 | #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) |
| 404 | _syscall3(int, ioprio_set, int, which, int, who, int, ioprio) |
| 405 | #endif |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 406 | #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) |
| 407 | _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags) |
| 408 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 409 | |
Laurent Vivier | 2f14788 | 2016-09-25 22:20:20 +0200 | [diff] [blame] | 410 | #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp) |
| 411 | _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type, |
| 412 | unsigned long, idx1, unsigned long, idx2) |
| 413 | #endif |
| 414 | |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 415 | /* |
| 416 | * It is assumed that struct statx is architecture independent. |
| 417 | */ |
| 418 | #if defined(TARGET_NR_statx) && defined(__NR_statx) |
| 419 | _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags, |
| 420 | unsigned int, mask, struct target_statx *, statxbuf) |
| 421 | #endif |
Andreas Schwab | 8500476 | 2019-05-13 11:02:53 +0200 | [diff] [blame] | 422 | #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier) |
| 423 | _syscall2(int, membarrier, int, cmd, int, flags) |
| 424 | #endif |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 425 | |
Philippe Mathieu-Daudé | 180d4ef | 2021-05-17 07:52:43 +0200 | [diff] [blame] | 426 | static const bitmask_transtbl fcntl_flags_tbl[] = { |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 427 | { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, }, |
| 428 | { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, }, |
| 429 | { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, }, |
| 430 | { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, }, |
| 431 | { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, }, |
| 432 | { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, }, |
| 433 | { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, }, |
| 434 | { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, }, |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 435 | { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, }, |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 436 | { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, }, |
| 437 | { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, }, |
| 438 | { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, }, |
| 439 | { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, }, |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 440 | #if defined(O_DIRECT) |
| 441 | { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, }, |
| 442 | #endif |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 443 | #if defined(O_NOATIME) |
| 444 | { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME }, |
| 445 | #endif |
| 446 | #if defined(O_CLOEXEC) |
| 447 | { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC }, |
| 448 | #endif |
| 449 | #if defined(O_PATH) |
| 450 | { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH }, |
| 451 | #endif |
Riku Voipio | 5f9cee4 | 2017-08-08 16:01:19 +0300 | [diff] [blame] | 452 | #if defined(O_TMPFILE) |
| 453 | { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE }, |
| 454 | #endif |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 455 | /* Don't terminate the list prematurely on 64-bit host+guest. */ |
| 456 | #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0 |
| 457 | { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, }, |
| 458 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 459 | }; |
| 460 | |
Andreas Schwab | 0f6bb19 | 2020-07-23 12:27:13 +0200 | [diff] [blame] | 461 | _syscall2(int, sys_getcwd1, char *, buf, size_t, size) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 462 | |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 463 | #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64) |
Peter Maydell | 700fa58 | 2016-07-18 11:47:55 +0100 | [diff] [blame] | 464 | #if defined(__NR_utimensat) |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 465 | #define __NR_sys_utimensat __NR_utimensat |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 466 | _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, |
| 467 | const struct timespec *,tsp,int,flags) |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 468 | #else |
| 469 | static int sys_utimensat(int dirfd, const char *pathname, |
| 470 | const struct timespec times[2], int flags) |
| 471 | { |
| 472 | errno = ENOSYS; |
| 473 | return -1; |
| 474 | } |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 475 | #endif |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 476 | #endif /* TARGET_NR_utimensat */ |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 477 | |
Andreas Schwab | 95d0307 | 2018-01-23 11:53:31 +0100 | [diff] [blame] | 478 | #ifdef TARGET_NR_renameat2 |
| 479 | #if defined(__NR_renameat2) |
| 480 | #define __NR_sys_renameat2 __NR_renameat2 |
| 481 | _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd, |
| 482 | const char *, new, unsigned int, flags) |
| 483 | #else |
| 484 | static int sys_renameat2(int oldfd, const char *old, |
| 485 | int newfd, const char *new, int flags) |
| 486 | { |
| 487 | if (flags == 0) { |
| 488 | return renameat(oldfd, old, newfd, new); |
| 489 | } |
| 490 | errno = ENOSYS; |
| 491 | return -1; |
| 492 | } |
| 493 | #endif |
| 494 | #endif /* TARGET_NR_renameat2 */ |
| 495 | |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 496 | #ifdef CONFIG_INOTIFY |
aurel32 | 8690e42 | 2009-04-17 13:50:32 +0000 | [diff] [blame] | 497 | #include <sys/inotify.h> |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 498 | #else |
| 499 | /* Userspace can usually survive runtime without inotify */ |
| 500 | #undef TARGET_NR_inotify_init |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 501 | #undef TARGET_NR_inotify_init1 |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 502 | #undef TARGET_NR_inotify_add_watch |
| 503 | #undef TARGET_NR_inotify_rm_watch |
| 504 | #endif /* CONFIG_INOTIFY */ |
| 505 | |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 506 | #if defined(TARGET_NR_prlimit64) |
| 507 | #ifndef __NR_prlimit64 |
| 508 | # define __NR_prlimit64 -1 |
| 509 | #endif |
| 510 | #define __NR_sys_prlimit64 __NR_prlimit64 |
| 511 | /* The glibc rlimit structure may not be that used by the underlying syscall */ |
| 512 | struct host_rlimit64 { |
| 513 | uint64_t rlim_cur; |
| 514 | uint64_t rlim_max; |
| 515 | }; |
| 516 | _syscall4(int, sys_prlimit64, pid_t, pid, int, resource, |
| 517 | const struct host_rlimit64 *, new_limit, |
| 518 | struct host_rlimit64 *, old_limit) |
| 519 | #endif |
| 520 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 521 | |
| 522 | #if defined(TARGET_NR_timer_create) |
zhaolichang | 6f9ff55 | 2020-09-17 15:50:25 +0800 | [diff] [blame] | 523 | /* Maximum of 32 active POSIX timers allowed at any one time. */ |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 524 | #define GUEST_TIMER_MAX 32 |
| 525 | static timer_t g_posix_timers[GUEST_TIMER_MAX]; |
| 526 | static int g_posix_timer_allocated[GUEST_TIMER_MAX]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 527 | |
| 528 | static inline int next_free_host_timer(void) |
| 529 | { |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 530 | int k; |
| 531 | for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) { |
| 532 | if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 533 | return k; |
| 534 | } |
| 535 | } |
| 536 | return -1; |
| 537 | } |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 538 | |
| 539 | static inline void free_host_timer_slot(int id) |
| 540 | { |
| 541 | qatomic_store_release(g_posix_timer_allocated + id, 0); |
| 542 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 543 | #endif |
| 544 | |
Philippe Mathieu-Daudé | 3ffe326 | 2021-07-08 19:05:49 +0200 | [diff] [blame] | 545 | static inline int host_to_target_errno(int host_errno) |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 546 | { |
Philippe Mathieu-Daudé | 3ffe326 | 2021-07-08 19:05:49 +0200 | [diff] [blame] | 547 | switch (host_errno) { |
| 548 | #define E(X) case X: return TARGET_##X; |
| 549 | #include "errnos.c.inc" |
| 550 | #undef E |
| 551 | default: |
| 552 | return host_errno; |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 553 | } |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Philippe Mathieu-Daudé | 3ffe326 | 2021-07-08 19:05:49 +0200 | [diff] [blame] | 556 | static inline int target_to_host_errno(int target_errno) |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 557 | { |
Philippe Mathieu-Daudé | 3ffe326 | 2021-07-08 19:05:49 +0200 | [diff] [blame] | 558 | switch (target_errno) { |
| 559 | #define E(X) case TARGET_##X: return X; |
| 560 | #include "errnos.c.inc" |
| 561 | #undef E |
| 562 | default: |
| 563 | return target_errno; |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 564 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Ilya Leoshkevich | 892a4f6 | 2022-06-21 16:42:05 +0200 | [diff] [blame] | 567 | abi_long get_errno(abi_long ret) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 568 | { |
| 569 | if (ret == -1) |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 570 | return -host_to_target_errno(errno); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 571 | else |
| 572 | return ret; |
| 573 | } |
| 574 | |
Peter Maydell | 7dcdaea | 2016-06-06 19:58:18 +0100 | [diff] [blame] | 575 | const char *target_strerror(int err) |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 576 | { |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 577 | if (err == QEMU_ERESTARTSYS) { |
Peter Maydell | da2a34f | 2016-06-06 19:58:19 +0100 | [diff] [blame] | 578 | return "To be restarted"; |
| 579 | } |
Richard Henderson | 57a0c93 | 2021-11-17 05:14:52 -0800 | [diff] [blame] | 580 | if (err == QEMU_ESIGRETURN) { |
Peter Maydell | da2a34f | 2016-06-06 19:58:19 +0100 | [diff] [blame] | 581 | return "Successful exit from sigreturn"; |
| 582 | } |
| 583 | |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 584 | return strerror(target_to_host_errno(err)); |
| 585 | } |
| 586 | |
Tonis Tiigi | 45ad761 | 2022-01-04 20:18:18 -0800 | [diff] [blame] | 587 | static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize) |
| 588 | { |
| 589 | int i; |
| 590 | uint8_t b; |
| 591 | if (usize <= ksize) { |
| 592 | return 1; |
| 593 | } |
| 594 | for (i = ksize; i < usize; i++) { |
| 595 | if (get_user_u8(b, addr + i)) { |
| 596 | return -TARGET_EFAULT; |
| 597 | } |
| 598 | if (b != 0) { |
| 599 | return 0; |
| 600 | } |
| 601 | } |
| 602 | return 1; |
| 603 | } |
| 604 | |
Timothy E Baldwin | 4d330ce | 2016-05-12 18:47:46 +0100 | [diff] [blame] | 605 | #define safe_syscall0(type, name) \ |
| 606 | static type safe_##name(void) \ |
| 607 | { \ |
| 608 | return safe_syscall(__NR_##name); \ |
| 609 | } |
| 610 | |
| 611 | #define safe_syscall1(type, name, type1, arg1) \ |
| 612 | static type safe_##name(type1 arg1) \ |
| 613 | { \ |
| 614 | return safe_syscall(__NR_##name, arg1); \ |
| 615 | } |
| 616 | |
| 617 | #define safe_syscall2(type, name, type1, arg1, type2, arg2) \ |
| 618 | static type safe_##name(type1 arg1, type2 arg2) \ |
| 619 | { \ |
| 620 | return safe_syscall(__NR_##name, arg1, arg2); \ |
| 621 | } |
| 622 | |
| 623 | #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ |
| 624 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \ |
| 625 | { \ |
| 626 | return safe_syscall(__NR_##name, arg1, arg2, arg3); \ |
| 627 | } |
| 628 | |
| 629 | #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 630 | type4, arg4) \ |
| 631 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ |
| 632 | { \ |
| 633 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \ |
| 634 | } |
| 635 | |
| 636 | #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 637 | type4, arg4, type5, arg5) \ |
| 638 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ |
| 639 | type5 arg5) \ |
| 640 | { \ |
| 641 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ |
| 642 | } |
| 643 | |
| 644 | #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 645 | type4, arg4, type5, arg5, type6, arg6) \ |
| 646 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ |
| 647 | type5 arg5, type6 arg6) \ |
| 648 | { \ |
| 649 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ |
| 650 | } |
| 651 | |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 652 | safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count) |
| 653 | safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count) |
Timothy E Baldwin | c10a073 | 2016-05-12 18:47:48 +0100 | [diff] [blame] | 654 | safe_syscall4(int, openat, int, dirfd, const char *, pathname, \ |
| 655 | int, flags, mode_t, mode) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 656 | #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid) |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 657 | safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \ |
| 658 | struct rusage *, rusage) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 659 | #endif |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 660 | safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ |
| 661 | int, options, struct rusage *, rusage) |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 662 | safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) |
Drew DeVault | 55bbe4d | 2022-11-04 18:36:32 +0100 | [diff] [blame] | 663 | safe_syscall5(int, execveat, int, dirfd, const char *, filename, |
| 664 | char **, argv, char **, envp, int, flags) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 665 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 666 | defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 667 | safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ |
| 668 | fd_set *, exceptfds, struct timespec *, timeout, void *, sig) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 669 | #endif |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 670 | #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 671 | safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, |
| 672 | struct timespec *, tsp, const sigset_t *, sigmask, |
| 673 | size_t, sigsetsize) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 674 | #endif |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 675 | safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events, |
| 676 | int, maxevents, int, timeout, const sigset_t *, sigmask, |
| 677 | size_t, sigsetsize) |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 678 | #if defined(__NR_futex) |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 679 | safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \ |
| 680 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 681 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 682 | #if defined(__NR_futex_time64) |
| 683 | safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \ |
| 684 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 685 | #endif |
Peter Maydell | 2fe4fba | 2016-05-27 15:51:48 +0100 | [diff] [blame] | 686 | safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize) |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 687 | safe_syscall2(int, kill, pid_t, pid, int, sig) |
| 688 | safe_syscall2(int, tkill, int, tid, int, sig) |
| 689 | safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig) |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 690 | safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt) |
| 691 | safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt) |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 692 | safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, |
| 693 | unsigned long, pos_l, unsigned long, pos_h) |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 694 | safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, |
| 695 | unsigned long, pos_l, unsigned long, pos_h) |
Peter Maydell | 2a3c761 | 2016-06-06 19:58:03 +0100 | [diff] [blame] | 696 | safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr, |
| 697 | socklen_t, addrlen) |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 698 | safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len, |
| 699 | int, flags, const struct sockaddr *, addr, socklen_t, addrlen) |
| 700 | safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len, |
| 701 | int, flags, struct sockaddr *, addr, socklen_t *, addrlen) |
| 702 | safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags) |
| 703 | safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags) |
Peter Maydell | 2a84598 | 2016-06-06 19:58:07 +0100 | [diff] [blame] | 704 | safe_syscall2(int, flock, int, fd, int, operation) |
Filip Bozuta | ddcbde1 | 2020-08-24 21:21:16 +0200 | [diff] [blame] | 705 | #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64) |
Peter Maydell | b3f8233 | 2016-06-06 19:58:08 +0100 | [diff] [blame] | 706 | safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo, |
| 707 | const struct timespec *, uts, size_t, sigsetsize) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 708 | #endif |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 709 | safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len, |
| 710 | int, flags) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 711 | #if defined(TARGET_NR_nanosleep) |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 712 | safe_syscall2(int, nanosleep, const struct timespec *, req, |
| 713 | struct timespec *, rem) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 714 | #endif |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 715 | #if defined(TARGET_NR_clock_nanosleep) || \ |
| 716 | defined(TARGET_NR_clock_nanosleep_time64) |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 717 | safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags, |
| 718 | const struct timespec *, req, struct timespec *, rem) |
| 719 | #endif |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 720 | #ifdef __NR_ipc |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 721 | #ifdef __s390x__ |
| 722 | safe_syscall5(int, ipc, int, call, long, first, long, second, long, third, |
| 723 | void *, ptr) |
| 724 | #else |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 725 | safe_syscall6(int, ipc, int, call, long, first, long, second, long, third, |
| 726 | void *, ptr, long, fifth) |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 727 | #endif |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 728 | #endif |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 729 | #ifdef __NR_msgsnd |
| 730 | safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz, |
| 731 | int, flags) |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 732 | #endif |
| 733 | #ifdef __NR_msgrcv |
| 734 | safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz, |
| 735 | long, msgtype, int, flags) |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 736 | #endif |
| 737 | #ifdef __NR_semtimedop |
| 738 | safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops, |
| 739 | unsigned, nsops, const struct timespec *, timeout) |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 740 | #endif |
Filip Bozuta | d107e37 | 2020-08-24 21:37:52 +0200 | [diff] [blame] | 741 | #if defined(TARGET_NR_mq_timedsend) || \ |
| 742 | defined(TARGET_NR_mq_timedsend_time64) |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 743 | safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr, |
| 744 | size_t, len, unsigned, prio, const struct timespec *, timeout) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 745 | #endif |
Filip Bozuta | d107e37 | 2020-08-24 21:37:52 +0200 | [diff] [blame] | 746 | #if defined(TARGET_NR_mq_timedreceive) || \ |
| 747 | defined(TARGET_NR_mq_timedreceive_time64) |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 748 | safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr, |
| 749 | size_t, len, unsigned *, prio, const struct timespec *, timeout) |
| 750 | #endif |
Andreas Schwab | 8494645 | 2020-11-12 12:45:16 +0100 | [diff] [blame] | 751 | #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range) |
| 752 | safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff, |
| 753 | int, outfd, loff_t *, poutoff, size_t, length, |
| 754 | unsigned int, flags) |
| 755 | #endif |
| 756 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 757 | /* We do ioctl like this rather than via safe_syscall3 to preserve the |
| 758 | * "third argument might be integer or pointer or not present" behaviour of |
| 759 | * the libc function. |
| 760 | */ |
| 761 | #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__) |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 762 | /* Similarly for fcntl. Since we always build with LFS enabled, |
| 763 | * we should be using the 64-bit structures automatically. |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 764 | */ |
| 765 | #ifdef __NR_fcntl64 |
| 766 | #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__) |
| 767 | #else |
| 768 | #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__) |
| 769 | #endif |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 770 | |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 771 | static inline int host_to_target_sock_type(int host_type) |
| 772 | { |
| 773 | int target_type; |
| 774 | |
| 775 | switch (host_type & 0xf /* SOCK_TYPE_MASK */) { |
| 776 | case SOCK_DGRAM: |
| 777 | target_type = TARGET_SOCK_DGRAM; |
| 778 | break; |
| 779 | case SOCK_STREAM: |
| 780 | target_type = TARGET_SOCK_STREAM; |
| 781 | break; |
| 782 | default: |
| 783 | target_type = host_type & 0xf /* SOCK_TYPE_MASK */; |
| 784 | break; |
| 785 | } |
| 786 | |
| 787 | #if defined(SOCK_CLOEXEC) |
| 788 | if (host_type & SOCK_CLOEXEC) { |
| 789 | target_type |= TARGET_SOCK_CLOEXEC; |
| 790 | } |
| 791 | #endif |
| 792 | |
| 793 | #if defined(SOCK_NONBLOCK) |
| 794 | if (host_type & SOCK_NONBLOCK) { |
| 795 | target_type |= TARGET_SOCK_NONBLOCK; |
| 796 | } |
| 797 | #endif |
| 798 | |
| 799 | return target_type; |
| 800 | } |
| 801 | |
Helge Deller | dfe4986 | 2023-07-17 12:27:13 +0200 | [diff] [blame] | 802 | static abi_ulong target_brk, initial_target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 803 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 804 | void target_set_brk(abi_ulong new_brk) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 805 | { |
Andreas Schwab | d28b3c9 | 2023-07-06 13:34:19 +0200 | [diff] [blame] | 806 | target_brk = TARGET_PAGE_ALIGN(new_brk); |
Helge Deller | dfe4986 | 2023-07-17 12:27:13 +0200 | [diff] [blame] | 807 | initial_target_brk = target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 808 | } |
| 809 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 810 | /* do_brk() must return target values and target errnos. */ |
Helge Deller | 86f0473 | 2022-12-25 09:23:19 +0100 | [diff] [blame] | 811 | abi_long do_brk(abi_ulong brk_val) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 812 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 813 | abi_long mapped_addr; |
Akihiko Odaki | 2aea137 | 2023-08-02 16:17:52 +0900 | [diff] [blame] | 814 | abi_ulong new_brk; |
| 815 | abi_ulong old_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 816 | |
Richard Henderson | ee1bf83 | 2021-02-12 10:48:44 -0800 | [diff] [blame] | 817 | /* brk pointers are always untagged */ |
| 818 | |
Helge Deller | dfe4986 | 2023-07-17 12:27:13 +0200 | [diff] [blame] | 819 | /* do not allow to shrink below initial brk value */ |
| 820 | if (brk_val < initial_target_brk) { |
Akihiko Odaki | cb9d5d1 | 2023-08-02 16:17:51 +0900 | [diff] [blame] | 821 | return target_brk; |
Helge Deller | dfe4986 | 2023-07-17 12:27:13 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Helge Deller | 86f0473 | 2022-12-25 09:23:19 +0100 | [diff] [blame] | 824 | new_brk = TARGET_PAGE_ALIGN(brk_val); |
Akihiko Odaki | 2aea137 | 2023-08-02 16:17:52 +0900 | [diff] [blame] | 825 | old_brk = TARGET_PAGE_ALIGN(target_brk); |
Helge Deller | 86f0473 | 2022-12-25 09:23:19 +0100 | [diff] [blame] | 826 | |
Akihiko Odaki | 2aea137 | 2023-08-02 16:17:52 +0900 | [diff] [blame] | 827 | /* new and old target_brk might be on the same page */ |
| 828 | if (new_brk == old_brk) { |
Helge Deller | 86f0473 | 2022-12-25 09:23:19 +0100 | [diff] [blame] | 829 | target_brk = brk_val; |
| 830 | return target_brk; |
| 831 | } |
| 832 | |
Michael Tokarev | 2cf91b9 | 2023-11-14 19:07:15 +0300 | [diff] [blame] | 833 | /* Release heap if necessary */ |
Akihiko Odaki | 2aea137 | 2023-08-02 16:17:52 +0900 | [diff] [blame] | 834 | if (new_brk < old_brk) { |
| 835 | target_munmap(new_brk, old_brk - new_brk); |
Helge Deller | 86f0473 | 2022-12-25 09:23:19 +0100 | [diff] [blame] | 836 | |
| 837 | target_brk = brk_val; |
| 838 | return target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Akihiko Odaki | 2aea137 | 2023-08-02 16:17:52 +0900 | [diff] [blame] | 841 | mapped_addr = target_mmap(old_brk, new_brk - old_brk, |
| 842 | PROT_READ | PROT_WRITE, |
| 843 | MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE, |
| 844 | -1, 0); |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 845 | |
Akihiko Odaki | 2aea137 | 2023-08-02 16:17:52 +0900 | [diff] [blame] | 846 | if (mapped_addr == old_brk) { |
Helge Deller | 86f0473 | 2022-12-25 09:23:19 +0100 | [diff] [blame] | 847 | target_brk = brk_val; |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 848 | return target_brk; |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 849 | } |
balrog | 7ab240a | 2008-04-26 12:17:34 +0000 | [diff] [blame] | 850 | |
Richard Henderson | 7dd46c0 | 2010-05-03 10:07:49 -0700 | [diff] [blame] | 851 | #if defined(TARGET_ALPHA) |
| 852 | /* We (partially) emulate OSF/1 on Alpha, which requires we |
| 853 | return a proper errno, not an unchanged brk value. */ |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 854 | return -TARGET_ENOMEM; |
Richard Henderson | 7dd46c0 | 2010-05-03 10:07:49 -0700 | [diff] [blame] | 855 | #endif |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 856 | /* For everything else, return the previous break. */ |
balrog | 7ab240a | 2008-04-26 12:17:34 +0000 | [diff] [blame] | 857 | return target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 860 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 861 | defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 862 | static inline abi_long copy_from_user_fdset(fd_set *fds, |
| 863 | abi_ulong target_fds_addr, |
| 864 | int n) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 865 | { |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 866 | int i, nw, j, k; |
| 867 | abi_ulong b, *target_fds; |
| 868 | |
Laurent Vivier | b1b2db2 | 2016-05-31 18:36:02 +0200 | [diff] [blame] | 869 | nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 870 | if (!(target_fds = lock_user(VERIFY_READ, |
| 871 | target_fds_addr, |
| 872 | sizeof(abi_ulong) * nw, |
| 873 | 1))) |
| 874 | return -TARGET_EFAULT; |
| 875 | |
| 876 | FD_ZERO(fds); |
| 877 | k = 0; |
| 878 | for (i = 0; i < nw; i++) { |
| 879 | /* grab the abi_ulong */ |
| 880 | __get_user(b, &target_fds[i]); |
| 881 | for (j = 0; j < TARGET_ABI_BITS; j++) { |
| 882 | /* check the bit inside the abi_ulong */ |
| 883 | if ((b >> j) & 1) |
| 884 | FD_SET(k, fds); |
| 885 | k++; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 886 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 887 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 888 | |
| 889 | unlock_user(target_fds, target_fds_addr, 0); |
| 890 | |
| 891 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 894 | static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr, |
| 895 | abi_ulong target_fds_addr, |
| 896 | int n) |
| 897 | { |
| 898 | if (target_fds_addr) { |
| 899 | if (copy_from_user_fdset(fds, target_fds_addr, n)) |
| 900 | return -TARGET_EFAULT; |
| 901 | *fds_ptr = fds; |
| 902 | } else { |
| 903 | *fds_ptr = NULL; |
| 904 | } |
| 905 | return 0; |
| 906 | } |
| 907 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 908 | static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr, |
| 909 | const fd_set *fds, |
| 910 | int n) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 911 | { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 912 | int i, nw, j, k; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 913 | abi_long v; |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 914 | abi_ulong *target_fds; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 915 | |
Laurent Vivier | b1b2db2 | 2016-05-31 18:36:02 +0200 | [diff] [blame] | 916 | nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 917 | if (!(target_fds = lock_user(VERIFY_WRITE, |
| 918 | target_fds_addr, |
| 919 | sizeof(abi_ulong) * nw, |
| 920 | 0))) |
| 921 | return -TARGET_EFAULT; |
| 922 | |
| 923 | k = 0; |
| 924 | for (i = 0; i < nw; i++) { |
| 925 | v = 0; |
| 926 | for (j = 0; j < TARGET_ABI_BITS; j++) { |
Andreas Schwab | 9ab709b | 2013-04-09 01:02:34 +0000 | [diff] [blame] | 927 | v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 928 | k++; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 929 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 930 | __put_user(v, &target_fds[i]); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 931 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 932 | |
| 933 | unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw); |
| 934 | |
| 935 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 936 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 937 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 938 | |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 939 | #if defined(__alpha__) |
| 940 | #define HOST_HZ 1024 |
| 941 | #else |
| 942 | #define HOST_HZ 100 |
| 943 | #endif |
| 944 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 945 | static inline abi_long host_to_target_clock_t(long ticks) |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 946 | { |
| 947 | #if HOST_HZ == TARGET_HZ |
| 948 | return ticks; |
| 949 | #else |
| 950 | return ((int64_t)ticks * TARGET_HZ) / HOST_HZ; |
| 951 | #endif |
| 952 | } |
| 953 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 954 | static inline abi_long host_to_target_rusage(abi_ulong target_addr, |
| 955 | const struct rusage *rusage) |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 956 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 957 | struct target_rusage *target_rusage; |
| 958 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 959 | if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) |
| 960 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 961 | target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec); |
| 962 | target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec); |
| 963 | target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec); |
| 964 | target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec); |
| 965 | target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss); |
| 966 | target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss); |
| 967 | target_rusage->ru_idrss = tswapal(rusage->ru_idrss); |
| 968 | target_rusage->ru_isrss = tswapal(rusage->ru_isrss); |
| 969 | target_rusage->ru_minflt = tswapal(rusage->ru_minflt); |
| 970 | target_rusage->ru_majflt = tswapal(rusage->ru_majflt); |
| 971 | target_rusage->ru_nswap = tswapal(rusage->ru_nswap); |
| 972 | target_rusage->ru_inblock = tswapal(rusage->ru_inblock); |
| 973 | target_rusage->ru_oublock = tswapal(rusage->ru_oublock); |
| 974 | target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd); |
| 975 | target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv); |
| 976 | target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals); |
| 977 | target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw); |
| 978 | target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 979 | unlock_user_struct(target_rusage, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 980 | |
| 981 | return 0; |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 984 | #ifdef TARGET_NR_setrlimit |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 985 | static inline rlim_t target_to_host_rlim(abi_ulong target_rlim) |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 986 | { |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 987 | abi_ulong target_rlim_swap; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 988 | rlim_t result; |
| 989 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 990 | target_rlim_swap = tswapal(target_rlim); |
| 991 | if (target_rlim_swap == TARGET_RLIM_INFINITY) |
| 992 | return RLIM_INFINITY; |
| 993 | |
| 994 | result = target_rlim_swap; |
| 995 | if (target_rlim_swap != (rlim_t)result) |
| 996 | return RLIM_INFINITY; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 997 | |
| 998 | return result; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 999 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1000 | #endif |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1001 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1002 | #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit) |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1003 | static inline abi_ulong host_to_target_rlim(rlim_t rlim) |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1004 | { |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1005 | abi_ulong target_rlim_swap; |
| 1006 | abi_ulong result; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1007 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1008 | if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim) |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1009 | target_rlim_swap = TARGET_RLIM_INFINITY; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1010 | else |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1011 | target_rlim_swap = rlim; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1012 | result = tswapal(target_rlim_swap); |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1013 | |
| 1014 | return result; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1015 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1016 | #endif |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1017 | |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 1018 | static inline int target_to_host_resource(int code) |
| 1019 | { |
| 1020 | switch (code) { |
| 1021 | case TARGET_RLIMIT_AS: |
| 1022 | return RLIMIT_AS; |
| 1023 | case TARGET_RLIMIT_CORE: |
| 1024 | return RLIMIT_CORE; |
| 1025 | case TARGET_RLIMIT_CPU: |
| 1026 | return RLIMIT_CPU; |
| 1027 | case TARGET_RLIMIT_DATA: |
| 1028 | return RLIMIT_DATA; |
| 1029 | case TARGET_RLIMIT_FSIZE: |
| 1030 | return RLIMIT_FSIZE; |
| 1031 | case TARGET_RLIMIT_LOCKS: |
| 1032 | return RLIMIT_LOCKS; |
| 1033 | case TARGET_RLIMIT_MEMLOCK: |
| 1034 | return RLIMIT_MEMLOCK; |
| 1035 | case TARGET_RLIMIT_MSGQUEUE: |
| 1036 | return RLIMIT_MSGQUEUE; |
| 1037 | case TARGET_RLIMIT_NICE: |
| 1038 | return RLIMIT_NICE; |
| 1039 | case TARGET_RLIMIT_NOFILE: |
| 1040 | return RLIMIT_NOFILE; |
| 1041 | case TARGET_RLIMIT_NPROC: |
| 1042 | return RLIMIT_NPROC; |
| 1043 | case TARGET_RLIMIT_RSS: |
| 1044 | return RLIMIT_RSS; |
| 1045 | case TARGET_RLIMIT_RTPRIO: |
| 1046 | return RLIMIT_RTPRIO; |
Fabrice Fontaine | c3a28d7 | 2022-05-23 12:52:39 +0200 | [diff] [blame] | 1047 | #ifdef RLIMIT_RTTIME |
Serge Belyshev | 244fd08 | 2022-01-29 22:48:23 +0300 | [diff] [blame] | 1048 | case TARGET_RLIMIT_RTTIME: |
| 1049 | return RLIMIT_RTTIME; |
Fabrice Fontaine | c3a28d7 | 2022-05-23 12:52:39 +0200 | [diff] [blame] | 1050 | #endif |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 1051 | case TARGET_RLIMIT_SIGPENDING: |
| 1052 | return RLIMIT_SIGPENDING; |
| 1053 | case TARGET_RLIMIT_STACK: |
| 1054 | return RLIMIT_STACK; |
| 1055 | default: |
| 1056 | return code; |
| 1057 | } |
| 1058 | } |
| 1059 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1060 | static inline abi_long copy_from_user_timeval(struct timeval *tv, |
| 1061 | abi_ulong target_tv_addr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1062 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1063 | struct target_timeval *target_tv; |
| 1064 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1065 | if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1066 | return -TARGET_EFAULT; |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1067 | } |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1068 | |
| 1069 | __get_user(tv->tv_sec, &target_tv->tv_sec); |
| 1070 | __get_user(tv->tv_usec, &target_tv->tv_usec); |
| 1071 | |
| 1072 | unlock_user_struct(target_tv, target_tv_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1073 | |
| 1074 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1077 | static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr, |
| 1078 | const struct timeval *tv) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1079 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1080 | struct target_timeval *target_tv; |
| 1081 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1082 | if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1083 | return -TARGET_EFAULT; |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1084 | } |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1085 | |
| 1086 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1087 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
| 1088 | |
| 1089 | unlock_user_struct(target_tv, target_tv_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1090 | |
| 1091 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 1094 | #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME) |
| 1095 | static inline abi_long copy_from_user_timeval64(struct timeval *tv, |
| 1096 | abi_ulong target_tv_addr) |
| 1097 | { |
| 1098 | struct target__kernel_sock_timeval *target_tv; |
| 1099 | |
| 1100 | if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) { |
| 1101 | return -TARGET_EFAULT; |
| 1102 | } |
| 1103 | |
| 1104 | __get_user(tv->tv_sec, &target_tv->tv_sec); |
| 1105 | __get_user(tv->tv_usec, &target_tv->tv_usec); |
| 1106 | |
| 1107 | unlock_user_struct(target_tv, target_tv_addr, 0); |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | #endif |
| 1112 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1113 | static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr, |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 1114 | const struct timeval *tv) |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1115 | { |
| 1116 | struct target__kernel_sock_timeval *target_tv; |
| 1117 | |
| 1118 | if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { |
| 1119 | return -TARGET_EFAULT; |
| 1120 | } |
| 1121 | |
| 1122 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1123 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
| 1124 | |
| 1125 | unlock_user_struct(target_tv, target_tv_addr, 1); |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1130 | #if defined(TARGET_NR_futex) || \ |
| 1131 | defined(TARGET_NR_rt_sigtimedwait) || \ |
| 1132 | defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \ |
| 1133 | defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \ |
| 1134 | defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \ |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 1135 | defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \ |
Filip Bozuta | 2c86c90 | 2020-07-22 17:34:20 +0200 | [diff] [blame] | 1136 | defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \ |
| 1137 | defined(TARGET_NR_timer_settime) || \ |
| 1138 | (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)) |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1139 | static inline abi_long target_to_host_timespec(struct timespec *host_ts, |
| 1140 | abi_ulong target_addr) |
| 1141 | { |
| 1142 | struct target_timespec *target_ts; |
| 1143 | |
| 1144 | if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { |
| 1145 | return -TARGET_EFAULT; |
| 1146 | } |
| 1147 | __get_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1148 | __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1149 | unlock_user_struct(target_ts, target_addr, 0); |
| 1150 | return 0; |
| 1151 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1152 | #endif |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1153 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 1154 | #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ |
| 1155 | defined(TARGET_NR_timer_settime64) || \ |
Filip Bozuta | d107e37 | 2020-08-24 21:37:52 +0200 | [diff] [blame] | 1156 | defined(TARGET_NR_mq_timedsend_time64) || \ |
| 1157 | defined(TARGET_NR_mq_timedreceive_time64) || \ |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 1158 | (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \ |
Filip Bozuta | ddcbde1 | 2020-08-24 21:21:16 +0200 | [diff] [blame] | 1159 | defined(TARGET_NR_clock_nanosleep_time64) || \ |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 1160 | defined(TARGET_NR_rt_sigtimedwait_time64) || \ |
| 1161 | defined(TARGET_NR_utimensat) || \ |
| 1162 | defined(TARGET_NR_utimensat_time64) || \ |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1163 | defined(TARGET_NR_semtimedop_time64) || \ |
| 1164 | defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 1165 | static inline abi_long target_to_host_timespec64(struct timespec *host_ts, |
| 1166 | abi_ulong target_addr) |
| 1167 | { |
| 1168 | struct target__kernel_timespec *target_ts; |
| 1169 | |
| 1170 | if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { |
| 1171 | return -TARGET_EFAULT; |
| 1172 | } |
| 1173 | __get_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1174 | __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
Laurent Vivier | 0057675 | 2020-08-27 09:04:49 +0200 | [diff] [blame] | 1175 | /* in 32bit mode, this drops the padding */ |
| 1176 | host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec; |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 1177 | unlock_user_struct(target_ts, target_addr, 0); |
| 1178 | return 0; |
| 1179 | } |
| 1180 | #endif |
| 1181 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1182 | static inline abi_long host_to_target_timespec(abi_ulong target_addr, |
| 1183 | struct timespec *host_ts) |
| 1184 | { |
| 1185 | struct target_timespec *target_ts; |
| 1186 | |
| 1187 | if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { |
| 1188 | return -TARGET_EFAULT; |
| 1189 | } |
| 1190 | __put_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1191 | __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1192 | unlock_user_struct(target_ts, target_addr, 1); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static inline abi_long host_to_target_timespec64(abi_ulong target_addr, |
| 1197 | struct timespec *host_ts) |
| 1198 | { |
| 1199 | struct target__kernel_timespec *target_ts; |
| 1200 | |
| 1201 | if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { |
| 1202 | return -TARGET_EFAULT; |
| 1203 | } |
| 1204 | __put_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1205 | __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1206 | unlock_user_struct(target_ts, target_addr, 1); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 1210 | #if defined(TARGET_NR_gettimeofday) |
| 1211 | static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr, |
| 1212 | struct timezone *tz) |
| 1213 | { |
| 1214 | struct target_timezone *target_tz; |
| 1215 | |
| 1216 | if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) { |
| 1217 | return -TARGET_EFAULT; |
| 1218 | } |
| 1219 | |
| 1220 | __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); |
| 1221 | __put_user(tz->tz_dsttime, &target_tz->tz_dsttime); |
| 1222 | |
| 1223 | unlock_user_struct(target_tz, target_tz_addr, 1); |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | #endif |
| 1228 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1229 | #if defined(TARGET_NR_settimeofday) |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 1230 | static inline abi_long copy_from_user_timezone(struct timezone *tz, |
| 1231 | abi_ulong target_tz_addr) |
| 1232 | { |
| 1233 | struct target_timezone *target_tz; |
| 1234 | |
| 1235 | if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) { |
| 1236 | return -TARGET_EFAULT; |
| 1237 | } |
| 1238 | |
| 1239 | __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); |
| 1240 | __get_user(tz->tz_dsttime, &target_tz->tz_dsttime); |
| 1241 | |
| 1242 | unlock_user_struct(target_tz, target_tz_addr, 0); |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1246 | #endif |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 1247 | |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 1248 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
| 1249 | #include <mqueue.h> |
| 1250 | |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 1251 | static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr, |
| 1252 | abi_ulong target_mq_attr_addr) |
| 1253 | { |
| 1254 | struct target_mq_attr *target_mq_attr; |
| 1255 | |
| 1256 | if (!lock_user_struct(VERIFY_READ, target_mq_attr, |
| 1257 | target_mq_attr_addr, 1)) |
| 1258 | return -TARGET_EFAULT; |
| 1259 | |
| 1260 | __get_user(attr->mq_flags, &target_mq_attr->mq_flags); |
| 1261 | __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); |
| 1262 | __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); |
| 1263 | __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); |
| 1264 | |
| 1265 | unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0); |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr, |
| 1271 | const struct mq_attr *attr) |
| 1272 | { |
| 1273 | struct target_mq_attr *target_mq_attr; |
| 1274 | |
| 1275 | if (!lock_user_struct(VERIFY_WRITE, target_mq_attr, |
| 1276 | target_mq_attr_addr, 0)) |
| 1277 | return -TARGET_EFAULT; |
| 1278 | |
| 1279 | __put_user(attr->mq_flags, &target_mq_attr->mq_flags); |
| 1280 | __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); |
| 1281 | __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); |
| 1282 | __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); |
| 1283 | |
| 1284 | unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1); |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 1288 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1289 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1290 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 1291 | /* do_select() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1292 | static abi_long do_select(int n, |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1293 | abi_ulong rfd_addr, abi_ulong wfd_addr, |
| 1294 | abi_ulong efd_addr, abi_ulong target_tv_addr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1295 | { |
| 1296 | fd_set rfds, wfds, efds; |
| 1297 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1298 | struct timeval tv; |
| 1299 | struct timespec ts, *ts_ptr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1300 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1301 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1302 | ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); |
| 1303 | if (ret) { |
| 1304 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1305 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1306 | ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); |
| 1307 | if (ret) { |
| 1308 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1309 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1310 | ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); |
| 1311 | if (ret) { |
| 1312 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1313 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1314 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1315 | if (target_tv_addr) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1316 | if (copy_from_user_timeval(&tv, target_tv_addr)) |
| 1317 | return -TARGET_EFAULT; |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1318 | ts.tv_sec = tv.tv_sec; |
| 1319 | ts.tv_nsec = tv.tv_usec * 1000; |
| 1320 | ts_ptr = &ts; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1321 | } else { |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1322 | ts_ptr = NULL; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1323 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1324 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1325 | ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, |
| 1326 | ts_ptr, NULL)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1327 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1328 | if (!is_error(ret)) { |
| 1329 | if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) |
| 1330 | return -TARGET_EFAULT; |
| 1331 | if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) |
| 1332 | return -TARGET_EFAULT; |
| 1333 | if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) |
| 1334 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1335 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1336 | if (target_tv_addr) { |
| 1337 | tv.tv_sec = ts.tv_sec; |
| 1338 | tv.tv_usec = ts.tv_nsec / 1000; |
| 1339 | if (copy_to_user_timeval(target_tv_addr, &tv)) { |
| 1340 | return -TARGET_EFAULT; |
| 1341 | } |
| 1342 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1343 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1344 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1345 | return ret; |
| 1346 | } |
Laurent Vivier | 5457dc9 | 2016-07-08 01:17:27 +0200 | [diff] [blame] | 1347 | |
| 1348 | #if defined(TARGET_WANT_OLD_SYS_SELECT) |
| 1349 | static abi_long do_old_select(abi_ulong arg1) |
| 1350 | { |
| 1351 | struct target_sel_arg_struct *sel; |
| 1352 | abi_ulong inp, outp, exp, tvp; |
| 1353 | long nsel; |
| 1354 | |
| 1355 | if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) { |
| 1356 | return -TARGET_EFAULT; |
| 1357 | } |
| 1358 | |
| 1359 | nsel = tswapal(sel->n); |
| 1360 | inp = tswapal(sel->inp); |
| 1361 | outp = tswapal(sel->outp); |
| 1362 | exp = tswapal(sel->exp); |
| 1363 | tvp = tswapal(sel->tvp); |
| 1364 | |
| 1365 | unlock_user_struct(sel, arg1, 0); |
| 1366 | |
| 1367 | return do_select(nsel, inp, outp, exp, tvp); |
| 1368 | } |
| 1369 | #endif |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1370 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1371 | |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1372 | #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) |
| 1373 | static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, |
| 1374 | abi_long arg4, abi_long arg5, abi_long arg6, |
| 1375 | bool time64) |
| 1376 | { |
| 1377 | abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; |
| 1378 | fd_set rfds, wfds, efds; |
| 1379 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; |
| 1380 | struct timespec ts, *ts_ptr; |
| 1381 | abi_long ret; |
| 1382 | |
| 1383 | /* |
| 1384 | * The 6th arg is actually two args smashed together, |
| 1385 | * so we cannot use the C library. |
| 1386 | */ |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1387 | struct { |
| 1388 | sigset_t *set; |
| 1389 | size_t size; |
| 1390 | } sig, *sig_ptr; |
| 1391 | |
| 1392 | abi_ulong arg_sigset, arg_sigsize, *arg7; |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1393 | |
| 1394 | n = arg1; |
| 1395 | rfd_addr = arg2; |
| 1396 | wfd_addr = arg3; |
| 1397 | efd_addr = arg4; |
| 1398 | ts_addr = arg5; |
| 1399 | |
| 1400 | ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); |
| 1401 | if (ret) { |
| 1402 | return ret; |
| 1403 | } |
| 1404 | ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); |
| 1405 | if (ret) { |
| 1406 | return ret; |
| 1407 | } |
| 1408 | ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); |
| 1409 | if (ret) { |
| 1410 | return ret; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * This takes a timespec, and not a timeval, so we cannot |
| 1415 | * use the do_select() helper ... |
| 1416 | */ |
| 1417 | if (ts_addr) { |
| 1418 | if (time64) { |
| 1419 | if (target_to_host_timespec64(&ts, ts_addr)) { |
| 1420 | return -TARGET_EFAULT; |
| 1421 | } |
| 1422 | } else { |
| 1423 | if (target_to_host_timespec(&ts, ts_addr)) { |
| 1424 | return -TARGET_EFAULT; |
| 1425 | } |
| 1426 | } |
| 1427 | ts_ptr = &ts; |
| 1428 | } else { |
| 1429 | ts_ptr = NULL; |
| 1430 | } |
| 1431 | |
| 1432 | /* Extract the two packed args for the sigset */ |
Richard Henderson | cb22603 | 2022-03-15 01:43:06 -0700 | [diff] [blame] | 1433 | sig_ptr = NULL; |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1434 | if (arg6) { |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1435 | arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); |
| 1436 | if (!arg7) { |
| 1437 | return -TARGET_EFAULT; |
| 1438 | } |
| 1439 | arg_sigset = tswapal(arg7[0]); |
| 1440 | arg_sigsize = tswapal(arg7[1]); |
| 1441 | unlock_user(arg7, arg6, 0); |
| 1442 | |
| 1443 | if (arg_sigset) { |
Richard Henderson | cb22603 | 2022-03-15 01:43:06 -0700 | [diff] [blame] | 1444 | ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize); |
| 1445 | if (ret != 0) { |
| 1446 | return ret; |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1447 | } |
Richard Henderson | cb22603 | 2022-03-15 01:43:06 -0700 | [diff] [blame] | 1448 | sig_ptr = &sig; |
| 1449 | sig.size = SIGSET_T_SIZE; |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1450 | } |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, |
| 1454 | ts_ptr, sig_ptr)); |
| 1455 | |
Richard Henderson | cb22603 | 2022-03-15 01:43:06 -0700 | [diff] [blame] | 1456 | if (sig_ptr) { |
| 1457 | finish_sigsuspend_mask(ret); |
| 1458 | } |
| 1459 | |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1460 | if (!is_error(ret)) { |
| 1461 | if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { |
| 1462 | return -TARGET_EFAULT; |
| 1463 | } |
| 1464 | if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { |
| 1465 | return -TARGET_EFAULT; |
| 1466 | } |
| 1467 | if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { |
| 1468 | return -TARGET_EFAULT; |
| 1469 | } |
| 1470 | if (time64) { |
| 1471 | if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { |
| 1472 | return -TARGET_EFAULT; |
| 1473 | } |
| 1474 | } else { |
| 1475 | if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { |
| 1476 | return -TARGET_EFAULT; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | return ret; |
| 1481 | } |
| 1482 | #endif |
| 1483 | |
| 1484 | #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ |
| 1485 | defined(TARGET_NR_ppoll_time64) |
| 1486 | static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, |
| 1487 | abi_long arg4, abi_long arg5, bool ppoll, bool time64) |
| 1488 | { |
| 1489 | struct target_pollfd *target_pfd; |
| 1490 | unsigned int nfds = arg2; |
| 1491 | struct pollfd *pfd; |
| 1492 | unsigned int i; |
| 1493 | abi_long ret; |
| 1494 | |
| 1495 | pfd = NULL; |
| 1496 | target_pfd = NULL; |
| 1497 | if (nfds) { |
| 1498 | if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { |
| 1499 | return -TARGET_EINVAL; |
| 1500 | } |
| 1501 | target_pfd = lock_user(VERIFY_WRITE, arg1, |
| 1502 | sizeof(struct target_pollfd) * nfds, 1); |
| 1503 | if (!target_pfd) { |
| 1504 | return -TARGET_EFAULT; |
| 1505 | } |
| 1506 | |
| 1507 | pfd = alloca(sizeof(struct pollfd) * nfds); |
| 1508 | for (i = 0; i < nfds; i++) { |
| 1509 | pfd[i].fd = tswap32(target_pfd[i].fd); |
| 1510 | pfd[i].events = tswap16(target_pfd[i].events); |
| 1511 | } |
| 1512 | } |
| 1513 | if (ppoll) { |
| 1514 | struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; |
Richard Henderson | db36aa7 | 2022-03-15 01:43:08 -0700 | [diff] [blame] | 1515 | sigset_t *set = NULL; |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1516 | |
| 1517 | if (arg3) { |
| 1518 | if (time64) { |
| 1519 | if (target_to_host_timespec64(timeout_ts, arg3)) { |
| 1520 | unlock_user(target_pfd, arg1, 0); |
| 1521 | return -TARGET_EFAULT; |
| 1522 | } |
| 1523 | } else { |
| 1524 | if (target_to_host_timespec(timeout_ts, arg3)) { |
| 1525 | unlock_user(target_pfd, arg1, 0); |
| 1526 | return -TARGET_EFAULT; |
| 1527 | } |
| 1528 | } |
| 1529 | } else { |
| 1530 | timeout_ts = NULL; |
| 1531 | } |
| 1532 | |
| 1533 | if (arg4) { |
Richard Henderson | db36aa7 | 2022-03-15 01:43:08 -0700 | [diff] [blame] | 1534 | ret = process_sigsuspend_mask(&set, arg4, arg5); |
| 1535 | if (ret != 0) { |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1536 | unlock_user(target_pfd, arg1, 0); |
Richard Henderson | db36aa7 | 2022-03-15 01:43:08 -0700 | [diff] [blame] | 1537 | return ret; |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1538 | } |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, |
| 1542 | set, SIGSET_T_SIZE)); |
| 1543 | |
Richard Henderson | db36aa7 | 2022-03-15 01:43:08 -0700 | [diff] [blame] | 1544 | if (set) { |
| 1545 | finish_sigsuspend_mask(ret); |
| 1546 | } |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1547 | if (!is_error(ret) && arg3) { |
| 1548 | if (time64) { |
| 1549 | if (host_to_target_timespec64(arg3, timeout_ts)) { |
| 1550 | return -TARGET_EFAULT; |
| 1551 | } |
| 1552 | } else { |
| 1553 | if (host_to_target_timespec(arg3, timeout_ts)) { |
| 1554 | return -TARGET_EFAULT; |
| 1555 | } |
| 1556 | } |
| 1557 | } |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 1558 | } else { |
| 1559 | struct timespec ts, *pts; |
| 1560 | |
| 1561 | if (arg3 >= 0) { |
| 1562 | /* Convert ms to secs, ns */ |
| 1563 | ts.tv_sec = arg3 / 1000; |
| 1564 | ts.tv_nsec = (arg3 % 1000) * 1000000LL; |
| 1565 | pts = &ts; |
| 1566 | } else { |
| 1567 | /* -ve poll() timeout means "infinite" */ |
| 1568 | pts = NULL; |
| 1569 | } |
| 1570 | ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); |
| 1571 | } |
| 1572 | |
| 1573 | if (!is_error(ret)) { |
| 1574 | for (i = 0; i < nfds; i++) { |
| 1575 | target_pfd[i].revents = tswap16(pfd[i].revents); |
| 1576 | } |
| 1577 | } |
| 1578 | unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); |
| 1579 | return ret; |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 1583 | static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes, |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1584 | int flags, int is_pipe2) |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1585 | { |
| 1586 | int host_pipe[2]; |
| 1587 | abi_long ret; |
Helge Deller | 499d805 | 2022-07-19 18:20:42 +0200 | [diff] [blame] | 1588 | ret = pipe2(host_pipe, flags); |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1589 | |
| 1590 | if (is_error(ret)) |
| 1591 | return get_errno(ret); |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1592 | |
| 1593 | /* Several targets have special calling conventions for the original |
| 1594 | pipe syscall, but didn't replicate this into the pipe2 syscall. */ |
| 1595 | if (!is_pipe2) { |
| 1596 | #if defined(TARGET_ALPHA) |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 1597 | cpu_env->ir[IR_A4] = host_pipe[1]; |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1598 | return host_pipe[0]; |
| 1599 | #elif defined(TARGET_MIPS) |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 1600 | cpu_env->active_tc.gpr[3] = host_pipe[1]; |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1601 | return host_pipe[0]; |
| 1602 | #elif defined(TARGET_SH4) |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 1603 | cpu_env->gregs[1] = host_pipe[1]; |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1604 | return host_pipe[0]; |
Peter Maydell | 82f05b6 | 2013-07-06 17:39:48 +0100 | [diff] [blame] | 1605 | #elif defined(TARGET_SPARC) |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 1606 | cpu_env->regwptr[1] = host_pipe[1]; |
Peter Maydell | 82f05b6 | 2013-07-06 17:39:48 +0100 | [diff] [blame] | 1607 | return host_pipe[0]; |
takasi-y@ops.dti.ne.jp | 597c021 | 2010-02-18 00:35:03 +0900 | [diff] [blame] | 1608 | #endif |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1611 | if (put_user_s32(host_pipe[0], pipedes) |
Helge Deller | 6f200f5 | 2022-07-17 18:21:53 +0200 | [diff] [blame] | 1612 | || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int))) |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1613 | return -TARGET_EFAULT; |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1614 | return get_errno(ret); |
| 1615 | } |
| 1616 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 1617 | static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1618 | abi_ulong target_addr, |
| 1619 | socklen_t len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1620 | { |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1621 | const socklen_t unix_maxlen = sizeof (struct sockaddr_un); |
| 1622 | sa_family_t sa_family; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1623 | struct target_sockaddr *target_saddr; |
| 1624 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 1625 | if (fd_trans_target_to_host_addr(fd)) { |
| 1626 | return fd_trans_target_to_host_addr(fd)(addr, target_addr, len); |
| 1627 | } |
| 1628 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1629 | target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); |
| 1630 | if (!target_saddr) |
| 1631 | return -TARGET_EFAULT; |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1632 | |
| 1633 | sa_family = tswap16(target_saddr->sa_family); |
| 1634 | |
| 1635 | /* Oops. The caller might send a incomplete sun_path; sun_path |
| 1636 | * must be terminated by \0 (see the manual page), but |
| 1637 | * unfortunately it is quite common to specify sockaddr_un |
| 1638 | * length as "strlen(x->sun_path)" while it should be |
| 1639 | * "strlen(...) + 1". We'll fix that here if needed. |
| 1640 | * Linux kernel has a similar feature. |
| 1641 | */ |
| 1642 | |
| 1643 | if (sa_family == AF_UNIX) { |
| 1644 | if (len < unix_maxlen && len > 0) { |
| 1645 | char *cp = (char*)target_saddr; |
| 1646 | |
| 1647 | if ( cp[len-1] && !cp[len] ) |
| 1648 | len++; |
| 1649 | } |
| 1650 | if (len > unix_maxlen) |
| 1651 | len = unix_maxlen; |
| 1652 | } |
| 1653 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1654 | memcpy(addr, target_saddr, len); |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1655 | addr->sa_family = sa_family; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1656 | if (sa_family == AF_NETLINK) { |
| 1657 | struct sockaddr_nl *nladdr; |
| 1658 | |
| 1659 | nladdr = (struct sockaddr_nl *)addr; |
| 1660 | nladdr->nl_pid = tswap32(nladdr->nl_pid); |
| 1661 | nladdr->nl_groups = tswap32(nladdr->nl_groups); |
| 1662 | } else if (sa_family == AF_PACKET) { |
Joakim Tjernlund | 33a29b5 | 2014-07-12 15:47:07 +0200 | [diff] [blame] | 1663 | struct target_sockaddr_ll *lladdr; |
| 1664 | |
| 1665 | lladdr = (struct target_sockaddr_ll *)addr; |
| 1666 | lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex); |
| 1667 | lladdr->sll_hatype = tswap16(lladdr->sll_hatype); |
Mathis Marion | 44cf673 | 2023-03-07 16:42:55 +0100 | [diff] [blame] | 1668 | } else if (sa_family == AF_INET6) { |
| 1669 | struct sockaddr_in6 *in6addr; |
| 1670 | |
| 1671 | in6addr = (struct sockaddr_in6 *)addr; |
| 1672 | in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id); |
Joakim Tjernlund | 33a29b5 | 2014-07-12 15:47:07 +0200 | [diff] [blame] | 1673 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1674 | unlock_user(target_saddr, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1675 | |
| 1676 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1679 | static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, |
| 1680 | struct sockaddr *addr, |
| 1681 | socklen_t len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1682 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1683 | struct target_sockaddr *target_saddr; |
| 1684 | |
Peter Maydell | a1e2219 | 2016-07-07 15:44:43 +0100 | [diff] [blame] | 1685 | if (len == 0) { |
| 1686 | return 0; |
| 1687 | } |
Philippe Mathieu-Daudé | 6860710 | 2017-07-26 23:42:18 -0300 | [diff] [blame] | 1688 | assert(addr); |
Peter Maydell | a1e2219 | 2016-07-07 15:44:43 +0100 | [diff] [blame] | 1689 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1690 | target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0); |
| 1691 | if (!target_saddr) |
| 1692 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1693 | memcpy(target_saddr, addr, len); |
Peter Maydell | a1e2219 | 2016-07-07 15:44:43 +0100 | [diff] [blame] | 1694 | if (len >= offsetof(struct target_sockaddr, sa_family) + |
| 1695 | sizeof(target_saddr->sa_family)) { |
| 1696 | target_saddr->sa_family = tswap16(addr->sa_family); |
| 1697 | } |
Philippe Mathieu-Daudé | a47401b | 2019-10-21 13:48:52 +0200 | [diff] [blame] | 1698 | if (addr->sa_family == AF_NETLINK && |
| 1699 | len >= sizeof(struct target_sockaddr_nl)) { |
| 1700 | struct target_sockaddr_nl *target_nl = |
| 1701 | (struct target_sockaddr_nl *)target_saddr; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1702 | target_nl->nl_pid = tswap32(target_nl->nl_pid); |
| 1703 | target_nl->nl_groups = tswap32(target_nl->nl_groups); |
Laurent Vivier | a82ea93 | 2016-06-27 00:18:22 +0200 | [diff] [blame] | 1704 | } else if (addr->sa_family == AF_PACKET) { |
| 1705 | struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr; |
| 1706 | target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex); |
| 1707 | target_ll->sll_hatype = tswap16(target_ll->sll_hatype); |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1708 | } else if (addr->sa_family == AF_INET6 && |
| 1709 | len >= sizeof(struct target_sockaddr_in6)) { |
| 1710 | struct target_sockaddr_in6 *target_in6 = |
| 1711 | (struct target_sockaddr_in6 *)target_saddr; |
| 1712 | target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1713 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1714 | unlock_user(target_saddr, target_addr, len); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1715 | |
| 1716 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1719 | static inline abi_long target_to_host_cmsg(struct msghdr *msgh, |
| 1720 | struct target_msghdr *target_msgh) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1721 | { |
| 1722 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1723 | abi_long msg_controllen; |
| 1724 | abi_ulong target_cmsg_addr; |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1725 | struct target_cmsghdr *target_cmsg, *target_cmsg_start; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1726 | socklen_t space = 0; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1727 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1728 | msg_controllen = tswapal(target_msgh->msg_controllen); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1729 | if (msg_controllen < sizeof (struct target_cmsghdr)) |
| 1730 | goto the_end; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1731 | target_cmsg_addr = tswapal(target_msgh->msg_control); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1732 | target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1733 | target_cmsg_start = target_cmsg; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1734 | if (!target_cmsg) |
| 1735 | return -TARGET_EFAULT; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1736 | |
| 1737 | while (cmsg && target_cmsg) { |
| 1738 | void *data = CMSG_DATA(cmsg); |
| 1739 | void *target_data = TARGET_CMSG_DATA(target_cmsg); |
| 1740 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1741 | int len = tswapal(target_cmsg->cmsg_len) |
Peter Maydell | ad762b9 | 2017-12-15 13:52:56 +0000 | [diff] [blame] | 1742 | - sizeof(struct target_cmsghdr); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1743 | |
| 1744 | space += CMSG_SPACE(len); |
| 1745 | if (space > msgh->msg_controllen) { |
| 1746 | space -= CMSG_SPACE(len); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1747 | /* This is a QEMU bug, since we allocated the payload |
| 1748 | * area ourselves (unlike overflow in host-to-target |
| 1749 | * conversion, which is just the guest giving us a buffer |
| 1750 | * that's too small). It can't happen for the payload types |
| 1751 | * we currently support; if it becomes an issue in future |
| 1752 | * we would need to improve our allocation strategy to |
| 1753 | * something more intelligent than "twice the size of the |
| 1754 | * target buffer we're reading from". |
| 1755 | */ |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 1756 | qemu_log_mask(LOG_UNIMP, |
| 1757 | ("Unsupported ancillary data %d/%d: " |
| 1758 | "unhandled msg size\n"), |
| 1759 | tswap32(target_cmsg->cmsg_level), |
| 1760 | tswap32(target_cmsg->cmsg_type)); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1761 | break; |
| 1762 | } |
| 1763 | |
Petar Jovanovic | dbf4f79 | 2013-09-13 19:27:29 +0200 | [diff] [blame] | 1764 | if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) { |
| 1765 | cmsg->cmsg_level = SOL_SOCKET; |
| 1766 | } else { |
| 1767 | cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level); |
| 1768 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1769 | cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type); |
| 1770 | cmsg->cmsg_len = CMSG_LEN(len); |
| 1771 | |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1772 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1773 | int *fd = (int *)data; |
| 1774 | int *target_fd = (int *)target_data; |
| 1775 | int i, numfds = len / sizeof(int); |
| 1776 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1777 | for (i = 0; i < numfds; i++) { |
| 1778 | __get_user(fd[i], target_fd + i); |
| 1779 | } |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1780 | } else if (cmsg->cmsg_level == SOL_SOCKET |
| 1781 | && cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 1782 | struct ucred *cred = (struct ucred *)data; |
| 1783 | struct target_ucred *target_cred = |
| 1784 | (struct target_ucred *)target_data; |
| 1785 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1786 | __get_user(cred->pid, &target_cred->pid); |
| 1787 | __get_user(cred->uid, &target_cred->uid); |
| 1788 | __get_user(cred->gid, &target_cred->gid); |
Helge Deller | 27404b6 | 2022-12-12 18:34:15 +0100 | [diff] [blame] | 1789 | } else if (cmsg->cmsg_level == SOL_ALG) { |
| 1790 | uint32_t *dst = (uint32_t *)data; |
| 1791 | |
| 1792 | memcpy(dst, target_data, len); |
Michael Tokarev | 669dcb6 | 2023-08-23 08:53:30 +0200 | [diff] [blame] | 1793 | /* fix endianness of first 32-bit word */ |
Helge Deller | 27404b6 | 2022-12-12 18:34:15 +0100 | [diff] [blame] | 1794 | if (len >= sizeof(uint32_t)) { |
| 1795 | *dst = tswap32(*dst); |
| 1796 | } |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1797 | } else { |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 1798 | qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n", |
| 1799 | cmsg->cmsg_level, cmsg->cmsg_type); |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1800 | memcpy(data, target_data, len); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | cmsg = CMSG_NXTHDR(msgh, cmsg); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1804 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, |
| 1805 | target_cmsg_start); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1806 | } |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1807 | unlock_user(target_cmsg, target_cmsg_addr, 0); |
| 1808 | the_end: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1809 | msgh->msg_controllen = space; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1810 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1813 | static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh, |
| 1814 | struct msghdr *msgh) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1815 | { |
| 1816 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1817 | abi_long msg_controllen; |
| 1818 | abi_ulong target_cmsg_addr; |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1819 | struct target_cmsghdr *target_cmsg, *target_cmsg_start; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1820 | socklen_t space = 0; |
| 1821 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1822 | msg_controllen = tswapal(target_msgh->msg_controllen); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1823 | if (msg_controllen < sizeof (struct target_cmsghdr)) |
| 1824 | goto the_end; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1825 | target_cmsg_addr = tswapal(target_msgh->msg_control); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1826 | target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1827 | target_cmsg_start = target_cmsg; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1828 | if (!target_cmsg) |
| 1829 | return -TARGET_EFAULT; |
| 1830 | |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1831 | while (cmsg && target_cmsg) { |
| 1832 | void *data = CMSG_DATA(cmsg); |
| 1833 | void *target_data = TARGET_CMSG_DATA(target_cmsg); |
| 1834 | |
Peter Maydell | ad762b9 | 2017-12-15 13:52:56 +0000 | [diff] [blame] | 1835 | int len = cmsg->cmsg_len - sizeof(struct cmsghdr); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1836 | int tgt_len, tgt_space; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1837 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1838 | /* We never copy a half-header but may copy half-data; |
| 1839 | * this is Linux's behaviour in put_cmsg(). Note that |
| 1840 | * truncation here is a guest problem (which we report |
| 1841 | * to the guest via the CTRUNC bit), unlike truncation |
| 1842 | * in target_to_host_cmsg, which is a QEMU bug. |
| 1843 | */ |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1844 | if (msg_controllen < sizeof(struct target_cmsghdr)) { |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1845 | target_msgh->msg_flags |= tswap32(MSG_CTRUNC); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1846 | break; |
| 1847 | } |
| 1848 | |
Petar Jovanovic | dbf4f79 | 2013-09-13 19:27:29 +0200 | [diff] [blame] | 1849 | if (cmsg->cmsg_level == SOL_SOCKET) { |
| 1850 | target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET); |
| 1851 | } else { |
| 1852 | target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level); |
| 1853 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1854 | target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1855 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1856 | /* Payload types which need a different size of payload on |
| 1857 | * the target must adjust tgt_len here. |
| 1858 | */ |
Peter Maydell | 309786c | 2018-05-18 19:47:15 +0100 | [diff] [blame] | 1859 | tgt_len = len; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1860 | switch (cmsg->cmsg_level) { |
| 1861 | case SOL_SOCKET: |
| 1862 | switch (cmsg->cmsg_type) { |
| 1863 | case SO_TIMESTAMP: |
| 1864 | tgt_len = sizeof(struct target_timeval); |
| 1865 | break; |
| 1866 | default: |
| 1867 | break; |
| 1868 | } |
Peter Maydell | 309786c | 2018-05-18 19:47:15 +0100 | [diff] [blame] | 1869 | break; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1870 | default: |
| 1871 | break; |
| 1872 | } |
| 1873 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1874 | if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) { |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1875 | target_msgh->msg_flags |= tswap32(MSG_CTRUNC); |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1876 | tgt_len = msg_controllen - sizeof(struct target_cmsghdr); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /* We must now copy-and-convert len bytes of payload |
| 1880 | * into tgt_len bytes of destination space. Bear in mind |
| 1881 | * that in both source and destination we may be dealing |
| 1882 | * with a truncated value! |
| 1883 | */ |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1884 | switch (cmsg->cmsg_level) { |
| 1885 | case SOL_SOCKET: |
| 1886 | switch (cmsg->cmsg_type) { |
| 1887 | case SCM_RIGHTS: |
| 1888 | { |
| 1889 | int *fd = (int *)data; |
| 1890 | int *target_fd = (int *)target_data; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1891 | int i, numfds = tgt_len / sizeof(int); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1892 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1893 | for (i = 0; i < numfds; i++) { |
| 1894 | __put_user(fd[i], target_fd + i); |
| 1895 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1896 | break; |
| 1897 | } |
| 1898 | case SO_TIMESTAMP: |
| 1899 | { |
| 1900 | struct timeval *tv = (struct timeval *)data; |
| 1901 | struct target_timeval *target_tv = |
| 1902 | (struct target_timeval *)target_data; |
Jing Huang | aebf5bc | 2012-07-24 14:01:42 +0000 | [diff] [blame] | 1903 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1904 | if (len != sizeof(struct timeval) || |
| 1905 | tgt_len != sizeof(struct target_timeval)) { |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1906 | goto unimplemented; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1907 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1908 | |
| 1909 | /* copy struct timeval to target */ |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1910 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1911 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1912 | break; |
| 1913 | } |
Huw Davies | 4bc2975 | 2014-04-17 14:02:48 +0100 | [diff] [blame] | 1914 | case SCM_CREDENTIALS: |
| 1915 | { |
| 1916 | struct ucred *cred = (struct ucred *)data; |
| 1917 | struct target_ucred *target_cred = |
| 1918 | (struct target_ucred *)target_data; |
| 1919 | |
| 1920 | __put_user(cred->pid, &target_cred->pid); |
| 1921 | __put_user(cred->uid, &target_cred->uid); |
| 1922 | __put_user(cred->gid, &target_cred->gid); |
| 1923 | break; |
| 1924 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1925 | default: |
| 1926 | goto unimplemented; |
| 1927 | } |
| 1928 | break; |
| 1929 | |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1930 | case SOL_IP: |
| 1931 | switch (cmsg->cmsg_type) { |
| 1932 | case IP_TTL: |
| 1933 | { |
| 1934 | uint32_t *v = (uint32_t *)data; |
| 1935 | uint32_t *t_int = (uint32_t *)target_data; |
| 1936 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1937 | if (len != sizeof(uint32_t) || |
| 1938 | tgt_len != sizeof(uint32_t)) { |
| 1939 | goto unimplemented; |
| 1940 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1941 | __put_user(*v, t_int); |
| 1942 | break; |
| 1943 | } |
| 1944 | case IP_RECVERR: |
| 1945 | { |
| 1946 | struct errhdr_t { |
| 1947 | struct sock_extended_err ee; |
| 1948 | struct sockaddr_in offender; |
| 1949 | }; |
| 1950 | struct errhdr_t *errh = (struct errhdr_t *)data; |
| 1951 | struct errhdr_t *target_errh = |
| 1952 | (struct errhdr_t *)target_data; |
| 1953 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1954 | if (len != sizeof(struct errhdr_t) || |
| 1955 | tgt_len != sizeof(struct errhdr_t)) { |
| 1956 | goto unimplemented; |
| 1957 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1958 | __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno); |
| 1959 | __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin); |
| 1960 | __put_user(errh->ee.ee_type, &target_errh->ee.ee_type); |
| 1961 | __put_user(errh->ee.ee_code, &target_errh->ee.ee_code); |
| 1962 | __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad); |
| 1963 | __put_user(errh->ee.ee_info, &target_errh->ee.ee_info); |
| 1964 | __put_user(errh->ee.ee_data, &target_errh->ee.ee_data); |
| 1965 | host_to_target_sockaddr((unsigned long) &target_errh->offender, |
| 1966 | (void *) &errh->offender, sizeof(errh->offender)); |
| 1967 | break; |
| 1968 | } |
| 1969 | default: |
| 1970 | goto unimplemented; |
| 1971 | } |
| 1972 | break; |
| 1973 | |
| 1974 | case SOL_IPV6: |
| 1975 | switch (cmsg->cmsg_type) { |
| 1976 | case IPV6_HOPLIMIT: |
| 1977 | { |
| 1978 | uint32_t *v = (uint32_t *)data; |
| 1979 | uint32_t *t_int = (uint32_t *)target_data; |
| 1980 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1981 | if (len != sizeof(uint32_t) || |
| 1982 | tgt_len != sizeof(uint32_t)) { |
| 1983 | goto unimplemented; |
| 1984 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1985 | __put_user(*v, t_int); |
| 1986 | break; |
| 1987 | } |
| 1988 | case IPV6_RECVERR: |
| 1989 | { |
| 1990 | struct errhdr6_t { |
| 1991 | struct sock_extended_err ee; |
| 1992 | struct sockaddr_in6 offender; |
| 1993 | }; |
| 1994 | struct errhdr6_t *errh = (struct errhdr6_t *)data; |
| 1995 | struct errhdr6_t *target_errh = |
| 1996 | (struct errhdr6_t *)target_data; |
| 1997 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1998 | if (len != sizeof(struct errhdr6_t) || |
| 1999 | tgt_len != sizeof(struct errhdr6_t)) { |
| 2000 | goto unimplemented; |
| 2001 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2002 | __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno); |
| 2003 | __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin); |
| 2004 | __put_user(errh->ee.ee_type, &target_errh->ee.ee_type); |
| 2005 | __put_user(errh->ee.ee_code, &target_errh->ee.ee_code); |
| 2006 | __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad); |
| 2007 | __put_user(errh->ee.ee_info, &target_errh->ee.ee_info); |
| 2008 | __put_user(errh->ee.ee_data, &target_errh->ee.ee_data); |
| 2009 | host_to_target_sockaddr((unsigned long) &target_errh->offender, |
| 2010 | (void *) &errh->offender, sizeof(errh->offender)); |
| 2011 | break; |
| 2012 | } |
| 2013 | default: |
| 2014 | goto unimplemented; |
| 2015 | } |
| 2016 | break; |
| 2017 | |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 2018 | default: |
| 2019 | unimplemented: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 2020 | qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n", |
| 2021 | cmsg->cmsg_level, cmsg->cmsg_type); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 2022 | memcpy(target_data, data, MIN(len, tgt_len)); |
| 2023 | if (tgt_len > len) { |
| 2024 | memset(target_data + len, 0, tgt_len - len); |
| 2025 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 2028 | target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len)); |
| 2029 | tgt_space = TARGET_CMSG_SPACE(tgt_len); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 2030 | if (msg_controllen < tgt_space) { |
| 2031 | tgt_space = msg_controllen; |
| 2032 | } |
| 2033 | msg_controllen -= tgt_space; |
| 2034 | space += tgt_space; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2035 | cmsg = CMSG_NXTHDR(msgh, cmsg); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 2036 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, |
| 2037 | target_cmsg_start); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2038 | } |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 2039 | unlock_user(target_cmsg, target_cmsg_addr, space); |
| 2040 | the_end: |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 2041 | target_msgh->msg_controllen = tswapal(space); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 2042 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2045 | /* do_setsockopt() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2046 | static abi_long do_setsockopt(int sockfd, int level, int optname, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2047 | abi_ulong optval_addr, socklen_t optlen) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2048 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2049 | abi_long ret; |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 2050 | int val; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2051 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2052 | switch(level) { |
| 2053 | case SOL_TCP: |
Shu-Chun Weng | fe51b0a | 2020-12-18 11:32:11 -0800 | [diff] [blame] | 2054 | case SOL_UDP: |
| 2055 | /* TCP and UDP options all take an 'int' value. */ |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2056 | if (optlen < sizeof(uint32_t)) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2057 | return -TARGET_EINVAL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2058 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2059 | if (get_user_u32(val, optval_addr)) |
| 2060 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2061 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
| 2062 | break; |
| 2063 | case SOL_IP: |
| 2064 | switch(optname) { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2065 | case IP_TOS: |
| 2066 | case IP_TTL: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2067 | case IP_HDRINCL: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2068 | case IP_ROUTER_ALERT: |
| 2069 | case IP_RECVOPTS: |
| 2070 | case IP_RETOPTS: |
| 2071 | case IP_PKTINFO: |
| 2072 | case IP_MTU_DISCOVER: |
| 2073 | case IP_RECVERR: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2074 | case IP_RECVTTL: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2075 | case IP_RECVTOS: |
| 2076 | #ifdef IP_FREEBIND |
| 2077 | case IP_FREEBIND: |
| 2078 | #endif |
| 2079 | case IP_MULTICAST_TTL: |
| 2080 | case IP_MULTICAST_LOOP: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2081 | val = 0; |
| 2082 | if (optlen >= sizeof(uint32_t)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2083 | if (get_user_u32(val, optval_addr)) |
| 2084 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2085 | } else if (optlen >= 1) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2086 | if (get_user_u8(val, optval_addr)) |
| 2087 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2088 | } |
| 2089 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
| 2090 | break; |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2091 | case IP_ADD_MEMBERSHIP: |
| 2092 | case IP_DROP_MEMBERSHIP: |
Michael Tokarev | 124a134 | 2024-03-31 13:07:35 +0300 | [diff] [blame] | 2093 | { |
| 2094 | struct ip_mreqn ip_mreq; |
| 2095 | struct target_ip_mreqn *target_smreqn; |
| 2096 | |
| 2097 | QEMU_BUILD_BUG_ON(sizeof(struct ip_mreq) != |
| 2098 | sizeof(struct target_ip_mreq)); |
| 2099 | |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2100 | if (optlen < sizeof (struct target_ip_mreq) || |
Michael Tokarev | 124a134 | 2024-03-31 13:07:35 +0300 | [diff] [blame] | 2101 | optlen > sizeof (struct target_ip_mreqn)) { |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2102 | return -TARGET_EINVAL; |
Michael Tokarev | 124a134 | 2024-03-31 13:07:35 +0300 | [diff] [blame] | 2103 | } |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2104 | |
Michael Tokarev | 124a134 | 2024-03-31 13:07:35 +0300 | [diff] [blame] | 2105 | target_smreqn = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
| 2106 | if (!target_smreqn) { |
| 2107 | return -TARGET_EFAULT; |
| 2108 | } |
| 2109 | ip_mreq.imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr; |
| 2110 | ip_mreq.imr_address.s_addr = target_smreqn->imr_address.s_addr; |
| 2111 | if (optlen == sizeof(struct target_ip_mreqn)) { |
| 2112 | ip_mreq.imr_ifindex = tswapal(target_smreqn->imr_ifindex); |
| 2113 | optlen = sizeof(struct ip_mreqn); |
| 2114 | } |
| 2115 | unlock_user(target_smreqn, optval_addr, 0); |
| 2116 | |
| 2117 | ret = get_errno(setsockopt(sockfd, level, optname, &ip_mreq, optlen)); |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2118 | break; |
Michael Tokarev | 124a134 | 2024-03-31 13:07:35 +0300 | [diff] [blame] | 2119 | } |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 2120 | case IP_BLOCK_SOURCE: |
| 2121 | case IP_UNBLOCK_SOURCE: |
| 2122 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 2123 | case IP_DROP_SOURCE_MEMBERSHIP: |
Michael Tokarev | 166bd92 | 2024-03-31 13:07:36 +0300 | [diff] [blame] | 2124 | { |
| 2125 | struct ip_mreq_source *ip_mreq_source; |
| 2126 | |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 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 Maydell | 74e43b0 | 2021-08-09 16:54:24 +0100 | [diff] [blame] | 2131 | if (!ip_mreq_source) { |
| 2132 | return -TARGET_EFAULT; |
| 2133 | } |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 2134 | ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen)); |
| 2135 | unlock_user (ip_mreq_source, optval_addr, 0); |
| 2136 | break; |
Michael Tokarev | 166bd92 | 2024-03-31 13:07:36 +0300 | [diff] [blame] | 2137 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2138 | default: |
| 2139 | goto unimplemented; |
| 2140 | } |
| 2141 | break; |
Laurent Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2142 | case SOL_IPV6: |
| 2143 | switch (optname) { |
| 2144 | case IPV6_MTU_DISCOVER: |
| 2145 | case IPV6_MTU: |
| 2146 | case IPV6_V6ONLY: |
| 2147 | case IPV6_RECVPKTINFO: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2148 | case IPV6_UNICAST_HOPS: |
Laurent Vivier | 21749c4 | 2018-06-27 23:21:52 +0200 | [diff] [blame] | 2149 | case IPV6_MULTICAST_HOPS: |
| 2150 | case IPV6_MULTICAST_LOOP: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2151 | case IPV6_RECVERR: |
| 2152 | case IPV6_RECVHOPLIMIT: |
| 2153 | case IPV6_2292HOPLIMIT: |
| 2154 | case IPV6_CHECKSUM: |
Helge Deller | b9cce6d | 2019-03-10 12:12:16 +0100 | [diff] [blame] | 2155 | case IPV6_ADDRFORM: |
| 2156 | case IPV6_2292PKTINFO: |
| 2157 | case IPV6_RECVTCLASS: |
| 2158 | case IPV6_RECVRTHDR: |
| 2159 | case IPV6_2292RTHDR: |
| 2160 | case IPV6_RECVHOPOPTS: |
| 2161 | case IPV6_2292HOPOPTS: |
| 2162 | case IPV6_RECVDSTOPTS: |
| 2163 | case IPV6_2292DSTOPTS: |
| 2164 | case IPV6_TCLASS: |
Shu-Chun Weng | 22db121 | 2020-12-18 11:32:12 -0800 | [diff] [blame] | 2165 | case IPV6_ADDR_PREFERENCES: |
Helge Deller | b9cce6d | 2019-03-10 12:12:16 +0100 | [diff] [blame] | 2166 | #ifdef IPV6_RECVPATHMTU |
| 2167 | case IPV6_RECVPATHMTU: |
| 2168 | #endif |
| 2169 | #ifdef IPV6_TRANSPARENT |
| 2170 | case IPV6_TRANSPARENT: |
| 2171 | #endif |
| 2172 | #ifdef IPV6_FREEBIND |
| 2173 | case IPV6_FREEBIND: |
| 2174 | #endif |
| 2175 | #ifdef IPV6_RECVORIGDSTADDR |
| 2176 | case IPV6_RECVORIGDSTADDR: |
| 2177 | #endif |
Laurent Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2178 | 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 Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2188 | 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 Chen | 22bf4ee | 2019-06-19 16:17:10 +0200 | [diff] [blame] | 2206 | 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 Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2225 | 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 Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2251 | default: |
| 2252 | goto unimplemented; |
| 2253 | } |
| 2254 | break; |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 2255 | case SOL_RAW: |
| 2256 | switch (optname) { |
| 2257 | case ICMP_FILTER: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2258 | case IPV6_CHECKSUM: |
| 2259 | /* those take an u32 value */ |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 2260 | 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 Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 2275 | #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE) |
| 2276 | case SOL_ALG: |
| 2277 | switch (optname) { |
| 2278 | case ALG_SET_KEY: |
| 2279 | { |
Michael Tokarev | 04f6fb8 | 2024-03-31 13:07:34 +0300 | [diff] [blame] | 2280 | char *alg_key = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
Yunqiang Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 2281 | if (!alg_key) { |
Yunqiang Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 2282 | return -TARGET_EFAULT; |
| 2283 | } |
| 2284 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2285 | alg_key, optlen)); |
Michael Tokarev | 04f6fb8 | 2024-03-31 13:07:34 +0300 | [diff] [blame] | 2286 | unlock_user(alg_key, optval_addr, optlen); |
Yunqiang Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 2287 | break; |
| 2288 | } |
| 2289 | case ALG_SET_AEAD_AUTHSIZE: |
| 2290 | { |
| 2291 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2292 | NULL, optlen)); |
| 2293 | break; |
| 2294 | } |
| 2295 | default: |
| 2296 | goto unimplemented; |
| 2297 | } |
| 2298 | break; |
| 2299 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2300 | case TARGET_SOL_SOCKET: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2301 | switch (optname) { |
Laurent Vivier | 1b09aeb | 2013-01-01 08:24:11 +0000 | [diff] [blame] | 2302 | case TARGET_SO_RCVTIMEO: |
Michael Tokarev | 88a722b | 2024-03-31 13:07:37 +0300 | [diff] [blame] | 2303 | case TARGET_SO_SNDTIMEO: |
Laurent Vivier | 1b09aeb | 2013-01-01 08:24:11 +0000 | [diff] [blame] | 2304 | { |
| 2305 | struct timeval tv; |
| 2306 | |
Laurent Vivier | 1b09aeb | 2013-01-01 08:24:11 +0000 | [diff] [blame] | 2307 | if (optlen != sizeof(struct target_timeval)) { |
| 2308 | return -TARGET_EINVAL; |
| 2309 | } |
| 2310 | |
| 2311 | if (copy_from_user_timeval(&tv, optval_addr)) { |
| 2312 | return -TARGET_EFAULT; |
| 2313 | } |
| 2314 | |
Michael Tokarev | 88a722b | 2024-03-31 13:07:37 +0300 | [diff] [blame] | 2315 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, |
| 2316 | optname == TARGET_SO_RCVTIMEO ? |
| 2317 | SO_RCVTIMEO : SO_SNDTIMEO, |
Laurent Vivier | 1b09aeb | 2013-01-01 08:24:11 +0000 | [diff] [blame] | 2318 | &tv, sizeof(tv))); |
| 2319 | return ret; |
| 2320 | } |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2321 | case TARGET_SO_ATTACH_FILTER: |
| 2322 | { |
| 2323 | struct target_sock_fprog *tfprog; |
| 2324 | struct target_sock_filter *tfilter; |
| 2325 | struct sock_fprog fprog; |
| 2326 | struct sock_filter *filter; |
| 2327 | int i; |
| 2328 | |
| 2329 | if (optlen != sizeof(*tfprog)) { |
| 2330 | return -TARGET_EINVAL; |
| 2331 | } |
| 2332 | if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) { |
| 2333 | return -TARGET_EFAULT; |
| 2334 | } |
| 2335 | if (!lock_user_struct(VERIFY_READ, tfilter, |
| 2336 | tswapal(tfprog->filter), 0)) { |
| 2337 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2338 | return -TARGET_EFAULT; |
| 2339 | } |
| 2340 | |
| 2341 | fprog.len = tswap16(tfprog->len); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2342 | filter = g_try_new(struct sock_filter, fprog.len); |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2343 | if (filter == NULL) { |
| 2344 | unlock_user_struct(tfilter, tfprog->filter, 1); |
| 2345 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2346 | return -TARGET_ENOMEM; |
| 2347 | } |
| 2348 | for (i = 0; i < fprog.len; i++) { |
| 2349 | filter[i].code = tswap16(tfilter[i].code); |
| 2350 | filter[i].jt = tfilter[i].jt; |
| 2351 | filter[i].jf = tfilter[i].jf; |
| 2352 | filter[i].k = tswap32(tfilter[i].k); |
| 2353 | } |
| 2354 | fprog.filter = filter; |
| 2355 | |
| 2356 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, |
| 2357 | SO_ATTACH_FILTER, &fprog, sizeof(fprog))); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2358 | g_free(filter); |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2359 | |
| 2360 | unlock_user_struct(tfilter, tfprog->filter, 1); |
| 2361 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2362 | return ret; |
| 2363 | } |
Joakim Tjernlund | 451aaf6 | 2014-07-12 15:47:06 +0200 | [diff] [blame] | 2364 | case TARGET_SO_BINDTODEVICE: |
| 2365 | { |
| 2366 | char *dev_ifname, *addr_ifname; |
| 2367 | |
| 2368 | if (optlen > IFNAMSIZ - 1) { |
| 2369 | optlen = IFNAMSIZ - 1; |
| 2370 | } |
| 2371 | dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
| 2372 | if (!dev_ifname) { |
| 2373 | return -TARGET_EFAULT; |
| 2374 | } |
| 2375 | optname = SO_BINDTODEVICE; |
| 2376 | addr_ifname = alloca(IFNAMSIZ); |
| 2377 | memcpy(addr_ifname, dev_ifname, optlen); |
| 2378 | addr_ifname[optlen] = 0; |
Chen Gang | fad6c58 | 2016-01-11 13:58:50 +0800 | [diff] [blame] | 2379 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, |
| 2380 | addr_ifname, optlen)); |
Joakim Tjernlund | 451aaf6 | 2014-07-12 15:47:06 +0200 | [diff] [blame] | 2381 | unlock_user (dev_ifname, optval_addr, 0); |
| 2382 | return ret; |
| 2383 | } |
Carlo Marcelo Arenas Belón | 83eb6e5 | 2018-08-24 01:56:01 -0700 | [diff] [blame] | 2384 | case TARGET_SO_LINGER: |
| 2385 | { |
| 2386 | struct linger lg; |
| 2387 | struct target_linger *tlg; |
| 2388 | |
| 2389 | if (optlen != sizeof(struct target_linger)) { |
| 2390 | return -TARGET_EINVAL; |
| 2391 | } |
| 2392 | if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) { |
| 2393 | return -TARGET_EFAULT; |
| 2394 | } |
| 2395 | __get_user(lg.l_onoff, &tlg->l_onoff); |
| 2396 | __get_user(lg.l_linger, &tlg->l_linger); |
| 2397 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER, |
| 2398 | &lg, sizeof(lg))); |
| 2399 | unlock_user_struct(tlg, optval_addr, 0); |
| 2400 | return ret; |
| 2401 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2402 | /* Options with 'int' argument. */ |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2403 | case TARGET_SO_DEBUG: |
| 2404 | optname = SO_DEBUG; |
| 2405 | break; |
| 2406 | case TARGET_SO_REUSEADDR: |
| 2407 | optname = SO_REUSEADDR; |
| 2408 | break; |
Yunqiang Su | 113a9dd | 2018-10-30 13:55:08 +0100 | [diff] [blame] | 2409 | #ifdef SO_REUSEPORT |
| 2410 | case TARGET_SO_REUSEPORT: |
| 2411 | optname = SO_REUSEPORT; |
| 2412 | break; |
| 2413 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2414 | case TARGET_SO_TYPE: |
| 2415 | optname = SO_TYPE; |
| 2416 | break; |
| 2417 | case TARGET_SO_ERROR: |
| 2418 | optname = SO_ERROR; |
| 2419 | break; |
| 2420 | case TARGET_SO_DONTROUTE: |
| 2421 | optname = SO_DONTROUTE; |
| 2422 | break; |
| 2423 | case TARGET_SO_BROADCAST: |
| 2424 | optname = SO_BROADCAST; |
| 2425 | break; |
| 2426 | case TARGET_SO_SNDBUF: |
| 2427 | optname = SO_SNDBUF; |
| 2428 | break; |
Paul Burton | d79b6cc | 2014-06-22 11:25:35 +0100 | [diff] [blame] | 2429 | case TARGET_SO_SNDBUFFORCE: |
| 2430 | optname = SO_SNDBUFFORCE; |
| 2431 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2432 | case TARGET_SO_RCVBUF: |
| 2433 | optname = SO_RCVBUF; |
| 2434 | break; |
Paul Burton | d79b6cc | 2014-06-22 11:25:35 +0100 | [diff] [blame] | 2435 | case TARGET_SO_RCVBUFFORCE: |
| 2436 | optname = SO_RCVBUFFORCE; |
| 2437 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2438 | case TARGET_SO_KEEPALIVE: |
| 2439 | optname = SO_KEEPALIVE; |
| 2440 | break; |
| 2441 | case TARGET_SO_OOBINLINE: |
| 2442 | optname = SO_OOBINLINE; |
| 2443 | break; |
| 2444 | case TARGET_SO_NO_CHECK: |
| 2445 | optname = SO_NO_CHECK; |
| 2446 | break; |
| 2447 | case TARGET_SO_PRIORITY: |
| 2448 | optname = SO_PRIORITY; |
| 2449 | break; |
bellard | 5e83e8e | 2005-03-01 22:32:06 +0000 | [diff] [blame] | 2450 | #ifdef SO_BSDCOMPAT |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2451 | case TARGET_SO_BSDCOMPAT: |
| 2452 | optname = SO_BSDCOMPAT; |
| 2453 | break; |
bellard | 5e83e8e | 2005-03-01 22:32:06 +0000 | [diff] [blame] | 2454 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2455 | case TARGET_SO_PASSCRED: |
| 2456 | optname = SO_PASSCRED; |
| 2457 | break; |
Paul Burton | 82d0fe6 | 2014-06-22 11:25:36 +0100 | [diff] [blame] | 2458 | case TARGET_SO_PASSSEC: |
| 2459 | optname = SO_PASSSEC; |
| 2460 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2461 | case TARGET_SO_TIMESTAMP: |
| 2462 | optname = SO_TIMESTAMP; |
| 2463 | break; |
| 2464 | case TARGET_SO_RCVLOWAT: |
| 2465 | optname = SO_RCVLOWAT; |
| 2466 | break; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2467 | default: |
| 2468 | goto unimplemented; |
| 2469 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2470 | if (optlen < sizeof(uint32_t)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2471 | return -TARGET_EINVAL; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2472 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2473 | if (get_user_u32(val, optval_addr)) |
| 2474 | return -TARGET_EFAULT; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2475 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val))); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2476 | break; |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2477 | #ifdef SOL_NETLINK |
| 2478 | case SOL_NETLINK: |
| 2479 | switch (optname) { |
| 2480 | case NETLINK_PKTINFO: |
| 2481 | case NETLINK_ADD_MEMBERSHIP: |
| 2482 | case NETLINK_DROP_MEMBERSHIP: |
| 2483 | case NETLINK_BROADCAST_ERROR: |
| 2484 | case NETLINK_NO_ENOBUFS: |
| 2485 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 2486 | case NETLINK_LISTEN_ALL_NSID: |
| 2487 | case NETLINK_CAP_ACK: |
| 2488 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ |
| 2489 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) |
| 2490 | case NETLINK_EXT_ACK: |
| 2491 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2492 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) |
| 2493 | case NETLINK_GET_STRICT_CHK: |
| 2494 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2495 | break; |
| 2496 | default: |
| 2497 | goto unimplemented; |
| 2498 | } |
| 2499 | val = 0; |
| 2500 | if (optlen < sizeof(uint32_t)) { |
| 2501 | return -TARGET_EINVAL; |
| 2502 | } |
| 2503 | if (get_user_u32(val, optval_addr)) { |
| 2504 | return -TARGET_EFAULT; |
| 2505 | } |
| 2506 | ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val, |
| 2507 | sizeof(val))); |
| 2508 | break; |
| 2509 | #endif /* SOL_NETLINK */ |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2510 | default: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2511 | unimplemented: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 2512 | qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n", |
| 2513 | level, optname); |
ths | 6fa13c1 | 2007-12-18 02:41:04 +0000 | [diff] [blame] | 2514 | ret = -TARGET_ENOPROTOOPT; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2515 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2516 | return ret; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2519 | /* do_getsockopt() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2520 | static abi_long do_getsockopt(int sockfd, int level, int optname, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2521 | abi_ulong optval_addr, abi_ulong optlen) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2522 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2523 | abi_long ret; |
blueswir1 | b55266b | 2008-09-20 08:07:15 +0000 | [diff] [blame] | 2524 | int len, val; |
| 2525 | socklen_t lv; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2526 | |
| 2527 | switch(level) { |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2528 | case TARGET_SOL_SOCKET: |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2529 | level = SOL_SOCKET; |
| 2530 | switch (optname) { |
| 2531 | /* These don't just return a single integer */ |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2532 | case TARGET_SO_PEERNAME: |
| 2533 | goto unimplemented; |
Andreas Schwab | 405dc4c | 2019-05-13 11:06:26 +0200 | [diff] [blame] | 2534 | case TARGET_SO_RCVTIMEO: { |
| 2535 | struct timeval tv; |
| 2536 | socklen_t tvlen; |
| 2537 | |
| 2538 | optname = SO_RCVTIMEO; |
| 2539 | |
| 2540 | get_timeout: |
| 2541 | if (get_user_u32(len, optlen)) { |
| 2542 | return -TARGET_EFAULT; |
| 2543 | } |
| 2544 | if (len < 0) { |
| 2545 | return -TARGET_EINVAL; |
| 2546 | } |
| 2547 | |
| 2548 | tvlen = sizeof(tv); |
| 2549 | ret = get_errno(getsockopt(sockfd, level, optname, |
| 2550 | &tv, &tvlen)); |
| 2551 | if (ret < 0) { |
| 2552 | return ret; |
| 2553 | } |
| 2554 | if (len > sizeof(struct target_timeval)) { |
| 2555 | len = sizeof(struct target_timeval); |
| 2556 | } |
| 2557 | if (copy_to_user_timeval(optval_addr, &tv)) { |
| 2558 | return -TARGET_EFAULT; |
| 2559 | } |
| 2560 | if (put_user_u32(len, optlen)) { |
| 2561 | return -TARGET_EFAULT; |
| 2562 | } |
| 2563 | break; |
| 2564 | } |
| 2565 | case TARGET_SO_SNDTIMEO: |
| 2566 | optname = SO_SNDTIMEO; |
| 2567 | goto get_timeout; |
Akos PASZTORY | 583359a | 2011-11-14 15:09:49 +0200 | [diff] [blame] | 2568 | case TARGET_SO_PEERCRED: { |
| 2569 | struct ucred cr; |
| 2570 | socklen_t crlen; |
| 2571 | struct target_ucred *tcr; |
| 2572 | |
| 2573 | if (get_user_u32(len, optlen)) { |
| 2574 | return -TARGET_EFAULT; |
| 2575 | } |
| 2576 | if (len < 0) { |
| 2577 | return -TARGET_EINVAL; |
| 2578 | } |
| 2579 | |
| 2580 | crlen = sizeof(cr); |
| 2581 | ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED, |
| 2582 | &cr, &crlen)); |
| 2583 | if (ret < 0) { |
| 2584 | return ret; |
| 2585 | } |
| 2586 | if (len > crlen) { |
| 2587 | len = crlen; |
| 2588 | } |
| 2589 | if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) { |
| 2590 | return -TARGET_EFAULT; |
| 2591 | } |
| 2592 | __put_user(cr.pid, &tcr->pid); |
| 2593 | __put_user(cr.uid, &tcr->uid); |
| 2594 | __put_user(cr.gid, &tcr->gid); |
| 2595 | unlock_user_struct(tcr, optval_addr, 1); |
| 2596 | if (put_user_u32(len, optlen)) { |
| 2597 | return -TARGET_EFAULT; |
| 2598 | } |
| 2599 | break; |
| 2600 | } |
Laurent Vivier | 6d485a5 | 2020-02-04 22:19:01 +0100 | [diff] [blame] | 2601 | case TARGET_SO_PEERSEC: { |
| 2602 | char *name; |
| 2603 | |
| 2604 | if (get_user_u32(len, optlen)) { |
| 2605 | return -TARGET_EFAULT; |
| 2606 | } |
| 2607 | if (len < 0) { |
| 2608 | return -TARGET_EINVAL; |
| 2609 | } |
| 2610 | name = lock_user(VERIFY_WRITE, optval_addr, len, 0); |
| 2611 | if (!name) { |
| 2612 | return -TARGET_EFAULT; |
| 2613 | } |
| 2614 | lv = len; |
| 2615 | ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC, |
| 2616 | name, &lv)); |
| 2617 | if (put_user_u32(lv, optlen)) { |
| 2618 | ret = -TARGET_EFAULT; |
| 2619 | } |
| 2620 | unlock_user(name, optval_addr, lv); |
| 2621 | break; |
| 2622 | } |
Carlo Marcelo Arenas Belón | 83eb6e5 | 2018-08-24 01:56:01 -0700 | [diff] [blame] | 2623 | case TARGET_SO_LINGER: |
| 2624 | { |
| 2625 | struct linger lg; |
| 2626 | socklen_t lglen; |
| 2627 | struct target_linger *tlg; |
| 2628 | |
| 2629 | if (get_user_u32(len, optlen)) { |
| 2630 | return -TARGET_EFAULT; |
| 2631 | } |
| 2632 | if (len < 0) { |
| 2633 | return -TARGET_EINVAL; |
| 2634 | } |
| 2635 | |
| 2636 | lglen = sizeof(lg); |
| 2637 | ret = get_errno(getsockopt(sockfd, level, SO_LINGER, |
| 2638 | &lg, &lglen)); |
| 2639 | if (ret < 0) { |
| 2640 | return ret; |
| 2641 | } |
| 2642 | if (len > lglen) { |
| 2643 | len = lglen; |
| 2644 | } |
| 2645 | if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) { |
| 2646 | return -TARGET_EFAULT; |
| 2647 | } |
| 2648 | __put_user(lg.l_onoff, &tlg->l_onoff); |
| 2649 | __put_user(lg.l_linger, &tlg->l_linger); |
| 2650 | unlock_user_struct(tlg, optval_addr, 1); |
| 2651 | if (put_user_u32(len, optlen)) { |
| 2652 | return -TARGET_EFAULT; |
| 2653 | } |
| 2654 | break; |
| 2655 | } |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2656 | /* Options with 'int' argument. */ |
| 2657 | case TARGET_SO_DEBUG: |
| 2658 | optname = SO_DEBUG; |
| 2659 | goto int_case; |
| 2660 | case TARGET_SO_REUSEADDR: |
| 2661 | optname = SO_REUSEADDR; |
| 2662 | goto int_case; |
Yunqiang Su | 113a9dd | 2018-10-30 13:55:08 +0100 | [diff] [blame] | 2663 | #ifdef SO_REUSEPORT |
| 2664 | case TARGET_SO_REUSEPORT: |
| 2665 | optname = SO_REUSEPORT; |
| 2666 | goto int_case; |
| 2667 | #endif |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2668 | case TARGET_SO_TYPE: |
| 2669 | optname = SO_TYPE; |
| 2670 | goto int_case; |
| 2671 | case TARGET_SO_ERROR: |
| 2672 | optname = SO_ERROR; |
| 2673 | goto int_case; |
| 2674 | case TARGET_SO_DONTROUTE: |
| 2675 | optname = SO_DONTROUTE; |
| 2676 | goto int_case; |
| 2677 | case TARGET_SO_BROADCAST: |
| 2678 | optname = SO_BROADCAST; |
| 2679 | goto int_case; |
| 2680 | case TARGET_SO_SNDBUF: |
| 2681 | optname = SO_SNDBUF; |
| 2682 | goto int_case; |
| 2683 | case TARGET_SO_RCVBUF: |
| 2684 | optname = SO_RCVBUF; |
| 2685 | goto int_case; |
| 2686 | case TARGET_SO_KEEPALIVE: |
| 2687 | optname = SO_KEEPALIVE; |
| 2688 | goto int_case; |
| 2689 | case TARGET_SO_OOBINLINE: |
| 2690 | optname = SO_OOBINLINE; |
| 2691 | goto int_case; |
| 2692 | case TARGET_SO_NO_CHECK: |
| 2693 | optname = SO_NO_CHECK; |
| 2694 | goto int_case; |
| 2695 | case TARGET_SO_PRIORITY: |
| 2696 | optname = SO_PRIORITY; |
| 2697 | goto int_case; |
| 2698 | #ifdef SO_BSDCOMPAT |
| 2699 | case TARGET_SO_BSDCOMPAT: |
| 2700 | optname = SO_BSDCOMPAT; |
| 2701 | goto int_case; |
| 2702 | #endif |
| 2703 | case TARGET_SO_PASSCRED: |
| 2704 | optname = SO_PASSCRED; |
| 2705 | goto int_case; |
| 2706 | case TARGET_SO_TIMESTAMP: |
| 2707 | optname = SO_TIMESTAMP; |
| 2708 | goto int_case; |
| 2709 | case TARGET_SO_RCVLOWAT: |
| 2710 | optname = SO_RCVLOWAT; |
| 2711 | goto int_case; |
Paul Burton | aec1ca4 | 2014-06-22 11:25:34 +0100 | [diff] [blame] | 2712 | case TARGET_SO_ACCEPTCONN: |
| 2713 | optname = SO_ACCEPTCONN; |
| 2714 | goto int_case; |
Jason A. Donenfeld | ec63e06 | 2021-02-04 16:39:25 +0100 | [diff] [blame] | 2715 | case TARGET_SO_PROTOCOL: |
| 2716 | optname = SO_PROTOCOL; |
| 2717 | goto int_case; |
| 2718 | case TARGET_SO_DOMAIN: |
| 2719 | optname = SO_DOMAIN; |
| 2720 | goto int_case; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2721 | default: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2722 | goto int_case; |
| 2723 | } |
| 2724 | break; |
| 2725 | case SOL_TCP: |
Shu-Chun Weng | fe51b0a | 2020-12-18 11:32:11 -0800 | [diff] [blame] | 2726 | case SOL_UDP: |
| 2727 | /* TCP and UDP options all take an 'int' value. */ |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2728 | int_case: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2729 | if (get_user_u32(len, optlen)) |
| 2730 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2731 | if (len < 0) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2732 | return -TARGET_EINVAL; |
Mike Frysinger | 73160d9 | 2011-02-07 01:05:49 -0500 | [diff] [blame] | 2733 | lv = sizeof(lv); |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2734 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2735 | if (ret < 0) |
| 2736 | return ret; |
Helge Deller | cb88b7c | 2023-01-27 21:25:27 +0100 | [diff] [blame] | 2737 | switch (optname) { |
| 2738 | case SO_TYPE: |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 2739 | val = host_to_target_sock_type(val); |
Helge Deller | cb88b7c | 2023-01-27 21:25:27 +0100 | [diff] [blame] | 2740 | break; |
| 2741 | case SO_ERROR: |
| 2742 | val = host_to_target_errno(val); |
| 2743 | break; |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 2744 | } |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2745 | if (len > lv) |
| 2746 | len = lv; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2747 | if (len == 4) { |
| 2748 | if (put_user_u32(val, optval_addr)) |
| 2749 | return -TARGET_EFAULT; |
| 2750 | } else { |
| 2751 | if (put_user_u8(val, optval_addr)) |
| 2752 | return -TARGET_EFAULT; |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2753 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2754 | if (put_user_u32(len, optlen)) |
| 2755 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | case SOL_IP: |
| 2758 | switch(optname) { |
| 2759 | case IP_TOS: |
| 2760 | case IP_TTL: |
| 2761 | case IP_HDRINCL: |
| 2762 | case IP_ROUTER_ALERT: |
| 2763 | case IP_RECVOPTS: |
| 2764 | case IP_RETOPTS: |
| 2765 | case IP_PKTINFO: |
| 2766 | case IP_MTU_DISCOVER: |
| 2767 | case IP_RECVERR: |
| 2768 | case IP_RECVTOS: |
| 2769 | #ifdef IP_FREEBIND |
| 2770 | case IP_FREEBIND: |
| 2771 | #endif |
| 2772 | case IP_MULTICAST_TTL: |
| 2773 | case IP_MULTICAST_LOOP: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2774 | if (get_user_u32(len, optlen)) |
| 2775 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2776 | if (len < 0) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2777 | return -TARGET_EINVAL; |
Mike Frysinger | 73160d9 | 2011-02-07 01:05:49 -0500 | [diff] [blame] | 2778 | lv = sizeof(lv); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2779 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2780 | if (ret < 0) |
| 2781 | return ret; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2782 | if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2783 | len = 1; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2784 | if (put_user_u32(len, optlen) |
| 2785 | || put_user_u8(val, optval_addr)) |
| 2786 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2787 | } else { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2788 | if (len > sizeof(int)) |
| 2789 | len = sizeof(int); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2790 | if (put_user_u32(len, optlen) |
| 2791 | || put_user_u32(val, optval_addr)) |
| 2792 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2793 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2794 | break; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2795 | default: |
ths | c02f499 | 2007-12-18 02:39:59 +0000 | [diff] [blame] | 2796 | ret = -TARGET_ENOPROTOOPT; |
| 2797 | break; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2798 | } |
| 2799 | break; |
Tom Deseyn | bd8ed48 | 2018-12-13 14:06:11 +0100 | [diff] [blame] | 2800 | case SOL_IPV6: |
| 2801 | switch (optname) { |
| 2802 | case IPV6_MTU_DISCOVER: |
| 2803 | case IPV6_MTU: |
| 2804 | case IPV6_V6ONLY: |
| 2805 | case IPV6_RECVPKTINFO: |
| 2806 | case IPV6_UNICAST_HOPS: |
| 2807 | case IPV6_MULTICAST_HOPS: |
| 2808 | case IPV6_MULTICAST_LOOP: |
| 2809 | case IPV6_RECVERR: |
| 2810 | case IPV6_RECVHOPLIMIT: |
| 2811 | case IPV6_2292HOPLIMIT: |
| 2812 | case IPV6_CHECKSUM: |
Helge Deller | b9cce6d | 2019-03-10 12:12:16 +0100 | [diff] [blame] | 2813 | case IPV6_ADDRFORM: |
| 2814 | case IPV6_2292PKTINFO: |
| 2815 | case IPV6_RECVTCLASS: |
| 2816 | case IPV6_RECVRTHDR: |
| 2817 | case IPV6_2292RTHDR: |
| 2818 | case IPV6_RECVHOPOPTS: |
| 2819 | case IPV6_2292HOPOPTS: |
| 2820 | case IPV6_RECVDSTOPTS: |
| 2821 | case IPV6_2292DSTOPTS: |
| 2822 | case IPV6_TCLASS: |
Shu-Chun Weng | 22db121 | 2020-12-18 11:32:12 -0800 | [diff] [blame] | 2823 | case IPV6_ADDR_PREFERENCES: |
Helge Deller | b9cce6d | 2019-03-10 12:12:16 +0100 | [diff] [blame] | 2824 | #ifdef IPV6_RECVPATHMTU |
| 2825 | case IPV6_RECVPATHMTU: |
| 2826 | #endif |
| 2827 | #ifdef IPV6_TRANSPARENT |
| 2828 | case IPV6_TRANSPARENT: |
| 2829 | #endif |
| 2830 | #ifdef IPV6_FREEBIND |
| 2831 | case IPV6_FREEBIND: |
| 2832 | #endif |
| 2833 | #ifdef IPV6_RECVORIGDSTADDR |
| 2834 | case IPV6_RECVORIGDSTADDR: |
| 2835 | #endif |
Tom Deseyn | bd8ed48 | 2018-12-13 14:06:11 +0100 | [diff] [blame] | 2836 | if (get_user_u32(len, optlen)) |
| 2837 | return -TARGET_EFAULT; |
| 2838 | if (len < 0) |
| 2839 | return -TARGET_EINVAL; |
| 2840 | lv = sizeof(lv); |
| 2841 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2842 | if (ret < 0) |
| 2843 | return ret; |
| 2844 | if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { |
| 2845 | len = 1; |
| 2846 | if (put_user_u32(len, optlen) |
| 2847 | || put_user_u8(val, optval_addr)) |
| 2848 | return -TARGET_EFAULT; |
| 2849 | } else { |
| 2850 | if (len > sizeof(int)) |
| 2851 | len = sizeof(int); |
| 2852 | if (put_user_u32(len, optlen) |
| 2853 | || put_user_u32(val, optval_addr)) |
| 2854 | return -TARGET_EFAULT; |
| 2855 | } |
| 2856 | break; |
| 2857 | default: |
| 2858 | ret = -TARGET_ENOPROTOOPT; |
| 2859 | break; |
| 2860 | } |
| 2861 | break; |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2862 | #ifdef SOL_NETLINK |
| 2863 | case SOL_NETLINK: |
| 2864 | switch (optname) { |
| 2865 | case NETLINK_PKTINFO: |
| 2866 | case NETLINK_BROADCAST_ERROR: |
| 2867 | case NETLINK_NO_ENOBUFS: |
| 2868 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 2869 | case NETLINK_LISTEN_ALL_NSID: |
| 2870 | case NETLINK_CAP_ACK: |
| 2871 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ |
| 2872 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) |
| 2873 | case NETLINK_EXT_ACK: |
| 2874 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2875 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) |
| 2876 | case NETLINK_GET_STRICT_CHK: |
| 2877 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2878 | if (get_user_u32(len, optlen)) { |
| 2879 | return -TARGET_EFAULT; |
| 2880 | } |
| 2881 | if (len != sizeof(val)) { |
| 2882 | return -TARGET_EINVAL; |
| 2883 | } |
| 2884 | lv = len; |
| 2885 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2886 | if (ret < 0) { |
| 2887 | return ret; |
| 2888 | } |
| 2889 | if (put_user_u32(lv, optlen) |
| 2890 | || put_user_u32(val, optval_addr)) { |
| 2891 | return -TARGET_EFAULT; |
| 2892 | } |
| 2893 | break; |
| 2894 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 2895 | case NETLINK_LIST_MEMBERSHIPS: |
| 2896 | { |
| 2897 | uint32_t *results; |
| 2898 | int i; |
| 2899 | if (get_user_u32(len, optlen)) { |
| 2900 | return -TARGET_EFAULT; |
| 2901 | } |
| 2902 | if (len < 0) { |
| 2903 | return -TARGET_EINVAL; |
| 2904 | } |
| 2905 | results = lock_user(VERIFY_WRITE, optval_addr, len, 1); |
Frédéric Fortier | 13e340c | 2021-03-28 14:01:35 -0400 | [diff] [blame] | 2906 | if (!results && len > 0) { |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2907 | return -TARGET_EFAULT; |
| 2908 | } |
| 2909 | lv = len; |
| 2910 | ret = get_errno(getsockopt(sockfd, level, optname, results, &lv)); |
| 2911 | if (ret < 0) { |
| 2912 | unlock_user(results, optval_addr, 0); |
| 2913 | return ret; |
| 2914 | } |
Michael Tokarev | 669dcb6 | 2023-08-23 08:53:30 +0200 | [diff] [blame] | 2915 | /* swap host endianness to target endianness. */ |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2916 | for (i = 0; i < (len / sizeof(uint32_t)); i++) { |
| 2917 | results[i] = tswap32(results[i]); |
| 2918 | } |
| 2919 | if (put_user_u32(lv, optlen)) { |
| 2920 | return -TARGET_EFAULT; |
| 2921 | } |
| 2922 | unlock_user(results, optval_addr, 0); |
| 2923 | break; |
| 2924 | } |
| 2925 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ |
| 2926 | default: |
| 2927 | goto unimplemented; |
| 2928 | } |
Laurent Vivier | c0cb880 | 2019-11-12 11:50:55 +0100 | [diff] [blame] | 2929 | break; |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2930 | #endif /* SOL_NETLINK */ |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2931 | default: |
| 2932 | unimplemented: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 2933 | qemu_log_mask(LOG_UNIMP, |
| 2934 | "getsockopt level=%d optname=%d not yet supported\n", |
| 2935 | level, optname); |
ths | c02f499 | 2007-12-18 02:39:59 +0000 | [diff] [blame] | 2936 | ret = -TARGET_EOPNOTSUPP; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2937 | break; |
| 2938 | } |
| 2939 | return ret; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Max Filippov | 9ac2251 | 2018-04-04 17:30:41 -0700 | [diff] [blame] | 2942 | /* Convert target low/high pair representing file offset into the host |
| 2943 | * low/high pair. This function doesn't handle offsets bigger than 64 bits |
| 2944 | * as the kernel doesn't handle them either. |
| 2945 | */ |
| 2946 | static void target_to_host_low_high(abi_ulong tlow, |
| 2947 | abi_ulong thigh, |
| 2948 | unsigned long *hlow, |
| 2949 | unsigned long *hhigh) |
| 2950 | { |
| 2951 | uint64_t off = tlow | |
| 2952 | ((unsigned long long)thigh << TARGET_LONG_BITS / 2) << |
| 2953 | TARGET_LONG_BITS / 2; |
| 2954 | |
| 2955 | *hlow = off; |
| 2956 | *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2; |
| 2957 | } |
| 2958 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2959 | static struct iovec *lock_iovec(int type, abi_ulong target_addr, |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 2960 | abi_ulong count, int copy) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2961 | { |
| 2962 | struct target_iovec *target_vec; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2963 | struct iovec *vec; |
| 2964 | abi_ulong total_len, max_len; |
balrog | d732dcb | 2008-10-28 10:21:03 +0000 | [diff] [blame] | 2965 | int i; |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2966 | int err = 0; |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2967 | bool bad_address = false; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2968 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2969 | if (count == 0) { |
| 2970 | errno = 0; |
| 2971 | return NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2972 | } |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 2973 | if (count > IOV_MAX) { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2974 | errno = EINVAL; |
| 2975 | return NULL; |
| 2976 | } |
| 2977 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2978 | vec = g_try_new0(struct iovec, count); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2979 | if (vec == NULL) { |
| 2980 | errno = ENOMEM; |
| 2981 | return NULL; |
| 2982 | } |
| 2983 | |
| 2984 | target_vec = lock_user(VERIFY_READ, target_addr, |
| 2985 | count * sizeof(struct target_iovec), 1); |
| 2986 | if (target_vec == NULL) { |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2987 | err = EFAULT; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2988 | goto fail2; |
| 2989 | } |
| 2990 | |
| 2991 | /* ??? If host page size > target page size, this will result in a |
| 2992 | value larger than what we can actually support. */ |
| 2993 | max_len = 0x7fffffff & TARGET_PAGE_MASK; |
| 2994 | total_len = 0; |
| 2995 | |
| 2996 | for (i = 0; i < count; i++) { |
| 2997 | abi_ulong base = tswapal(target_vec[i].iov_base); |
| 2998 | abi_long len = tswapal(target_vec[i].iov_len); |
| 2999 | |
| 3000 | if (len < 0) { |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 3001 | err = EINVAL; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3002 | goto fail; |
| 3003 | } else if (len == 0) { |
| 3004 | /* Zero length pointer is ignored. */ |
| 3005 | vec[i].iov_base = 0; |
| 3006 | } else { |
| 3007 | vec[i].iov_base = lock_user(type, base, len, copy); |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 3008 | /* If the first buffer pointer is bad, this is a fault. But |
| 3009 | * subsequent bad buffers will result in a partial write; this |
| 3010 | * is realized by filling the vector with null pointers and |
| 3011 | * zero lengths. */ |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3012 | if (!vec[i].iov_base) { |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 3013 | if (i == 0) { |
| 3014 | err = EFAULT; |
| 3015 | goto fail; |
| 3016 | } else { |
| 3017 | bad_address = true; |
| 3018 | } |
| 3019 | } |
| 3020 | if (bad_address) { |
| 3021 | len = 0; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3022 | } |
| 3023 | if (len > max_len - total_len) { |
| 3024 | len = max_len - total_len; |
| 3025 | } |
| 3026 | } |
| 3027 | vec[i].iov_len = len; |
| 3028 | total_len += len; |
| 3029 | } |
| 3030 | |
| 3031 | unlock_user(target_vec, target_addr, 0); |
| 3032 | return vec; |
| 3033 | |
| 3034 | fail: |
Chen Gang S | 7eff518 | 2015-01-23 18:01:09 +0800 | [diff] [blame] | 3035 | while (--i >= 0) { |
| 3036 | if (tswapal(target_vec[i].iov_len) > 0) { |
| 3037 | unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0); |
| 3038 | } |
| 3039 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3040 | unlock_user(target_vec, target_addr, 0); |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 3041 | fail2: |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3042 | g_free(vec); |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 3043 | errno = err; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3044 | return NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3047 | static void unlock_iovec(struct iovec *vec, abi_ulong target_addr, |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 3048 | abi_ulong count, int copy) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3049 | { |
| 3050 | struct target_iovec *target_vec; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3051 | int i; |
| 3052 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3053 | target_vec = lock_user(VERIFY_READ, target_addr, |
| 3054 | count * sizeof(struct target_iovec), 1); |
| 3055 | if (target_vec) { |
| 3056 | for (i = 0; i < count; i++) { |
| 3057 | abi_ulong base = tswapal(target_vec[i].iov_base); |
Chen Gang S | 71ec7ce | 2015-01-23 18:07:50 +0800 | [diff] [blame] | 3058 | abi_long len = tswapal(target_vec[i].iov_len); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3059 | if (len < 0) { |
| 3060 | break; |
| 3061 | } |
balrog | d732dcb | 2008-10-28 10:21:03 +0000 | [diff] [blame] | 3062 | unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); |
| 3063 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3064 | unlock_user(target_vec, target_addr, 0); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3065 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3066 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3067 | g_free(vec); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3070 | static inline int target_to_host_sock_type(int *type) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3071 | { |
| 3072 | int host_type = 0; |
| 3073 | int target_type = *type; |
| 3074 | |
| 3075 | switch (target_type & TARGET_SOCK_TYPE_MASK) { |
| 3076 | case TARGET_SOCK_DGRAM: |
| 3077 | host_type = SOCK_DGRAM; |
| 3078 | break; |
| 3079 | case TARGET_SOCK_STREAM: |
| 3080 | host_type = SOCK_STREAM; |
| 3081 | break; |
| 3082 | default: |
| 3083 | host_type = target_type & TARGET_SOCK_TYPE_MASK; |
| 3084 | break; |
| 3085 | } |
| 3086 | if (target_type & TARGET_SOCK_CLOEXEC) { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3087 | #if defined(SOCK_CLOEXEC) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3088 | host_type |= SOCK_CLOEXEC; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3089 | #else |
| 3090 | return -TARGET_EINVAL; |
| 3091 | #endif |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3092 | } |
| 3093 | if (target_type & TARGET_SOCK_NONBLOCK) { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3094 | #if defined(SOCK_NONBLOCK) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3095 | host_type |= SOCK_NONBLOCK; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3096 | #elif !defined(O_NONBLOCK) |
| 3097 | return -TARGET_EINVAL; |
| 3098 | #endif |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3099 | } |
| 3100 | *type = host_type; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3101 | return 0; |
| 3102 | } |
| 3103 | |
| 3104 | /* Try to emulate socket type flags after socket creation. */ |
| 3105 | static int sock_flags_fixup(int fd, int target_type) |
| 3106 | { |
| 3107 | #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK) |
| 3108 | if (target_type & TARGET_SOCK_NONBLOCK) { |
| 3109 | int flags = fcntl(fd, F_GETFL); |
| 3110 | if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) { |
| 3111 | close(fd); |
| 3112 | return -TARGET_EINVAL; |
| 3113 | } |
| 3114 | } |
| 3115 | #endif |
| 3116 | return fd; |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3117 | } |
| 3118 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3119 | /* do_socket() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3120 | static abi_long do_socket(int domain, int type, int protocol) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3121 | { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3122 | int target_type = type; |
| 3123 | int ret; |
| 3124 | |
| 3125 | ret = target_to_host_sock_type(&type); |
| 3126 | if (ret) { |
| 3127 | return ret; |
| 3128 | } |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3129 | |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 3130 | if (domain == PF_NETLINK && !( |
| 3131 | #ifdef CONFIG_RTNETLINK |
| 3132 | protocol == NETLINK_ROUTE || |
| 3133 | #endif |
| 3134 | protocol == NETLINK_KOBJECT_UEVENT || |
| 3135 | protocol == NETLINK_AUDIT)) { |
Josh Kunz | 71e2443 | 2020-07-06 17:10:36 -0700 | [diff] [blame] | 3136 | return -TARGET_EPROTONOSUPPORT; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3137 | } |
Laurent Vivier | ff626f2 | 2015-10-28 21:40:42 +0100 | [diff] [blame] | 3138 | |
| 3139 | if (domain == AF_PACKET || |
| 3140 | (domain == AF_INET && type == SOCK_PACKET)) { |
| 3141 | protocol = tswap16(protocol); |
| 3142 | } |
| 3143 | |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3144 | ret = get_errno(socket(domain, type, protocol)); |
| 3145 | if (ret >= 0) { |
| 3146 | ret = sock_flags_fixup(ret, target_type); |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 3147 | if (type == SOCK_PACKET) { |
| 3148 | /* Manage an obsolete case : |
| 3149 | * if socket type is SOCK_PACKET, bind by name |
| 3150 | */ |
| 3151 | fd_trans_register(ret, &target_packet_trans); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3152 | } else if (domain == PF_NETLINK) { |
| 3153 | switch (protocol) { |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 3154 | #ifdef CONFIG_RTNETLINK |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3155 | case NETLINK_ROUTE: |
| 3156 | fd_trans_register(ret, &target_netlink_route_trans); |
| 3157 | break; |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 3158 | #endif |
Laurent Vivier | b265620 | 2016-05-22 18:56:20 +0200 | [diff] [blame] | 3159 | case NETLINK_KOBJECT_UEVENT: |
| 3160 | /* nothing to do: messages are strings */ |
| 3161 | break; |
Laurent Vivier | 5ce9bb5 | 2016-05-22 18:56:21 +0200 | [diff] [blame] | 3162 | case NETLINK_AUDIT: |
| 3163 | fd_trans_register(ret, &target_netlink_audit_trans); |
| 3164 | break; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3165 | default: |
| 3166 | g_assert_not_reached(); |
| 3167 | } |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 3168 | } |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3169 | } |
| 3170 | return ret; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3173 | /* do_bind() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3174 | static abi_long do_bind(int sockfd, abi_ulong target_addr, |
| 3175 | socklen_t addrlen) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3176 | { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3177 | void *addr; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3178 | abi_long ret; |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3179 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3180 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3181 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3182 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3183 | |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 3184 | addr = alloca(addrlen+1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3185 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3186 | ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3187 | if (ret) |
| 3188 | return ret; |
| 3189 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3190 | return get_errno(bind(sockfd, addr, addrlen)); |
| 3191 | } |
| 3192 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3193 | /* do_connect() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3194 | static abi_long do_connect(int sockfd, abi_ulong target_addr, |
| 3195 | socklen_t addrlen) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3196 | { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3197 | void *addr; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3198 | abi_long ret; |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3199 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3200 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3201 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3202 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3203 | |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3204 | addr = alloca(addrlen+1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3205 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3206 | ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3207 | if (ret) |
| 3208 | return ret; |
| 3209 | |
Peter Maydell | 2a3c761 | 2016-06-06 19:58:03 +0100 | [diff] [blame] | 3210 | return get_errno(safe_connect(sockfd, addr, addrlen)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3213 | /* do_sendrecvmsg_locked() Must return target values and target errnos. */ |
| 3214 | static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, |
| 3215 | int flags, int send) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3216 | { |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3217 | abi_long ret, len; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3218 | struct msghdr msg; |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 3219 | abi_ulong count; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3220 | struct iovec *vec; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3221 | abi_ulong target_vec; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3222 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3223 | if (msgp->msg_name) { |
| 3224 | msg.msg_namelen = tswap32(msgp->msg_namelen); |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3225 | msg.msg_name = alloca(msg.msg_namelen+1); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3226 | ret = target_to_host_sockaddr(fd, msg.msg_name, |
| 3227 | tswapal(msgp->msg_name), |
| 3228 | msg.msg_namelen); |
Peter Maydell | 26a6fc9 | 2016-07-15 14:57:28 +0100 | [diff] [blame] | 3229 | if (ret == -TARGET_EFAULT) { |
| 3230 | /* For connected sockets msg_name and msg_namelen must |
| 3231 | * be ignored, so returning EFAULT immediately is wrong. |
| 3232 | * Instead, pass a bad msg_name to the host kernel, and |
| 3233 | * let it decide whether to return EFAULT or not. |
| 3234 | */ |
| 3235 | msg.msg_name = (void *)-1; |
| 3236 | } else if (ret) { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3237 | goto out2; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3238 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3239 | } else { |
| 3240 | msg.msg_name = NULL; |
| 3241 | msg.msg_namelen = 0; |
| 3242 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3243 | msg.msg_controllen = 2 * tswapal(msgp->msg_controllen); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3244 | msg.msg_control = alloca(msg.msg_controllen); |
Jonas Schievink | 1d3d1b2 | 2018-07-12 00:12:44 +0200 | [diff] [blame] | 3245 | memset(msg.msg_control, 0, msg.msg_controllen); |
| 3246 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3247 | msg.msg_flags = tswap32(msgp->msg_flags); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3248 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3249 | count = tswapal(msgp->msg_iovlen); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3250 | target_vec = tswapal(msgp->msg_iov); |
Peter Maydell | 97b0797 | 2016-07-15 14:57:27 +0100 | [diff] [blame] | 3251 | |
| 3252 | if (count > IOV_MAX) { |
| 3253 | /* sendrcvmsg returns a different errno for this condition than |
| 3254 | * readv/writev, so we must catch it here before lock_iovec() does. |
| 3255 | */ |
| 3256 | ret = -TARGET_EMSGSIZE; |
| 3257 | goto out2; |
| 3258 | } |
| 3259 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3260 | vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, |
| 3261 | target_vec, count, send); |
| 3262 | if (vec == NULL) { |
| 3263 | ret = -host_to_target_errno(errno); |
Helge Deller | 3f0744f | 2022-12-12 18:34:16 +0100 | [diff] [blame] | 3264 | /* allow sending packet without any iov, e.g. with MSG_MORE flag */ |
| 3265 | if (!send || ret) { |
| 3266 | goto out2; |
| 3267 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3268 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3269 | msg.msg_iovlen = count; |
| 3270 | msg.msg_iov = vec; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3271 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3272 | if (send) { |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3273 | if (fd_trans_target_to_host_data(fd)) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3274 | void *host_msg; |
| 3275 | |
| 3276 | host_msg = g_malloc(msg.msg_iov->iov_len); |
| 3277 | memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len); |
| 3278 | ret = fd_trans_target_to_host_data(fd)(host_msg, |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3279 | msg.msg_iov->iov_len); |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3280 | if (ret >= 0) { |
| 3281 | msg.msg_iov->iov_base = host_msg; |
| 3282 | ret = get_errno(safe_sendmsg(fd, &msg, flags)); |
| 3283 | } |
| 3284 | g_free(host_msg); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3285 | } else { |
| 3286 | ret = target_to_host_cmsg(&msg, msgp); |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3287 | if (ret == 0) { |
| 3288 | ret = get_errno(safe_sendmsg(fd, &msg, flags)); |
| 3289 | } |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3290 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3291 | } else { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3292 | ret = get_errno(safe_recvmsg(fd, &msg, flags)); |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3293 | if (!is_error(ret)) { |
| 3294 | len = ret; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3295 | if (fd_trans_host_to_target_data(fd)) { |
| 3296 | ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base, |
Laurent Vivier | 2a03d3e | 2018-08-20 19:15:54 +0200 | [diff] [blame] | 3297 | MIN(msg.msg_iov->iov_len, len)); |
Icenowy Zheng | 16c81dd | 2022-10-28 16:12:20 +0800 | [diff] [blame] | 3298 | } |
| 3299 | if (!is_error(ret)) { |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3300 | ret = host_to_target_cmsg(msgp, &msg); |
| 3301 | } |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3302 | if (!is_error(ret)) { |
| 3303 | msgp->msg_namelen = tswap32(msg.msg_namelen); |
Andreas Schwab | 24894f3 | 2019-02-12 17:34:35 +0100 | [diff] [blame] | 3304 | msgp->msg_flags = tswap32(msg.msg_flags); |
Peter Maydell | 26a6fc9 | 2016-07-15 14:57:28 +0100 | [diff] [blame] | 3305 | if (msg.msg_name != NULL && msg.msg_name != (void *)-1) { |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3306 | ret = host_to_target_sockaddr(tswapal(msgp->msg_name), |
| 3307 | msg.msg_name, msg.msg_namelen); |
| 3308 | if (ret) { |
| 3309 | goto out; |
| 3310 | } |
| 3311 | } |
| 3312 | |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3313 | ret = len; |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3314 | } |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3315 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3316 | } |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3317 | |
| 3318 | out: |
Helge Deller | 3f0744f | 2022-12-12 18:34:16 +0100 | [diff] [blame] | 3319 | if (vec) { |
| 3320 | unlock_iovec(vec, target_vec, count, !send); |
| 3321 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3322 | out2: |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3323 | return ret; |
| 3324 | } |
| 3325 | |
| 3326 | static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg, |
| 3327 | int flags, int send) |
| 3328 | { |
| 3329 | abi_long ret; |
| 3330 | struct target_msghdr *msgp; |
| 3331 | |
| 3332 | if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE, |
| 3333 | msgp, |
| 3334 | target_msg, |
| 3335 | send ? 1 : 0)) { |
| 3336 | return -TARGET_EFAULT; |
| 3337 | } |
| 3338 | ret = do_sendrecvmsg_locked(fd, msgp, flags, send); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3339 | unlock_user_struct(msgp, target_msg, send ? 0 : 1); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3340 | return ret; |
| 3341 | } |
| 3342 | |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3343 | /* We don't rely on the C library to have sendmmsg/recvmmsg support, |
| 3344 | * so it might not have this *mmsg-specific flag either. |
| 3345 | */ |
| 3346 | #ifndef MSG_WAITFORONE |
| 3347 | #define MSG_WAITFORONE 0x10000 |
| 3348 | #endif |
| 3349 | |
| 3350 | static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec, |
| 3351 | unsigned int vlen, unsigned int flags, |
| 3352 | int send) |
| 3353 | { |
| 3354 | struct target_mmsghdr *mmsgp; |
| 3355 | abi_long ret = 0; |
| 3356 | int i; |
| 3357 | |
| 3358 | if (vlen > UIO_MAXIOV) { |
| 3359 | vlen = UIO_MAXIOV; |
| 3360 | } |
| 3361 | |
| 3362 | mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1); |
| 3363 | if (!mmsgp) { |
| 3364 | return -TARGET_EFAULT; |
| 3365 | } |
| 3366 | |
| 3367 | for (i = 0; i < vlen; i++) { |
| 3368 | ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send); |
| 3369 | if (is_error(ret)) { |
| 3370 | break; |
| 3371 | } |
| 3372 | mmsgp[i].msg_len = tswap32(ret); |
| 3373 | /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ |
| 3374 | if (flags & MSG_WAITFORONE) { |
| 3375 | flags |= MSG_DONTWAIT; |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i); |
| 3380 | |
| 3381 | /* Return number of datagrams sent if we sent any at all; |
| 3382 | * otherwise return the error. |
| 3383 | */ |
| 3384 | if (i) { |
| 3385 | return i; |
| 3386 | } |
| 3387 | return ret; |
| 3388 | } |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3389 | |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3390 | /* do_accept4() Must return target values and target errnos. */ |
| 3391 | static abi_long do_accept4(int fd, abi_ulong target_addr, |
| 3392 | abi_ulong target_addrlen_addr, int flags) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3393 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3394 | socklen_t addrlen, ret_addrlen; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3395 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3396 | abi_long ret; |
Petar Jovanovic | d25295d | 2014-03-31 17:41:23 +0200 | [diff] [blame] | 3397 | int host_flags; |
| 3398 | |
Helge Deller | dca4c83 | 2023-07-08 07:24:00 +0200 | [diff] [blame] | 3399 | if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) { |
| 3400 | return -TARGET_EINVAL; |
| 3401 | } |
| 3402 | |
| 3403 | host_flags = 0; |
| 3404 | if (flags & TARGET_SOCK_NONBLOCK) { |
| 3405 | host_flags |= SOCK_NONBLOCK; |
| 3406 | } |
| 3407 | if (flags & TARGET_SOCK_CLOEXEC) { |
| 3408 | host_flags |= SOCK_CLOEXEC; |
| 3409 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3410 | |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3411 | if (target_addr == 0) { |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 3412 | return get_errno(safe_accept4(fd, NULL, NULL, host_flags)); |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3413 | } |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3414 | |
Matus Kysel | e554eb4 | 2020-09-30 17:16:16 +0200 | [diff] [blame] | 3415 | /* linux returns EFAULT if addrlen pointer is invalid */ |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3416 | if (get_user_u32(addrlen, target_addrlen_addr)) |
Matus Kysel | e554eb4 | 2020-09-30 17:16:16 +0200 | [diff] [blame] | 3417 | return -TARGET_EFAULT; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3418 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3419 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3420 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3421 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3422 | |
Richard Henderson | c7169b0 | 2021-02-12 10:48:47 -0800 | [diff] [blame] | 3423 | if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) { |
Matus Kysel | e554eb4 | 2020-09-30 17:16:16 +0200 | [diff] [blame] | 3424 | return -TARGET_EFAULT; |
Richard Henderson | c7169b0 | 2021-02-12 10:48:47 -0800 | [diff] [blame] | 3425 | } |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3426 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3427 | addr = alloca(addrlen); |
| 3428 | |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3429 | ret_addrlen = addrlen; |
| 3430 | ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3431 | if (!is_error(ret)) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3432 | host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); |
| 3433 | if (put_user_u32(ret_addrlen, target_addrlen_addr)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3434 | ret = -TARGET_EFAULT; |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3435 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3436 | } |
| 3437 | return ret; |
| 3438 | } |
| 3439 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3440 | /* do_getpeername() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3441 | static abi_long do_getpeername(int fd, abi_ulong target_addr, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3442 | abi_ulong target_addrlen_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3443 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3444 | socklen_t addrlen, ret_addrlen; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3445 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3446 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3447 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3448 | if (get_user_u32(addrlen, target_addrlen_addr)) |
| 3449 | return -TARGET_EFAULT; |
| 3450 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3451 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3452 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3453 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3454 | |
Richard Henderson | c7169b0 | 2021-02-12 10:48:47 -0800 | [diff] [blame] | 3455 | if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) { |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3456 | return -TARGET_EFAULT; |
Richard Henderson | c7169b0 | 2021-02-12 10:48:47 -0800 | [diff] [blame] | 3457 | } |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3458 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3459 | addr = alloca(addrlen); |
| 3460 | |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3461 | ret_addrlen = addrlen; |
| 3462 | ret = get_errno(getpeername(fd, addr, &ret_addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3463 | if (!is_error(ret)) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3464 | host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); |
| 3465 | if (put_user_u32(ret_addrlen, target_addrlen_addr)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3466 | ret = -TARGET_EFAULT; |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3467 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3468 | } |
| 3469 | return ret; |
| 3470 | } |
| 3471 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3472 | /* do_getsockname() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3473 | static abi_long do_getsockname(int fd, abi_ulong target_addr, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3474 | abi_ulong target_addrlen_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3475 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3476 | socklen_t addrlen, ret_addrlen; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3477 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3478 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3479 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3480 | if (get_user_u32(addrlen, target_addrlen_addr)) |
| 3481 | return -TARGET_EFAULT; |
| 3482 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3483 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3484 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3485 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3486 | |
Richard Henderson | c7169b0 | 2021-02-12 10:48:47 -0800 | [diff] [blame] | 3487 | if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) { |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3488 | return -TARGET_EFAULT; |
Richard Henderson | c7169b0 | 2021-02-12 10:48:47 -0800 | [diff] [blame] | 3489 | } |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3490 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3491 | addr = alloca(addrlen); |
| 3492 | |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3493 | ret_addrlen = addrlen; |
| 3494 | ret = get_errno(getsockname(fd, addr, &ret_addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3495 | if (!is_error(ret)) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3496 | host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); |
| 3497 | if (put_user_u32(ret_addrlen, target_addrlen_addr)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3498 | ret = -TARGET_EFAULT; |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3499 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3500 | } |
| 3501 | return ret; |
| 3502 | } |
| 3503 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3504 | /* do_socketpair() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3505 | static abi_long do_socketpair(int domain, int type, int protocol, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3506 | abi_ulong target_tab_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3507 | { |
| 3508 | int tab[2]; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3509 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3510 | |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3511 | target_to_host_sock_type(&type); |
| 3512 | |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3513 | ret = get_errno(socketpair(domain, type, protocol, tab)); |
| 3514 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3515 | if (put_user_s32(tab[0], target_tab_addr) |
| 3516 | || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0]))) |
| 3517 | ret = -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3518 | } |
| 3519 | return ret; |
| 3520 | } |
| 3521 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3522 | /* do_sendto() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3523 | static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags, |
| 3524 | abi_ulong target_addr, socklen_t addrlen) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3525 | { |
| 3526 | void *addr; |
| 3527 | void *host_msg; |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3528 | void *copy_msg = NULL; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3529 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3530 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3531 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3532 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3533 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3534 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3535 | host_msg = lock_user(VERIFY_READ, msg, len, 1); |
| 3536 | if (!host_msg) |
| 3537 | return -TARGET_EFAULT; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3538 | if (fd_trans_target_to_host_data(fd)) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3539 | copy_msg = host_msg; |
| 3540 | host_msg = g_malloc(len); |
| 3541 | memcpy(host_msg, copy_msg, len); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3542 | ret = fd_trans_target_to_host_data(fd)(host_msg, len); |
| 3543 | if (ret < 0) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3544 | goto fail; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3545 | } |
| 3546 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3547 | if (target_addr) { |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3548 | addr = alloca(addrlen+1); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3549 | ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3550 | if (ret) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3551 | goto fail; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3552 | } |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3553 | ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3554 | } else { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3555 | ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3556 | } |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3557 | fail: |
| 3558 | if (copy_msg) { |
| 3559 | g_free(host_msg); |
| 3560 | host_msg = copy_msg; |
| 3561 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3562 | unlock_user(host_msg, msg, 0); |
| 3563 | return ret; |
| 3564 | } |
| 3565 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3566 | /* do_recvfrom() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3567 | static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags, |
| 3568 | abi_ulong target_addr, |
| 3569 | abi_ulong target_addrlen) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3570 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3571 | socklen_t addrlen, ret_addrlen; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3572 | void *addr; |
| 3573 | void *host_msg; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3574 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3575 | |
Zach Reizner | 4a1e6bc | 2021-03-26 22:11:16 -0400 | [diff] [blame] | 3576 | if (!msg) { |
| 3577 | host_msg = NULL; |
| 3578 | } else { |
| 3579 | host_msg = lock_user(VERIFY_WRITE, msg, len, 0); |
| 3580 | if (!host_msg) { |
| 3581 | return -TARGET_EFAULT; |
| 3582 | } |
| 3583 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3584 | if (target_addr) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3585 | if (get_user_u32(addrlen, target_addrlen)) { |
| 3586 | ret = -TARGET_EFAULT; |
| 3587 | goto fail; |
| 3588 | } |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3589 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3590 | ret = -TARGET_EINVAL; |
| 3591 | goto fail; |
| 3592 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3593 | addr = alloca(addrlen); |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3594 | ret_addrlen = addrlen; |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3595 | ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3596 | addr, &ret_addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3597 | } else { |
| 3598 | addr = NULL; /* To keep compiler quiet. */ |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3599 | addrlen = 0; /* To keep compiler quiet. */ |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3600 | ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3601 | } |
| 3602 | if (!is_error(ret)) { |
Laurent Vivier | c35e1f9 | 2016-06-21 19:51:15 +0200 | [diff] [blame] | 3603 | if (fd_trans_host_to_target_data(fd)) { |
Laurent Vivier | 2a03d3e | 2018-08-20 19:15:54 +0200 | [diff] [blame] | 3604 | abi_long trans; |
| 3605 | trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len)); |
| 3606 | if (is_error(trans)) { |
| 3607 | ret = trans; |
| 3608 | goto fail; |
| 3609 | } |
Laurent Vivier | c35e1f9 | 2016-06-21 19:51:15 +0200 | [diff] [blame] | 3610 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3611 | if (target_addr) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3612 | host_to_target_sockaddr(target_addr, addr, |
| 3613 | MIN(addrlen, ret_addrlen)); |
| 3614 | if (put_user_u32(ret_addrlen, target_addrlen)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3615 | ret = -TARGET_EFAULT; |
| 3616 | goto fail; |
| 3617 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3618 | } |
| 3619 | unlock_user(host_msg, msg, len); |
| 3620 | } else { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3621 | fail: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3622 | unlock_user(host_msg, msg, 0); |
| 3623 | } |
| 3624 | return ret; |
| 3625 | } |
| 3626 | |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 3627 | #ifdef TARGET_NR_socketcall |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3628 | /* do_socketcall() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3629 | static abi_long do_socketcall(int num, abi_ulong vptr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3630 | { |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3631 | static const unsigned nargs[] = { /* number of arguments per operation */ |
| 3632 | [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */ |
| 3633 | [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */ |
| 3634 | [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */ |
| 3635 | [TARGET_SYS_LISTEN] = 2, /* fd, backlog */ |
| 3636 | [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */ |
| 3637 | [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */ |
| 3638 | [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */ |
| 3639 | [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */ |
| 3640 | [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */ |
| 3641 | [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */ |
| 3642 | [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */ |
| 3643 | [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */ |
| 3644 | [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */ |
| 3645 | [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */ |
| 3646 | [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */ |
| 3647 | [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */ |
| 3648 | [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */ |
| 3649 | [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */ |
| 3650 | [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */ |
| 3651 | [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3652 | }; |
| 3653 | abi_long a[6]; /* max 6 args */ |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3654 | unsigned i; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3655 | |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3656 | /* check the range of the first argument num */ |
| 3657 | /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */ |
| 3658 | if (num < 1 || num > TARGET_SYS_SENDMMSG) { |
| 3659 | return -TARGET_EINVAL; |
| 3660 | } |
| 3661 | /* ensure we have space for args */ |
| 3662 | if (nargs[num] > ARRAY_SIZE(a)) { |
| 3663 | return -TARGET_EINVAL; |
| 3664 | } |
| 3665 | /* collect the arguments in a[] according to nargs[] */ |
| 3666 | for (i = 0; i < nargs[num]; ++i) { |
| 3667 | if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) { |
| 3668 | return -TARGET_EFAULT; |
André Hentschel | b9d36eb | 2014-01-06 20:18:52 +0100 | [diff] [blame] | 3669 | } |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3670 | } |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3671 | /* now when we have the args, invoke the appropriate underlying function */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3672 | switch (num) { |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3673 | case TARGET_SYS_SOCKET: /* domain, type, protocol */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3674 | return do_socket(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3675 | case TARGET_SYS_BIND: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3676 | return do_bind(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3677 | case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3678 | return do_connect(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3679 | case TARGET_SYS_LISTEN: /* sockfd, backlog */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3680 | return get_errno(listen(a[0], a[1])); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3681 | case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3682 | return do_accept4(a[0], a[1], a[2], 0); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3683 | case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3684 | return do_getsockname(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3685 | case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3686 | return do_getpeername(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3687 | case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3688 | return do_socketpair(a[0], a[1], a[2], a[3]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3689 | case TARGET_SYS_SEND: /* sockfd, msg, len, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3690 | return do_sendto(a[0], a[1], a[2], a[3], 0, 0); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3691 | case TARGET_SYS_RECV: /* sockfd, msg, len, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3692 | return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3693 | case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3694 | return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3695 | case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3696 | return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3697 | case TARGET_SYS_SHUTDOWN: /* sockfd, how */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3698 | return get_errno(shutdown(a[0], a[1])); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3699 | case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3700 | return do_setsockopt(a[0], a[1], a[2], a[3], a[4]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3701 | case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3702 | return do_getsockopt(a[0], a[1], a[2], a[3], a[4]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3703 | case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */ |
| 3704 | return do_sendrecvmsg(a[0], a[1], a[2], 1); |
| 3705 | case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */ |
| 3706 | return do_sendrecvmsg(a[0], a[1], a[2], 0); |
| 3707 | case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */ |
| 3708 | return do_accept4(a[0], a[1], a[2], a[3]); |
| 3709 | case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */ |
| 3710 | return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0); |
| 3711 | case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */ |
| 3712 | return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3713 | default: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 3714 | qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3715 | return -TARGET_EINVAL; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3716 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3717 | } |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 3718 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3719 | |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3720 | #ifndef TARGET_SEMID64_DS |
| 3721 | /* asm-generic version of this struct */ |
| 3722 | struct target_semid64_ds |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3723 | { |
| 3724 | struct target_ipc_perm sem_perm; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3725 | abi_ulong sem_otime; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3726 | #if TARGET_ABI_BITS == 32 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3727 | abi_ulong __unused1; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3728 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3729 | abi_ulong sem_ctime; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3730 | #if TARGET_ABI_BITS == 32 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3731 | abi_ulong __unused2; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3732 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3733 | abi_ulong sem_nsems; |
| 3734 | abi_ulong __unused3; |
| 3735 | abi_ulong __unused4; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3736 | }; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3737 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3738 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3739 | static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip, |
| 3740 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3741 | { |
| 3742 | struct target_ipc_perm *target_ip; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3743 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3744 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3745 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3746 | return -TARGET_EFAULT; |
Michael S. Tsirkin | e8bbe36 | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 3747 | target_ip = &(target_sd->sem_perm); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3748 | host_ip->__key = tswap32(target_ip->__key); |
| 3749 | host_ip->uid = tswap32(target_ip->uid); |
| 3750 | host_ip->gid = tswap32(target_ip->gid); |
| 3751 | host_ip->cuid = tswap32(target_ip->cuid); |
| 3752 | host_ip->cgid = tswap32(target_ip->cgid); |
| 3753 | #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) |
| 3754 | host_ip->mode = tswap32(target_ip->mode); |
| 3755 | #else |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3756 | host_ip->mode = tswap16(target_ip->mode); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3757 | #endif |
| 3758 | #if defined(TARGET_PPC) |
| 3759 | host_ip->__seq = tswap32(target_ip->__seq); |
| 3760 | #else |
| 3761 | host_ip->__seq = tswap16(target_ip->__seq); |
| 3762 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3763 | unlock_user_struct(target_sd, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3764 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3767 | static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr, |
| 3768 | struct ipc_perm *host_ip) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3769 | { |
| 3770 | struct target_ipc_perm *target_ip; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3771 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3772 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3773 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3774 | return -TARGET_EFAULT; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3775 | target_ip = &(target_sd->sem_perm); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3776 | target_ip->__key = tswap32(host_ip->__key); |
| 3777 | target_ip->uid = tswap32(host_ip->uid); |
| 3778 | target_ip->gid = tswap32(host_ip->gid); |
| 3779 | target_ip->cuid = tswap32(host_ip->cuid); |
| 3780 | target_ip->cgid = tswap32(host_ip->cgid); |
| 3781 | #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) |
| 3782 | target_ip->mode = tswap32(host_ip->mode); |
| 3783 | #else |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3784 | target_ip->mode = tswap16(host_ip->mode); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3785 | #endif |
| 3786 | #if defined(TARGET_PPC) |
| 3787 | target_ip->__seq = tswap32(host_ip->__seq); |
| 3788 | #else |
| 3789 | target_ip->__seq = tswap16(host_ip->__seq); |
| 3790 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3791 | unlock_user_struct(target_sd, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3792 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3795 | static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd, |
| 3796 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3797 | { |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3798 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3799 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3800 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3801 | return -TARGET_EFAULT; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3802 | if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr)) |
| 3803 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3804 | host_sd->sem_nsems = tswapal(target_sd->sem_nsems); |
| 3805 | host_sd->sem_otime = tswapal(target_sd->sem_otime); |
| 3806 | host_sd->sem_ctime = tswapal(target_sd->sem_ctime); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3807 | unlock_user_struct(target_sd, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3808 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3811 | static inline abi_long host_to_target_semid_ds(abi_ulong target_addr, |
| 3812 | struct semid_ds *host_sd) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3813 | { |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3814 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3815 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3816 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3817 | return -TARGET_EFAULT; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3818 | if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm))) |
Dong Xu Wang | 3a93113 | 2011-11-29 16:52:38 +0800 | [diff] [blame] | 3819 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3820 | target_sd->sem_nsems = tswapal(host_sd->sem_nsems); |
| 3821 | target_sd->sem_otime = tswapal(host_sd->sem_otime); |
| 3822 | target_sd->sem_ctime = tswapal(host_sd->sem_ctime); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3823 | unlock_user_struct(target_sd, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3824 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3827 | struct target_seminfo { |
| 3828 | int semmap; |
| 3829 | int semmni; |
| 3830 | int semmns; |
| 3831 | int semmnu; |
| 3832 | int semmsl; |
| 3833 | int semopm; |
| 3834 | int semume; |
| 3835 | int semusz; |
| 3836 | int semvmx; |
| 3837 | int semaem; |
| 3838 | }; |
| 3839 | |
| 3840 | static inline abi_long host_to_target_seminfo(abi_ulong target_addr, |
| 3841 | struct seminfo *host_seminfo) |
| 3842 | { |
| 3843 | struct target_seminfo *target_seminfo; |
| 3844 | if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0)) |
| 3845 | return -TARGET_EFAULT; |
| 3846 | __put_user(host_seminfo->semmap, &target_seminfo->semmap); |
| 3847 | __put_user(host_seminfo->semmni, &target_seminfo->semmni); |
| 3848 | __put_user(host_seminfo->semmns, &target_seminfo->semmns); |
| 3849 | __put_user(host_seminfo->semmnu, &target_seminfo->semmnu); |
| 3850 | __put_user(host_seminfo->semmsl, &target_seminfo->semmsl); |
| 3851 | __put_user(host_seminfo->semopm, &target_seminfo->semopm); |
| 3852 | __put_user(host_seminfo->semume, &target_seminfo->semume); |
| 3853 | __put_user(host_seminfo->semusz, &target_seminfo->semusz); |
| 3854 | __put_user(host_seminfo->semvmx, &target_seminfo->semvmx); |
| 3855 | __put_user(host_seminfo->semaem, &target_seminfo->semaem); |
| 3856 | unlock_user_struct(target_seminfo, target_addr, 1); |
| 3857 | return 0; |
| 3858 | } |
| 3859 | |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3860 | union semun { |
| 3861 | int val; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3862 | struct semid_ds *buf; |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3863 | unsigned short *array; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3864 | struct seminfo *__buf; |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3865 | }; |
| 3866 | |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3867 | union target_semun { |
| 3868 | int val; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3869 | abi_ulong buf; |
| 3870 | abi_ulong array; |
| 3871 | abi_ulong __buf; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3872 | }; |
| 3873 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3874 | static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array, |
| 3875 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3876 | { |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3877 | int nsems; |
| 3878 | unsigned short *array; |
| 3879 | union semun semun; |
| 3880 | struct semid_ds semid_ds; |
| 3881 | int i, ret; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3882 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3883 | semun.buf = &semid_ds; |
| 3884 | |
| 3885 | ret = semctl(semid, 0, IPC_STAT, semun); |
| 3886 | if (ret == -1) |
| 3887 | return get_errno(ret); |
| 3888 | |
| 3889 | nsems = semid_ds.sem_nsems; |
| 3890 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3891 | *host_array = g_try_new(unsigned short, nsems); |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3892 | if (!*host_array) { |
| 3893 | return -TARGET_ENOMEM; |
| 3894 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3895 | array = lock_user(VERIFY_READ, target_addr, |
| 3896 | nsems*sizeof(unsigned short), 1); |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3897 | if (!array) { |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3898 | g_free(*host_array); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3899 | return -TARGET_EFAULT; |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3900 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3901 | |
| 3902 | for(i=0; i<nsems; i++) { |
| 3903 | __get_user((*host_array)[i], &array[i]); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3904 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3905 | unlock_user(array, target_addr, 0); |
| 3906 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3907 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3910 | static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr, |
| 3911 | unsigned short **host_array) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3912 | { |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3913 | int nsems; |
| 3914 | unsigned short *array; |
| 3915 | union semun semun; |
| 3916 | struct semid_ds semid_ds; |
| 3917 | int i, ret; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3918 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3919 | semun.buf = &semid_ds; |
| 3920 | |
| 3921 | ret = semctl(semid, 0, IPC_STAT, semun); |
| 3922 | if (ret == -1) |
| 3923 | return get_errno(ret); |
| 3924 | |
| 3925 | nsems = semid_ds.sem_nsems; |
| 3926 | |
| 3927 | array = lock_user(VERIFY_WRITE, target_addr, |
| 3928 | nsems*sizeof(unsigned short), 0); |
| 3929 | if (!array) |
| 3930 | return -TARGET_EFAULT; |
| 3931 | |
| 3932 | for(i=0; i<nsems; i++) { |
| 3933 | __put_user((*host_array)[i], &array[i]); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3934 | } |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3935 | g_free(*host_array); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3936 | unlock_user(array, target_addr, 1); |
| 3937 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3938 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3941 | static inline abi_long do_semctl(int semid, int semnum, int cmd, |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 3942 | abi_ulong target_arg) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3943 | { |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 3944 | union target_semun target_su = { .buf = target_arg }; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3945 | union semun arg; |
| 3946 | struct semid_ds dsarg; |
vibi sreenivasan | 7b8118e | 2009-06-19 13:34:39 +0530 | [diff] [blame] | 3947 | unsigned short *array = NULL; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3948 | struct seminfo seminfo; |
| 3949 | abi_long ret = -TARGET_EINVAL; |
| 3950 | abi_long err; |
| 3951 | cmd &= 0xff; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3952 | |
| 3953 | switch( cmd ) { |
| 3954 | case GETVAL: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3955 | case SETVAL: |
Tom Musta | 5464bae | 2014-08-12 13:53:34 -0500 | [diff] [blame] | 3956 | /* In 64 bit cross-endian situations, we will erroneously pick up |
| 3957 | * the wrong half of the union for the "val" element. To rectify |
| 3958 | * this, the entire 8-byte structure is byteswapped, followed by |
| 3959 | * a swap of the 4 byte val field. In other cases, the data is |
| 3960 | * already in proper host byte order. */ |
| 3961 | if (sizeof(target_su.val) != (sizeof(target_su.buf))) { |
| 3962 | target_su.buf = tswapal(target_su.buf); |
| 3963 | arg.val = tswap32(target_su.val); |
| 3964 | } else { |
| 3965 | arg.val = target_su.val; |
| 3966 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3967 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3968 | break; |
| 3969 | case GETALL: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3970 | case SETALL: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3971 | err = target_to_host_semarray(semid, &array, target_su.array); |
| 3972 | if (err) |
| 3973 | return err; |
| 3974 | arg.array = array; |
| 3975 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3976 | err = host_to_target_semarray(semid, target_su.array, &array); |
| 3977 | if (err) |
| 3978 | return err; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3979 | break; |
| 3980 | case IPC_STAT: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3981 | case IPC_SET: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3982 | case SEM_STAT: |
| 3983 | err = target_to_host_semid_ds(&dsarg, target_su.buf); |
| 3984 | if (err) |
| 3985 | return err; |
| 3986 | arg.buf = &dsarg; |
| 3987 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3988 | err = host_to_target_semid_ds(target_su.buf, &dsarg); |
| 3989 | if (err) |
| 3990 | return err; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3991 | break; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3992 | case IPC_INFO: |
| 3993 | case SEM_INFO: |
| 3994 | arg.__buf = &seminfo; |
| 3995 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3996 | err = host_to_target_seminfo(target_su.__buf, &seminfo); |
| 3997 | if (err) |
| 3998 | return err; |
| 3999 | break; |
| 4000 | case IPC_RMID: |
| 4001 | case GETPID: |
| 4002 | case GETNCNT: |
| 4003 | case GETZCNT: |
| 4004 | ret = get_errno(semctl(semid, semnum, cmd, NULL)); |
| 4005 | break; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | return ret; |
| 4009 | } |
| 4010 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4011 | struct target_sembuf { |
| 4012 | unsigned short sem_num; |
| 4013 | short sem_op; |
| 4014 | short sem_flg; |
| 4015 | }; |
| 4016 | |
| 4017 | static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, |
| 4018 | abi_ulong target_addr, |
| 4019 | unsigned nsops) |
| 4020 | { |
| 4021 | struct target_sembuf *target_sembuf; |
| 4022 | int i; |
| 4023 | |
| 4024 | target_sembuf = lock_user(VERIFY_READ, target_addr, |
| 4025 | nsops*sizeof(struct target_sembuf), 1); |
| 4026 | if (!target_sembuf) |
| 4027 | return -TARGET_EFAULT; |
| 4028 | |
| 4029 | for(i=0; i<nsops; i++) { |
| 4030 | __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num); |
| 4031 | __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op); |
| 4032 | __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg); |
| 4033 | } |
| 4034 | |
| 4035 | unlock_user(target_sembuf, target_addr, 0); |
| 4036 | |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4040 | #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 4041 | defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4042 | |
| 4043 | /* |
| 4044 | * This macro is required to handle the s390 variants, which passes the |
| 4045 | * arguments in a different order than default. |
| 4046 | */ |
| 4047 | #ifdef __s390x__ |
| 4048 | #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \ |
| 4049 | (__nsops), (__timeout), (__sops) |
| 4050 | #else |
| 4051 | #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \ |
| 4052 | (__nsops), 0, (__sops), (__timeout) |
| 4053 | #endif |
| 4054 | |
| 4055 | static inline abi_long do_semtimedop(int semid, |
| 4056 | abi_long ptr, |
| 4057 | unsigned nsops, |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 4058 | abi_long timeout, bool time64) |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4059 | { |
Filip Bozuta | 0a7ec84 | 2020-08-18 20:07:22 +0200 | [diff] [blame] | 4060 | struct sembuf *sops; |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4061 | struct timespec ts, *pts = NULL; |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4062 | abi_long ret; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4063 | |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4064 | if (timeout) { |
| 4065 | pts = &ts; |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 4066 | if (time64) { |
| 4067 | if (target_to_host_timespec64(pts, timeout)) { |
| 4068 | return -TARGET_EFAULT; |
| 4069 | } |
| 4070 | } else { |
| 4071 | if (target_to_host_timespec(pts, timeout)) { |
| 4072 | return -TARGET_EFAULT; |
| 4073 | } |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4074 | } |
| 4075 | } |
| 4076 | |
Filip Bozuta | 0a7ec84 | 2020-08-18 20:07:22 +0200 | [diff] [blame] | 4077 | if (nsops > TARGET_SEMOPM) { |
| 4078 | return -TARGET_E2BIG; |
| 4079 | } |
| 4080 | |
| 4081 | sops = g_new(struct sembuf, nsops); |
| 4082 | |
| 4083 | if (target_to_host_sembuf(sops, ptr, nsops)) { |
| 4084 | g_free(sops); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4085 | return -TARGET_EFAULT; |
Filip Bozuta | 0a7ec84 | 2020-08-18 20:07:22 +0200 | [diff] [blame] | 4086 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4087 | |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4088 | ret = -TARGET_ENOSYS; |
| 4089 | #ifdef __NR_semtimedop |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4090 | ret = get_errno(safe_semtimedop(semid, sops, nsops, pts)); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4091 | #endif |
| 4092 | #ifdef __NR_ipc |
| 4093 | if (ret == -TARGET_ENOSYS) { |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4094 | ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, |
| 4095 | SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts))); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4096 | } |
| 4097 | #endif |
Filip Bozuta | 0a7ec84 | 2020-08-18 20:07:22 +0200 | [diff] [blame] | 4098 | g_free(sops); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4099 | return ret; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4100 | } |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4101 | #endif |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4102 | |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4103 | struct target_msqid_ds |
| 4104 | { |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4105 | struct target_ipc_perm msg_perm; |
| 4106 | abi_ulong msg_stime; |
| 4107 | #if TARGET_ABI_BITS == 32 |
| 4108 | abi_ulong __unused1; |
| 4109 | #endif |
| 4110 | abi_ulong msg_rtime; |
| 4111 | #if TARGET_ABI_BITS == 32 |
| 4112 | abi_ulong __unused2; |
| 4113 | #endif |
| 4114 | abi_ulong msg_ctime; |
| 4115 | #if TARGET_ABI_BITS == 32 |
| 4116 | abi_ulong __unused3; |
| 4117 | #endif |
| 4118 | abi_ulong __msg_cbytes; |
| 4119 | abi_ulong msg_qnum; |
| 4120 | abi_ulong msg_qbytes; |
| 4121 | abi_ulong msg_lspid; |
| 4122 | abi_ulong msg_lrpid; |
| 4123 | abi_ulong __unused4; |
| 4124 | abi_ulong __unused5; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4125 | }; |
| 4126 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4127 | static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md, |
| 4128 | abi_ulong target_addr) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4129 | { |
| 4130 | struct target_msqid_ds *target_md; |
| 4131 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4132 | if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1)) |
| 4133 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4134 | if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr)) |
| 4135 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 4136 | host_md->msg_stime = tswapal(target_md->msg_stime); |
| 4137 | host_md->msg_rtime = tswapal(target_md->msg_rtime); |
| 4138 | host_md->msg_ctime = tswapal(target_md->msg_ctime); |
| 4139 | host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes); |
| 4140 | host_md->msg_qnum = tswapal(target_md->msg_qnum); |
| 4141 | host_md->msg_qbytes = tswapal(target_md->msg_qbytes); |
| 4142 | host_md->msg_lspid = tswapal(target_md->msg_lspid); |
| 4143 | host_md->msg_lrpid = tswapal(target_md->msg_lrpid); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4144 | unlock_user_struct(target_md, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4145 | return 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4148 | static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr, |
| 4149 | struct msqid_ds *host_md) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4150 | { |
| 4151 | struct target_msqid_ds *target_md; |
| 4152 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4153 | if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) |
| 4154 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4155 | if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm))) |
| 4156 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 4157 | target_md->msg_stime = tswapal(host_md->msg_stime); |
| 4158 | target_md->msg_rtime = tswapal(host_md->msg_rtime); |
| 4159 | target_md->msg_ctime = tswapal(host_md->msg_ctime); |
| 4160 | target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes); |
| 4161 | target_md->msg_qnum = tswapal(host_md->msg_qnum); |
| 4162 | target_md->msg_qbytes = tswapal(host_md->msg_qbytes); |
| 4163 | target_md->msg_lspid = tswapal(host_md->msg_lspid); |
| 4164 | target_md->msg_lrpid = tswapal(host_md->msg_lrpid); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4165 | unlock_user_struct(target_md, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4166 | return 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4169 | struct target_msginfo { |
| 4170 | int msgpool; |
| 4171 | int msgmap; |
| 4172 | int msgmax; |
| 4173 | int msgmnb; |
| 4174 | int msgmni; |
| 4175 | int msgssz; |
| 4176 | int msgtql; |
| 4177 | unsigned short int msgseg; |
| 4178 | }; |
| 4179 | |
| 4180 | static inline abi_long host_to_target_msginfo(abi_ulong target_addr, |
| 4181 | struct msginfo *host_msginfo) |
| 4182 | { |
| 4183 | struct target_msginfo *target_msginfo; |
| 4184 | if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0)) |
| 4185 | return -TARGET_EFAULT; |
| 4186 | __put_user(host_msginfo->msgpool, &target_msginfo->msgpool); |
| 4187 | __put_user(host_msginfo->msgmap, &target_msginfo->msgmap); |
| 4188 | __put_user(host_msginfo->msgmax, &target_msginfo->msgmax); |
| 4189 | __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb); |
| 4190 | __put_user(host_msginfo->msgmni, &target_msginfo->msgmni); |
| 4191 | __put_user(host_msginfo->msgssz, &target_msginfo->msgssz); |
| 4192 | __put_user(host_msginfo->msgtql, &target_msginfo->msgtql); |
| 4193 | __put_user(host_msginfo->msgseg, &target_msginfo->msgseg); |
| 4194 | unlock_user_struct(target_msginfo, target_addr, 1); |
aurel32 | 00b229a | 2008-10-24 13:12:52 +0000 | [diff] [blame] | 4195 | return 0; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4199 | { |
| 4200 | struct msqid_ds dsarg; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4201 | struct msginfo msginfo; |
| 4202 | abi_long ret = -TARGET_EINVAL; |
| 4203 | |
| 4204 | cmd &= 0xff; |
| 4205 | |
| 4206 | switch (cmd) { |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4207 | case IPC_STAT: |
| 4208 | case IPC_SET: |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4209 | case MSG_STAT: |
| 4210 | if (target_to_host_msqid_ds(&dsarg,ptr)) |
| 4211 | return -TARGET_EFAULT; |
| 4212 | ret = get_errno(msgctl(msgid, cmd, &dsarg)); |
| 4213 | if (host_to_target_msqid_ds(ptr,&dsarg)) |
| 4214 | return -TARGET_EFAULT; |
| 4215 | break; |
| 4216 | case IPC_RMID: |
| 4217 | ret = get_errno(msgctl(msgid, cmd, NULL)); |
| 4218 | break; |
| 4219 | case IPC_INFO: |
| 4220 | case MSG_INFO: |
| 4221 | ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo)); |
| 4222 | if (host_to_target_msginfo(ptr, &msginfo)) |
| 4223 | return -TARGET_EFAULT; |
| 4224 | break; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4225 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4226 | |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4227 | return ret; |
| 4228 | } |
| 4229 | |
| 4230 | struct target_msgbuf { |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4231 | abi_long mtype; |
| 4232 | char mtext[1]; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4233 | }; |
| 4234 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4235 | static inline abi_long do_msgsnd(int msqid, abi_long msgp, |
Tom Musta | edcc5f9 | 2014-08-12 13:53:37 -0500 | [diff] [blame] | 4236 | ssize_t msgsz, int msgflg) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4237 | { |
| 4238 | struct target_msgbuf *target_mb; |
| 4239 | struct msgbuf *host_mb; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4240 | abi_long ret = 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4241 | |
Tom Musta | edcc5f9 | 2014-08-12 13:53:37 -0500 | [diff] [blame] | 4242 | if (msgsz < 0) { |
| 4243 | return -TARGET_EINVAL; |
| 4244 | } |
| 4245 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4246 | if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0)) |
| 4247 | return -TARGET_EFAULT; |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4248 | host_mb = g_try_malloc(msgsz + sizeof(long)); |
zhanghailiang | 29e03fc | 2014-08-14 15:29:18 +0800 | [diff] [blame] | 4249 | if (!host_mb) { |
| 4250 | unlock_user_struct(target_mb, msgp, 0); |
| 4251 | return -TARGET_ENOMEM; |
| 4252 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 4253 | host_mb->mtype = (abi_long) tswapal(target_mb->mtype); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4254 | memcpy(host_mb->mtext, target_mb->mtext, msgsz); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4255 | ret = -TARGET_ENOSYS; |
| 4256 | #ifdef __NR_msgsnd |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 4257 | ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg)); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4258 | #endif |
| 4259 | #ifdef __NR_ipc |
| 4260 | if (ret == -TARGET_ENOSYS) { |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4261 | #ifdef __s390x__ |
| 4262 | ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, |
| 4263 | host_mb)); |
| 4264 | #else |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4265 | ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, |
| 4266 | host_mb, 0)); |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4267 | #endif |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4268 | } |
| 4269 | #endif |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4270 | g_free(host_mb); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4271 | unlock_user_struct(target_mb, msgp, 0); |
| 4272 | |
| 4273 | return ret; |
| 4274 | } |
| 4275 | |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4276 | #ifdef __NR_ipc |
| 4277 | #if defined(__sparc__) |
| 4278 | /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */ |
| 4279 | #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp |
| 4280 | #elif defined(__s390x__) |
| 4281 | /* The s390 sys_ipc variant has only five parameters. */ |
| 4282 | #define MSGRCV_ARGS(__msgp, __msgtyp) \ |
| 4283 | ((long int[]){(long int)__msgp, __msgtyp}) |
| 4284 | #else |
| 4285 | #define MSGRCV_ARGS(__msgp, __msgtyp) \ |
| 4286 | ((long int[]){(long int)__msgp, __msgtyp}), 0 |
| 4287 | #endif |
| 4288 | #endif |
| 4289 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4290 | static inline abi_long do_msgrcv(int msqid, abi_long msgp, |
Peter Maydell | 99874f6 | 2016-05-20 19:00:56 +0100 | [diff] [blame] | 4291 | ssize_t msgsz, abi_long msgtyp, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4292 | int msgflg) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4293 | { |
| 4294 | struct target_msgbuf *target_mb; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4295 | char *target_mtext; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4296 | struct msgbuf *host_mb; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4297 | abi_long ret = 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4298 | |
Peter Maydell | 99874f6 | 2016-05-20 19:00:56 +0100 | [diff] [blame] | 4299 | if (msgsz < 0) { |
| 4300 | return -TARGET_EINVAL; |
| 4301 | } |
| 4302 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4303 | if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0)) |
| 4304 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4305 | |
Peter Maydell | 415d847 | 2016-05-20 19:00:57 +0100 | [diff] [blame] | 4306 | host_mb = g_try_malloc(msgsz + sizeof(long)); |
| 4307 | if (!host_mb) { |
| 4308 | ret = -TARGET_ENOMEM; |
| 4309 | goto end; |
| 4310 | } |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4311 | ret = -TARGET_ENOSYS; |
| 4312 | #ifdef __NR_msgrcv |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 4313 | ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg)); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4314 | #endif |
| 4315 | #ifdef __NR_ipc |
| 4316 | if (ret == -TARGET_ENOSYS) { |
| 4317 | ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz, |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4318 | msgflg, MSGRCV_ARGS(host_mb, msgtyp))); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4319 | } |
| 4320 | #endif |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4321 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4322 | if (ret > 0) { |
| 4323 | abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong); |
| 4324 | target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0); |
| 4325 | if (!target_mtext) { |
| 4326 | ret = -TARGET_EFAULT; |
| 4327 | goto end; |
| 4328 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4329 | memcpy(target_mb->mtext, host_mb->mtext, ret); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4330 | unlock_user(target_mtext, target_mtext_addr, ret); |
| 4331 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4332 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 4333 | target_mb->mtype = tswapal(host_mb->mtype); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4334 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4335 | end: |
| 4336 | if (target_mb) |
| 4337 | unlock_user_struct(target_mb, msgp, 1); |
Jim Meyering | 0d07fe4 | 2012-08-22 13:55:53 +0200 | [diff] [blame] | 4338 | g_free(host_mb); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4339 | return ret; |
| 4340 | } |
| 4341 | |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4342 | static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd, |
| 4343 | abi_ulong target_addr) |
| 4344 | { |
| 4345 | struct target_shmid_ds *target_sd; |
| 4346 | |
| 4347 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 4348 | return -TARGET_EFAULT; |
| 4349 | if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr)) |
| 4350 | return -TARGET_EFAULT; |
| 4351 | __get_user(host_sd->shm_segsz, &target_sd->shm_segsz); |
| 4352 | __get_user(host_sd->shm_atime, &target_sd->shm_atime); |
| 4353 | __get_user(host_sd->shm_dtime, &target_sd->shm_dtime); |
| 4354 | __get_user(host_sd->shm_ctime, &target_sd->shm_ctime); |
| 4355 | __get_user(host_sd->shm_cpid, &target_sd->shm_cpid); |
| 4356 | __get_user(host_sd->shm_lpid, &target_sd->shm_lpid); |
| 4357 | __get_user(host_sd->shm_nattch, &target_sd->shm_nattch); |
| 4358 | unlock_user_struct(target_sd, target_addr, 0); |
| 4359 | return 0; |
| 4360 | } |
| 4361 | |
| 4362 | static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr, |
| 4363 | struct shmid_ds *host_sd) |
| 4364 | { |
| 4365 | struct target_shmid_ds *target_sd; |
| 4366 | |
| 4367 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 4368 | return -TARGET_EFAULT; |
| 4369 | if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm))) |
| 4370 | return -TARGET_EFAULT; |
| 4371 | __put_user(host_sd->shm_segsz, &target_sd->shm_segsz); |
| 4372 | __put_user(host_sd->shm_atime, &target_sd->shm_atime); |
| 4373 | __put_user(host_sd->shm_dtime, &target_sd->shm_dtime); |
| 4374 | __put_user(host_sd->shm_ctime, &target_sd->shm_ctime); |
| 4375 | __put_user(host_sd->shm_cpid, &target_sd->shm_cpid); |
| 4376 | __put_user(host_sd->shm_lpid, &target_sd->shm_lpid); |
| 4377 | __put_user(host_sd->shm_nattch, &target_sd->shm_nattch); |
| 4378 | unlock_user_struct(target_sd, target_addr, 1); |
| 4379 | return 0; |
| 4380 | } |
| 4381 | |
| 4382 | struct target_shminfo { |
| 4383 | abi_ulong shmmax; |
| 4384 | abi_ulong shmmin; |
| 4385 | abi_ulong shmmni; |
| 4386 | abi_ulong shmseg; |
| 4387 | abi_ulong shmall; |
| 4388 | }; |
| 4389 | |
| 4390 | static inline abi_long host_to_target_shminfo(abi_ulong target_addr, |
| 4391 | struct shminfo *host_shminfo) |
| 4392 | { |
| 4393 | struct target_shminfo *target_shminfo; |
| 4394 | if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0)) |
| 4395 | return -TARGET_EFAULT; |
| 4396 | __put_user(host_shminfo->shmmax, &target_shminfo->shmmax); |
| 4397 | __put_user(host_shminfo->shmmin, &target_shminfo->shmmin); |
| 4398 | __put_user(host_shminfo->shmmni, &target_shminfo->shmmni); |
| 4399 | __put_user(host_shminfo->shmseg, &target_shminfo->shmseg); |
| 4400 | __put_user(host_shminfo->shmall, &target_shminfo->shmall); |
| 4401 | unlock_user_struct(target_shminfo, target_addr, 1); |
| 4402 | return 0; |
| 4403 | } |
| 4404 | |
| 4405 | struct target_shm_info { |
| 4406 | int used_ids; |
| 4407 | abi_ulong shm_tot; |
| 4408 | abi_ulong shm_rss; |
| 4409 | abi_ulong shm_swp; |
| 4410 | abi_ulong swap_attempts; |
| 4411 | abi_ulong swap_successes; |
| 4412 | }; |
| 4413 | |
| 4414 | static inline abi_long host_to_target_shm_info(abi_ulong target_addr, |
| 4415 | struct shm_info *host_shm_info) |
| 4416 | { |
| 4417 | struct target_shm_info *target_shm_info; |
| 4418 | if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0)) |
| 4419 | return -TARGET_EFAULT; |
| 4420 | __put_user(host_shm_info->used_ids, &target_shm_info->used_ids); |
| 4421 | __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot); |
| 4422 | __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss); |
| 4423 | __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp); |
| 4424 | __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts); |
| 4425 | __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes); |
| 4426 | unlock_user_struct(target_shm_info, target_addr, 1); |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
| 4430 | static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf) |
| 4431 | { |
| 4432 | struct shmid_ds dsarg; |
| 4433 | struct shminfo shminfo; |
| 4434 | struct shm_info shm_info; |
| 4435 | abi_long ret = -TARGET_EINVAL; |
| 4436 | |
| 4437 | cmd &= 0xff; |
| 4438 | |
| 4439 | switch(cmd) { |
| 4440 | case IPC_STAT: |
| 4441 | case IPC_SET: |
| 4442 | case SHM_STAT: |
| 4443 | if (target_to_host_shmid_ds(&dsarg, buf)) |
| 4444 | return -TARGET_EFAULT; |
| 4445 | ret = get_errno(shmctl(shmid, cmd, &dsarg)); |
| 4446 | if (host_to_target_shmid_ds(buf, &dsarg)) |
| 4447 | return -TARGET_EFAULT; |
| 4448 | break; |
| 4449 | case IPC_INFO: |
| 4450 | ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo)); |
| 4451 | if (host_to_target_shminfo(buf, &shminfo)) |
| 4452 | return -TARGET_EFAULT; |
| 4453 | break; |
| 4454 | case SHM_INFO: |
| 4455 | ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info)); |
| 4456 | if (host_to_target_shm_info(buf, &shm_info)) |
| 4457 | return -TARGET_EFAULT; |
| 4458 | break; |
| 4459 | case IPC_RMID: |
| 4460 | case SHM_LOCK: |
| 4461 | case SHM_UNLOCK: |
| 4462 | ret = get_errno(shmctl(shmid, cmd, NULL)); |
| 4463 | break; |
| 4464 | } |
| 4465 | |
| 4466 | return ret; |
| 4467 | } |
| 4468 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4469 | #ifdef TARGET_NR_ipc |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4470 | /* ??? This only works with linear mappings. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4471 | /* do_ipc() must return target values and target errnos. */ |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4472 | static abi_long do_ipc(CPUArchState *cpu_env, |
| 4473 | unsigned int call, abi_long first, |
Tom Musta | 37ed095 | 2014-08-12 13:53:35 -0500 | [diff] [blame] | 4474 | abi_long second, abi_long third, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4475 | abi_long ptr, abi_long fifth) |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4476 | { |
| 4477 | int version; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4478 | abi_long ret = 0; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4479 | |
| 4480 | version = call >> 16; |
| 4481 | call &= 0xffff; |
| 4482 | |
| 4483 | switch (call) { |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4484 | case IPCOP_semop: |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 4485 | ret = do_semtimedop(first, ptr, second, 0, false); |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4486 | break; |
| 4487 | case IPCOP_semtimedop: |
| 4488 | /* |
| 4489 | * The s390 sys_ipc variant has only five parameters instead of six |
| 4490 | * (as for default variant) and the only difference is the handling of |
| 4491 | * SEMTIMEDOP where on s390 the third parameter is used as a pointer |
| 4492 | * to a struct timespec where the generic variant uses fifth parameter. |
| 4493 | */ |
| 4494 | #if defined(TARGET_S390X) |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 4495 | ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4496 | #else |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 4497 | ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 4498 | #endif |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4499 | break; |
| 4500 | |
| 4501 | case IPCOP_semget: |
| 4502 | ret = get_errno(semget(first, second, third)); |
| 4503 | break; |
| 4504 | |
Tom Musta | 5d2fa8e | 2014-08-12 13:53:33 -0500 | [diff] [blame] | 4505 | case IPCOP_semctl: { |
| 4506 | /* The semun argument to semctl is passed by value, so dereference the |
| 4507 | * ptr argument. */ |
| 4508 | abi_ulong atptr; |
Tom Musta | 37ed095 | 2014-08-12 13:53:35 -0500 | [diff] [blame] | 4509 | get_user_ual(atptr, ptr); |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 4510 | ret = do_semctl(first, second, third, atptr); |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4511 | break; |
Tom Musta | 5d2fa8e | 2014-08-12 13:53:33 -0500 | [diff] [blame] | 4512 | } |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4513 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4514 | case IPCOP_msgget: |
| 4515 | ret = get_errno(msgget(first, second)); |
| 4516 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4517 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4518 | case IPCOP_msgsnd: |
| 4519 | ret = do_msgsnd(first, ptr, second, third); |
| 4520 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4521 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4522 | case IPCOP_msgctl: |
| 4523 | ret = do_msgctl(first, second, ptr); |
| 4524 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4525 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4526 | case IPCOP_msgrcv: |
| 4527 | switch (version) { |
| 4528 | case 0: |
| 4529 | { |
| 4530 | struct target_ipc_kludge { |
| 4531 | abi_long msgp; |
| 4532 | abi_long msgtyp; |
| 4533 | } *tmp; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4534 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4535 | if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) { |
| 4536 | ret = -TARGET_EFAULT; |
| 4537 | break; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4538 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4539 | |
Laurent Vivier | 79dd77d | 2012-12-20 11:00:11 +0000 | [diff] [blame] | 4540 | ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4541 | |
| 4542 | unlock_user_struct(tmp, ptr, 0); |
| 4543 | break; |
| 4544 | } |
| 4545 | default: |
| 4546 | ret = do_msgrcv(first, ptr, second, fifth, third); |
| 4547 | } |
| 4548 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4549 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4550 | case IPCOP_shmat: |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4551 | switch (version) { |
| 4552 | default: |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4553 | { |
| 4554 | abi_ulong raddr; |
Richard Henderson | 225a206 | 2023-08-20 09:24:14 -0700 | [diff] [blame] | 4555 | raddr = target_shmat(cpu_env, first, ptr, second); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4556 | if (is_error(raddr)) |
| 4557 | return get_errno(raddr); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 4558 | if (put_user_ual(raddr, third)) |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4559 | return -TARGET_EFAULT; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4560 | break; |
| 4561 | } |
| 4562 | case 1: |
| 4563 | ret = -TARGET_EINVAL; |
| 4564 | break; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4565 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4566 | break; |
| 4567 | case IPCOP_shmdt: |
Richard Henderson | 225a206 | 2023-08-20 09:24:14 -0700 | [diff] [blame] | 4568 | ret = target_shmdt(ptr); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4569 | break; |
| 4570 | |
| 4571 | case IPCOP_shmget: |
| 4572 | /* IPC_* flag values are the same on all linux platforms */ |
| 4573 | ret = get_errno(shmget(first, second, third)); |
| 4574 | break; |
| 4575 | |
| 4576 | /* IPC_* and SHM_* command values are the same on all linux platforms */ |
| 4577 | case IPCOP_shmctl: |
Petar Jovanovic | a292678 | 2013-10-30 14:46:32 +0100 | [diff] [blame] | 4578 | ret = do_shmctl(first, second, ptr); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4579 | break; |
| 4580 | default: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 4581 | qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n", |
| 4582 | call, version); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4583 | ret = -TARGET_ENOSYS; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4584 | break; |
| 4585 | } |
| 4586 | return ret; |
| 4587 | } |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4588 | #endif |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4589 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4590 | /* kernel structure types definitions */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4591 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4592 | #define STRUCT(name, ...) STRUCT_ ## name, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4593 | #define STRUCT_SPECIAL(name) STRUCT_ ## name, |
| 4594 | enum { |
| 4595 | #include "syscall_types.h" |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 4596 | STRUCT_MAX |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4597 | }; |
| 4598 | #undef STRUCT |
| 4599 | #undef STRUCT_SPECIAL |
| 4600 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4601 | #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL }; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4602 | #define STRUCT_SPECIAL(name) |
| 4603 | #include "syscall_types.h" |
| 4604 | #undef STRUCT |
| 4605 | #undef STRUCT_SPECIAL |
| 4606 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4607 | #define MAX_STRUCT_SIZE 4096 |
| 4608 | |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 4609 | #ifdef CONFIG_FIEMAP |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4610 | /* So fiemap access checks don't overflow on 32 bit systems. |
| 4611 | * This is very slightly smaller than the limit imposed by |
| 4612 | * the underlying kernel. |
| 4613 | */ |
| 4614 | #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \ |
| 4615 | / sizeof(struct fiemap_extent)) |
| 4616 | |
| 4617 | static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 4618 | int fd, int cmd, abi_long arg) |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4619 | { |
| 4620 | /* The parameter for this ioctl is a struct fiemap followed |
| 4621 | * by an array of struct fiemap_extent whose size is set |
| 4622 | * in fiemap->fm_extent_count. The array is filled in by the |
| 4623 | * ioctl. |
| 4624 | */ |
| 4625 | int target_size_in, target_size_out; |
| 4626 | struct fiemap *fm; |
| 4627 | const argtype *arg_type = ie->arg_type; |
| 4628 | const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) }; |
| 4629 | void *argptr, *p; |
| 4630 | abi_long ret; |
| 4631 | int i, extent_size = thunk_type_size(extent_arg_type, 0); |
| 4632 | uint32_t outbufsz; |
| 4633 | int free_fm = 0; |
| 4634 | |
| 4635 | assert(arg_type[0] == TYPE_PTR); |
| 4636 | assert(ie->access == IOC_RW); |
| 4637 | arg_type++; |
| 4638 | target_size_in = thunk_type_size(arg_type, 0); |
| 4639 | argptr = lock_user(VERIFY_READ, arg, target_size_in, 1); |
| 4640 | if (!argptr) { |
| 4641 | return -TARGET_EFAULT; |
| 4642 | } |
| 4643 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4644 | unlock_user(argptr, arg, 0); |
| 4645 | fm = (struct fiemap *)buf_temp; |
| 4646 | if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) { |
| 4647 | return -TARGET_EINVAL; |
| 4648 | } |
| 4649 | |
| 4650 | outbufsz = sizeof (*fm) + |
| 4651 | (sizeof(struct fiemap_extent) * fm->fm_extent_count); |
| 4652 | |
| 4653 | if (outbufsz > MAX_STRUCT_SIZE) { |
| 4654 | /* We can't fit all the extents into the fixed size buffer. |
| 4655 | * Allocate one that is large enough and use it instead. |
| 4656 | */ |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4657 | fm = g_try_malloc(outbufsz); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4658 | if (!fm) { |
| 4659 | return -TARGET_ENOMEM; |
| 4660 | } |
| 4661 | memcpy(fm, buf_temp, sizeof(struct fiemap)); |
| 4662 | free_fm = 1; |
| 4663 | } |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4664 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm)); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4665 | if (!is_error(ret)) { |
| 4666 | target_size_out = target_size_in; |
| 4667 | /* An extent_count of 0 means we were only counting the extents |
| 4668 | * so there are no structs to copy |
| 4669 | */ |
| 4670 | if (fm->fm_extent_count != 0) { |
| 4671 | target_size_out += fm->fm_mapped_extents * extent_size; |
| 4672 | } |
| 4673 | argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0); |
| 4674 | if (!argptr) { |
| 4675 | ret = -TARGET_EFAULT; |
| 4676 | } else { |
| 4677 | /* Convert the struct fiemap */ |
| 4678 | thunk_convert(argptr, fm, arg_type, THUNK_TARGET); |
| 4679 | if (fm->fm_extent_count != 0) { |
| 4680 | p = argptr + target_size_in; |
| 4681 | /* ...and then all the struct fiemap_extents */ |
| 4682 | for (i = 0; i < fm->fm_mapped_extents; i++) { |
| 4683 | thunk_convert(p, &fm->fm_extents[i], extent_arg_type, |
| 4684 | THUNK_TARGET); |
| 4685 | p += extent_size; |
| 4686 | } |
| 4687 | } |
| 4688 | unlock_user(argptr, arg, target_size_out); |
| 4689 | } |
| 4690 | } |
| 4691 | if (free_fm) { |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4692 | g_free(fm); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4693 | } |
| 4694 | return ret; |
| 4695 | } |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 4696 | #endif |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4697 | |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4698 | static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 4699 | int fd, int cmd, abi_long arg) |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4700 | { |
| 4701 | const argtype *arg_type = ie->arg_type; |
| 4702 | int target_size; |
| 4703 | void *argptr; |
| 4704 | int ret; |
| 4705 | struct ifconf *host_ifconf; |
| 4706 | uint32_t outbufsz; |
| 4707 | const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) }; |
Stefan | 4df7b7f | 2021-01-09 19:59:42 +0100 | [diff] [blame] | 4708 | const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) }; |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4709 | int target_ifreq_size; |
| 4710 | int nb_ifreq; |
| 4711 | int free_buf = 0; |
| 4712 | int i; |
| 4713 | int target_ifc_len; |
| 4714 | abi_long target_ifc_buf; |
| 4715 | int host_ifc_len; |
| 4716 | char *host_ifc_buf; |
| 4717 | |
| 4718 | assert(arg_type[0] == TYPE_PTR); |
| 4719 | assert(ie->access == IOC_RW); |
| 4720 | |
| 4721 | arg_type++; |
| 4722 | target_size = thunk_type_size(arg_type, 0); |
| 4723 | |
| 4724 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4725 | if (!argptr) |
| 4726 | return -TARGET_EFAULT; |
| 4727 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4728 | unlock_user(argptr, arg, 0); |
| 4729 | |
| 4730 | host_ifconf = (struct ifconf *)(unsigned long)buf_temp; |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4731 | target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf; |
Stefan | 4df7b7f | 2021-01-09 19:59:42 +0100 | [diff] [blame] | 4732 | target_ifreq_size = thunk_type_size(ifreq_max_type, 0); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4733 | |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4734 | if (target_ifc_buf != 0) { |
| 4735 | target_ifc_len = host_ifconf->ifc_len; |
| 4736 | nb_ifreq = target_ifc_len / target_ifreq_size; |
| 4737 | host_ifc_len = nb_ifreq * sizeof(struct ifreq); |
| 4738 | |
| 4739 | outbufsz = sizeof(*host_ifconf) + host_ifc_len; |
| 4740 | if (outbufsz > MAX_STRUCT_SIZE) { |
| 4741 | /* |
| 4742 | * We can't fit all the extents into the fixed size buffer. |
| 4743 | * Allocate one that is large enough and use it instead. |
| 4744 | */ |
Ahmed Abouzied | 7a5626a | 2022-01-04 16:38:41 +0200 | [diff] [blame] | 4745 | host_ifconf = g_try_malloc(outbufsz); |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4746 | if (!host_ifconf) { |
| 4747 | return -TARGET_ENOMEM; |
| 4748 | } |
| 4749 | memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf)); |
| 4750 | free_buf = 1; |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4751 | } |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4752 | host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4753 | |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4754 | host_ifconf->ifc_len = host_ifc_len; |
| 4755 | } else { |
| 4756 | host_ifc_buf = NULL; |
| 4757 | } |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4758 | host_ifconf->ifc_buf = host_ifc_buf; |
| 4759 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4760 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf)); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4761 | if (!is_error(ret)) { |
| 4762 | /* convert host ifc_len to target ifc_len */ |
| 4763 | |
| 4764 | nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq); |
| 4765 | target_ifc_len = nb_ifreq * target_ifreq_size; |
| 4766 | host_ifconf->ifc_len = target_ifc_len; |
| 4767 | |
| 4768 | /* restore target ifc_buf */ |
| 4769 | |
| 4770 | host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf; |
| 4771 | |
| 4772 | /* copy struct ifconf to target user */ |
| 4773 | |
| 4774 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4775 | if (!argptr) |
| 4776 | return -TARGET_EFAULT; |
| 4777 | thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET); |
| 4778 | unlock_user(argptr, arg, target_size); |
| 4779 | |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4780 | if (target_ifc_buf != 0) { |
| 4781 | /* copy ifreq[] to target user */ |
| 4782 | argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0); |
| 4783 | for (i = 0; i < nb_ifreq ; i++) { |
| 4784 | thunk_convert(argptr + i * target_ifreq_size, |
| 4785 | host_ifc_buf + i * sizeof(struct ifreq), |
| 4786 | ifreq_arg_type, THUNK_TARGET); |
| 4787 | } |
| 4788 | unlock_user(argptr, target_ifc_buf, target_ifc_len); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4789 | } |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | if (free_buf) { |
Ahmed Abouzied | 7a5626a | 2022-01-04 16:38:41 +0200 | [diff] [blame] | 4793 | g_free(host_ifconf); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | return ret; |
| 4797 | } |
| 4798 | |
Cortland Tölva | a133367 | 2018-10-08 09:35:21 -0700 | [diff] [blame] | 4799 | #if defined(CONFIG_USBFS) |
| 4800 | #if HOST_LONG_BITS > 64 |
| 4801 | #error USBDEVFS thunks do not support >64 bit hosts yet. |
| 4802 | #endif |
| 4803 | struct live_urb { |
| 4804 | uint64_t target_urb_adr; |
| 4805 | uint64_t target_buf_adr; |
| 4806 | char *target_buf_ptr; |
| 4807 | struct usbdevfs_urb host_urb; |
| 4808 | }; |
| 4809 | |
| 4810 | static GHashTable *usbdevfs_urb_hashtable(void) |
| 4811 | { |
| 4812 | static GHashTable *urb_hashtable; |
| 4813 | |
| 4814 | if (!urb_hashtable) { |
| 4815 | urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal); |
| 4816 | } |
| 4817 | return urb_hashtable; |
| 4818 | } |
| 4819 | |
| 4820 | static void urb_hashtable_insert(struct live_urb *urb) |
| 4821 | { |
| 4822 | GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); |
| 4823 | g_hash_table_insert(urb_hashtable, urb, urb); |
| 4824 | } |
| 4825 | |
| 4826 | static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr) |
| 4827 | { |
| 4828 | GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); |
| 4829 | return g_hash_table_lookup(urb_hashtable, &target_urb_adr); |
| 4830 | } |
| 4831 | |
| 4832 | static void urb_hashtable_remove(struct live_urb *urb) |
| 4833 | { |
| 4834 | GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); |
| 4835 | g_hash_table_remove(urb_hashtable, urb); |
| 4836 | } |
| 4837 | |
| 4838 | static abi_long |
| 4839 | do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 4840 | int fd, int cmd, abi_long arg) |
| 4841 | { |
| 4842 | const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) }; |
| 4843 | const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 }; |
| 4844 | struct live_urb *lurb; |
| 4845 | void *argptr; |
| 4846 | uint64_t hurb; |
| 4847 | int target_size; |
| 4848 | uintptr_t target_urb_adr; |
| 4849 | abi_long ret; |
| 4850 | |
| 4851 | target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET); |
| 4852 | |
| 4853 | memset(buf_temp, 0, sizeof(uint64_t)); |
| 4854 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
| 4855 | if (is_error(ret)) { |
| 4856 | return ret; |
| 4857 | } |
| 4858 | |
| 4859 | memcpy(&hurb, buf_temp, sizeof(uint64_t)); |
| 4860 | lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb)); |
| 4861 | if (!lurb->target_urb_adr) { |
| 4862 | return -TARGET_EFAULT; |
| 4863 | } |
| 4864 | urb_hashtable_remove(lurb); |
| 4865 | unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, |
| 4866 | lurb->host_urb.buffer_length); |
| 4867 | lurb->target_buf_ptr = NULL; |
| 4868 | |
| 4869 | /* restore the guest buffer pointer */ |
| 4870 | lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr; |
| 4871 | |
| 4872 | /* update the guest urb struct */ |
| 4873 | argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0); |
| 4874 | if (!argptr) { |
| 4875 | g_free(lurb); |
| 4876 | return -TARGET_EFAULT; |
| 4877 | } |
| 4878 | thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET); |
| 4879 | unlock_user(argptr, lurb->target_urb_adr, target_size); |
| 4880 | |
| 4881 | target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET); |
| 4882 | /* write back the urb handle */ |
| 4883 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4884 | if (!argptr) { |
| 4885 | g_free(lurb); |
| 4886 | return -TARGET_EFAULT; |
| 4887 | } |
| 4888 | |
| 4889 | /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */ |
| 4890 | target_urb_adr = lurb->target_urb_adr; |
| 4891 | thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET); |
| 4892 | unlock_user(argptr, arg, target_size); |
| 4893 | |
| 4894 | g_free(lurb); |
| 4895 | return ret; |
| 4896 | } |
| 4897 | |
| 4898 | static abi_long |
| 4899 | do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie, |
| 4900 | uint8_t *buf_temp __attribute__((unused)), |
| 4901 | int fd, int cmd, abi_long arg) |
| 4902 | { |
| 4903 | struct live_urb *lurb; |
| 4904 | |
| 4905 | /* map target address back to host URB with metadata. */ |
| 4906 | lurb = urb_hashtable_lookup(arg); |
| 4907 | if (!lurb) { |
| 4908 | return -TARGET_EFAULT; |
| 4909 | } |
| 4910 | return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); |
| 4911 | } |
| 4912 | |
| 4913 | static abi_long |
| 4914 | do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 4915 | int fd, int cmd, abi_long arg) |
| 4916 | { |
| 4917 | const argtype *arg_type = ie->arg_type; |
| 4918 | int target_size; |
| 4919 | abi_long ret; |
| 4920 | void *argptr; |
| 4921 | int rw_dir; |
| 4922 | struct live_urb *lurb; |
| 4923 | |
| 4924 | /* |
| 4925 | * each submitted URB needs to map to a unique ID for the |
| 4926 | * kernel, and that unique ID needs to be a pointer to |
| 4927 | * host memory. hence, we need to malloc for each URB. |
| 4928 | * isochronous transfers have a variable length struct. |
| 4929 | */ |
| 4930 | arg_type++; |
| 4931 | target_size = thunk_type_size(arg_type, THUNK_TARGET); |
| 4932 | |
| 4933 | /* construct host copy of urb and metadata */ |
Markus Armbruster | b21e238 | 2022-03-15 15:41:56 +0100 | [diff] [blame] | 4934 | lurb = g_try_new0(struct live_urb, 1); |
Cortland Tölva | a133367 | 2018-10-08 09:35:21 -0700 | [diff] [blame] | 4935 | if (!lurb) { |
| 4936 | return -TARGET_ENOMEM; |
| 4937 | } |
| 4938 | |
| 4939 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4940 | if (!argptr) { |
| 4941 | g_free(lurb); |
| 4942 | return -TARGET_EFAULT; |
| 4943 | } |
| 4944 | thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST); |
| 4945 | unlock_user(argptr, arg, 0); |
| 4946 | |
| 4947 | lurb->target_urb_adr = arg; |
| 4948 | lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer; |
| 4949 | |
| 4950 | /* buffer space used depends on endpoint type so lock the entire buffer */ |
| 4951 | /* control type urbs should check the buffer contents for true direction */ |
| 4952 | rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ; |
| 4953 | lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr, |
| 4954 | lurb->host_urb.buffer_length, 1); |
| 4955 | if (lurb->target_buf_ptr == NULL) { |
| 4956 | g_free(lurb); |
| 4957 | return -TARGET_EFAULT; |
| 4958 | } |
| 4959 | |
| 4960 | /* update buffer pointer in host copy */ |
| 4961 | lurb->host_urb.buffer = lurb->target_buf_ptr; |
| 4962 | |
| 4963 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); |
| 4964 | if (is_error(ret)) { |
| 4965 | unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0); |
| 4966 | g_free(lurb); |
| 4967 | } else { |
| 4968 | urb_hashtable_insert(lurb); |
| 4969 | } |
| 4970 | |
| 4971 | return ret; |
| 4972 | } |
| 4973 | #endif /* CONFIG_USBFS */ |
| 4974 | |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4975 | static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 4976 | int cmd, abi_long arg) |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4977 | { |
| 4978 | void *argptr; |
| 4979 | struct dm_ioctl *host_dm; |
| 4980 | abi_long guest_data; |
| 4981 | uint32_t guest_data_size; |
| 4982 | int target_size; |
| 4983 | const argtype *arg_type = ie->arg_type; |
| 4984 | abi_long ret; |
| 4985 | void *big_buf = NULL; |
| 4986 | char *host_data; |
| 4987 | |
| 4988 | arg_type++; |
| 4989 | target_size = thunk_type_size(arg_type, 0); |
| 4990 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4991 | if (!argptr) { |
| 4992 | ret = -TARGET_EFAULT; |
| 4993 | goto out; |
| 4994 | } |
| 4995 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4996 | unlock_user(argptr, arg, 0); |
| 4997 | |
| 4998 | /* buf_temp is too small, so fetch things into a bigger buffer */ |
| 4999 | big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2); |
| 5000 | memcpy(big_buf, buf_temp, target_size); |
| 5001 | buf_temp = big_buf; |
| 5002 | host_dm = big_buf; |
| 5003 | |
| 5004 | guest_data = arg + host_dm->data_start; |
| 5005 | if ((guest_data - arg) < 0) { |
Peter Maydell | f9757b1 | 2016-07-12 13:02:14 +0100 | [diff] [blame] | 5006 | ret = -TARGET_EINVAL; |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5007 | goto out; |
| 5008 | } |
| 5009 | guest_data_size = host_dm->data_size - host_dm->data_start; |
| 5010 | host_data = (char*)host_dm + host_dm->data_start; |
| 5011 | |
| 5012 | argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1); |
Peter Maydell | 3211215 | 2016-07-12 13:02:13 +0100 | [diff] [blame] | 5013 | if (!argptr) { |
| 5014 | ret = -TARGET_EFAULT; |
| 5015 | goto out; |
| 5016 | } |
| 5017 | |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5018 | switch (ie->host_cmd) { |
| 5019 | case DM_REMOVE_ALL: |
| 5020 | case DM_LIST_DEVICES: |
| 5021 | case DM_DEV_CREATE: |
| 5022 | case DM_DEV_REMOVE: |
| 5023 | case DM_DEV_SUSPEND: |
| 5024 | case DM_DEV_STATUS: |
| 5025 | case DM_DEV_WAIT: |
| 5026 | case DM_TABLE_STATUS: |
| 5027 | case DM_TABLE_CLEAR: |
| 5028 | case DM_TABLE_DEPS: |
| 5029 | case DM_LIST_VERSIONS: |
| 5030 | /* no input data */ |
| 5031 | break; |
| 5032 | case DM_DEV_RENAME: |
| 5033 | case DM_DEV_SET_GEOMETRY: |
| 5034 | /* data contains only strings */ |
| 5035 | memcpy(host_data, argptr, guest_data_size); |
| 5036 | break; |
| 5037 | case DM_TARGET_MSG: |
| 5038 | memcpy(host_data, argptr, guest_data_size); |
| 5039 | *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr); |
| 5040 | break; |
| 5041 | case DM_TABLE_LOAD: |
| 5042 | { |
| 5043 | void *gspec = argptr; |
| 5044 | void *cur_data = host_data; |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5045 | const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; |
| 5046 | int spec_size = thunk_type_size(dm_arg_type, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5047 | int i; |
| 5048 | |
| 5049 | for (i = 0; i < host_dm->target_count; i++) { |
| 5050 | struct dm_target_spec *spec = cur_data; |
| 5051 | uint32_t next; |
| 5052 | int slen; |
| 5053 | |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5054 | thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5055 | slen = strlen((char*)gspec + spec_size) + 1; |
| 5056 | next = spec->next; |
| 5057 | spec->next = sizeof(*spec) + slen; |
| 5058 | strcpy((char*)&spec[1], gspec + spec_size); |
| 5059 | gspec += next; |
| 5060 | cur_data += spec->next; |
| 5061 | } |
| 5062 | break; |
| 5063 | } |
| 5064 | default: |
| 5065 | ret = -TARGET_EINVAL; |
Chen Gang S | dec0473 | 2015-01-25 08:00:42 +0800 | [diff] [blame] | 5066 | unlock_user(argptr, guest_data, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5067 | goto out; |
| 5068 | } |
| 5069 | unlock_user(argptr, guest_data, 0); |
| 5070 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5071 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5072 | if (!is_error(ret)) { |
| 5073 | guest_data = arg + host_dm->data_start; |
| 5074 | guest_data_size = host_dm->data_size - host_dm->data_start; |
| 5075 | argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0); |
| 5076 | switch (ie->host_cmd) { |
| 5077 | case DM_REMOVE_ALL: |
| 5078 | case DM_DEV_CREATE: |
| 5079 | case DM_DEV_REMOVE: |
| 5080 | case DM_DEV_RENAME: |
| 5081 | case DM_DEV_SUSPEND: |
| 5082 | case DM_DEV_STATUS: |
| 5083 | case DM_TABLE_LOAD: |
| 5084 | case DM_TABLE_CLEAR: |
| 5085 | case DM_TARGET_MSG: |
| 5086 | case DM_DEV_SET_GEOMETRY: |
| 5087 | /* no return data */ |
| 5088 | break; |
| 5089 | case DM_LIST_DEVICES: |
| 5090 | { |
| 5091 | struct dm_name_list *nl = (void*)host_dm + host_dm->data_start; |
| 5092 | uint32_t remaining_data = guest_data_size; |
| 5093 | void *cur_data = argptr; |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5094 | const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) }; |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5095 | int nl_size = 12; /* can't use thunk_size due to alignment */ |
| 5096 | |
| 5097 | while (1) { |
| 5098 | uint32_t next = nl->next; |
| 5099 | if (next) { |
| 5100 | nl->next = nl_size + (strlen(nl->name) + 1); |
| 5101 | } |
| 5102 | if (remaining_data < nl->next) { |
| 5103 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 5104 | break; |
| 5105 | } |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5106 | thunk_convert(cur_data, nl, dm_arg_type, THUNK_TARGET); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5107 | strcpy(cur_data + nl_size, nl->name); |
| 5108 | cur_data += nl->next; |
| 5109 | remaining_data -= nl->next; |
| 5110 | if (!next) { |
| 5111 | break; |
| 5112 | } |
| 5113 | nl = (void*)nl + next; |
| 5114 | } |
| 5115 | break; |
| 5116 | } |
| 5117 | case DM_DEV_WAIT: |
| 5118 | case DM_TABLE_STATUS: |
| 5119 | { |
| 5120 | struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start; |
| 5121 | void *cur_data = argptr; |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5122 | const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; |
| 5123 | int spec_size = thunk_type_size(dm_arg_type, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5124 | int i; |
| 5125 | |
| 5126 | for (i = 0; i < host_dm->target_count; i++) { |
| 5127 | uint32_t next = spec->next; |
| 5128 | int slen = strlen((char*)&spec[1]) + 1; |
| 5129 | spec->next = (cur_data - argptr) + spec_size + slen; |
| 5130 | if (guest_data_size < spec->next) { |
| 5131 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 5132 | break; |
| 5133 | } |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5134 | thunk_convert(cur_data, spec, dm_arg_type, THUNK_TARGET); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5135 | strcpy(cur_data + spec_size, (char*)&spec[1]); |
| 5136 | cur_data = argptr + spec->next; |
| 5137 | spec = (void*)host_dm + host_dm->data_start + next; |
| 5138 | } |
| 5139 | break; |
| 5140 | } |
| 5141 | case DM_TABLE_DEPS: |
| 5142 | { |
| 5143 | void *hdata = (void*)host_dm + host_dm->data_start; |
| 5144 | int count = *(uint32_t*)hdata; |
| 5145 | uint64_t *hdev = hdata + 8; |
| 5146 | uint64_t *gdev = argptr + 8; |
| 5147 | int i; |
| 5148 | |
| 5149 | *(uint32_t*)argptr = tswap32(count); |
| 5150 | for (i = 0; i < count; i++) { |
| 5151 | *gdev = tswap64(*hdev); |
| 5152 | gdev++; |
| 5153 | hdev++; |
| 5154 | } |
| 5155 | break; |
| 5156 | } |
| 5157 | case DM_LIST_VERSIONS: |
| 5158 | { |
| 5159 | struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start; |
| 5160 | uint32_t remaining_data = guest_data_size; |
| 5161 | void *cur_data = argptr; |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5162 | const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) }; |
| 5163 | int vers_size = thunk_type_size(dm_arg_type, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5164 | |
| 5165 | while (1) { |
| 5166 | uint32_t next = vers->next; |
| 5167 | if (next) { |
| 5168 | vers->next = vers_size + (strlen(vers->name) + 1); |
| 5169 | } |
| 5170 | if (remaining_data < vers->next) { |
| 5171 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 5172 | break; |
| 5173 | } |
Laurent Vivier | eb2929c | 2023-09-25 17:10:27 +0200 | [diff] [blame] | 5174 | thunk_convert(cur_data, vers, dm_arg_type, THUNK_TARGET); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5175 | strcpy(cur_data + vers_size, vers->name); |
| 5176 | cur_data += vers->next; |
| 5177 | remaining_data -= vers->next; |
| 5178 | if (!next) { |
| 5179 | break; |
| 5180 | } |
| 5181 | vers = (void*)vers + next; |
| 5182 | } |
| 5183 | break; |
| 5184 | } |
| 5185 | default: |
Chen Gang S | dec0473 | 2015-01-25 08:00:42 +0800 | [diff] [blame] | 5186 | unlock_user(argptr, guest_data, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5187 | ret = -TARGET_EINVAL; |
| 5188 | goto out; |
| 5189 | } |
| 5190 | unlock_user(argptr, guest_data, guest_data_size); |
| 5191 | |
| 5192 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 5193 | if (!argptr) { |
| 5194 | ret = -TARGET_EFAULT; |
| 5195 | goto out; |
| 5196 | } |
| 5197 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 5198 | unlock_user(argptr, arg, target_size); |
| 5199 | } |
| 5200 | out: |
Stefan Weil | ad11ad7 | 2012-09-04 22:14:19 +0200 | [diff] [blame] | 5201 | g_free(big_buf); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5202 | return ret; |
| 5203 | } |
| 5204 | |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 5205 | static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 5206 | int cmd, abi_long arg) |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 5207 | { |
| 5208 | void *argptr; |
| 5209 | int target_size; |
| 5210 | const argtype *arg_type = ie->arg_type; |
| 5211 | const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) }; |
| 5212 | abi_long ret; |
| 5213 | |
| 5214 | struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp; |
| 5215 | struct blkpg_partition host_part; |
| 5216 | |
| 5217 | /* Read and convert blkpg */ |
| 5218 | arg_type++; |
| 5219 | target_size = thunk_type_size(arg_type, 0); |
| 5220 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5221 | if (!argptr) { |
| 5222 | ret = -TARGET_EFAULT; |
| 5223 | goto out; |
| 5224 | } |
| 5225 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 5226 | unlock_user(argptr, arg, 0); |
| 5227 | |
| 5228 | switch (host_blkpg->op) { |
| 5229 | case BLKPG_ADD_PARTITION: |
| 5230 | case BLKPG_DEL_PARTITION: |
| 5231 | /* payload is struct blkpg_partition */ |
| 5232 | break; |
| 5233 | default: |
| 5234 | /* Unknown opcode */ |
| 5235 | ret = -TARGET_EINVAL; |
| 5236 | goto out; |
| 5237 | } |
| 5238 | |
| 5239 | /* Read and convert blkpg->data */ |
| 5240 | arg = (abi_long)(uintptr_t)host_blkpg->data; |
| 5241 | target_size = thunk_type_size(part_arg_type, 0); |
| 5242 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5243 | if (!argptr) { |
| 5244 | ret = -TARGET_EFAULT; |
| 5245 | goto out; |
| 5246 | } |
| 5247 | thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST); |
| 5248 | unlock_user(argptr, arg, 0); |
| 5249 | |
| 5250 | /* Swizzle the data pointer to our local copy and call! */ |
| 5251 | host_blkpg->data = &host_part; |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5252 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg)); |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 5253 | |
| 5254 | out: |
| 5255 | return ret; |
| 5256 | } |
| 5257 | |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5258 | static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 5259 | int fd, int cmd, abi_long arg) |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5260 | { |
| 5261 | const argtype *arg_type = ie->arg_type; |
| 5262 | const StructEntry *se; |
| 5263 | const argtype *field_types; |
| 5264 | const int *dst_offsets, *src_offsets; |
| 5265 | int target_size; |
| 5266 | void *argptr; |
Marc-André Lureau | b78c522 | 2019-03-05 16:15:00 +0100 | [diff] [blame] | 5267 | abi_ulong *target_rt_dev_ptr = NULL; |
| 5268 | unsigned long *host_rt_dev_ptr = NULL; |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5269 | abi_long ret; |
| 5270 | int i; |
| 5271 | |
| 5272 | assert(ie->access == IOC_W); |
| 5273 | assert(*arg_type == TYPE_PTR); |
| 5274 | arg_type++; |
| 5275 | assert(*arg_type == TYPE_STRUCT); |
| 5276 | target_size = thunk_type_size(arg_type, 0); |
| 5277 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5278 | if (!argptr) { |
| 5279 | return -TARGET_EFAULT; |
| 5280 | } |
| 5281 | arg_type++; |
| 5282 | assert(*arg_type == (int)STRUCT_rtentry); |
| 5283 | se = struct_entries + *arg_type++; |
| 5284 | assert(se->convert[0] == NULL); |
| 5285 | /* convert struct here to be able to catch rt_dev string */ |
| 5286 | field_types = se->field_types; |
| 5287 | dst_offsets = se->field_offsets[THUNK_HOST]; |
| 5288 | src_offsets = se->field_offsets[THUNK_TARGET]; |
| 5289 | for (i = 0; i < se->nb_fields; i++) { |
| 5290 | if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) { |
| 5291 | assert(*field_types == TYPE_PTRVOID); |
Markus Armbruster | 3d55833 | 2022-11-23 14:38:11 +0100 | [diff] [blame] | 5292 | target_rt_dev_ptr = argptr + src_offsets[i]; |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5293 | host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]); |
| 5294 | if (*target_rt_dev_ptr != 0) { |
| 5295 | *host_rt_dev_ptr = (unsigned long)lock_user_string( |
| 5296 | tswapal(*target_rt_dev_ptr)); |
| 5297 | if (!*host_rt_dev_ptr) { |
| 5298 | unlock_user(argptr, arg, 0); |
| 5299 | return -TARGET_EFAULT; |
| 5300 | } |
| 5301 | } else { |
| 5302 | *host_rt_dev_ptr = 0; |
| 5303 | } |
| 5304 | field_types++; |
| 5305 | continue; |
| 5306 | } |
| 5307 | field_types = thunk_convert(buf_temp + dst_offsets[i], |
| 5308 | argptr + src_offsets[i], |
| 5309 | field_types, THUNK_HOST); |
| 5310 | } |
| 5311 | unlock_user(argptr, arg, 0); |
| 5312 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5313 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
Marc-André Lureau | b78c522 | 2019-03-05 16:15:00 +0100 | [diff] [blame] | 5314 | |
| 5315 | assert(host_rt_dev_ptr != NULL); |
| 5316 | assert(target_rt_dev_ptr != NULL); |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5317 | if (*host_rt_dev_ptr != 0) { |
| 5318 | unlock_user((void *)*host_rt_dev_ptr, |
| 5319 | *target_rt_dev_ptr, 0); |
| 5320 | } |
| 5321 | return ret; |
| 5322 | } |
| 5323 | |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 5324 | static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 5325 | int fd, int cmd, abi_long arg) |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 5326 | { |
| 5327 | int sig = target_to_host_signal(arg); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5328 | return get_errno(safe_ioctl(fd, ie->host_cmd, sig)); |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 5329 | } |
| 5330 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 5331 | static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5332 | int fd, int cmd, abi_long arg) |
| 5333 | { |
| 5334 | struct timeval tv; |
| 5335 | abi_long ret; |
| 5336 | |
| 5337 | ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv)); |
| 5338 | if (is_error(ret)) { |
| 5339 | return ret; |
| 5340 | } |
| 5341 | |
| 5342 | if (cmd == (int)TARGET_SIOCGSTAMP_OLD) { |
| 5343 | if (copy_to_user_timeval(arg, &tv)) { |
| 5344 | return -TARGET_EFAULT; |
| 5345 | } |
| 5346 | } else { |
| 5347 | if (copy_to_user_timeval64(arg, &tv)) { |
| 5348 | return -TARGET_EFAULT; |
| 5349 | } |
| 5350 | } |
| 5351 | |
| 5352 | return ret; |
| 5353 | } |
| 5354 | |
| 5355 | static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5356 | int fd, int cmd, abi_long arg) |
| 5357 | { |
| 5358 | struct timespec ts; |
| 5359 | abi_long ret; |
| 5360 | |
| 5361 | ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts)); |
| 5362 | if (is_error(ret)) { |
| 5363 | return ret; |
| 5364 | } |
| 5365 | |
| 5366 | if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) { |
| 5367 | if (host_to_target_timespec(arg, &ts)) { |
| 5368 | return -TARGET_EFAULT; |
| 5369 | } |
| 5370 | } else{ |
| 5371 | if (host_to_target_timespec64(arg, &ts)) { |
| 5372 | return -TARGET_EFAULT; |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | return ret; |
| 5377 | } |
| 5378 | |
Andreas Schwab | 2b74f62 | 2018-01-29 11:47:06 +0100 | [diff] [blame] | 5379 | #ifdef TIOCGPTPEER |
| 5380 | static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5381 | int fd, int cmd, abi_long arg) |
| 5382 | { |
| 5383 | int flags = target_to_host_bitmask(arg, fcntl_flags_tbl); |
| 5384 | return get_errno(safe_ioctl(fd, ie->host_cmd, flags)); |
| 5385 | } |
| 5386 | #endif |
| 5387 | |
Chen Gang | e865b97 | 2020-06-05 09:32:21 +0800 | [diff] [blame] | 5388 | #ifdef HAVE_DRM_H |
| 5389 | |
| 5390 | static void unlock_drm_version(struct drm_version *host_ver, |
| 5391 | struct target_drm_version *target_ver, |
| 5392 | bool copy) |
| 5393 | { |
| 5394 | unlock_user(host_ver->name, target_ver->name, |
| 5395 | copy ? host_ver->name_len : 0); |
| 5396 | unlock_user(host_ver->date, target_ver->date, |
| 5397 | copy ? host_ver->date_len : 0); |
| 5398 | unlock_user(host_ver->desc, target_ver->desc, |
| 5399 | copy ? host_ver->desc_len : 0); |
| 5400 | } |
| 5401 | |
| 5402 | static inline abi_long target_to_host_drmversion(struct drm_version *host_ver, |
| 5403 | struct target_drm_version *target_ver) |
| 5404 | { |
| 5405 | memset(host_ver, 0, sizeof(*host_ver)); |
| 5406 | |
| 5407 | __get_user(host_ver->name_len, &target_ver->name_len); |
| 5408 | if (host_ver->name_len) { |
| 5409 | host_ver->name = lock_user(VERIFY_WRITE, target_ver->name, |
| 5410 | target_ver->name_len, 0); |
| 5411 | if (!host_ver->name) { |
| 5412 | return -EFAULT; |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | __get_user(host_ver->date_len, &target_ver->date_len); |
| 5417 | if (host_ver->date_len) { |
| 5418 | host_ver->date = lock_user(VERIFY_WRITE, target_ver->date, |
| 5419 | target_ver->date_len, 0); |
| 5420 | if (!host_ver->date) { |
| 5421 | goto err; |
| 5422 | } |
| 5423 | } |
| 5424 | |
| 5425 | __get_user(host_ver->desc_len, &target_ver->desc_len); |
| 5426 | if (host_ver->desc_len) { |
| 5427 | host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc, |
| 5428 | target_ver->desc_len, 0); |
| 5429 | if (!host_ver->desc) { |
| 5430 | goto err; |
| 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | return 0; |
| 5435 | err: |
| 5436 | unlock_drm_version(host_ver, target_ver, false); |
| 5437 | return -EFAULT; |
| 5438 | } |
| 5439 | |
| 5440 | static inline void host_to_target_drmversion( |
| 5441 | struct target_drm_version *target_ver, |
| 5442 | struct drm_version *host_ver) |
| 5443 | { |
| 5444 | __put_user(host_ver->version_major, &target_ver->version_major); |
| 5445 | __put_user(host_ver->version_minor, &target_ver->version_minor); |
| 5446 | __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel); |
| 5447 | __put_user(host_ver->name_len, &target_ver->name_len); |
| 5448 | __put_user(host_ver->date_len, &target_ver->date_len); |
| 5449 | __put_user(host_ver->desc_len, &target_ver->desc_len); |
| 5450 | unlock_drm_version(host_ver, target_ver, true); |
| 5451 | } |
| 5452 | |
| 5453 | static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5454 | int fd, int cmd, abi_long arg) |
| 5455 | { |
| 5456 | struct drm_version *ver; |
| 5457 | struct target_drm_version *target_ver; |
| 5458 | abi_long ret; |
| 5459 | |
| 5460 | switch (ie->host_cmd) { |
| 5461 | case DRM_IOCTL_VERSION: |
| 5462 | if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) { |
| 5463 | return -TARGET_EFAULT; |
| 5464 | } |
| 5465 | ver = (struct drm_version *)buf_temp; |
| 5466 | ret = target_to_host_drmversion(ver, target_ver); |
| 5467 | if (!is_error(ret)) { |
| 5468 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver)); |
| 5469 | if (is_error(ret)) { |
| 5470 | unlock_drm_version(ver, target_ver, false); |
| 5471 | } else { |
| 5472 | host_to_target_drmversion(target_ver, ver); |
| 5473 | } |
| 5474 | } |
| 5475 | unlock_user_struct(target_ver, arg, 0); |
| 5476 | return ret; |
| 5477 | } |
| 5478 | return -TARGET_ENOSYS; |
| 5479 | } |
| 5480 | |
Chen Gang | 913b03c | 2020-08-02 21:39:38 +0800 | [diff] [blame] | 5481 | static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie, |
| 5482 | struct drm_i915_getparam *gparam, |
| 5483 | int fd, abi_long arg) |
| 5484 | { |
| 5485 | abi_long ret; |
| 5486 | int value; |
| 5487 | struct target_drm_i915_getparam *target_gparam; |
| 5488 | |
| 5489 | if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) { |
| 5490 | return -TARGET_EFAULT; |
| 5491 | } |
| 5492 | |
| 5493 | __get_user(gparam->param, &target_gparam->param); |
| 5494 | gparam->value = &value; |
| 5495 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam)); |
| 5496 | put_user_s32(value, target_gparam->value); |
| 5497 | |
| 5498 | unlock_user_struct(target_gparam, arg, 0); |
| 5499 | return ret; |
| 5500 | } |
| 5501 | |
| 5502 | static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5503 | int fd, int cmd, abi_long arg) |
| 5504 | { |
| 5505 | switch (ie->host_cmd) { |
| 5506 | case DRM_IOCTL_I915_GETPARAM: |
| 5507 | return do_ioctl_drm_i915_getparam(ie, |
| 5508 | (struct drm_i915_getparam *)buf_temp, |
| 5509 | fd, arg); |
| 5510 | default: |
| 5511 | return -TARGET_ENOSYS; |
| 5512 | } |
| 5513 | } |
| 5514 | |
Chen Gang | e865b97 | 2020-06-05 09:32:21 +0800 | [diff] [blame] | 5515 | #endif |
| 5516 | |
Shu-Chun Weng | 6addf06 | 2020-09-28 18:48:01 -0700 | [diff] [blame] | 5517 | static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5518 | int fd, int cmd, abi_long arg) |
| 5519 | { |
| 5520 | struct tun_filter *filter = (struct tun_filter *)buf_temp; |
| 5521 | struct tun_filter *target_filter; |
| 5522 | char *target_addr; |
| 5523 | |
| 5524 | assert(ie->access == IOC_W); |
| 5525 | |
| 5526 | target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1); |
| 5527 | if (!target_filter) { |
| 5528 | return -TARGET_EFAULT; |
| 5529 | } |
| 5530 | filter->flags = tswap16(target_filter->flags); |
| 5531 | filter->count = tswap16(target_filter->count); |
| 5532 | unlock_user(target_filter, arg, 0); |
| 5533 | |
| 5534 | if (filter->count) { |
| 5535 | if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN > |
| 5536 | MAX_STRUCT_SIZE) { |
| 5537 | return -TARGET_EFAULT; |
| 5538 | } |
| 5539 | |
| 5540 | target_addr = lock_user(VERIFY_READ, |
| 5541 | arg + offsetof(struct tun_filter, addr), |
| 5542 | filter->count * ETH_ALEN, 1); |
| 5543 | if (!target_addr) { |
| 5544 | return -TARGET_EFAULT; |
| 5545 | } |
| 5546 | memcpy(filter->addr, target_addr, filter->count * ETH_ALEN); |
| 5547 | unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0); |
| 5548 | } |
| 5549 | |
| 5550 | return get_errno(safe_ioctl(fd, ie->host_cmd, filter)); |
| 5551 | } |
| 5552 | |
Filip Bozuta | 79482e59 | 2020-06-19 14:47:27 +0200 | [diff] [blame] | 5553 | IOCTLEntry ioctl_entries[] = { |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 5554 | #define IOCTL(cmd, access, ...) \ |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 5555 | { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } }, |
| 5556 | #define IOCTL_SPECIAL(cmd, access, dofn, ...) \ |
| 5557 | { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } }, |
Helge Deller | 8cb762f | 2016-12-06 16:24:03 +0100 | [diff] [blame] | 5558 | #define IOCTL_IGNORE(cmd) \ |
| 5559 | { TARGET_ ## cmd, 0, #cmd }, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5560 | #include "ioctls.h" |
| 5561 | { 0, 0, }, |
| 5562 | }; |
| 5563 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5564 | /* ??? Implement proper locking for ioctls. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 5565 | /* do_ioctl() Must return target values and target errnos. */ |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 5566 | static abi_long do_ioctl(int fd, int cmd, abi_long arg) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5567 | { |
| 5568 | const IOCTLEntry *ie; |
| 5569 | const argtype *arg_type; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5570 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5571 | uint8_t buf_temp[MAX_STRUCT_SIZE]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5572 | int target_size; |
| 5573 | void *argptr; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5574 | |
| 5575 | ie = ioctl_entries; |
| 5576 | for(;;) { |
| 5577 | if (ie->target_cmd == 0) { |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 5578 | qemu_log_mask( |
| 5579 | LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd); |
Thomas Weißschuh | 59d1172 | 2023-04-26 09:06:59 +0200 | [diff] [blame] | 5580 | return -TARGET_ENOTTY; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5581 | } |
| 5582 | if (ie->target_cmd == cmd) |
| 5583 | break; |
| 5584 | ie++; |
| 5585 | } |
| 5586 | arg_type = ie->arg_type; |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 5587 | if (ie->do_ioctl) { |
| 5588 | return ie->do_ioctl(ie, buf_temp, fd, cmd, arg); |
Helge Deller | 8cb762f | 2016-12-06 16:24:03 +0100 | [diff] [blame] | 5589 | } else if (!ie->host_cmd) { |
| 5590 | /* Some architectures define BSD ioctls in their headers |
| 5591 | that are not implemented in Linux. */ |
Thomas Weißschuh | 59d1172 | 2023-04-26 09:06:59 +0200 | [diff] [blame] | 5592 | return -TARGET_ENOTTY; |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5595 | switch(arg_type[0]) { |
| 5596 | case TYPE_NULL: |
| 5597 | /* no argument */ |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5598 | ret = get_errno(safe_ioctl(fd, ie->host_cmd)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5599 | break; |
| 5600 | case TYPE_PTRVOID: |
| 5601 | case TYPE_INT: |
Filip Bozuta | c858e53 | 2020-01-15 20:36:47 +0100 | [diff] [blame] | 5602 | case TYPE_LONG: |
| 5603 | case TYPE_ULONG: |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5604 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5605 | break; |
| 5606 | case TYPE_PTR: |
| 5607 | arg_type++; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5608 | target_size = thunk_type_size(arg_type, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5609 | switch(ie->access) { |
| 5610 | case IOC_R: |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5611 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5612 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5613 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 5614 | if (!argptr) |
| 5615 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5616 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 5617 | unlock_user(argptr, arg, target_size); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5618 | } |
| 5619 | break; |
| 5620 | case IOC_W: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5621 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5622 | if (!argptr) |
| 5623 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5624 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 5625 | unlock_user(argptr, arg, 0); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5626 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5627 | break; |
| 5628 | default: |
| 5629 | case IOC_RW: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5630 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5631 | if (!argptr) |
| 5632 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5633 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 5634 | unlock_user(argptr, arg, 0); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5635 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5636 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5637 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 5638 | if (!argptr) |
| 5639 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5640 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 5641 | unlock_user(argptr, arg, target_size); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5642 | } |
| 5643 | break; |
| 5644 | } |
| 5645 | break; |
| 5646 | default: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 5647 | qemu_log_mask(LOG_UNIMP, |
| 5648 | "Unsupported ioctl type: cmd=0x%04lx type=%d\n", |
| 5649 | (long)cmd, arg_type[0]); |
Thomas Weißschuh | 59d1172 | 2023-04-26 09:06:59 +0200 | [diff] [blame] | 5650 | ret = -TARGET_ENOTTY; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5651 | break; |
| 5652 | } |
| 5653 | return ret; |
| 5654 | } |
| 5655 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5656 | static const bitmask_transtbl iflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5657 | { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK }, |
| 5658 | { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT }, |
| 5659 | { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR }, |
| 5660 | { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK }, |
| 5661 | { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK }, |
| 5662 | { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP }, |
| 5663 | { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR }, |
| 5664 | { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR }, |
| 5665 | { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL }, |
| 5666 | { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC }, |
| 5667 | { TARGET_IXON, TARGET_IXON, IXON, IXON }, |
| 5668 | { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY }, |
| 5669 | { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF }, |
| 5670 | { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL }, |
Filip Bozuta | c218b4e | 2020-07-23 23:02:32 +0200 | [diff] [blame] | 5671 | { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8}, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5672 | }; |
| 5673 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5674 | static const bitmask_transtbl oflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5675 | { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST }, |
| 5676 | { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC }, |
| 5677 | { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR }, |
| 5678 | { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL }, |
| 5679 | { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR }, |
| 5680 | { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET }, |
| 5681 | { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL }, |
| 5682 | { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL }, |
| 5683 | { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 }, |
| 5684 | { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 }, |
| 5685 | { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 }, |
| 5686 | { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 }, |
| 5687 | { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 }, |
| 5688 | { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 }, |
| 5689 | { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 }, |
| 5690 | { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 }, |
| 5691 | { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 }, |
| 5692 | { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 }, |
| 5693 | { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 }, |
| 5694 | { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 }, |
| 5695 | { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 }, |
| 5696 | { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 }, |
| 5697 | { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 }, |
| 5698 | { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 }, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5699 | }; |
| 5700 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5701 | static const bitmask_transtbl cflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5702 | { TARGET_CBAUD, TARGET_B0, CBAUD, B0 }, |
| 5703 | { TARGET_CBAUD, TARGET_B50, CBAUD, B50 }, |
| 5704 | { TARGET_CBAUD, TARGET_B75, CBAUD, B75 }, |
| 5705 | { TARGET_CBAUD, TARGET_B110, CBAUD, B110 }, |
| 5706 | { TARGET_CBAUD, TARGET_B134, CBAUD, B134 }, |
| 5707 | { TARGET_CBAUD, TARGET_B150, CBAUD, B150 }, |
| 5708 | { TARGET_CBAUD, TARGET_B200, CBAUD, B200 }, |
| 5709 | { TARGET_CBAUD, TARGET_B300, CBAUD, B300 }, |
| 5710 | { TARGET_CBAUD, TARGET_B600, CBAUD, B600 }, |
| 5711 | { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 }, |
| 5712 | { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 }, |
| 5713 | { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 }, |
| 5714 | { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 }, |
| 5715 | { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 }, |
| 5716 | { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 }, |
| 5717 | { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 }, |
| 5718 | { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 }, |
| 5719 | { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 }, |
| 5720 | { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 }, |
| 5721 | { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 }, |
| 5722 | { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 }, |
| 5723 | { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 }, |
| 5724 | { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 }, |
| 5725 | { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 }, |
| 5726 | { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB }, |
| 5727 | { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD }, |
| 5728 | { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB }, |
| 5729 | { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD }, |
| 5730 | { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL }, |
| 5731 | { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL }, |
| 5732 | { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS }, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5733 | }; |
| 5734 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5735 | static const bitmask_transtbl lflag_tbl[] = { |
Filip Bozuta | c218b4e | 2020-07-23 23:02:32 +0200 | [diff] [blame] | 5736 | { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG }, |
| 5737 | { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON }, |
| 5738 | { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE }, |
| 5739 | { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO }, |
| 5740 | { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE }, |
| 5741 | { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK }, |
| 5742 | { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL }, |
| 5743 | { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH }, |
| 5744 | { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP }, |
| 5745 | { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL }, |
| 5746 | { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT }, |
| 5747 | { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE }, |
| 5748 | { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO }, |
| 5749 | { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN }, |
| 5750 | { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN }, |
| 5751 | { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC}, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5752 | }; |
| 5753 | |
| 5754 | static void target_to_host_termios (void *dst, const void *src) |
| 5755 | { |
| 5756 | struct host_termios *host = dst; |
| 5757 | const struct target_termios *target = src; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5758 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5759 | host->c_iflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5760 | target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5761 | host->c_oflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5762 | target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5763 | host->c_cflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5764 | target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5765 | host->c_lflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5766 | target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl); |
| 5767 | host->c_line = target->c_line; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5768 | |
Arnaud Patard | 4460712 | 2009-04-21 17:39:08 +0300 | [diff] [blame] | 5769 | memset(host->c_cc, 0, sizeof(host->c_cc)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5770 | host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; |
| 5771 | host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5772 | host->c_cc[VERASE] = target->c_cc[TARGET_VERASE]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5773 | host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5774 | host->c_cc[VEOF] = target->c_cc[TARGET_VEOF]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5775 | host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5776 | host->c_cc[VMIN] = target->c_cc[TARGET_VMIN]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5777 | host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5778 | host->c_cc[VSTART] = target->c_cc[TARGET_VSTART]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5779 | host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; |
| 5780 | host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5781 | host->c_cc[VEOL] = target->c_cc[TARGET_VEOL]; |
| 5782 | host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT]; |
| 5783 | host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD]; |
| 5784 | host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE]; |
| 5785 | host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5786 | host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5787 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5788 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5789 | static void host_to_target_termios (void *dst, const void *src) |
| 5790 | { |
| 5791 | struct target_termios *target = dst; |
| 5792 | const struct host_termios *host = src; |
| 5793 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5794 | target->c_iflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5795 | tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5796 | target->c_oflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5797 | tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5798 | target->c_cflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5799 | tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5800 | target->c_lflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5801 | tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl)); |
| 5802 | target->c_line = host->c_line; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5803 | |
Arnaud Patard | 4460712 | 2009-04-21 17:39:08 +0300 | [diff] [blame] | 5804 | memset(target->c_cc, 0, sizeof(target->c_cc)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5805 | target->c_cc[TARGET_VINTR] = host->c_cc[VINTR]; |
| 5806 | target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT]; |
| 5807 | target->c_cc[TARGET_VERASE] = host->c_cc[VERASE]; |
| 5808 | target->c_cc[TARGET_VKILL] = host->c_cc[VKILL]; |
| 5809 | target->c_cc[TARGET_VEOF] = host->c_cc[VEOF]; |
| 5810 | target->c_cc[TARGET_VTIME] = host->c_cc[VTIME]; |
| 5811 | target->c_cc[TARGET_VMIN] = host->c_cc[VMIN]; |
| 5812 | target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC]; |
| 5813 | target->c_cc[TARGET_VSTART] = host->c_cc[VSTART]; |
| 5814 | target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP]; |
| 5815 | target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP]; |
| 5816 | target->c_cc[TARGET_VEOL] = host->c_cc[VEOL]; |
| 5817 | target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT]; |
| 5818 | target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD]; |
| 5819 | target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE]; |
| 5820 | target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT]; |
| 5821 | target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2]; |
| 5822 | } |
| 5823 | |
blueswir1 | 8e853dc | 2008-10-05 10:49:32 +0000 | [diff] [blame] | 5824 | static const StructEntry struct_termios_def = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5825 | .convert = { host_to_target_termios, target_to_host_termios }, |
| 5826 | .size = { sizeof(struct target_termios), sizeof(struct host_termios) }, |
| 5827 | .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) }, |
Filip Bozuta | 888468d | 2020-07-23 23:02:33 +0200 | [diff] [blame] | 5828 | .print = print_termios, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5829 | }; |
| 5830 | |
Richard Henderson | 4b840f9 | 2023-07-07 21:40:36 +0100 | [diff] [blame] | 5831 | /* If the host does not provide these bits, they may be safely discarded. */ |
| 5832 | #ifndef MAP_SYNC |
| 5833 | #define MAP_SYNC 0 |
| 5834 | #endif |
| 5835 | #ifndef MAP_UNINITIALIZED |
| 5836 | #define MAP_UNINITIALIZED 0 |
| 5837 | #endif |
| 5838 | |
Philippe Mathieu-Daudé | 180d4ef | 2021-05-17 07:52:43 +0200 | [diff] [blame] | 5839 | static const bitmask_transtbl mmap_flags_tbl[] = { |
Helge Deller | 541e169 | 2017-10-31 13:53:58 +0100 | [diff] [blame] | 5840 | { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED }, |
| 5841 | { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, |
| 5842 | MAP_ANONYMOUS, MAP_ANONYMOUS }, |
| 5843 | { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, |
| 5844 | MAP_GROWSDOWN, MAP_GROWSDOWN }, |
| 5845 | { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, |
| 5846 | MAP_DENYWRITE, MAP_DENYWRITE }, |
| 5847 | { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, |
| 5848 | MAP_EXECUTABLE, MAP_EXECUTABLE }, |
| 5849 | { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED }, |
| 5850 | { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, |
| 5851 | MAP_NORESERVE, MAP_NORESERVE }, |
| 5852 | { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB }, |
| 5853 | /* MAP_STACK had been ignored by the kernel for quite some time. |
| 5854 | Recognize it for the target insofar as we do not want to pass |
| 5855 | it through to the host. */ |
| 5856 | { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 }, |
Richard Henderson | 4b840f9 | 2023-07-07 21:40:36 +0100 | [diff] [blame] | 5857 | { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK }, |
| 5858 | { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE }, |
| 5859 | { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE, |
| 5860 | MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE }, |
| 5861 | { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED, |
| 5862 | MAP_UNINITIALIZED, MAP_UNINITIALIZED }, |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 5863 | }; |
| 5864 | |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 5865 | /* |
Richard Henderson | 9ab8d071 | 2023-08-07 18:22:35 -0700 | [diff] [blame] | 5866 | * Arrange for legacy / undefined architecture specific flags to be |
| 5867 | * ignored by mmap handling code. |
| 5868 | */ |
| 5869 | #ifndef TARGET_MAP_32BIT |
| 5870 | #define TARGET_MAP_32BIT 0 |
| 5871 | #endif |
| 5872 | #ifndef TARGET_MAP_HUGE_2MB |
| 5873 | #define TARGET_MAP_HUGE_2MB 0 |
| 5874 | #endif |
| 5875 | #ifndef TARGET_MAP_HUGE_1GB |
| 5876 | #define TARGET_MAP_HUGE_1GB 0 |
| 5877 | #endif |
| 5878 | |
| 5879 | static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot, |
| 5880 | int target_flags, int fd, off_t offset) |
| 5881 | { |
| 5882 | /* |
| 5883 | * The historical set of flags that all mmap types implicitly support. |
| 5884 | */ |
| 5885 | enum { |
| 5886 | TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED |
| 5887 | | TARGET_MAP_PRIVATE |
| 5888 | | TARGET_MAP_FIXED |
| 5889 | | TARGET_MAP_ANONYMOUS |
| 5890 | | TARGET_MAP_DENYWRITE |
| 5891 | | TARGET_MAP_EXECUTABLE |
| 5892 | | TARGET_MAP_UNINITIALIZED |
| 5893 | | TARGET_MAP_GROWSDOWN |
| 5894 | | TARGET_MAP_LOCKED |
| 5895 | | TARGET_MAP_NORESERVE |
| 5896 | | TARGET_MAP_POPULATE |
| 5897 | | TARGET_MAP_NONBLOCK |
| 5898 | | TARGET_MAP_STACK |
| 5899 | | TARGET_MAP_HUGETLB |
| 5900 | | TARGET_MAP_32BIT |
| 5901 | | TARGET_MAP_HUGE_2MB |
| 5902 | | TARGET_MAP_HUGE_1GB |
| 5903 | }; |
| 5904 | int host_flags; |
| 5905 | |
| 5906 | switch (target_flags & TARGET_MAP_TYPE) { |
| 5907 | case TARGET_MAP_PRIVATE: |
| 5908 | host_flags = MAP_PRIVATE; |
| 5909 | break; |
| 5910 | case TARGET_MAP_SHARED: |
| 5911 | host_flags = MAP_SHARED; |
| 5912 | break; |
| 5913 | case TARGET_MAP_SHARED_VALIDATE: |
| 5914 | /* |
| 5915 | * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is |
| 5916 | * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK. |
| 5917 | */ |
| 5918 | if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) { |
| 5919 | return -TARGET_EOPNOTSUPP; |
| 5920 | } |
| 5921 | host_flags = MAP_SHARED_VALIDATE; |
| 5922 | if (target_flags & TARGET_MAP_SYNC) { |
| 5923 | host_flags |= MAP_SYNC; |
| 5924 | } |
| 5925 | break; |
| 5926 | default: |
| 5927 | return -TARGET_EINVAL; |
| 5928 | } |
| 5929 | host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl); |
| 5930 | |
| 5931 | return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset)); |
| 5932 | } |
| 5933 | |
| 5934 | /* |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 5935 | * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64) |
| 5936 | * TARGET_I386 is defined if TARGET_X86_64 is defined |
| 5937 | */ |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5938 | #if defined(TARGET_I386) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5939 | |
| 5940 | /* NOTE: there is really one LDT for all the threads */ |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 5941 | static uint8_t *ldt_table; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5942 | |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5943 | static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5944 | { |
| 5945 | int size; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5946 | void *p; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5947 | |
| 5948 | if (!ldt_table) |
| 5949 | return 0; |
| 5950 | size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE; |
| 5951 | if (size > bytecount) |
| 5952 | size = bytecount; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5953 | p = lock_user(VERIFY_WRITE, ptr, size, 0); |
| 5954 | if (!p) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5955 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5956 | /* ??? Should this by byteswapped? */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5957 | memcpy(p, ldt_table, size); |
| 5958 | unlock_user(p, ptr, size); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5959 | return size; |
| 5960 | } |
| 5961 | |
| 5962 | /* XXX: add locking support */ |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5963 | static abi_long write_ldt(CPUX86State *env, |
| 5964 | abi_ulong ptr, unsigned long bytecount, int oldmode) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5965 | { |
| 5966 | struct target_modify_ldt_ldt_s ldt_info; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5967 | struct target_modify_ldt_ldt_s *target_ldt_info; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5968 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5969 | int seg_not_present, useable, lm; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5970 | uint32_t *lp, entry_1, entry_2; |
| 5971 | |
| 5972 | if (bytecount != sizeof(ldt_info)) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5973 | return -TARGET_EINVAL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5974 | if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1)) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5975 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5976 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5977 | ldt_info.base_addr = tswapal(target_ldt_info->base_addr); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5978 | ldt_info.limit = tswap32(target_ldt_info->limit); |
| 5979 | ldt_info.flags = tswap32(target_ldt_info->flags); |
| 5980 | unlock_user_struct(target_ldt_info, ptr, 0); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5981 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5982 | if (ldt_info.entry_number >= TARGET_LDT_ENTRIES) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5983 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5984 | seg_32bit = ldt_info.flags & 1; |
| 5985 | contents = (ldt_info.flags >> 1) & 3; |
| 5986 | read_exec_only = (ldt_info.flags >> 3) & 1; |
| 5987 | limit_in_pages = (ldt_info.flags >> 4) & 1; |
| 5988 | seg_not_present = (ldt_info.flags >> 5) & 1; |
| 5989 | useable = (ldt_info.flags >> 6) & 1; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5990 | #ifdef TARGET_ABI32 |
| 5991 | lm = 0; |
| 5992 | #else |
| 5993 | lm = (ldt_info.flags >> 7) & 1; |
| 5994 | #endif |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5995 | if (contents == 3) { |
| 5996 | if (oldmode) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5997 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5998 | if (seg_not_present == 0) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5999 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6000 | } |
| 6001 | /* allocate the LDT */ |
| 6002 | if (!ldt_table) { |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 6003 | env->ldt.base = target_mmap(0, |
| 6004 | TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE, |
| 6005 | PROT_READ|PROT_WRITE, |
| 6006 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 6007 | if (env->ldt.base == -1) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 6008 | return -TARGET_ENOMEM; |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 6009 | memset(g2h_untagged(env->ldt.base), 0, |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 6010 | TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6011 | env->ldt.limit = 0xffff; |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 6012 | ldt_table = g2h_untagged(env->ldt.base); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6013 | } |
| 6014 | |
| 6015 | /* NOTE: same code as Linux kernel */ |
| 6016 | /* Allow LDTs to be cleared by the user. */ |
| 6017 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 6018 | if (oldmode || |
| 6019 | (contents == 0 && |
| 6020 | read_exec_only == 1 && |
| 6021 | seg_32bit == 0 && |
| 6022 | limit_in_pages == 0 && |
| 6023 | seg_not_present == 1 && |
| 6024 | useable == 0 )) { |
| 6025 | entry_1 = 0; |
| 6026 | entry_2 = 0; |
| 6027 | goto install; |
| 6028 | } |
| 6029 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6030 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6031 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
| 6032 | (ldt_info.limit & 0x0ffff); |
| 6033 | entry_2 = (ldt_info.base_addr & 0xff000000) | |
| 6034 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | |
| 6035 | (ldt_info.limit & 0xf0000) | |
| 6036 | ((read_exec_only ^ 1) << 9) | |
| 6037 | (contents << 10) | |
| 6038 | ((seg_not_present ^ 1) << 15) | |
| 6039 | (seg_32bit << 22) | |
| 6040 | (limit_in_pages << 23) | |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6041 | (lm << 21) | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6042 | 0x7000; |
| 6043 | if (!oldmode) |
| 6044 | entry_2 |= (useable << 20); |
bellard | 14ae3ba | 2003-05-27 23:25:06 +0000 | [diff] [blame] | 6045 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6046 | /* Install the new entry ... */ |
| 6047 | install: |
| 6048 | lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3)); |
| 6049 | lp[0] = tswap32(entry_1); |
| 6050 | lp[1] = tswap32(entry_2); |
| 6051 | return 0; |
| 6052 | } |
| 6053 | |
| 6054 | /* specific and weird i386 syscalls */ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 6055 | static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, |
| 6056 | unsigned long bytecount) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6057 | { |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 6058 | abi_long ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6059 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6060 | switch (func) { |
| 6061 | case 0: |
| 6062 | ret = read_ldt(ptr, bytecount); |
| 6063 | break; |
| 6064 | case 1: |
| 6065 | ret = write_ldt(env, ptr, bytecount, 1); |
| 6066 | break; |
| 6067 | case 0x11: |
| 6068 | ret = write_ldt(env, ptr, bytecount, 0); |
| 6069 | break; |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 6070 | default: |
| 6071 | ret = -TARGET_ENOSYS; |
| 6072 | break; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 6073 | } |
| 6074 | return ret; |
| 6075 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6076 | |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6077 | #if defined(TARGET_ABI32) |
Alexander Graf | bc22eb4 | 2013-07-16 18:44:58 +0100 | [diff] [blame] | 6078 | abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr) |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6079 | { |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 6080 | uint64_t *gdt_table = g2h_untagged(env->gdt.base); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6081 | struct target_modify_ldt_ldt_s ldt_info; |
| 6082 | struct target_modify_ldt_ldt_s *target_ldt_info; |
| 6083 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
| 6084 | int seg_not_present, useable, lm; |
| 6085 | uint32_t *lp, entry_1, entry_2; |
| 6086 | int i; |
| 6087 | |
| 6088 | lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); |
| 6089 | if (!target_ldt_info) |
| 6090 | return -TARGET_EFAULT; |
| 6091 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 6092 | ldt_info.base_addr = tswapal(target_ldt_info->base_addr); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6093 | ldt_info.limit = tswap32(target_ldt_info->limit); |
| 6094 | ldt_info.flags = tswap32(target_ldt_info->flags); |
| 6095 | if (ldt_info.entry_number == -1) { |
| 6096 | for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) { |
| 6097 | if (gdt_table[i] == 0) { |
| 6098 | ldt_info.entry_number = i; |
| 6099 | target_ldt_info->entry_number = tswap32(i); |
| 6100 | break; |
| 6101 | } |
| 6102 | } |
| 6103 | } |
| 6104 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 6105 | |
| 6106 | if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN || |
| 6107 | ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX) |
| 6108 | return -TARGET_EINVAL; |
| 6109 | seg_32bit = ldt_info.flags & 1; |
| 6110 | contents = (ldt_info.flags >> 1) & 3; |
| 6111 | read_exec_only = (ldt_info.flags >> 3) & 1; |
| 6112 | limit_in_pages = (ldt_info.flags >> 4) & 1; |
| 6113 | seg_not_present = (ldt_info.flags >> 5) & 1; |
| 6114 | useable = (ldt_info.flags >> 6) & 1; |
| 6115 | #ifdef TARGET_ABI32 |
| 6116 | lm = 0; |
| 6117 | #else |
| 6118 | lm = (ldt_info.flags >> 7) & 1; |
| 6119 | #endif |
| 6120 | |
| 6121 | if (contents == 3) { |
| 6122 | if (seg_not_present == 0) |
| 6123 | return -TARGET_EINVAL; |
| 6124 | } |
| 6125 | |
| 6126 | /* NOTE: same code as Linux kernel */ |
| 6127 | /* Allow LDTs to be cleared by the user. */ |
| 6128 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 6129 | if ((contents == 0 && |
| 6130 | read_exec_only == 1 && |
| 6131 | seg_32bit == 0 && |
| 6132 | limit_in_pages == 0 && |
| 6133 | seg_not_present == 1 && |
| 6134 | useable == 0 )) { |
| 6135 | entry_1 = 0; |
| 6136 | entry_2 = 0; |
| 6137 | goto install; |
| 6138 | } |
| 6139 | } |
| 6140 | |
| 6141 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
| 6142 | (ldt_info.limit & 0x0ffff); |
| 6143 | entry_2 = (ldt_info.base_addr & 0xff000000) | |
| 6144 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | |
| 6145 | (ldt_info.limit & 0xf0000) | |
| 6146 | ((read_exec_only ^ 1) << 9) | |
| 6147 | (contents << 10) | |
| 6148 | ((seg_not_present ^ 1) << 15) | |
| 6149 | (seg_32bit << 22) | |
| 6150 | (limit_in_pages << 23) | |
| 6151 | (useable << 20) | |
| 6152 | (lm << 21) | |
| 6153 | 0x7000; |
| 6154 | |
| 6155 | /* Install the new entry ... */ |
| 6156 | install: |
| 6157 | lp = (uint32_t *)(gdt_table + ldt_info.entry_number); |
| 6158 | lp[0] = tswap32(entry_1); |
| 6159 | lp[1] = tswap32(entry_2); |
| 6160 | return 0; |
| 6161 | } |
| 6162 | |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 6163 | static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr) |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6164 | { |
| 6165 | struct target_modify_ldt_ldt_s *target_ldt_info; |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 6166 | uint64_t *gdt_table = g2h_untagged(env->gdt.base); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6167 | uint32_t base_addr, limit, flags; |
| 6168 | int seg_32bit, contents, read_exec_only, limit_in_pages, idx; |
| 6169 | int seg_not_present, useable, lm; |
| 6170 | uint32_t *lp, entry_1, entry_2; |
| 6171 | |
| 6172 | lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); |
| 6173 | if (!target_ldt_info) |
| 6174 | return -TARGET_EFAULT; |
| 6175 | idx = tswap32(target_ldt_info->entry_number); |
| 6176 | if (idx < TARGET_GDT_ENTRY_TLS_MIN || |
| 6177 | idx > TARGET_GDT_ENTRY_TLS_MAX) { |
| 6178 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 6179 | return -TARGET_EINVAL; |
| 6180 | } |
| 6181 | lp = (uint32_t *)(gdt_table + idx); |
| 6182 | entry_1 = tswap32(lp[0]); |
| 6183 | entry_2 = tswap32(lp[1]); |
| 6184 | |
| 6185 | read_exec_only = ((entry_2 >> 9) & 1) ^ 1; |
| 6186 | contents = (entry_2 >> 10) & 3; |
| 6187 | seg_not_present = ((entry_2 >> 15) & 1) ^ 1; |
| 6188 | seg_32bit = (entry_2 >> 22) & 1; |
| 6189 | limit_in_pages = (entry_2 >> 23) & 1; |
| 6190 | useable = (entry_2 >> 20) & 1; |
| 6191 | #ifdef TARGET_ABI32 |
| 6192 | lm = 0; |
| 6193 | #else |
| 6194 | lm = (entry_2 >> 21) & 1; |
| 6195 | #endif |
| 6196 | flags = (seg_32bit << 0) | (contents << 1) | |
| 6197 | (read_exec_only << 3) | (limit_in_pages << 4) | |
| 6198 | (seg_not_present << 5) | (useable << 6) | (lm << 7); |
| 6199 | limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000); |
| 6200 | base_addr = (entry_1 >> 16) | |
| 6201 | (entry_2 & 0xff000000) | |
| 6202 | ((entry_2 & 0xff) << 16); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 6203 | target_ldt_info->base_addr = tswapal(base_addr); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 6204 | target_ldt_info->limit = tswap32(limit); |
| 6205 | target_ldt_info->flags = tswap32(flags); |
| 6206 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 6207 | return 0; |
| 6208 | } |
| 6209 | |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6210 | abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) |
| 6211 | { |
Helge Deller | 538fabc | 2020-04-25 00:00:33 +0200 | [diff] [blame] | 6212 | return -TARGET_ENOSYS; |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6213 | } |
| 6214 | #else |
Peter Maydell | 2667e71 | 2013-07-16 18:44:59 +0100 | [diff] [blame] | 6215 | abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6216 | { |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6217 | abi_long ret = 0; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6218 | abi_ulong val; |
| 6219 | int idx; |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6220 | |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6221 | switch(code) { |
| 6222 | case TARGET_ARCH_SET_GS: |
| 6223 | case TARGET_ARCH_SET_FS: |
| 6224 | if (code == TARGET_ARCH_SET_GS) |
| 6225 | idx = R_GS; |
| 6226 | else |
| 6227 | idx = R_FS; |
| 6228 | cpu_x86_load_seg(env, idx, 0); |
| 6229 | env->segs[idx].base = addr; |
| 6230 | break; |
| 6231 | case TARGET_ARCH_GET_GS: |
| 6232 | case TARGET_ARCH_GET_FS: |
| 6233 | if (code == TARGET_ARCH_GET_GS) |
| 6234 | idx = R_GS; |
| 6235 | else |
| 6236 | idx = R_FS; |
| 6237 | val = env->segs[idx].base; |
| 6238 | if (put_user(val, addr, abi_ulong)) |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6239 | ret = -TARGET_EFAULT; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6240 | break; |
| 6241 | default: |
| 6242 | ret = -TARGET_EINVAL; |
| 6243 | break; |
| 6244 | } |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6245 | return ret; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6246 | } |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6247 | #endif /* defined(TARGET_ABI32 */ |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6248 | #endif /* defined(TARGET_I386) */ |
| 6249 | |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6250 | /* |
| 6251 | * These constants are generic. Supply any that are missing from the host. |
| 6252 | */ |
| 6253 | #ifndef PR_SET_NAME |
| 6254 | # define PR_SET_NAME 15 |
| 6255 | # define PR_GET_NAME 16 |
| 6256 | #endif |
| 6257 | #ifndef PR_SET_FP_MODE |
| 6258 | # define PR_SET_FP_MODE 45 |
| 6259 | # define PR_GET_FP_MODE 46 |
| 6260 | # define PR_FP_MODE_FR (1 << 0) |
| 6261 | # define PR_FP_MODE_FRE (1 << 1) |
| 6262 | #endif |
| 6263 | #ifndef PR_SVE_SET_VL |
| 6264 | # define PR_SVE_SET_VL 50 |
| 6265 | # define PR_SVE_GET_VL 51 |
| 6266 | # define PR_SVE_VL_LEN_MASK 0xffff |
| 6267 | # define PR_SVE_VL_INHERIT (1 << 17) |
| 6268 | #endif |
| 6269 | #ifndef PR_PAC_RESET_KEYS |
| 6270 | # define PR_PAC_RESET_KEYS 54 |
| 6271 | # define PR_PAC_APIAKEY (1 << 0) |
| 6272 | # define PR_PAC_APIBKEY (1 << 1) |
| 6273 | # define PR_PAC_APDAKEY (1 << 2) |
| 6274 | # define PR_PAC_APDBKEY (1 << 3) |
| 6275 | # define PR_PAC_APGAKEY (1 << 4) |
| 6276 | #endif |
| 6277 | #ifndef PR_SET_TAGGED_ADDR_CTRL |
| 6278 | # define PR_SET_TAGGED_ADDR_CTRL 55 |
| 6279 | # define PR_GET_TAGGED_ADDR_CTRL 56 |
| 6280 | # define PR_TAGGED_ADDR_ENABLE (1UL << 0) |
| 6281 | #endif |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6282 | #ifndef PR_SET_IO_FLUSHER |
| 6283 | # define PR_SET_IO_FLUSHER 57 |
| 6284 | # define PR_GET_IO_FLUSHER 58 |
| 6285 | #endif |
| 6286 | #ifndef PR_SET_SYSCALL_USER_DISPATCH |
| 6287 | # define PR_SET_SYSCALL_USER_DISPATCH 59 |
| 6288 | #endif |
Richard Henderson | 24d87c1 | 2022-07-08 20:45:37 +0530 | [diff] [blame] | 6289 | #ifndef PR_SME_SET_VL |
| 6290 | # define PR_SME_SET_VL 63 |
| 6291 | # define PR_SME_GET_VL 64 |
| 6292 | # define PR_SME_VL_LEN_MASK 0xffff |
| 6293 | # define PR_SME_VL_INHERIT (1 << 17) |
| 6294 | #endif |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6295 | |
| 6296 | #include "target_prctl.h" |
| 6297 | |
| 6298 | static abi_long do_prctl_inval0(CPUArchState *env) |
| 6299 | { |
| 6300 | return -TARGET_EINVAL; |
| 6301 | } |
| 6302 | |
| 6303 | static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2) |
| 6304 | { |
| 6305 | return -TARGET_EINVAL; |
| 6306 | } |
| 6307 | |
| 6308 | #ifndef do_prctl_get_fp_mode |
| 6309 | #define do_prctl_get_fp_mode do_prctl_inval0 |
| 6310 | #endif |
| 6311 | #ifndef do_prctl_set_fp_mode |
| 6312 | #define do_prctl_set_fp_mode do_prctl_inval1 |
| 6313 | #endif |
Richard Henderson | fd72f5d | 2022-07-08 20:45:36 +0530 | [diff] [blame] | 6314 | #ifndef do_prctl_sve_get_vl |
| 6315 | #define do_prctl_sve_get_vl do_prctl_inval0 |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6316 | #endif |
Richard Henderson | fd72f5d | 2022-07-08 20:45:36 +0530 | [diff] [blame] | 6317 | #ifndef do_prctl_sve_set_vl |
| 6318 | #define do_prctl_sve_set_vl do_prctl_inval1 |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6319 | #endif |
| 6320 | #ifndef do_prctl_reset_keys |
| 6321 | #define do_prctl_reset_keys do_prctl_inval1 |
| 6322 | #endif |
| 6323 | #ifndef do_prctl_set_tagged_addr_ctrl |
| 6324 | #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1 |
| 6325 | #endif |
| 6326 | #ifndef do_prctl_get_tagged_addr_ctrl |
| 6327 | #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0 |
| 6328 | #endif |
Richard Henderson | 6e8dcac | 2021-12-27 07:01:24 -0800 | [diff] [blame] | 6329 | #ifndef do_prctl_get_unalign |
| 6330 | #define do_prctl_get_unalign do_prctl_inval1 |
| 6331 | #endif |
| 6332 | #ifndef do_prctl_set_unalign |
| 6333 | #define do_prctl_set_unalign do_prctl_inval1 |
| 6334 | #endif |
Richard Henderson | 24d87c1 | 2022-07-08 20:45:37 +0530 | [diff] [blame] | 6335 | #ifndef do_prctl_sme_get_vl |
| 6336 | #define do_prctl_sme_get_vl do_prctl_inval0 |
| 6337 | #endif |
| 6338 | #ifndef do_prctl_sme_set_vl |
| 6339 | #define do_prctl_sme_set_vl do_prctl_inval1 |
| 6340 | #endif |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6341 | |
| 6342 | static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, |
| 6343 | abi_long arg3, abi_long arg4, abi_long arg5) |
| 6344 | { |
| 6345 | abi_long ret; |
| 6346 | |
| 6347 | switch (option) { |
| 6348 | case PR_GET_PDEATHSIG: |
| 6349 | { |
| 6350 | int deathsig; |
| 6351 | ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig, |
| 6352 | arg3, arg4, arg5)); |
Richard Henderson | 1edebb3 | 2022-01-06 14:57:36 -0800 | [diff] [blame] | 6353 | if (!is_error(ret) && |
| 6354 | put_user_s32(host_to_target_signal(deathsig), arg2)) { |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6355 | return -TARGET_EFAULT; |
| 6356 | } |
| 6357 | return ret; |
| 6358 | } |
Richard Henderson | f746c65 | 2022-01-06 14:57:37 -0800 | [diff] [blame] | 6359 | case PR_SET_PDEATHSIG: |
| 6360 | return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2), |
| 6361 | arg3, arg4, arg5)); |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6362 | case PR_GET_NAME: |
| 6363 | { |
| 6364 | void *name = lock_user(VERIFY_WRITE, arg2, 16, 1); |
| 6365 | if (!name) { |
| 6366 | return -TARGET_EFAULT; |
| 6367 | } |
| 6368 | ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name, |
| 6369 | arg3, arg4, arg5)); |
| 6370 | unlock_user(name, arg2, 16); |
| 6371 | return ret; |
| 6372 | } |
| 6373 | case PR_SET_NAME: |
| 6374 | { |
| 6375 | void *name = lock_user(VERIFY_READ, arg2, 16, 1); |
| 6376 | if (!name) { |
| 6377 | return -TARGET_EFAULT; |
| 6378 | } |
| 6379 | ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name, |
| 6380 | arg3, arg4, arg5)); |
| 6381 | unlock_user(name, arg2, 0); |
| 6382 | return ret; |
| 6383 | } |
| 6384 | case PR_GET_FP_MODE: |
| 6385 | return do_prctl_get_fp_mode(env); |
| 6386 | case PR_SET_FP_MODE: |
| 6387 | return do_prctl_set_fp_mode(env, arg2); |
| 6388 | case PR_SVE_GET_VL: |
Richard Henderson | fd72f5d | 2022-07-08 20:45:36 +0530 | [diff] [blame] | 6389 | return do_prctl_sve_get_vl(env); |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6390 | case PR_SVE_SET_VL: |
Richard Henderson | fd72f5d | 2022-07-08 20:45:36 +0530 | [diff] [blame] | 6391 | return do_prctl_sve_set_vl(env, arg2); |
Richard Henderson | 24d87c1 | 2022-07-08 20:45:37 +0530 | [diff] [blame] | 6392 | case PR_SME_GET_VL: |
| 6393 | return do_prctl_sme_get_vl(env); |
| 6394 | case PR_SME_SET_VL: |
| 6395 | return do_prctl_sme_set_vl(env, arg2); |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6396 | case PR_PAC_RESET_KEYS: |
| 6397 | if (arg3 || arg4 || arg5) { |
| 6398 | return -TARGET_EINVAL; |
| 6399 | } |
| 6400 | return do_prctl_reset_keys(env, arg2); |
| 6401 | case PR_SET_TAGGED_ADDR_CTRL: |
| 6402 | if (arg3 || arg4 || arg5) { |
| 6403 | return -TARGET_EINVAL; |
| 6404 | } |
| 6405 | return do_prctl_set_tagged_addr_ctrl(env, arg2); |
| 6406 | case PR_GET_TAGGED_ADDR_CTRL: |
| 6407 | if (arg2 || arg3 || arg4 || arg5) { |
| 6408 | return -TARGET_EINVAL; |
| 6409 | } |
| 6410 | return do_prctl_get_tagged_addr_ctrl(env); |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6411 | |
Richard Henderson | 6e8dcac | 2021-12-27 07:01:24 -0800 | [diff] [blame] | 6412 | case PR_GET_UNALIGN: |
| 6413 | return do_prctl_get_unalign(env, arg2); |
| 6414 | case PR_SET_UNALIGN: |
| 6415 | return do_prctl_set_unalign(env, arg2); |
| 6416 | |
Richard Henderson | 4f4e556 | 2022-01-06 14:57:38 -0800 | [diff] [blame] | 6417 | case PR_CAP_AMBIENT: |
| 6418 | case PR_CAPBSET_READ: |
| 6419 | case PR_CAPBSET_DROP: |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6420 | case PR_GET_DUMPABLE: |
| 6421 | case PR_SET_DUMPABLE: |
| 6422 | case PR_GET_KEEPCAPS: |
| 6423 | case PR_SET_KEEPCAPS: |
Richard Henderson | 4f4e556 | 2022-01-06 14:57:38 -0800 | [diff] [blame] | 6424 | case PR_GET_SECUREBITS: |
| 6425 | case PR_SET_SECUREBITS: |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6426 | case PR_GET_TIMING: |
| 6427 | case PR_SET_TIMING: |
| 6428 | case PR_GET_TIMERSLACK: |
| 6429 | case PR_SET_TIMERSLACK: |
| 6430 | case PR_MCE_KILL: |
| 6431 | case PR_MCE_KILL_GET: |
| 6432 | case PR_GET_NO_NEW_PRIVS: |
| 6433 | case PR_SET_NO_NEW_PRIVS: |
| 6434 | case PR_GET_IO_FLUSHER: |
| 6435 | case PR_SET_IO_FLUSHER: |
Richard Henderson | 07726f5 | 2024-03-01 14:52:30 -1000 | [diff] [blame] | 6436 | case PR_SET_CHILD_SUBREAPER: |
Richard Henderson | 91511bd | 2024-03-01 14:56:45 -1000 | [diff] [blame] | 6437 | case PR_GET_SPECULATION_CTRL: |
| 6438 | case PR_SET_SPECULATION_CTRL: |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6439 | /* Some prctl options have no pointer arguments and we can pass on. */ |
| 6440 | return get_errno(prctl(option, arg2, arg3, arg4, arg5)); |
| 6441 | |
| 6442 | case PR_GET_CHILD_SUBREAPER: |
Richard Henderson | 07726f5 | 2024-03-01 14:52:30 -1000 | [diff] [blame] | 6443 | { |
| 6444 | int val; |
| 6445 | ret = get_errno(prctl(PR_GET_CHILD_SUBREAPER, &val, |
| 6446 | arg3, arg4, arg5)); |
| 6447 | if (!is_error(ret) && put_user_s32(val, arg2)) { |
| 6448 | return -TARGET_EFAULT; |
| 6449 | } |
| 6450 | return ret; |
| 6451 | } |
| 6452 | |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6453 | case PR_GET_TID_ADDRESS: |
Richard Henderson | 8de24b1 | 2024-03-01 15:04:39 -1000 | [diff] [blame] | 6454 | { |
Philippe Mathieu-Daudé | 5927246 | 2024-04-25 11:12:19 +0200 | [diff] [blame] | 6455 | TaskState *ts = get_task_state(env_cpu(env)); |
Richard Henderson | 8de24b1 | 2024-03-01 15:04:39 -1000 | [diff] [blame] | 6456 | return put_user_ual(ts->child_tidptr, arg2); |
| 6457 | } |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6458 | |
| 6459 | case PR_GET_FPEXC: |
| 6460 | case PR_SET_FPEXC: |
| 6461 | /* Was used for SPE on PowerPC. */ |
| 6462 | return -TARGET_EINVAL; |
| 6463 | |
| 6464 | case PR_GET_ENDIAN: |
| 6465 | case PR_SET_ENDIAN: |
| 6466 | case PR_GET_FPEMU: |
| 6467 | case PR_SET_FPEMU: |
| 6468 | case PR_SET_MM: |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6469 | case PR_GET_SECCOMP: |
| 6470 | case PR_SET_SECCOMP: |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6471 | case PR_SET_SYSCALL_USER_DISPATCH: |
| 6472 | case PR_GET_THP_DISABLE: |
| 6473 | case PR_SET_THP_DISABLE: |
| 6474 | case PR_GET_TSC: |
| 6475 | case PR_SET_TSC: |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6476 | /* Disable to prevent the target disabling stuff we need. */ |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6477 | return -TARGET_EINVAL; |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6478 | |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6479 | default: |
Richard Henderson | 220717a | 2021-12-27 07:01:23 -0800 | [diff] [blame] | 6480 | qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n", |
| 6481 | option); |
| 6482 | return -TARGET_EINVAL; |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 6483 | } |
| 6484 | } |
| 6485 | |
Riku Voipio | 05098a9 | 2011-03-04 15:27:29 +0200 | [diff] [blame] | 6486 | #define NEW_STACK_SIZE 0x40000 |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6487 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6488 | |
| 6489 | static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER; |
| 6490 | typedef struct { |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6491 | CPUArchState *env; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6492 | pthread_mutex_t mutex; |
| 6493 | pthread_cond_t cond; |
| 6494 | pthread_t thread; |
| 6495 | uint32_t tid; |
| 6496 | abi_ulong child_tidptr; |
| 6497 | abi_ulong parent_tidptr; |
| 6498 | sigset_t sigmask; |
| 6499 | } new_thread_info; |
| 6500 | |
| 6501 | static void *clone_func(void *arg) |
| 6502 | { |
| 6503 | new_thread_info *info = arg; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6504 | CPUArchState *env; |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 6505 | CPUState *cpu; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 6506 | TaskState *ts; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6507 | |
Emilio G. Cota | 7090376 | 2015-08-23 20:23:41 -0400 | [diff] [blame] | 6508 | rcu_register_thread(); |
Emilio G. Cota | 3468b59 | 2017-07-19 18:57:58 -0400 | [diff] [blame] | 6509 | tcg_register_thread(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6510 | env = info->env; |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 6511 | cpu = env_cpu(env); |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 6512 | thread_cpu = cpu; |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 6513 | ts = get_task_state(cpu); |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 6514 | info->tid = sys_gettid(); |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 6515 | task_settid(ts); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6516 | if (info->child_tidptr) |
| 6517 | put_user_u32(info->tid, info->child_tidptr); |
| 6518 | if (info->parent_tidptr) |
| 6519 | put_user_u32(info->tid, info->parent_tidptr); |
Richard Henderson | 5ebdd77 | 2019-03-14 13:10:53 -0700 | [diff] [blame] | 6520 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6521 | /* Enable signals. */ |
| 6522 | sigprocmask(SIG_SETMASK, &info->sigmask, NULL); |
| 6523 | /* Signal to the parent that we're ready. */ |
| 6524 | pthread_mutex_lock(&info->mutex); |
| 6525 | pthread_cond_broadcast(&info->cond); |
| 6526 | pthread_mutex_unlock(&info->mutex); |
Emilio G. Cota | a4dd3d5 | 2017-10-18 18:01:41 -0400 | [diff] [blame] | 6527 | /* Wait until the parent has finished initializing the tls state. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6528 | pthread_mutex_lock(&clone_lock); |
| 6529 | pthread_mutex_unlock(&clone_lock); |
| 6530 | cpu_loop(env); |
| 6531 | /* never exits */ |
| 6532 | return NULL; |
| 6533 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6534 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 6535 | /* do_fork() Must return host values and target errnos (unlike most |
| 6536 | do_*() functions). */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6537 | static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6538 | abi_ulong parent_tidptr, target_ulong newtls, |
| 6539 | abi_ulong child_tidptr) |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6540 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 6541 | CPUState *cpu = env_cpu(env); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6542 | int ret; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 6543 | TaskState *ts; |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6544 | CPUState *new_cpu; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6545 | CPUArchState *new_env; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6546 | sigset_t sigmask; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6547 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 6548 | flags &= ~CLONE_IGNORED_FLAGS; |
| 6549 | |
balrog | 436d124 | 2008-09-21 02:39:45 +0000 | [diff] [blame] | 6550 | /* Emulate vfork() with fork() */ |
| 6551 | if (flags & CLONE_VFORK) |
| 6552 | flags &= ~(CLONE_VFORK | CLONE_VM); |
| 6553 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6554 | if (flags & CLONE_VM) { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 6555 | TaskState *parent_ts = get_task_state(cpu); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6556 | new_thread_info info; |
| 6557 | pthread_attr_t attr; |
Peter Maydell | 24cb36a | 2013-07-16 18:45:00 +0100 | [diff] [blame] | 6558 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 6559 | if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) || |
| 6560 | (flags & CLONE_INVALID_THREAD_FLAGS)) { |
| 6561 | return -TARGET_EINVAL; |
| 6562 | } |
| 6563 | |
Markus Armbruster | c78d65e | 2015-09-14 13:53:03 +0200 | [diff] [blame] | 6564 | ts = g_new0(TaskState, 1); |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 6565 | init_task_state(ts); |
Max Filippov | 73a988d | 2018-03-30 06:19:58 -0700 | [diff] [blame] | 6566 | |
| 6567 | /* Grab a mutex so that thread setup appears atomic. */ |
| 6568 | pthread_mutex_lock(&clone_lock); |
| 6569 | |
Richard Henderson | 6cc9d67 | 2021-03-01 19:21:08 -0800 | [diff] [blame] | 6570 | /* |
| 6571 | * If this is our first additional thread, we need to ensure we |
| 6572 | * generate code for parallel execution and flush old translations. |
| 6573 | * Do this now so that the copy gets CF_PARALLEL too. |
| 6574 | */ |
Philippe Mathieu-Daudé | b254c34 | 2024-01-10 18:09:56 +0100 | [diff] [blame] | 6575 | if (!tcg_cflags_has(cpu, CF_PARALLEL)) { |
| 6576 | tcg_cflags_set(cpu, CF_PARALLEL); |
Richard Henderson | 6cc9d67 | 2021-03-01 19:21:08 -0800 | [diff] [blame] | 6577 | tb_flush(cpu); |
| 6578 | } |
| 6579 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6580 | /* we create a new CPU instance. */ |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 6581 | new_env = cpu_copy(env); |
pbrook | 6e68e07 | 2008-05-30 17:22:15 +0000 | [diff] [blame] | 6582 | /* Init regs that differ from the parent. */ |
Richard Henderson | 608999d | 2019-11-06 12:33:15 +0100 | [diff] [blame] | 6583 | cpu_clone_regs_child(new_env, newsp, flags); |
Richard Henderson | 07a6ecf | 2019-11-06 12:33:16 +0100 | [diff] [blame] | 6584 | cpu_clone_regs_parent(env, flags); |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 6585 | new_cpu = env_cpu(new_env); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6586 | new_cpu->opaque = ts; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 6587 | ts->bprm = parent_ts->bprm; |
| 6588 | ts->info = parent_ts->info; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 6589 | ts->signal_mask = parent_ts->signal_mask; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6590 | |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6591 | if (flags & CLONE_CHILD_CLEARTID) { |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6592 | ts->child_tidptr = child_tidptr; |
| 6593 | } |
| 6594 | |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6595 | if (flags & CLONE_SETTLS) { |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6596 | cpu_set_tls (new_env, newtls); |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6597 | } |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6598 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6599 | memset(&info, 0, sizeof(info)); |
| 6600 | pthread_mutex_init(&info.mutex, NULL); |
| 6601 | pthread_mutex_lock(&info.mutex); |
| 6602 | pthread_cond_init(&info.cond, NULL); |
| 6603 | info.env = new_env; |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6604 | if (flags & CLONE_CHILD_SETTID) { |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6605 | info.child_tidptr = child_tidptr; |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6606 | } |
| 6607 | if (flags & CLONE_PARENT_SETTID) { |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6608 | info.parent_tidptr = parent_tidptr; |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6609 | } |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6610 | |
| 6611 | ret = pthread_attr_init(&attr); |
Nathan Froyd | 48e15fc | 2010-10-29 07:48:57 -0700 | [diff] [blame] | 6612 | ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE); |
| 6613 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6614 | /* It is not safe to deliver signals until the child has finished |
| 6615 | initializing, so temporarily block all signals. */ |
| 6616 | sigfillset(&sigmask); |
| 6617 | sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask); |
Richard Henderson | 5ebdd77 | 2019-03-14 13:10:53 -0700 | [diff] [blame] | 6618 | cpu->random_seed = qemu_guest_random_seed_thread_part1(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6619 | |
| 6620 | ret = pthread_create(&info.thread, &attr, clone_func, &info); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6621 | /* TODO: Free new CPU state if thread creation failed. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6622 | |
| 6623 | sigprocmask(SIG_SETMASK, &info.sigmask, NULL); |
| 6624 | pthread_attr_destroy(&attr); |
| 6625 | if (ret == 0) { |
| 6626 | /* Wait for the child to initialize. */ |
| 6627 | pthread_cond_wait(&info.cond, &info.mutex); |
| 6628 | ret = info.tid; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6629 | } else { |
| 6630 | ret = -1; |
| 6631 | } |
| 6632 | pthread_mutex_unlock(&info.mutex); |
| 6633 | pthread_cond_destroy(&info.cond); |
| 6634 | pthread_mutex_destroy(&info.mutex); |
| 6635 | pthread_mutex_unlock(&clone_lock); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6636 | } else { |
| 6637 | /* if no CLONE_VM, we consider it is a fork */ |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 6638 | if (flags & CLONE_INVALID_FORK_FLAGS) { |
| 6639 | return -TARGET_EINVAL; |
| 6640 | } |
| 6641 | |
| 6642 | /* We can't support custom termination signals */ |
| 6643 | if ((flags & CSIGNAL) != TARGET_SIGCHLD) { |
Timothy E Baldwin | 93b4eff | 2015-08-31 00:26:21 +0100 | [diff] [blame] | 6644 | return -TARGET_EINVAL; |
| 6645 | } |
Timothy E Baldwin | 7d92d34 | 2016-05-27 15:51:58 +0100 | [diff] [blame] | 6646 | |
Helge Deller | 895ce8b | 2022-11-29 12:08:20 +0100 | [diff] [blame] | 6647 | #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open) |
| 6648 | if (flags & CLONE_PIDFD) { |
| 6649 | return -TARGET_EINVAL; |
| 6650 | } |
| 6651 | #endif |
| 6652 | |
| 6653 | /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */ |
| 6654 | if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) { |
| 6655 | return -TARGET_EINVAL; |
| 6656 | } |
| 6657 | |
Timothy E Baldwin | 7d92d34 | 2016-05-27 15:51:58 +0100 | [diff] [blame] | 6658 | if (block_signals()) { |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 6659 | return -QEMU_ERESTARTSYS; |
Timothy E Baldwin | 7d92d34 | 2016-05-27 15:51:58 +0100 | [diff] [blame] | 6660 | } |
| 6661 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6662 | fork_start(); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6663 | ret = fork(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6664 | if (ret == 0) { |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 6665 | /* Child Process. */ |
Richard Henderson | 608999d | 2019-11-06 12:33:15 +0100 | [diff] [blame] | 6666 | cpu_clone_regs_child(env, newsp, flags); |
Ilya Leoshkevich | 4edc98f | 2024-03-05 12:09:42 +0000 | [diff] [blame] | 6667 | fork_end(ret); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 6668 | /* There is a race condition here. The parent process could |
| 6669 | theoretically read the TID in the child process before the child |
| 6670 | tid is set. This would require using either ptrace |
| 6671 | (not implemented) or having *_tidptr to point at a shared memory |
| 6672 | mapping. We can't repeat the spinlock hack used above because |
| 6673 | the child process gets its own copy of the lock. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6674 | if (flags & CLONE_CHILD_SETTID) |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 6675 | put_user_u32(sys_gettid(), child_tidptr); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6676 | if (flags & CLONE_PARENT_SETTID) |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 6677 | put_user_u32(sys_gettid(), parent_tidptr); |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 6678 | ts = get_task_state(cpu); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6679 | if (flags & CLONE_SETTLS) |
| 6680 | cpu_set_tls (env, newtls); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6681 | if (flags & CLONE_CHILD_CLEARTID) |
| 6682 | ts->child_tidptr = child_tidptr; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6683 | } else { |
Richard Henderson | 07a6ecf | 2019-11-06 12:33:16 +0100 | [diff] [blame] | 6684 | cpu_clone_regs_parent(env, flags); |
Helge Deller | 895ce8b | 2022-11-29 12:08:20 +0100 | [diff] [blame] | 6685 | if (flags & CLONE_PIDFD) { |
| 6686 | int pid_fd = 0; |
| 6687 | #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open) |
| 6688 | int pid_child = ret; |
| 6689 | pid_fd = pidfd_open(pid_child, 0); |
| 6690 | if (pid_fd >= 0) { |
| 6691 | fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL) |
| 6692 | | FD_CLOEXEC); |
| 6693 | } else { |
| 6694 | pid_fd = 0; |
| 6695 | } |
| 6696 | #endif |
| 6697 | put_user_u32(pid_fd, parent_tidptr); |
Ilya Leoshkevich | 4edc98f | 2024-03-05 12:09:42 +0000 | [diff] [blame] | 6698 | } |
| 6699 | fork_end(ret); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6700 | } |
Ilya Leoshkevich | 7de0816 | 2023-02-14 15:08:26 +0100 | [diff] [blame] | 6701 | g_assert(!cpu_in_exclusive_context(cpu)); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6702 | } |
| 6703 | return ret; |
| 6704 | } |
| 6705 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6706 | /* warning : doesn't handle linux specific flags... */ |
| 6707 | static int target_to_host_fcntl_cmd(int cmd) |
| 6708 | { |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6709 | int ret; |
| 6710 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6711 | switch(cmd) { |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6712 | case TARGET_F_DUPFD: |
| 6713 | case TARGET_F_GETFD: |
| 6714 | case TARGET_F_SETFD: |
| 6715 | case TARGET_F_GETFL: |
| 6716 | case TARGET_F_SETFL: |
Andreas Schwab | 2d92c68 | 2020-05-25 09:59:28 +0200 | [diff] [blame] | 6717 | case TARGET_F_OFD_GETLK: |
| 6718 | case TARGET_F_OFD_SETLK: |
| 6719 | case TARGET_F_OFD_SETLKW: |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6720 | ret = cmd; |
| 6721 | break; |
| 6722 | case TARGET_F_GETLK: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6723 | ret = F_GETLK; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6724 | break; |
| 6725 | case TARGET_F_SETLK: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6726 | ret = F_SETLK; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6727 | break; |
| 6728 | case TARGET_F_SETLKW: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6729 | ret = F_SETLKW; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6730 | break; |
| 6731 | case TARGET_F_GETOWN: |
| 6732 | ret = F_GETOWN; |
| 6733 | break; |
| 6734 | case TARGET_F_SETOWN: |
| 6735 | ret = F_SETOWN; |
| 6736 | break; |
| 6737 | case TARGET_F_GETSIG: |
| 6738 | ret = F_GETSIG; |
| 6739 | break; |
| 6740 | case TARGET_F_SETSIG: |
| 6741 | ret = F_SETSIG; |
| 6742 | break; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6743 | #if TARGET_ABI_BITS == 32 |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6744 | case TARGET_F_GETLK64: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6745 | ret = F_GETLK; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6746 | break; |
| 6747 | case TARGET_F_SETLK64: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6748 | ret = F_SETLK; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6749 | break; |
| 6750 | case TARGET_F_SETLKW64: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6751 | ret = F_SETLKW; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6752 | break; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6753 | #endif |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6754 | case TARGET_F_SETLEASE: |
| 6755 | ret = F_SETLEASE; |
| 6756 | break; |
| 6757 | case TARGET_F_GETLEASE: |
| 6758 | ret = F_GETLEASE; |
| 6759 | break; |
malc | fbd5de9 | 2009-09-06 06:31:59 +0400 | [diff] [blame] | 6760 | #ifdef F_DUPFD_CLOEXEC |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6761 | case TARGET_F_DUPFD_CLOEXEC: |
| 6762 | ret = F_DUPFD_CLOEXEC; |
| 6763 | break; |
malc | fbd5de9 | 2009-09-06 06:31:59 +0400 | [diff] [blame] | 6764 | #endif |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6765 | case TARGET_F_NOTIFY: |
| 6766 | ret = F_NOTIFY; |
| 6767 | break; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6768 | #ifdef F_GETOWN_EX |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6769 | case TARGET_F_GETOWN_EX: |
| 6770 | ret = F_GETOWN_EX; |
| 6771 | break; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6772 | #endif |
| 6773 | #ifdef F_SETOWN_EX |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6774 | case TARGET_F_SETOWN_EX: |
| 6775 | ret = F_SETOWN_EX; |
| 6776 | break; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6777 | #endif |
Peter Maydell | ddf31aa | 2016-06-30 17:33:49 +0100 | [diff] [blame] | 6778 | #ifdef F_SETPIPE_SZ |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6779 | case TARGET_F_SETPIPE_SZ: |
| 6780 | ret = F_SETPIPE_SZ; |
| 6781 | break; |
| 6782 | case TARGET_F_GETPIPE_SZ: |
| 6783 | ret = F_GETPIPE_SZ; |
| 6784 | break; |
Peter Maydell | ddf31aa | 2016-06-30 17:33:49 +0100 | [diff] [blame] | 6785 | #endif |
Shu-Chun Weng | 2bb963f | 2020-12-18 11:32:10 -0800 | [diff] [blame] | 6786 | #ifdef F_ADD_SEALS |
| 6787 | case TARGET_F_ADD_SEALS: |
| 6788 | ret = F_ADD_SEALS; |
| 6789 | break; |
| 6790 | case TARGET_F_GET_SEALS: |
| 6791 | ret = F_GET_SEALS; |
| 6792 | break; |
| 6793 | #endif |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6794 | default: |
| 6795 | ret = -TARGET_EINVAL; |
| 6796 | break; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6797 | } |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6798 | |
| 6799 | #if defined(__powerpc64__) |
| 6800 | /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and |
| 6801 | * is not supported by kernel. The glibc fcntl call actually adjusts |
| 6802 | * them to 5, 6 and 7 before making the syscall(). Since we make the |
| 6803 | * syscall directly, adjust to what is supported by the kernel. |
| 6804 | */ |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6805 | if (ret >= F_GETLK && ret <= F_SETLKW) { |
| 6806 | ret -= F_GETLK - 5; |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6807 | } |
| 6808 | #endif |
| 6809 | |
| 6810 | return ret; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6811 | } |
| 6812 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6813 | #define FLOCK_TRANSTBL \ |
| 6814 | switch (type) { \ |
| 6815 | TRANSTBL_CONVERT(F_RDLCK); \ |
| 6816 | TRANSTBL_CONVERT(F_WRLCK); \ |
| 6817 | TRANSTBL_CONVERT(F_UNLCK); \ |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6818 | } |
| 6819 | |
| 6820 | static int target_to_host_flock(int type) |
| 6821 | { |
| 6822 | #define TRANSTBL_CONVERT(a) case TARGET_##a: return a |
| 6823 | FLOCK_TRANSTBL |
| 6824 | #undef TRANSTBL_CONVERT |
| 6825 | return -TARGET_EINVAL; |
| 6826 | } |
| 6827 | |
| 6828 | static int host_to_target_flock(int type) |
| 6829 | { |
| 6830 | #define TRANSTBL_CONVERT(a) case a: return TARGET_##a |
| 6831 | FLOCK_TRANSTBL |
| 6832 | #undef TRANSTBL_CONVERT |
| 6833 | /* if we don't know how to convert the value coming |
| 6834 | * from the host we copy to the target field as-is |
| 6835 | */ |
| 6836 | return type; |
| 6837 | } |
Laurent Vivier | 2ba7f73 | 2013-01-10 21:42:48 +0100 | [diff] [blame] | 6838 | |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6839 | static inline abi_long copy_from_user_flock(struct flock *fl, |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6840 | abi_ulong target_flock_addr) |
| 6841 | { |
| 6842 | struct target_flock *target_fl; |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6843 | int l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6844 | |
| 6845 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 6846 | return -TARGET_EFAULT; |
| 6847 | } |
| 6848 | |
| 6849 | __get_user(l_type, &target_fl->l_type); |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6850 | l_type = target_to_host_flock(l_type); |
| 6851 | if (l_type < 0) { |
| 6852 | return l_type; |
| 6853 | } |
| 6854 | fl->l_type = l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6855 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 6856 | __get_user(fl->l_start, &target_fl->l_start); |
| 6857 | __get_user(fl->l_len, &target_fl->l_len); |
| 6858 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 6859 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 6860 | return 0; |
| 6861 | } |
| 6862 | |
| 6863 | static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr, |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6864 | const struct flock *fl) |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6865 | { |
| 6866 | struct target_flock *target_fl; |
| 6867 | short l_type; |
| 6868 | |
| 6869 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 6870 | return -TARGET_EFAULT; |
| 6871 | } |
| 6872 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6873 | l_type = host_to_target_flock(fl->l_type); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6874 | __put_user(l_type, &target_fl->l_type); |
| 6875 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 6876 | __put_user(fl->l_start, &target_fl->l_start); |
| 6877 | __put_user(fl->l_len, &target_fl->l_len); |
| 6878 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 6879 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 6880 | return 0; |
| 6881 | } |
| 6882 | |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6883 | typedef abi_long from_flock64_fn(struct flock *fl, abi_ulong target_addr); |
| 6884 | typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock *fl); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6885 | |
| 6886 | #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32 |
Richard Henderson | ffe81d4 | 2022-01-06 20:25:59 -0800 | [diff] [blame] | 6887 | struct target_oabi_flock64 { |
| 6888 | abi_short l_type; |
| 6889 | abi_short l_whence; |
| 6890 | abi_llong l_start; |
| 6891 | abi_llong l_len; |
| 6892 | abi_int l_pid; |
| 6893 | } QEMU_PACKED; |
| 6894 | |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6895 | static inline abi_long copy_from_user_oabi_flock64(struct flock *fl, |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6896 | abi_ulong target_flock_addr) |
| 6897 | { |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6898 | struct target_oabi_flock64 *target_fl; |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6899 | int l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6900 | |
| 6901 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 6902 | return -TARGET_EFAULT; |
| 6903 | } |
| 6904 | |
| 6905 | __get_user(l_type, &target_fl->l_type); |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6906 | l_type = target_to_host_flock(l_type); |
| 6907 | if (l_type < 0) { |
| 6908 | return l_type; |
| 6909 | } |
| 6910 | fl->l_type = l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6911 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 6912 | __get_user(fl->l_start, &target_fl->l_start); |
| 6913 | __get_user(fl->l_len, &target_fl->l_len); |
| 6914 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 6915 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 6916 | return 0; |
| 6917 | } |
| 6918 | |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6919 | static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr, |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6920 | const struct flock *fl) |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6921 | { |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6922 | struct target_oabi_flock64 *target_fl; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6923 | short l_type; |
| 6924 | |
| 6925 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 6926 | return -TARGET_EFAULT; |
| 6927 | } |
| 6928 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6929 | l_type = host_to_target_flock(fl->l_type); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6930 | __put_user(l_type, &target_fl->l_type); |
| 6931 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 6932 | __put_user(fl->l_start, &target_fl->l_start); |
| 6933 | __put_user(fl->l_len, &target_fl->l_len); |
| 6934 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 6935 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 6936 | return 0; |
| 6937 | } |
| 6938 | #endif |
| 6939 | |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6940 | static inline abi_long copy_from_user_flock64(struct flock *fl, |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6941 | abi_ulong target_flock_addr) |
| 6942 | { |
| 6943 | struct target_flock64 *target_fl; |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6944 | int l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6945 | |
| 6946 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 6947 | return -TARGET_EFAULT; |
| 6948 | } |
| 6949 | |
| 6950 | __get_user(l_type, &target_fl->l_type); |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6951 | l_type = target_to_host_flock(l_type); |
| 6952 | if (l_type < 0) { |
| 6953 | return l_type; |
| 6954 | } |
| 6955 | fl->l_type = l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6956 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 6957 | __get_user(fl->l_start, &target_fl->l_start); |
| 6958 | __get_user(fl->l_len, &target_fl->l_len); |
| 6959 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 6960 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 6961 | return 0; |
| 6962 | } |
| 6963 | |
| 6964 | static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr, |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6965 | const struct flock *fl) |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6966 | { |
| 6967 | struct target_flock64 *target_fl; |
| 6968 | short l_type; |
| 6969 | |
| 6970 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 6971 | return -TARGET_EFAULT; |
| 6972 | } |
| 6973 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6974 | l_type = host_to_target_flock(fl->l_type); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6975 | __put_user(l_type, &target_fl->l_type); |
| 6976 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 6977 | __put_user(fl->l_start, &target_fl->l_start); |
| 6978 | __put_user(fl->l_len, &target_fl->l_len); |
| 6979 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 6980 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 6981 | return 0; |
| 6982 | } |
| 6983 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6984 | static abi_long do_fcntl(int fd, int cmd, abi_ulong arg) |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6985 | { |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6986 | struct flock fl; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6987 | #ifdef F_GETOWN_EX |
| 6988 | struct f_owner_ex fox; |
| 6989 | struct target_f_owner_ex *target_fox; |
| 6990 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6991 | abi_long ret; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6992 | int host_cmd = target_to_host_fcntl_cmd(cmd); |
| 6993 | |
| 6994 | if (host_cmd == -TARGET_EINVAL) |
| 6995 | return host_cmd; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6996 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6997 | switch(cmd) { |
| 6998 | case TARGET_F_GETLK: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 6999 | ret = copy_from_user_flock(&fl, arg); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7000 | if (ret) { |
| 7001 | return ret; |
| 7002 | } |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7003 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7004 | if (ret == 0) { |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7005 | ret = copy_to_user_flock(arg, &fl); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7006 | } |
| 7007 | break; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7008 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7009 | case TARGET_F_SETLK: |
| 7010 | case TARGET_F_SETLKW: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7011 | ret = copy_from_user_flock(&fl, arg); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7012 | if (ret) { |
| 7013 | return ret; |
| 7014 | } |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7015 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7016 | break; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7017 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7018 | case TARGET_F_GETLK64: |
Andreas Schwab | 2d92c68 | 2020-05-25 09:59:28 +0200 | [diff] [blame] | 7019 | case TARGET_F_OFD_GETLK: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7020 | ret = copy_from_user_flock64(&fl, arg); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7021 | if (ret) { |
| 7022 | return ret; |
| 7023 | } |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7024 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl)); |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 7025 | if (ret == 0) { |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7026 | ret = copy_to_user_flock64(arg, &fl); |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 7027 | } |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 7028 | break; |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7029 | case TARGET_F_SETLK64: |
| 7030 | case TARGET_F_SETLKW64: |
Andreas Schwab | 2d92c68 | 2020-05-25 09:59:28 +0200 | [diff] [blame] | 7031 | case TARGET_F_OFD_SETLK: |
| 7032 | case TARGET_F_OFD_SETLKW: |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7033 | ret = copy_from_user_flock64(&fl, arg); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7034 | if (ret) { |
| 7035 | return ret; |
| 7036 | } |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 7037 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7038 | break; |
| 7039 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 7040 | case TARGET_F_GETFL: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7041 | ret = get_errno(safe_fcntl(fd, host_cmd, arg)); |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 7042 | if (ret >= 0) { |
| 7043 | ret = host_to_target_bitmask(ret, fcntl_flags_tbl); |
Helge Deller | e0ddf8e | 2023-07-08 07:00:25 +0200 | [diff] [blame] | 7044 | /* tell 32-bit guests it uses largefile on 64-bit hosts: */ |
| 7045 | if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) { |
| 7046 | ret |= TARGET_O_LARGEFILE; |
| 7047 | } |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 7048 | } |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 7049 | break; |
| 7050 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 7051 | case TARGET_F_SETFL: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7052 | ret = get_errno(safe_fcntl(fd, host_cmd, |
| 7053 | target_to_host_bitmask(arg, |
| 7054 | fcntl_flags_tbl))); |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 7055 | break; |
| 7056 | |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 7057 | #ifdef F_GETOWN_EX |
| 7058 | case TARGET_F_GETOWN_EX: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7059 | ret = get_errno(safe_fcntl(fd, host_cmd, &fox)); |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 7060 | if (ret >= 0) { |
| 7061 | if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0)) |
| 7062 | return -TARGET_EFAULT; |
| 7063 | target_fox->type = tswap32(fox.type); |
| 7064 | target_fox->pid = tswap32(fox.pid); |
| 7065 | unlock_user_struct(target_fox, arg, 1); |
| 7066 | } |
| 7067 | break; |
| 7068 | #endif |
| 7069 | |
| 7070 | #ifdef F_SETOWN_EX |
| 7071 | case TARGET_F_SETOWN_EX: |
| 7072 | if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1)) |
| 7073 | return -TARGET_EFAULT; |
| 7074 | fox.type = tswap32(target_fox->type); |
| 7075 | fox.pid = tswap32(target_fox->pid); |
| 7076 | unlock_user_struct(target_fox, arg, 0); |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7077 | ret = get_errno(safe_fcntl(fd, host_cmd, &fox)); |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 7078 | break; |
| 7079 | #endif |
| 7080 | |
Timothy Baldwin | a7b4c9b | 2020-07-29 21:06:44 +0100 | [diff] [blame] | 7081 | case TARGET_F_SETSIG: |
| 7082 | ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg))); |
| 7083 | break; |
| 7084 | |
| 7085 | case TARGET_F_GETSIG: |
| 7086 | ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg))); |
| 7087 | break; |
| 7088 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 7089 | case TARGET_F_SETOWN: |
| 7090 | case TARGET_F_GETOWN: |
Ulrich Hecht | 7e22e54 | 2009-07-24 19:10:27 +0200 | [diff] [blame] | 7091 | case TARGET_F_SETLEASE: |
| 7092 | case TARGET_F_GETLEASE: |
Peter Maydell | 7e3b92e | 2016-06-20 15:50:37 +0100 | [diff] [blame] | 7093 | case TARGET_F_SETPIPE_SZ: |
| 7094 | case TARGET_F_GETPIPE_SZ: |
Shu-Chun Weng | 2bb963f | 2020-12-18 11:32:10 -0800 | [diff] [blame] | 7095 | case TARGET_F_ADD_SEALS: |
| 7096 | case TARGET_F_GET_SEALS: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7097 | ret = get_errno(safe_fcntl(fd, host_cmd, arg)); |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 7098 | break; |
| 7099 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7100 | default: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 7101 | ret = get_errno(safe_fcntl(fd, cmd, arg)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7102 | break; |
| 7103 | } |
| 7104 | return ret; |
| 7105 | } |
| 7106 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 7107 | #ifdef USE_UID16 |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 7108 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 7109 | static inline int high2lowuid(int uid) |
| 7110 | { |
| 7111 | if (uid > 65535) |
| 7112 | return 65534; |
| 7113 | else |
| 7114 | return uid; |
| 7115 | } |
| 7116 | |
| 7117 | static inline int high2lowgid(int gid) |
| 7118 | { |
| 7119 | if (gid > 65535) |
| 7120 | return 65534; |
| 7121 | else |
| 7122 | return gid; |
| 7123 | } |
| 7124 | |
| 7125 | static inline int low2highuid(int uid) |
| 7126 | { |
| 7127 | if ((int16_t)uid == -1) |
| 7128 | return -1; |
| 7129 | else |
| 7130 | return uid; |
| 7131 | } |
| 7132 | |
| 7133 | static inline int low2highgid(int gid) |
| 7134 | { |
| 7135 | if ((int16_t)gid == -1) |
| 7136 | return -1; |
| 7137 | else |
| 7138 | return gid; |
| 7139 | } |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 7140 | static inline int tswapid(int id) |
| 7141 | { |
| 7142 | return tswap16(id); |
| 7143 | } |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 7144 | |
| 7145 | #define put_user_id(x, gaddr) put_user_u16(x, gaddr) |
| 7146 | |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 7147 | #else /* !USE_UID16 */ |
| 7148 | static inline int high2lowuid(int uid) |
| 7149 | { |
| 7150 | return uid; |
| 7151 | } |
| 7152 | static inline int high2lowgid(int gid) |
| 7153 | { |
| 7154 | return gid; |
| 7155 | } |
| 7156 | static inline int low2highuid(int uid) |
| 7157 | { |
| 7158 | return uid; |
| 7159 | } |
| 7160 | static inline int low2highgid(int gid) |
| 7161 | { |
| 7162 | return gid; |
| 7163 | } |
| 7164 | static inline int tswapid(int id) |
| 7165 | { |
| 7166 | return tswap32(id); |
| 7167 | } |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 7168 | |
| 7169 | #define put_user_id(x, gaddr) put_user_u32(x, gaddr) |
| 7170 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 7171 | #endif /* USE_UID16 */ |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 7172 | |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 7173 | /* We must do direct syscalls for setting UID/GID, because we want to |
| 7174 | * implement the Linux system call semantics of "change only for this thread", |
| 7175 | * not the libc/POSIX semantics of "change for all threads in process". |
| 7176 | * (See http://ewontfix.com/17/ for more details.) |
| 7177 | * We use the 32-bit version of the syscalls if present; if it is not |
| 7178 | * then either the host architecture supports 32-bit UIDs natively with |
| 7179 | * the standard syscall, or the 16-bit UID is the best we can do. |
| 7180 | */ |
| 7181 | #ifdef __NR_setuid32 |
| 7182 | #define __NR_sys_setuid __NR_setuid32 |
| 7183 | #else |
| 7184 | #define __NR_sys_setuid __NR_setuid |
| 7185 | #endif |
| 7186 | #ifdef __NR_setgid32 |
| 7187 | #define __NR_sys_setgid __NR_setgid32 |
| 7188 | #else |
| 7189 | #define __NR_sys_setgid __NR_setgid |
| 7190 | #endif |
| 7191 | #ifdef __NR_setresuid32 |
| 7192 | #define __NR_sys_setresuid __NR_setresuid32 |
| 7193 | #else |
| 7194 | #define __NR_sys_setresuid __NR_setresuid |
| 7195 | #endif |
| 7196 | #ifdef __NR_setresgid32 |
| 7197 | #define __NR_sys_setresgid __NR_setresgid32 |
| 7198 | #else |
| 7199 | #define __NR_sys_setresgid __NR_setresgid |
| 7200 | #endif |
Ilya Leoshkevich | 54b2792 | 2024-06-14 17:46:40 +0200 | [diff] [blame] | 7201 | #ifdef __NR_setgroups32 |
| 7202 | #define __NR_sys_setgroups __NR_setgroups32 |
| 7203 | #else |
| 7204 | #define __NR_sys_setgroups __NR_setgroups |
| 7205 | #endif |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 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) |
Ilya Leoshkevich | 54b2792 | 2024-06-14 17:46:40 +0200 | [diff] [blame] | 7211 | _syscall2(int, sys_setgroups, int, size, gid_t *, grouplist) |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 7212 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7213 | void syscall_init(void) |
| 7214 | { |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 7215 | IOCTLEntry *ie; |
| 7216 | const argtype *arg_type; |
| 7217 | int size; |
| 7218 | |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 7219 | thunk_init(STRUCT_MAX); |
| 7220 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 7221 | #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7222 | #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7223 | #include "syscall_types.h" |
| 7224 | #undef STRUCT |
| 7225 | #undef STRUCT_SPECIAL |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 7226 | |
| 7227 | /* we patch the ioctl size if necessary. We rely on the fact that |
| 7228 | no ioctl has all the bits at '1' in the size field */ |
| 7229 | ie = ioctl_entries; |
| 7230 | while (ie->target_cmd != 0) { |
| 7231 | if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) == |
| 7232 | TARGET_IOC_SIZEMASK) { |
| 7233 | arg_type = ie->arg_type; |
| 7234 | if (arg_type[0] != TYPE_PTR) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7235 | fprintf(stderr, "cannot patch size for ioctl 0x%x\n", |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 7236 | ie->target_cmd); |
| 7237 | exit(1); |
| 7238 | } |
| 7239 | arg_type++; |
| 7240 | size = thunk_type_size(arg_type, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7241 | ie->target_cmd = (ie->target_cmd & |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 7242 | ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) | |
| 7243 | (size << TARGET_IOC_SIZESHIFT); |
| 7244 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 7245 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 7246 | /* automatic consistency check if same arch */ |
balrog | 872ea0c | 2008-09-21 02:31:19 +0000 | [diff] [blame] | 7247 | #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \ |
| 7248 | (defined(__x86_64__) && defined(TARGET_X86_64)) |
| 7249 | if (unlikely(ie->target_cmd != ie->host_cmd)) { |
| 7250 | fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n", |
| 7251 | ie->name, ie->target_cmd, ie->host_cmd); |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 7252 | } |
| 7253 | #endif |
| 7254 | ie++; |
| 7255 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7256 | } |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 7257 | |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7258 | #ifdef TARGET_NR_truncate64 |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 7259 | static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7260 | abi_long arg2, |
| 7261 | abi_long arg3, |
| 7262 | abi_long arg4) |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7263 | { |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 7264 | if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) { |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7265 | arg2 = arg3; |
| 7266 | arg3 = arg4; |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 7267 | } |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 7268 | return get_errno(truncate(arg1, target_offset64(arg2, arg3))); |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7269 | } |
| 7270 | #endif |
| 7271 | |
| 7272 | #ifdef TARGET_NR_ftruncate64 |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 7273 | static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7274 | abi_long arg2, |
| 7275 | abi_long arg3, |
| 7276 | abi_long arg4) |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7277 | { |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 7278 | if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) { |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7279 | arg2 = arg3; |
| 7280 | arg3 = arg4; |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 7281 | } |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 7282 | return get_errno(ftruncate(arg1, target_offset64(arg2, arg3))); |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 7283 | } |
| 7284 | #endif |
| 7285 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7286 | #if defined(TARGET_NR_timer_settime) || \ |
| 7287 | (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)) |
Filip Bozuta | 2c86c90 | 2020-07-22 17:34:20 +0200 | [diff] [blame] | 7288 | static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its, |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7289 | abi_ulong target_addr) |
| 7290 | { |
Filip Bozuta | 2c86c90 | 2020-07-22 17:34:20 +0200 | [diff] [blame] | 7291 | if (target_to_host_timespec(&host_its->it_interval, target_addr + |
| 7292 | offsetof(struct target_itimerspec, |
| 7293 | it_interval)) || |
| 7294 | target_to_host_timespec(&host_its->it_value, target_addr + |
| 7295 | offsetof(struct target_itimerspec, |
| 7296 | it_value))) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7297 | return -TARGET_EFAULT; |
| 7298 | } |
| 7299 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7300 | return 0; |
| 7301 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7302 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7303 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 7304 | #if defined(TARGET_NR_timer_settime64) || \ |
| 7305 | (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) |
| 7306 | static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its, |
| 7307 | abi_ulong target_addr) |
| 7308 | { |
| 7309 | if (target_to_host_timespec64(&host_its->it_interval, target_addr + |
| 7310 | offsetof(struct target__kernel_itimerspec, |
| 7311 | it_interval)) || |
| 7312 | target_to_host_timespec64(&host_its->it_value, target_addr + |
| 7313 | offsetof(struct target__kernel_itimerspec, |
| 7314 | it_value))) { |
| 7315 | return -TARGET_EFAULT; |
| 7316 | } |
| 7317 | |
| 7318 | return 0; |
| 7319 | } |
| 7320 | #endif |
| 7321 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7322 | #if ((defined(TARGET_NR_timerfd_gettime) || \ |
| 7323 | defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \ |
Filip Bozuta | 2c86c90 | 2020-07-22 17:34:20 +0200 | [diff] [blame] | 7324 | defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime) |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7325 | static inline abi_long host_to_target_itimerspec(abi_ulong target_addr, |
Filip Bozuta | 2c86c90 | 2020-07-22 17:34:20 +0200 | [diff] [blame] | 7326 | struct itimerspec *host_its) |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7327 | { |
Filip Bozuta | 2c86c90 | 2020-07-22 17:34:20 +0200 | [diff] [blame] | 7328 | if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec, |
| 7329 | it_interval), |
| 7330 | &host_its->it_interval) || |
| 7331 | host_to_target_timespec(target_addr + offsetof(struct target_itimerspec, |
| 7332 | it_value), |
| 7333 | &host_its->it_value)) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7334 | return -TARGET_EFAULT; |
| 7335 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7336 | return 0; |
| 7337 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7338 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 7339 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 7340 | #if ((defined(TARGET_NR_timerfd_gettime64) || \ |
| 7341 | defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \ |
| 7342 | defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64) |
| 7343 | static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr, |
| 7344 | struct itimerspec *host_its) |
| 7345 | { |
| 7346 | if (host_to_target_timespec64(target_addr + |
| 7347 | offsetof(struct target__kernel_itimerspec, |
| 7348 | it_interval), |
| 7349 | &host_its->it_interval) || |
| 7350 | host_to_target_timespec64(target_addr + |
| 7351 | offsetof(struct target__kernel_itimerspec, |
| 7352 | it_value), |
| 7353 | &host_its->it_value)) { |
| 7354 | return -TARGET_EFAULT; |
| 7355 | } |
| 7356 | return 0; |
| 7357 | } |
| 7358 | #endif |
| 7359 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7360 | #if defined(TARGET_NR_adjtimex) || \ |
| 7361 | (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)) |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 7362 | static inline abi_long target_to_host_timex(struct timex *host_tx, |
| 7363 | abi_long target_addr) |
| 7364 | { |
| 7365 | struct target_timex *target_tx; |
| 7366 | |
| 7367 | if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) { |
| 7368 | return -TARGET_EFAULT; |
| 7369 | } |
| 7370 | |
| 7371 | __get_user(host_tx->modes, &target_tx->modes); |
| 7372 | __get_user(host_tx->offset, &target_tx->offset); |
| 7373 | __get_user(host_tx->freq, &target_tx->freq); |
| 7374 | __get_user(host_tx->maxerror, &target_tx->maxerror); |
| 7375 | __get_user(host_tx->esterror, &target_tx->esterror); |
| 7376 | __get_user(host_tx->status, &target_tx->status); |
| 7377 | __get_user(host_tx->constant, &target_tx->constant); |
| 7378 | __get_user(host_tx->precision, &target_tx->precision); |
| 7379 | __get_user(host_tx->tolerance, &target_tx->tolerance); |
| 7380 | __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec); |
| 7381 | __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec); |
| 7382 | __get_user(host_tx->tick, &target_tx->tick); |
| 7383 | __get_user(host_tx->ppsfreq, &target_tx->ppsfreq); |
| 7384 | __get_user(host_tx->jitter, &target_tx->jitter); |
| 7385 | __get_user(host_tx->shift, &target_tx->shift); |
| 7386 | __get_user(host_tx->stabil, &target_tx->stabil); |
| 7387 | __get_user(host_tx->jitcnt, &target_tx->jitcnt); |
| 7388 | __get_user(host_tx->calcnt, &target_tx->calcnt); |
| 7389 | __get_user(host_tx->errcnt, &target_tx->errcnt); |
| 7390 | __get_user(host_tx->stbcnt, &target_tx->stbcnt); |
| 7391 | __get_user(host_tx->tai, &target_tx->tai); |
| 7392 | |
| 7393 | unlock_user_struct(target_tx, target_addr, 0); |
| 7394 | return 0; |
| 7395 | } |
| 7396 | |
| 7397 | static inline abi_long host_to_target_timex(abi_long target_addr, |
| 7398 | struct timex *host_tx) |
| 7399 | { |
| 7400 | struct target_timex *target_tx; |
| 7401 | |
| 7402 | if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) { |
| 7403 | return -TARGET_EFAULT; |
| 7404 | } |
| 7405 | |
| 7406 | __put_user(host_tx->modes, &target_tx->modes); |
| 7407 | __put_user(host_tx->offset, &target_tx->offset); |
| 7408 | __put_user(host_tx->freq, &target_tx->freq); |
| 7409 | __put_user(host_tx->maxerror, &target_tx->maxerror); |
| 7410 | __put_user(host_tx->esterror, &target_tx->esterror); |
| 7411 | __put_user(host_tx->status, &target_tx->status); |
| 7412 | __put_user(host_tx->constant, &target_tx->constant); |
| 7413 | __put_user(host_tx->precision, &target_tx->precision); |
| 7414 | __put_user(host_tx->tolerance, &target_tx->tolerance); |
| 7415 | __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec); |
| 7416 | __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec); |
| 7417 | __put_user(host_tx->tick, &target_tx->tick); |
| 7418 | __put_user(host_tx->ppsfreq, &target_tx->ppsfreq); |
| 7419 | __put_user(host_tx->jitter, &target_tx->jitter); |
| 7420 | __put_user(host_tx->shift, &target_tx->shift); |
| 7421 | __put_user(host_tx->stabil, &target_tx->stabil); |
| 7422 | __put_user(host_tx->jitcnt, &target_tx->jitcnt); |
| 7423 | __put_user(host_tx->calcnt, &target_tx->calcnt); |
| 7424 | __put_user(host_tx->errcnt, &target_tx->errcnt); |
| 7425 | __put_user(host_tx->stbcnt, &target_tx->stbcnt); |
| 7426 | __put_user(host_tx->tai, &target_tx->tai); |
| 7427 | |
| 7428 | unlock_user_struct(target_tx, target_addr, 1); |
| 7429 | return 0; |
| 7430 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7431 | #endif |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 7432 | |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 7433 | |
| 7434 | #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME) |
| 7435 | static inline abi_long target_to_host_timex64(struct timex *host_tx, |
| 7436 | abi_long target_addr) |
| 7437 | { |
| 7438 | struct target__kernel_timex *target_tx; |
| 7439 | |
| 7440 | if (copy_from_user_timeval64(&host_tx->time, target_addr + |
| 7441 | offsetof(struct target__kernel_timex, |
| 7442 | time))) { |
| 7443 | return -TARGET_EFAULT; |
| 7444 | } |
| 7445 | |
| 7446 | if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) { |
| 7447 | return -TARGET_EFAULT; |
| 7448 | } |
| 7449 | |
| 7450 | __get_user(host_tx->modes, &target_tx->modes); |
| 7451 | __get_user(host_tx->offset, &target_tx->offset); |
| 7452 | __get_user(host_tx->freq, &target_tx->freq); |
| 7453 | __get_user(host_tx->maxerror, &target_tx->maxerror); |
| 7454 | __get_user(host_tx->esterror, &target_tx->esterror); |
| 7455 | __get_user(host_tx->status, &target_tx->status); |
| 7456 | __get_user(host_tx->constant, &target_tx->constant); |
| 7457 | __get_user(host_tx->precision, &target_tx->precision); |
| 7458 | __get_user(host_tx->tolerance, &target_tx->tolerance); |
| 7459 | __get_user(host_tx->tick, &target_tx->tick); |
| 7460 | __get_user(host_tx->ppsfreq, &target_tx->ppsfreq); |
| 7461 | __get_user(host_tx->jitter, &target_tx->jitter); |
| 7462 | __get_user(host_tx->shift, &target_tx->shift); |
| 7463 | __get_user(host_tx->stabil, &target_tx->stabil); |
| 7464 | __get_user(host_tx->jitcnt, &target_tx->jitcnt); |
| 7465 | __get_user(host_tx->calcnt, &target_tx->calcnt); |
| 7466 | __get_user(host_tx->errcnt, &target_tx->errcnt); |
| 7467 | __get_user(host_tx->stbcnt, &target_tx->stbcnt); |
| 7468 | __get_user(host_tx->tai, &target_tx->tai); |
| 7469 | |
| 7470 | unlock_user_struct(target_tx, target_addr, 0); |
| 7471 | return 0; |
| 7472 | } |
| 7473 | |
| 7474 | static inline abi_long host_to_target_timex64(abi_long target_addr, |
| 7475 | struct timex *host_tx) |
| 7476 | { |
| 7477 | struct target__kernel_timex *target_tx; |
| 7478 | |
| 7479 | if (copy_to_user_timeval64(target_addr + |
| 7480 | offsetof(struct target__kernel_timex, time), |
| 7481 | &host_tx->time)) { |
| 7482 | return -TARGET_EFAULT; |
| 7483 | } |
| 7484 | |
| 7485 | if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) { |
| 7486 | return -TARGET_EFAULT; |
| 7487 | } |
| 7488 | |
| 7489 | __put_user(host_tx->modes, &target_tx->modes); |
| 7490 | __put_user(host_tx->offset, &target_tx->offset); |
| 7491 | __put_user(host_tx->freq, &target_tx->freq); |
| 7492 | __put_user(host_tx->maxerror, &target_tx->maxerror); |
| 7493 | __put_user(host_tx->esterror, &target_tx->esterror); |
| 7494 | __put_user(host_tx->status, &target_tx->status); |
| 7495 | __put_user(host_tx->constant, &target_tx->constant); |
| 7496 | __put_user(host_tx->precision, &target_tx->precision); |
| 7497 | __put_user(host_tx->tolerance, &target_tx->tolerance); |
| 7498 | __put_user(host_tx->tick, &target_tx->tick); |
| 7499 | __put_user(host_tx->ppsfreq, &target_tx->ppsfreq); |
| 7500 | __put_user(host_tx->jitter, &target_tx->jitter); |
| 7501 | __put_user(host_tx->shift, &target_tx->shift); |
| 7502 | __put_user(host_tx->stabil, &target_tx->stabil); |
| 7503 | __put_user(host_tx->jitcnt, &target_tx->jitcnt); |
| 7504 | __put_user(host_tx->calcnt, &target_tx->calcnt); |
| 7505 | __put_user(host_tx->errcnt, &target_tx->errcnt); |
| 7506 | __put_user(host_tx->stbcnt, &target_tx->stbcnt); |
| 7507 | __put_user(host_tx->tai, &target_tx->tai); |
| 7508 | |
| 7509 | unlock_user_struct(target_tx, target_addr, 1); |
| 7510 | return 0; |
| 7511 | } |
| 7512 | #endif |
| 7513 | |
Michael Forney | 96ff758 | 2021-05-25 20:55:56 -0700 | [diff] [blame] | 7514 | #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID |
| 7515 | #define sigev_notify_thread_id _sigev_un._tid |
| 7516 | #endif |
| 7517 | |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 7518 | static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp, |
| 7519 | abi_ulong target_addr) |
| 7520 | { |
| 7521 | struct target_sigevent *target_sevp; |
| 7522 | |
| 7523 | if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) { |
| 7524 | return -TARGET_EFAULT; |
| 7525 | } |
| 7526 | |
| 7527 | /* This union is awkward on 64 bit systems because it has a 32 bit |
| 7528 | * integer and a pointer in it; we follow the conversion approach |
| 7529 | * used for handling sigval types in signal.c so the guest should get |
| 7530 | * the correct value back even if we did a 64 bit byteswap and it's |
| 7531 | * using the 32 bit integer. |
| 7532 | */ |
| 7533 | host_sevp->sigev_value.sival_ptr = |
| 7534 | (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr); |
| 7535 | host_sevp->sigev_signo = |
| 7536 | target_to_host_signal(tswap32(target_sevp->sigev_signo)); |
| 7537 | host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify); |
Michael Forney | 96ff758 | 2021-05-25 20:55:56 -0700 | [diff] [blame] | 7538 | host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid); |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 7539 | |
| 7540 | unlock_user_struct(target_sevp, target_addr, 1); |
| 7541 | return 0; |
| 7542 | } |
| 7543 | |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 7544 | #if defined(TARGET_NR_mlockall) |
| 7545 | static inline int target_to_host_mlockall_arg(int arg) |
| 7546 | { |
| 7547 | int result = 0; |
| 7548 | |
Filip Bozuta | 02e5d7d | 2020-08-11 18:45:51 +0200 | [diff] [blame] | 7549 | if (arg & TARGET_MCL_CURRENT) { |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 7550 | result |= MCL_CURRENT; |
| 7551 | } |
Filip Bozuta | 02e5d7d | 2020-08-11 18:45:51 +0200 | [diff] [blame] | 7552 | if (arg & TARGET_MCL_FUTURE) { |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 7553 | result |= MCL_FUTURE; |
| 7554 | } |
Filip Bozuta | 02e5d7d | 2020-08-11 18:45:51 +0200 | [diff] [blame] | 7555 | #ifdef MCL_ONFAULT |
| 7556 | if (arg & TARGET_MCL_ONFAULT) { |
| 7557 | result |= MCL_ONFAULT; |
| 7558 | } |
| 7559 | #endif |
| 7560 | |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 7561 | return result; |
| 7562 | } |
| 7563 | #endif |
| 7564 | |
Helge Deller | fe08059 | 2022-12-15 08:27:46 +0100 | [diff] [blame] | 7565 | static inline int target_to_host_msync_arg(abi_long arg) |
| 7566 | { |
| 7567 | return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) | |
| 7568 | ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) | |
| 7569 | ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) | |
| 7570 | (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC)); |
| 7571 | } |
| 7572 | |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 7573 | #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \ |
| 7574 | defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \ |
| 7575 | defined(TARGET_NR_newfstatat)) |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 7576 | static inline abi_long host_to_target_stat64(CPUArchState *cpu_env, |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7577 | abi_ulong target_addr, |
| 7578 | struct stat *host_st) |
| 7579 | { |
Alexander Graf | 0970119 | 2013-09-03 20:12:15 +0100 | [diff] [blame] | 7580 | #if defined(TARGET_ARM) && defined(TARGET_ABI32) |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 7581 | if (cpu_env->eabi) { |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7582 | struct target_eabi_stat64 *target_st; |
| 7583 | |
| 7584 | if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) |
| 7585 | return -TARGET_EFAULT; |
| 7586 | memset(target_st, 0, sizeof(struct target_eabi_stat64)); |
| 7587 | __put_user(host_st->st_dev, &target_st->st_dev); |
| 7588 | __put_user(host_st->st_ino, &target_st->st_ino); |
| 7589 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
| 7590 | __put_user(host_st->st_ino, &target_st->__st_ino); |
| 7591 | #endif |
| 7592 | __put_user(host_st->st_mode, &target_st->st_mode); |
| 7593 | __put_user(host_st->st_nlink, &target_st->st_nlink); |
| 7594 | __put_user(host_st->st_uid, &target_st->st_uid); |
| 7595 | __put_user(host_st->st_gid, &target_st->st_gid); |
| 7596 | __put_user(host_st->st_rdev, &target_st->st_rdev); |
| 7597 | __put_user(host_st->st_size, &target_st->st_size); |
| 7598 | __put_user(host_st->st_blksize, &target_st->st_blksize); |
| 7599 | __put_user(host_st->st_blocks, &target_st->st_blocks); |
| 7600 | __put_user(host_st->st_atime, &target_st->target_st_atime); |
| 7601 | __put_user(host_st->st_mtime, &target_st->target_st_mtime); |
| 7602 | __put_user(host_st->st_ctime, &target_st->target_st_ctime); |
Michael Forney | febf6fa | 2021-05-25 20:55:31 -0700 | [diff] [blame] | 7603 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
Chen-Yu Tsai | 5f992db | 2019-05-23 00:21:47 +0800 | [diff] [blame] | 7604 | __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec); |
| 7605 | __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec); |
| 7606 | __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec); |
| 7607 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7608 | unlock_user_struct(target_st, target_addr, 1); |
| 7609 | } else |
| 7610 | #endif |
| 7611 | { |
Stefan Weil | 20d155b | 2013-10-30 22:52:24 +0100 | [diff] [blame] | 7612 | #if defined(TARGET_HAS_STRUCT_STAT64) |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7613 | struct target_stat64 *target_st; |
Stefan Weil | 20d155b | 2013-10-30 22:52:24 +0100 | [diff] [blame] | 7614 | #else |
| 7615 | struct target_stat *target_st; |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 7616 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7617 | |
| 7618 | if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) |
| 7619 | return -TARGET_EFAULT; |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 7620 | memset(target_st, 0, sizeof(*target_st)); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7621 | __put_user(host_st->st_dev, &target_st->st_dev); |
| 7622 | __put_user(host_st->st_ino, &target_st->st_ino); |
| 7623 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
| 7624 | __put_user(host_st->st_ino, &target_st->__st_ino); |
| 7625 | #endif |
| 7626 | __put_user(host_st->st_mode, &target_st->st_mode); |
| 7627 | __put_user(host_st->st_nlink, &target_st->st_nlink); |
| 7628 | __put_user(host_st->st_uid, &target_st->st_uid); |
| 7629 | __put_user(host_st->st_gid, &target_st->st_gid); |
| 7630 | __put_user(host_st->st_rdev, &target_st->st_rdev); |
| 7631 | /* XXX: better use of kernel struct */ |
| 7632 | __put_user(host_st->st_size, &target_st->st_size); |
| 7633 | __put_user(host_st->st_blksize, &target_st->st_blksize); |
| 7634 | __put_user(host_st->st_blocks, &target_st->st_blocks); |
| 7635 | __put_user(host_st->st_atime, &target_st->target_st_atime); |
| 7636 | __put_user(host_st->st_mtime, &target_st->target_st_mtime); |
| 7637 | __put_user(host_st->st_ctime, &target_st->target_st_ctime); |
Michael Forney | febf6fa | 2021-05-25 20:55:31 -0700 | [diff] [blame] | 7638 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
Chen-Yu Tsai | 5f992db | 2019-05-23 00:21:47 +0800 | [diff] [blame] | 7639 | __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec); |
| 7640 | __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec); |
| 7641 | __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec); |
| 7642 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7643 | unlock_user_struct(target_st, target_addr, 1); |
| 7644 | } |
| 7645 | |
| 7646 | return 0; |
| 7647 | } |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 7648 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7649 | |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 7650 | #if defined(TARGET_NR_statx) && defined(__NR_statx) |
| 7651 | static inline abi_long host_to_target_statx(struct target_statx *host_stx, |
| 7652 | abi_ulong target_addr) |
| 7653 | { |
| 7654 | struct target_statx *target_stx; |
| 7655 | |
| 7656 | if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) { |
| 7657 | return -TARGET_EFAULT; |
| 7658 | } |
| 7659 | memset(target_stx, 0, sizeof(*target_stx)); |
| 7660 | |
| 7661 | __put_user(host_stx->stx_mask, &target_stx->stx_mask); |
| 7662 | __put_user(host_stx->stx_blksize, &target_stx->stx_blksize); |
| 7663 | __put_user(host_stx->stx_attributes, &target_stx->stx_attributes); |
| 7664 | __put_user(host_stx->stx_nlink, &target_stx->stx_nlink); |
| 7665 | __put_user(host_stx->stx_uid, &target_stx->stx_uid); |
| 7666 | __put_user(host_stx->stx_gid, &target_stx->stx_gid); |
| 7667 | __put_user(host_stx->stx_mode, &target_stx->stx_mode); |
| 7668 | __put_user(host_stx->stx_ino, &target_stx->stx_ino); |
| 7669 | __put_user(host_stx->stx_size, &target_stx->stx_size); |
| 7670 | __put_user(host_stx->stx_blocks, &target_stx->stx_blocks); |
| 7671 | __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask); |
| 7672 | __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec); |
| 7673 | __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec); |
Ariadne Conill | d1e2670 | 2019-11-22 11:40:40 -0600 | [diff] [blame] | 7674 | __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec); |
| 7675 | __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec); |
| 7676 | __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec); |
| 7677 | __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec); |
| 7678 | __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec); |
| 7679 | __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec); |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 7680 | __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major); |
| 7681 | __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor); |
| 7682 | __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major); |
| 7683 | __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor); |
| 7684 | |
| 7685 | unlock_user_struct(target_stx, target_addr, 1); |
| 7686 | |
| 7687 | return 0; |
| 7688 | } |
| 7689 | #endif |
| 7690 | |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7691 | static int do_sys_futex(int *uaddr, int op, int val, |
| 7692 | const struct timespec *timeout, int *uaddr2, |
| 7693 | int val3) |
| 7694 | { |
| 7695 | #if HOST_LONG_BITS == 64 |
| 7696 | #if defined(__NR_futex) |
| 7697 | /* always a 64-bit time_t, it doesn't define _time64 version */ |
| 7698 | return sys_futex(uaddr, op, val, timeout, uaddr2, val3); |
| 7699 | |
| 7700 | #endif |
| 7701 | #else /* HOST_LONG_BITS == 64 */ |
| 7702 | #if defined(__NR_futex_time64) |
| 7703 | if (sizeof(timeout->tv_sec) == 8) { |
| 7704 | /* _time64 function on 32bit arch */ |
| 7705 | return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3); |
| 7706 | } |
| 7707 | #endif |
| 7708 | #if defined(__NR_futex) |
| 7709 | /* old function on 32bit arch */ |
| 7710 | return sys_futex(uaddr, op, val, timeout, uaddr2, val3); |
| 7711 | #endif |
| 7712 | #endif /* HOST_LONG_BITS == 64 */ |
| 7713 | g_assert_not_reached(); |
| 7714 | } |
| 7715 | |
| 7716 | static int do_safe_futex(int *uaddr, int op, int val, |
| 7717 | const struct timespec *timeout, int *uaddr2, |
| 7718 | int val3) |
| 7719 | { |
| 7720 | #if HOST_LONG_BITS == 64 |
| 7721 | #if defined(__NR_futex) |
| 7722 | /* always a 64-bit time_t, it doesn't define _time64 version */ |
| 7723 | return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3)); |
| 7724 | #endif |
| 7725 | #else /* HOST_LONG_BITS == 64 */ |
| 7726 | #if defined(__NR_futex_time64) |
| 7727 | if (sizeof(timeout->tv_sec) == 8) { |
| 7728 | /* _time64 function on 32bit arch */ |
| 7729 | return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2, |
| 7730 | val3)); |
| 7731 | } |
| 7732 | #endif |
| 7733 | #if defined(__NR_futex) |
| 7734 | /* old function on 32bit arch */ |
| 7735 | return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3)); |
| 7736 | #endif |
| 7737 | #endif /* HOST_LONG_BITS == 64 */ |
| 7738 | return -TARGET_ENOSYS; |
| 7739 | } |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 7740 | |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7741 | /* ??? Using host futex calls even when target atomic operations |
| 7742 | are not really atomic probably breaks things. However implementing |
| 7743 | futexes locally would make futexes shared between multiple processes |
| 7744 | tricky. However they're probably useless because guest atomic |
| 7745 | operations won't work either. */ |
Richard Henderson | 0fbc0f8 | 2022-08-28 19:09:59 -0700 | [diff] [blame] | 7746 | #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64) |
| 7747 | static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr, |
| 7748 | int op, int val, target_ulong timeout, |
| 7749 | target_ulong uaddr2, int val3) |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7750 | { |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7751 | struct timespec ts, *pts = NULL; |
| 7752 | void *haddr2 = NULL; |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7753 | int base_op; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7754 | |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7755 | /* We assume FUTEX_* constants are the same on both host and target. */ |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 7756 | #ifdef FUTEX_CMD_MASK |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7757 | base_op = op & FUTEX_CMD_MASK; |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 7758 | #else |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7759 | base_op = op; |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 7760 | #endif |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7761 | switch (base_op) { |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7762 | case FUTEX_WAIT: |
John Rigby | cce246e | 2013-02-23 16:14:07 -0700 | [diff] [blame] | 7763 | case FUTEX_WAIT_BITSET: |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7764 | val = tswap32(val); |
| 7765 | break; |
Richard Henderson | c72a90d | 2022-08-28 19:10:03 -0700 | [diff] [blame] | 7766 | case FUTEX_WAIT_REQUEUE_PI: |
| 7767 | val = tswap32(val); |
| 7768 | haddr2 = g2h(cpu, uaddr2); |
| 7769 | break; |
| 7770 | case FUTEX_LOCK_PI: |
| 7771 | case FUTEX_LOCK_PI2: |
| 7772 | break; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7773 | case FUTEX_WAKE: |
Richard Henderson | a6180f8 | 2022-08-28 19:10:01 -0700 | [diff] [blame] | 7774 | case FUTEX_WAKE_BITSET: |
Richard Henderson | c72a90d | 2022-08-28 19:10:03 -0700 | [diff] [blame] | 7775 | case FUTEX_TRYLOCK_PI: |
| 7776 | case FUTEX_UNLOCK_PI: |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7777 | timeout = 0; |
| 7778 | break; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7779 | case FUTEX_FD: |
Richard Henderson | 0f94673 | 2022-08-28 19:10:02 -0700 | [diff] [blame] | 7780 | val = target_to_host_signal(val); |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7781 | timeout = 0; |
| 7782 | break; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7783 | case FUTEX_CMP_REQUEUE: |
Richard Henderson | c72a90d | 2022-08-28 19:10:03 -0700 | [diff] [blame] | 7784 | case FUTEX_CMP_REQUEUE_PI: |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7785 | val3 = tswap32(val3); |
| 7786 | /* fall through */ |
| 7787 | case FUTEX_REQUEUE: |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7788 | case FUTEX_WAKE_OP: |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7789 | /* |
| 7790 | * For these, the 4th argument is not TIMEOUT, but VAL2. |
| 7791 | * But the prototype of do_safe_futex takes a pointer, so |
| 7792 | * insert casts to satisfy the compiler. We do not need |
| 7793 | * to tswap VAL2 since it's not compared to guest memory. |
| 7794 | */ |
| 7795 | pts = (struct timespec *)(uintptr_t)timeout; |
| 7796 | timeout = 0; |
| 7797 | haddr2 = g2h(cpu, uaddr2); |
| 7798 | break; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7799 | default: |
| 7800 | return -TARGET_ENOSYS; |
| 7801 | } |
Richard Henderson | 57b9ccd | 2022-08-28 19:10:00 -0700 | [diff] [blame] | 7802 | if (timeout) { |
| 7803 | pts = &ts; |
| 7804 | if (time64 |
| 7805 | ? target_to_host_timespec64(pts, timeout) |
| 7806 | : target_to_host_timespec(pts, timeout)) { |
| 7807 | return -TARGET_EFAULT; |
| 7808 | } |
| 7809 | } |
| 7810 | return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7811 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7812 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7813 | |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7814 | #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 7815 | static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname, |
| 7816 | abi_long handle, abi_long mount_id, |
| 7817 | abi_long flags) |
| 7818 | { |
| 7819 | struct file_handle *target_fh; |
| 7820 | struct file_handle *fh; |
| 7821 | int mid = 0; |
| 7822 | abi_long ret; |
| 7823 | char *name; |
| 7824 | unsigned int size, total_size; |
| 7825 | |
| 7826 | if (get_user_s32(size, handle)) { |
| 7827 | return -TARGET_EFAULT; |
| 7828 | } |
| 7829 | |
| 7830 | name = lock_user_string(pathname); |
| 7831 | if (!name) { |
| 7832 | return -TARGET_EFAULT; |
| 7833 | } |
| 7834 | |
| 7835 | total_size = sizeof(struct file_handle) + size; |
| 7836 | target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0); |
| 7837 | if (!target_fh) { |
| 7838 | unlock_user(name, pathname, 0); |
| 7839 | return -TARGET_EFAULT; |
| 7840 | } |
| 7841 | |
| 7842 | fh = g_malloc0(total_size); |
| 7843 | fh->handle_bytes = size; |
| 7844 | |
| 7845 | ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags)); |
| 7846 | unlock_user(name, pathname, 0); |
| 7847 | |
| 7848 | /* man name_to_handle_at(2): |
| 7849 | * Other than the use of the handle_bytes field, the caller should treat |
| 7850 | * the file_handle structure as an opaque data type |
| 7851 | */ |
| 7852 | |
| 7853 | memcpy(target_fh, fh, total_size); |
| 7854 | target_fh->handle_bytes = tswap32(fh->handle_bytes); |
| 7855 | target_fh->handle_type = tswap32(fh->handle_type); |
| 7856 | g_free(fh); |
| 7857 | unlock_user(target_fh, handle, total_size); |
| 7858 | |
| 7859 | if (put_user_s32(mid, mount_id)) { |
| 7860 | return -TARGET_EFAULT; |
| 7861 | } |
| 7862 | |
| 7863 | return ret; |
| 7864 | |
| 7865 | } |
| 7866 | #endif |
| 7867 | |
| 7868 | #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 7869 | static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle, |
| 7870 | abi_long flags) |
| 7871 | { |
| 7872 | struct file_handle *target_fh; |
| 7873 | struct file_handle *fh; |
| 7874 | unsigned int size, total_size; |
| 7875 | abi_long ret; |
| 7876 | |
| 7877 | if (get_user_s32(size, handle)) { |
| 7878 | return -TARGET_EFAULT; |
| 7879 | } |
| 7880 | |
| 7881 | total_size = sizeof(struct file_handle) + size; |
| 7882 | target_fh = lock_user(VERIFY_READ, handle, total_size, 1); |
| 7883 | if (!target_fh) { |
| 7884 | return -TARGET_EFAULT; |
| 7885 | } |
| 7886 | |
Thomas Huth | e9d49d5 | 2015-10-09 17:56:38 +0200 | [diff] [blame] | 7887 | fh = g_memdup(target_fh, total_size); |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7888 | fh->handle_bytes = size; |
| 7889 | fh->handle_type = tswap32(target_fh->handle_type); |
| 7890 | |
| 7891 | ret = get_errno(open_by_handle_at(mount_fd, fh, |
| 7892 | target_to_host_bitmask(flags, fcntl_flags_tbl))); |
| 7893 | |
| 7894 | g_free(fh); |
| 7895 | |
| 7896 | unlock_user(target_fh, handle, total_size); |
| 7897 | |
| 7898 | return ret; |
| 7899 | } |
| 7900 | #endif |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7901 | |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7902 | #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4) |
| 7903 | |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7904 | static abi_long do_signalfd4(int fd, abi_long mask, int flags) |
| 7905 | { |
| 7906 | int host_flags; |
| 7907 | target_sigset_t *target_mask; |
| 7908 | sigset_t host_mask; |
| 7909 | abi_long ret; |
| 7910 | |
Helge Deller | 7872130 | 2021-02-10 07:12:14 +0100 | [diff] [blame] | 7911 | if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) { |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7912 | return -TARGET_EINVAL; |
| 7913 | } |
| 7914 | if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) { |
| 7915 | return -TARGET_EFAULT; |
| 7916 | } |
| 7917 | |
| 7918 | target_to_host_sigset(&host_mask, target_mask); |
| 7919 | |
| 7920 | host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); |
| 7921 | |
| 7922 | ret = get_errno(signalfd(fd, &host_mask, host_flags)); |
| 7923 | if (ret >= 0) { |
| 7924 | fd_trans_register(ret, &target_signalfd_trans); |
| 7925 | } |
| 7926 | |
| 7927 | unlock_user_struct(target_mask, mask, 0); |
| 7928 | |
| 7929 | return ret; |
| 7930 | } |
| 7931 | #endif |
| 7932 | |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 7933 | /* Map host to target signal numbers for the wait family of syscalls. |
| 7934 | Assume all other status bits are the same. */ |
Richard Henderson | a05c640 | 2012-09-15 11:34:20 -0700 | [diff] [blame] | 7935 | int host_to_target_waitstatus(int status) |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 7936 | { |
| 7937 | if (WIFSIGNALED(status)) { |
| 7938 | return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f); |
| 7939 | } |
| 7940 | if (WIFSTOPPED(status)) { |
| 7941 | return (host_to_target_signal(WSTOPSIG(status)) << 8) |
| 7942 | | (status & 0xff); |
| 7943 | } |
| 7944 | return status; |
| 7945 | } |
| 7946 | |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 7947 | static int open_self_cmdline(CPUArchState *cpu_env, int fd) |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7948 | { |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 7949 | CPUState *cpu = env_cpu(cpu_env); |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 7950 | struct linux_binprm *bprm = get_task_state(cpu)->bprm; |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7951 | int i; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7952 | |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7953 | for (i = 0; i < bprm->argc; i++) { |
| 7954 | size_t len = strlen(bprm->argv[i]) + 1; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7955 | |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7956 | if (write(fd, bprm->argv[i], len) != len) { |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7957 | return -1; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7958 | } |
| 7959 | } |
| 7960 | |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7961 | return 0; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7962 | } |
| 7963 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 7964 | struct open_self_maps_data { |
| 7965 | TaskState *ts; |
| 7966 | IntervalTreeRoot *host_maps; |
| 7967 | int fd; |
| 7968 | bool smaps; |
| 7969 | }; |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 7970 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 7971 | /* |
| 7972 | * Subroutine to output one line of /proc/self/maps, |
| 7973 | * or one region of /proc/self/smaps. |
| 7974 | */ |
| 7975 | |
| 7976 | #ifdef TARGET_HPPA |
| 7977 | # define test_stack(S, E, L) (E == L) |
| 7978 | #else |
| 7979 | # define test_stack(S, E, L) (S == L) |
| 7980 | #endif |
| 7981 | |
| 7982 | static void open_self_maps_4(const struct open_self_maps_data *d, |
| 7983 | const MapInfo *mi, abi_ptr start, |
| 7984 | abi_ptr end, unsigned flags) |
| 7985 | { |
| 7986 | const struct image_info *info = d->ts->info; |
| 7987 | const char *path = mi->path; |
| 7988 | uint64_t offset; |
| 7989 | int fd = d->fd; |
| 7990 | int count; |
| 7991 | |
| 7992 | if (test_stack(start, end, info->stack_limit)) { |
| 7993 | path = "[stack]"; |
Richard Henderson | 92d2a03 | 2023-08-16 10:33:28 -0700 | [diff] [blame] | 7994 | } else if (start == info->brk) { |
| 7995 | path = "[heap]"; |
Richard Henderson | 5d94c2f | 2023-08-16 10:54:57 -0700 | [diff] [blame] | 7996 | } else if (start == info->vdso) { |
| 7997 | path = "[vdso]"; |
Richard Henderson | 4ef1f55 | 2024-02-24 02:29:41 +0000 | [diff] [blame] | 7998 | #ifdef TARGET_X86_64 |
| 7999 | } else if (start == TARGET_VSYSCALL_PAGE) { |
| 8000 | path = "[vsyscall]"; |
| 8001 | #endif |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8002 | } |
| 8003 | |
| 8004 | /* Except null device (MAP_ANON), adjust offset for this fragment. */ |
| 8005 | offset = mi->offset; |
| 8006 | if (mi->dev) { |
| 8007 | uintptr_t hstart = (uintptr_t)g2h_untagged(start); |
| 8008 | offset += hstart - mi->itree.start; |
| 8009 | } |
| 8010 | |
| 8011 | count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr |
| 8012 | " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64, |
| 8013 | start, end, |
| 8014 | (flags & PAGE_READ) ? 'r' : '-', |
| 8015 | (flags & PAGE_WRITE_ORG) ? 'w' : '-', |
| 8016 | (flags & PAGE_EXEC) ? 'x' : '-', |
| 8017 | mi->is_priv ? 'p' : 's', |
| 8018 | offset, major(mi->dev), minor(mi->dev), |
| 8019 | (uint64_t)mi->inode); |
| 8020 | if (path) { |
| 8021 | dprintf(fd, "%*s%s\n", 73 - count, "", path); |
| 8022 | } else { |
| 8023 | dprintf(fd, "\n"); |
| 8024 | } |
| 8025 | |
| 8026 | if (d->smaps) { |
| 8027 | unsigned long size = end - start; |
| 8028 | unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10; |
| 8029 | unsigned long size_kb = size >> 10; |
| 8030 | |
| 8031 | dprintf(fd, "Size: %lu kB\n" |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 8032 | "KernelPageSize: %lu kB\n" |
| 8033 | "MMUPageSize: %lu kB\n" |
| 8034 | "Rss: 0 kB\n" |
| 8035 | "Pss: 0 kB\n" |
| 8036 | "Pss_Dirty: 0 kB\n" |
| 8037 | "Shared_Clean: 0 kB\n" |
| 8038 | "Shared_Dirty: 0 kB\n" |
| 8039 | "Private_Clean: 0 kB\n" |
| 8040 | "Private_Dirty: 0 kB\n" |
| 8041 | "Referenced: 0 kB\n" |
Ilya Leoshkevich | 6467d9e | 2023-08-23 17:55:33 -0700 | [diff] [blame] | 8042 | "Anonymous: %lu kB\n" |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 8043 | "LazyFree: 0 kB\n" |
| 8044 | "AnonHugePages: 0 kB\n" |
| 8045 | "ShmemPmdMapped: 0 kB\n" |
| 8046 | "FilePmdMapped: 0 kB\n" |
| 8047 | "Shared_Hugetlb: 0 kB\n" |
| 8048 | "Private_Hugetlb: 0 kB\n" |
| 8049 | "Swap: 0 kB\n" |
| 8050 | "SwapPss: 0 kB\n" |
| 8051 | "Locked: 0 kB\n" |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8052 | "THPeligible: 0\n" |
| 8053 | "VmFlags:%s%s%s%s%s%s%s%s\n", |
| 8054 | size_kb, page_size_kb, page_size_kb, |
Ilya Leoshkevich | 6467d9e | 2023-08-23 17:55:33 -0700 | [diff] [blame] | 8055 | (flags & PAGE_ANON ? size_kb : 0), |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8056 | (flags & PAGE_READ) ? " rd" : "", |
| 8057 | (flags & PAGE_WRITE_ORG) ? " wr" : "", |
| 8058 | (flags & PAGE_EXEC) ? " ex" : "", |
| 8059 | mi->is_priv ? "" : " sh", |
| 8060 | (flags & PAGE_READ) ? " mr" : "", |
| 8061 | (flags & PAGE_WRITE_ORG) ? " mw" : "", |
| 8062 | (flags & PAGE_EXEC) ? " me" : "", |
| 8063 | mi->is_priv ? "" : " ms"); |
| 8064 | } |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 8065 | } |
| 8066 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8067 | /* |
| 8068 | * Callback for walk_memory_regions, when read_self_maps() fails. |
| 8069 | * Proceed without the benefit of host /proc/self/maps cross-check. |
| 8070 | */ |
| 8071 | static int open_self_maps_3(void *opaque, target_ulong guest_start, |
| 8072 | target_ulong guest_end, unsigned long flags) |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 8073 | { |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8074 | static const MapInfo mi = { .is_priv = true }; |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 8075 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8076 | open_self_maps_4(opaque, &mi, guest_start, guest_end, flags); |
| 8077 | return 0; |
| 8078 | } |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 8079 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8080 | /* |
| 8081 | * Callback for walk_memory_regions, when read_self_maps() succeeds. |
| 8082 | */ |
| 8083 | static int open_self_maps_2(void *opaque, target_ulong guest_start, |
| 8084 | target_ulong guest_end, unsigned long flags) |
| 8085 | { |
| 8086 | const struct open_self_maps_data *d = opaque; |
| 8087 | uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start); |
| 8088 | uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1); |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 8089 | |
Richard Henderson | 4ef1f55 | 2024-02-24 02:29:41 +0000 | [diff] [blame] | 8090 | #ifdef TARGET_X86_64 |
| 8091 | /* |
| 8092 | * Because of the extremely high position of the page within the guest |
| 8093 | * virtual address space, this is not backed by host memory at all. |
| 8094 | * Therefore the loop below would fail. This is the only instance |
| 8095 | * of not having host backing memory. |
| 8096 | */ |
| 8097 | if (guest_start == TARGET_VSYSCALL_PAGE) { |
| 8098 | return open_self_maps_3(opaque, guest_start, guest_end, flags); |
| 8099 | } |
| 8100 | #endif |
| 8101 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8102 | while (1) { |
| 8103 | IntervalTreeNode *n = |
| 8104 | interval_tree_iter_first(d->host_maps, host_start, host_start); |
| 8105 | MapInfo *mi = container_of(n, MapInfo, itree); |
| 8106 | uintptr_t this_hlast = MIN(host_last, n->last); |
| 8107 | target_ulong this_gend = h2g(this_hlast) + 1; |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 8108 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8109 | open_self_maps_4(d, mi, guest_start, this_gend, flags); |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 8110 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8111 | if (this_hlast == host_last) { |
| 8112 | return 0; |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 8113 | } |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8114 | host_start = this_hlast + 1; |
| 8115 | guest_start = h2g(host_start); |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 8116 | } |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8117 | } |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 8118 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8119 | static int open_self_maps_1(CPUArchState *env, int fd, bool smaps) |
| 8120 | { |
| 8121 | struct open_self_maps_data d = { |
Philippe Mathieu-Daudé | 5927246 | 2024-04-25 11:12:19 +0200 | [diff] [blame] | 8122 | .ts = get_task_state(env_cpu(env)), |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8123 | .host_maps = read_self_maps(), |
| 8124 | .fd = fd, |
| 8125 | .smaps = smaps |
| 8126 | }; |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 8127 | |
Richard Henderson | 7b7a336 | 2023-08-08 20:02:19 -0700 | [diff] [blame] | 8128 | if (d.host_maps) { |
| 8129 | walk_memory_regions(&d, open_self_maps_2); |
| 8130 | free_self_maps(d.host_maps); |
| 8131 | } else { |
| 8132 | walk_memory_regions(&d, open_self_maps_3); |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 8133 | } |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 8134 | return 0; |
| 8135 | } |
| 8136 | |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 8137 | static int open_self_maps(CPUArchState *cpu_env, int fd) |
| 8138 | { |
| 8139 | return open_self_maps_1(cpu_env, fd, false); |
| 8140 | } |
| 8141 | |
| 8142 | static int open_self_smaps(CPUArchState *cpu_env, int fd) |
| 8143 | { |
| 8144 | return open_self_maps_1(cpu_env, fd, true); |
| 8145 | } |
| 8146 | |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 8147 | static int open_self_stat(CPUArchState *cpu_env, int fd) |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 8148 | { |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 8149 | CPUState *cpu = env_cpu(cpu_env); |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 8150 | TaskState *ts = get_task_state(cpu); |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 8151 | g_autoptr(GString) buf = g_string_new(NULL); |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 8152 | int i; |
| 8153 | |
| 8154 | for (i = 0; i < 44; i++) { |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 8155 | if (i == 0) { |
| 8156 | /* pid */ |
| 8157 | g_string_printf(buf, FMT_pid " ", getpid()); |
| 8158 | } else if (i == 1) { |
| 8159 | /* app name */ |
| 8160 | gchar *bin = g_strrstr(ts->bprm->argv[0], "/"); |
| 8161 | bin = bin ? bin + 1 : ts->bprm->argv[0]; |
| 8162 | g_string_printf(buf, "(%.15s) ", bin); |
Andreas Schwab | 25bb27c | 2023-03-06 10:59:29 +0100 | [diff] [blame] | 8163 | } else if (i == 2) { |
| 8164 | /* task state */ |
| 8165 | g_string_assign(buf, "R "); /* we are running right now */ |
Andreas Schwab | 7aa9fe3 | 2021-06-21 11:32:59 +0200 | [diff] [blame] | 8166 | } else if (i == 3) { |
| 8167 | /* ppid */ |
| 8168 | g_string_printf(buf, FMT_pid " ", getppid()); |
Fabio D'Urso | d9b019e | 2024-06-19 21:41:09 +0200 | [diff] [blame] | 8169 | } else if (i == 19) { |
| 8170 | /* num_threads */ |
| 8171 | int cpus = 0; |
| 8172 | WITH_RCU_READ_LOCK_GUARD() { |
| 8173 | CPUState *cpu_iter; |
| 8174 | CPU_FOREACH(cpu_iter) { |
| 8175 | cpus++; |
| 8176 | } |
| 8177 | } |
| 8178 | g_string_printf(buf, "%d ", cpus); |
Cameron Esfahani | eb33cda | 2022-01-27 16:12:51 -0800 | [diff] [blame] | 8179 | } else if (i == 21) { |
| 8180 | /* starttime */ |
| 8181 | g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime); |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 8182 | } else if (i == 27) { |
| 8183 | /* stack bottom */ |
| 8184 | g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack); |
| 8185 | } else { |
| 8186 | /* for the rest, there is MasterCard */ |
| 8187 | g_string_printf(buf, "0%c", i == 43 ? '\n' : ' '); |
| 8188 | } |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 8189 | |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 8190 | if (write(fd, buf->str, buf->len) != buf->len) { |
| 8191 | return -1; |
| 8192 | } |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 8193 | } |
| 8194 | |
| 8195 | return 0; |
| 8196 | } |
| 8197 | |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 8198 | static int open_self_auxv(CPUArchState *cpu_env, int fd) |
Alexander Graf | 257450e | 2011-11-02 20:23:26 +0100 | [diff] [blame] | 8199 | { |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 8200 | CPUState *cpu = env_cpu(cpu_env); |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 8201 | TaskState *ts = get_task_state(cpu); |
Alexander Graf | 257450e | 2011-11-02 20:23:26 +0100 | [diff] [blame] | 8202 | abi_ulong auxv = ts->info->saved_auxv; |
| 8203 | abi_ulong len = ts->info->auxv_len; |
| 8204 | char *ptr; |
| 8205 | |
| 8206 | /* |
| 8207 | * Auxiliary vector is stored in target process stack. |
| 8208 | * read in whole auxv vector and copy it to file |
| 8209 | */ |
| 8210 | ptr = lock_user(VERIFY_READ, auxv, len, 0); |
| 8211 | if (ptr != NULL) { |
| 8212 | while (len > 0) { |
| 8213 | ssize_t r; |
| 8214 | r = write(fd, ptr, len); |
| 8215 | if (r <= 0) { |
| 8216 | break; |
| 8217 | } |
| 8218 | len -= r; |
| 8219 | ptr += r; |
| 8220 | } |
| 8221 | lseek(fd, 0, SEEK_SET); |
| 8222 | unlock_user(ptr, auxv, len); |
| 8223 | } |
| 8224 | |
| 8225 | return 0; |
| 8226 | } |
| 8227 | |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8228 | static int is_proc_myself(const char *filename, const char *entry) |
| 8229 | { |
| 8230 | if (!strncmp(filename, "/proc/", strlen("/proc/"))) { |
| 8231 | filename += strlen("/proc/"); |
| 8232 | if (!strncmp(filename, "self/", strlen("self/"))) { |
| 8233 | filename += strlen("self/"); |
| 8234 | } else if (*filename >= '1' && *filename <= '9') { |
| 8235 | char myself[80]; |
| 8236 | snprintf(myself, sizeof(myself), "%d/", getpid()); |
| 8237 | if (!strncmp(filename, myself, strlen(myself))) { |
| 8238 | filename += strlen(myself); |
| 8239 | } else { |
| 8240 | return 0; |
| 8241 | } |
| 8242 | } else { |
| 8243 | return 0; |
| 8244 | } |
| 8245 | if (!strcmp(filename, entry)) { |
| 8246 | return 1; |
| 8247 | } |
| 8248 | } |
| 8249 | return 0; |
| 8250 | } |
| 8251 | |
Helge Deller | bd5ccd6 | 2022-10-24 22:18:09 +0200 | [diff] [blame] | 8252 | static void excp_dump_file(FILE *logfile, CPUArchState *env, |
| 8253 | const char *fmt, int code) |
| 8254 | { |
| 8255 | if (logfile) { |
| 8256 | CPUState *cs = env_cpu(env); |
| 8257 | |
| 8258 | fprintf(logfile, fmt, code); |
| 8259 | fprintf(logfile, "Failing executable: %s\n", exec_path); |
| 8260 | cpu_dump_state(cs, logfile, 0); |
| 8261 | open_self_maps(env, fileno(logfile)); |
| 8262 | } |
| 8263 | } |
| 8264 | |
| 8265 | void target_exception_dump(CPUArchState *env, const char *fmt, int code) |
| 8266 | { |
| 8267 | /* dump to console */ |
| 8268 | excp_dump_file(stderr, env, fmt, code); |
| 8269 | |
| 8270 | /* dump to log file */ |
| 8271 | if (qemu_log_separate()) { |
| 8272 | FILE *logfile = qemu_log_trylock(); |
| 8273 | |
| 8274 | excp_dump_file(logfile, env, fmt, code); |
| 8275 | qemu_log_unlock(logfile); |
| 8276 | } |
| 8277 | } |
| 8278 | |
Richard Henderson | 121c8dd | 2023-08-23 17:13:14 -0700 | [diff] [blame] | 8279 | #include "target_proc.h" |
| 8280 | |
Marc-André Lureau | ee3eb3a | 2022-03-23 19:57:18 +0400 | [diff] [blame] | 8281 | #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \ |
Richard Henderson | 121c8dd | 2023-08-23 17:13:14 -0700 | [diff] [blame] | 8282 | defined(HAVE_ARCH_PROC_CPUINFO) || \ |
| 8283 | defined(HAVE_ARCH_PROC_HARDWARE) |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8284 | static int is_proc(const char *filename, const char *entry) |
| 8285 | { |
| 8286 | return strcmp(filename, entry) == 0; |
| 8287 | } |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 8288 | #endif |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8289 | |
Marc-André Lureau | ee3eb3a | 2022-03-23 19:57:18 +0400 | [diff] [blame] | 8290 | #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 8291 | static int open_net_route(CPUArchState *cpu_env, int fd) |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8292 | { |
| 8293 | FILE *fp; |
| 8294 | char *line = NULL; |
| 8295 | size_t len = 0; |
| 8296 | ssize_t read; |
| 8297 | |
| 8298 | fp = fopen("/proc/net/route", "r"); |
| 8299 | if (fp == NULL) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 8300 | return -1; |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8301 | } |
| 8302 | |
| 8303 | /* read header */ |
| 8304 | |
| 8305 | read = getline(&line, &len, fp); |
| 8306 | dprintf(fd, "%s", line); |
| 8307 | |
| 8308 | /* read routes */ |
| 8309 | |
| 8310 | while ((read = getline(&line, &len, fp)) != -1) { |
| 8311 | char iface[16]; |
| 8312 | uint32_t dest, gw, mask; |
| 8313 | unsigned int flags, refcnt, use, metric, mtu, window, irtt; |
Peter Maydell | 9d0bd0c | 2019-02-05 17:42:07 +0000 | [diff] [blame] | 8314 | int fields; |
| 8315 | |
| 8316 | fields = sscanf(line, |
| 8317 | "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", |
| 8318 | iface, &dest, &gw, &flags, &refcnt, &use, &metric, |
| 8319 | &mask, &mtu, &window, &irtt); |
| 8320 | if (fields != 11) { |
| 8321 | continue; |
| 8322 | } |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8323 | dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", |
| 8324 | iface, tswap32(dest), tswap32(gw), flags, refcnt, use, |
| 8325 | metric, tswap32(mask), mtu, window, irtt); |
| 8326 | } |
| 8327 | |
| 8328 | free(line); |
| 8329 | fclose(fp); |
| 8330 | |
| 8331 | return 0; |
| 8332 | } |
| 8333 | #endif |
| 8334 | |
Helge Deller | b800205 | 2023-08-03 23:44:47 +0200 | [diff] [blame] | 8335 | int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname, |
Ilya Leoshkevich | 35be898 | 2023-06-30 19:04:17 +0100 | [diff] [blame] | 8336 | int flags, mode_t mode, bool safe) |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8337 | { |
Helge Deller | b800205 | 2023-08-03 23:44:47 +0200 | [diff] [blame] | 8338 | g_autofree char *proc_name = NULL; |
| 8339 | const char *pathname; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8340 | struct fake_open { |
| 8341 | const char *filename; |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 8342 | int (*fill)(CPUArchState *cpu_env, int fd); |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8343 | int (*cmp)(const char *s1, const char *s2); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8344 | }; |
| 8345 | const struct fake_open *fake_open; |
| 8346 | static const struct fake_open fakes[] = { |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8347 | { "maps", open_self_maps, is_proc_myself }, |
Ilya Leoshkevich | 77ae576 | 2023-06-30 19:04:18 +0100 | [diff] [blame] | 8348 | { "smaps", open_self_smaps, is_proc_myself }, |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8349 | { "stat", open_self_stat, is_proc_myself }, |
| 8350 | { "auxv", open_self_auxv, is_proc_myself }, |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 8351 | { "cmdline", open_self_cmdline, is_proc_myself }, |
Marc-André Lureau | ee3eb3a | 2022-03-23 19:57:18 +0400 | [diff] [blame] | 8352 | #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8353 | { "/proc/net/route", open_net_route, is_proc }, |
| 8354 | #endif |
Richard Henderson | 121c8dd | 2023-08-23 17:13:14 -0700 | [diff] [blame] | 8355 | #if defined(HAVE_ARCH_PROC_CPUINFO) |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 8356 | { "/proc/cpuinfo", open_cpuinfo, is_proc }, |
| 8357 | #endif |
Richard Henderson | 121c8dd | 2023-08-23 17:13:14 -0700 | [diff] [blame] | 8358 | #if defined(HAVE_ARCH_PROC_HARDWARE) |
Laurent Vivier | 4ab6713 | 2019-05-17 15:31:49 +0200 | [diff] [blame] | 8359 | { "/proc/hardware", open_hardware, is_proc }, |
| 8360 | #endif |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8361 | { NULL, NULL, NULL } |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8362 | }; |
| 8363 | |
Helge Deller | b800205 | 2023-08-03 23:44:47 +0200 | [diff] [blame] | 8364 | /* if this is a file from /proc/ filesystem, expand full name */ |
| 8365 | proc_name = realpath(fname, NULL); |
| 8366 | if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) { |
| 8367 | pathname = proc_name; |
| 8368 | } else { |
| 8369 | pathname = fname; |
| 8370 | } |
| 8371 | |
Maxim Ostapenko | aa07f5e | 2014-05-02 11:17:07 +0300 | [diff] [blame] | 8372 | if (is_proc_myself(pathname, "exe")) { |
Ilya Leoshkevich | 35be898 | 2023-06-30 19:04:17 +0100 | [diff] [blame] | 8373 | if (safe) { |
| 8374 | return safe_openat(dirfd, exec_path, flags, mode); |
| 8375 | } else { |
| 8376 | return openat(dirfd, exec_path, flags, mode); |
| 8377 | } |
Maxim Ostapenko | aa07f5e | 2014-05-02 11:17:07 +0300 | [diff] [blame] | 8378 | } |
| 8379 | |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8380 | for (fake_open = fakes; fake_open->filename; fake_open++) { |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 8381 | if (fake_open->cmp(pathname, fake_open->filename)) { |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8382 | break; |
| 8383 | } |
| 8384 | } |
| 8385 | |
| 8386 | if (fake_open->filename) { |
| 8387 | const char *tmpdir; |
| 8388 | char filename[PATH_MAX]; |
| 8389 | int fd, r; |
| 8390 | |
Rainer Müller | 5b63de6 | 2022-07-29 17:49:51 +0200 | [diff] [blame] | 8391 | fd = memfd_create("qemu-open", 0); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8392 | if (fd < 0) { |
Rainer Müller | 5b63de6 | 2022-07-29 17:49:51 +0200 | [diff] [blame] | 8393 | if (errno != ENOSYS) { |
| 8394 | return fd; |
| 8395 | } |
| 8396 | /* create temporary file to map stat to */ |
| 8397 | tmpdir = getenv("TMPDIR"); |
| 8398 | if (!tmpdir) |
| 8399 | tmpdir = "/tmp"; |
| 8400 | snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); |
| 8401 | fd = mkstemp(filename); |
| 8402 | if (fd < 0) { |
| 8403 | return fd; |
| 8404 | } |
| 8405 | unlink(filename); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8406 | } |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8407 | |
| 8408 | if ((r = fake_open->fill(cpu_env, fd))) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 8409 | int e = errno; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8410 | close(fd); |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 8411 | errno = e; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8412 | return r; |
| 8413 | } |
| 8414 | lseek(fd, 0, SEEK_SET); |
| 8415 | |
| 8416 | return fd; |
| 8417 | } |
| 8418 | |
Ilya Leoshkevich | 35be898 | 2023-06-30 19:04:17 +0100 | [diff] [blame] | 8419 | if (safe) { |
| 8420 | return safe_openat(dirfd, path(pathname), flags, mode); |
| 8421 | } else { |
| 8422 | return openat(dirfd, path(pathname), flags, mode); |
| 8423 | } |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 8424 | } |
| 8425 | |
Ilya Leoshkevich | a4dab0a | 2023-06-30 19:04:16 +0100 | [diff] [blame] | 8426 | ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz) |
| 8427 | { |
| 8428 | ssize_t ret; |
| 8429 | |
| 8430 | if (!pathname || !buf) { |
| 8431 | errno = EFAULT; |
| 8432 | return -1; |
| 8433 | } |
| 8434 | |
| 8435 | if (!bufsiz) { |
| 8436 | /* Short circuit this for the magic exe check. */ |
| 8437 | errno = EINVAL; |
| 8438 | return -1; |
| 8439 | } |
| 8440 | |
| 8441 | if (is_proc_myself((const char *)pathname, "exe")) { |
| 8442 | /* |
| 8443 | * Don't worry about sign mismatch as earlier mapping |
| 8444 | * logic would have thrown a bad address error. |
| 8445 | */ |
| 8446 | ret = MIN(strlen(exec_path), bufsiz); |
| 8447 | /* We cannot NUL terminate the string. */ |
| 8448 | memcpy(buf, exec_path, ret); |
| 8449 | } else { |
| 8450 | ret = readlink(path(pathname), buf, bufsiz); |
| 8451 | } |
| 8452 | |
| 8453 | return ret; |
| 8454 | } |
| 8455 | |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 8456 | static int do_execv(CPUArchState *cpu_env, int dirfd, |
| 8457 | abi_long pathname, abi_long guest_argp, |
| 8458 | abi_long guest_envp, int flags, bool is_execveat) |
Drew DeVault | 156e1f6 | 2022-11-04 18:36:31 +0100 | [diff] [blame] | 8459 | { |
| 8460 | int ret; |
| 8461 | char **argp, **envp; |
| 8462 | int argc, envc; |
| 8463 | abi_ulong gp; |
| 8464 | abi_ulong addr; |
| 8465 | char **q; |
| 8466 | void *p; |
| 8467 | |
| 8468 | argc = 0; |
| 8469 | |
| 8470 | for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) { |
| 8471 | if (get_user_ual(addr, gp)) { |
| 8472 | return -TARGET_EFAULT; |
| 8473 | } |
| 8474 | if (!addr) { |
| 8475 | break; |
| 8476 | } |
| 8477 | argc++; |
| 8478 | } |
| 8479 | envc = 0; |
| 8480 | for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) { |
| 8481 | if (get_user_ual(addr, gp)) { |
| 8482 | return -TARGET_EFAULT; |
| 8483 | } |
| 8484 | if (!addr) { |
| 8485 | break; |
| 8486 | } |
| 8487 | envc++; |
| 8488 | } |
| 8489 | |
| 8490 | argp = g_new0(char *, argc + 1); |
| 8491 | envp = g_new0(char *, envc + 1); |
| 8492 | |
| 8493 | for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) { |
| 8494 | if (get_user_ual(addr, gp)) { |
| 8495 | goto execve_efault; |
| 8496 | } |
| 8497 | if (!addr) { |
| 8498 | break; |
| 8499 | } |
| 8500 | *q = lock_user_string(addr); |
| 8501 | if (!*q) { |
| 8502 | goto execve_efault; |
| 8503 | } |
| 8504 | } |
| 8505 | *q = NULL; |
| 8506 | |
| 8507 | for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) { |
| 8508 | if (get_user_ual(addr, gp)) { |
| 8509 | goto execve_efault; |
| 8510 | } |
| 8511 | if (!addr) { |
| 8512 | break; |
| 8513 | } |
| 8514 | *q = lock_user_string(addr); |
| 8515 | if (!*q) { |
| 8516 | goto execve_efault; |
| 8517 | } |
| 8518 | } |
| 8519 | *q = NULL; |
| 8520 | |
| 8521 | /* |
| 8522 | * Although execve() is not an interruptible syscall it is |
| 8523 | * a special case where we must use the safe_syscall wrapper: |
| 8524 | * if we allow a signal to happen before we make the host |
| 8525 | * syscall then we will 'lose' it, because at the point of |
| 8526 | * execve the process leaves QEMU's control. So we use the |
| 8527 | * safe syscall wrapper to ensure that we either take the |
| 8528 | * signal as a guest signal, or else it does not happen |
| 8529 | * before the execve completes and makes it the other |
| 8530 | * program's problem. |
| 8531 | */ |
| 8532 | p = lock_user_string(pathname); |
| 8533 | if (!p) { |
| 8534 | goto execve_efault; |
| 8535 | } |
| 8536 | |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 8537 | const char *exe = p; |
Drew DeVault | 156e1f6 | 2022-11-04 18:36:31 +0100 | [diff] [blame] | 8538 | if (is_proc_myself(p, "exe")) { |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 8539 | exe = exec_path; |
Drew DeVault | 156e1f6 | 2022-11-04 18:36:31 +0100 | [diff] [blame] | 8540 | } |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 8541 | ret = is_execveat |
| 8542 | ? safe_execveat(dirfd, exe, argp, envp, flags) |
| 8543 | : safe_execve(exe, argp, envp); |
| 8544 | ret = get_errno(ret); |
Drew DeVault | 156e1f6 | 2022-11-04 18:36:31 +0100 | [diff] [blame] | 8545 | |
| 8546 | unlock_user(p, pathname, 0); |
| 8547 | |
| 8548 | goto execve_end; |
| 8549 | |
| 8550 | execve_efault: |
| 8551 | ret = -TARGET_EFAULT; |
| 8552 | |
| 8553 | execve_end: |
| 8554 | for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) { |
| 8555 | if (get_user_ual(addr, gp) || !addr) { |
| 8556 | break; |
| 8557 | } |
| 8558 | unlock_user(*q, addr, 0); |
| 8559 | } |
| 8560 | for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) { |
| 8561 | if (get_user_ual(addr, gp) || !addr) { |
| 8562 | break; |
| 8563 | } |
| 8564 | unlock_user(*q, addr, 0); |
| 8565 | } |
| 8566 | |
| 8567 | g_free(argp); |
| 8568 | g_free(envp); |
| 8569 | return ret; |
| 8570 | } |
| 8571 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 8572 | #define TIMER_MAGIC 0x0caf0000 |
| 8573 | #define TIMER_MAGIC_MASK 0xffff0000 |
| 8574 | |
| 8575 | /* Convert QEMU provided timer ID back to internal 16bit index format */ |
| 8576 | static target_timer_t get_timer_id(abi_long arg) |
| 8577 | { |
| 8578 | target_timer_t timerid = arg; |
| 8579 | |
| 8580 | if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) { |
| 8581 | return -TARGET_EINVAL; |
| 8582 | } |
| 8583 | |
| 8584 | timerid &= 0xffff; |
| 8585 | |
| 8586 | if (timerid >= ARRAY_SIZE(g_posix_timers)) { |
| 8587 | return -TARGET_EINVAL; |
| 8588 | } |
| 8589 | |
| 8590 | return timerid; |
| 8591 | } |
| 8592 | |
Samuel Thibault | 2e0a871 | 2018-01-09 21:16:43 +0100 | [diff] [blame] | 8593 | static int target_to_host_cpu_mask(unsigned long *host_mask, |
| 8594 | size_t host_size, |
| 8595 | abi_ulong target_addr, |
| 8596 | size_t target_size) |
| 8597 | { |
| 8598 | unsigned target_bits = sizeof(abi_ulong) * 8; |
| 8599 | unsigned host_bits = sizeof(*host_mask) * 8; |
| 8600 | abi_ulong *target_mask; |
| 8601 | unsigned i, j; |
| 8602 | |
| 8603 | assert(host_size >= target_size); |
| 8604 | |
| 8605 | target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1); |
| 8606 | if (!target_mask) { |
| 8607 | return -TARGET_EFAULT; |
| 8608 | } |
| 8609 | memset(host_mask, 0, host_size); |
| 8610 | |
| 8611 | for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) { |
| 8612 | unsigned bit = i * target_bits; |
| 8613 | abi_ulong val; |
| 8614 | |
| 8615 | __get_user(val, &target_mask[i]); |
| 8616 | for (j = 0; j < target_bits; j++, bit++) { |
| 8617 | if (val & (1UL << j)) { |
| 8618 | host_mask[bit / host_bits] |= 1UL << (bit % host_bits); |
| 8619 | } |
| 8620 | } |
| 8621 | } |
| 8622 | |
| 8623 | unlock_user(target_mask, target_addr, 0); |
| 8624 | return 0; |
| 8625 | } |
| 8626 | |
| 8627 | static int host_to_target_cpu_mask(const unsigned long *host_mask, |
| 8628 | size_t host_size, |
| 8629 | abi_ulong target_addr, |
| 8630 | size_t target_size) |
| 8631 | { |
| 8632 | unsigned target_bits = sizeof(abi_ulong) * 8; |
| 8633 | unsigned host_bits = sizeof(*host_mask) * 8; |
| 8634 | abi_ulong *target_mask; |
| 8635 | unsigned i, j; |
| 8636 | |
| 8637 | assert(host_size >= target_size); |
| 8638 | |
| 8639 | target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0); |
| 8640 | if (!target_mask) { |
| 8641 | return -TARGET_EFAULT; |
| 8642 | } |
| 8643 | |
| 8644 | for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) { |
| 8645 | unsigned bit = i * target_bits; |
| 8646 | abi_ulong val = 0; |
| 8647 | |
| 8648 | for (j = 0; j < target_bits; j++, bit++) { |
| 8649 | if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) { |
| 8650 | val |= 1UL << j; |
| 8651 | } |
| 8652 | } |
| 8653 | __put_user(val, &target_mask[i]); |
| 8654 | } |
| 8655 | |
| 8656 | unlock_user(target_mask, target_addr, target_size); |
| 8657 | return 0; |
| 8658 | } |
| 8659 | |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8660 | #ifdef TARGET_NR_getdents |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8661 | static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count) |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8662 | { |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8663 | g_autofree void *hdirp = NULL; |
| 8664 | void *tdirp; |
| 8665 | int hlen, hoff, toff; |
| 8666 | int hreclen, treclen; |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 8667 | off_t prev_diroff = 0; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8668 | |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8669 | hdirp = g_try_malloc(count); |
| 8670 | if (!hdirp) { |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8671 | return -TARGET_ENOMEM; |
| 8672 | } |
| 8673 | |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8674 | #ifdef EMULATE_GETDENTS_WITH_GETDENTS |
| 8675 | hlen = sys_getdents(dirfd, hdirp, count); |
| 8676 | #else |
| 8677 | hlen = sys_getdents64(dirfd, hdirp, count); |
| 8678 | #endif |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8679 | |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8680 | hlen = get_errno(hlen); |
| 8681 | if (is_error(hlen)) { |
| 8682 | return hlen; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8683 | } |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8684 | |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8685 | tdirp = lock_user(VERIFY_WRITE, arg2, count, 0); |
| 8686 | if (!tdirp) { |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8687 | return -TARGET_EFAULT; |
| 8688 | } |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8689 | |
| 8690 | for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) { |
| 8691 | #ifdef EMULATE_GETDENTS_WITH_GETDENTS |
| 8692 | struct linux_dirent *hde = hdirp + hoff; |
| 8693 | #else |
| 8694 | struct linux_dirent64 *hde = hdirp + hoff; |
| 8695 | #endif |
| 8696 | struct target_dirent *tde = tdirp + toff; |
| 8697 | int namelen; |
| 8698 | uint8_t type; |
| 8699 | |
| 8700 | namelen = strlen(hde->d_name); |
| 8701 | hreclen = hde->d_reclen; |
| 8702 | treclen = offsetof(struct target_dirent, d_name) + namelen + 2; |
| 8703 | treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent)); |
| 8704 | |
| 8705 | if (toff + treclen > count) { |
| 8706 | /* |
| 8707 | * If the host struct is smaller than the target struct, or |
| 8708 | * requires less alignment and thus packs into less space, |
| 8709 | * then the host can return more entries than we can pass |
| 8710 | * on to the guest. |
| 8711 | */ |
| 8712 | if (toff == 0) { |
| 8713 | toff = -TARGET_EINVAL; /* result buffer is too small */ |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8714 | break; |
| 8715 | } |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8716 | /* |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8717 | * Return what we have, resetting the file pointer to the |
| 8718 | * location of the first record not returned. |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8719 | */ |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 8720 | lseek(dirfd, prev_diroff, SEEK_SET); |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8721 | break; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8722 | } |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8723 | |
| 8724 | prev_diroff = hde->d_off; |
| 8725 | tde->d_ino = tswapal(hde->d_ino); |
| 8726 | tde->d_off = tswapal(hde->d_off); |
| 8727 | tde->d_reclen = tswap16(treclen); |
| 8728 | memcpy(tde->d_name, hde->d_name, namelen + 1); |
| 8729 | |
| 8730 | /* |
| 8731 | * The getdents type is in what was formerly a padding byte at the |
| 8732 | * end of the structure. |
| 8733 | */ |
| 8734 | #ifdef EMULATE_GETDENTS_WITH_GETDENTS |
| 8735 | type = *((uint8_t *)hde + hreclen - 1); |
| 8736 | #else |
| 8737 | type = hde->d_type; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8738 | #endif |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8739 | *((uint8_t *)tde + treclen - 1) = type; |
| 8740 | } |
| 8741 | |
| 8742 | unlock_user(tdirp, arg2, toff); |
| 8743 | return toff; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8744 | } |
| 8745 | #endif /* TARGET_NR_getdents */ |
| 8746 | |
| 8747 | #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8748 | static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count) |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8749 | { |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8750 | g_autofree void *hdirp = NULL; |
| 8751 | void *tdirp; |
| 8752 | int hlen, hoff, toff; |
| 8753 | int hreclen, treclen; |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 8754 | off_t prev_diroff = 0; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8755 | |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8756 | hdirp = g_try_malloc(count); |
| 8757 | if (!hdirp) { |
| 8758 | return -TARGET_ENOMEM; |
| 8759 | } |
| 8760 | |
| 8761 | hlen = get_errno(sys_getdents64(dirfd, hdirp, count)); |
| 8762 | if (is_error(hlen)) { |
| 8763 | return hlen; |
| 8764 | } |
| 8765 | |
| 8766 | tdirp = lock_user(VERIFY_WRITE, arg2, count, 0); |
| 8767 | if (!tdirp) { |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8768 | return -TARGET_EFAULT; |
| 8769 | } |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8770 | |
| 8771 | for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) { |
| 8772 | struct linux_dirent64 *hde = hdirp + hoff; |
| 8773 | struct target_dirent64 *tde = tdirp + toff; |
| 8774 | int namelen; |
| 8775 | |
| 8776 | namelen = strlen(hde->d_name) + 1; |
| 8777 | hreclen = hde->d_reclen; |
| 8778 | treclen = offsetof(struct target_dirent64, d_name) + namelen; |
| 8779 | treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64)); |
| 8780 | |
| 8781 | if (toff + treclen > count) { |
| 8782 | /* |
| 8783 | * If the host struct is smaller than the target struct, or |
| 8784 | * requires less alignment and thus packs into less space, |
| 8785 | * then the host can return more entries than we can pass |
| 8786 | * on to the guest. |
| 8787 | */ |
| 8788 | if (toff == 0) { |
| 8789 | toff = -TARGET_EINVAL; /* result buffer is too small */ |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8790 | break; |
| 8791 | } |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8792 | /* |
| 8793 | * Return what we have, resetting the file pointer to the |
| 8794 | * location of the first record not returned. |
| 8795 | */ |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 8796 | lseek(dirfd, prev_diroff, SEEK_SET); |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8797 | break; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8798 | } |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8799 | |
| 8800 | prev_diroff = hde->d_off; |
| 8801 | tde->d_ino = tswap64(hde->d_ino); |
| 8802 | tde->d_off = tswap64(hde->d_off); |
| 8803 | tde->d_reclen = tswap16(treclen); |
| 8804 | tde->d_type = hde->d_type; |
| 8805 | memcpy(tde->d_name, hde->d_name, namelen); |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8806 | } |
Richard Henderson | aee14c7 | 2021-11-14 11:35:39 +0100 | [diff] [blame] | 8807 | |
| 8808 | unlock_user(tdirp, arg2, toff); |
| 8809 | return toff; |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 8810 | } |
| 8811 | #endif /* TARGET_NR_getdents64 */ |
| 8812 | |
Robbin Ehn | 9e1c7d9 | 2023-06-19 10:24:03 +0200 | [diff] [blame] | 8813 | #if defined(TARGET_NR_riscv_hwprobe) |
| 8814 | |
| 8815 | #define RISCV_HWPROBE_KEY_MVENDORID 0 |
| 8816 | #define RISCV_HWPROBE_KEY_MARCHID 1 |
| 8817 | #define RISCV_HWPROBE_KEY_MIMPID 2 |
| 8818 | |
| 8819 | #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3 |
| 8820 | #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0) |
| 8821 | |
Christoph Müllner | a3432cf | 2024-02-07 12:59:26 +0100 | [diff] [blame] | 8822 | #define RISCV_HWPROBE_KEY_IMA_EXT_0 4 |
| 8823 | #define RISCV_HWPROBE_IMA_FD (1 << 0) |
| 8824 | #define RISCV_HWPROBE_IMA_C (1 << 1) |
| 8825 | #define RISCV_HWPROBE_IMA_V (1 << 2) |
| 8826 | #define RISCV_HWPROBE_EXT_ZBA (1 << 3) |
| 8827 | #define RISCV_HWPROBE_EXT_ZBB (1 << 4) |
| 8828 | #define RISCV_HWPROBE_EXT_ZBS (1 << 5) |
| 8829 | #define RISCV_HWPROBE_EXT_ZICBOZ (1 << 6) |
| 8830 | #define RISCV_HWPROBE_EXT_ZBC (1 << 7) |
| 8831 | #define RISCV_HWPROBE_EXT_ZBKB (1 << 8) |
| 8832 | #define RISCV_HWPROBE_EXT_ZBKC (1 << 9) |
| 8833 | #define RISCV_HWPROBE_EXT_ZBKX (1 << 10) |
| 8834 | #define RISCV_HWPROBE_EXT_ZKND (1 << 11) |
| 8835 | #define RISCV_HWPROBE_EXT_ZKNE (1 << 12) |
| 8836 | #define RISCV_HWPROBE_EXT_ZKNH (1 << 13) |
| 8837 | #define RISCV_HWPROBE_EXT_ZKSED (1 << 14) |
| 8838 | #define RISCV_HWPROBE_EXT_ZKSH (1 << 15) |
| 8839 | #define RISCV_HWPROBE_EXT_ZKT (1 << 16) |
| 8840 | #define RISCV_HWPROBE_EXT_ZVBB (1 << 17) |
| 8841 | #define RISCV_HWPROBE_EXT_ZVBC (1 << 18) |
| 8842 | #define RISCV_HWPROBE_EXT_ZVKB (1 << 19) |
| 8843 | #define RISCV_HWPROBE_EXT_ZVKG (1 << 20) |
| 8844 | #define RISCV_HWPROBE_EXT_ZVKNED (1 << 21) |
| 8845 | #define RISCV_HWPROBE_EXT_ZVKNHA (1 << 22) |
| 8846 | #define RISCV_HWPROBE_EXT_ZVKNHB (1 << 23) |
| 8847 | #define RISCV_HWPROBE_EXT_ZVKSED (1 << 24) |
| 8848 | #define RISCV_HWPROBE_EXT_ZVKSH (1 << 25) |
| 8849 | #define RISCV_HWPROBE_EXT_ZVKT (1 << 26) |
| 8850 | #define RISCV_HWPROBE_EXT_ZFH (1 << 27) |
| 8851 | #define RISCV_HWPROBE_EXT_ZFHMIN (1 << 28) |
| 8852 | #define RISCV_HWPROBE_EXT_ZIHINTNTL (1 << 29) |
| 8853 | #define RISCV_HWPROBE_EXT_ZVFH (1 << 30) |
| 8854 | #define RISCV_HWPROBE_EXT_ZVFHMIN (1 << 31) |
| 8855 | #define RISCV_HWPROBE_EXT_ZFA (1ULL << 32) |
Christoph Müllner | 4f1a53b | 2024-02-07 13:22:55 +0100 | [diff] [blame] | 8856 | #define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33) |
Christoph Müllner | a3432cf | 2024-02-07 12:59:26 +0100 | [diff] [blame] | 8857 | #define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34) |
| 8858 | #define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35) |
Robbin Ehn | 9e1c7d9 | 2023-06-19 10:24:03 +0200 | [diff] [blame] | 8859 | |
| 8860 | #define RISCV_HWPROBE_KEY_CPUPERF_0 5 |
| 8861 | #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) |
| 8862 | #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) |
| 8863 | #define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0) |
| 8864 | #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0) |
| 8865 | #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0) |
| 8866 | #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0) |
| 8867 | |
Palmer Dabbelt | 301c65f | 2023-11-10 09:37:16 -0800 | [diff] [blame] | 8868 | #define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6 |
| 8869 | |
Robbin Ehn | 9e1c7d9 | 2023-06-19 10:24:03 +0200 | [diff] [blame] | 8870 | struct riscv_hwprobe { |
| 8871 | abi_llong key; |
| 8872 | abi_ullong value; |
| 8873 | }; |
| 8874 | |
| 8875 | static void risc_hwprobe_fill_pairs(CPURISCVState *env, |
| 8876 | struct riscv_hwprobe *pair, |
| 8877 | size_t pair_count) |
| 8878 | { |
| 8879 | const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); |
| 8880 | |
| 8881 | for (; pair_count > 0; pair_count--, pair++) { |
| 8882 | abi_llong key; |
| 8883 | abi_ullong value; |
| 8884 | __put_user(0, &pair->value); |
| 8885 | __get_user(key, &pair->key); |
| 8886 | switch (key) { |
| 8887 | case RISCV_HWPROBE_KEY_MVENDORID: |
| 8888 | __put_user(cfg->mvendorid, &pair->value); |
| 8889 | break; |
| 8890 | case RISCV_HWPROBE_KEY_MARCHID: |
| 8891 | __put_user(cfg->marchid, &pair->value); |
| 8892 | break; |
| 8893 | case RISCV_HWPROBE_KEY_MIMPID: |
| 8894 | __put_user(cfg->mimpid, &pair->value); |
| 8895 | break; |
| 8896 | case RISCV_HWPROBE_KEY_BASE_BEHAVIOR: |
| 8897 | value = riscv_has_ext(env, RVI) && |
| 8898 | riscv_has_ext(env, RVM) && |
| 8899 | riscv_has_ext(env, RVA) ? |
| 8900 | RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0; |
| 8901 | __put_user(value, &pair->value); |
| 8902 | break; |
| 8903 | case RISCV_HWPROBE_KEY_IMA_EXT_0: |
| 8904 | value = riscv_has_ext(env, RVF) && |
| 8905 | riscv_has_ext(env, RVD) ? |
| 8906 | RISCV_HWPROBE_IMA_FD : 0; |
| 8907 | value |= riscv_has_ext(env, RVC) ? |
Robbin Ehn | bb0a45e | 2023-08-28 16:56:59 +0200 | [diff] [blame] | 8908 | RISCV_HWPROBE_IMA_C : 0; |
| 8909 | value |= riscv_has_ext(env, RVV) ? |
| 8910 | RISCV_HWPROBE_IMA_V : 0; |
| 8911 | value |= cfg->ext_zba ? |
| 8912 | RISCV_HWPROBE_EXT_ZBA : 0; |
| 8913 | value |= cfg->ext_zbb ? |
| 8914 | RISCV_HWPROBE_EXT_ZBB : 0; |
| 8915 | value |= cfg->ext_zbs ? |
| 8916 | RISCV_HWPROBE_EXT_ZBS : 0; |
Christoph Müllner | 0191131 | 2024-02-07 12:59:25 +0100 | [diff] [blame] | 8917 | value |= cfg->ext_zicboz ? |
| 8918 | RISCV_HWPROBE_EXT_ZICBOZ : 0; |
Christoph Müllner | a3432cf | 2024-02-07 12:59:26 +0100 | [diff] [blame] | 8919 | value |= cfg->ext_zbc ? |
| 8920 | RISCV_HWPROBE_EXT_ZBC : 0; |
| 8921 | value |= cfg->ext_zbkb ? |
| 8922 | RISCV_HWPROBE_EXT_ZBKB : 0; |
| 8923 | value |= cfg->ext_zbkc ? |
| 8924 | RISCV_HWPROBE_EXT_ZBKC : 0; |
| 8925 | value |= cfg->ext_zbkx ? |
| 8926 | RISCV_HWPROBE_EXT_ZBKX : 0; |
| 8927 | value |= cfg->ext_zknd ? |
| 8928 | RISCV_HWPROBE_EXT_ZKND : 0; |
| 8929 | value |= cfg->ext_zkne ? |
| 8930 | RISCV_HWPROBE_EXT_ZKNE : 0; |
| 8931 | value |= cfg->ext_zknh ? |
| 8932 | RISCV_HWPROBE_EXT_ZKNH : 0; |
| 8933 | value |= cfg->ext_zksed ? |
| 8934 | RISCV_HWPROBE_EXT_ZKSED : 0; |
| 8935 | value |= cfg->ext_zksh ? |
| 8936 | RISCV_HWPROBE_EXT_ZKSH : 0; |
| 8937 | value |= cfg->ext_zkt ? |
| 8938 | RISCV_HWPROBE_EXT_ZKT : 0; |
| 8939 | value |= cfg->ext_zvbb ? |
| 8940 | RISCV_HWPROBE_EXT_ZVBB : 0; |
| 8941 | value |= cfg->ext_zvbc ? |
| 8942 | RISCV_HWPROBE_EXT_ZVBC : 0; |
| 8943 | value |= cfg->ext_zvkb ? |
| 8944 | RISCV_HWPROBE_EXT_ZVKB : 0; |
| 8945 | value |= cfg->ext_zvkg ? |
| 8946 | RISCV_HWPROBE_EXT_ZVKG : 0; |
| 8947 | value |= cfg->ext_zvkned ? |
| 8948 | RISCV_HWPROBE_EXT_ZVKNED : 0; |
| 8949 | value |= cfg->ext_zvknha ? |
| 8950 | RISCV_HWPROBE_EXT_ZVKNHA : 0; |
| 8951 | value |= cfg->ext_zvknhb ? |
| 8952 | RISCV_HWPROBE_EXT_ZVKNHB : 0; |
| 8953 | value |= cfg->ext_zvksed ? |
| 8954 | RISCV_HWPROBE_EXT_ZVKSED : 0; |
| 8955 | value |= cfg->ext_zvksh ? |
| 8956 | RISCV_HWPROBE_EXT_ZVKSH : 0; |
| 8957 | value |= cfg->ext_zvkt ? |
| 8958 | RISCV_HWPROBE_EXT_ZVKT : 0; |
| 8959 | value |= cfg->ext_zfh ? |
| 8960 | RISCV_HWPROBE_EXT_ZFH : 0; |
| 8961 | value |= cfg->ext_zfhmin ? |
| 8962 | RISCV_HWPROBE_EXT_ZFHMIN : 0; |
| 8963 | value |= cfg->ext_zihintntl ? |
| 8964 | RISCV_HWPROBE_EXT_ZIHINTNTL : 0; |
| 8965 | value |= cfg->ext_zvfh ? |
| 8966 | RISCV_HWPROBE_EXT_ZVFH : 0; |
| 8967 | value |= cfg->ext_zvfhmin ? |
| 8968 | RISCV_HWPROBE_EXT_ZVFHMIN : 0; |
| 8969 | value |= cfg->ext_zfa ? |
| 8970 | RISCV_HWPROBE_EXT_ZFA : 0; |
Christoph Müllner | 4f1a53b | 2024-02-07 13:22:55 +0100 | [diff] [blame] | 8971 | value |= cfg->ext_ztso ? |
| 8972 | RISCV_HWPROBE_EXT_ZTSO : 0; |
Christoph Müllner | a3432cf | 2024-02-07 12:59:26 +0100 | [diff] [blame] | 8973 | value |= cfg->ext_zacas ? |
| 8974 | RISCV_HWPROBE_EXT_ZACAS : 0; |
| 8975 | value |= cfg->ext_zicond ? |
| 8976 | RISCV_HWPROBE_EXT_ZICOND : 0; |
Robbin Ehn | 9e1c7d9 | 2023-06-19 10:24:03 +0200 | [diff] [blame] | 8977 | __put_user(value, &pair->value); |
| 8978 | break; |
| 8979 | case RISCV_HWPROBE_KEY_CPUPERF_0: |
| 8980 | __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value); |
| 8981 | break; |
Palmer Dabbelt | 301c65f | 2023-11-10 09:37:16 -0800 | [diff] [blame] | 8982 | case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE: |
| 8983 | value = cfg->ext_zicboz ? cfg->cboz_blocksize : 0; |
| 8984 | __put_user(value, &pair->value); |
| 8985 | break; |
Robbin Ehn | 9e1c7d9 | 2023-06-19 10:24:03 +0200 | [diff] [blame] | 8986 | default: |
| 8987 | __put_user(-1, &pair->key); |
| 8988 | break; |
| 8989 | } |
| 8990 | } |
| 8991 | } |
| 8992 | |
| 8993 | static int cpu_set_valid(abi_long arg3, abi_long arg4) |
| 8994 | { |
| 8995 | int ret, i, tmp; |
| 8996 | size_t host_mask_size, target_mask_size; |
| 8997 | unsigned long *host_mask; |
| 8998 | |
| 8999 | /* |
| 9000 | * cpu_set_t represent CPU masks as bit masks of type unsigned long *. |
| 9001 | * arg3 contains the cpu count. |
| 9002 | */ |
| 9003 | tmp = (8 * sizeof(abi_ulong)); |
| 9004 | target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong); |
| 9005 | host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) & |
| 9006 | ~(sizeof(*host_mask) - 1); |
| 9007 | |
| 9008 | host_mask = alloca(host_mask_size); |
| 9009 | |
| 9010 | ret = target_to_host_cpu_mask(host_mask, host_mask_size, |
| 9011 | arg4, target_mask_size); |
| 9012 | if (ret != 0) { |
| 9013 | return ret; |
| 9014 | } |
| 9015 | |
| 9016 | for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) { |
| 9017 | if (host_mask[i] != 0) { |
| 9018 | return 0; |
| 9019 | } |
| 9020 | } |
| 9021 | return -TARGET_EINVAL; |
| 9022 | } |
| 9023 | |
| 9024 | static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1, |
| 9025 | abi_long arg2, abi_long arg3, |
| 9026 | abi_long arg4, abi_long arg5) |
| 9027 | { |
| 9028 | int ret; |
| 9029 | struct riscv_hwprobe *host_pairs; |
| 9030 | |
| 9031 | /* flags must be 0 */ |
| 9032 | if (arg5 != 0) { |
| 9033 | return -TARGET_EINVAL; |
| 9034 | } |
| 9035 | |
| 9036 | /* check cpu_set */ |
| 9037 | if (arg3 != 0) { |
| 9038 | ret = cpu_set_valid(arg3, arg4); |
| 9039 | if (ret != 0) { |
| 9040 | return ret; |
| 9041 | } |
| 9042 | } else if (arg4 != 0) { |
| 9043 | return -TARGET_EINVAL; |
| 9044 | } |
| 9045 | |
| 9046 | /* no pairs */ |
| 9047 | if (arg2 == 0) { |
| 9048 | return 0; |
| 9049 | } |
| 9050 | |
| 9051 | host_pairs = lock_user(VERIFY_WRITE, arg1, |
| 9052 | sizeof(*host_pairs) * (size_t)arg2, 0); |
| 9053 | if (host_pairs == NULL) { |
| 9054 | return -TARGET_EFAULT; |
| 9055 | } |
| 9056 | risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2); |
| 9057 | unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2); |
| 9058 | return 0; |
| 9059 | } |
| 9060 | #endif /* TARGET_NR_riscv_hwprobe */ |
| 9061 | |
YAMAMOTO Takashi | e10fbe8 | 2021-05-31 14:50:12 +0900 | [diff] [blame] | 9062 | #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root) |
| 9063 | _syscall2(int, pivot_root, const char *, new_root, const char *, put_old) |
| 9064 | #endif |
| 9065 | |
Thomas Weißschuh | 7f696cdd | 2023-04-24 17:34:29 +0200 | [diff] [blame] | 9066 | #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree) |
| 9067 | #define __NR_sys_open_tree __NR_open_tree |
| 9068 | _syscall3(int, sys_open_tree, int, __dfd, const char *, __filename, |
| 9069 | unsigned int, __flags) |
| 9070 | #endif |
| 9071 | |
Thomas Weißschuh | 4b2d275 | 2023-04-24 17:34:28 +0200 | [diff] [blame] | 9072 | #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount) |
| 9073 | #define __NR_sys_move_mount __NR_move_mount |
| 9074 | _syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname, |
| 9075 | int, __to_dfd, const char *, __to_pathname, unsigned int, flag) |
| 9076 | #endif |
| 9077 | |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 9078 | /* This is an internal helper for do_syscall so that it is easier |
| 9079 | * to have a single return point, so that actions, such as logging |
| 9080 | * of syscall results, can be performed. |
| 9081 | * All errnos that do_syscall() returns must be -TARGET_<errcode>. |
| 9082 | */ |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 9083 | static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 9084 | abi_long arg2, abi_long arg3, abi_long arg4, |
| 9085 | abi_long arg5, abi_long arg6, abi_long arg7, |
| 9086 | abi_long arg8) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9087 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 9088 | CPUState *cpu = env_cpu(cpu_env); |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9089 | abi_long ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9090 | #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \ |
| 9091 | || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \ |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 9092 | || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \ |
| 9093 | || defined(TARGET_NR_statx) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9094 | struct stat st; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9095 | #endif |
| 9096 | #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \ |
| 9097 | || defined(TARGET_NR_fstatfs) |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 9098 | struct statfs stfs; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9099 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9100 | void *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9101 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9102 | switch(num) { |
| 9103 | case TARGET_NR_exit: |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 9104 | /* In old applications this may be used to implement _exit(2). |
zhaolichang | 6f9ff55 | 2020-09-17 15:50:25 +0800 | [diff] [blame] | 9105 | However in threaded applications it is used for thread termination, |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 9106 | and _exit_group is used for application termination. |
| 9107 | Do thread termination if we have more then one thread. */ |
Timothy E Baldwin | a099588 | 2016-05-27 15:51:56 +0100 | [diff] [blame] | 9108 | |
| 9109 | if (block_signals()) { |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 9110 | return -QEMU_ERESTARTSYS; |
Timothy E Baldwin | a099588 | 2016-05-27 15:51:56 +0100 | [diff] [blame] | 9111 | } |
| 9112 | |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 9113 | pthread_mutex_lock(&clone_lock); |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 9114 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 9115 | if (CPU_NEXT(first_cpu)) { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 9116 | TaskState *ts = get_task_state(cpu); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 9117 | |
Richard Henderson | 6490d9a | 2023-01-24 10:10:19 -1000 | [diff] [blame] | 9118 | if (ts->child_tidptr) { |
| 9119 | put_user_u32(0, ts->child_tidptr); |
| 9120 | do_sys_futex(g2h(cpu, ts->child_tidptr), |
| 9121 | FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
| 9122 | } |
| 9123 | |
| 9124 | object_unparent(OBJECT(cpu)); |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 9125 | object_unref(OBJECT(cpu)); |
| 9126 | /* |
| 9127 | * At this point the CPU should be unrealized and removed |
| 9128 | * from cpu lists. We can clean-up the rest of the thread |
| 9129 | * data without the lock held. |
| 9130 | */ |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 9131 | |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 9132 | pthread_mutex_unlock(&clone_lock); |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 9133 | |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 9134 | thread_cpu = NULL; |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 9135 | g_free(ts); |
Emilio G. Cota | 7090376 | 2015-08-23 20:23:41 -0400 | [diff] [blame] | 9136 | rcu_unregister_thread(); |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 9137 | pthread_exit(NULL); |
| 9138 | } |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 9139 | |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 9140 | pthread_mutex_unlock(&clone_lock); |
Alex Bennée | 708b6a6 | 2018-06-22 17:09:10 +0100 | [diff] [blame] | 9141 | preexit_cleanup(cpu_env, arg1); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 9142 | _exit(arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9143 | return 0; /* avoid warning */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9144 | case TARGET_NR_read: |
Andreas Schwab | ba584f1 | 2019-03-05 17:45:05 +0100 | [diff] [blame] | 9145 | if (arg2 == 0 && arg3 == 0) { |
| 9146 | return get_errno(safe_read(arg1, 0, 0)); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9147 | } else { |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 9148 | if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9149 | return -TARGET_EFAULT; |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 9150 | ret = get_errno(safe_read(arg1, p, arg3)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9151 | if (ret >= 0 && |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 9152 | fd_trans_host_to_target_data(arg1)) { |
| 9153 | ret = fd_trans_host_to_target_data(arg1)(p, ret); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9154 | } |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 9155 | unlock_user(p, arg2, ret); |
| 9156 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9157 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9158 | case TARGET_NR_write: |
Tony Garnock-Jones | 58cfa6c | 2018-09-08 19:22:05 +0100 | [diff] [blame] | 9159 | if (arg2 == 0 && arg3 == 0) { |
| 9160 | return get_errno(safe_write(arg1, 0, 0)); |
| 9161 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9162 | if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9163 | return -TARGET_EFAULT; |
Laurent Vivier | 04b9bcf | 2017-03-01 10:37:47 +0100 | [diff] [blame] | 9164 | if (fd_trans_target_to_host_data(arg1)) { |
| 9165 | void *copy = g_malloc(arg3); |
| 9166 | memcpy(copy, p, arg3); |
| 9167 | ret = fd_trans_target_to_host_data(arg1)(copy, arg3); |
| 9168 | if (ret >= 0) { |
| 9169 | ret = get_errno(safe_write(arg1, copy, ret)); |
| 9170 | } |
| 9171 | g_free(copy); |
| 9172 | } else { |
| 9173 | ret = get_errno(safe_write(arg1, p, arg3)); |
| 9174 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9175 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9176 | return ret; |
| 9177 | |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9178 | #ifdef TARGET_NR_open |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9179 | case TARGET_NR_open: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 9180 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9181 | return -TARGET_EFAULT; |
Ilya Leoshkevich | a4dab0a | 2023-06-30 19:04:16 +0100 | [diff] [blame] | 9182 | ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p, |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 9183 | target_to_host_bitmask(arg2, fcntl_flags_tbl), |
Ilya Leoshkevich | 35be898 | 2023-06-30 19:04:17 +0100 | [diff] [blame] | 9184 | arg3, true)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9185 | fd_trans_unregister(ret); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9186 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9187 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9188 | #endif |
ths | 8242483 | 2007-09-24 09:21:55 +0000 | [diff] [blame] | 9189 | case TARGET_NR_openat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9190 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9191 | return -TARGET_EFAULT; |
Ilya Leoshkevich | a4dab0a | 2023-06-30 19:04:16 +0100 | [diff] [blame] | 9192 | ret = get_errno(do_guest_openat(cpu_env, arg1, p, |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 9193 | target_to_host_bitmask(arg3, fcntl_flags_tbl), |
Ilya Leoshkevich | 35be898 | 2023-06-30 19:04:17 +0100 | [diff] [blame] | 9194 | arg4, true)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9195 | fd_trans_unregister(ret); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9196 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9197 | return ret; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 9198 | #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 9199 | case TARGET_NR_name_to_handle_at: |
| 9200 | ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9201 | return ret; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 9202 | #endif |
| 9203 | #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 9204 | case TARGET_NR_open_by_handle_at: |
| 9205 | ret = do_open_by_handle_at(arg1, arg2, arg3); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9206 | fd_trans_unregister(ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9207 | return ret; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 9208 | #endif |
Helge Deller | cc054c6 | 2022-09-18 21:45:46 +0200 | [diff] [blame] | 9209 | #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open) |
| 9210 | case TARGET_NR_pidfd_open: |
| 9211 | return get_errno(pidfd_open(arg1, arg2)); |
| 9212 | #endif |
| 9213 | #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal) |
| 9214 | case TARGET_NR_pidfd_send_signal: |
| 9215 | { |
Laurent Vivier | 46187d7 | 2022-10-05 18:38:26 +0200 | [diff] [blame] | 9216 | siginfo_t uinfo, *puinfo; |
Helge Deller | cc054c6 | 2022-09-18 21:45:46 +0200 | [diff] [blame] | 9217 | |
Laurent Vivier | 46187d7 | 2022-10-05 18:38:26 +0200 | [diff] [blame] | 9218 | if (arg3) { |
| 9219 | p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1); |
| 9220 | if (!p) { |
| 9221 | return -TARGET_EFAULT; |
| 9222 | } |
| 9223 | target_to_host_siginfo(&uinfo, p); |
| 9224 | unlock_user(p, arg3, 0); |
| 9225 | puinfo = &uinfo; |
| 9226 | } else { |
| 9227 | puinfo = NULL; |
Helge Deller | cc054c6 | 2022-09-18 21:45:46 +0200 | [diff] [blame] | 9228 | } |
Helge Deller | cc054c6 | 2022-09-18 21:45:46 +0200 | [diff] [blame] | 9229 | ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2), |
Laurent Vivier | 46187d7 | 2022-10-05 18:38:26 +0200 | [diff] [blame] | 9230 | puinfo, arg4)); |
Helge Deller | cc054c6 | 2022-09-18 21:45:46 +0200 | [diff] [blame] | 9231 | } |
| 9232 | return ret; |
| 9233 | #endif |
| 9234 | #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd) |
| 9235 | case TARGET_NR_pidfd_getfd: |
| 9236 | return get_errno(pidfd_getfd(arg1, arg2, arg3)); |
| 9237 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9238 | case TARGET_NR_close: |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9239 | fd_trans_unregister(arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9240 | return get_errno(close(arg1)); |
Helge Deller | af804f3 | 2022-10-25 04:34:14 +0200 | [diff] [blame] | 9241 | #if defined(__NR_close_range) && defined(TARGET_NR_close_range) |
| 9242 | case TARGET_NR_close_range: |
| 9243 | ret = get_errno(sys_close_range(arg1, arg2, arg3)); |
| 9244 | if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) { |
| 9245 | abi_long fd, maxfd; |
| 9246 | maxfd = MIN(arg2, target_fd_max); |
| 9247 | for (fd = arg1; fd < maxfd; fd++) { |
| 9248 | fd_trans_unregister(fd); |
| 9249 | } |
| 9250 | } |
| 9251 | return ret; |
| 9252 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9253 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9254 | case TARGET_NR_brk: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9255 | return do_brk(arg1); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9256 | #ifdef TARGET_NR_fork |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9257 | case TARGET_NR_fork: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9258 | return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0)); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9259 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9260 | #ifdef TARGET_NR_waitpid |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9261 | case TARGET_NR_waitpid: |
| 9262 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9263 | int status; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 9264 | ret = get_errno(safe_wait4(arg1, &status, arg3, 0)); |
Alexander Graf | 5379557 | 2011-11-24 00:44:43 +0100 | [diff] [blame] | 9265 | if (!is_error(ret) && arg2 && ret |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 9266 | && put_user_s32(host_to_target_waitstatus(status), arg2)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9267 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9268 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9269 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9270 | #endif |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 9271 | #ifdef TARGET_NR_waitid |
| 9272 | case TARGET_NR_waitid: |
| 9273 | { |
Richard Henderson | f0907ff | 2024-04-05 11:58:14 -1000 | [diff] [blame] | 9274 | struct rusage ru; |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 9275 | siginfo_t info; |
Richard Henderson | f0907ff | 2024-04-05 11:58:14 -1000 | [diff] [blame] | 9276 | |
| 9277 | ret = get_errno(safe_waitid(arg1, arg2, (arg3 ? &info : NULL), |
| 9278 | arg4, (arg5 ? &ru : NULL))); |
| 9279 | if (!is_error(ret)) { |
| 9280 | if (arg3) { |
| 9281 | p = lock_user(VERIFY_WRITE, arg3, |
| 9282 | sizeof(target_siginfo_t), 0); |
| 9283 | if (!p) { |
| 9284 | return -TARGET_EFAULT; |
| 9285 | } |
| 9286 | host_to_target_siginfo(p, &info); |
| 9287 | unlock_user(p, arg3, sizeof(target_siginfo_t)); |
| 9288 | } |
| 9289 | if (arg5 && host_to_target_rusage(arg5, &ru)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9290 | return -TARGET_EFAULT; |
Richard Henderson | f0907ff | 2024-04-05 11:58:14 -1000 | [diff] [blame] | 9291 | } |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 9292 | } |
| 9293 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9294 | return ret; |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 9295 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9296 | #ifdef TARGET_NR_creat /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9297 | case TARGET_NR_creat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9298 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9299 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9300 | ret = get_errno(creat(p, arg2)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9301 | fd_trans_unregister(ret); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9302 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9303 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9304 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9305 | #ifdef TARGET_NR_link |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9306 | case TARGET_NR_link: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9307 | { |
| 9308 | void * p2; |
| 9309 | p = lock_user_string(arg1); |
| 9310 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9311 | if (!p || !p2) |
| 9312 | ret = -TARGET_EFAULT; |
| 9313 | else |
| 9314 | ret = get_errno(link(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9315 | unlock_user(p2, arg2, 0); |
| 9316 | unlock_user(p, arg1, 0); |
| 9317 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9318 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9319 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9320 | #if defined(TARGET_NR_linkat) |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 9321 | case TARGET_NR_linkat: |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 9322 | { |
| 9323 | void * p2 = NULL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9324 | if (!arg2 || !arg4) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9325 | return -TARGET_EFAULT; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 9326 | p = lock_user_string(arg2); |
| 9327 | p2 = lock_user_string(arg4); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9328 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 9329 | ret = -TARGET_EFAULT; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 9330 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9331 | ret = get_errno(linkat(arg1, p, arg3, p2, arg5)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9332 | unlock_user(p, arg2, 0); |
| 9333 | unlock_user(p2, arg4, 0); |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 9334 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9335 | return ret; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 9336 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9337 | #ifdef TARGET_NR_unlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9338 | case TARGET_NR_unlink: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9339 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9340 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9341 | ret = get_errno(unlink(p)); |
| 9342 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9343 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9344 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9345 | #if defined(TARGET_NR_unlinkat) |
ths | 8170f56 | 2007-09-24 09:24:11 +0000 | [diff] [blame] | 9346 | case TARGET_NR_unlinkat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9347 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9348 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9349 | ret = get_errno(unlinkat(arg1, p, arg3)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9350 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9351 | return ret; |
balrog | b7d35e6 | 2007-12-12 00:40:24 +0000 | [diff] [blame] | 9352 | #endif |
Drew DeVault | 55bbe4d | 2022-11-04 18:36:32 +0100 | [diff] [blame] | 9353 | case TARGET_NR_execveat: |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 9354 | return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9355 | case TARGET_NR_execve: |
Pierrick Bouvier | 7a8d9f3 | 2023-07-05 14:10:23 +0200 | [diff] [blame] | 9356 | return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9357 | case TARGET_NR_chdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9358 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9359 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9360 | ret = get_errno(chdir(p)); |
| 9361 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9362 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9363 | #ifdef TARGET_NR_time |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9364 | case TARGET_NR_time: |
| 9365 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9366 | time_t host_time; |
| 9367 | ret = get_errno(time(&host_time)); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 9368 | if (!is_error(ret) |
| 9369 | && arg1 |
| 9370 | && put_user_sal(host_time, arg1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9371 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9372 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9373 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9374 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9375 | #ifdef TARGET_NR_mknod |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9376 | case TARGET_NR_mknod: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9377 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9378 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9379 | ret = get_errno(mknod(p, arg2, arg3)); |
| 9380 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9381 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9382 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9383 | #if defined(TARGET_NR_mknodat) |
ths | 75ac37a | 2007-09-24 09:23:05 +0000 | [diff] [blame] | 9384 | case TARGET_NR_mknodat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9385 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9386 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9387 | ret = get_errno(mknodat(arg1, p, arg3, arg4)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9388 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9389 | return ret; |
ths | 75ac37a | 2007-09-24 09:23:05 +0000 | [diff] [blame] | 9390 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9391 | #ifdef TARGET_NR_chmod |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9392 | case TARGET_NR_chmod: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9393 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9394 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9395 | ret = get_errno(chmod(p, arg2)); |
| 9396 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9397 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9398 | #endif |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9399 | #ifdef TARGET_NR_lseek |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9400 | case TARGET_NR_lseek: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9401 | return get_errno(lseek(arg1, arg2, arg3)); |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9402 | #endif |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 9403 | #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA) |
| 9404 | /* Alpha specific */ |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9405 | case TARGET_NR_getxpid: |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 9406 | cpu_env->ir[IR_A4] = getppid(); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9407 | return get_errno(getpid()); |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 9408 | #endif |
| 9409 | #ifdef TARGET_NR_getpid |
| 9410 | case TARGET_NR_getpid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9411 | return get_errno(getpid()); |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 9412 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9413 | case TARGET_NR_mount: |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 9414 | { |
| 9415 | /* need to look at the data field */ |
| 9416 | void *p2, *p3; |
| 9417 | |
| 9418 | if (arg1) { |
| 9419 | p = lock_user_string(arg1); |
| 9420 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9421 | return -TARGET_EFAULT; |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 9422 | } |
| 9423 | } else { |
| 9424 | p = NULL; |
| 9425 | } |
| 9426 | |
| 9427 | p2 = lock_user_string(arg2); |
| 9428 | if (!p2) { |
| 9429 | if (arg1) { |
| 9430 | unlock_user(p, arg1, 0); |
| 9431 | } |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9432 | return -TARGET_EFAULT; |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 9433 | } |
| 9434 | |
| 9435 | if (arg3) { |
| 9436 | p3 = lock_user_string(arg3); |
| 9437 | if (!p3) { |
| 9438 | if (arg1) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9439 | unlock_user(p, arg1, 0); |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 9440 | } |
| 9441 | unlock_user(p2, arg2, 0); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9442 | return -TARGET_EFAULT; |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 9443 | } |
| 9444 | } else { |
| 9445 | p3 = NULL; |
| 9446 | } |
| 9447 | |
| 9448 | /* FIXME - arg5 should be locked, but it isn't clear how to |
| 9449 | * do that since it's not guaranteed to be a NULL-terminated |
| 9450 | * string. |
| 9451 | */ |
| 9452 | if (!arg5) { |
| 9453 | ret = mount(p, p2, p3, (unsigned long)arg4, NULL); |
| 9454 | } else { |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 9455 | ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5)); |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 9456 | } |
| 9457 | ret = get_errno(ret); |
| 9458 | |
| 9459 | if (arg1) { |
| 9460 | unlock_user(p, arg1, 0); |
| 9461 | } |
| 9462 | unlock_user(p2, arg2, 0); |
| 9463 | if (arg3) { |
| 9464 | unlock_user(p3, arg3, 0); |
| 9465 | } |
| 9466 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9467 | return ret; |
Laurent Vivier | 6eb9dbf | 2020-05-02 21:46:42 +0200 | [diff] [blame] | 9468 | #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount) |
| 9469 | #if defined(TARGET_NR_umount) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9470 | case TARGET_NR_umount: |
Laurent Vivier | 6eb9dbf | 2020-05-02 21:46:42 +0200 | [diff] [blame] | 9471 | #endif |
| 9472 | #if defined(TARGET_NR_oldumount) |
| 9473 | case TARGET_NR_oldumount: |
| 9474 | #endif |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9475 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9476 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9477 | ret = get_errno(umount(p)); |
| 9478 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9479 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9480 | #endif |
Thomas Weißschuh | 4b2d275 | 2023-04-24 17:34:28 +0200 | [diff] [blame] | 9481 | #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount) |
| 9482 | case TARGET_NR_move_mount: |
| 9483 | { |
| 9484 | void *p2, *p4; |
| 9485 | |
| 9486 | if (!arg2 || !arg4) { |
| 9487 | return -TARGET_EFAULT; |
| 9488 | } |
| 9489 | |
| 9490 | p2 = lock_user_string(arg2); |
| 9491 | if (!p2) { |
| 9492 | return -TARGET_EFAULT; |
| 9493 | } |
| 9494 | |
| 9495 | p4 = lock_user_string(arg4); |
| 9496 | if (!p4) { |
| 9497 | unlock_user(p2, arg2, 0); |
| 9498 | return -TARGET_EFAULT; |
| 9499 | } |
| 9500 | ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5)); |
| 9501 | |
| 9502 | unlock_user(p2, arg2, 0); |
| 9503 | unlock_user(p4, arg4, 0); |
| 9504 | |
| 9505 | return ret; |
| 9506 | } |
| 9507 | #endif |
Thomas Weißschuh | 7f696cdd | 2023-04-24 17:34:29 +0200 | [diff] [blame] | 9508 | #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree) |
| 9509 | case TARGET_NR_open_tree: |
| 9510 | { |
| 9511 | void *p2; |
| 9512 | int host_flags; |
| 9513 | |
| 9514 | if (!arg2) { |
| 9515 | return -TARGET_EFAULT; |
| 9516 | } |
| 9517 | |
| 9518 | p2 = lock_user_string(arg2); |
| 9519 | if (!p2) { |
| 9520 | return -TARGET_EFAULT; |
| 9521 | } |
| 9522 | |
| 9523 | host_flags = arg3 & ~TARGET_O_CLOEXEC; |
| 9524 | if (arg3 & TARGET_O_CLOEXEC) { |
| 9525 | host_flags |= O_CLOEXEC; |
| 9526 | } |
| 9527 | |
| 9528 | ret = get_errno(sys_open_tree(arg1, p2, host_flags)); |
| 9529 | |
| 9530 | unlock_user(p2, arg2, 0); |
| 9531 | |
| 9532 | return ret; |
| 9533 | } |
| 9534 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9535 | #ifdef TARGET_NR_stime /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9536 | case TARGET_NR_stime: |
| 9537 | { |
Laurent Vivier | 0f1f2d4 | 2019-11-12 15:25:56 +0100 | [diff] [blame] | 9538 | struct timespec ts; |
| 9539 | ts.tv_nsec = 0; |
| 9540 | if (get_user_sal(ts.tv_sec, arg1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9541 | return -TARGET_EFAULT; |
Laurent Vivier | 0f1f2d4 | 2019-11-12 15:25:56 +0100 | [diff] [blame] | 9542 | } |
| 9543 | return get_errno(clock_settime(CLOCK_REALTIME, &ts)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9544 | } |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9545 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9546 | #ifdef TARGET_NR_alarm /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9547 | case TARGET_NR_alarm: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9548 | return alarm(arg1); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9549 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9550 | #ifdef TARGET_NR_pause /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9551 | case TARGET_NR_pause: |
Timothy E Baldwin | f59ec60 | 2016-05-27 15:51:55 +0100 | [diff] [blame] | 9552 | if (!block_signals()) { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 9553 | sigsuspend(&get_task_state(cpu)->signal_mask); |
Timothy E Baldwin | f59ec60 | 2016-05-27 15:51:55 +0100 | [diff] [blame] | 9554 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9555 | return -TARGET_EINTR; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9556 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9557 | #ifdef TARGET_NR_utime |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9558 | case TARGET_NR_utime: |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9559 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9560 | struct utimbuf tbuf, *host_tbuf; |
| 9561 | struct target_utimbuf *target_tbuf; |
| 9562 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9563 | if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9564 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 9565 | tbuf.actime = tswapal(target_tbuf->actime); |
| 9566 | tbuf.modtime = tswapal(target_tbuf->modtime); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9567 | unlock_user_struct(target_tbuf, arg2, 0); |
| 9568 | host_tbuf = &tbuf; |
bellard | f72e8ff | 2004-05-03 19:23:07 +0000 | [diff] [blame] | 9569 | } else { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9570 | host_tbuf = NULL; |
bellard | f72e8ff | 2004-05-03 19:23:07 +0000 | [diff] [blame] | 9571 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9572 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9573 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9574 | ret = get_errno(utime(p, host_tbuf)); |
| 9575 | unlock_user(p, arg1, 0); |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9576 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9577 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9578 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9579 | #ifdef TARGET_NR_utimes |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 9580 | case TARGET_NR_utimes: |
| 9581 | { |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 9582 | struct timeval *tvp, tv[2]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9583 | if (arg2) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 9584 | if (copy_from_user_timeval(&tv[0], arg2) |
| 9585 | || copy_from_user_timeval(&tv[1], |
| 9586 | arg2 + sizeof(struct target_timeval))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9587 | return -TARGET_EFAULT; |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 9588 | tvp = tv; |
| 9589 | } else { |
| 9590 | tvp = NULL; |
| 9591 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9592 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9593 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9594 | ret = get_errno(utimes(p, tvp)); |
| 9595 | unlock_user(p, arg1, 0); |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 9596 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9597 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9598 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9599 | #if defined(TARGET_NR_futimesat) |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 9600 | case TARGET_NR_futimesat: |
| 9601 | { |
| 9602 | struct timeval *tvp, tv[2]; |
| 9603 | if (arg3) { |
| 9604 | if (copy_from_user_timeval(&tv[0], arg3) |
| 9605 | || copy_from_user_timeval(&tv[1], |
| 9606 | arg3 + sizeof(struct target_timeval))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9607 | return -TARGET_EFAULT; |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 9608 | tvp = tv; |
| 9609 | } else { |
| 9610 | tvp = NULL; |
| 9611 | } |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9612 | if (!(p = lock_user_string(arg2))) { |
| 9613 | return -TARGET_EFAULT; |
| 9614 | } |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9615 | ret = get_errno(futimesat(arg1, path(p), tvp)); |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 9616 | unlock_user(p, arg2, 0); |
| 9617 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9618 | return ret; |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 9619 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9620 | #ifdef TARGET_NR_access |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9621 | case TARGET_NR_access: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9622 | if (!(p = lock_user_string(arg1))) { |
| 9623 | return -TARGET_EFAULT; |
| 9624 | } |
Ulrich Hecht | 719f908 | 2009-07-03 17:09:29 +0200 | [diff] [blame] | 9625 | ret = get_errno(access(path(p), arg2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9626 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9627 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9628 | #endif |
ths | 92a34c1 | 2007-09-24 09:27:49 +0000 | [diff] [blame] | 9629 | #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) |
| 9630 | case TARGET_NR_faccessat: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9631 | if (!(p = lock_user_string(arg2))) { |
| 9632 | return -TARGET_EFAULT; |
| 9633 | } |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9634 | ret = get_errno(faccessat(arg1, p, arg3, 0)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9635 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9636 | return ret; |
ths | 92a34c1 | 2007-09-24 09:27:49 +0000 | [diff] [blame] | 9637 | #endif |
WANG Xuerui | 35a2c85 | 2022-10-09 14:08:13 +0800 | [diff] [blame] | 9638 | #if defined(TARGET_NR_faccessat2) |
| 9639 | case TARGET_NR_faccessat2: |
| 9640 | if (!(p = lock_user_string(arg2))) { |
| 9641 | return -TARGET_EFAULT; |
| 9642 | } |
| 9643 | ret = get_errno(faccessat(arg1, p, arg3, arg4)); |
| 9644 | unlock_user(p, arg2, 0); |
| 9645 | return ret; |
| 9646 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9647 | #ifdef TARGET_NR_nice /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9648 | case TARGET_NR_nice: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9649 | return get_errno(nice(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9650 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9651 | case TARGET_NR_sync: |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 9652 | sync(); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9653 | return 0; |
Aleksandar Markovic | 5a03cd0 | 2016-10-10 13:23:30 +0200 | [diff] [blame] | 9654 | #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS) |
| 9655 | case TARGET_NR_syncfs: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9656 | return get_errno(syncfs(arg1)); |
Aleksandar Markovic | 5a03cd0 | 2016-10-10 13:23:30 +0200 | [diff] [blame] | 9657 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9658 | case TARGET_NR_kill: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9659 | return get_errno(safe_kill(arg1, target_to_host_signal(arg2))); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9660 | #ifdef TARGET_NR_rename |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9661 | case TARGET_NR_rename: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9662 | { |
| 9663 | void *p2; |
| 9664 | p = lock_user_string(arg1); |
| 9665 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9666 | if (!p || !p2) |
| 9667 | ret = -TARGET_EFAULT; |
| 9668 | else |
| 9669 | ret = get_errno(rename(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9670 | unlock_user(p2, arg2, 0); |
| 9671 | unlock_user(p, arg1, 0); |
| 9672 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9673 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9674 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9675 | #if defined(TARGET_NR_renameat) |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 9676 | case TARGET_NR_renameat: |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 9677 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9678 | void *p2; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 9679 | p = lock_user_string(arg2); |
| 9680 | p2 = lock_user_string(arg4); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9681 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 9682 | ret = -TARGET_EFAULT; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 9683 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9684 | ret = get_errno(renameat(arg1, p, arg3, p2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9685 | unlock_user(p2, arg4, 0); |
| 9686 | unlock_user(p, arg2, 0); |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 9687 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9688 | return ret; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 9689 | #endif |
Andreas Schwab | 95d0307 | 2018-01-23 11:53:31 +0100 | [diff] [blame] | 9690 | #if defined(TARGET_NR_renameat2) |
| 9691 | case TARGET_NR_renameat2: |
| 9692 | { |
| 9693 | void *p2; |
| 9694 | p = lock_user_string(arg2); |
| 9695 | p2 = lock_user_string(arg4); |
| 9696 | if (!p || !p2) { |
| 9697 | ret = -TARGET_EFAULT; |
| 9698 | } else { |
| 9699 | ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5)); |
| 9700 | } |
| 9701 | unlock_user(p2, arg4, 0); |
| 9702 | unlock_user(p, arg2, 0); |
| 9703 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9704 | return ret; |
Andreas Schwab | 95d0307 | 2018-01-23 11:53:31 +0100 | [diff] [blame] | 9705 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9706 | #ifdef TARGET_NR_mkdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9707 | case TARGET_NR_mkdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9708 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9709 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9710 | ret = get_errno(mkdir(p, arg2)); |
| 9711 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9712 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9713 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9714 | #if defined(TARGET_NR_mkdirat) |
ths | 4472ad0 | 2007-09-24 09:22:32 +0000 | [diff] [blame] | 9715 | case TARGET_NR_mkdirat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9716 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9717 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9718 | ret = get_errno(mkdirat(arg1, p, arg3)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9719 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9720 | return ret; |
ths | 4472ad0 | 2007-09-24 09:22:32 +0000 | [diff] [blame] | 9721 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9722 | #ifdef TARGET_NR_rmdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9723 | case TARGET_NR_rmdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9724 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9725 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9726 | ret = get_errno(rmdir(p)); |
| 9727 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9728 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9729 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9730 | case TARGET_NR_dup: |
| 9731 | ret = get_errno(dup(arg1)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9732 | if (ret >= 0) { |
| 9733 | fd_trans_dup(arg1, ret); |
| 9734 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9735 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9736 | #ifdef TARGET_NR_pipe |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9737 | case TARGET_NR_pipe: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9738 | return do_pipe(cpu_env, arg1, 0, 0); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9739 | #endif |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 9740 | #ifdef TARGET_NR_pipe2 |
| 9741 | case TARGET_NR_pipe2: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9742 | return do_pipe(cpu_env, arg1, |
| 9743 | target_to_host_bitmask(arg2, fcntl_flags_tbl), 1); |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 9744 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9745 | case TARGET_NR_times: |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 9746 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9747 | struct target_tms *tmsp; |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 9748 | struct tms tms; |
| 9749 | ret = get_errno(times(&tms)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9750 | if (arg1) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9751 | tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0); |
| 9752 | if (!tmsp) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9753 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 9754 | tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime)); |
| 9755 | tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime)); |
| 9756 | tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime)); |
| 9757 | tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime)); |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 9758 | } |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 9759 | if (!is_error(ret)) |
| 9760 | ret = host_to_target_clock_t(ret); |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 9761 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9762 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9763 | case TARGET_NR_acct: |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 9764 | if (arg1 == 0) { |
| 9765 | ret = get_errno(acct(NULL)); |
| 9766 | } else { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9767 | if (!(p = lock_user_string(arg1))) { |
| 9768 | return -TARGET_EFAULT; |
| 9769 | } |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 9770 | ret = get_errno(acct(path(p))); |
| 9771 | unlock_user(p, arg1, 0); |
| 9772 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9773 | return ret; |
Richard Henderson | 8070e7b | 2013-07-24 09:50:00 -1000 | [diff] [blame] | 9774 | #ifdef TARGET_NR_umount2 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9775 | case TARGET_NR_umount2: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9776 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9777 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9778 | ret = get_errno(umount2(p, arg2)); |
| 9779 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9780 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9781 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9782 | case TARGET_NR_ioctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9783 | return do_ioctl(arg1, arg2, arg3); |
Michael Clark | 47ae93c | 2018-03-03 01:31:11 +1300 | [diff] [blame] | 9784 | #ifdef TARGET_NR_fcntl |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9785 | case TARGET_NR_fcntl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9786 | return do_fcntl(arg1, arg2, arg3); |
Michael Clark | 47ae93c | 2018-03-03 01:31:11 +1300 | [diff] [blame] | 9787 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9788 | case TARGET_NR_setpgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9789 | return get_errno(setpgid(arg1, arg2)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9790 | case TARGET_NR_umask: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9791 | return get_errno(umask(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9792 | case TARGET_NR_chroot: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9793 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9794 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9795 | ret = get_errno(chroot(p)); |
| 9796 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9797 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9798 | #ifdef TARGET_NR_dup2 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9799 | case TARGET_NR_dup2: |
| 9800 | ret = get_errno(dup2(arg1, arg2)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9801 | if (ret >= 0) { |
| 9802 | fd_trans_dup(arg1, arg2); |
| 9803 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9804 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9805 | #endif |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 9806 | #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3) |
| 9807 | case TARGET_NR_dup3: |
Peter Maydell | 10fa993 | 2017-12-15 15:18:00 +0000 | [diff] [blame] | 9808 | { |
| 9809 | int host_flags; |
| 9810 | |
| 9811 | if ((arg3 & ~TARGET_O_CLOEXEC) != 0) { |
| 9812 | return -EINVAL; |
| 9813 | } |
| 9814 | host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl); |
| 9815 | ret = get_errno(dup3(arg1, arg2, host_flags)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 9816 | if (ret >= 0) { |
| 9817 | fd_trans_dup(arg1, arg2); |
| 9818 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9819 | return ret; |
Peter Maydell | 10fa993 | 2017-12-15 15:18:00 +0000 | [diff] [blame] | 9820 | } |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 9821 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9822 | #ifdef TARGET_NR_getppid /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9823 | case TARGET_NR_getppid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9824 | return get_errno(getppid()); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9825 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9826 | #ifdef TARGET_NR_getpgrp |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9827 | case TARGET_NR_getpgrp: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9828 | return get_errno(getpgrp()); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9829 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9830 | case TARGET_NR_setsid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9831 | return get_errno(setsid()); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9832 | #ifdef TARGET_NR_sigaction |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9833 | case TARGET_NR_sigaction: |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9834 | { |
Richard Henderson | 02d0de1 | 2021-04-22 16:02:26 -0700 | [diff] [blame] | 9835 | #if defined(TARGET_MIPS) |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 9836 | struct target_sigaction act, oact, *pact, *old_act; |
| 9837 | |
| 9838 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9839 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9840 | return -TARGET_EFAULT; |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 9841 | act._sa_handler = old_act->_sa_handler; |
| 9842 | target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]); |
| 9843 | act.sa_flags = old_act->sa_flags; |
| 9844 | unlock_user_struct(old_act, arg2, 0); |
| 9845 | pact = &act; |
| 9846 | } else { |
| 9847 | pact = NULL; |
| 9848 | } |
| 9849 | |
Richard Henderson | 02fb28e | 2021-04-22 16:02:23 -0700 | [diff] [blame] | 9850 | ret = get_errno(do_sigaction(arg1, pact, &oact, 0)); |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 9851 | |
| 9852 | if (!is_error(ret) && arg3) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9853 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9854 | return -TARGET_EFAULT; |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 9855 | old_act->_sa_handler = oact._sa_handler; |
| 9856 | old_act->sa_flags = oact.sa_flags; |
| 9857 | old_act->sa_mask.sig[0] = oact.sa_mask.sig[0]; |
| 9858 | old_act->sa_mask.sig[1] = 0; |
| 9859 | old_act->sa_mask.sig[2] = 0; |
| 9860 | old_act->sa_mask.sig[3] = 0; |
| 9861 | unlock_user_struct(old_act, arg3, 1); |
| 9862 | } |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9863 | #else |
| 9864 | struct target_old_sigaction *old_act; |
| 9865 | struct target_sigaction act, oact, *pact; |
| 9866 | if (arg2) { |
| 9867 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9868 | return -TARGET_EFAULT; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9869 | act._sa_handler = old_act->_sa_handler; |
| 9870 | target_siginitset(&act.sa_mask, old_act->sa_mask); |
| 9871 | act.sa_flags = old_act->sa_flags; |
Richard Henderson | ca19227 | 2021-04-22 16:02:24 -0700 | [diff] [blame] | 9872 | #ifdef TARGET_ARCH_HAS_SA_RESTORER |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9873 | act.sa_restorer = old_act->sa_restorer; |
Richard Henderson | ca19227 | 2021-04-22 16:02:24 -0700 | [diff] [blame] | 9874 | #endif |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9875 | unlock_user_struct(old_act, arg2, 0); |
| 9876 | pact = &act; |
| 9877 | } else { |
| 9878 | pact = NULL; |
| 9879 | } |
Richard Henderson | 02fb28e | 2021-04-22 16:02:23 -0700 | [diff] [blame] | 9880 | ret = get_errno(do_sigaction(arg1, pact, &oact, 0)); |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9881 | if (!is_error(ret) && arg3) { |
| 9882 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9883 | return -TARGET_EFAULT; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9884 | old_act->_sa_handler = oact._sa_handler; |
| 9885 | old_act->sa_mask = oact.sa_mask.sig[0]; |
| 9886 | old_act->sa_flags = oact.sa_flags; |
Richard Henderson | ca19227 | 2021-04-22 16:02:24 -0700 | [diff] [blame] | 9887 | #ifdef TARGET_ARCH_HAS_SA_RESTORER |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9888 | old_act->sa_restorer = oact.sa_restorer; |
Richard Henderson | ca19227 | 2021-04-22 16:02:24 -0700 | [diff] [blame] | 9889 | #endif |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 9890 | unlock_user_struct(old_act, arg3, 1); |
| 9891 | } |
ths | 388bb21 | 2007-05-13 13:58:00 +0000 | [diff] [blame] | 9892 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9893 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9894 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9895 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9896 | case TARGET_NR_rt_sigaction: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9897 | { |
Richard Henderson | 0f6f990 | 2021-04-22 16:02:25 -0700 | [diff] [blame] | 9898 | /* |
| 9899 | * For Alpha and SPARC this is a 5 argument syscall, with |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 9900 | * a 'restorer' parameter which must be copied into the |
| 9901 | * sa_restorer field of the sigaction struct. |
| 9902 | * For Alpha that 'restorer' is arg5; for SPARC it is arg4, |
| 9903 | * and arg5 is the sigsetsize. |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 9904 | */ |
Richard Henderson | 0f6f990 | 2021-04-22 16:02:25 -0700 | [diff] [blame] | 9905 | #if defined(TARGET_ALPHA) |
| 9906 | target_ulong sigsetsize = arg4; |
| 9907 | target_ulong restorer = arg5; |
| 9908 | #elif defined(TARGET_SPARC) |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 9909 | target_ulong restorer = arg4; |
| 9910 | target_ulong sigsetsize = arg5; |
| 9911 | #else |
| 9912 | target_ulong sigsetsize = arg4; |
Richard Henderson | 02fb28e | 2021-04-22 16:02:23 -0700 | [diff] [blame] | 9913 | target_ulong restorer = 0; |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 9914 | #endif |
Richard Henderson | fb80439 | 2021-04-22 16:02:27 -0700 | [diff] [blame] | 9915 | struct target_sigaction *act = NULL; |
| 9916 | struct target_sigaction *oact = NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9917 | |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 9918 | if (sigsetsize != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9919 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 9920 | } |
Richard Henderson | fb80439 | 2021-04-22 16:02:27 -0700 | [diff] [blame] | 9921 | if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) { |
| 9922 | return -TARGET_EFAULT; |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 9923 | } |
Richard Henderson | fb80439 | 2021-04-22 16:02:27 -0700 | [diff] [blame] | 9924 | if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) { |
| 9925 | ret = -TARGET_EFAULT; |
| 9926 | } else { |
| 9927 | ret = get_errno(do_sigaction(arg1, act, oact, restorer)); |
| 9928 | if (oact) { |
| 9929 | unlock_user_struct(oact, arg3, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9930 | } |
Richard Henderson | fb80439 | 2021-04-22 16:02:27 -0700 | [diff] [blame] | 9931 | } |
| 9932 | if (act) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9933 | unlock_user_struct(act, arg2, 0); |
Richard Henderson | fb80439 | 2021-04-22 16:02:27 -0700 | [diff] [blame] | 9934 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9935 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9936 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9937 | #ifdef TARGET_NR_sgetmask /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9938 | case TARGET_NR_sgetmask: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9939 | { |
| 9940 | sigset_t cur_set; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9941 | abi_ulong target_set; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 9942 | ret = do_sigprocmask(0, NULL, &cur_set); |
| 9943 | if (!ret) { |
| 9944 | host_to_target_old_sigset(&target_set, &cur_set); |
| 9945 | ret = target_set; |
| 9946 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9947 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9948 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9949 | #endif |
| 9950 | #ifdef TARGET_NR_ssetmask /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9951 | case TARGET_NR_ssetmask: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9952 | { |
Miloš Stojanović | a8617d8 | 2017-05-15 16:59:43 +0200 | [diff] [blame] | 9953 | sigset_t set, oset; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9954 | abi_ulong target_set = arg1; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9955 | target_to_host_old_sigset(&set, &target_set); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 9956 | ret = do_sigprocmask(SIG_SETMASK, &set, &oset); |
| 9957 | if (!ret) { |
| 9958 | host_to_target_old_sigset(&target_set, &oset); |
| 9959 | ret = target_set; |
| 9960 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9961 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9962 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9963 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9964 | #ifdef TARGET_NR_sigprocmask |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9965 | case TARGET_NR_sigprocmask: |
| 9966 | { |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 9967 | #if defined(TARGET_ALPHA) |
| 9968 | sigset_t set, oldset; |
| 9969 | abi_ulong mask; |
| 9970 | int how; |
| 9971 | |
| 9972 | switch (arg1) { |
| 9973 | case TARGET_SIG_BLOCK: |
| 9974 | how = SIG_BLOCK; |
| 9975 | break; |
| 9976 | case TARGET_SIG_UNBLOCK: |
| 9977 | how = SIG_UNBLOCK; |
| 9978 | break; |
| 9979 | case TARGET_SIG_SETMASK: |
| 9980 | how = SIG_SETMASK; |
| 9981 | break; |
| 9982 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9983 | return -TARGET_EINVAL; |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 9984 | } |
| 9985 | mask = arg2; |
| 9986 | target_to_host_old_sigset(&set, &mask); |
| 9987 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 9988 | ret = do_sigprocmask(how, &set, &oldset); |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 9989 | if (!is_error(ret)) { |
| 9990 | host_to_target_old_sigset(&mask, &oldset); |
| 9991 | ret = mask; |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 9992 | cpu_env->ir[IR_V0] = 0; /* force no error */ |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 9993 | } |
| 9994 | #else |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9995 | sigset_t set, oldset, *set_ptr; |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 9996 | int how; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9997 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9998 | if (arg2) { |
Patrick Venture | ebce171 | 2022-01-26 13:25:59 -0800 | [diff] [blame] | 9999 | p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1); |
| 10000 | if (!p) { |
| 10001 | return -TARGET_EFAULT; |
| 10002 | } |
| 10003 | target_to_host_old_sigset(&set, p); |
| 10004 | unlock_user(p, arg2, 0); |
| 10005 | set_ptr = &set; |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 10006 | switch (arg1) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10007 | case TARGET_SIG_BLOCK: |
| 10008 | how = SIG_BLOCK; |
| 10009 | break; |
| 10010 | case TARGET_SIG_UNBLOCK: |
| 10011 | how = SIG_UNBLOCK; |
| 10012 | break; |
| 10013 | case TARGET_SIG_SETMASK: |
| 10014 | how = SIG_SETMASK; |
| 10015 | break; |
| 10016 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 10017 | return -TARGET_EINVAL; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10018 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10019 | } else { |
| 10020 | how = 0; |
| 10021 | set_ptr = NULL; |
| 10022 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10023 | ret = do_sigprocmask(how, set_ptr, &oldset); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10024 | if (!is_error(ret) && arg3) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10025 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10026 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10027 | host_to_target_old_sigset(p, &oldset); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10028 | unlock_user(p, arg3, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10029 | } |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 10030 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10031 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10032 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10033 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10034 | case TARGET_NR_rt_sigprocmask: |
| 10035 | { |
| 10036 | int how = arg1; |
| 10037 | sigset_t set, oldset, *set_ptr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10038 | |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10039 | if (arg4 != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10040 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10041 | } |
| 10042 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10043 | if (arg2) { |
Shu-Chun Weng | d3ced2a | 2022-01-26 13:25:58 -0800 | [diff] [blame] | 10044 | p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1); |
| 10045 | if (!p) { |
| 10046 | return -TARGET_EFAULT; |
| 10047 | } |
| 10048 | target_to_host_sigset(&set, p); |
| 10049 | unlock_user(p, arg2, 0); |
| 10050 | set_ptr = &set; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10051 | switch(how) { |
| 10052 | case TARGET_SIG_BLOCK: |
| 10053 | how = SIG_BLOCK; |
| 10054 | break; |
| 10055 | case TARGET_SIG_UNBLOCK: |
| 10056 | how = SIG_UNBLOCK; |
| 10057 | break; |
| 10058 | case TARGET_SIG_SETMASK: |
| 10059 | how = SIG_SETMASK; |
| 10060 | break; |
| 10061 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 10062 | return -TARGET_EINVAL; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10063 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10064 | } else { |
| 10065 | how = 0; |
| 10066 | set_ptr = NULL; |
| 10067 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10068 | ret = do_sigprocmask(how, set_ptr, &oldset); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10069 | if (!is_error(ret) && arg3) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10070 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10071 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10072 | host_to_target_sigset(p, &oldset); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10073 | unlock_user(p, arg3, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10074 | } |
| 10075 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10076 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10077 | #ifdef TARGET_NR_sigpending |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10078 | case TARGET_NR_sigpending: |
| 10079 | { |
| 10080 | sigset_t set; |
| 10081 | ret = get_errno(sigpending(&set)); |
| 10082 | if (!is_error(ret)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10083 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10084 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10085 | host_to_target_old_sigset(p, &set); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10086 | unlock_user(p, arg1, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10087 | } |
| 10088 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10089 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10090 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10091 | case TARGET_NR_rt_sigpending: |
| 10092 | { |
| 10093 | sigset_t set; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10094 | |
| 10095 | /* Yes, this check is >, not != like most. We follow the kernel's |
| 10096 | * logic and it does it like this because it implements |
| 10097 | * NR_sigpending through the same code path, and in that case |
| 10098 | * the old_sigset_t is smaller in size. |
| 10099 | */ |
| 10100 | if (arg2 > sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10101 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10102 | } |
| 10103 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10104 | ret = get_errno(sigpending(&set)); |
| 10105 | if (!is_error(ret)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10106 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10107 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10108 | host_to_target_sigset(p, &set); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10109 | unlock_user(p, arg1, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10110 | } |
| 10111 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10112 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10113 | #ifdef TARGET_NR_sigsuspend |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10114 | case TARGET_NR_sigsuspend: |
| 10115 | { |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10116 | sigset_t *set; |
| 10117 | |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 10118 | #if defined(TARGET_ALPHA) |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 10119 | TaskState *ts = get_task_state(cpu); |
Richard Henderson | 7fb5ef3 | 2022-03-15 01:43:04 -0700 | [diff] [blame] | 10120 | /* target_to_host_old_sigset will bswap back */ |
| 10121 | abi_ulong mask = tswapal(arg1); |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10122 | set = &ts->sigsuspend_mask; |
| 10123 | target_to_host_old_sigset(set, &mask); |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 10124 | #else |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10125 | ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t)); |
| 10126 | if (ret != 0) { |
| 10127 | return ret; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10128 | } |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10129 | #endif |
| 10130 | ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE)); |
| 10131 | finish_sigsuspend_mask(ret); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10132 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10133 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10134 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10135 | case TARGET_NR_rt_sigsuspend: |
| 10136 | { |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10137 | sigset_t *set; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10138 | |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10139 | ret = process_sigsuspend_mask(&set, arg1, arg2); |
| 10140 | if (ret != 0) { |
| 10141 | return ret; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10142 | } |
Richard Henderson | 0a99f09 | 2022-03-15 01:43:05 -0700 | [diff] [blame] | 10143 | ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE)); |
| 10144 | finish_sigsuspend_mask(ret); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10145 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10146 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10147 | #ifdef TARGET_NR_rt_sigtimedwait |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10148 | case TARGET_NR_rt_sigtimedwait: |
| 10149 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10150 | sigset_t set; |
| 10151 | struct timespec uts, *puts; |
| 10152 | siginfo_t uinfo; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10153 | |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10154 | if (arg4 != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10155 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10156 | } |
| 10157 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 10158 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10159 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10160 | target_to_host_sigset(&set, p); |
| 10161 | unlock_user(p, arg1, 0); |
| 10162 | if (arg3) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10163 | puts = &uts; |
Filip Bozuta | 4d21300 | 2020-07-24 20:16:51 +0200 | [diff] [blame] | 10164 | if (target_to_host_timespec(puts, arg3)) { |
| 10165 | return -TARGET_EFAULT; |
| 10166 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10167 | } else { |
| 10168 | puts = NULL; |
| 10169 | } |
Peter Maydell | b3f8233 | 2016-06-06 19:58:08 +0100 | [diff] [blame] | 10170 | ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts, |
| 10171 | SIGSET_T_SIZE)); |
Petar Jovanovic | 974a196 | 2014-03-03 15:07:41 +0100 | [diff] [blame] | 10172 | if (!is_error(ret)) { |
| 10173 | if (arg2) { |
| 10174 | p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), |
| 10175 | 0); |
| 10176 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10177 | return -TARGET_EFAULT; |
Petar Jovanovic | 974a196 | 2014-03-03 15:07:41 +0100 | [diff] [blame] | 10178 | } |
| 10179 | host_to_target_siginfo(p, &uinfo); |
| 10180 | unlock_user(p, arg2, sizeof(target_siginfo_t)); |
| 10181 | } |
| 10182 | ret = host_to_target_signal(ret); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10183 | } |
| 10184 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10185 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10186 | #endif |
Filip Bozuta | ddcbde1 | 2020-08-24 21:21:16 +0200 | [diff] [blame] | 10187 | #ifdef TARGET_NR_rt_sigtimedwait_time64 |
| 10188 | case TARGET_NR_rt_sigtimedwait_time64: |
| 10189 | { |
| 10190 | sigset_t set; |
| 10191 | struct timespec uts, *puts; |
| 10192 | siginfo_t uinfo; |
| 10193 | |
| 10194 | if (arg4 != sizeof(target_sigset_t)) { |
| 10195 | return -TARGET_EINVAL; |
| 10196 | } |
| 10197 | |
| 10198 | p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1); |
| 10199 | if (!p) { |
| 10200 | return -TARGET_EFAULT; |
| 10201 | } |
| 10202 | target_to_host_sigset(&set, p); |
| 10203 | unlock_user(p, arg1, 0); |
| 10204 | if (arg3) { |
| 10205 | puts = &uts; |
| 10206 | if (target_to_host_timespec64(puts, arg3)) { |
| 10207 | return -TARGET_EFAULT; |
| 10208 | } |
| 10209 | } else { |
| 10210 | puts = NULL; |
| 10211 | } |
| 10212 | ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts, |
| 10213 | SIGSET_T_SIZE)); |
| 10214 | if (!is_error(ret)) { |
| 10215 | if (arg2) { |
| 10216 | p = lock_user(VERIFY_WRITE, arg2, |
| 10217 | sizeof(target_siginfo_t), 0); |
| 10218 | if (!p) { |
| 10219 | return -TARGET_EFAULT; |
| 10220 | } |
| 10221 | host_to_target_siginfo(p, &uinfo); |
| 10222 | unlock_user(p, arg2, sizeof(target_siginfo_t)); |
| 10223 | } |
| 10224 | ret = host_to_target_signal(ret); |
| 10225 | } |
| 10226 | } |
| 10227 | return ret; |
| 10228 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10229 | case TARGET_NR_rt_sigqueueinfo: |
| 10230 | { |
| 10231 | siginfo_t uinfo; |
Peter Maydell | 4debae6 | 2016-06-20 15:50:36 +0100 | [diff] [blame] | 10232 | |
| 10233 | p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1); |
| 10234 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10235 | return -TARGET_EFAULT; |
Peter Maydell | 4debae6 | 2016-06-20 15:50:36 +0100 | [diff] [blame] | 10236 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10237 | target_to_host_siginfo(&uinfo, p); |
Miloš Stojanović | d8b6d89 | 2017-05-15 16:59:44 +0200 | [diff] [blame] | 10238 | unlock_user(p, arg3, 0); |
fanwenjie | 9b9145f | 2022-08-31 11:55:25 +0800 | [diff] [blame] | 10239 | ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10240 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10241 | return ret; |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 10242 | case TARGET_NR_rt_tgsigqueueinfo: |
| 10243 | { |
| 10244 | siginfo_t uinfo; |
| 10245 | |
| 10246 | p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1); |
| 10247 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10248 | return -TARGET_EFAULT; |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 10249 | } |
| 10250 | target_to_host_siginfo(&uinfo, p); |
| 10251 | unlock_user(p, arg4, 0); |
fanwenjie | 9b9145f | 2022-08-31 11:55:25 +0800 | [diff] [blame] | 10252 | ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo)); |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 10253 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10254 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10255 | #ifdef TARGET_NR_sigreturn |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10256 | case TARGET_NR_sigreturn: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10257 | if (block_signals()) { |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 10258 | return -QEMU_ERESTARTSYS; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10259 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10260 | return do_sigreturn(cpu_env); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10261 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10262 | case TARGET_NR_rt_sigreturn: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10263 | if (block_signals()) { |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 10264 | return -QEMU_ERESTARTSYS; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 10265 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10266 | return do_rt_sigreturn(cpu_env); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10267 | case TARGET_NR_sethostname: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10268 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10269 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10270 | ret = get_errno(sethostname(p, arg2)); |
| 10271 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10272 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10273 | #ifdef TARGET_NR_setrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10274 | case TARGET_NR_setrlimit: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10275 | { |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 10276 | int resource = target_to_host_resource(arg1); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10277 | struct target_rlimit *target_rlim; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10278 | struct rlimit rlim; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10279 | if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10280 | return -TARGET_EFAULT; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 10281 | rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur); |
| 10282 | rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10283 | unlock_user_struct(target_rlim, arg2, 0); |
Max Filippov | 5dfa88f | 2018-09-17 11:13:14 -0700 | [diff] [blame] | 10284 | /* |
| 10285 | * If we just passed through resource limit settings for memory then |
| 10286 | * they would also apply to QEMU's own allocations, and QEMU will |
| 10287 | * crash or hang or die if its allocations fail. Ideally we would |
| 10288 | * track the guest allocations in QEMU and apply the limits ourselves. |
| 10289 | * For now, just tell the guest the call succeeded but don't actually |
| 10290 | * limit anything. |
| 10291 | */ |
| 10292 | if (resource != RLIMIT_AS && |
| 10293 | resource != RLIMIT_DATA && |
| 10294 | resource != RLIMIT_STACK) { |
| 10295 | return get_errno(setrlimit(resource, &rlim)); |
| 10296 | } else { |
| 10297 | return 0; |
| 10298 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10299 | } |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10300 | #endif |
| 10301 | #ifdef TARGET_NR_getrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10302 | case TARGET_NR_getrlimit: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10303 | { |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 10304 | int resource = target_to_host_resource(arg1); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10305 | struct target_rlimit *target_rlim; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10306 | struct rlimit rlim; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10307 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10308 | ret = get_errno(getrlimit(resource, &rlim)); |
| 10309 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10310 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10311 | return -TARGET_EFAULT; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 10312 | target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); |
| 10313 | target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10314 | unlock_user_struct(target_rlim, arg2, 1); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10315 | } |
| 10316 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10317 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10318 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10319 | case TARGET_NR_getrusage: |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 10320 | { |
| 10321 | struct rusage rusage; |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 10322 | ret = get_errno(getrusage(arg1, &rusage)); |
| 10323 | if (!is_error(ret)) { |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 10324 | ret = host_to_target_rusage(arg2, &rusage); |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 10325 | } |
| 10326 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10327 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10328 | #if defined(TARGET_NR_gettimeofday) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10329 | case TARGET_NR_gettimeofday: |
| 10330 | { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10331 | struct timeval tv; |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 10332 | struct timezone tz; |
| 10333 | |
| 10334 | ret = get_errno(gettimeofday(&tv, &tz)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10335 | if (!is_error(ret)) { |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 10336 | if (arg1 && copy_to_user_timeval(arg1, &tv)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10337 | return -TARGET_EFAULT; |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 10338 | } |
| 10339 | if (arg2 && copy_to_user_timezone(arg2, &tz)) { |
| 10340 | return -TARGET_EFAULT; |
| 10341 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10342 | } |
| 10343 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10344 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10345 | #endif |
| 10346 | #if defined(TARGET_NR_settimeofday) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10347 | case TARGET_NR_settimeofday: |
| 10348 | { |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 10349 | struct timeval tv, *ptv = NULL; |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 10350 | struct timezone tz, *ptz = NULL; |
| 10351 | |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 10352 | if (arg1) { |
| 10353 | if (copy_from_user_timeval(&tv, arg1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10354 | return -TARGET_EFAULT; |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 10355 | } |
| 10356 | ptv = &tv; |
| 10357 | } |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 10358 | |
| 10359 | if (arg2) { |
| 10360 | if (copy_from_user_timezone(&tz, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10361 | return -TARGET_EFAULT; |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 10362 | } |
| 10363 | ptz = &tz; |
| 10364 | } |
| 10365 | |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10366 | return get_errno(settimeofday(ptv, ptz)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10367 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10368 | #endif |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 10369 | #if defined(TARGET_NR_select) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10370 | case TARGET_NR_select: |
Laurent Vivier | 5457dc9 | 2016-07-08 01:17:27 +0200 | [diff] [blame] | 10371 | #if defined(TARGET_WANT_NI_OLD_SELECT) |
| 10372 | /* some architectures used to have old_select here |
| 10373 | * but now ENOSYS it. |
| 10374 | */ |
| 10375 | ret = -TARGET_ENOSYS; |
| 10376 | #elif defined(TARGET_WANT_OLD_SYS_SELECT) |
| 10377 | ret = do_old_select(arg1); |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 10378 | #else |
Laurent Vivier | 5457dc9 | 2016-07-08 01:17:27 +0200 | [diff] [blame] | 10379 | ret = do_select(arg1, arg2, arg3, arg4, arg5); |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 10380 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10381 | return ret; |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 10382 | #endif |
Riku Voipio | 9e42382 | 2010-05-07 12:28:05 +0000 | [diff] [blame] | 10383 | #ifdef TARGET_NR_pselect6 |
| 10384 | case TARGET_NR_pselect6: |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 10385 | return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); |
| 10386 | #endif |
| 10387 | #ifdef TARGET_NR_pselect6_time64 |
| 10388 | case TARGET_NR_pselect6_time64: |
| 10389 | return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); |
Riku Voipio | 9e42382 | 2010-05-07 12:28:05 +0000 | [diff] [blame] | 10390 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10391 | #ifdef TARGET_NR_symlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10392 | case TARGET_NR_symlink: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10393 | { |
| 10394 | void *p2; |
| 10395 | p = lock_user_string(arg1); |
| 10396 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10397 | if (!p || !p2) |
| 10398 | ret = -TARGET_EFAULT; |
| 10399 | else |
| 10400 | ret = get_errno(symlink(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10401 | unlock_user(p2, arg2, 0); |
| 10402 | unlock_user(p, arg1, 0); |
| 10403 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10404 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10405 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10406 | #if defined(TARGET_NR_symlinkat) |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 10407 | case TARGET_NR_symlinkat: |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 10408 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10409 | void *p2; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 10410 | p = lock_user_string(arg1); |
| 10411 | p2 = lock_user_string(arg3); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10412 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 10413 | ret = -TARGET_EFAULT; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 10414 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10415 | ret = get_errno(symlinkat(p, arg2, p2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10416 | unlock_user(p2, arg3, 0); |
| 10417 | unlock_user(p, arg1, 0); |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 10418 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10419 | return ret; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 10420 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10421 | #ifdef TARGET_NR_readlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10422 | case TARGET_NR_readlink: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10423 | { |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 10424 | void *p2; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10425 | p = lock_user_string(arg1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10426 | p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
Ilya Leoshkevich | a4dab0a | 2023-06-30 19:04:16 +0100 | [diff] [blame] | 10427 | ret = get_errno(do_guest_readlink(p, p2, arg3)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10428 | unlock_user(p2, arg2, ret); |
| 10429 | unlock_user(p, arg1, 0); |
| 10430 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10431 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10432 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10433 | #if defined(TARGET_NR_readlinkat) |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 10434 | case TARGET_NR_readlinkat: |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 10435 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10436 | void *p2; |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 10437 | p = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10438 | p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 10439 | if (!p || !p2) { |
| 10440 | ret = -TARGET_EFAULT; |
Jameson Nash | 65d4830 | 2022-08-08 15:07:27 -0400 | [diff] [blame] | 10441 | } else if (!arg4) { |
| 10442 | /* Short circuit this for the magic exe check. */ |
| 10443 | ret = -TARGET_EINVAL; |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 10444 | } else if (is_proc_myself((const char *)p, "exe")) { |
Helge Deller | 258bec3 | 2022-12-05 12:38:25 +0100 | [diff] [blame] | 10445 | /* |
| 10446 | * Don't worry about sign mismatch as earlier mapping |
| 10447 | * logic would have thrown a bad address error. |
| 10448 | */ |
| 10449 | ret = MIN(strlen(exec_path), arg4); |
| 10450 | /* We cannot NUL terminate the string. */ |
| 10451 | memcpy(p2, exec_path, ret); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 10452 | } else { |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10453 | ret = get_errno(readlinkat(arg1, path(p), p2, arg4)); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 10454 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10455 | unlock_user(p2, arg3, ret); |
| 10456 | unlock_user(p, arg2, 0); |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 10457 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10458 | return ret; |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 10459 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10460 | #ifdef TARGET_NR_swapon |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10461 | case TARGET_NR_swapon: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10462 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10463 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10464 | ret = get_errno(swapon(p, arg2)); |
| 10465 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10466 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10467 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10468 | case TARGET_NR_reboot: |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 10469 | if (arg3 == LINUX_REBOOT_CMD_RESTART2) { |
| 10470 | /* arg4 must be ignored in all other cases */ |
| 10471 | p = lock_user_string(arg4); |
| 10472 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10473 | return -TARGET_EFAULT; |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 10474 | } |
| 10475 | ret = get_errno(reboot(arg1, arg2, arg3, p)); |
| 10476 | unlock_user(p, arg4, 0); |
| 10477 | } else { |
| 10478 | ret = get_errno(reboot(arg1, arg2, arg3, NULL)); |
| 10479 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10480 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10481 | #ifdef TARGET_NR_mmap |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10482 | case TARGET_NR_mmap: |
Alexander Graf | 0970119 | 2013-09-03 20:12:15 +0100 | [diff] [blame] | 10483 | #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \ |
| 10484 | (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \ |
Philippe Mathieu-Daudé | bff4b02 | 2024-07-24 13:47:57 +0200 | [diff] [blame] | 10485 | defined(TARGET_M68K) || defined(TARGET_MICROBLAZE) \ |
Ulrich Hecht | a4c075f | 2009-07-24 16:57:31 +0200 | [diff] [blame] | 10486 | || defined(TARGET_S390X) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10487 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 10488 | abi_ulong *v; |
| 10489 | abi_ulong v1, v2, v3, v4, v5, v6; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10490 | if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10491 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 10492 | v1 = tswapal(v[0]); |
| 10493 | v2 = tswapal(v[1]); |
| 10494 | v3 = tswapal(v[2]); |
| 10495 | v4 = tswapal(v[3]); |
| 10496 | v5 = tswapal(v[4]); |
| 10497 | v6 = tswapal(v[5]); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10498 | unlock_user(v, arg1, 0); |
Richard Henderson | 9ab8d071 | 2023-08-07 18:22:35 -0700 | [diff] [blame] | 10499 | return do_mmap(v1, v2, v3, v4, v5, v6); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10500 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10501 | #else |
Richard Henderson | ee1bf83 | 2021-02-12 10:48:44 -0800 | [diff] [blame] | 10502 | /* mmap pointers are always untagged */ |
Richard Henderson | 9ab8d071 | 2023-08-07 18:22:35 -0700 | [diff] [blame] | 10503 | return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10504 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10505 | #endif |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10506 | #ifdef TARGET_NR_mmap2 |
bellard | 6fb883e | 2003-07-09 17:12:39 +0000 | [diff] [blame] | 10507 | case TARGET_NR_mmap2: |
pbrook | bb7ec04 | 2008-03-25 22:28:25 +0000 | [diff] [blame] | 10508 | #ifndef MMAP_SHIFT |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 10509 | #define MMAP_SHIFT 12 |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 10510 | #endif |
Richard Henderson | 9ab8d071 | 2023-08-07 18:22:35 -0700 | [diff] [blame] | 10511 | return do_mmap(arg1, arg2, arg3, arg4, arg5, |
| 10512 | (off_t)(abi_ulong)arg6 << MMAP_SHIFT); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10513 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10514 | case TARGET_NR_munmap: |
Richard Henderson | ee1bf83 | 2021-02-12 10:48:44 -0800 | [diff] [blame] | 10515 | arg1 = cpu_untagged_addr(cpu, arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10516 | return get_errno(target_munmap(arg1, arg2)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10517 | case TARGET_NR_mprotect: |
Richard Henderson | ee1bf83 | 2021-02-12 10:48:44 -0800 | [diff] [blame] | 10518 | arg1 = cpu_untagged_addr(cpu, arg1); |
Paul Brook | 97374d3 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 10519 | { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 10520 | TaskState *ts = get_task_state(cpu); |
Paul Brook | 97374d3 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 10521 | /* Special hack to detect libc making the stack executable. */ |
| 10522 | if ((arg3 & PROT_GROWSDOWN) |
| 10523 | && arg1 >= ts->info->stack_limit |
| 10524 | && arg1 <= ts->info->start_stack) { |
| 10525 | arg3 &= ~PROT_GROWSDOWN; |
| 10526 | arg2 = arg2 + arg1 - ts->info->stack_limit; |
| 10527 | arg1 = ts->info->stack_limit; |
| 10528 | } |
| 10529 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10530 | return get_errno(target_mprotect(arg1, arg2, arg3)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10531 | #ifdef TARGET_NR_mremap |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10532 | case TARGET_NR_mremap: |
Richard Henderson | ee1bf83 | 2021-02-12 10:48:44 -0800 | [diff] [blame] | 10533 | arg1 = cpu_untagged_addr(cpu, arg1); |
| 10534 | /* mremap new_addr (arg5) is always untagged */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10535 | return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10536 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10537 | /* ??? msync/mlock/munlock are broken for softmmu. */ |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10538 | #ifdef TARGET_NR_msync |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10539 | case TARGET_NR_msync: |
Helge Deller | fe08059 | 2022-12-15 08:27:46 +0100 | [diff] [blame] | 10540 | return get_errno(msync(g2h(cpu, arg1), arg2, |
| 10541 | target_to_host_msync_arg(arg3))); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10542 | #endif |
| 10543 | #ifdef TARGET_NR_mlock |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10544 | case TARGET_NR_mlock: |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 10545 | return get_errno(mlock(g2h(cpu, arg1), arg2)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10546 | #endif |
| 10547 | #ifdef TARGET_NR_munlock |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10548 | case TARGET_NR_munlock: |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 10549 | return get_errno(munlock(g2h(cpu, arg1), arg2)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10550 | #endif |
| 10551 | #ifdef TARGET_NR_mlockall |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10552 | case TARGET_NR_mlockall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10553 | return get_errno(mlockall(target_to_host_mlockall_arg(arg1))); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10554 | #endif |
| 10555 | #ifdef TARGET_NR_munlockall |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10556 | case TARGET_NR_munlockall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10557 | return get_errno(munlockall()); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10558 | #endif |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10559 | #ifdef TARGET_NR_truncate |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10560 | case TARGET_NR_truncate: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10561 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10562 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10563 | ret = get_errno(truncate(p, arg2)); |
| 10564 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10565 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10566 | #endif |
| 10567 | #ifdef TARGET_NR_ftruncate |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10568 | case TARGET_NR_ftruncate: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10569 | return get_errno(ftruncate(arg1, arg2)); |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10570 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10571 | case TARGET_NR_fchmod: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10572 | return get_errno(fchmod(arg1, arg2)); |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10573 | #if defined(TARGET_NR_fchmodat) |
ths | 814d797 | 2007-09-24 09:26:51 +0000 | [diff] [blame] | 10574 | case TARGET_NR_fchmodat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10575 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10576 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10577 | ret = get_errno(fchmodat(arg1, p, arg3, 0)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10578 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10579 | return ret; |
ths | 814d797 | 2007-09-24 09:26:51 +0000 | [diff] [blame] | 10580 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10581 | case TARGET_NR_getpriority: |
Richard Henderson | 95c0982 | 2012-06-07 15:14:50 -0700 | [diff] [blame] | 10582 | /* Note that negative values are valid for getpriority, so we must |
| 10583 | differentiate based on errno settings. */ |
| 10584 | errno = 0; |
| 10585 | ret = getpriority(arg1, arg2); |
| 10586 | if (ret == -1 && errno != 0) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10587 | return -host_to_target_errno(errno); |
Richard Henderson | 95c0982 | 2012-06-07 15:14:50 -0700 | [diff] [blame] | 10588 | } |
| 10589 | #ifdef TARGET_ALPHA |
| 10590 | /* Return value is the unbiased priority. Signal no error. */ |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 10591 | cpu_env->ir[IR_V0] = 0; |
Richard Henderson | 95c0982 | 2012-06-07 15:14:50 -0700 | [diff] [blame] | 10592 | #else |
| 10593 | /* Return value is a biased priority to avoid negative numbers. */ |
| 10594 | ret = 20 - ret; |
| 10595 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10596 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10597 | case TARGET_NR_setpriority: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10598 | return get_errno(setpriority(arg1, arg2, arg3)); |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10599 | #ifdef TARGET_NR_statfs |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10600 | case TARGET_NR_statfs: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10601 | if (!(p = lock_user_string(arg1))) { |
| 10602 | return -TARGET_EFAULT; |
| 10603 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10604 | ret = get_errno(statfs(path(p), &stfs)); |
| 10605 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10606 | convert_statfs: |
| 10607 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10608 | struct target_statfs *target_stfs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10609 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10610 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10611 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10612 | __put_user(stfs.f_type, &target_stfs->f_type); |
| 10613 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); |
| 10614 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); |
| 10615 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); |
| 10616 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); |
| 10617 | __put_user(stfs.f_files, &target_stfs->f_files); |
| 10618 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); |
| 10619 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); |
| 10620 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); |
| 10621 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 10622 | __put_user(stfs.f_frsize, &target_stfs->f_frsize); |
Shea Levy | d4247ec | 2018-03-01 06:15:00 -0500 | [diff] [blame] | 10623 | #ifdef _STATFS_F_FLAGS |
| 10624 | __put_user(stfs.f_flags, &target_stfs->f_flags); |
| 10625 | #else |
| 10626 | __put_user(0, &target_stfs->f_flags); |
| 10627 | #endif |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 10628 | memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10629 | unlock_user_struct(target_stfs, arg2, 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10630 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10631 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10632 | #endif |
| 10633 | #ifdef TARGET_NR_fstatfs |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10634 | case TARGET_NR_fstatfs: |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 10635 | ret = get_errno(fstatfs(arg1, &stfs)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10636 | goto convert_statfs; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10637 | #endif |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 10638 | #ifdef TARGET_NR_statfs64 |
| 10639 | case TARGET_NR_statfs64: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10640 | if (!(p = lock_user_string(arg1))) { |
| 10641 | return -TARGET_EFAULT; |
| 10642 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10643 | ret = get_errno(statfs(path(p), &stfs)); |
| 10644 | unlock_user(p, arg1, 0); |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 10645 | convert_statfs64: |
| 10646 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10647 | struct target_statfs64 *target_stfs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10648 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10649 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10650 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10651 | __put_user(stfs.f_type, &target_stfs->f_type); |
| 10652 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); |
| 10653 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); |
| 10654 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); |
| 10655 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); |
| 10656 | __put_user(stfs.f_files, &target_stfs->f_files); |
| 10657 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); |
| 10658 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); |
| 10659 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); |
| 10660 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 10661 | __put_user(stfs.f_frsize, &target_stfs->f_frsize); |
Frajo | b94e2b4 | 2020-01-30 15:00:30 +0200 | [diff] [blame] | 10662 | #ifdef _STATFS_F_FLAGS |
| 10663 | __put_user(stfs.f_flags, &target_stfs->f_flags); |
| 10664 | #else |
| 10665 | __put_user(0, &target_stfs->f_flags); |
| 10666 | #endif |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 10667 | memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10668 | unlock_user_struct(target_stfs, arg3, 1); |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 10669 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10670 | return ret; |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 10671 | case TARGET_NR_fstatfs64: |
| 10672 | ret = get_errno(fstatfs(arg1, &stfs)); |
| 10673 | goto convert_statfs64; |
| 10674 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10675 | #ifdef TARGET_NR_socketcall |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10676 | case TARGET_NR_socketcall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10677 | return do_socketcall(arg1, arg2); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10678 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10679 | #ifdef TARGET_NR_accept |
| 10680 | case TARGET_NR_accept: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10681 | return do_accept4(arg1, arg2, arg3, 0); |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 10682 | #endif |
| 10683 | #ifdef TARGET_NR_accept4 |
| 10684 | case TARGET_NR_accept4: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10685 | return do_accept4(arg1, arg2, arg3, arg4); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10686 | #endif |
| 10687 | #ifdef TARGET_NR_bind |
| 10688 | case TARGET_NR_bind: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10689 | return do_bind(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10690 | #endif |
| 10691 | #ifdef TARGET_NR_connect |
| 10692 | case TARGET_NR_connect: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10693 | return do_connect(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10694 | #endif |
| 10695 | #ifdef TARGET_NR_getpeername |
| 10696 | case TARGET_NR_getpeername: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10697 | return do_getpeername(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10698 | #endif |
| 10699 | #ifdef TARGET_NR_getsockname |
| 10700 | case TARGET_NR_getsockname: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10701 | return do_getsockname(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10702 | #endif |
| 10703 | #ifdef TARGET_NR_getsockopt |
| 10704 | case TARGET_NR_getsockopt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10705 | return do_getsockopt(arg1, arg2, arg3, arg4, arg5); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10706 | #endif |
| 10707 | #ifdef TARGET_NR_listen |
| 10708 | case TARGET_NR_listen: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10709 | return get_errno(listen(arg1, arg2)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10710 | #endif |
| 10711 | #ifdef TARGET_NR_recv |
| 10712 | case TARGET_NR_recv: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10713 | return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10714 | #endif |
| 10715 | #ifdef TARGET_NR_recvfrom |
| 10716 | case TARGET_NR_recvfrom: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10717 | return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10718 | #endif |
| 10719 | #ifdef TARGET_NR_recvmsg |
| 10720 | case TARGET_NR_recvmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10721 | return do_sendrecvmsg(arg1, arg2, arg3, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10722 | #endif |
| 10723 | #ifdef TARGET_NR_send |
| 10724 | case TARGET_NR_send: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10725 | return do_sendto(arg1, arg2, arg3, arg4, 0, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10726 | #endif |
| 10727 | #ifdef TARGET_NR_sendmsg |
| 10728 | case TARGET_NR_sendmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10729 | return do_sendrecvmsg(arg1, arg2, arg3, 1); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10730 | #endif |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 10731 | #ifdef TARGET_NR_sendmmsg |
| 10732 | case TARGET_NR_sendmmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10733 | return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1); |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10734 | #endif |
| 10735 | #ifdef TARGET_NR_recvmmsg |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 10736 | case TARGET_NR_recvmmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10737 | return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0); |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 10738 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10739 | #ifdef TARGET_NR_sendto |
| 10740 | case TARGET_NR_sendto: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10741 | return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10742 | #endif |
| 10743 | #ifdef TARGET_NR_shutdown |
| 10744 | case TARGET_NR_shutdown: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10745 | return get_errno(shutdown(arg1, arg2)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10746 | #endif |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 10747 | #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) |
| 10748 | case TARGET_NR_getrandom: |
| 10749 | p = lock_user(VERIFY_WRITE, arg1, arg2, 0); |
| 10750 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10751 | return -TARGET_EFAULT; |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 10752 | } |
| 10753 | ret = get_errno(getrandom(p, arg2, arg3)); |
| 10754 | unlock_user(p, arg1, ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10755 | return ret; |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 10756 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10757 | #ifdef TARGET_NR_socket |
| 10758 | case TARGET_NR_socket: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10759 | return do_socket(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10760 | #endif |
| 10761 | #ifdef TARGET_NR_socketpair |
| 10762 | case TARGET_NR_socketpair: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10763 | return do_socketpair(arg1, arg2, arg3, arg4); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10764 | #endif |
| 10765 | #ifdef TARGET_NR_setsockopt |
| 10766 | case TARGET_NR_setsockopt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10767 | return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 10768 | #endif |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10769 | #if defined(TARGET_NR_syslog) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10770 | case TARGET_NR_syslog: |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10771 | { |
| 10772 | int len = arg2; |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 10773 | |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10774 | switch (arg1) { |
| 10775 | case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */ |
| 10776 | case TARGET_SYSLOG_ACTION_OPEN: /* Open log */ |
| 10777 | case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ |
| 10778 | case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */ |
| 10779 | case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */ |
| 10780 | case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */ |
| 10781 | case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */ |
| 10782 | case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10783 | return get_errno(sys_syslog((int)arg1, NULL, (int)arg3)); |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10784 | case TARGET_SYSLOG_ACTION_READ: /* Read from log */ |
| 10785 | case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */ |
| 10786 | case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */ |
| 10787 | { |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10788 | if (len < 0) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 10789 | return -TARGET_EINVAL; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10790 | } |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10791 | if (len == 0) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10792 | return 0; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10793 | } |
| 10794 | p = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 10795 | if (!p) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 10796 | return -TARGET_EFAULT; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10797 | } |
| 10798 | ret = get_errno(sys_syslog((int)arg1, p, (int)arg3)); |
| 10799 | unlock_user(p, arg2, arg3); |
| 10800 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10801 | return ret; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10802 | default: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10803 | return -TARGET_EINVAL; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 10804 | } |
| 10805 | } |
| 10806 | break; |
| 10807 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10808 | case TARGET_NR_setitimer: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10809 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10810 | struct itimerval value, ovalue, *pvalue; |
| 10811 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10812 | if (arg2) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10813 | pvalue = &value; |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 10814 | if (copy_from_user_timeval(&pvalue->it_interval, arg2) |
| 10815 | || copy_from_user_timeval(&pvalue->it_value, |
| 10816 | arg2 + sizeof(struct target_timeval))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10817 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10818 | } else { |
| 10819 | pvalue = NULL; |
| 10820 | } |
| 10821 | ret = get_errno(setitimer(arg1, pvalue, &ovalue)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10822 | if (!is_error(ret) && arg3) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 10823 | if (copy_to_user_timeval(arg3, |
| 10824 | &ovalue.it_interval) |
| 10825 | || copy_to_user_timeval(arg3 + sizeof(struct target_timeval), |
| 10826 | &ovalue.it_value)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10827 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10828 | } |
| 10829 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10830 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10831 | case TARGET_NR_getitimer: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10832 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10833 | struct itimerval value; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10834 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10835 | ret = get_errno(getitimer(arg1, &value)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10836 | if (!is_error(ret) && arg2) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 10837 | if (copy_to_user_timeval(arg2, |
| 10838 | &value.it_interval) |
| 10839 | || copy_to_user_timeval(arg2 + sizeof(struct target_timeval), |
| 10840 | &value.it_value)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10841 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 10842 | } |
| 10843 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10844 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10845 | #ifdef TARGET_NR_stat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10846 | case TARGET_NR_stat: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10847 | if (!(p = lock_user_string(arg1))) { |
| 10848 | return -TARGET_EFAULT; |
| 10849 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10850 | ret = get_errno(stat(path(p), &st)); |
| 10851 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10852 | goto do_stat; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10853 | #endif |
| 10854 | #ifdef TARGET_NR_lstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10855 | case TARGET_NR_lstat: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10856 | if (!(p = lock_user_string(arg1))) { |
| 10857 | return -TARGET_EFAULT; |
| 10858 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10859 | ret = get_errno(lstat(path(p), &st)); |
| 10860 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10861 | goto do_stat; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10862 | #endif |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10863 | #ifdef TARGET_NR_fstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10864 | case TARGET_NR_fstat: |
| 10865 | { |
| 10866 | ret = get_errno(fstat(arg1, &st)); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10867 | #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10868 | do_stat: |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10869 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10870 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10871 | struct target_stat *target_st; |
ths | e358465 | 2007-06-01 11:49:38 +0000 | [diff] [blame] | 10872 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10873 | if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10874 | return -TARGET_EFAULT; |
Ulrich Hecht | 1272791 | 2009-07-24 19:10:32 +0200 | [diff] [blame] | 10875 | memset(target_st, 0, sizeof(*target_st)); |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 10876 | __put_user(st.st_dev, &target_st->st_dev); |
| 10877 | __put_user(st.st_ino, &target_st->st_ino); |
| 10878 | __put_user(st.st_mode, &target_st->st_mode); |
| 10879 | __put_user(st.st_uid, &target_st->st_uid); |
| 10880 | __put_user(st.st_gid, &target_st->st_gid); |
| 10881 | __put_user(st.st_nlink, &target_st->st_nlink); |
| 10882 | __put_user(st.st_rdev, &target_st->st_rdev); |
| 10883 | __put_user(st.st_size, &target_st->st_size); |
| 10884 | __put_user(st.st_blksize, &target_st->st_blksize); |
| 10885 | __put_user(st.st_blocks, &target_st->st_blocks); |
| 10886 | __put_user(st.st_atime, &target_st->target_st_atime); |
| 10887 | __put_user(st.st_mtime, &target_st->target_st_mtime); |
| 10888 | __put_user(st.st_ctime, &target_st->target_st_ctime); |
Michael Forney | febf6fa | 2021-05-25 20:55:31 -0700 | [diff] [blame] | 10889 | #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC) |
Chen-Yu Tsai | 5f992db | 2019-05-23 00:21:47 +0800 | [diff] [blame] | 10890 | __put_user(st.st_atim.tv_nsec, |
| 10891 | &target_st->target_st_atime_nsec); |
| 10892 | __put_user(st.st_mtim.tv_nsec, |
| 10893 | &target_st->target_st_mtime_nsec); |
| 10894 | __put_user(st.st_ctim.tv_nsec, |
| 10895 | &target_st->target_st_ctime_nsec); |
| 10896 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10897 | unlock_user_struct(target_st, arg2, 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10898 | } |
| 10899 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10900 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10901 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10902 | case TARGET_NR_vhangup: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10903 | return get_errno(vhangup()); |
bellard | 42ad6ae | 2005-01-03 22:48:11 +0000 | [diff] [blame] | 10904 | #ifdef TARGET_NR_syscall |
| 10905 | case TARGET_NR_syscall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10906 | return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5, |
| 10907 | arg6, arg7, arg8, 0); |
bellard | 42ad6ae | 2005-01-03 22:48:11 +0000 | [diff] [blame] | 10908 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10909 | #if defined(TARGET_NR_wait4) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10910 | case TARGET_NR_wait4: |
| 10911 | { |
| 10912 | int status; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 10913 | abi_long status_ptr = arg2; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10914 | struct rusage rusage, *rusage_ptr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 10915 | abi_ulong target_rusage = arg4; |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 10916 | abi_long rusage_err; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10917 | if (target_rusage) |
| 10918 | rusage_ptr = &rusage; |
| 10919 | else |
| 10920 | rusage_ptr = NULL; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 10921 | ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10922 | if (!is_error(ret)) { |
Alexander Graf | 5379557 | 2011-11-24 00:44:43 +0100 | [diff] [blame] | 10923 | if (status_ptr && ret) { |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 10924 | status = host_to_target_waitstatus(status); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 10925 | if (put_user_s32(status, status_ptr)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10926 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10927 | } |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 10928 | if (target_rusage) { |
| 10929 | rusage_err = host_to_target_rusage(target_rusage, &rusage); |
| 10930 | if (rusage_err) { |
| 10931 | ret = rusage_err; |
| 10932 | } |
| 10933 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10934 | } |
| 10935 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10936 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10937 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10938 | #ifdef TARGET_NR_swapoff |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10939 | case TARGET_NR_swapoff: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10940 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10941 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10942 | ret = get_errno(swapoff(p)); |
| 10943 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10944 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10945 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10946 | case TARGET_NR_sysinfo: |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 10947 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10948 | struct target_sysinfo *target_value; |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 10949 | struct sysinfo value; |
| 10950 | ret = get_errno(sysinfo(&value)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10951 | if (!is_error(ret) && arg1) |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 10952 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10953 | if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10954 | return -TARGET_EFAULT; |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 10955 | __put_user(value.uptime, &target_value->uptime); |
| 10956 | __put_user(value.loads[0], &target_value->loads[0]); |
| 10957 | __put_user(value.loads[1], &target_value->loads[1]); |
| 10958 | __put_user(value.loads[2], &target_value->loads[2]); |
| 10959 | __put_user(value.totalram, &target_value->totalram); |
| 10960 | __put_user(value.freeram, &target_value->freeram); |
| 10961 | __put_user(value.sharedram, &target_value->sharedram); |
| 10962 | __put_user(value.bufferram, &target_value->bufferram); |
| 10963 | __put_user(value.totalswap, &target_value->totalswap); |
| 10964 | __put_user(value.freeswap, &target_value->freeswap); |
| 10965 | __put_user(value.procs, &target_value->procs); |
| 10966 | __put_user(value.totalhigh, &target_value->totalhigh); |
| 10967 | __put_user(value.freehigh, &target_value->freehigh); |
| 10968 | __put_user(value.mem_unit, &target_value->mem_unit); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10969 | unlock_user_struct(target_value, arg1, 1); |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 10970 | } |
| 10971 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10972 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10973 | #ifdef TARGET_NR_ipc |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10974 | case TARGET_NR_ipc: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10975 | return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10976 | #endif |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 10977 | #ifdef TARGET_NR_semget |
| 10978 | case TARGET_NR_semget: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10979 | return get_errno(semget(arg1, arg2, arg3)); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 10980 | #endif |
| 10981 | #ifdef TARGET_NR_semop |
| 10982 | case TARGET_NR_semop: |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 10983 | return do_semtimedop(arg1, arg2, arg3, 0, false); |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame] | 10984 | #endif |
| 10985 | #ifdef TARGET_NR_semtimedop |
| 10986 | case TARGET_NR_semtimedop: |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 10987 | return do_semtimedop(arg1, arg2, arg3, arg4, false); |
| 10988 | #endif |
| 10989 | #ifdef TARGET_NR_semtimedop_time64 |
| 10990 | case TARGET_NR_semtimedop_time64: |
| 10991 | return do_semtimedop(arg1, arg2, arg3, arg4, true); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 10992 | #endif |
| 10993 | #ifdef TARGET_NR_semctl |
| 10994 | case TARGET_NR_semctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10995 | return do_semctl(arg1, arg2, arg3, arg4); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 10996 | #endif |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 10997 | #ifdef TARGET_NR_msgctl |
| 10998 | case TARGET_NR_msgctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10999 | return do_msgctl(arg1, arg2, arg3); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 11000 | #endif |
| 11001 | #ifdef TARGET_NR_msgget |
| 11002 | case TARGET_NR_msgget: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11003 | return get_errno(msgget(arg1, arg2)); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 11004 | #endif |
| 11005 | #ifdef TARGET_NR_msgrcv |
| 11006 | case TARGET_NR_msgrcv: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11007 | return do_msgrcv(arg1, arg2, arg3, arg4, arg5); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 11008 | #endif |
| 11009 | #ifdef TARGET_NR_msgsnd |
| 11010 | case TARGET_NR_msgsnd: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11011 | return do_msgsnd(arg1, arg2, arg3, arg4); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 11012 | #endif |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 11013 | #ifdef TARGET_NR_shmget |
| 11014 | case TARGET_NR_shmget: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11015 | return get_errno(shmget(arg1, arg2, arg3)); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 11016 | #endif |
| 11017 | #ifdef TARGET_NR_shmctl |
| 11018 | case TARGET_NR_shmctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11019 | return do_shmctl(arg1, arg2, arg3); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 11020 | #endif |
| 11021 | #ifdef TARGET_NR_shmat |
| 11022 | case TARGET_NR_shmat: |
Richard Henderson | 225a206 | 2023-08-20 09:24:14 -0700 | [diff] [blame] | 11023 | return target_shmat(cpu_env, arg1, arg2, arg3); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 11024 | #endif |
| 11025 | #ifdef TARGET_NR_shmdt |
| 11026 | case TARGET_NR_shmdt: |
Richard Henderson | 225a206 | 2023-08-20 09:24:14 -0700 | [diff] [blame] | 11027 | return target_shmdt(arg1); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 11028 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11029 | case TARGET_NR_fsync: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11030 | return get_errno(fsync(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11031 | case TARGET_NR_clone: |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 11032 | /* Linux manages to have three different orderings for its |
| 11033 | * arguments to clone(); the BACKWARDS and BACKWARDS2 defines |
| 11034 | * match the kernel's CONFIG_CLONE_* settings. |
| 11035 | * Microblaze is further special in that it uses a sixth |
| 11036 | * implicit argument to clone for the TLS pointer. |
| 11037 | */ |
| 11038 | #if defined(TARGET_MICROBLAZE) |
Edgar E. Iglesias | a5b3bdc | 2012-04-26 14:17:41 +0200 | [diff] [blame] | 11039 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5)); |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 11040 | #elif defined(TARGET_CLONE_BACKWARDS) |
| 11041 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5)); |
| 11042 | #elif defined(TARGET_CLONE_BACKWARDS2) |
Ulrich Hecht | a4c075f | 2009-07-24 16:57:31 +0200 | [diff] [blame] | 11043 | ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4)); |
aurel32 | 0b6d3ae | 2008-09-15 07:43:43 +0000 | [diff] [blame] | 11044 | #else |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 11045 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4)); |
aurel32 | 0b6d3ae | 2008-09-15 07:43:43 +0000 | [diff] [blame] | 11046 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11047 | return ret; |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 11048 | #ifdef __NR_exit_group |
| 11049 | /* new thread calls */ |
| 11050 | case TARGET_NR_exit_group: |
Alex Bennée | 708b6a6 | 2018-06-22 17:09:10 +0100 | [diff] [blame] | 11051 | preexit_cleanup(cpu_env, arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11052 | return get_errno(exit_group(arg1)); |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 11053 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11054 | case TARGET_NR_setdomainname: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11055 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11056 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11057 | ret = get_errno(setdomainname(p, arg2)); |
| 11058 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11059 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11060 | case TARGET_NR_uname: |
| 11061 | /* no need to transcode because we use the linux syscall */ |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 11062 | { |
| 11063 | struct new_utsname * buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 11064 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11065 | if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11066 | return -TARGET_EFAULT; |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 11067 | ret = get_errno(sys_uname(buf)); |
| 11068 | if (!is_error(ret)) { |
Peter Maydell | 332c978 | 2016-07-12 13:02:16 +0100 | [diff] [blame] | 11069 | /* Overwrite the native machine name with whatever is being |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 11070 | emulated. */ |
Philippe Mathieu-Daudé | 871f95c | 2017-07-24 15:27:47 -0300 | [diff] [blame] | 11071 | g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env), |
| 11072 | sizeof(buf->machine)); |
pbrook | c593722 | 2006-05-14 11:30:38 +0000 | [diff] [blame] | 11073 | /* Allow the user to override the reported release. */ |
Peter Maydell | 332c978 | 2016-07-12 13:02:16 +0100 | [diff] [blame] | 11074 | if (qemu_uname_release && *qemu_uname_release) { |
| 11075 | g_strlcpy(buf->release, qemu_uname_release, |
| 11076 | sizeof(buf->release)); |
| 11077 | } |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 11078 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11079 | unlock_user_struct(buf, arg1, 1); |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 11080 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11081 | return ret; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 11082 | #ifdef TARGET_I386 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11083 | case TARGET_NR_modify_ldt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11084 | return do_modify_ldt(cpu_env, arg1, arg2, arg3); |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 11085 | #if !defined(TARGET_X86_64) |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11086 | case TARGET_NR_vm86: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11087 | return do_vm86(cpu_env, arg1, arg2); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 11088 | #endif |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 11089 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11090 | #if defined(TARGET_NR_adjtimex) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11091 | case TARGET_NR_adjtimex: |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 11092 | { |
| 11093 | struct timex host_buf; |
| 11094 | |
| 11095 | if (target_to_host_timex(&host_buf, arg1) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11096 | return -TARGET_EFAULT; |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 11097 | } |
| 11098 | ret = get_errno(adjtimex(&host_buf)); |
| 11099 | if (!is_error(ret)) { |
| 11100 | if (host_to_target_timex(arg1, &host_buf) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11101 | return -TARGET_EFAULT; |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 11102 | } |
| 11103 | } |
| 11104 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11105 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11106 | #endif |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 11107 | #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME) |
| 11108 | case TARGET_NR_clock_adjtime: |
| 11109 | { |
Peter Maydell | aab7461 | 2023-07-17 11:05:07 +0100 | [diff] [blame] | 11110 | struct timex htx; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 11111 | |
Peter Maydell | aab7461 | 2023-07-17 11:05:07 +0100 | [diff] [blame] | 11112 | if (target_to_host_timex(&htx, arg2) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11113 | return -TARGET_EFAULT; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 11114 | } |
Peter Maydell | aab7461 | 2023-07-17 11:05:07 +0100 | [diff] [blame] | 11115 | ret = get_errno(clock_adjtime(arg1, &htx)); |
| 11116 | if (!is_error(ret) && host_to_target_timex(arg2, &htx)) { |
| 11117 | return -TARGET_EFAULT; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 11118 | } |
| 11119 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11120 | return ret; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 11121 | #endif |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 11122 | #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME) |
| 11123 | case TARGET_NR_clock_adjtime64: |
| 11124 | { |
| 11125 | struct timex htx; |
| 11126 | |
| 11127 | if (target_to_host_timex64(&htx, arg2) != 0) { |
| 11128 | return -TARGET_EFAULT; |
| 11129 | } |
| 11130 | ret = get_errno(clock_adjtime(arg1, &htx)); |
| 11131 | if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) { |
| 11132 | return -TARGET_EFAULT; |
| 11133 | } |
| 11134 | } |
| 11135 | return ret; |
| 11136 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11137 | case TARGET_NR_getpgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11138 | return get_errno(getpgid(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11139 | case TARGET_NR_fchdir: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11140 | return get_errno(fchdir(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11141 | case TARGET_NR_personality: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11142 | return get_errno(personality(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 11143 | #ifdef TARGET_NR__llseek /* Not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11144 | case TARGET_NR__llseek: |
| 11145 | { |
| 11146 | int64_t res; |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 11147 | #if !defined(__NR_llseek) |
Peter Maydell | 9fea273 | 2016-08-11 18:59:39 +0100 | [diff] [blame] | 11148 | res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5); |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 11149 | if (res == -1) { |
| 11150 | ret = get_errno(res); |
| 11151 | } else { |
| 11152 | ret = 0; |
| 11153 | } |
| 11154 | #else |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11155 | ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5)); |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 11156 | #endif |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 11157 | if ((ret == 0) && put_user_s64(res, arg4)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11158 | return -TARGET_EFAULT; |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 11159 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11160 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11161 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 11162 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11163 | #ifdef TARGET_NR_getdents |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11164 | case TARGET_NR_getdents: |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 11165 | return do_getdents(arg1, arg2, arg3); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11166 | #endif /* TARGET_NR_getdents */ |
ths | 3ae4320 | 2007-09-16 21:39:48 +0000 | [diff] [blame] | 11167 | #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 11168 | case TARGET_NR_getdents64: |
Richard Henderson | fd08ddb | 2021-11-14 11:35:36 +0100 | [diff] [blame] | 11169 | return do_getdents64(arg1, arg2, arg3); |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 11170 | #endif /* TARGET_NR_getdents64 */ |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 11171 | #if defined(TARGET_NR__newselect) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11172 | case TARGET_NR__newselect: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11173 | return do_select(arg1, arg2, arg3, arg4, arg5); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 11174 | #endif |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 11175 | #ifdef TARGET_NR_poll |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 11176 | case TARGET_NR_poll: |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 11177 | return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); |
| 11178 | #endif |
| 11179 | #ifdef TARGET_NR_ppoll |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 11180 | case TARGET_NR_ppoll: |
Filip Bozuta | e5ce968 | 2020-08-25 00:30:49 +0200 | [diff] [blame] | 11181 | return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); |
| 11182 | #endif |
| 11183 | #ifdef TARGET_NR_ppoll_time64 |
| 11184 | case TARGET_NR_ppoll_time64: |
| 11185 | return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 11186 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11187 | case TARGET_NR_flock: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 11188 | /* NOTE: the flock constant seems to be the same for every |
| 11189 | Linux platform */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11190 | return get_errno(safe_flock(arg1, arg2)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11191 | case TARGET_NR_readv: |
| 11192 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 11193 | struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); |
| 11194 | if (vec != NULL) { |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 11195 | ret = get_errno(safe_readv(arg1, vec, arg3)); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 11196 | unlock_iovec(vec, arg2, arg3, 1); |
| 11197 | } else { |
| 11198 | ret = -host_to_target_errno(errno); |
| 11199 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11200 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11201 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11202 | case TARGET_NR_writev: |
| 11203 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 11204 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 11205 | if (vec != NULL) { |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 11206 | ret = get_errno(safe_writev(arg1, vec, arg3)); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 11207 | unlock_iovec(vec, arg2, arg3, 0); |
| 11208 | } else { |
| 11209 | ret = -host_to_target_errno(errno); |
| 11210 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11211 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11212 | return ret; |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 11213 | #if defined(TARGET_NR_preadv) |
| 11214 | case TARGET_NR_preadv: |
| 11215 | { |
| 11216 | struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); |
| 11217 | if (vec != NULL) { |
Max Filippov | 9ac2251 | 2018-04-04 17:30:41 -0700 | [diff] [blame] | 11218 | unsigned long low, high; |
| 11219 | |
| 11220 | target_to_host_low_high(arg4, arg5, &low, &high); |
| 11221 | ret = get_errno(safe_preadv(arg1, vec, arg3, low, high)); |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 11222 | unlock_iovec(vec, arg2, arg3, 1); |
| 11223 | } else { |
| 11224 | ret = -host_to_target_errno(errno); |
| 11225 | } |
| 11226 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11227 | return ret; |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 11228 | #endif |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 11229 | #if defined(TARGET_NR_pwritev) |
| 11230 | case TARGET_NR_pwritev: |
| 11231 | { |
| 11232 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 11233 | if (vec != NULL) { |
Max Filippov | 9ac2251 | 2018-04-04 17:30:41 -0700 | [diff] [blame] | 11234 | unsigned long low, high; |
| 11235 | |
| 11236 | target_to_host_low_high(arg4, arg5, &low, &high); |
| 11237 | ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high)); |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 11238 | unlock_iovec(vec, arg2, arg3, 0); |
| 11239 | } else { |
| 11240 | ret = -host_to_target_errno(errno); |
| 11241 | } |
| 11242 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11243 | return ret; |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 11244 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11245 | case TARGET_NR_getsid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11246 | return get_errno(getsid(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 11247 | #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11248 | case TARGET_NR_fdatasync: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11249 | return get_errno(fdatasync(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 11250 | #endif |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11251 | case TARGET_NR_sched_getaffinity: |
| 11252 | { |
| 11253 | unsigned int mask_size; |
| 11254 | unsigned long *mask; |
| 11255 | |
| 11256 | /* |
| 11257 | * sched_getaffinity needs multiples of ulong, so need to take |
| 11258 | * care of mismatches between target ulong and host ulong sizes. |
| 11259 | */ |
| 11260 | if (arg2 & (sizeof(abi_ulong) - 1)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11261 | return -TARGET_EINVAL; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11262 | } |
| 11263 | mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); |
| 11264 | |
| 11265 | mask = alloca(mask_size); |
Samuel Thibault | 2e0a871 | 2018-01-09 21:16:43 +0100 | [diff] [blame] | 11266 | memset(mask, 0, mask_size); |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11267 | ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask)); |
| 11268 | |
| 11269 | if (!is_error(ret)) { |
Peter Maydell | be3bd28 | 2014-05-15 14:40:23 +0100 | [diff] [blame] | 11270 | if (ret > arg2) { |
| 11271 | /* More data returned than the caller's buffer will fit. |
| 11272 | * This only happens if sizeof(abi_long) < sizeof(long) |
| 11273 | * and the caller passed us a buffer holding an odd number |
| 11274 | * of abi_longs. If the host kernel is actually using the |
| 11275 | * extra 4 bytes then fail EINVAL; otherwise we can just |
| 11276 | * ignore them and only copy the interesting part. |
| 11277 | */ |
| 11278 | int numcpus = sysconf(_SC_NPROCESSORS_CONF); |
| 11279 | if (numcpus > arg2 * 8) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11280 | return -TARGET_EINVAL; |
Peter Maydell | be3bd28 | 2014-05-15 14:40:23 +0100 | [diff] [blame] | 11281 | } |
| 11282 | ret = arg2; |
| 11283 | } |
| 11284 | |
Samuel Thibault | 5fdefcf | 2018-02-11 18:47:04 +0100 | [diff] [blame] | 11285 | if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11286 | return -TARGET_EFAULT; |
Samuel Thibault | 5fdefcf | 2018-02-11 18:47:04 +0100 | [diff] [blame] | 11287 | } |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11288 | } |
| 11289 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11290 | return ret; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11291 | case TARGET_NR_sched_setaffinity: |
| 11292 | { |
| 11293 | unsigned int mask_size; |
| 11294 | unsigned long *mask; |
| 11295 | |
| 11296 | /* |
| 11297 | * sched_setaffinity needs multiples of ulong, so need to take |
| 11298 | * care of mismatches between target ulong and host ulong sizes. |
| 11299 | */ |
| 11300 | if (arg2 & (sizeof(abi_ulong) - 1)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11301 | return -TARGET_EINVAL; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11302 | } |
| 11303 | mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11304 | mask = alloca(mask_size); |
Samuel Thibault | 2e0a871 | 2018-01-09 21:16:43 +0100 | [diff] [blame] | 11305 | |
| 11306 | ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2); |
| 11307 | if (ret) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11308 | return ret; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11309 | } |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11310 | |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11311 | return get_errno(sys_sched_setaffinity(arg1, mask_size, mask)); |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 11312 | } |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 11313 | case TARGET_NR_getcpu: |
| 11314 | { |
Laurent Vivier | 9962b7c | 2023-09-25 17:10:28 +0200 | [diff] [blame] | 11315 | unsigned cpuid, node; |
| 11316 | ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL, |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 11317 | arg2 ? &node : NULL, |
| 11318 | NULL)); |
| 11319 | if (is_error(ret)) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11320 | return ret; |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 11321 | } |
Laurent Vivier | 9962b7c | 2023-09-25 17:10:28 +0200 | [diff] [blame] | 11322 | if (arg1 && put_user_u32(cpuid, arg1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11323 | return -TARGET_EFAULT; |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 11324 | } |
| 11325 | if (arg2 && put_user_u32(node, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11326 | return -TARGET_EFAULT; |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 11327 | } |
| 11328 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11329 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11330 | case TARGET_NR_sched_setparam: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11331 | { |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11332 | struct target_sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11333 | struct sched_param schp; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11334 | |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 11335 | if (arg2 == 0) { |
| 11336 | return -TARGET_EINVAL; |
| 11337 | } |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11338 | if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11339 | return -TARGET_EFAULT; |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11340 | } |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11341 | schp.sched_priority = tswap32(target_schp->sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11342 | unlock_user_struct(target_schp, arg2, 0); |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11343 | return get_errno(sys_sched_setparam(arg1, &schp)); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11344 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11345 | case TARGET_NR_sched_getparam: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11346 | { |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11347 | struct target_sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11348 | struct sched_param schp; |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 11349 | |
| 11350 | if (arg2 == 0) { |
| 11351 | return -TARGET_EINVAL; |
| 11352 | } |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11353 | ret = get_errno(sys_sched_getparam(arg1, &schp)); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11354 | if (!is_error(ret)) { |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11355 | if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11356 | return -TARGET_EFAULT; |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11357 | } |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11358 | target_schp->sched_priority = tswap32(schp.sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11359 | unlock_user_struct(target_schp, arg2, 1); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11360 | } |
| 11361 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11362 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11363 | case TARGET_NR_sched_setscheduler: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11364 | { |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11365 | struct target_sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11366 | struct sched_param schp; |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 11367 | if (arg3 == 0) { |
| 11368 | return -TARGET_EINVAL; |
| 11369 | } |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11370 | if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11371 | return -TARGET_EFAULT; |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11372 | } |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11373 | schp.sched_priority = tswap32(target_schp->sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11374 | unlock_user_struct(target_schp, arg3, 0); |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11375 | return get_errno(sys_sched_setscheduler(arg1, arg2, &schp)); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11376 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11377 | case TARGET_NR_sched_getscheduler: |
Tonis Tiigi | 407a119 | 2022-01-04 20:18:19 -0800 | [diff] [blame] | 11378 | return get_errno(sys_sched_getscheduler(arg1)); |
Tonis Tiigi | 45ad761 | 2022-01-04 20:18:18 -0800 | [diff] [blame] | 11379 | case TARGET_NR_sched_getattr: |
| 11380 | { |
| 11381 | struct target_sched_attr *target_scha; |
| 11382 | struct sched_attr scha; |
| 11383 | if (arg2 == 0) { |
| 11384 | return -TARGET_EINVAL; |
| 11385 | } |
| 11386 | if (arg3 > sizeof(scha)) { |
| 11387 | arg3 = sizeof(scha); |
| 11388 | } |
| 11389 | ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4)); |
| 11390 | if (!is_error(ret)) { |
| 11391 | target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 11392 | if (!target_scha) { |
| 11393 | return -TARGET_EFAULT; |
| 11394 | } |
| 11395 | target_scha->size = tswap32(scha.size); |
| 11396 | target_scha->sched_policy = tswap32(scha.sched_policy); |
| 11397 | target_scha->sched_flags = tswap64(scha.sched_flags); |
| 11398 | target_scha->sched_nice = tswap32(scha.sched_nice); |
| 11399 | target_scha->sched_priority = tswap32(scha.sched_priority); |
| 11400 | target_scha->sched_runtime = tswap64(scha.sched_runtime); |
| 11401 | target_scha->sched_deadline = tswap64(scha.sched_deadline); |
| 11402 | target_scha->sched_period = tswap64(scha.sched_period); |
| 11403 | if (scha.size > offsetof(struct sched_attr, sched_util_min)) { |
| 11404 | target_scha->sched_util_min = tswap32(scha.sched_util_min); |
| 11405 | target_scha->sched_util_max = tswap32(scha.sched_util_max); |
| 11406 | } |
| 11407 | unlock_user(target_scha, arg2, arg3); |
| 11408 | } |
| 11409 | return ret; |
| 11410 | } |
| 11411 | case TARGET_NR_sched_setattr: |
| 11412 | { |
| 11413 | struct target_sched_attr *target_scha; |
| 11414 | struct sched_attr scha; |
| 11415 | uint32_t size; |
| 11416 | int zeroed; |
| 11417 | if (arg2 == 0) { |
| 11418 | return -TARGET_EINVAL; |
| 11419 | } |
| 11420 | if (get_user_u32(size, arg2)) { |
| 11421 | return -TARGET_EFAULT; |
| 11422 | } |
| 11423 | if (!size) { |
| 11424 | size = offsetof(struct target_sched_attr, sched_util_min); |
| 11425 | } |
| 11426 | if (size < offsetof(struct target_sched_attr, sched_util_min)) { |
| 11427 | if (put_user_u32(sizeof(struct target_sched_attr), arg2)) { |
| 11428 | return -TARGET_EFAULT; |
| 11429 | } |
| 11430 | return -TARGET_E2BIG; |
| 11431 | } |
| 11432 | |
| 11433 | zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size); |
| 11434 | if (zeroed < 0) { |
| 11435 | return zeroed; |
| 11436 | } else if (zeroed == 0) { |
| 11437 | if (put_user_u32(sizeof(struct target_sched_attr), arg2)) { |
| 11438 | return -TARGET_EFAULT; |
| 11439 | } |
| 11440 | return -TARGET_E2BIG; |
| 11441 | } |
| 11442 | if (size > sizeof(struct target_sched_attr)) { |
| 11443 | size = sizeof(struct target_sched_attr); |
| 11444 | } |
| 11445 | |
| 11446 | target_scha = lock_user(VERIFY_READ, arg2, size, 1); |
| 11447 | if (!target_scha) { |
| 11448 | return -TARGET_EFAULT; |
| 11449 | } |
| 11450 | scha.size = size; |
| 11451 | scha.sched_policy = tswap32(target_scha->sched_policy); |
| 11452 | scha.sched_flags = tswap64(target_scha->sched_flags); |
| 11453 | scha.sched_nice = tswap32(target_scha->sched_nice); |
| 11454 | scha.sched_priority = tswap32(target_scha->sched_priority); |
| 11455 | scha.sched_runtime = tswap64(target_scha->sched_runtime); |
| 11456 | scha.sched_deadline = tswap64(target_scha->sched_deadline); |
| 11457 | scha.sched_period = tswap64(target_scha->sched_period); |
| 11458 | if (size > offsetof(struct target_sched_attr, sched_util_min)) { |
| 11459 | scha.sched_util_min = tswap32(target_scha->sched_util_min); |
| 11460 | scha.sched_util_max = tswap32(target_scha->sched_util_max); |
| 11461 | } |
| 11462 | unlock_user(target_scha, arg2, 0); |
| 11463 | return get_errno(sys_sched_setattr(arg1, &scha, arg3)); |
| 11464 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11465 | case TARGET_NR_sched_yield: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11466 | return get_errno(sched_yield()); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11467 | case TARGET_NR_sched_get_priority_max: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11468 | return get_errno(sched_get_priority_max(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11469 | case TARGET_NR_sched_get_priority_min: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11470 | return get_errno(sched_get_priority_min(arg1)); |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11471 | #ifdef TARGET_NR_sched_rr_get_interval |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11472 | case TARGET_NR_sched_rr_get_interval: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11473 | { |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11474 | struct timespec ts; |
| 11475 | ret = get_errno(sched_rr_get_interval(arg1, &ts)); |
| 11476 | if (!is_error(ret)) { |
Tom Musta | d4290c4 | 2014-08-12 13:53:39 -0500 | [diff] [blame] | 11477 | ret = host_to_target_timespec(arg2, &ts); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11478 | } |
| 11479 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11480 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11481 | #endif |
Filip Bozuta | ddcbde1 | 2020-08-24 21:21:16 +0200 | [diff] [blame] | 11482 | #ifdef TARGET_NR_sched_rr_get_interval_time64 |
| 11483 | case TARGET_NR_sched_rr_get_interval_time64: |
| 11484 | { |
| 11485 | struct timespec ts; |
| 11486 | ret = get_errno(sched_rr_get_interval(arg1, &ts)); |
| 11487 | if (!is_error(ret)) { |
| 11488 | ret = host_to_target_timespec64(arg2, &ts); |
| 11489 | } |
| 11490 | } |
| 11491 | return ret; |
| 11492 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11493 | #if defined(TARGET_NR_nanosleep) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11494 | case TARGET_NR_nanosleep: |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 11495 | { |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 11496 | struct timespec req, rem; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11497 | target_to_host_timespec(&req, arg1); |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 11498 | ret = get_errno(safe_nanosleep(&req, &rem)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11499 | if (is_error(ret) && arg2) { |
| 11500 | host_to_target_timespec(arg2, &rem); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 11501 | } |
| 11502 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11503 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11504 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11505 | case TARGET_NR_prctl: |
Richard Henderson | 87e9bf2 | 2021-12-27 07:01:22 -0800 | [diff] [blame] | 11506 | return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5); |
ths | 39b9aae | 2007-02-11 18:36:44 +0000 | [diff] [blame] | 11507 | break; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 11508 | #ifdef TARGET_NR_arch_prctl |
| 11509 | case TARGET_NR_arch_prctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11510 | return do_arch_prctl(cpu_env, arg1, arg2); |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 11511 | #endif |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 11512 | #ifdef TARGET_NR_pread64 |
| 11513 | case TARGET_NR_pread64: |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 11514 | if (regpairs_aligned(cpu_env, num)) { |
Alexander Graf | ae017a5 | 2012-09-29 15:32:39 +0000 | [diff] [blame] | 11515 | arg4 = arg5; |
| 11516 | arg5 = arg6; |
| 11517 | } |
Peter Maydell | 2bd3f89 | 2019-01-08 18:49:00 +0000 | [diff] [blame] | 11518 | if (arg2 == 0 && arg3 == 0) { |
| 11519 | /* Special-case NULL buffer and zero length, which should succeed */ |
| 11520 | p = 0; |
| 11521 | } else { |
| 11522 | p = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 11523 | if (!p) { |
| 11524 | return -TARGET_EFAULT; |
| 11525 | } |
| 11526 | } |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 11527 | ret = get_errno(pread(arg1, p, arg3, target_offset64(arg4, arg5))); |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 11528 | unlock_user(p, arg2, ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11529 | return ret; |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 11530 | case TARGET_NR_pwrite64: |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 11531 | if (regpairs_aligned(cpu_env, num)) { |
Alexander Graf | ae017a5 | 2012-09-29 15:32:39 +0000 | [diff] [blame] | 11532 | arg4 = arg5; |
| 11533 | arg5 = arg6; |
| 11534 | } |
Peter Maydell | 2bd3f89 | 2019-01-08 18:49:00 +0000 | [diff] [blame] | 11535 | if (arg2 == 0 && arg3 == 0) { |
| 11536 | /* Special-case NULL buffer and zero length, which should succeed */ |
| 11537 | p = 0; |
| 11538 | } else { |
| 11539 | p = lock_user(VERIFY_READ, arg2, arg3, 1); |
| 11540 | if (!p) { |
| 11541 | return -TARGET_EFAULT; |
| 11542 | } |
| 11543 | } |
Michael Tokarev | 99174ce | 2024-08-29 09:59:51 +0300 | [diff] [blame] | 11544 | ret = get_errno(pwrite(arg1, p, arg3, target_offset64(arg4, arg5))); |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 11545 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11546 | return ret; |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 11547 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11548 | case TARGET_NR_getcwd: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11549 | if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11550 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11551 | ret = get_errno(sys_getcwd1(p, arg2)); |
| 11552 | unlock_user(p, arg1, ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11553 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11554 | case TARGET_NR_capget: |
| 11555 | case TARGET_NR_capset: |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 11556 | { |
| 11557 | struct target_user_cap_header *target_header; |
| 11558 | struct target_user_cap_data *target_data = NULL; |
| 11559 | struct __user_cap_header_struct header; |
| 11560 | struct __user_cap_data_struct data[2]; |
| 11561 | struct __user_cap_data_struct *dataptr = NULL; |
| 11562 | int i, target_datalen; |
| 11563 | int data_items = 1; |
| 11564 | |
| 11565 | if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11566 | return -TARGET_EFAULT; |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 11567 | } |
| 11568 | header.version = tswap32(target_header->version); |
| 11569 | header.pid = tswap32(target_header->pid); |
| 11570 | |
Peter Maydell | ec86487 | 2014-03-19 16:07:30 +0000 | [diff] [blame] | 11571 | if (header.version != _LINUX_CAPABILITY_VERSION) { |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 11572 | /* Version 2 and up takes pointer to two user_data structs */ |
| 11573 | data_items = 2; |
| 11574 | } |
| 11575 | |
| 11576 | target_datalen = sizeof(*target_data) * data_items; |
| 11577 | |
| 11578 | if (arg2) { |
| 11579 | if (num == TARGET_NR_capget) { |
| 11580 | target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0); |
| 11581 | } else { |
| 11582 | target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1); |
| 11583 | } |
| 11584 | if (!target_data) { |
| 11585 | unlock_user_struct(target_header, arg1, 0); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11586 | return -TARGET_EFAULT; |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 11587 | } |
| 11588 | |
| 11589 | if (num == TARGET_NR_capset) { |
| 11590 | for (i = 0; i < data_items; i++) { |
| 11591 | data[i].effective = tswap32(target_data[i].effective); |
| 11592 | data[i].permitted = tswap32(target_data[i].permitted); |
| 11593 | data[i].inheritable = tswap32(target_data[i].inheritable); |
| 11594 | } |
| 11595 | } |
| 11596 | |
| 11597 | dataptr = data; |
| 11598 | } |
| 11599 | |
| 11600 | if (num == TARGET_NR_capget) { |
| 11601 | ret = get_errno(capget(&header, dataptr)); |
| 11602 | } else { |
| 11603 | ret = get_errno(capset(&header, dataptr)); |
| 11604 | } |
| 11605 | |
| 11606 | /* The kernel always updates version for both capget and capset */ |
| 11607 | target_header->version = tswap32(header.version); |
| 11608 | unlock_user_struct(target_header, arg1, 1); |
| 11609 | |
| 11610 | if (arg2) { |
| 11611 | if (num == TARGET_NR_capget) { |
| 11612 | for (i = 0; i < data_items; i++) { |
| 11613 | target_data[i].effective = tswap32(data[i].effective); |
| 11614 | target_data[i].permitted = tswap32(data[i].permitted); |
| 11615 | target_data[i].inheritable = tswap32(data[i].inheritable); |
| 11616 | } |
| 11617 | unlock_user(target_data, arg2, target_datalen); |
| 11618 | } else { |
| 11619 | unlock_user(target_data, arg2, 0); |
| 11620 | } |
| 11621 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11622 | return ret; |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 11623 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11624 | case TARGET_NR_sigaltstack: |
Richard Henderson | 6b20875 | 2021-04-25 19:53:12 -0700 | [diff] [blame] | 11625 | return do_sigaltstack(arg1, arg2, cpu_env); |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11626 | |
| 11627 | #ifdef CONFIG_SENDFILE |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 11628 | #ifdef TARGET_NR_sendfile |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11629 | case TARGET_NR_sendfile: |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11630 | { |
| 11631 | off_t *offp = NULL; |
| 11632 | off_t off; |
| 11633 | if (arg3) { |
| 11634 | ret = get_user_sal(off, arg3); |
| 11635 | if (is_error(ret)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11636 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11637 | } |
| 11638 | offp = &off; |
| 11639 | } |
| 11640 | ret = get_errno(sendfile(arg1, arg2, offp, arg4)); |
| 11641 | if (!is_error(ret) && arg3) { |
| 11642 | abi_long ret2 = put_user_sal(off, arg3); |
| 11643 | if (is_error(ret2)) { |
| 11644 | ret = ret2; |
| 11645 | } |
| 11646 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11647 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11648 | } |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 11649 | #endif |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11650 | #ifdef TARGET_NR_sendfile64 |
| 11651 | case TARGET_NR_sendfile64: |
| 11652 | { |
| 11653 | off_t *offp = NULL; |
| 11654 | off_t off; |
| 11655 | if (arg3) { |
| 11656 | ret = get_user_s64(off, arg3); |
| 11657 | if (is_error(ret)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11658 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11659 | } |
| 11660 | offp = &off; |
| 11661 | } |
| 11662 | ret = get_errno(sendfile(arg1, arg2, offp, arg4)); |
| 11663 | if (!is_error(ret) && arg3) { |
| 11664 | abi_long ret2 = put_user_s64(off, arg3); |
| 11665 | if (is_error(ret2)) { |
| 11666 | ret = ret2; |
| 11667 | } |
| 11668 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11669 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 11670 | } |
| 11671 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11672 | #endif |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 11673 | #ifdef TARGET_NR_vfork |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11674 | case TARGET_NR_vfork: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11675 | return get_errno(do_fork(cpu_env, |
| 11676 | CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD, |
| 11677 | 0, 0, 0, 0)); |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 11678 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11679 | #ifdef TARGET_NR_ugetrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11680 | case TARGET_NR_ugetrlimit: |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 11681 | { |
| 11682 | struct rlimit rlim; |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 11683 | int resource = target_to_host_resource(arg1); |
| 11684 | ret = get_errno(getrlimit(resource, &rlim)); |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 11685 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11686 | struct target_rlimit *target_rlim; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11687 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11688 | return -TARGET_EFAULT; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 11689 | target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); |
| 11690 | target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11691 | unlock_user_struct(target_rlim, arg2, 1); |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 11692 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11693 | return ret; |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 11694 | } |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11695 | #endif |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11696 | #ifdef TARGET_NR_truncate64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11697 | case TARGET_NR_truncate64: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11698 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11699 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11700 | ret = target_truncate64(cpu_env, p, arg2, arg3, arg4); |
| 11701 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11702 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11703 | #endif |
| 11704 | #ifdef TARGET_NR_ftruncate64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11705 | case TARGET_NR_ftruncate64: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11706 | return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11707 | #endif |
| 11708 | #ifdef TARGET_NR_stat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11709 | case TARGET_NR_stat64: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11710 | if (!(p = lock_user_string(arg1))) { |
| 11711 | return -TARGET_EFAULT; |
| 11712 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11713 | ret = get_errno(stat(path(p), &st)); |
| 11714 | unlock_user(p, arg1, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 11715 | if (!is_error(ret)) |
| 11716 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11717 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11718 | #endif |
| 11719 | #ifdef TARGET_NR_lstat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11720 | case TARGET_NR_lstat64: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11721 | if (!(p = lock_user_string(arg1))) { |
| 11722 | return -TARGET_EFAULT; |
| 11723 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11724 | ret = get_errno(lstat(path(p), &st)); |
| 11725 | unlock_user(p, arg1, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 11726 | if (!is_error(ret)) |
| 11727 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11728 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11729 | #endif |
| 11730 | #ifdef TARGET_NR_fstat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11731 | case TARGET_NR_fstat64: |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 11732 | ret = get_errno(fstat(arg1, &st)); |
| 11733 | if (!is_error(ret)) |
| 11734 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11735 | return ret; |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 11736 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 11737 | #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 11738 | #ifdef TARGET_NR_fstatat64 |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 11739 | case TARGET_NR_fstatat64: |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 11740 | #endif |
| 11741 | #ifdef TARGET_NR_newfstatat |
| 11742 | case TARGET_NR_newfstatat: |
| 11743 | #endif |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11744 | if (!(p = lock_user_string(arg2))) { |
| 11745 | return -TARGET_EFAULT; |
| 11746 | } |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 11747 | ret = get_errno(fstatat(arg1, path(p), &st, arg4)); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11748 | unlock_user(p, arg2, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 11749 | if (!is_error(ret)) |
| 11750 | ret = host_to_target_stat64(cpu_env, arg3, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11751 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11752 | #endif |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 11753 | #if defined(TARGET_NR_statx) |
| 11754 | case TARGET_NR_statx: |
| 11755 | { |
| 11756 | struct target_statx *target_stx; |
| 11757 | int dirfd = arg1; |
| 11758 | int flags = arg3; |
| 11759 | |
| 11760 | p = lock_user_string(arg2); |
| 11761 | if (p == NULL) { |
| 11762 | return -TARGET_EFAULT; |
| 11763 | } |
| 11764 | #if defined(__NR_statx) |
| 11765 | { |
| 11766 | /* |
| 11767 | * It is assumed that struct statx is architecture independent. |
| 11768 | */ |
| 11769 | struct target_statx host_stx; |
| 11770 | int mask = arg4; |
| 11771 | |
| 11772 | ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx)); |
| 11773 | if (!is_error(ret)) { |
| 11774 | if (host_to_target_statx(&host_stx, arg5) != 0) { |
| 11775 | unlock_user(p, arg2, 0); |
| 11776 | return -TARGET_EFAULT; |
| 11777 | } |
| 11778 | } |
| 11779 | |
| 11780 | if (ret != -TARGET_ENOSYS) { |
| 11781 | unlock_user(p, arg2, 0); |
| 11782 | return ret; |
| 11783 | } |
| 11784 | } |
| 11785 | #endif |
| 11786 | ret = get_errno(fstatat(dirfd, path(p), &st, flags)); |
| 11787 | unlock_user(p, arg2, 0); |
| 11788 | |
| 11789 | if (!is_error(ret)) { |
| 11790 | if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) { |
| 11791 | return -TARGET_EFAULT; |
| 11792 | } |
| 11793 | memset(target_stx, 0, sizeof(*target_stx)); |
| 11794 | __put_user(major(st.st_dev), &target_stx->stx_dev_major); |
| 11795 | __put_user(minor(st.st_dev), &target_stx->stx_dev_minor); |
| 11796 | __put_user(st.st_ino, &target_stx->stx_ino); |
| 11797 | __put_user(st.st_mode, &target_stx->stx_mode); |
| 11798 | __put_user(st.st_uid, &target_stx->stx_uid); |
| 11799 | __put_user(st.st_gid, &target_stx->stx_gid); |
| 11800 | __put_user(st.st_nlink, &target_stx->stx_nlink); |
| 11801 | __put_user(major(st.st_rdev), &target_stx->stx_rdev_major); |
| 11802 | __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor); |
| 11803 | __put_user(st.st_size, &target_stx->stx_size); |
| 11804 | __put_user(st.st_blksize, &target_stx->stx_blksize); |
| 11805 | __put_user(st.st_blocks, &target_stx->stx_blocks); |
| 11806 | __put_user(st.st_atime, &target_stx->stx_atime.tv_sec); |
| 11807 | __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec); |
| 11808 | __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec); |
| 11809 | unlock_user_struct(target_stx, arg5, 1); |
| 11810 | } |
| 11811 | } |
| 11812 | return ret; |
| 11813 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11814 | #ifdef TARGET_NR_lchown |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11815 | case TARGET_NR_lchown: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11816 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11817 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11818 | ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3))); |
| 11819 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11820 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11821 | #endif |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11822 | #ifdef TARGET_NR_getuid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11823 | case TARGET_NR_getuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11824 | return get_errno(high2lowuid(getuid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11825 | #endif |
| 11826 | #ifdef TARGET_NR_getgid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11827 | case TARGET_NR_getgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11828 | return get_errno(high2lowgid(getgid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11829 | #endif |
| 11830 | #ifdef TARGET_NR_geteuid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11831 | case TARGET_NR_geteuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11832 | return get_errno(high2lowuid(geteuid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11833 | #endif |
| 11834 | #ifdef TARGET_NR_getegid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11835 | case TARGET_NR_getegid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11836 | return get_errno(high2lowgid(getegid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11837 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11838 | case TARGET_NR_setreuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11839 | return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11840 | case TARGET_NR_setregid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11841 | return get_errno(setregid(low2highgid(arg1), low2highgid(arg2))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11842 | case TARGET_NR_getgroups: |
Michael Tokarev | 725160f | 2023-06-03 20:23:38 +0300 | [diff] [blame] | 11843 | { /* the same code as for TARGET_NR_getgroups32 */ |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11844 | int gidsetsize = arg1; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11845 | target_id *target_grouplist; |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 11846 | g_autofree gid_t *grouplist = NULL; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11847 | int i; |
| 11848 | |
Peter Maydell | 8fbf89a | 2023-06-09 17:29:15 +0100 | [diff] [blame] | 11849 | if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 11850 | return -TARGET_EINVAL; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11851 | } |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 11852 | if (gidsetsize > 0) { |
| 11853 | grouplist = g_try_new(gid_t, gidsetsize); |
| 11854 | if (!grouplist) { |
| 11855 | return -TARGET_ENOMEM; |
| 11856 | } |
| 11857 | } |
| 11858 | ret = get_errno(getgroups(gidsetsize, grouplist)); |
| 11859 | if (!is_error(ret) && gidsetsize > 0) { |
| 11860 | target_grouplist = lock_user(VERIFY_WRITE, arg2, |
| 11861 | gidsetsize * sizeof(target_id), 0); |
| 11862 | if (!target_grouplist) { |
| 11863 | return -TARGET_EFAULT; |
| 11864 | } |
| 11865 | for (i = 0; i < ret; i++) { |
| 11866 | target_grouplist[i] = tswapid(high2lowgid(grouplist[i])); |
| 11867 | } |
| 11868 | unlock_user(target_grouplist, arg2, |
| 11869 | gidsetsize * sizeof(target_id)); |
| 11870 | } |
| 11871 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11872 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11873 | case TARGET_NR_setgroups: |
Michael Tokarev | 725160f | 2023-06-03 20:23:38 +0300 | [diff] [blame] | 11874 | { /* the same code as for TARGET_NR_setgroups32 */ |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11875 | int gidsetsize = arg1; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 11876 | target_id *target_grouplist; |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 11877 | g_autofree gid_t *grouplist = NULL; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11878 | int i; |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 11879 | |
| 11880 | if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { |
| 11881 | return -TARGET_EINVAL; |
| 11882 | } |
| 11883 | if (gidsetsize > 0) { |
| 11884 | grouplist = g_try_new(gid_t, gidsetsize); |
| 11885 | if (!grouplist) { |
| 11886 | return -TARGET_ENOMEM; |
| 11887 | } |
| 11888 | target_grouplist = lock_user(VERIFY_READ, arg2, |
| 11889 | gidsetsize * sizeof(target_id), 1); |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 11890 | if (!target_grouplist) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11891 | return -TARGET_EFAULT; |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 11892 | } |
| 11893 | for (i = 0; i < gidsetsize; i++) { |
| 11894 | grouplist[i] = low2highgid(tswapid(target_grouplist[i])); |
| 11895 | } |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 11896 | unlock_user(target_grouplist, arg2, |
| 11897 | gidsetsize * sizeof(target_id)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11898 | } |
Ilya Leoshkevich | 54b2792 | 2024-06-14 17:46:40 +0200 | [diff] [blame] | 11899 | return get_errno(sys_setgroups(gidsetsize, grouplist)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11900 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11901 | case TARGET_NR_fchown: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11902 | return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 11903 | #if defined(TARGET_NR_fchownat) |
ths | ccfa72b | 2007-09-24 09:23:34 +0000 | [diff] [blame] | 11904 | case TARGET_NR_fchownat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11905 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11906 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 11907 | ret = get_errno(fchownat(arg1, p, low2highuid(arg3), |
| 11908 | low2highgid(arg4), arg5)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11909 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11910 | return ret; |
ths | ccfa72b | 2007-09-24 09:23:34 +0000 | [diff] [blame] | 11911 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11912 | #ifdef TARGET_NR_setresuid |
| 11913 | case TARGET_NR_setresuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11914 | return get_errno(sys_setresuid(low2highuid(arg1), |
| 11915 | low2highuid(arg2), |
| 11916 | low2highuid(arg3))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11917 | #endif |
| 11918 | #ifdef TARGET_NR_getresuid |
| 11919 | case TARGET_NR_getresuid: |
| 11920 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11921 | uid_t ruid, euid, suid; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11922 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 11923 | if (!is_error(ret)) { |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 11924 | if (put_user_id(high2lowuid(ruid), arg1) |
| 11925 | || put_user_id(high2lowuid(euid), arg2) |
| 11926 | || put_user_id(high2lowuid(suid), arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11927 | return -TARGET_EFAULT; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11928 | } |
| 11929 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11930 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11931 | #endif |
| 11932 | #ifdef TARGET_NR_getresgid |
| 11933 | case TARGET_NR_setresgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11934 | return get_errno(sys_setresgid(low2highgid(arg1), |
| 11935 | low2highgid(arg2), |
| 11936 | low2highgid(arg3))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11937 | #endif |
| 11938 | #ifdef TARGET_NR_getresgid |
| 11939 | case TARGET_NR_getresgid: |
| 11940 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11941 | gid_t rgid, egid, sgid; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11942 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 11943 | if (!is_error(ret)) { |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 11944 | if (put_user_id(high2lowgid(rgid), arg1) |
| 11945 | || put_user_id(high2lowgid(egid), arg2) |
| 11946 | || put_user_id(high2lowgid(sgid), arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11947 | return -TARGET_EFAULT; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11948 | } |
| 11949 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11950 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11951 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11952 | #ifdef TARGET_NR_chown |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11953 | case TARGET_NR_chown: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11954 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11955 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11956 | ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3))); |
| 11957 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11958 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11959 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11960 | case TARGET_NR_setuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11961 | return get_errno(sys_setuid(low2highuid(arg1))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11962 | case TARGET_NR_setgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11963 | return get_errno(sys_setgid(low2highgid(arg1))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11964 | case TARGET_NR_setfsuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11965 | return get_errno(setfsuid(arg1)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11966 | case TARGET_NR_setfsgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11967 | return get_errno(setfsgid(arg1)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11968 | |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11969 | #ifdef TARGET_NR_lchown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11970 | case TARGET_NR_lchown32: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11971 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11972 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11973 | ret = get_errno(lchown(p, arg2, arg3)); |
| 11974 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11975 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11976 | #endif |
| 11977 | #ifdef TARGET_NR_getuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11978 | case TARGET_NR_getuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11979 | return get_errno(getuid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11980 | #endif |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 11981 | |
| 11982 | #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA) |
| 11983 | /* Alpha specific */ |
| 11984 | case TARGET_NR_getxuid: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11985 | { |
| 11986 | uid_t euid; |
| 11987 | euid=geteuid(); |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 11988 | cpu_env->ir[IR_A4]=euid; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11989 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11990 | return get_errno(getuid()); |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 11991 | #endif |
| 11992 | #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA) |
| 11993 | /* Alpha specific */ |
| 11994 | case TARGET_NR_getxgid: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11995 | { |
| 11996 | uid_t egid; |
| 11997 | egid=getegid(); |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 11998 | cpu_env->ir[IR_A4]=egid; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11999 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12000 | return get_errno(getgid()); |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 12001 | #endif |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12002 | #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA) |
| 12003 | /* Alpha specific */ |
| 12004 | case TARGET_NR_osf_getsysinfo: |
| 12005 | ret = -TARGET_EOPNOTSUPP; |
| 12006 | switch (arg1) { |
| 12007 | case TARGET_GSI_IEEE_FP_CONTROL: |
| 12008 | { |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12009 | uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env); |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 12010 | uint64_t swcr = cpu_env->swcr; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12011 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12012 | swcr &= ~SWCR_STATUS_MASK; |
| 12013 | swcr |= (fpcr >> 35) & SWCR_STATUS_MASK; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12014 | |
| 12015 | if (put_user_u64 (swcr, arg2)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12016 | return -TARGET_EFAULT; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12017 | ret = 0; |
| 12018 | } |
| 12019 | break; |
| 12020 | |
| 12021 | /* case GSI_IEEE_STATE_AT_SIGNAL: |
| 12022 | -- Not implemented in linux kernel. |
| 12023 | case GSI_UACPROC: |
| 12024 | -- Retrieves current unaligned access state; not much used. |
| 12025 | case GSI_PROC_TYPE: |
| 12026 | -- Retrieves implver information; surely not used. |
| 12027 | case GSI_GET_HWRPB: |
| 12028 | -- Grabs a copy of the HWRPB; surely not used. |
| 12029 | */ |
| 12030 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12031 | return ret; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12032 | #endif |
| 12033 | #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA) |
| 12034 | /* Alpha specific */ |
| 12035 | case TARGET_NR_osf_setsysinfo: |
| 12036 | ret = -TARGET_EOPNOTSUPP; |
| 12037 | switch (arg1) { |
| 12038 | case TARGET_SSI_IEEE_FP_CONTROL: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12039 | { |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12040 | uint64_t swcr, fpcr; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12041 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12042 | if (get_user_u64 (swcr, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12043 | return -TARGET_EFAULT; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12044 | } |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12045 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12046 | /* |
| 12047 | * The kernel calls swcr_update_status to update the |
| 12048 | * status bits from the fpcr at every point that it |
| 12049 | * could be queried. Therefore, we store the status |
| 12050 | * bits only in FPCR. |
| 12051 | */ |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 12052 | cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12053 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12054 | fpcr = cpu_alpha_load_fpcr(cpu_env); |
| 12055 | fpcr &= ((uint64_t)FPCR_DYN_MASK << 32); |
| 12056 | fpcr |= alpha_ieee_swcr_to_fpcr(swcr); |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12057 | cpu_alpha_store_fpcr(cpu_env, fpcr); |
| 12058 | ret = 0; |
| 12059 | } |
| 12060 | break; |
| 12061 | |
| 12062 | case TARGET_SSI_IEEE_RAISE_EXCEPTION: |
| 12063 | { |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12064 | uint64_t exc, fpcr, fex; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12065 | |
| 12066 | if (get_user_u64(exc, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12067 | return -TARGET_EFAULT; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12068 | } |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12069 | exc &= SWCR_STATUS_MASK; |
| 12070 | fpcr = cpu_alpha_load_fpcr(cpu_env); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12071 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12072 | /* Old exceptions are not signaled. */ |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12073 | fex = alpha_ieee_fpcr_to_swcr(fpcr); |
| 12074 | fex = exc & ~fex; |
| 12075 | fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT; |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 12076 | fex &= (cpu_env)->swcr; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12077 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12078 | /* Update the hardware fpcr. */ |
| 12079 | fpcr |= alpha_ieee_swcr_to_fpcr(exc); |
| 12080 | cpu_alpha_store_fpcr(cpu_env, fpcr); |
| 12081 | |
| 12082 | if (fex) { |
| 12083 | int si_code = TARGET_FPE_FLTUNK; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12084 | target_siginfo_t info; |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12085 | |
| 12086 | if (fex & SWCR_TRAP_ENABLE_DNO) { |
| 12087 | si_code = TARGET_FPE_FLTUND; |
| 12088 | } |
| 12089 | if (fex & SWCR_TRAP_ENABLE_INE) { |
| 12090 | si_code = TARGET_FPE_FLTRES; |
| 12091 | } |
| 12092 | if (fex & SWCR_TRAP_ENABLE_UNF) { |
| 12093 | si_code = TARGET_FPE_FLTUND; |
| 12094 | } |
| 12095 | if (fex & SWCR_TRAP_ENABLE_OVF) { |
| 12096 | si_code = TARGET_FPE_FLTOVF; |
| 12097 | } |
| 12098 | if (fex & SWCR_TRAP_ENABLE_DZE) { |
| 12099 | si_code = TARGET_FPE_FLTDIV; |
| 12100 | } |
| 12101 | if (fex & SWCR_TRAP_ENABLE_INV) { |
| 12102 | si_code = TARGET_FPE_FLTINV; |
| 12103 | } |
| 12104 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 12105 | info.si_signo = SIGFPE; |
| 12106 | info.si_errno = 0; |
| 12107 | info.si_code = si_code; |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 12108 | info._sifields._sigfault._addr = (cpu_env)->pc; |
| 12109 | queue_signal(cpu_env, info.si_signo, |
Peter Maydell | 9d2803f | 2016-07-28 16:44:46 +0100 | [diff] [blame] | 12110 | QEMU_SI_FAULT, &info); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12111 | } |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 12112 | ret = 0; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12113 | } |
| 12114 | break; |
| 12115 | |
| 12116 | /* case SSI_NVPAIRS: |
| 12117 | -- Used with SSIN_UACPROC to enable unaligned accesses. |
| 12118 | case SSI_IEEE_STATE_AT_SIGNAL: |
| 12119 | case SSI_IEEE_IGNORE_STATE_AT_SIGNAL: |
| 12120 | -- Not implemented in linux kernel |
| 12121 | */ |
| 12122 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12123 | return ret; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12124 | #endif |
| 12125 | #ifdef TARGET_NR_osf_sigprocmask |
| 12126 | /* Alpha specific. */ |
| 12127 | case TARGET_NR_osf_sigprocmask: |
| 12128 | { |
| 12129 | abi_ulong mask; |
Juan Quintela | bc088ba | 2011-06-16 17:37:10 +0100 | [diff] [blame] | 12130 | int how; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12131 | sigset_t set, oldset; |
| 12132 | |
| 12133 | switch(arg1) { |
| 12134 | case TARGET_SIG_BLOCK: |
| 12135 | how = SIG_BLOCK; |
| 12136 | break; |
| 12137 | case TARGET_SIG_UNBLOCK: |
| 12138 | how = SIG_UNBLOCK; |
| 12139 | break; |
| 12140 | case TARGET_SIG_SETMASK: |
| 12141 | how = SIG_SETMASK; |
| 12142 | break; |
| 12143 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 12144 | return -TARGET_EINVAL; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12145 | } |
| 12146 | mask = arg2; |
| 12147 | target_to_host_old_sigset(&set, &mask); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 12148 | ret = do_sigprocmask(how, &set, &oldset); |
| 12149 | if (!ret) { |
| 12150 | host_to_target_old_sigset(&mask, &oldset); |
| 12151 | ret = mask; |
| 12152 | } |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12153 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12154 | return ret; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 12155 | #endif |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 12156 | |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12157 | #ifdef TARGET_NR_getgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12158 | case TARGET_NR_getgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12159 | return get_errno(getgid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12160 | #endif |
| 12161 | #ifdef TARGET_NR_geteuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12162 | case TARGET_NR_geteuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12163 | return get_errno(geteuid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12164 | #endif |
| 12165 | #ifdef TARGET_NR_getegid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12166 | case TARGET_NR_getegid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12167 | return get_errno(getegid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12168 | #endif |
| 12169 | #ifdef TARGET_NR_setreuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12170 | case TARGET_NR_setreuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12171 | return get_errno(setreuid(arg1, arg2)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12172 | #endif |
| 12173 | #ifdef TARGET_NR_setregid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12174 | case TARGET_NR_setregid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12175 | return get_errno(setregid(arg1, arg2)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12176 | #endif |
| 12177 | #ifdef TARGET_NR_getgroups32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12178 | case TARGET_NR_getgroups32: |
Michael Tokarev | 725160f | 2023-06-03 20:23:38 +0300 | [diff] [blame] | 12179 | { /* the same code as for TARGET_NR_getgroups */ |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12180 | int gidsetsize = arg1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12181 | uint32_t *target_grouplist; |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12182 | g_autofree gid_t *grouplist = NULL; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12183 | int i; |
| 12184 | |
Peter Maydell | 8fbf89a | 2023-06-09 17:29:15 +0100 | [diff] [blame] | 12185 | if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12186 | return -TARGET_EINVAL; |
| 12187 | } |
| 12188 | if (gidsetsize > 0) { |
| 12189 | grouplist = g_try_new(gid_t, gidsetsize); |
| 12190 | if (!grouplist) { |
| 12191 | return -TARGET_ENOMEM; |
| 12192 | } |
| 12193 | } |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12194 | ret = get_errno(getgroups(gidsetsize, grouplist)); |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12195 | if (!is_error(ret) && gidsetsize > 0) { |
| 12196 | target_grouplist = lock_user(VERIFY_WRITE, arg2, |
| 12197 | gidsetsize * 4, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12198 | if (!target_grouplist) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 12199 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12200 | } |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12201 | for (i = 0; i < ret; i++) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12202 | target_grouplist[i] = tswap32(grouplist[i]); |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12203 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12204 | unlock_user(target_grouplist, arg2, gidsetsize * 4); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12205 | } |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12206 | return ret; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12207 | } |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12208 | #endif |
| 12209 | #ifdef TARGET_NR_setgroups32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12210 | case TARGET_NR_setgroups32: |
Michael Tokarev | 725160f | 2023-06-03 20:23:38 +0300 | [diff] [blame] | 12211 | { /* the same code as for TARGET_NR_setgroups */ |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12212 | int gidsetsize = arg1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12213 | uint32_t *target_grouplist; |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12214 | g_autofree gid_t *grouplist = NULL; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12215 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 12216 | |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12217 | if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { |
| 12218 | return -TARGET_EINVAL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12219 | } |
Michael Tokarev | 1e35d32 | 2023-04-09 13:53:27 +0300 | [diff] [blame] | 12220 | if (gidsetsize > 0) { |
| 12221 | grouplist = g_try_new(gid_t, gidsetsize); |
| 12222 | if (!grouplist) { |
| 12223 | return -TARGET_ENOMEM; |
| 12224 | } |
| 12225 | target_grouplist = lock_user(VERIFY_READ, arg2, |
| 12226 | gidsetsize * 4, 1); |
| 12227 | if (!target_grouplist) { |
| 12228 | return -TARGET_EFAULT; |
| 12229 | } |
| 12230 | for (i = 0; i < gidsetsize; i++) { |
| 12231 | grouplist[i] = tswap32(target_grouplist[i]); |
| 12232 | } |
| 12233 | unlock_user(target_grouplist, arg2, 0); |
| 12234 | } |
Ilya Leoshkevich | 54b2792 | 2024-06-14 17:46:40 +0200 | [diff] [blame] | 12235 | return get_errno(sys_setgroups(gidsetsize, grouplist)); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 12236 | } |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12237 | #endif |
| 12238 | #ifdef TARGET_NR_fchown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12239 | case TARGET_NR_fchown32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12240 | return get_errno(fchown(arg1, arg2, arg3)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12241 | #endif |
| 12242 | #ifdef TARGET_NR_setresuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12243 | case TARGET_NR_setresuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12244 | return get_errno(sys_setresuid(arg1, arg2, arg3)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12245 | #endif |
| 12246 | #ifdef TARGET_NR_getresuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12247 | case TARGET_NR_getresuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 12248 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12249 | uid_t ruid, euid, suid; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 12250 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 12251 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 12252 | if (put_user_u32(ruid, arg1) |
| 12253 | || put_user_u32(euid, arg2) |
| 12254 | || put_user_u32(suid, arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12255 | return -TARGET_EFAULT; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 12256 | } |
| 12257 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12258 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12259 | #endif |
| 12260 | #ifdef TARGET_NR_setresgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12261 | case TARGET_NR_setresgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12262 | return get_errno(sys_setresgid(arg1, arg2, arg3)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12263 | #endif |
| 12264 | #ifdef TARGET_NR_getresgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12265 | case TARGET_NR_getresgid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 12266 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12267 | gid_t rgid, egid, sgid; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 12268 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 12269 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 12270 | if (put_user_u32(rgid, arg1) |
| 12271 | || put_user_u32(egid, arg2) |
| 12272 | || put_user_u32(sgid, arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12273 | return -TARGET_EFAULT; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 12274 | } |
| 12275 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12276 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12277 | #endif |
| 12278 | #ifdef TARGET_NR_chown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12279 | case TARGET_NR_chown32: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12280 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12281 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 12282 | ret = get_errno(chown(p, arg2, arg3)); |
| 12283 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12284 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12285 | #endif |
| 12286 | #ifdef TARGET_NR_setuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12287 | case TARGET_NR_setuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12288 | return get_errno(sys_setuid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12289 | #endif |
| 12290 | #ifdef TARGET_NR_setgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12291 | case TARGET_NR_setgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12292 | return get_errno(sys_setgid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12293 | #endif |
| 12294 | #ifdef TARGET_NR_setfsuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12295 | case TARGET_NR_setfsuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12296 | return get_errno(setfsuid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12297 | #endif |
| 12298 | #ifdef TARGET_NR_setfsgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12299 | case TARGET_NR_setfsgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12300 | return get_errno(setfsgid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 12301 | #endif |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 12302 | #ifdef TARGET_NR_mincore |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12303 | case TARGET_NR_mincore: |
aurel32 | 04bb9ac | 2008-10-01 21:46:41 +0000 | [diff] [blame] | 12304 | { |
Thomas Weißschuh | f443a26 | 2023-04-22 12:03:14 +0200 | [diff] [blame] | 12305 | void *a = lock_user(VERIFY_NONE, arg1, arg2, 0); |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 12306 | if (!a) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 12307 | return -TARGET_ENOMEM; |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 12308 | } |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 12309 | p = lock_user_string(arg3); |
| 12310 | if (!p) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 12311 | ret = -TARGET_EFAULT; |
| 12312 | } else { |
| 12313 | ret = get_errno(mincore(a, arg2, p)); |
| 12314 | unlock_user(p, arg3, ret); |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 12315 | } |
aurel32 | 04bb9ac | 2008-10-01 21:46:41 +0000 | [diff] [blame] | 12316 | unlock_user(a, arg1, 0); |
| 12317 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12318 | return ret; |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 12319 | #endif |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 12320 | #ifdef TARGET_NR_arm_fadvise64_64 |
| 12321 | case TARGET_NR_arm_fadvise64_64: |
Peter Maydell | e0156a9 | 2016-05-31 15:45:09 +0100 | [diff] [blame] | 12322 | /* arm_fadvise64_64 looks like fadvise64_64 but |
| 12323 | * with different argument order: fd, advice, offset, len |
| 12324 | * rather than the usual fd, offset, len, advice. |
| 12325 | * Note that offset and len are both 64-bit so appear as |
| 12326 | * pairs of 32-bit registers. |
| 12327 | */ |
| 12328 | ret = posix_fadvise(arg1, target_offset64(arg3, arg4), |
| 12329 | target_offset64(arg5, arg6), arg2); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12330 | return -host_to_target_errno(ret); |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 12331 | #endif |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12332 | |
WANG Xuerui | eeed229 | 2022-10-06 16:55:00 +0800 | [diff] [blame] | 12333 | #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32) |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12334 | |
| 12335 | #ifdef TARGET_NR_fadvise64_64 |
| 12336 | case TARGET_NR_fadvise64_64: |
Max Filippov | 64a563d | 2018-04-01 15:02:34 -0700 | [diff] [blame] | 12337 | #if defined(TARGET_PPC) || defined(TARGET_XTENSA) |
Laurent Vivier | 43046b5 | 2017-03-02 01:11:45 +0100 | [diff] [blame] | 12338 | /* 6 args: fd, advice, offset (high, low), len (high, low) */ |
| 12339 | ret = arg2; |
| 12340 | arg2 = arg3; |
| 12341 | arg3 = arg4; |
| 12342 | arg4 = arg5; |
| 12343 | arg5 = arg6; |
| 12344 | arg6 = ret; |
| 12345 | #else |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12346 | /* 6 args: fd, offset (high, low), len (high, low), advice */ |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 12347 | if (regpairs_aligned(cpu_env, num)) { |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12348 | /* offset is in (3,4), len in (5,6) and advice in 7 */ |
| 12349 | arg2 = arg3; |
| 12350 | arg3 = arg4; |
| 12351 | arg4 = arg5; |
| 12352 | arg5 = arg6; |
| 12353 | arg6 = arg7; |
| 12354 | } |
Laurent Vivier | 43046b5 | 2017-03-02 01:11:45 +0100 | [diff] [blame] | 12355 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12356 | ret = posix_fadvise(arg1, target_offset64(arg2, arg3), |
| 12357 | target_offset64(arg4, arg5), arg6); |
| 12358 | return -host_to_target_errno(ret); |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12359 | #endif |
| 12360 | |
| 12361 | #ifdef TARGET_NR_fadvise64 |
| 12362 | case TARGET_NR_fadvise64: |
| 12363 | /* 5 args: fd, offset (high, low), len, advice */ |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 12364 | if (regpairs_aligned(cpu_env, num)) { |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12365 | /* offset is in (3,4), len in 5 and advice in 6 */ |
| 12366 | arg2 = arg3; |
| 12367 | arg3 = arg4; |
| 12368 | arg4 = arg5; |
| 12369 | arg5 = arg6; |
| 12370 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12371 | ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5); |
| 12372 | return -host_to_target_errno(ret); |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12373 | #endif |
| 12374 | |
| 12375 | #else /* not a 32-bit ABI */ |
Peter Maydell | e0156a9 | 2016-05-31 15:45:09 +0100 | [diff] [blame] | 12376 | #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64) |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 12377 | #ifdef TARGET_NR_fadvise64_64 |
| 12378 | case TARGET_NR_fadvise64_64: |
| 12379 | #endif |
Ulrich Hecht | e72d2cc | 2009-07-24 19:10:31 +0200 | [diff] [blame] | 12380 | #ifdef TARGET_NR_fadvise64 |
| 12381 | case TARGET_NR_fadvise64: |
| 12382 | #endif |
| 12383 | #ifdef TARGET_S390X |
| 12384 | switch (arg4) { |
| 12385 | case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */ |
| 12386 | case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */ |
| 12387 | case 6: arg4 = POSIX_FADV_DONTNEED; break; |
| 12388 | case 7: arg4 = POSIX_FADV_NOREUSE; break; |
| 12389 | default: break; |
| 12390 | } |
| 12391 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12392 | return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4)); |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 12393 | #endif |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 12394 | #endif /* end of 64-bit ABI fadvise handling */ |
| 12395 | |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 12396 | #ifdef TARGET_NR_madvise |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12397 | case TARGET_NR_madvise: |
Ilya Leoshkevich | 892a4f6 | 2022-06-21 16:42:05 +0200 | [diff] [blame] | 12398 | return target_madvise(arg1, arg2, arg3); |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 12399 | #endif |
Alex Bennée | bbf5f2a | 2020-04-03 20:11:40 +0100 | [diff] [blame] | 12400 | #ifdef TARGET_NR_fcntl64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12401 | case TARGET_NR_fcntl64: |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 12402 | { |
Alex Bennée | bbf5f2a | 2020-04-03 20:11:40 +0100 | [diff] [blame] | 12403 | int cmd; |
Michael Tokarev | ac1bbe8 | 2024-08-29 09:39:50 +0300 | [diff] [blame] | 12404 | struct flock fl; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 12405 | from_flock64_fn *copyfrom = copy_from_user_flock64; |
| 12406 | to_flock64_fn *copyto = copy_to_user_flock64; |
| 12407 | |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 12408 | #ifdef TARGET_ARM |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 12409 | if (!cpu_env->eabi) { |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 12410 | copyfrom = copy_from_user_oabi_flock64; |
| 12411 | copyto = copy_to_user_oabi_flock64; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 12412 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 12413 | #endif |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 12414 | |
Alex Bennée | bbf5f2a | 2020-04-03 20:11:40 +0100 | [diff] [blame] | 12415 | cmd = target_to_host_fcntl_cmd(arg2); |
Peter Maydell | 31b6319 | 2011-12-05 23:11:50 +0000 | [diff] [blame] | 12416 | if (cmd == -TARGET_EINVAL) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12417 | return cmd; |
Peter Maydell | 31b6319 | 2011-12-05 23:11:50 +0000 | [diff] [blame] | 12418 | } |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 12419 | |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 12420 | switch(arg2) { |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 12421 | case TARGET_F_GETLK64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 12422 | ret = copyfrom(&fl, arg3); |
| 12423 | if (ret) { |
| 12424 | break; |
ths | 5813427 | 2007-03-31 18:59:32 +0000 | [diff] [blame] | 12425 | } |
Laurent Vivier | af8ab2b | 2018-07-13 14:58:05 +0200 | [diff] [blame] | 12426 | ret = get_errno(safe_fcntl(arg1, cmd, &fl)); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 12427 | if (ret == 0) { |
| 12428 | ret = copyto(arg3, &fl); |
| 12429 | } |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 12430 | break; |
| 12431 | |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 12432 | case TARGET_F_SETLK64: |
| 12433 | case TARGET_F_SETLKW64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 12434 | ret = copyfrom(&fl, arg3); |
| 12435 | if (ret) { |
| 12436 | break; |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 12437 | } |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 12438 | ret = get_errno(safe_fcntl(arg1, cmd, &fl)); |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 12439 | break; |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 12440 | default: |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 12441 | ret = do_fcntl(arg1, arg2, arg3); |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 12442 | break; |
| 12443 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12444 | return ret; |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 12445 | } |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 12446 | #endif |
ths | 7d600c8 | 2006-12-08 01:32:58 +0000 | [diff] [blame] | 12447 | #ifdef TARGET_NR_cacheflush |
| 12448 | case TARGET_NR_cacheflush: |
| 12449 | /* self-modifying code is handled automatically, so nothing needed */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12450 | return 0; |
ths | 7d600c8 | 2006-12-08 01:32:58 +0000 | [diff] [blame] | 12451 | #endif |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 12452 | #ifdef TARGET_NR_getpagesize |
| 12453 | case TARGET_NR_getpagesize: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12454 | return TARGET_PAGE_SIZE; |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 12455 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12456 | case TARGET_NR_gettid: |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 12457 | return get_errno(sys_gettid()); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 12458 | #ifdef TARGET_NR_readahead |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12459 | case TARGET_NR_readahead: |
WANG Xuerui | eeed229 | 2022-10-06 16:55:00 +0800 | [diff] [blame] | 12460 | #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32) |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 12461 | if (regpairs_aligned(cpu_env, num)) { |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 12462 | arg2 = arg3; |
| 12463 | arg3 = arg4; |
| 12464 | arg4 = arg5; |
| 12465 | } |
Lena Djokic | 77c6850 | 2016-11-24 17:08:56 +0100 | [diff] [blame] | 12466 | ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4)); |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 12467 | #else |
| 12468 | ret = get_errno(readahead(arg1, arg2, arg3)); |
| 12469 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12470 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 12471 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12472 | #ifdef CONFIG_ATTR |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 12473 | #ifdef TARGET_NR_setxattr |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12474 | case TARGET_NR_listxattr: |
| 12475 | case TARGET_NR_llistxattr: |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 12476 | { |
Laurent Vivier | 77c9f17 | 2023-09-25 17:10:29 +0200 | [diff] [blame] | 12477 | void *b = 0; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 12478 | if (arg2) { |
| 12479 | b = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 12480 | if (!b) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12481 | return -TARGET_EFAULT; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 12482 | } |
| 12483 | } |
| 12484 | p = lock_user_string(arg1); |
| 12485 | if (p) { |
| 12486 | if (num == TARGET_NR_listxattr) { |
| 12487 | ret = get_errno(listxattr(p, b, arg3)); |
| 12488 | } else { |
| 12489 | ret = get_errno(llistxattr(p, b, arg3)); |
| 12490 | } |
| 12491 | } else { |
| 12492 | ret = -TARGET_EFAULT; |
| 12493 | } |
| 12494 | unlock_user(p, arg1, 0); |
| 12495 | unlock_user(b, arg2, arg3); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12496 | return ret; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 12497 | } |
| 12498 | case TARGET_NR_flistxattr: |
| 12499 | { |
| 12500 | void *b = 0; |
| 12501 | if (arg2) { |
| 12502 | b = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 12503 | if (!b) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12504 | return -TARGET_EFAULT; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 12505 | } |
| 12506 | } |
| 12507 | ret = get_errno(flistxattr(arg1, b, arg3)); |
| 12508 | unlock_user(b, arg2, arg3); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12509 | return ret; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 12510 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12511 | case TARGET_NR_setxattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12512 | case TARGET_NR_lsetxattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12513 | { |
Laurent Vivier | 77c9f17 | 2023-09-25 17:10:29 +0200 | [diff] [blame] | 12514 | void *n, *v = 0; |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 12515 | if (arg3) { |
| 12516 | v = lock_user(VERIFY_READ, arg3, arg4, 1); |
| 12517 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12518 | return -TARGET_EFAULT; |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 12519 | } |
| 12520 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12521 | p = lock_user_string(arg1); |
| 12522 | n = lock_user_string(arg2); |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 12523 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12524 | if (num == TARGET_NR_setxattr) { |
| 12525 | ret = get_errno(setxattr(p, n, v, arg4, arg5)); |
| 12526 | } else { |
| 12527 | ret = get_errno(lsetxattr(p, n, v, arg4, arg5)); |
| 12528 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12529 | } else { |
| 12530 | ret = -TARGET_EFAULT; |
| 12531 | } |
| 12532 | unlock_user(p, arg1, 0); |
| 12533 | unlock_user(n, arg2, 0); |
| 12534 | unlock_user(v, arg3, 0); |
| 12535 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12536 | return ret; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12537 | case TARGET_NR_fsetxattr: |
| 12538 | { |
| 12539 | void *n, *v = 0; |
| 12540 | if (arg3) { |
| 12541 | v = lock_user(VERIFY_READ, arg3, arg4, 1); |
| 12542 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12543 | return -TARGET_EFAULT; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12544 | } |
| 12545 | } |
| 12546 | n = lock_user_string(arg2); |
| 12547 | if (n) { |
| 12548 | ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5)); |
| 12549 | } else { |
| 12550 | ret = -TARGET_EFAULT; |
| 12551 | } |
| 12552 | unlock_user(n, arg2, 0); |
| 12553 | unlock_user(v, arg3, 0); |
| 12554 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12555 | return ret; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12556 | case TARGET_NR_getxattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12557 | case TARGET_NR_lgetxattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12558 | { |
Laurent Vivier | 77c9f17 | 2023-09-25 17:10:29 +0200 | [diff] [blame] | 12559 | void *n, *v = 0; |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 12560 | if (arg3) { |
| 12561 | v = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
| 12562 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12563 | return -TARGET_EFAULT; |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 12564 | } |
| 12565 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12566 | p = lock_user_string(arg1); |
| 12567 | n = lock_user_string(arg2); |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 12568 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12569 | if (num == TARGET_NR_getxattr) { |
| 12570 | ret = get_errno(getxattr(p, n, v, arg4)); |
| 12571 | } else { |
| 12572 | ret = get_errno(lgetxattr(p, n, v, arg4)); |
| 12573 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12574 | } else { |
| 12575 | ret = -TARGET_EFAULT; |
| 12576 | } |
| 12577 | unlock_user(p, arg1, 0); |
| 12578 | unlock_user(n, arg2, 0); |
| 12579 | unlock_user(v, arg3, arg4); |
| 12580 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12581 | return ret; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12582 | case TARGET_NR_fgetxattr: |
| 12583 | { |
| 12584 | void *n, *v = 0; |
| 12585 | if (arg3) { |
| 12586 | v = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
| 12587 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12588 | return -TARGET_EFAULT; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12589 | } |
| 12590 | } |
| 12591 | n = lock_user_string(arg2); |
| 12592 | if (n) { |
| 12593 | ret = get_errno(fgetxattr(arg1, n, v, arg4)); |
| 12594 | } else { |
| 12595 | ret = -TARGET_EFAULT; |
| 12596 | } |
| 12597 | unlock_user(n, arg2, 0); |
| 12598 | unlock_user(v, arg3, arg4); |
| 12599 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12600 | return ret; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12601 | case TARGET_NR_removexattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12602 | case TARGET_NR_lremovexattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12603 | { |
Laurent Vivier | 77c9f17 | 2023-09-25 17:10:29 +0200 | [diff] [blame] | 12604 | void *n; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12605 | p = lock_user_string(arg1); |
| 12606 | n = lock_user_string(arg2); |
| 12607 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12608 | if (num == TARGET_NR_removexattr) { |
| 12609 | ret = get_errno(removexattr(p, n)); |
| 12610 | } else { |
| 12611 | ret = get_errno(lremovexattr(p, n)); |
| 12612 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12613 | } else { |
| 12614 | ret = -TARGET_EFAULT; |
| 12615 | } |
| 12616 | unlock_user(p, arg1, 0); |
| 12617 | unlock_user(n, arg2, 0); |
| 12618 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12619 | return ret; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 12620 | case TARGET_NR_fremovexattr: |
| 12621 | { |
| 12622 | void *n; |
| 12623 | n = lock_user_string(arg2); |
| 12624 | if (n) { |
| 12625 | ret = get_errno(fremovexattr(arg1, n)); |
| 12626 | } else { |
| 12627 | ret = -TARGET_EFAULT; |
| 12628 | } |
| 12629 | unlock_user(n, arg2, 0); |
| 12630 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12631 | return ret; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 12632 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 12633 | #endif /* CONFIG_ATTR */ |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 12634 | #ifdef TARGET_NR_set_thread_area |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 12635 | case TARGET_NR_set_thread_area: |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 12636 | #if defined(TARGET_MIPS) |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 12637 | cpu_env->active_tc.CP0_UserLocal = arg1; |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12638 | return 0; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 12639 | #elif defined(TARGET_I386) && defined(TARGET_ABI32) |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12640 | return do_set_thread_area(cpu_env, arg1); |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 12641 | #elif defined(TARGET_M68K) |
| 12642 | { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 12643 | TaskState *ts = get_task_state(cpu); |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 12644 | ts->tp_value = arg1; |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12645 | return 0; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 12646 | } |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 12647 | #else |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 12648 | return -TARGET_ENOSYS; |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 12649 | #endif |
| 12650 | #endif |
| 12651 | #ifdef TARGET_NR_get_thread_area |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 12652 | case TARGET_NR_get_thread_area: |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 12653 | #if defined(TARGET_I386) && defined(TARGET_ABI32) |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12654 | return do_get_thread_area(cpu_env, arg1); |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 12655 | #elif defined(TARGET_M68K) |
| 12656 | { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 12657 | TaskState *ts = get_task_state(cpu); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12658 | return ts->tp_value; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 12659 | } |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 12660 | #else |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 12661 | return -TARGET_ENOSYS; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 12662 | #endif |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 12663 | #endif |
bellard | 48dc41e | 2006-06-21 18:15:50 +0000 | [diff] [blame] | 12664 | #ifdef TARGET_NR_getdomainname |
| 12665 | case TARGET_NR_getdomainname: |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 12666 | return -TARGET_ENOSYS; |
bellard | 48dc41e | 2006-06-21 18:15:50 +0000 | [diff] [blame] | 12667 | #endif |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 12668 | |
Max Filippov | 12e3340 | 2018-04-01 13:13:49 -0700 | [diff] [blame] | 12669 | #ifdef TARGET_NR_clock_settime |
| 12670 | case TARGET_NR_clock_settime: |
| 12671 | { |
| 12672 | struct timespec ts; |
| 12673 | |
| 12674 | ret = target_to_host_timespec(&ts, arg2); |
| 12675 | if (!is_error(ret)) { |
| 12676 | ret = get_errno(clock_settime(arg1, &ts)); |
| 12677 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12678 | return ret; |
Max Filippov | 12e3340 | 2018-04-01 13:13:49 -0700 | [diff] [blame] | 12679 | } |
| 12680 | #endif |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 12681 | #ifdef TARGET_NR_clock_settime64 |
| 12682 | case TARGET_NR_clock_settime64: |
| 12683 | { |
| 12684 | struct timespec ts; |
| 12685 | |
| 12686 | ret = target_to_host_timespec64(&ts, arg2); |
| 12687 | if (!is_error(ret)) { |
| 12688 | ret = get_errno(clock_settime(arg1, &ts)); |
| 12689 | } |
| 12690 | return ret; |
| 12691 | } |
| 12692 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 12693 | #ifdef TARGET_NR_clock_gettime |
| 12694 | case TARGET_NR_clock_gettime: |
| 12695 | { |
| 12696 | struct timespec ts; |
| 12697 | ret = get_errno(clock_gettime(arg1, &ts)); |
| 12698 | if (!is_error(ret)) { |
Max Filippov | b9f9908 | 2018-04-01 13:14:04 -0700 | [diff] [blame] | 12699 | ret = host_to_target_timespec(arg2, &ts); |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 12700 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12701 | return ret; |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 12702 | } |
| 12703 | #endif |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 12704 | #ifdef TARGET_NR_clock_gettime64 |
| 12705 | case TARGET_NR_clock_gettime64: |
| 12706 | { |
| 12707 | struct timespec ts; |
| 12708 | ret = get_errno(clock_gettime(arg1, &ts)); |
| 12709 | if (!is_error(ret)) { |
| 12710 | ret = host_to_target_timespec64(arg2, &ts); |
| 12711 | } |
| 12712 | return ret; |
| 12713 | } |
| 12714 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 12715 | #ifdef TARGET_NR_clock_getres |
| 12716 | case TARGET_NR_clock_getres: |
| 12717 | { |
| 12718 | struct timespec ts; |
| 12719 | ret = get_errno(clock_getres(arg1, &ts)); |
| 12720 | if (!is_error(ret)) { |
| 12721 | host_to_target_timespec(arg2, &ts); |
| 12722 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12723 | return ret; |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 12724 | } |
| 12725 | #endif |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 12726 | #ifdef TARGET_NR_clock_getres_time64 |
| 12727 | case TARGET_NR_clock_getres_time64: |
| 12728 | { |
| 12729 | struct timespec ts; |
| 12730 | ret = get_errno(clock_getres(arg1, &ts)); |
| 12731 | if (!is_error(ret)) { |
| 12732 | host_to_target_timespec64(arg2, &ts); |
| 12733 | } |
| 12734 | return ret; |
| 12735 | } |
| 12736 | #endif |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 12737 | #ifdef TARGET_NR_clock_nanosleep |
| 12738 | case TARGET_NR_clock_nanosleep: |
| 12739 | { |
| 12740 | struct timespec ts; |
Filip Bozuta | b09d640 | 2020-07-27 22:13:26 +0200 | [diff] [blame] | 12741 | if (target_to_host_timespec(&ts, arg3)) { |
| 12742 | return -TARGET_EFAULT; |
| 12743 | } |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 12744 | ret = get_errno(safe_clock_nanosleep(arg1, arg2, |
| 12745 | &ts, arg4 ? &ts : NULL)); |
Laurent Vivier | 8ec68a0 | 2020-07-24 07:45:05 +0100 | [diff] [blame] | 12746 | /* |
| 12747 | * if the call is interrupted by a signal handler, it fails |
| 12748 | * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not |
| 12749 | * TIMER_ABSTIME, it returns the remaining unslept time in arg4. |
| 12750 | */ |
Filip Bozuta | b09d640 | 2020-07-27 22:13:26 +0200 | [diff] [blame] | 12751 | if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME && |
| 12752 | host_to_target_timespec(arg4, &ts)) { |
| 12753 | return -TARGET_EFAULT; |
Laurent Vivier | 8ec68a0 | 2020-07-24 07:45:05 +0100 | [diff] [blame] | 12754 | } |
Tom Musta | 8fbe8fd | 2014-08-12 13:53:41 -0500 | [diff] [blame] | 12755 | |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12756 | return ret; |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 12757 | } |
| 12758 | #endif |
Filip Bozuta | 6ac03b2 | 2020-08-24 21:21:15 +0200 | [diff] [blame] | 12759 | #ifdef TARGET_NR_clock_nanosleep_time64 |
| 12760 | case TARGET_NR_clock_nanosleep_time64: |
| 12761 | { |
| 12762 | struct timespec ts; |
| 12763 | |
| 12764 | if (target_to_host_timespec64(&ts, arg3)) { |
| 12765 | return -TARGET_EFAULT; |
| 12766 | } |
| 12767 | |
| 12768 | ret = get_errno(safe_clock_nanosleep(arg1, arg2, |
| 12769 | &ts, arg4 ? &ts : NULL)); |
| 12770 | |
| 12771 | if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME && |
| 12772 | host_to_target_timespec64(arg4, &ts)) { |
| 12773 | return -TARGET_EFAULT; |
| 12774 | } |
| 12775 | return ret; |
| 12776 | } |
| 12777 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 12778 | |
Helge Deller | 9a7f682 | 2022-05-28 12:52:10 +0200 | [diff] [blame] | 12779 | #if defined(TARGET_NR_set_tid_address) |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 12780 | case TARGET_NR_set_tid_address: |
Helge Deller | 9a7f682 | 2022-05-28 12:52:10 +0200 | [diff] [blame] | 12781 | { |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 12782 | TaskState *ts = get_task_state(cpu); |
Helge Deller | 9a7f682 | 2022-05-28 12:52:10 +0200 | [diff] [blame] | 12783 | ts->child_tidptr = arg1; |
| 12784 | /* do not call host set_tid_address() syscall, instead return tid() */ |
| 12785 | return get_errno(sys_gettid()); |
| 12786 | } |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 12787 | #endif |
| 12788 | |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 12789 | case TARGET_NR_tkill: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12790 | return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2))); |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 12791 | |
ths | 7145557 | 2007-06-21 21:45:30 +0000 | [diff] [blame] | 12792 | case TARGET_NR_tgkill: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12793 | return get_errno(safe_tgkill((int)arg1, (int)arg2, |
| 12794 | target_to_host_signal(arg3))); |
ths | 7145557 | 2007-06-21 21:45:30 +0000 | [diff] [blame] | 12795 | |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 12796 | #ifdef TARGET_NR_set_robust_list |
| 12797 | case TARGET_NR_set_robust_list: |
Peter Maydell | e9a970a | 2013-02-08 04:34:54 +0000 | [diff] [blame] | 12798 | case TARGET_NR_get_robust_list: |
| 12799 | /* The ABI for supporting robust futexes has userspace pass |
| 12800 | * the kernel a pointer to a linked list which is updated by |
| 12801 | * userspace after the syscall; the list is walked by the kernel |
| 12802 | * when the thread exits. Since the linked list in QEMU guest |
| 12803 | * memory isn't a valid linked list for the host and we have |
| 12804 | * no way to reliably intercept the thread-death event, we can't |
| 12805 | * support these. Silently return ENOSYS so that guest userspace |
| 12806 | * falls back to a non-robust futex implementation (which should |
| 12807 | * be OK except in the corner case of the guest crashing while |
| 12808 | * holding a mutex that is shared with another process via |
| 12809 | * shared memory). |
| 12810 | */ |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 12811 | return -TARGET_ENOSYS; |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 12812 | #endif |
| 12813 | |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 12814 | #if defined(TARGET_NR_utimensat) |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 12815 | case TARGET_NR_utimensat: |
| 12816 | { |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 12817 | struct timespec *tsp, ts[2]; |
| 12818 | if (!arg3) { |
| 12819 | tsp = NULL; |
| 12820 | } else { |
Filip Bozuta | b3a3af7 | 2020-08-11 13:31:01 +0200 | [diff] [blame] | 12821 | if (target_to_host_timespec(ts, arg3)) { |
| 12822 | return -TARGET_EFAULT; |
| 12823 | } |
| 12824 | if (target_to_host_timespec(ts + 1, arg3 + |
| 12825 | sizeof(struct target_timespec))) { |
| 12826 | return -TARGET_EFAULT; |
| 12827 | } |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 12828 | tsp = ts; |
| 12829 | } |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 12830 | if (!arg2) |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 12831 | ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 12832 | else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12833 | if (!(p = lock_user_string(arg2))) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 12834 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12835 | } |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 12836 | ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 12837 | unlock_user(p, arg2, 0); |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 12838 | } |
| 12839 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12840 | return ret; |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 12841 | #endif |
Filip Bozuta | cac46eb | 2020-08-25 00:30:50 +0200 | [diff] [blame] | 12842 | #ifdef TARGET_NR_utimensat_time64 |
| 12843 | case TARGET_NR_utimensat_time64: |
| 12844 | { |
| 12845 | struct timespec *tsp, ts[2]; |
| 12846 | if (!arg3) { |
| 12847 | tsp = NULL; |
| 12848 | } else { |
| 12849 | if (target_to_host_timespec64(ts, arg3)) { |
| 12850 | return -TARGET_EFAULT; |
| 12851 | } |
| 12852 | if (target_to_host_timespec64(ts + 1, arg3 + |
| 12853 | sizeof(struct target__kernel_timespec))) { |
| 12854 | return -TARGET_EFAULT; |
| 12855 | } |
| 12856 | tsp = ts; |
| 12857 | } |
| 12858 | if (!arg2) |
| 12859 | ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); |
| 12860 | else { |
| 12861 | p = lock_user_string(arg2); |
| 12862 | if (!p) { |
| 12863 | return -TARGET_EFAULT; |
| 12864 | } |
| 12865 | ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); |
| 12866 | unlock_user(p, arg2, 0); |
| 12867 | } |
| 12868 | } |
| 12869 | return ret; |
| 12870 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 12871 | #ifdef TARGET_NR_futex |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 12872 | case TARGET_NR_futex: |
Richard Henderson | 0fbc0f8 | 2022-08-28 19:09:59 -0700 | [diff] [blame] | 12873 | return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6); |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 12874 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 12875 | #ifdef TARGET_NR_futex_time64 |
| 12876 | case TARGET_NR_futex_time64: |
Richard Henderson | 0fbc0f8 | 2022-08-28 19:09:59 -0700 | [diff] [blame] | 12877 | return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6); |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 12878 | #endif |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12879 | #ifdef CONFIG_INOTIFY |
| 12880 | #if defined(TARGET_NR_inotify_init) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12881 | case TARGET_NR_inotify_init: |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12882 | ret = get_errno(inotify_init()); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 12883 | if (ret >= 0) { |
| 12884 | fd_trans_register(ret, &target_inotify_trans); |
| 12885 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12886 | return ret; |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12887 | #endif |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12888 | #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1) |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 12889 | case TARGET_NR_inotify_init1: |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12890 | ret = get_errno(inotify_init1(target_to_host_bitmask(arg1, |
Lena Djokic | fea243e | 2016-11-24 17:08:53 +0100 | [diff] [blame] | 12891 | fcntl_flags_tbl))); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 12892 | if (ret >= 0) { |
| 12893 | fd_trans_register(ret, &target_inotify_trans); |
| 12894 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12895 | return ret; |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 12896 | #endif |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12897 | #if defined(TARGET_NR_inotify_add_watch) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12898 | case TARGET_NR_inotify_add_watch: |
| 12899 | p = lock_user_string(arg2); |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12900 | ret = get_errno(inotify_add_watch(arg1, path(p), arg3)); |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12901 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12902 | return ret; |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12903 | #endif |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12904 | #if defined(TARGET_NR_inotify_rm_watch) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12905 | case TARGET_NR_inotify_rm_watch: |
Paul Brook | 33f53ac | 2022-01-26 20:26:36 +0000 | [diff] [blame] | 12906 | return get_errno(inotify_rm_watch(arg1, arg2)); |
| 12907 | #endif |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 12908 | #endif |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 12909 | |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 12910 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12911 | case TARGET_NR_mq_open: |
| 12912 | { |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 12913 | struct mq_attr posix_mq_attr; |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 12914 | struct mq_attr *pposix_mq_attr; |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 12915 | int host_flags; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12916 | |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 12917 | host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl); |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 12918 | pposix_mq_attr = NULL; |
| 12919 | if (arg4) { |
| 12920 | if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12921 | return -TARGET_EFAULT; |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 12922 | } |
| 12923 | pposix_mq_attr = &posix_mq_attr; |
Tom Musta | b6ce1f6 | 2014-08-12 13:53:36 -0500 | [diff] [blame] | 12924 | } |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 12925 | p = lock_user_string(arg1 - 1); |
| 12926 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12927 | return -TARGET_EFAULT; |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 12928 | } |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 12929 | ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12930 | unlock_user (p, arg1, 0); |
| 12931 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12932 | return ret; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12933 | |
| 12934 | case TARGET_NR_mq_unlink: |
| 12935 | p = lock_user_string(arg1 - 1); |
Peter Maydell | 3211215 | 2016-07-12 13:02:13 +0100 | [diff] [blame] | 12936 | if (!p) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12937 | return -TARGET_EFAULT; |
Peter Maydell | 3211215 | 2016-07-12 13:02:13 +0100 | [diff] [blame] | 12938 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12939 | ret = get_errno(mq_unlink(p)); |
| 12940 | unlock_user (p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12941 | return ret; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12942 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 12943 | #ifdef TARGET_NR_mq_timedsend |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12944 | case TARGET_NR_mq_timedsend: |
| 12945 | { |
| 12946 | struct timespec ts; |
| 12947 | |
| 12948 | p = lock_user (VERIFY_READ, arg2, arg3, 1); |
| 12949 | if (arg5 != 0) { |
Filip Bozuta | dcbcf5c | 2020-08-24 21:37:51 +0200 | [diff] [blame] | 12950 | if (target_to_host_timespec(&ts, arg5)) { |
| 12951 | return -TARGET_EFAULT; |
| 12952 | } |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 12953 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts)); |
Filip Bozuta | dcbcf5c | 2020-08-24 21:37:51 +0200 | [diff] [blame] | 12954 | if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) { |
| 12955 | return -TARGET_EFAULT; |
| 12956 | } |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 12957 | } else { |
| 12958 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12959 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12960 | unlock_user (p, arg2, arg3); |
| 12961 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12962 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 12963 | #endif |
Filip Bozuta | d107e37 | 2020-08-24 21:37:52 +0200 | [diff] [blame] | 12964 | #ifdef TARGET_NR_mq_timedsend_time64 |
| 12965 | case TARGET_NR_mq_timedsend_time64: |
| 12966 | { |
| 12967 | struct timespec ts; |
| 12968 | |
| 12969 | p = lock_user(VERIFY_READ, arg2, arg3, 1); |
| 12970 | if (arg5 != 0) { |
| 12971 | if (target_to_host_timespec64(&ts, arg5)) { |
| 12972 | return -TARGET_EFAULT; |
| 12973 | } |
| 12974 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts)); |
| 12975 | if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) { |
| 12976 | return -TARGET_EFAULT; |
| 12977 | } |
| 12978 | } else { |
| 12979 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL)); |
| 12980 | } |
| 12981 | unlock_user(p, arg2, arg3); |
| 12982 | } |
| 12983 | return ret; |
| 12984 | #endif |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12985 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 12986 | #ifdef TARGET_NR_mq_timedreceive |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12987 | case TARGET_NR_mq_timedreceive: |
| 12988 | { |
| 12989 | struct timespec ts; |
| 12990 | unsigned int prio; |
| 12991 | |
| 12992 | p = lock_user (VERIFY_READ, arg2, arg3, 1); |
| 12993 | if (arg5 != 0) { |
Filip Bozuta | dcbcf5c | 2020-08-24 21:37:51 +0200 | [diff] [blame] | 12994 | if (target_to_host_timespec(&ts, arg5)) { |
| 12995 | return -TARGET_EFAULT; |
| 12996 | } |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 12997 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 12998 | &prio, &ts)); |
Filip Bozuta | dcbcf5c | 2020-08-24 21:37:51 +0200 | [diff] [blame] | 12999 | if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) { |
| 13000 | return -TARGET_EFAULT; |
| 13001 | } |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 13002 | } else { |
| 13003 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 13004 | &prio, NULL)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13005 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13006 | unlock_user (p, arg2, arg3); |
| 13007 | if (arg4 != 0) |
| 13008 | put_user_u32(prio, arg4); |
| 13009 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13010 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 13011 | #endif |
Filip Bozuta | d107e37 | 2020-08-24 21:37:52 +0200 | [diff] [blame] | 13012 | #ifdef TARGET_NR_mq_timedreceive_time64 |
| 13013 | case TARGET_NR_mq_timedreceive_time64: |
| 13014 | { |
| 13015 | struct timespec ts; |
| 13016 | unsigned int prio; |
| 13017 | |
| 13018 | p = lock_user(VERIFY_READ, arg2, arg3, 1); |
| 13019 | if (arg5 != 0) { |
| 13020 | if (target_to_host_timespec64(&ts, arg5)) { |
| 13021 | return -TARGET_EFAULT; |
| 13022 | } |
| 13023 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 13024 | &prio, &ts)); |
| 13025 | if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) { |
| 13026 | return -TARGET_EFAULT; |
| 13027 | } |
| 13028 | } else { |
| 13029 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 13030 | &prio, NULL)); |
| 13031 | } |
| 13032 | unlock_user(p, arg2, arg3); |
| 13033 | if (arg4 != 0) { |
| 13034 | put_user_u32(prio, arg4); |
| 13035 | } |
| 13036 | } |
| 13037 | return ret; |
| 13038 | #endif |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13039 | |
| 13040 | /* Not implemented for now... */ |
| 13041 | /* case TARGET_NR_mq_notify: */ |
| 13042 | /* break; */ |
| 13043 | |
| 13044 | case TARGET_NR_mq_getsetattr: |
| 13045 | { |
| 13046 | struct mq_attr posix_mq_attr_in, posix_mq_attr_out; |
| 13047 | ret = 0; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13048 | if (arg2 != 0) { |
| 13049 | copy_from_user_mq_attr(&posix_mq_attr_in, arg2); |
Max Filippov | a23ea40 | 2018-03-31 08:20:15 -0700 | [diff] [blame] | 13050 | ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in, |
| 13051 | &posix_mq_attr_out)); |
| 13052 | } else if (arg3 != 0) { |
| 13053 | ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13054 | } |
Max Filippov | a23ea40 | 2018-03-31 08:20:15 -0700 | [diff] [blame] | 13055 | if (ret == 0 && arg3 != 0) { |
| 13056 | copy_to_user_mq_attr(arg3, &posix_mq_attr_out); |
| 13057 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13058 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13059 | return ret; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 13060 | #endif |
| 13061 | |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13062 | #ifdef CONFIG_SPLICE |
| 13063 | #ifdef TARGET_NR_tee |
| 13064 | case TARGET_NR_tee: |
| 13065 | { |
| 13066 | ret = get_errno(tee(arg1,arg2,arg3,arg4)); |
| 13067 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13068 | return ret; |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13069 | #endif |
| 13070 | #ifdef TARGET_NR_splice |
| 13071 | case TARGET_NR_splice: |
| 13072 | { |
| 13073 | loff_t loff_in, loff_out; |
| 13074 | loff_t *ploff_in = NULL, *ploff_out = NULL; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13075 | if (arg2) { |
| 13076 | if (get_user_u64(loff_in, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13077 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13078 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13079 | ploff_in = &loff_in; |
| 13080 | } |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13081 | if (arg4) { |
| 13082 | if (get_user_u64(loff_out, arg4)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13083 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13084 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13085 | ploff_out = &loff_out; |
| 13086 | } |
| 13087 | ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6)); |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13088 | if (arg2) { |
| 13089 | if (put_user_u64(loff_in, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13090 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13091 | } |
| 13092 | } |
| 13093 | if (arg4) { |
| 13094 | if (put_user_u64(loff_out, arg4)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13095 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 13096 | } |
| 13097 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13098 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13099 | return ret; |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13100 | #endif |
| 13101 | #ifdef TARGET_NR_vmsplice |
| 13102 | case TARGET_NR_vmsplice: |
| 13103 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 13104 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 13105 | if (vec != NULL) { |
| 13106 | ret = get_errno(vmsplice(arg1, vec, arg3, arg4)); |
| 13107 | unlock_iovec(vec, arg2, arg3, 0); |
| 13108 | } else { |
| 13109 | ret = -host_to_target_errno(errno); |
| 13110 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13111 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13112 | return ret; |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 13113 | #endif |
| 13114 | #endif /* CONFIG_SPLICE */ |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 13115 | #ifdef CONFIG_EVENTFD |
| 13116 | #if defined(TARGET_NR_eventfd) |
| 13117 | case TARGET_NR_eventfd: |
| 13118 | ret = get_errno(eventfd(arg1, 0)); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 13119 | if (ret >= 0) { |
| 13120 | fd_trans_register(ret, &target_eventfd_trans); |
| 13121 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13122 | return ret; |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 13123 | #endif |
| 13124 | #if defined(TARGET_NR_eventfd2) |
| 13125 | case TARGET_NR_eventfd2: |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 13126 | { |
Helge Deller | 7872130 | 2021-02-10 07:12:14 +0100 | [diff] [blame] | 13127 | int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)); |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 13128 | if (arg2 & TARGET_O_NONBLOCK) { |
| 13129 | host_flags |= O_NONBLOCK; |
| 13130 | } |
| 13131 | if (arg2 & TARGET_O_CLOEXEC) { |
| 13132 | host_flags |= O_CLOEXEC; |
| 13133 | } |
| 13134 | ret = get_errno(eventfd(arg1, host_flags)); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 13135 | if (ret >= 0) { |
| 13136 | fd_trans_register(ret, &target_eventfd_trans); |
| 13137 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13138 | return ret; |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 13139 | } |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 13140 | #endif |
| 13141 | #endif /* CONFIG_EVENTFD */ |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 13142 | #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate) |
| 13143 | case TARGET_NR_fallocate: |
WANG Xuerui | eeed229 | 2022-10-06 16:55:00 +0800 | [diff] [blame] | 13144 | #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32) |
Alexander Graf | 20249ae | 2012-02-06 21:37:07 +0100 | [diff] [blame] | 13145 | ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4), |
| 13146 | target_offset64(arg5, arg6))); |
| 13147 | #else |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 13148 | ret = get_errno(fallocate(arg1, arg2, arg3, arg4)); |
Alexander Graf | 20249ae | 2012-02-06 21:37:07 +0100 | [diff] [blame] | 13149 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13150 | return ret; |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 13151 | #endif |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13152 | #if defined(CONFIG_SYNC_FILE_RANGE) |
| 13153 | #if defined(TARGET_NR_sync_file_range) |
| 13154 | case TARGET_NR_sync_file_range: |
WANG Xuerui | eeed229 | 2022-10-06 16:55:00 +0800 | [diff] [blame] | 13155 | #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32) |
Riku Voipio | bfcedc5 | 2011-06-20 16:24:39 +0300 | [diff] [blame] | 13156 | #if defined(TARGET_MIPS) |
| 13157 | ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), |
| 13158 | target_offset64(arg5, arg6), arg7)); |
| 13159 | #else |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13160 | ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3), |
| 13161 | target_offset64(arg4, arg5), arg6)); |
Riku Voipio | bfcedc5 | 2011-06-20 16:24:39 +0300 | [diff] [blame] | 13162 | #endif /* !TARGET_MIPS */ |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13163 | #else |
| 13164 | ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4)); |
| 13165 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13166 | return ret; |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13167 | #endif |
Laurent Vivier | 5bcb498 | 2020-03-10 11:33:50 +0100 | [diff] [blame] | 13168 | #if defined(TARGET_NR_sync_file_range2) || \ |
| 13169 | defined(TARGET_NR_arm_sync_file_range) |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13170 | #if defined(TARGET_NR_sync_file_range2) |
| 13171 | case TARGET_NR_sync_file_range2: |
Laurent Vivier | 5bcb498 | 2020-03-10 11:33:50 +0100 | [diff] [blame] | 13172 | #endif |
| 13173 | #if defined(TARGET_NR_arm_sync_file_range) |
| 13174 | case TARGET_NR_arm_sync_file_range: |
| 13175 | #endif |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13176 | /* This is like sync_file_range but the arguments are reordered */ |
WANG Xuerui | eeed229 | 2022-10-06 16:55:00 +0800 | [diff] [blame] | 13177 | #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32) |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13178 | ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), |
| 13179 | target_offset64(arg5, arg6), arg2)); |
| 13180 | #else |
| 13181 | ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2)); |
| 13182 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13183 | return ret; |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 13184 | #endif |
| 13185 | #endif |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 13186 | #if defined(TARGET_NR_signalfd4) |
| 13187 | case TARGET_NR_signalfd4: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13188 | return do_signalfd4(arg1, arg2, arg4); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 13189 | #endif |
| 13190 | #if defined(TARGET_NR_signalfd) |
| 13191 | case TARGET_NR_signalfd: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13192 | return do_signalfd4(arg1, arg2, 0); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 13193 | #endif |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13194 | #if defined(CONFIG_EPOLL) |
| 13195 | #if defined(TARGET_NR_epoll_create) |
| 13196 | case TARGET_NR_epoll_create: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13197 | return get_errno(epoll_create(arg1)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13198 | #endif |
| 13199 | #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1) |
| 13200 | case TARGET_NR_epoll_create1: |
Sergei Trofimovich | 386d386 | 2020-04-15 23:05:08 +0100 | [diff] [blame] | 13201 | return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl))); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13202 | #endif |
| 13203 | #if defined(TARGET_NR_epoll_ctl) |
| 13204 | case TARGET_NR_epoll_ctl: |
| 13205 | { |
| 13206 | struct epoll_event ep; |
| 13207 | struct epoll_event *epp = 0; |
| 13208 | if (arg4) { |
LemonBoy | c781102 | 2020-04-17 17:34:54 +0200 | [diff] [blame] | 13209 | if (arg2 != EPOLL_CTL_DEL) { |
| 13210 | struct target_epoll_event *target_ep; |
| 13211 | if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) { |
| 13212 | return -TARGET_EFAULT; |
| 13213 | } |
| 13214 | ep.events = tswap32(target_ep->events); |
| 13215 | /* |
| 13216 | * The epoll_data_t union is just opaque data to the kernel, |
| 13217 | * so we transfer all 64 bits across and need not worry what |
| 13218 | * actual data type it is. |
| 13219 | */ |
| 13220 | ep.data.u64 = tswap64(target_ep->data.u64); |
| 13221 | unlock_user_struct(target_ep, arg4, 0); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13222 | } |
LemonBoy | c781102 | 2020-04-17 17:34:54 +0200 | [diff] [blame] | 13223 | /* |
| 13224 | * before kernel 2.6.9, EPOLL_CTL_DEL operation required a |
| 13225 | * non-null pointer, even though this argument is ignored. |
| 13226 | * |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13227 | */ |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13228 | epp = &ep; |
| 13229 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13230 | return get_errno(epoll_ctl(arg1, arg2, arg3, epp)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13231 | } |
| 13232 | #endif |
| 13233 | |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 13234 | #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13235 | #if defined(TARGET_NR_epoll_wait) |
| 13236 | case TARGET_NR_epoll_wait: |
| 13237 | #endif |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 13238 | #if defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13239 | case TARGET_NR_epoll_pwait: |
| 13240 | #endif |
| 13241 | { |
| 13242 | struct target_epoll_event *target_ep; |
| 13243 | struct epoll_event *ep; |
| 13244 | int epfd = arg1; |
| 13245 | int maxevents = arg3; |
| 13246 | int timeout = arg4; |
| 13247 | |
Peter Maydell | 2ba7fae3 | 2016-07-18 15:35:59 +0100 | [diff] [blame] | 13248 | if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13249 | return -TARGET_EINVAL; |
Peter Maydell | 2ba7fae3 | 2016-07-18 15:35:59 +0100 | [diff] [blame] | 13250 | } |
| 13251 | |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13252 | target_ep = lock_user(VERIFY_WRITE, arg2, |
| 13253 | maxevents * sizeof(struct target_epoll_event), 1); |
| 13254 | if (!target_ep) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13255 | return -TARGET_EFAULT; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13256 | } |
| 13257 | |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 13258 | ep = g_try_new(struct epoll_event, maxevents); |
| 13259 | if (!ep) { |
| 13260 | unlock_user(target_ep, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13261 | return -TARGET_ENOMEM; |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 13262 | } |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13263 | |
| 13264 | switch (num) { |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 13265 | #if defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13266 | case TARGET_NR_epoll_pwait: |
| 13267 | { |
Richard Henderson | cd0e31a | 2022-03-15 01:43:07 -0700 | [diff] [blame] | 13268 | sigset_t *set = NULL; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13269 | |
| 13270 | if (arg5) { |
Richard Henderson | cd0e31a | 2022-03-15 01:43:07 -0700 | [diff] [blame] | 13271 | ret = process_sigsuspend_mask(&set, arg5, arg6); |
| 13272 | if (ret != 0) { |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 13273 | break; |
| 13274 | } |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13275 | } |
| 13276 | |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 13277 | ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, |
| 13278 | set, SIGSET_T_SIZE)); |
Richard Henderson | cd0e31a | 2022-03-15 01:43:07 -0700 | [diff] [blame] | 13279 | |
| 13280 | if (set) { |
| 13281 | finish_sigsuspend_mask(ret); |
| 13282 | } |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13283 | break; |
| 13284 | } |
| 13285 | #endif |
| 13286 | #if defined(TARGET_NR_epoll_wait) |
| 13287 | case TARGET_NR_epoll_wait: |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 13288 | ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, |
| 13289 | NULL, 0)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13290 | break; |
| 13291 | #endif |
| 13292 | default: |
| 13293 | ret = -TARGET_ENOSYS; |
| 13294 | } |
| 13295 | if (!is_error(ret)) { |
| 13296 | int i; |
| 13297 | for (i = 0; i < ret; i++) { |
| 13298 | target_ep[i].events = tswap32(ep[i].events); |
| 13299 | target_ep[i].data.u64 = tswap64(ep[i].data.u64); |
| 13300 | } |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 13301 | unlock_user(target_ep, arg2, |
| 13302 | ret * sizeof(struct target_epoll_event)); |
| 13303 | } else { |
| 13304 | unlock_user(target_ep, arg2, 0); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13305 | } |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 13306 | g_free(ep); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13307 | return ret; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 13308 | } |
| 13309 | #endif |
| 13310 | #endif |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13311 | #ifdef TARGET_NR_prlimit64 |
| 13312 | case TARGET_NR_prlimit64: |
| 13313 | { |
| 13314 | /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */ |
| 13315 | struct target_rlimit64 *target_rnew, *target_rold; |
| 13316 | struct host_rlimit64 rnew, rold, *rnewp = 0; |
Felix Janda | 9501801 | 2014-12-02 22:11:17 +0100 | [diff] [blame] | 13317 | int resource = target_to_host_resource(arg2); |
Tobias Koch | 055d92f | 2020-03-05 21:24:00 +0100 | [diff] [blame] | 13318 | |
| 13319 | if (arg3 && (resource != RLIMIT_AS && |
| 13320 | resource != RLIMIT_DATA && |
| 13321 | resource != RLIMIT_STACK)) { |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13322 | if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13323 | return -TARGET_EFAULT; |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13324 | } |
Ilya Leoshkevich | 9c1da8b | 2023-02-24 01:39:06 +0100 | [diff] [blame] | 13325 | __get_user(rnew.rlim_cur, &target_rnew->rlim_cur); |
| 13326 | __get_user(rnew.rlim_max, &target_rnew->rlim_max); |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13327 | unlock_user_struct(target_rnew, arg3, 0); |
| 13328 | rnewp = &rnew; |
| 13329 | } |
| 13330 | |
Felix Janda | 9501801 | 2014-12-02 22:11:17 +0100 | [diff] [blame] | 13331 | ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0)); |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13332 | if (!is_error(ret) && arg4) { |
| 13333 | if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13334 | return -TARGET_EFAULT; |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13335 | } |
Ilya Leoshkevich | 9c1da8b | 2023-02-24 01:39:06 +0100 | [diff] [blame] | 13336 | __put_user(rold.rlim_cur, &target_rold->rlim_cur); |
| 13337 | __put_user(rold.rlim_max, &target_rold->rlim_max); |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13338 | unlock_user_struct(target_rold, arg4, 1); |
| 13339 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13340 | return ret; |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 13341 | } |
| 13342 | #endif |
Richard Henderson | 3d21d29 | 2012-09-15 13:20:46 -0700 | [diff] [blame] | 13343 | #ifdef TARGET_NR_gethostname |
| 13344 | case TARGET_NR_gethostname: |
| 13345 | { |
| 13346 | char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0); |
| 13347 | if (name) { |
| 13348 | ret = get_errno(gethostname(name, arg2)); |
| 13349 | unlock_user(name, arg1, arg2); |
| 13350 | } else { |
| 13351 | ret = -TARGET_EFAULT; |
| 13352 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13353 | return ret; |
Richard Henderson | 3d21d29 | 2012-09-15 13:20:46 -0700 | [diff] [blame] | 13354 | } |
| 13355 | #endif |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 13356 | #ifdef TARGET_NR_atomic_cmpxchg_32 |
| 13357 | case TARGET_NR_atomic_cmpxchg_32: |
| 13358 | { |
| 13359 | /* should use start_exclusive from main.c */ |
| 13360 | abi_ulong mem_value; |
| 13361 | if (get_user_u32(mem_value, arg6)) { |
| 13362 | target_siginfo_t info; |
| 13363 | info.si_signo = SIGSEGV; |
| 13364 | info.si_errno = 0; |
| 13365 | info.si_code = TARGET_SEGV_MAPERR; |
| 13366 | info._sifields._sigfault._addr = arg6; |
Philippe Mathieu-Daudé | 0effdc2 | 2022-05-09 22:57:28 +0200 | [diff] [blame] | 13367 | queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info); |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 13368 | ret = 0xdeadbeef; |
| 13369 | |
| 13370 | } |
| 13371 | if (mem_value == arg2) |
| 13372 | put_user_u32(arg1, arg6); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13373 | return mem_value; |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 13374 | } |
| 13375 | #endif |
| 13376 | #ifdef TARGET_NR_atomic_barrier |
| 13377 | case TARGET_NR_atomic_barrier: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13378 | /* Like the kernel implementation and the |
| 13379 | qemu arm barrier, no-op this? */ |
| 13380 | return 0; |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 13381 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13382 | |
| 13383 | #ifdef TARGET_NR_timer_create |
| 13384 | case TARGET_NR_timer_create: |
| 13385 | { |
| 13386 | /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */ |
| 13387 | |
| 13388 | struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13389 | |
| 13390 | int clkid = arg1; |
| 13391 | int timer_index = next_free_host_timer(); |
| 13392 | |
| 13393 | if (timer_index < 0) { |
| 13394 | ret = -TARGET_EAGAIN; |
| 13395 | } else { |
| 13396 | timer_t *phtimer = g_posix_timers + timer_index; |
| 13397 | |
| 13398 | if (arg2) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13399 | phost_sevp = &host_sevp; |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 13400 | ret = target_to_host_sigevent(phost_sevp, arg2); |
| 13401 | if (ret != 0) { |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 13402 | free_host_timer_slot(timer_index); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13403 | return ret; |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 13404 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13405 | } |
| 13406 | |
| 13407 | ret = get_errno(timer_create(clkid, phost_sevp, phtimer)); |
| 13408 | if (ret) { |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 13409 | free_host_timer_slot(timer_index); |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13410 | } else { |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13411 | if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) { |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 13412 | timer_delete(*phtimer); |
| 13413 | free_host_timer_slot(timer_index); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13414 | return -TARGET_EFAULT; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13415 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13416 | } |
| 13417 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13418 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13419 | } |
| 13420 | #endif |
| 13421 | |
| 13422 | #ifdef TARGET_NR_timer_settime |
| 13423 | case TARGET_NR_timer_settime: |
| 13424 | { |
| 13425 | /* args: timer_t timerid, int flags, const struct itimerspec *new_value, |
| 13426 | * struct itimerspec * old_value */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13427 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13428 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13429 | if (timerid < 0) { |
| 13430 | ret = timerid; |
| 13431 | } else if (arg3 == 0) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13432 | ret = -TARGET_EINVAL; |
| 13433 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13434 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13435 | struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; |
| 13436 | |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 13437 | if (target_to_host_itimerspec(&hspec_new, arg3)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13438 | return -TARGET_EFAULT; |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 13439 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13440 | ret = get_errno( |
| 13441 | timer_settime(htimer, arg2, &hspec_new, &hspec_old)); |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 13442 | if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13443 | return -TARGET_EFAULT; |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 13444 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13445 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13446 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13447 | } |
| 13448 | #endif |
| 13449 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 13450 | #ifdef TARGET_NR_timer_settime64 |
| 13451 | case TARGET_NR_timer_settime64: |
| 13452 | { |
| 13453 | target_timer_t timerid = get_timer_id(arg1); |
| 13454 | |
| 13455 | if (timerid < 0) { |
| 13456 | ret = timerid; |
| 13457 | } else if (arg3 == 0) { |
| 13458 | ret = -TARGET_EINVAL; |
| 13459 | } else { |
| 13460 | timer_t htimer = g_posix_timers[timerid]; |
| 13461 | struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; |
| 13462 | |
| 13463 | if (target_to_host_itimerspec64(&hspec_new, arg3)) { |
| 13464 | return -TARGET_EFAULT; |
| 13465 | } |
| 13466 | ret = get_errno( |
| 13467 | timer_settime(htimer, arg2, &hspec_new, &hspec_old)); |
| 13468 | if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) { |
| 13469 | return -TARGET_EFAULT; |
| 13470 | } |
| 13471 | } |
| 13472 | return ret; |
| 13473 | } |
| 13474 | #endif |
| 13475 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13476 | #ifdef TARGET_NR_timer_gettime |
| 13477 | case TARGET_NR_timer_gettime: |
| 13478 | { |
| 13479 | /* args: timer_t timerid, struct itimerspec *curr_value */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13480 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13481 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13482 | if (timerid < 0) { |
| 13483 | ret = timerid; |
| 13484 | } else if (!arg2) { |
| 13485 | ret = -TARGET_EFAULT; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13486 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13487 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13488 | struct itimerspec hspec; |
| 13489 | ret = get_errno(timer_gettime(htimer, &hspec)); |
| 13490 | |
| 13491 | if (host_to_target_itimerspec(arg2, &hspec)) { |
| 13492 | ret = -TARGET_EFAULT; |
| 13493 | } |
| 13494 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13495 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13496 | } |
| 13497 | #endif |
| 13498 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 13499 | #ifdef TARGET_NR_timer_gettime64 |
| 13500 | case TARGET_NR_timer_gettime64: |
| 13501 | { |
| 13502 | /* args: timer_t timerid, struct itimerspec64 *curr_value */ |
| 13503 | target_timer_t timerid = get_timer_id(arg1); |
| 13504 | |
| 13505 | if (timerid < 0) { |
| 13506 | ret = timerid; |
| 13507 | } else if (!arg2) { |
| 13508 | ret = -TARGET_EFAULT; |
| 13509 | } else { |
| 13510 | timer_t htimer = g_posix_timers[timerid]; |
| 13511 | struct itimerspec hspec; |
| 13512 | ret = get_errno(timer_gettime(htimer, &hspec)); |
| 13513 | |
| 13514 | if (host_to_target_itimerspec64(arg2, &hspec)) { |
| 13515 | ret = -TARGET_EFAULT; |
| 13516 | } |
| 13517 | } |
| 13518 | return ret; |
| 13519 | } |
| 13520 | #endif |
| 13521 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13522 | #ifdef TARGET_NR_timer_getoverrun |
| 13523 | case TARGET_NR_timer_getoverrun: |
| 13524 | { |
| 13525 | /* args: timer_t timerid */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13526 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13527 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13528 | if (timerid < 0) { |
| 13529 | ret = timerid; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13530 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13531 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13532 | ret = get_errno(timer_getoverrun(htimer)); |
| 13533 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13534 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13535 | } |
| 13536 | #endif |
| 13537 | |
| 13538 | #ifdef TARGET_NR_timer_delete |
| 13539 | case TARGET_NR_timer_delete: |
| 13540 | { |
| 13541 | /* args: timer_t timerid */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13542 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13543 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 13544 | if (timerid < 0) { |
| 13545 | ret = timerid; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13546 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 13547 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13548 | ret = get_errno(timer_delete(htimer)); |
Peter Maydell | 9e59899 | 2022-07-25 12:00:35 +0100 | [diff] [blame] | 13549 | free_host_timer_slot(timerid); |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13550 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13551 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 13552 | } |
| 13553 | #endif |
| 13554 | |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13555 | #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD) |
| 13556 | case TARGET_NR_timerfd_create: |
Mathis Marion | d759a62 | 2023-02-20 09:58:19 +0100 | [diff] [blame] | 13557 | ret = get_errno(timerfd_create(arg1, |
| 13558 | target_to_host_bitmask(arg2, fcntl_flags_tbl))); |
| 13559 | if (ret >= 0) { |
| 13560 | fd_trans_register(ret, &target_timerfd_trans); |
| 13561 | } |
| 13562 | return ret; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13563 | #endif |
| 13564 | |
| 13565 | #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD) |
| 13566 | case TARGET_NR_timerfd_gettime: |
| 13567 | { |
| 13568 | struct itimerspec its_curr; |
| 13569 | |
| 13570 | ret = get_errno(timerfd_gettime(arg1, &its_curr)); |
| 13571 | |
| 13572 | if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13573 | return -TARGET_EFAULT; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13574 | } |
| 13575 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13576 | return ret; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13577 | #endif |
| 13578 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 13579 | #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD) |
| 13580 | case TARGET_NR_timerfd_gettime64: |
| 13581 | { |
| 13582 | struct itimerspec its_curr; |
| 13583 | |
| 13584 | ret = get_errno(timerfd_gettime(arg1, &its_curr)); |
| 13585 | |
| 13586 | if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) { |
| 13587 | return -TARGET_EFAULT; |
| 13588 | } |
| 13589 | } |
| 13590 | return ret; |
| 13591 | #endif |
| 13592 | |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13593 | #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD) |
| 13594 | case TARGET_NR_timerfd_settime: |
| 13595 | { |
| 13596 | struct itimerspec its_new, its_old, *p_new; |
| 13597 | |
| 13598 | if (arg3) { |
| 13599 | if (target_to_host_itimerspec(&its_new, arg3)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13600 | return -TARGET_EFAULT; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13601 | } |
| 13602 | p_new = &its_new; |
| 13603 | } else { |
| 13604 | p_new = NULL; |
| 13605 | } |
| 13606 | |
| 13607 | ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old)); |
| 13608 | |
| 13609 | if (arg4 && host_to_target_itimerspec(arg4, &its_old)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 13610 | return -TARGET_EFAULT; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13611 | } |
| 13612 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13613 | return ret; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 13614 | #endif |
| 13615 | |
Filip Bozuta | 828cb3a | 2020-07-22 17:34:21 +0200 | [diff] [blame] | 13616 | #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD) |
| 13617 | case TARGET_NR_timerfd_settime64: |
| 13618 | { |
| 13619 | struct itimerspec its_new, its_old, *p_new; |
| 13620 | |
| 13621 | if (arg3) { |
| 13622 | if (target_to_host_itimerspec64(&its_new, arg3)) { |
| 13623 | return -TARGET_EFAULT; |
| 13624 | } |
| 13625 | p_new = &its_new; |
| 13626 | } else { |
| 13627 | p_new = NULL; |
| 13628 | } |
| 13629 | |
| 13630 | ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old)); |
| 13631 | |
| 13632 | if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) { |
| 13633 | return -TARGET_EFAULT; |
| 13634 | } |
| 13635 | } |
| 13636 | return ret; |
| 13637 | #endif |
| 13638 | |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 13639 | #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) |
| 13640 | case TARGET_NR_ioprio_get: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13641 | return get_errno(ioprio_get(arg1, arg2)); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 13642 | #endif |
| 13643 | |
| 13644 | #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) |
| 13645 | case TARGET_NR_ioprio_set: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13646 | return get_errno(ioprio_set(arg1, arg2, arg3)); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 13647 | #endif |
| 13648 | |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 13649 | #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS) |
| 13650 | case TARGET_NR_setns: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13651 | return get_errno(setns(arg1, arg2)); |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 13652 | #endif |
| 13653 | #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS) |
| 13654 | case TARGET_NR_unshare: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13655 | return get_errno(unshare(arg1)); |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 13656 | #endif |
Laurent Vivier | 2f14788 | 2016-09-25 22:20:20 +0200 | [diff] [blame] | 13657 | #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp) |
| 13658 | case TARGET_NR_kcmp: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13659 | return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5)); |
Laurent Vivier | 2f14788 | 2016-09-25 22:20:20 +0200 | [diff] [blame] | 13660 | #endif |
Richard Henderson | fa97e38 | 2018-07-18 13:06:48 -0700 | [diff] [blame] | 13661 | #ifdef TARGET_NR_swapcontext |
| 13662 | case TARGET_NR_swapcontext: |
| 13663 | /* PowerPC specific. */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13664 | return do_swapcontext(cpu_env, arg1, arg2, arg3); |
Richard Henderson | fa97e38 | 2018-07-18 13:06:48 -0700 | [diff] [blame] | 13665 | #endif |
Shu-Chun Weng | 9bdfa4d | 2019-08-19 11:09:47 -0700 | [diff] [blame] | 13666 | #ifdef TARGET_NR_memfd_create |
| 13667 | case TARGET_NR_memfd_create: |
| 13668 | p = lock_user_string(arg1); |
| 13669 | if (!p) { |
| 13670 | return -TARGET_EFAULT; |
| 13671 | } |
| 13672 | ret = get_errno(memfd_create(p, arg2)); |
| 13673 | fd_trans_unregister(ret); |
| 13674 | unlock_user(p, arg1, 0); |
| 13675 | return ret; |
| 13676 | #endif |
Andreas Schwab | 8500476 | 2019-05-13 11:02:53 +0200 | [diff] [blame] | 13677 | #if defined TARGET_NR_membarrier && defined __NR_membarrier |
| 13678 | case TARGET_NR_membarrier: |
| 13679 | return get_errno(membarrier(arg1, arg2)); |
| 13680 | #endif |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 13681 | |
Andreas Schwab | 8494645 | 2020-11-12 12:45:16 +0100 | [diff] [blame] | 13682 | #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range) |
| 13683 | case TARGET_NR_copy_file_range: |
| 13684 | { |
| 13685 | loff_t inoff, outoff; |
| 13686 | loff_t *pinoff = NULL, *poutoff = NULL; |
| 13687 | |
| 13688 | if (arg2) { |
| 13689 | if (get_user_u64(inoff, arg2)) { |
| 13690 | return -TARGET_EFAULT; |
| 13691 | } |
| 13692 | pinoff = &inoff; |
| 13693 | } |
| 13694 | if (arg4) { |
| 13695 | if (get_user_u64(outoff, arg4)) { |
| 13696 | return -TARGET_EFAULT; |
| 13697 | } |
| 13698 | poutoff = &outoff; |
| 13699 | } |
Giuseppe Musacchio | 0fa259d | 2021-05-03 19:41:59 +0200 | [diff] [blame] | 13700 | /* Do not sign-extend the count parameter. */ |
Andreas Schwab | 8494645 | 2020-11-12 12:45:16 +0100 | [diff] [blame] | 13701 | ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff, |
Giuseppe Musacchio | 0fa259d | 2021-05-03 19:41:59 +0200 | [diff] [blame] | 13702 | (abi_ulong)arg5, arg6)); |
Andreas Schwab | 8494645 | 2020-11-12 12:45:16 +0100 | [diff] [blame] | 13703 | if (!is_error(ret) && ret > 0) { |
| 13704 | if (arg2) { |
| 13705 | if (put_user_u64(inoff, arg2)) { |
| 13706 | return -TARGET_EFAULT; |
| 13707 | } |
| 13708 | } |
| 13709 | if (arg4) { |
| 13710 | if (put_user_u64(outoff, arg4)) { |
| 13711 | return -TARGET_EFAULT; |
| 13712 | } |
| 13713 | } |
| 13714 | } |
| 13715 | } |
| 13716 | return ret; |
| 13717 | #endif |
| 13718 | |
YAMAMOTO Takashi | e10fbe8 | 2021-05-31 14:50:12 +0900 | [diff] [blame] | 13719 | #if defined(TARGET_NR_pivot_root) |
| 13720 | case TARGET_NR_pivot_root: |
| 13721 | { |
| 13722 | void *p2; |
| 13723 | p = lock_user_string(arg1); /* new_root */ |
| 13724 | p2 = lock_user_string(arg2); /* put_old */ |
| 13725 | if (!p || !p2) { |
| 13726 | ret = -TARGET_EFAULT; |
| 13727 | } else { |
| 13728 | ret = get_errno(pivot_root(p, p2)); |
| 13729 | } |
| 13730 | unlock_user(p2, arg2, 0); |
| 13731 | unlock_user(p, arg1, 0); |
| 13732 | } |
| 13733 | return ret; |
| 13734 | #endif |
| 13735 | |
Robbin Ehn | 9e1c7d9 | 2023-06-19 10:24:03 +0200 | [diff] [blame] | 13736 | #if defined(TARGET_NR_riscv_hwprobe) |
| 13737 | case TARGET_NR_riscv_hwprobe: |
| 13738 | return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5); |
| 13739 | #endif |
| 13740 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 13741 | default: |
Philippe Mathieu-Daudé | 122f9c8 | 2018-07-06 12:51:25 -0300 | [diff] [blame] | 13742 | qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 13743 | return -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 13744 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 13745 | return ret; |
| 13746 | } |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13747 | |
Philippe Mathieu-Daudé | a0939b8 | 2022-05-09 22:57:27 +0200 | [diff] [blame] | 13748 | abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1, |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13749 | abi_long arg2, abi_long arg3, abi_long arg4, |
| 13750 | abi_long arg5, abi_long arg6, abi_long arg7, |
| 13751 | abi_long arg8) |
| 13752 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 13753 | CPUState *cpu = env_cpu(cpu_env); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13754 | abi_long ret; |
| 13755 | |
| 13756 | #ifdef DEBUG_ERESTARTSYS |
| 13757 | /* Debug-only code for exercising the syscall-restart code paths |
| 13758 | * in the per-architecture cpu main loops: restart every syscall |
| 13759 | * the guest makes once before letting it through. |
| 13760 | */ |
| 13761 | { |
| 13762 | static bool flag; |
| 13763 | flag = !flag; |
| 13764 | if (flag) { |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 13765 | return -QEMU_ERESTARTSYS; |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13766 | } |
| 13767 | } |
| 13768 | #endif |
| 13769 | |
Emilio G. Cota | c36f7a6 | 2018-10-21 13:27:44 -0400 | [diff] [blame] | 13770 | record_syscall_start(cpu, num, arg1, |
| 13771 | arg2, arg3, arg4, arg5, arg6, arg7, arg8); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13772 | |
Josh Kunz | 4b25a50 | 2020-02-03 18:54:14 -0800 | [diff] [blame] | 13773 | if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { |
Filip Bozuta | e400e11 | 2020-08-11 18:45:49 +0200 | [diff] [blame] | 13774 | print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6); |
Josh Kunz | 4b25a50 | 2020-02-03 18:54:14 -0800 | [diff] [blame] | 13775 | } |
| 13776 | |
| 13777 | ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4, |
| 13778 | arg5, arg6, arg7, arg8); |
| 13779 | |
| 13780 | if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { |
Filip Bozuta | e400e11 | 2020-08-11 18:45:49 +0200 | [diff] [blame] | 13781 | print_syscall_ret(cpu_env, num, ret, arg1, arg2, |
| 13782 | arg3, arg4, arg5, arg6); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13783 | } |
| 13784 | |
Emilio G. Cota | c36f7a6 | 2018-10-21 13:27:44 -0400 | [diff] [blame] | 13785 | record_syscall_return(cpu, num, ret); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 13786 | return ret; |
| 13787 | } |