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" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 25 | #include <elf.h> |
| 26 | #include <endian.h> |
John Spencer | c56dc77 | 2012-12-10 07:59:46 +0100 | [diff] [blame] | 27 | #include <grp.h> |
ths | d08d3bb | 2007-03-19 13:09:22 +0000 | [diff] [blame] | 28 | #include <sys/ipc.h> |
| 29 | #include <sys/msg.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 30 | #include <sys/wait.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 31 | #include <sys/mount.h> |
John Spencer | 586b0be | 2012-12-26 00:49:49 +0100 | [diff] [blame] | 32 | #include <sys/file.h> |
| 33 | #include <sys/fsuid.h> |
| 34 | #include <sys/personality.h> |
ths | 39b9aae | 2007-02-11 18:36:44 +0000 | [diff] [blame] | 35 | #include <sys/prctl.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 36 | #include <sys/resource.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 37 | #include <sys/swap.h> |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 38 | #include <linux/capability.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 39 | #include <sched.h> |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 40 | #include <sys/timex.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 41 | #include <sys/socket.h> |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 42 | #include <linux/sockios.h> |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 43 | #include <sys/un.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 44 | #include <sys/uio.h> |
Felix Janda | 0839f11 | 2016-09-30 19:40:21 -0400 | [diff] [blame] | 45 | #include <poll.h> |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 46 | #include <sys/times.h> |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 47 | #include <sys/shm.h> |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 48 | #include <sys/sem.h> |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 49 | #include <sys/statfs.h> |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 50 | #include <utime.h> |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 51 | #include <sys/sysinfo.h> |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 52 | #include <sys/signalfd.h> |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 53 | //#include <sys/user.h> |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 54 | #include <netinet/ip.h> |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 55 | #include <netinet/tcp.h> |
Laurent Vivier | 86fcd94 | 2011-03-30 01:35:23 +0200 | [diff] [blame] | 56 | #include <linux/wireless.h> |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 57 | #include <linux/icmp.h> |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 58 | #include <linux/icmpv6.h> |
| 59 | #include <linux/errqueue.h> |
Marco A L Barbosa | d6d6d6f | 2017-10-05 10:55:30 -0300 | [diff] [blame] | 60 | #include <linux/random.h> |
Riku Voipio | d80a190 | 2014-10-01 16:05:46 +0300 | [diff] [blame] | 61 | #ifdef CONFIG_TIMERFD |
| 62 | #include <sys/timerfd.h> |
| 63 | #endif |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 64 | #ifdef CONFIG_EVENTFD |
| 65 | #include <sys/eventfd.h> |
| 66 | #endif |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 67 | #ifdef CONFIG_EPOLL |
| 68 | #include <sys/epoll.h> |
| 69 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_ATTR |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 71 | #include "qemu/xattr.h" |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 72 | #endif |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 73 | #ifdef CONFIG_SENDFILE |
| 74 | #include <sys/sendfile.h> |
| 75 | #endif |
Aleksandar Markovic | bd27e67 | 2020-01-16 23:49:50 +0100 | [diff] [blame] | 76 | #ifdef CONFIG_KCOV |
| 77 | #include <sys/kcov.h> |
| 78 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 79 | |
| 80 | #define termios host_termios |
| 81 | #define winsize host_winsize |
| 82 | #define termio host_termio |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 83 | #define sgttyb host_sgttyb /* same as target */ |
| 84 | #define tchars host_tchars /* same as target */ |
| 85 | #define ltchars host_ltchars /* same as target */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 86 | |
| 87 | #include <linux/termios.h> |
| 88 | #include <linux/unistd.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 89 | #include <linux/cdrom.h> |
| 90 | #include <linux/hdreg.h> |
| 91 | #include <linux/soundcard.h> |
bellard | 19b84f3 | 2003-05-08 15:41:49 +0000 | [diff] [blame] | 92 | #include <linux/kd.h> |
balrog | 8fbd6b5 | 2008-09-20 03:03:09 +0000 | [diff] [blame] | 93 | #include <linux/mtio.h> |
Martin Mohring | 350d177 | 2009-05-04 21:21:41 +0300 | [diff] [blame] | 94 | #include <linux/fs.h> |
Yunqiang Su | ab22b4d | 2019-09-04 14:59:24 +0200 | [diff] [blame] | 95 | #include <linux/fd.h> |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 96 | #if defined(CONFIG_FIEMAP) |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 97 | #include <linux/fiemap.h> |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 98 | #endif |
Ulrich Hecht | f7680a5 | 2009-10-16 17:00:44 +0200 | [diff] [blame] | 99 | #include <linux/fb.h> |
Cortland Tölva | 6c753a6 | 2018-10-08 09:35:20 -0700 | [diff] [blame] | 100 | #if defined(CONFIG_USBFS) |
| 101 | #include <linux/usbdevice_fs.h> |
Cortland Tölva | a133367 | 2018-10-08 09:35:21 -0700 | [diff] [blame] | 102 | #include <linux/usb/ch9.h> |
Cortland Tölva | 6c753a6 | 2018-10-08 09:35:20 -0700 | [diff] [blame] | 103 | #endif |
Ulrich Hecht | f7680a5 | 2009-10-16 17:00:44 +0200 | [diff] [blame] | 104 | #include <linux/vt.h> |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 105 | #include <linux/dm-ioctl.h> |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 106 | #include <linux/reboot.h> |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 107 | #include <linux/route.h> |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 108 | #include <linux/filter.h> |
Andreas Färber | fff8c53 | 2014-01-18 07:38:30 +0100 | [diff] [blame] | 109 | #include <linux/blkpg.h> |
Laurent Vivier | a82ea93 | 2016-06-27 00:18:22 +0200 | [diff] [blame] | 110 | #include <netpacket/packet.h> |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 111 | #include <linux/netlink.h> |
Yunqiang Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 112 | #include <linux/if_alg.h> |
Filip Bozuta | 68365f9 | 2020-01-15 20:36:35 +0100 | [diff] [blame] | 113 | #include <linux/rtc.h> |
Filip Bozuta | 1c4c6fc | 2020-01-15 20:36:41 +0100 | [diff] [blame] | 114 | #include <sound/asound.h> |
Chen Gang | e865b97 | 2020-06-05 09:32:21 +0800 | [diff] [blame] | 115 | #ifdef HAVE_DRM_H |
| 116 | #include <libdrm/drm.h> |
| 117 | #endif |
pbrook | d7e4036 | 2008-05-23 16:06:43 +0000 | [diff] [blame] | 118 | #include "linux_loop.h" |
Riku Voipio | 18cb008 | 2014-02-19 12:59:58 +0200 | [diff] [blame] | 119 | #include "uname.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 120 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 121 | #include "qemu.h" |
Richard Henderson | 5ebdd77 | 2019-03-14 13:10:53 -0700 | [diff] [blame] | 122 | #include "qemu/guest-random.h" |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 123 | #include "qemu/selfmap.h" |
Emilio G. Cota | c36f7a6 | 2018-10-21 13:27:44 -0400 | [diff] [blame] | 124 | #include "user/syscall-trace.h" |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 125 | #include "qapi/error.h" |
Laurent Vivier | f7e6a40 | 2018-08-24 00:22:15 +0200 | [diff] [blame] | 126 | #include "fd-trans.h" |
Philippe Mathieu-Daudé | dcb32f1 | 2020-01-01 12:23:00 +0100 | [diff] [blame] | 127 | #include "tcg/tcg.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 128 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 129 | #ifndef CLONE_IO |
| 130 | #define CLONE_IO 0x80000000 /* Clone io context */ |
| 131 | #endif |
| 132 | |
| 133 | /* We can't directly call the host clone syscall, because this will |
| 134 | * badly confuse libc (breaking mutexes, for example). So we must |
| 135 | * divide clone flags into: |
| 136 | * * flag combinations that look like pthread_create() |
| 137 | * * flag combinations that look like fork() |
| 138 | * * flags we can implement within QEMU itself |
| 139 | * * flags we can't support and will return an error for |
| 140 | */ |
| 141 | /* For thread creation, all these flags must be present; for |
| 142 | * fork, none must be present. |
| 143 | */ |
| 144 | #define CLONE_THREAD_FLAGS \ |
| 145 | (CLONE_VM | CLONE_FS | CLONE_FILES | \ |
| 146 | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM) |
| 147 | |
| 148 | /* These flags are ignored: |
| 149 | * CLONE_DETACHED is now ignored by the kernel; |
| 150 | * CLONE_IO is just an optimisation hint to the I/O scheduler |
| 151 | */ |
| 152 | #define CLONE_IGNORED_FLAGS \ |
| 153 | (CLONE_DETACHED | CLONE_IO) |
| 154 | |
| 155 | /* Flags for fork which we can implement within QEMU itself */ |
| 156 | #define CLONE_OPTIONAL_FORK_FLAGS \ |
| 157 | (CLONE_SETTLS | CLONE_PARENT_SETTID | \ |
| 158 | CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID) |
| 159 | |
| 160 | /* Flags for thread creation which we can implement within QEMU itself */ |
| 161 | #define CLONE_OPTIONAL_THREAD_FLAGS \ |
| 162 | (CLONE_SETTLS | CLONE_PARENT_SETTID | \ |
| 163 | CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT) |
| 164 | |
| 165 | #define CLONE_INVALID_FORK_FLAGS \ |
| 166 | (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS)) |
| 167 | |
| 168 | #define CLONE_INVALID_THREAD_FLAGS \ |
| 169 | (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \ |
| 170 | CLONE_IGNORED_FLAGS)) |
| 171 | |
| 172 | /* CLONE_VFORK is special cased early in do_fork(). The other flag bits |
| 173 | * have almost all been allocated. We cannot support any of |
| 174 | * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC, |
| 175 | * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED. |
| 176 | * The checks against the invalid thread masks above will catch these. |
| 177 | * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.) |
| 178 | */ |
pbrook | 30813ce | 2008-06-02 15:45:44 +0000 | [diff] [blame] | 179 | |
Timothy E Baldwin | 71a8f7f | 2016-05-12 18:47:45 +0100 | [diff] [blame] | 180 | /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted |
| 181 | * once. This exercises the codepaths for restart. |
| 182 | */ |
| 183 | //#define DEBUG_ERESTARTSYS |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 184 | |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 185 | //#include <linux/msdos_fs.h> |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 186 | #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2]) |
| 187 | #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2]) |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 188 | |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 189 | #undef _syscall0 |
| 190 | #undef _syscall1 |
| 191 | #undef _syscall2 |
| 192 | #undef _syscall3 |
| 193 | #undef _syscall4 |
| 194 | #undef _syscall5 |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 195 | #undef _syscall6 |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 196 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 197 | #define _syscall0(type,name) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 198 | static type name (void) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 199 | { \ |
| 200 | return syscall(__NR_##name); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 201 | } |
| 202 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 203 | #define _syscall1(type,name,type1,arg1) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 204 | static type name (type1 arg1) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 205 | { \ |
| 206 | return syscall(__NR_##name, arg1); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 207 | } |
| 208 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 209 | #define _syscall2(type,name,type1,arg1,type2,arg2) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 210 | static type name (type1 arg1,type2 arg2) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 211 | { \ |
| 212 | return syscall(__NR_##name, arg1, arg2); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 213 | } |
| 214 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 215 | #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 216 | static type name (type1 arg1,type2 arg2,type3 arg3) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 217 | { \ |
| 218 | return syscall(__NR_##name, arg1, arg2, arg3); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 219 | } |
| 220 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 221 | #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 222 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 223 | { \ |
| 224 | return syscall(__NR_##name, arg1, arg2, arg3, arg4); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 225 | } |
| 226 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 227 | #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
| 228 | type5,arg5) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 229 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 230 | { \ |
| 231 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 232 | } |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
| 236 | type5,arg5,type6,arg6) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 237 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \ |
| 238 | type6 arg6) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 239 | { \ |
| 240 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ |
| 241 | } |
| 242 | |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 243 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 244 | #define __NR_sys_uname __NR_uname |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 245 | #define __NR_sys_getcwd1 __NR_getcwd |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 246 | #define __NR_sys_getdents __NR_getdents |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 247 | #define __NR_sys_getdents64 __NR_getdents64 |
ths | c6cda17 | 2007-10-09 03:42:34 +0000 | [diff] [blame] | 248 | #define __NR_sys_getpriority __NR_getpriority |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 249 | #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 250 | #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 251 | #define __NR_sys_syslog __NR_syslog |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 252 | #if defined(__NR_futex) |
| 253 | # define __NR_sys_futex __NR_futex |
| 254 | #endif |
| 255 | #if defined(__NR_futex_time64) |
| 256 | # define __NR_sys_futex_time64 __NR_futex_time64 |
| 257 | #endif |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 258 | #define __NR_sys_inotify_init __NR_inotify_init |
| 259 | #define __NR_sys_inotify_add_watch __NR_inotify_add_watch |
| 260 | #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 261 | #define __NR_sys_statx __NR_statx |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 262 | |
Peter Maydell | b1cef6d | 2018-01-25 16:19:49 +0000 | [diff] [blame] | 263 | #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__) |
bellard | 9af9eaa | 2003-04-07 21:34:41 +0000 | [diff] [blame] | 264 | #define __NR__llseek __NR_lseek |
| 265 | #endif |
| 266 | |
James Hogan | a29e5ba | 2014-03-25 21:51:08 +0000 | [diff] [blame] | 267 | /* Newer kernel ports have llseek() instead of _llseek() */ |
| 268 | #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek) |
| 269 | #define TARGET_NR__llseek TARGET_NR_llseek |
| 270 | #endif |
| 271 | |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 272 | #define __NR_sys_gettid __NR_gettid |
| 273 | _syscall0(int, sys_gettid) |
Peter Maydell | 2b3f64c | 2018-04-19 13:57:40 +0100 | [diff] [blame] | 274 | |
| 275 | /* For the 64-bit guest on 32-bit host case we must emulate |
| 276 | * getdents using getdents64, because otherwise the host |
| 277 | * might hand us back more dirent records than we can fit |
| 278 | * into the guest buffer after structure format conversion. |
| 279 | * Otherwise we emulate getdents with getdents if the host has it. |
| 280 | */ |
| 281 | #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS |
| 282 | #define EMULATE_GETDENTS_WITH_GETDENTS |
| 283 | #endif |
| 284 | |
| 285 | #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 286 | _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count); |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 287 | #endif |
Peter Maydell | 2b3f64c | 2018-04-19 13:57:40 +0100 | [diff] [blame] | 288 | #if (defined(TARGET_NR_getdents) && \ |
| 289 | !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \ |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 290 | (defined(TARGET_NR_getdents64) && defined(__NR_getdents64)) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 291 | _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count); |
| 292 | #endif |
Richard Henderson | d35b261 | 2010-06-04 12:14:10 -0700 | [diff] [blame] | 293 | #if defined(TARGET_NR__llseek) && defined(__NR_llseek) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 294 | _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, |
| 295 | loff_t *, res, uint, wh); |
| 296 | #endif |
Miloš Stojanović | c1a402a | 2017-05-15 16:59:45 +0200 | [diff] [blame] | 297 | _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] | 298 | _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig, |
| 299 | siginfo_t *, uinfo) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 300 | _syscall3(int,sys_syslog,int,type,char*,bufp,int,len) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 301 | #ifdef __NR_exit_group |
| 302 | _syscall1(int,exit_group,int,error_code) |
| 303 | #endif |
| 304 | #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) |
| 305 | _syscall1(int,set_tid_address,int *,tidptr) |
| 306 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 307 | #if defined(__NR_futex) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 308 | _syscall6(int,sys_futex,int *,uaddr,int,op,int,val, |
| 309 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 310 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 311 | #if defined(__NR_futex_time64) |
| 312 | _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val, |
| 313 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 314 | #endif |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 315 | #define __NR_sys_sched_getaffinity __NR_sched_getaffinity |
| 316 | _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len, |
| 317 | unsigned long *, user_mask_ptr); |
| 318 | #define __NR_sys_sched_setaffinity __NR_sched_setaffinity |
| 319 | _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len, |
| 320 | unsigned long *, user_mask_ptr); |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 321 | #define __NR_sys_getcpu __NR_getcpu |
| 322 | _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache); |
Alexander Graf | 0f6b4d2 | 2011-09-27 14:39:42 +0200 | [diff] [blame] | 323 | _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, |
| 324 | void *, arg); |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 325 | _syscall2(int, capget, struct __user_cap_header_struct *, header, |
| 326 | struct __user_cap_data_struct *, data); |
| 327 | _syscall2(int, capset, struct __user_cap_header_struct *, header, |
| 328 | struct __user_cap_data_struct *, data); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 329 | #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) |
| 330 | _syscall2(int, ioprio_get, int, which, int, who) |
| 331 | #endif |
| 332 | #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) |
| 333 | _syscall3(int, ioprio_set, int, which, int, who, int, ioprio) |
| 334 | #endif |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 335 | #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) |
| 336 | _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags) |
| 337 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 338 | |
Laurent Vivier | 2f14788 | 2016-09-25 22:20:20 +0200 | [diff] [blame] | 339 | #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp) |
| 340 | _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type, |
| 341 | unsigned long, idx1, unsigned long, idx2) |
| 342 | #endif |
| 343 | |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 344 | /* |
| 345 | * It is assumed that struct statx is architecture independent. |
| 346 | */ |
| 347 | #if defined(TARGET_NR_statx) && defined(__NR_statx) |
| 348 | _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags, |
| 349 | unsigned int, mask, struct target_statx *, statxbuf) |
| 350 | #endif |
Andreas Schwab | 8500476 | 2019-05-13 11:02:53 +0200 | [diff] [blame] | 351 | #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier) |
| 352 | _syscall2(int, membarrier, int, cmd, int, flags) |
| 353 | #endif |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 354 | |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 355 | static bitmask_transtbl fcntl_flags_tbl[] = { |
| 356 | { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, }, |
| 357 | { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, }, |
| 358 | { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, }, |
| 359 | { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, }, |
| 360 | { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, }, |
| 361 | { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, }, |
| 362 | { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, }, |
| 363 | { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, }, |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 364 | { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, }, |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 365 | { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, }, |
| 366 | { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, }, |
| 367 | { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, }, |
| 368 | { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, }, |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 369 | #if defined(O_DIRECT) |
| 370 | { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, }, |
| 371 | #endif |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 372 | #if defined(O_NOATIME) |
| 373 | { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME }, |
| 374 | #endif |
| 375 | #if defined(O_CLOEXEC) |
| 376 | { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC }, |
| 377 | #endif |
| 378 | #if defined(O_PATH) |
| 379 | { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH }, |
| 380 | #endif |
Riku Voipio | 5f9cee4 | 2017-08-08 16:01:19 +0300 | [diff] [blame] | 381 | #if defined(O_TMPFILE) |
| 382 | { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE }, |
| 383 | #endif |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 384 | /* Don't terminate the list prematurely on 64-bit host+guest. */ |
| 385 | #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0 |
| 386 | { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, }, |
| 387 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 388 | { 0, 0, 0, 0 } |
| 389 | }; |
| 390 | |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 391 | static int sys_getcwd1(char *buf, size_t size) |
| 392 | { |
| 393 | if (getcwd(buf, size) == NULL) { |
| 394 | /* getcwd() sets errno */ |
| 395 | return (-1); |
| 396 | } |
aurel32 | aaf4ad3 | 2009-04-16 14:17:14 +0000 | [diff] [blame] | 397 | return strlen(buf)+1; |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 400 | #ifdef TARGET_NR_utimensat |
Peter Maydell | 700fa58 | 2016-07-18 11:47:55 +0100 | [diff] [blame] | 401 | #if defined(__NR_utimensat) |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 402 | #define __NR_sys_utimensat __NR_utimensat |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 403 | _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, |
| 404 | const struct timespec *,tsp,int,flags) |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 405 | #else |
| 406 | static int sys_utimensat(int dirfd, const char *pathname, |
| 407 | const struct timespec times[2], int flags) |
| 408 | { |
| 409 | errno = ENOSYS; |
| 410 | return -1; |
| 411 | } |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 412 | #endif |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 413 | #endif /* TARGET_NR_utimensat */ |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 414 | |
Andreas Schwab | 95d0307 | 2018-01-23 11:53:31 +0100 | [diff] [blame] | 415 | #ifdef TARGET_NR_renameat2 |
| 416 | #if defined(__NR_renameat2) |
| 417 | #define __NR_sys_renameat2 __NR_renameat2 |
| 418 | _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd, |
| 419 | const char *, new, unsigned int, flags) |
| 420 | #else |
| 421 | static int sys_renameat2(int oldfd, const char *old, |
| 422 | int newfd, const char *new, int flags) |
| 423 | { |
| 424 | if (flags == 0) { |
| 425 | return renameat(oldfd, old, newfd, new); |
| 426 | } |
| 427 | errno = ENOSYS; |
| 428 | return -1; |
| 429 | } |
| 430 | #endif |
| 431 | #endif /* TARGET_NR_renameat2 */ |
| 432 | |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 433 | #ifdef CONFIG_INOTIFY |
aurel32 | 8690e42 | 2009-04-17 13:50:32 +0000 | [diff] [blame] | 434 | #include <sys/inotify.h> |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 435 | |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 436 | #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 437 | static int sys_inotify_init(void) |
| 438 | { |
| 439 | return (inotify_init()); |
| 440 | } |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 441 | #endif |
| 442 | #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 443 | static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask) |
| 444 | { |
| 445 | return (inotify_add_watch(fd, pathname, mask)); |
| 446 | } |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 447 | #endif |
| 448 | #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 449 | static int sys_inotify_rm_watch(int fd, int32_t wd) |
| 450 | { |
aurel32 | 8690e42 | 2009-04-17 13:50:32 +0000 | [diff] [blame] | 451 | return (inotify_rm_watch(fd, wd)); |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 452 | } |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 453 | #endif |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 454 | #ifdef CONFIG_INOTIFY1 |
| 455 | #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1) |
| 456 | static int sys_inotify_init1(int flags) |
| 457 | { |
| 458 | return (inotify_init1(flags)); |
| 459 | } |
| 460 | #endif |
| 461 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 462 | #else |
| 463 | /* Userspace can usually survive runtime without inotify */ |
| 464 | #undef TARGET_NR_inotify_init |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 465 | #undef TARGET_NR_inotify_init1 |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 466 | #undef TARGET_NR_inotify_add_watch |
| 467 | #undef TARGET_NR_inotify_rm_watch |
| 468 | #endif /* CONFIG_INOTIFY */ |
| 469 | |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 470 | #if defined(TARGET_NR_prlimit64) |
| 471 | #ifndef __NR_prlimit64 |
| 472 | # define __NR_prlimit64 -1 |
| 473 | #endif |
| 474 | #define __NR_sys_prlimit64 __NR_prlimit64 |
| 475 | /* The glibc rlimit structure may not be that used by the underlying syscall */ |
| 476 | struct host_rlimit64 { |
| 477 | uint64_t rlim_cur; |
| 478 | uint64_t rlim_max; |
| 479 | }; |
| 480 | _syscall4(int, sys_prlimit64, pid_t, pid, int, resource, |
| 481 | const struct host_rlimit64 *, new_limit, |
| 482 | struct host_rlimit64 *, old_limit) |
| 483 | #endif |
| 484 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 485 | |
| 486 | #if defined(TARGET_NR_timer_create) |
| 487 | /* Maxiumum of 32 active POSIX timers allowed at any one time. */ |
| 488 | static timer_t g_posix_timers[32] = { 0, } ; |
| 489 | |
| 490 | static inline int next_free_host_timer(void) |
| 491 | { |
| 492 | int k ; |
| 493 | /* FIXME: Does finding the next free slot require a lock? */ |
| 494 | for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) { |
| 495 | if (g_posix_timers[k] == 0) { |
| 496 | g_posix_timers[k] = (timer_t) 1; |
| 497 | return k; |
| 498 | } |
| 499 | } |
| 500 | return -1; |
| 501 | } |
| 502 | #endif |
| 503 | |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 504 | /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */ |
Alexander Graf | 4a1def4 | 2012-09-29 15:32:38 +0000 | [diff] [blame] | 505 | #ifdef TARGET_ARM |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 506 | static inline int regpairs_aligned(void *cpu_env, int num) |
| 507 | { |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 508 | return ((((CPUARMState *)cpu_env)->eabi) == 1) ; |
| 509 | } |
Aleksandar Markovic | d7779ac | 2016-09-19 13:44:42 +0200 | [diff] [blame] | 510 | #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32) |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 511 | static inline int regpairs_aligned(void *cpu_env, int num) { return 1; } |
Alexander Graf | 4a1def4 | 2012-09-29 15:32:38 +0000 | [diff] [blame] | 512 | #elif defined(TARGET_PPC) && !defined(TARGET_PPC64) |
| 513 | /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs |
| 514 | * of registers which translates to the same as ARM/MIPS, because we start with |
| 515 | * r3 as arg1 */ |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 516 | static inline int regpairs_aligned(void *cpu_env, int num) { return 1; } |
| 517 | #elif defined(TARGET_SH4) |
| 518 | /* SH4 doesn't align register pairs, except for p{read,write}64 */ |
| 519 | static inline int regpairs_aligned(void *cpu_env, int num) |
| 520 | { |
| 521 | switch (num) { |
| 522 | case TARGET_NR_pread64: |
| 523 | case TARGET_NR_pwrite64: |
| 524 | return 1; |
| 525 | |
| 526 | default: |
| 527 | return 0; |
| 528 | } |
| 529 | } |
Max Filippov | ba7651f | 2017-01-25 10:54:11 -0800 | [diff] [blame] | 530 | #elif defined(TARGET_XTENSA) |
| 531 | static inline int regpairs_aligned(void *cpu_env, int num) { return 1; } |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 532 | #else |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 533 | static inline int regpairs_aligned(void *cpu_env, int num) { return 0; } |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 534 | #endif |
| 535 | |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 536 | #define ERRNO_TABLE_SIZE 1200 |
| 537 | |
| 538 | /* target_to_host_errno_table[] is initialized from |
| 539 | * host_to_target_errno_table[] in syscall_init(). */ |
| 540 | static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = { |
| 541 | }; |
| 542 | |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 543 | /* |
ths | fe8f096 | 2007-07-12 10:59:21 +0000 | [diff] [blame] | 544 | * This list is the union of errno values overridden in asm-<arch>/errno.h |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 545 | * minus the errnos that are not actually generic to all archs. |
| 546 | */ |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 547 | static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = { |
Chen Gang | 08703b9 | 2015-09-07 10:35:06 +0800 | [diff] [blame] | 548 | [EAGAIN] = TARGET_EAGAIN, |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 549 | [EIDRM] = TARGET_EIDRM, |
| 550 | [ECHRNG] = TARGET_ECHRNG, |
| 551 | [EL2NSYNC] = TARGET_EL2NSYNC, |
| 552 | [EL3HLT] = TARGET_EL3HLT, |
| 553 | [EL3RST] = TARGET_EL3RST, |
| 554 | [ELNRNG] = TARGET_ELNRNG, |
| 555 | [EUNATCH] = TARGET_EUNATCH, |
| 556 | [ENOCSI] = TARGET_ENOCSI, |
| 557 | [EL2HLT] = TARGET_EL2HLT, |
| 558 | [EDEADLK] = TARGET_EDEADLK, |
| 559 | [ENOLCK] = TARGET_ENOLCK, |
| 560 | [EBADE] = TARGET_EBADE, |
| 561 | [EBADR] = TARGET_EBADR, |
| 562 | [EXFULL] = TARGET_EXFULL, |
| 563 | [ENOANO] = TARGET_ENOANO, |
| 564 | [EBADRQC] = TARGET_EBADRQC, |
| 565 | [EBADSLT] = TARGET_EBADSLT, |
| 566 | [EBFONT] = TARGET_EBFONT, |
| 567 | [ENOSTR] = TARGET_ENOSTR, |
| 568 | [ENODATA] = TARGET_ENODATA, |
| 569 | [ETIME] = TARGET_ETIME, |
| 570 | [ENOSR] = TARGET_ENOSR, |
| 571 | [ENONET] = TARGET_ENONET, |
| 572 | [ENOPKG] = TARGET_ENOPKG, |
| 573 | [EREMOTE] = TARGET_EREMOTE, |
| 574 | [ENOLINK] = TARGET_ENOLINK, |
| 575 | [EADV] = TARGET_EADV, |
| 576 | [ESRMNT] = TARGET_ESRMNT, |
| 577 | [ECOMM] = TARGET_ECOMM, |
| 578 | [EPROTO] = TARGET_EPROTO, |
| 579 | [EDOTDOT] = TARGET_EDOTDOT, |
| 580 | [EMULTIHOP] = TARGET_EMULTIHOP, |
| 581 | [EBADMSG] = TARGET_EBADMSG, |
| 582 | [ENAMETOOLONG] = TARGET_ENAMETOOLONG, |
| 583 | [EOVERFLOW] = TARGET_EOVERFLOW, |
| 584 | [ENOTUNIQ] = TARGET_ENOTUNIQ, |
| 585 | [EBADFD] = TARGET_EBADFD, |
| 586 | [EREMCHG] = TARGET_EREMCHG, |
| 587 | [ELIBACC] = TARGET_ELIBACC, |
| 588 | [ELIBBAD] = TARGET_ELIBBAD, |
| 589 | [ELIBSCN] = TARGET_ELIBSCN, |
| 590 | [ELIBMAX] = TARGET_ELIBMAX, |
| 591 | [ELIBEXEC] = TARGET_ELIBEXEC, |
| 592 | [EILSEQ] = TARGET_EILSEQ, |
| 593 | [ENOSYS] = TARGET_ENOSYS, |
| 594 | [ELOOP] = TARGET_ELOOP, |
| 595 | [ERESTART] = TARGET_ERESTART, |
| 596 | [ESTRPIPE] = TARGET_ESTRPIPE, |
| 597 | [ENOTEMPTY] = TARGET_ENOTEMPTY, |
| 598 | [EUSERS] = TARGET_EUSERS, |
| 599 | [ENOTSOCK] = TARGET_ENOTSOCK, |
| 600 | [EDESTADDRREQ] = TARGET_EDESTADDRREQ, |
| 601 | [EMSGSIZE] = TARGET_EMSGSIZE, |
| 602 | [EPROTOTYPE] = TARGET_EPROTOTYPE, |
| 603 | [ENOPROTOOPT] = TARGET_ENOPROTOOPT, |
| 604 | [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT, |
| 605 | [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT, |
| 606 | [EOPNOTSUPP] = TARGET_EOPNOTSUPP, |
| 607 | [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT, |
| 608 | [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT, |
| 609 | [EADDRINUSE] = TARGET_EADDRINUSE, |
| 610 | [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL, |
| 611 | [ENETDOWN] = TARGET_ENETDOWN, |
| 612 | [ENETUNREACH] = TARGET_ENETUNREACH, |
| 613 | [ENETRESET] = TARGET_ENETRESET, |
| 614 | [ECONNABORTED] = TARGET_ECONNABORTED, |
| 615 | [ECONNRESET] = TARGET_ECONNRESET, |
| 616 | [ENOBUFS] = TARGET_ENOBUFS, |
| 617 | [EISCONN] = TARGET_EISCONN, |
| 618 | [ENOTCONN] = TARGET_ENOTCONN, |
| 619 | [EUCLEAN] = TARGET_EUCLEAN, |
| 620 | [ENOTNAM] = TARGET_ENOTNAM, |
| 621 | [ENAVAIL] = TARGET_ENAVAIL, |
| 622 | [EISNAM] = TARGET_EISNAM, |
| 623 | [EREMOTEIO] = TARGET_EREMOTEIO, |
Aleksandar Markovic | 0444a3b | 2016-09-19 13:44:43 +0200 | [diff] [blame] | 624 | [EDQUOT] = TARGET_EDQUOT, |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 625 | [ESHUTDOWN] = TARGET_ESHUTDOWN, |
| 626 | [ETOOMANYREFS] = TARGET_ETOOMANYREFS, |
| 627 | [ETIMEDOUT] = TARGET_ETIMEDOUT, |
| 628 | [ECONNREFUSED] = TARGET_ECONNREFUSED, |
| 629 | [EHOSTDOWN] = TARGET_EHOSTDOWN, |
| 630 | [EHOSTUNREACH] = TARGET_EHOSTUNREACH, |
| 631 | [EALREADY] = TARGET_EALREADY, |
| 632 | [EINPROGRESS] = TARGET_EINPROGRESS, |
| 633 | [ESTALE] = TARGET_ESTALE, |
| 634 | [ECANCELED] = TARGET_ECANCELED, |
| 635 | [ENOMEDIUM] = TARGET_ENOMEDIUM, |
| 636 | [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 637 | #ifdef ENOKEY |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 638 | [ENOKEY] = TARGET_ENOKEY, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 639 | #endif |
| 640 | #ifdef EKEYEXPIRED |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 641 | [EKEYEXPIRED] = TARGET_EKEYEXPIRED, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 642 | #endif |
| 643 | #ifdef EKEYREVOKED |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 644 | [EKEYREVOKED] = TARGET_EKEYREVOKED, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 645 | #endif |
| 646 | #ifdef EKEYREJECTED |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 647 | [EKEYREJECTED] = TARGET_EKEYREJECTED, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 648 | #endif |
| 649 | #ifdef EOWNERDEAD |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 650 | [EOWNERDEAD] = TARGET_EOWNERDEAD, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 651 | #endif |
| 652 | #ifdef ENOTRECOVERABLE |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 653 | [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 654 | #endif |
Aleksandar Markovic | da39db6 | 2016-09-22 18:56:56 +0200 | [diff] [blame] | 655 | #ifdef ENOMSG |
| 656 | [ENOMSG] = TARGET_ENOMSG, |
| 657 | #endif |
Richard Henderson | fe8ed7d | 2016-12-05 13:08:12 -0800 | [diff] [blame] | 658 | #ifdef ERKFILL |
| 659 | [ERFKILL] = TARGET_ERFKILL, |
| 660 | #endif |
| 661 | #ifdef EHWPOISON |
| 662 | [EHWPOISON] = TARGET_EHWPOISON, |
| 663 | #endif |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 664 | }; |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 665 | |
| 666 | static inline int host_to_target_errno(int err) |
| 667 | { |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 668 | if (err >= 0 && err < ERRNO_TABLE_SIZE && |
| 669 | host_to_target_errno_table[err]) { |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 670 | return host_to_target_errno_table[err]; |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 671 | } |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 672 | return err; |
| 673 | } |
| 674 | |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 675 | static inline int target_to_host_errno(int err) |
| 676 | { |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 677 | if (err >= 0 && err < ERRNO_TABLE_SIZE && |
| 678 | target_to_host_errno_table[err]) { |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 679 | return target_to_host_errno_table[err]; |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 680 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 681 | return err; |
| 682 | } |
| 683 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 684 | static inline abi_long get_errno(abi_long ret) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 685 | { |
| 686 | if (ret == -1) |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 687 | return -host_to_target_errno(errno); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 688 | else |
| 689 | return ret; |
| 690 | } |
| 691 | |
Peter Maydell | 7dcdaea | 2016-06-06 19:58:18 +0100 | [diff] [blame] | 692 | const char *target_strerror(int err) |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 693 | { |
Peter Maydell | da2a34f | 2016-06-06 19:58:19 +0100 | [diff] [blame] | 694 | if (err == TARGET_ERESTARTSYS) { |
| 695 | return "To be restarted"; |
| 696 | } |
| 697 | if (err == TARGET_QEMU_ESIGRETURN) { |
| 698 | return "Successful exit from sigreturn"; |
| 699 | } |
| 700 | |
Alexander Graf | 962b289 | 2011-11-21 12:04:07 +0100 | [diff] [blame] | 701 | if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) { |
| 702 | return NULL; |
| 703 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 704 | return strerror(target_to_host_errno(err)); |
| 705 | } |
| 706 | |
Timothy E Baldwin | 4d330ce | 2016-05-12 18:47:46 +0100 | [diff] [blame] | 707 | #define safe_syscall0(type, name) \ |
| 708 | static type safe_##name(void) \ |
| 709 | { \ |
| 710 | return safe_syscall(__NR_##name); \ |
| 711 | } |
| 712 | |
| 713 | #define safe_syscall1(type, name, type1, arg1) \ |
| 714 | static type safe_##name(type1 arg1) \ |
| 715 | { \ |
| 716 | return safe_syscall(__NR_##name, arg1); \ |
| 717 | } |
| 718 | |
| 719 | #define safe_syscall2(type, name, type1, arg1, type2, arg2) \ |
| 720 | static type safe_##name(type1 arg1, type2 arg2) \ |
| 721 | { \ |
| 722 | return safe_syscall(__NR_##name, arg1, arg2); \ |
| 723 | } |
| 724 | |
| 725 | #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ |
| 726 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \ |
| 727 | { \ |
| 728 | return safe_syscall(__NR_##name, arg1, arg2, arg3); \ |
| 729 | } |
| 730 | |
| 731 | #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 732 | type4, arg4) \ |
| 733 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ |
| 734 | { \ |
| 735 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \ |
| 736 | } |
| 737 | |
| 738 | #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 739 | type4, arg4, type5, arg5) \ |
| 740 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ |
| 741 | type5 arg5) \ |
| 742 | { \ |
| 743 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ |
| 744 | } |
| 745 | |
| 746 | #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 747 | type4, arg4, type5, arg5, type6, arg6) \ |
| 748 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ |
| 749 | type5 arg5, type6 arg6) \ |
| 750 | { \ |
| 751 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ |
| 752 | } |
| 753 | |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 754 | safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count) |
| 755 | 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] | 756 | safe_syscall4(int, openat, int, dirfd, const char *, pathname, \ |
| 757 | int, flags, mode_t, mode) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 758 | #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid) |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 759 | safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \ |
| 760 | struct rusage *, rusage) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 761 | #endif |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 762 | safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ |
| 763 | int, options, struct rusage *, rusage) |
Timothy E Baldwin | ffdcbe2 | 2016-05-12 18:47:50 +0100 | [diff] [blame] | 764 | safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 765 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ |
| 766 | defined(TARGET_NR_pselect6) |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 767 | safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ |
| 768 | fd_set *, exceptfds, struct timespec *, timeout, void *, sig) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 769 | #endif |
| 770 | #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll) |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 771 | safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, |
| 772 | struct timespec *, tsp, const sigset_t *, sigmask, |
| 773 | size_t, sigsetsize) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 774 | #endif |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 775 | safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events, |
| 776 | int, maxevents, int, timeout, const sigset_t *, sigmask, |
| 777 | size_t, sigsetsize) |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 778 | #if defined(__NR_futex) |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 779 | safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \ |
| 780 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 781 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 782 | #if defined(__NR_futex_time64) |
| 783 | safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \ |
| 784 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 785 | #endif |
Peter Maydell | 2fe4fba | 2016-05-27 15:51:48 +0100 | [diff] [blame] | 786 | safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize) |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 787 | safe_syscall2(int, kill, pid_t, pid, int, sig) |
| 788 | safe_syscall2(int, tkill, int, tid, int, sig) |
| 789 | safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig) |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 790 | safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt) |
| 791 | 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] | 792 | safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, |
| 793 | unsigned long, pos_l, unsigned long, pos_h) |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 794 | safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, |
| 795 | unsigned long, pos_l, unsigned long, pos_h) |
Peter Maydell | 2a3c761 | 2016-06-06 19:58:03 +0100 | [diff] [blame] | 796 | safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr, |
| 797 | socklen_t, addrlen) |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 798 | safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len, |
| 799 | int, flags, const struct sockaddr *, addr, socklen_t, addrlen) |
| 800 | safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len, |
| 801 | int, flags, struct sockaddr *, addr, socklen_t *, addrlen) |
| 802 | safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags) |
| 803 | safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags) |
Peter Maydell | 2a84598 | 2016-06-06 19:58:07 +0100 | [diff] [blame] | 804 | safe_syscall2(int, flock, int, fd, int, operation) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 805 | #ifdef TARGET_NR_rt_sigtimedwait |
Peter Maydell | b3f8233 | 2016-06-06 19:58:08 +0100 | [diff] [blame] | 806 | safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo, |
| 807 | const struct timespec *, uts, size_t, sigsetsize) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 808 | #endif |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 809 | safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len, |
| 810 | int, flags) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 811 | #if defined(TARGET_NR_nanosleep) |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 812 | safe_syscall2(int, nanosleep, const struct timespec *, req, |
| 813 | struct timespec *, rem) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 814 | #endif |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 815 | #ifdef TARGET_NR_clock_nanosleep |
| 816 | safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags, |
| 817 | const struct timespec *, req, struct timespec *, rem) |
| 818 | #endif |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 819 | #ifdef __NR_ipc |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 820 | #ifdef __s390x__ |
| 821 | safe_syscall5(int, ipc, int, call, long, first, long, second, long, third, |
| 822 | void *, ptr) |
| 823 | #else |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 824 | safe_syscall6(int, ipc, int, call, long, first, long, second, long, third, |
| 825 | void *, ptr, long, fifth) |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 826 | #endif |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 827 | #endif |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 828 | #ifdef __NR_msgsnd |
| 829 | safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz, |
| 830 | int, flags) |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 831 | #endif |
| 832 | #ifdef __NR_msgrcv |
| 833 | safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz, |
| 834 | long, msgtype, int, flags) |
Laurent Vivier | 86e6369 | 2019-05-23 19:54:13 +0200 | [diff] [blame] | 835 | #endif |
| 836 | #ifdef __NR_semtimedop |
| 837 | safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops, |
| 838 | unsigned, nsops, const struct timespec *, timeout) |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 839 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 840 | #ifdef TARGET_NR_mq_timedsend |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 841 | safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr, |
| 842 | size_t, len, unsigned, prio, const struct timespec *, timeout) |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 843 | #endif |
| 844 | #ifdef TARGET_NR_mq_timedreceive |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 845 | safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr, |
| 846 | size_t, len, unsigned *, prio, const struct timespec *, timeout) |
| 847 | #endif |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 848 | /* We do ioctl like this rather than via safe_syscall3 to preserve the |
| 849 | * "third argument might be integer or pointer or not present" behaviour of |
| 850 | * the libc function. |
| 851 | */ |
| 852 | #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__) |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 853 | /* Similarly for fcntl. Note that callers must always: |
| 854 | * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK |
| 855 | * use the flock64 struct rather than unsuffixed flock |
| 856 | * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts. |
| 857 | */ |
| 858 | #ifdef __NR_fcntl64 |
| 859 | #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__) |
| 860 | #else |
| 861 | #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__) |
| 862 | #endif |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 863 | |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 864 | static inline int host_to_target_sock_type(int host_type) |
| 865 | { |
| 866 | int target_type; |
| 867 | |
| 868 | switch (host_type & 0xf /* SOCK_TYPE_MASK */) { |
| 869 | case SOCK_DGRAM: |
| 870 | target_type = TARGET_SOCK_DGRAM; |
| 871 | break; |
| 872 | case SOCK_STREAM: |
| 873 | target_type = TARGET_SOCK_STREAM; |
| 874 | break; |
| 875 | default: |
| 876 | target_type = host_type & 0xf /* SOCK_TYPE_MASK */; |
| 877 | break; |
| 878 | } |
| 879 | |
| 880 | #if defined(SOCK_CLOEXEC) |
| 881 | if (host_type & SOCK_CLOEXEC) { |
| 882 | target_type |= TARGET_SOCK_CLOEXEC; |
| 883 | } |
| 884 | #endif |
| 885 | |
| 886 | #if defined(SOCK_NONBLOCK) |
| 887 | if (host_type & SOCK_NONBLOCK) { |
| 888 | target_type |= TARGET_SOCK_NONBLOCK; |
| 889 | } |
| 890 | #endif |
| 891 | |
| 892 | return target_type; |
| 893 | } |
| 894 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 895 | static abi_ulong target_brk; |
| 896 | static abi_ulong target_original_brk; |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 897 | static abi_ulong brk_page; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 898 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 899 | void target_set_brk(abi_ulong new_brk) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 900 | { |
blueswir1 | 4c1de73 | 2007-07-07 20:45:44 +0000 | [diff] [blame] | 901 | target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 902 | brk_page = HOST_PAGE_ALIGN(target_brk); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 903 | } |
| 904 | |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 905 | //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0) |
| 906 | #define DEBUGF_BRK(message, args...) |
| 907 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 908 | /* do_brk() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 909 | abi_long do_brk(abi_ulong new_brk) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 910 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 911 | abi_long mapped_addr; |
Peter Maydell | ef4330c | 2016-07-22 13:41:52 +0100 | [diff] [blame] | 912 | abi_ulong new_alloc_size; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 913 | |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 914 | DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 915 | |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 916 | if (!new_brk) { |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 917 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 918 | return target_brk; |
| 919 | } |
| 920 | if (new_brk < target_original_brk) { |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 921 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n", |
| 922 | target_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 923 | return target_brk; |
| 924 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 925 | |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 926 | /* If the new brk is less than the highest page reserved to the |
| 927 | * target heap allocation, set it and we're almost done... */ |
| 928 | if (new_brk <= brk_page) { |
| 929 | /* Heap contents are initialized to zero, as for anonymous |
| 930 | * mapped pages. */ |
| 931 | if (new_brk > target_brk) { |
| 932 | memset(g2h(target_brk), 0, new_brk - target_brk); |
| 933 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 934 | target_brk = new_brk; |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 935 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk); |
Paolo Bonzini | 72e21db | 2018-12-13 23:37:36 +0100 | [diff] [blame] | 936 | return target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 939 | /* We need to allocate more memory after the brk... Note that |
| 940 | * we don't use MAP_FIXED because that will map over the top of |
| 941 | * any existing mapping (like the one with the host libc or qemu |
| 942 | * itself); instead we treat "mapped but at wrong address" as |
| 943 | * a failure and unmap again. |
| 944 | */ |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 945 | new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 946 | mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 947 | PROT_READ|PROT_WRITE, |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 948 | MAP_ANON|MAP_PRIVATE, 0, 0)); |
| 949 | |
| 950 | if (mapped_addr == brk_page) { |
Cédric VINCENT | 70afc34 | 2011-08-26 10:56:50 +0200 | [diff] [blame] | 951 | /* Heap contents are initialized to zero, as for anonymous |
| 952 | * mapped pages. Technically the new pages are already |
| 953 | * initialized to zero since they *are* anonymous mapped |
| 954 | * pages, however we have to take care with the contents that |
| 955 | * come from the remaining part of the previous page: it may |
| 956 | * contains garbage data due to a previous heap usage (grown |
| 957 | * then shrunken). */ |
| 958 | memset(g2h(target_brk), 0, brk_page - target_brk); |
| 959 | |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 960 | target_brk = new_brk; |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 961 | brk_page = HOST_PAGE_ALIGN(target_brk); |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 962 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n", |
| 963 | target_brk); |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 964 | return target_brk; |
| 965 | } else if (mapped_addr != -1) { |
| 966 | /* Mapped but at wrong address, meaning there wasn't actually |
| 967 | * enough space for this brk. |
| 968 | */ |
| 969 | target_munmap(mapped_addr, new_alloc_size); |
| 970 | mapped_addr = -1; |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 971 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 972 | } |
| 973 | else { |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 974 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk); |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 975 | } |
balrog | 7ab240a | 2008-04-26 12:17:34 +0000 | [diff] [blame] | 976 | |
Richard Henderson | 7dd46c0 | 2010-05-03 10:07:49 -0700 | [diff] [blame] | 977 | #if defined(TARGET_ALPHA) |
| 978 | /* We (partially) emulate OSF/1 on Alpha, which requires we |
| 979 | return a proper errno, not an unchanged brk value. */ |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 980 | return -TARGET_ENOMEM; |
Richard Henderson | 7dd46c0 | 2010-05-03 10:07:49 -0700 | [diff] [blame] | 981 | #endif |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 982 | /* For everything else, return the previous break. */ |
balrog | 7ab240a | 2008-04-26 12:17:34 +0000 | [diff] [blame] | 983 | return target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 986 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ |
| 987 | defined(TARGET_NR_pselect6) |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 988 | static inline abi_long copy_from_user_fdset(fd_set *fds, |
| 989 | abi_ulong target_fds_addr, |
| 990 | int n) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 991 | { |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 992 | int i, nw, j, k; |
| 993 | abi_ulong b, *target_fds; |
| 994 | |
Laurent Vivier | b1b2db2 | 2016-05-31 18:36:02 +0200 | [diff] [blame] | 995 | nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 996 | if (!(target_fds = lock_user(VERIFY_READ, |
| 997 | target_fds_addr, |
| 998 | sizeof(abi_ulong) * nw, |
| 999 | 1))) |
| 1000 | return -TARGET_EFAULT; |
| 1001 | |
| 1002 | FD_ZERO(fds); |
| 1003 | k = 0; |
| 1004 | for (i = 0; i < nw; i++) { |
| 1005 | /* grab the abi_ulong */ |
| 1006 | __get_user(b, &target_fds[i]); |
| 1007 | for (j = 0; j < TARGET_ABI_BITS; j++) { |
| 1008 | /* check the bit inside the abi_ulong */ |
| 1009 | if ((b >> j) & 1) |
| 1010 | FD_SET(k, fds); |
| 1011 | k++; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1012 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1013 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1014 | |
| 1015 | unlock_user(target_fds, target_fds_addr, 0); |
| 1016 | |
| 1017 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1020 | static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr, |
| 1021 | abi_ulong target_fds_addr, |
| 1022 | int n) |
| 1023 | { |
| 1024 | if (target_fds_addr) { |
| 1025 | if (copy_from_user_fdset(fds, target_fds_addr, n)) |
| 1026 | return -TARGET_EFAULT; |
| 1027 | *fds_ptr = fds; |
| 1028 | } else { |
| 1029 | *fds_ptr = NULL; |
| 1030 | } |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1034 | static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr, |
| 1035 | const fd_set *fds, |
| 1036 | int n) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1037 | { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1038 | int i, nw, j, k; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1039 | abi_long v; |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1040 | abi_ulong *target_fds; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1041 | |
Laurent Vivier | b1b2db2 | 2016-05-31 18:36:02 +0200 | [diff] [blame] | 1042 | nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1043 | if (!(target_fds = lock_user(VERIFY_WRITE, |
| 1044 | target_fds_addr, |
| 1045 | sizeof(abi_ulong) * nw, |
| 1046 | 0))) |
| 1047 | return -TARGET_EFAULT; |
| 1048 | |
| 1049 | k = 0; |
| 1050 | for (i = 0; i < nw; i++) { |
| 1051 | v = 0; |
| 1052 | for (j = 0; j < TARGET_ABI_BITS; j++) { |
Andreas Schwab | 9ab709b | 2013-04-09 01:02:34 +0000 | [diff] [blame] | 1053 | v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1054 | k++; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1055 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1056 | __put_user(v, &target_fds[i]); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1057 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1058 | |
| 1059 | unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw); |
| 1060 | |
| 1061 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1062 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1063 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1064 | |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 1065 | #if defined(__alpha__) |
| 1066 | #define HOST_HZ 1024 |
| 1067 | #else |
| 1068 | #define HOST_HZ 100 |
| 1069 | #endif |
| 1070 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1071 | static inline abi_long host_to_target_clock_t(long ticks) |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 1072 | { |
| 1073 | #if HOST_HZ == TARGET_HZ |
| 1074 | return ticks; |
| 1075 | #else |
| 1076 | return ((int64_t)ticks * TARGET_HZ) / HOST_HZ; |
| 1077 | #endif |
| 1078 | } |
| 1079 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1080 | static inline abi_long host_to_target_rusage(abi_ulong target_addr, |
| 1081 | const struct rusage *rusage) |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 1082 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1083 | struct target_rusage *target_rusage; |
| 1084 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1085 | if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) |
| 1086 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1087 | target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec); |
| 1088 | target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec); |
| 1089 | target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec); |
| 1090 | target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec); |
| 1091 | target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss); |
| 1092 | target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss); |
| 1093 | target_rusage->ru_idrss = tswapal(rusage->ru_idrss); |
| 1094 | target_rusage->ru_isrss = tswapal(rusage->ru_isrss); |
| 1095 | target_rusage->ru_minflt = tswapal(rusage->ru_minflt); |
| 1096 | target_rusage->ru_majflt = tswapal(rusage->ru_majflt); |
| 1097 | target_rusage->ru_nswap = tswapal(rusage->ru_nswap); |
| 1098 | target_rusage->ru_inblock = tswapal(rusage->ru_inblock); |
| 1099 | target_rusage->ru_oublock = tswapal(rusage->ru_oublock); |
| 1100 | target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd); |
| 1101 | target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv); |
| 1102 | target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals); |
| 1103 | target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw); |
| 1104 | target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1105 | unlock_user_struct(target_rusage, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1106 | |
| 1107 | return 0; |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1110 | #ifdef TARGET_NR_setrlimit |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1111 | 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] | 1112 | { |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1113 | abi_ulong target_rlim_swap; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1114 | rlim_t result; |
| 1115 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1116 | target_rlim_swap = tswapal(target_rlim); |
| 1117 | if (target_rlim_swap == TARGET_RLIM_INFINITY) |
| 1118 | return RLIM_INFINITY; |
| 1119 | |
| 1120 | result = target_rlim_swap; |
| 1121 | if (target_rlim_swap != (rlim_t)result) |
| 1122 | return RLIM_INFINITY; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1123 | |
| 1124 | return result; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1125 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1126 | #endif |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1127 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1128 | #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit) |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1129 | 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] | 1130 | { |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1131 | abi_ulong target_rlim_swap; |
| 1132 | abi_ulong result; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1133 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1134 | if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim) |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1135 | target_rlim_swap = TARGET_RLIM_INFINITY; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1136 | else |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1137 | target_rlim_swap = rlim; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1138 | result = tswapal(target_rlim_swap); |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1139 | |
| 1140 | return result; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1141 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1142 | #endif |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1143 | |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 1144 | static inline int target_to_host_resource(int code) |
| 1145 | { |
| 1146 | switch (code) { |
| 1147 | case TARGET_RLIMIT_AS: |
| 1148 | return RLIMIT_AS; |
| 1149 | case TARGET_RLIMIT_CORE: |
| 1150 | return RLIMIT_CORE; |
| 1151 | case TARGET_RLIMIT_CPU: |
| 1152 | return RLIMIT_CPU; |
| 1153 | case TARGET_RLIMIT_DATA: |
| 1154 | return RLIMIT_DATA; |
| 1155 | case TARGET_RLIMIT_FSIZE: |
| 1156 | return RLIMIT_FSIZE; |
| 1157 | case TARGET_RLIMIT_LOCKS: |
| 1158 | return RLIMIT_LOCKS; |
| 1159 | case TARGET_RLIMIT_MEMLOCK: |
| 1160 | return RLIMIT_MEMLOCK; |
| 1161 | case TARGET_RLIMIT_MSGQUEUE: |
| 1162 | return RLIMIT_MSGQUEUE; |
| 1163 | case TARGET_RLIMIT_NICE: |
| 1164 | return RLIMIT_NICE; |
| 1165 | case TARGET_RLIMIT_NOFILE: |
| 1166 | return RLIMIT_NOFILE; |
| 1167 | case TARGET_RLIMIT_NPROC: |
| 1168 | return RLIMIT_NPROC; |
| 1169 | case TARGET_RLIMIT_RSS: |
| 1170 | return RLIMIT_RSS; |
| 1171 | case TARGET_RLIMIT_RTPRIO: |
| 1172 | return RLIMIT_RTPRIO; |
| 1173 | case TARGET_RLIMIT_SIGPENDING: |
| 1174 | return RLIMIT_SIGPENDING; |
| 1175 | case TARGET_RLIMIT_STACK: |
| 1176 | return RLIMIT_STACK; |
| 1177 | default: |
| 1178 | return code; |
| 1179 | } |
| 1180 | } |
| 1181 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1182 | static inline abi_long copy_from_user_timeval(struct timeval *tv, |
| 1183 | abi_ulong target_tv_addr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1184 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1185 | struct target_timeval *target_tv; |
| 1186 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1187 | if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1188 | return -TARGET_EFAULT; |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1189 | } |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1190 | |
| 1191 | __get_user(tv->tv_sec, &target_tv->tv_sec); |
| 1192 | __get_user(tv->tv_usec, &target_tv->tv_usec); |
| 1193 | |
| 1194 | unlock_user_struct(target_tv, target_tv_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1195 | |
| 1196 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1199 | static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr, |
| 1200 | const struct timeval *tv) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1201 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1202 | struct target_timeval *target_tv; |
| 1203 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1204 | if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1205 | return -TARGET_EFAULT; |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1206 | } |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1207 | |
| 1208 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1209 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
| 1210 | |
| 1211 | unlock_user_struct(target_tv, target_tv_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1212 | |
| 1213 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1216 | static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr, |
| 1217 | const struct timeval *tv) |
| 1218 | { |
| 1219 | struct target__kernel_sock_timeval *target_tv; |
| 1220 | |
| 1221 | if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { |
| 1222 | return -TARGET_EFAULT; |
| 1223 | } |
| 1224 | |
| 1225 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1226 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
| 1227 | |
| 1228 | unlock_user_struct(target_tv, target_tv_addr, 1); |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1233 | #if defined(TARGET_NR_futex) || \ |
| 1234 | defined(TARGET_NR_rt_sigtimedwait) || \ |
| 1235 | defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \ |
| 1236 | defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \ |
| 1237 | defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \ |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 1238 | defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \ |
| 1239 | defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1240 | static inline abi_long target_to_host_timespec(struct timespec *host_ts, |
| 1241 | abi_ulong target_addr) |
| 1242 | { |
| 1243 | struct target_timespec *target_ts; |
| 1244 | |
| 1245 | if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { |
| 1246 | return -TARGET_EFAULT; |
| 1247 | } |
| 1248 | __get_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1249 | __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1250 | unlock_user_struct(target_ts, target_addr, 0); |
| 1251 | return 0; |
| 1252 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1253 | #endif |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1254 | |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 1255 | #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 1256 | static inline abi_long target_to_host_timespec64(struct timespec *host_ts, |
| 1257 | abi_ulong target_addr) |
| 1258 | { |
| 1259 | struct target__kernel_timespec *target_ts; |
| 1260 | |
| 1261 | if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { |
| 1262 | return -TARGET_EFAULT; |
| 1263 | } |
| 1264 | __get_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1265 | __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1266 | unlock_user_struct(target_ts, target_addr, 0); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | #endif |
| 1270 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 1271 | static inline abi_long host_to_target_timespec(abi_ulong target_addr, |
| 1272 | struct timespec *host_ts) |
| 1273 | { |
| 1274 | struct target_timespec *target_ts; |
| 1275 | |
| 1276 | if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { |
| 1277 | return -TARGET_EFAULT; |
| 1278 | } |
| 1279 | __put_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1280 | __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1281 | unlock_user_struct(target_ts, target_addr, 1); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static inline abi_long host_to_target_timespec64(abi_ulong target_addr, |
| 1286 | struct timespec *host_ts) |
| 1287 | { |
| 1288 | struct target__kernel_timespec *target_ts; |
| 1289 | |
| 1290 | if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { |
| 1291 | return -TARGET_EFAULT; |
| 1292 | } |
| 1293 | __put_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 1294 | __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
| 1295 | unlock_user_struct(target_ts, target_addr, 1); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 1299 | #if defined(TARGET_NR_gettimeofday) |
| 1300 | static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr, |
| 1301 | struct timezone *tz) |
| 1302 | { |
| 1303 | struct target_timezone *target_tz; |
| 1304 | |
| 1305 | if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) { |
| 1306 | return -TARGET_EFAULT; |
| 1307 | } |
| 1308 | |
| 1309 | __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); |
| 1310 | __put_user(tz->tz_dsttime, &target_tz->tz_dsttime); |
| 1311 | |
| 1312 | unlock_user_struct(target_tz, target_tz_addr, 1); |
| 1313 | |
| 1314 | return 0; |
| 1315 | } |
| 1316 | #endif |
| 1317 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1318 | #if defined(TARGET_NR_settimeofday) |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 1319 | static inline abi_long copy_from_user_timezone(struct timezone *tz, |
| 1320 | abi_ulong target_tz_addr) |
| 1321 | { |
| 1322 | struct target_timezone *target_tz; |
| 1323 | |
| 1324 | if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) { |
| 1325 | return -TARGET_EFAULT; |
| 1326 | } |
| 1327 | |
| 1328 | __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); |
| 1329 | __get_user(tz->tz_dsttime, &target_tz->tz_dsttime); |
| 1330 | |
| 1331 | unlock_user_struct(target_tz, target_tz_addr, 0); |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 1335 | #endif |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 1336 | |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 1337 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
| 1338 | #include <mqueue.h> |
| 1339 | |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 1340 | static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr, |
| 1341 | abi_ulong target_mq_attr_addr) |
| 1342 | { |
| 1343 | struct target_mq_attr *target_mq_attr; |
| 1344 | |
| 1345 | if (!lock_user_struct(VERIFY_READ, target_mq_attr, |
| 1346 | target_mq_attr_addr, 1)) |
| 1347 | return -TARGET_EFAULT; |
| 1348 | |
| 1349 | __get_user(attr->mq_flags, &target_mq_attr->mq_flags); |
| 1350 | __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); |
| 1351 | __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); |
| 1352 | __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); |
| 1353 | |
| 1354 | unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0); |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr, |
| 1360 | const struct mq_attr *attr) |
| 1361 | { |
| 1362 | struct target_mq_attr *target_mq_attr; |
| 1363 | |
| 1364 | if (!lock_user_struct(VERIFY_WRITE, target_mq_attr, |
| 1365 | target_mq_attr_addr, 0)) |
| 1366 | return -TARGET_EFAULT; |
| 1367 | |
| 1368 | __put_user(attr->mq_flags, &target_mq_attr->mq_flags); |
| 1369 | __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); |
| 1370 | __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); |
| 1371 | __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); |
| 1372 | |
| 1373 | unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1); |
| 1374 | |
| 1375 | return 0; |
| 1376 | } |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 1377 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1378 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1379 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 1380 | /* do_select() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1381 | static abi_long do_select(int n, |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1382 | abi_ulong rfd_addr, abi_ulong wfd_addr, |
| 1383 | abi_ulong efd_addr, abi_ulong target_tv_addr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1384 | { |
| 1385 | fd_set rfds, wfds, efds; |
| 1386 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1387 | struct timeval tv; |
| 1388 | struct timespec ts, *ts_ptr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1389 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1390 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1391 | ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); |
| 1392 | if (ret) { |
| 1393 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1394 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1395 | ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); |
| 1396 | if (ret) { |
| 1397 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1398 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1399 | ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); |
| 1400 | if (ret) { |
| 1401 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1402 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1403 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1404 | if (target_tv_addr) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1405 | if (copy_from_user_timeval(&tv, target_tv_addr)) |
| 1406 | return -TARGET_EFAULT; |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1407 | ts.tv_sec = tv.tv_sec; |
| 1408 | ts.tv_nsec = tv.tv_usec * 1000; |
| 1409 | ts_ptr = &ts; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1410 | } else { |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1411 | ts_ptr = NULL; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1412 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1413 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1414 | ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, |
| 1415 | ts_ptr, NULL)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1416 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1417 | if (!is_error(ret)) { |
| 1418 | if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) |
| 1419 | return -TARGET_EFAULT; |
| 1420 | if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) |
| 1421 | return -TARGET_EFAULT; |
| 1422 | if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) |
| 1423 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1424 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1425 | if (target_tv_addr) { |
| 1426 | tv.tv_sec = ts.tv_sec; |
| 1427 | tv.tv_usec = ts.tv_nsec / 1000; |
| 1428 | if (copy_to_user_timeval(target_tv_addr, &tv)) { |
| 1429 | return -TARGET_EFAULT; |
| 1430 | } |
| 1431 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1432 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1433 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1434 | return ret; |
| 1435 | } |
Laurent Vivier | 5457dc9 | 2016-07-08 01:17:27 +0200 | [diff] [blame] | 1436 | |
| 1437 | #if defined(TARGET_WANT_OLD_SYS_SELECT) |
| 1438 | static abi_long do_old_select(abi_ulong arg1) |
| 1439 | { |
| 1440 | struct target_sel_arg_struct *sel; |
| 1441 | abi_ulong inp, outp, exp, tvp; |
| 1442 | long nsel; |
| 1443 | |
| 1444 | if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) { |
| 1445 | return -TARGET_EFAULT; |
| 1446 | } |
| 1447 | |
| 1448 | nsel = tswapal(sel->n); |
| 1449 | inp = tswapal(sel->inp); |
| 1450 | outp = tswapal(sel->outp); |
| 1451 | exp = tswapal(sel->exp); |
| 1452 | tvp = tswapal(sel->tvp); |
| 1453 | |
| 1454 | unlock_user_struct(sel, arg1, 0); |
| 1455 | |
| 1456 | return do_select(nsel, inp, outp, exp, tvp); |
| 1457 | } |
| 1458 | #endif |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1459 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1460 | |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1461 | static abi_long do_pipe2(int host_pipe[], int flags) |
| 1462 | { |
| 1463 | #ifdef CONFIG_PIPE2 |
| 1464 | return pipe2(host_pipe, flags); |
| 1465 | #else |
| 1466 | return -ENOSYS; |
| 1467 | #endif |
| 1468 | } |
| 1469 | |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1470 | static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, |
| 1471 | int flags, int is_pipe2) |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1472 | { |
| 1473 | int host_pipe[2]; |
| 1474 | abi_long ret; |
| 1475 | ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe); |
| 1476 | |
| 1477 | if (is_error(ret)) |
| 1478 | return get_errno(ret); |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1479 | |
| 1480 | /* Several targets have special calling conventions for the original |
| 1481 | pipe syscall, but didn't replicate this into the pipe2 syscall. */ |
| 1482 | if (!is_pipe2) { |
| 1483 | #if defined(TARGET_ALPHA) |
| 1484 | ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1]; |
| 1485 | return host_pipe[0]; |
| 1486 | #elif defined(TARGET_MIPS) |
| 1487 | ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1]; |
| 1488 | return host_pipe[0]; |
| 1489 | #elif defined(TARGET_SH4) |
takasi-y@ops.dti.ne.jp | 597c021 | 2010-02-18 00:35:03 +0900 | [diff] [blame] | 1490 | ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1]; |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1491 | return host_pipe[0]; |
Peter Maydell | 82f05b6 | 2013-07-06 17:39:48 +0100 | [diff] [blame] | 1492 | #elif defined(TARGET_SPARC) |
| 1493 | ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1]; |
| 1494 | return host_pipe[0]; |
takasi-y@ops.dti.ne.jp | 597c021 | 2010-02-18 00:35:03 +0900 | [diff] [blame] | 1495 | #endif |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1498 | if (put_user_s32(host_pipe[0], pipedes) |
| 1499 | || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0]))) |
| 1500 | return -TARGET_EFAULT; |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1501 | return get_errno(ret); |
| 1502 | } |
| 1503 | |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 1504 | static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, |
| 1505 | abi_ulong target_addr, |
| 1506 | socklen_t len) |
| 1507 | { |
| 1508 | struct target_ip_mreqn *target_smreqn; |
| 1509 | |
| 1510 | target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1); |
| 1511 | if (!target_smreqn) |
| 1512 | return -TARGET_EFAULT; |
| 1513 | mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr; |
| 1514 | mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr; |
| 1515 | if (len == sizeof(struct target_ip_mreqn)) |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1516 | mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex); |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 1517 | unlock_user(target_smreqn, target_addr, 0); |
| 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 1522 | static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1523 | abi_ulong target_addr, |
| 1524 | socklen_t len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1525 | { |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1526 | const socklen_t unix_maxlen = sizeof (struct sockaddr_un); |
| 1527 | sa_family_t sa_family; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1528 | struct target_sockaddr *target_saddr; |
| 1529 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 1530 | if (fd_trans_target_to_host_addr(fd)) { |
| 1531 | return fd_trans_target_to_host_addr(fd)(addr, target_addr, len); |
| 1532 | } |
| 1533 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1534 | target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); |
| 1535 | if (!target_saddr) |
| 1536 | return -TARGET_EFAULT; |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1537 | |
| 1538 | sa_family = tswap16(target_saddr->sa_family); |
| 1539 | |
| 1540 | /* Oops. The caller might send a incomplete sun_path; sun_path |
| 1541 | * must be terminated by \0 (see the manual page), but |
| 1542 | * unfortunately it is quite common to specify sockaddr_un |
| 1543 | * length as "strlen(x->sun_path)" while it should be |
| 1544 | * "strlen(...) + 1". We'll fix that here if needed. |
| 1545 | * Linux kernel has a similar feature. |
| 1546 | */ |
| 1547 | |
| 1548 | if (sa_family == AF_UNIX) { |
| 1549 | if (len < unix_maxlen && len > 0) { |
| 1550 | char *cp = (char*)target_saddr; |
| 1551 | |
| 1552 | if ( cp[len-1] && !cp[len] ) |
| 1553 | len++; |
| 1554 | } |
| 1555 | if (len > unix_maxlen) |
| 1556 | len = unix_maxlen; |
| 1557 | } |
| 1558 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1559 | memcpy(addr, target_saddr, len); |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1560 | addr->sa_family = sa_family; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1561 | if (sa_family == AF_NETLINK) { |
| 1562 | struct sockaddr_nl *nladdr; |
| 1563 | |
| 1564 | nladdr = (struct sockaddr_nl *)addr; |
| 1565 | nladdr->nl_pid = tswap32(nladdr->nl_pid); |
| 1566 | nladdr->nl_groups = tswap32(nladdr->nl_groups); |
| 1567 | } else if (sa_family == AF_PACKET) { |
Joakim Tjernlund | 33a29b5 | 2014-07-12 15:47:07 +0200 | [diff] [blame] | 1568 | struct target_sockaddr_ll *lladdr; |
| 1569 | |
| 1570 | lladdr = (struct target_sockaddr_ll *)addr; |
| 1571 | lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex); |
| 1572 | lladdr->sll_hatype = tswap16(lladdr->sll_hatype); |
| 1573 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1574 | unlock_user(target_saddr, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1575 | |
| 1576 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1579 | static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, |
| 1580 | struct sockaddr *addr, |
| 1581 | socklen_t len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1582 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1583 | struct target_sockaddr *target_saddr; |
| 1584 | |
Peter Maydell | a1e2219 | 2016-07-07 15:44:43 +0100 | [diff] [blame] | 1585 | if (len == 0) { |
| 1586 | return 0; |
| 1587 | } |
Philippe Mathieu-Daudé | 6860710 | 2017-07-26 23:42:18 -0300 | [diff] [blame] | 1588 | assert(addr); |
Peter Maydell | a1e2219 | 2016-07-07 15:44:43 +0100 | [diff] [blame] | 1589 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1590 | target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0); |
| 1591 | if (!target_saddr) |
| 1592 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1593 | memcpy(target_saddr, addr, len); |
Peter Maydell | a1e2219 | 2016-07-07 15:44:43 +0100 | [diff] [blame] | 1594 | if (len >= offsetof(struct target_sockaddr, sa_family) + |
| 1595 | sizeof(target_saddr->sa_family)) { |
| 1596 | target_saddr->sa_family = tswap16(addr->sa_family); |
| 1597 | } |
Philippe Mathieu-Daudé | a47401b | 2019-10-21 13:48:52 +0200 | [diff] [blame] | 1598 | if (addr->sa_family == AF_NETLINK && |
| 1599 | len >= sizeof(struct target_sockaddr_nl)) { |
| 1600 | struct target_sockaddr_nl *target_nl = |
| 1601 | (struct target_sockaddr_nl *)target_saddr; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1602 | target_nl->nl_pid = tswap32(target_nl->nl_pid); |
| 1603 | target_nl->nl_groups = tswap32(target_nl->nl_groups); |
Laurent Vivier | a82ea93 | 2016-06-27 00:18:22 +0200 | [diff] [blame] | 1604 | } else if (addr->sa_family == AF_PACKET) { |
| 1605 | struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr; |
| 1606 | target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex); |
| 1607 | target_ll->sll_hatype = tswap16(target_ll->sll_hatype); |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1608 | } else if (addr->sa_family == AF_INET6 && |
| 1609 | len >= sizeof(struct target_sockaddr_in6)) { |
| 1610 | struct target_sockaddr_in6 *target_in6 = |
| 1611 | (struct target_sockaddr_in6 *)target_saddr; |
| 1612 | target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1613 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1614 | unlock_user(target_saddr, target_addr, len); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1615 | |
| 1616 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1619 | static inline abi_long target_to_host_cmsg(struct msghdr *msgh, |
| 1620 | struct target_msghdr *target_msgh) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1621 | { |
| 1622 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1623 | abi_long msg_controllen; |
| 1624 | abi_ulong target_cmsg_addr; |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1625 | struct target_cmsghdr *target_cmsg, *target_cmsg_start; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1626 | socklen_t space = 0; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1627 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1628 | msg_controllen = tswapal(target_msgh->msg_controllen); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1629 | if (msg_controllen < sizeof (struct target_cmsghdr)) |
| 1630 | goto the_end; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1631 | target_cmsg_addr = tswapal(target_msgh->msg_control); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1632 | 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] | 1633 | target_cmsg_start = target_cmsg; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1634 | if (!target_cmsg) |
| 1635 | return -TARGET_EFAULT; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1636 | |
| 1637 | while (cmsg && target_cmsg) { |
| 1638 | void *data = CMSG_DATA(cmsg); |
| 1639 | void *target_data = TARGET_CMSG_DATA(target_cmsg); |
| 1640 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1641 | int len = tswapal(target_cmsg->cmsg_len) |
Peter Maydell | ad762b9 | 2017-12-15 13:52:56 +0000 | [diff] [blame] | 1642 | - sizeof(struct target_cmsghdr); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1643 | |
| 1644 | space += CMSG_SPACE(len); |
| 1645 | if (space > msgh->msg_controllen) { |
| 1646 | space -= CMSG_SPACE(len); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1647 | /* This is a QEMU bug, since we allocated the payload |
| 1648 | * area ourselves (unlike overflow in host-to-target |
| 1649 | * conversion, which is just the guest giving us a buffer |
| 1650 | * that's too small). It can't happen for the payload types |
| 1651 | * we currently support; if it becomes an issue in future |
| 1652 | * we would need to improve our allocation strategy to |
| 1653 | * something more intelligent than "twice the size of the |
| 1654 | * target buffer we're reading from". |
| 1655 | */ |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 1656 | qemu_log_mask(LOG_UNIMP, |
| 1657 | ("Unsupported ancillary data %d/%d: " |
| 1658 | "unhandled msg size\n"), |
| 1659 | tswap32(target_cmsg->cmsg_level), |
| 1660 | tswap32(target_cmsg->cmsg_type)); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | } |
| 1663 | |
Petar Jovanovic | dbf4f79 | 2013-09-13 19:27:29 +0200 | [diff] [blame] | 1664 | if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) { |
| 1665 | cmsg->cmsg_level = SOL_SOCKET; |
| 1666 | } else { |
| 1667 | cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level); |
| 1668 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1669 | cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type); |
| 1670 | cmsg->cmsg_len = CMSG_LEN(len); |
| 1671 | |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1672 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1673 | int *fd = (int *)data; |
| 1674 | int *target_fd = (int *)target_data; |
| 1675 | int i, numfds = len / sizeof(int); |
| 1676 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1677 | for (i = 0; i < numfds; i++) { |
| 1678 | __get_user(fd[i], target_fd + i); |
| 1679 | } |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1680 | } else if (cmsg->cmsg_level == SOL_SOCKET |
| 1681 | && cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 1682 | struct ucred *cred = (struct ucred *)data; |
| 1683 | struct target_ucred *target_cred = |
| 1684 | (struct target_ucred *)target_data; |
| 1685 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1686 | __get_user(cred->pid, &target_cred->pid); |
| 1687 | __get_user(cred->uid, &target_cred->uid); |
| 1688 | __get_user(cred->gid, &target_cred->gid); |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1689 | } else { |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 1690 | qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n", |
| 1691 | cmsg->cmsg_level, cmsg->cmsg_type); |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1692 | memcpy(data, target_data, len); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | cmsg = CMSG_NXTHDR(msgh, cmsg); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1696 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, |
| 1697 | target_cmsg_start); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1698 | } |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1699 | unlock_user(target_cmsg, target_cmsg_addr, 0); |
| 1700 | the_end: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1701 | msgh->msg_controllen = space; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1702 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1705 | static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh, |
| 1706 | struct msghdr *msgh) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1707 | { |
| 1708 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1709 | abi_long msg_controllen; |
| 1710 | abi_ulong target_cmsg_addr; |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1711 | struct target_cmsghdr *target_cmsg, *target_cmsg_start; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1712 | socklen_t space = 0; |
| 1713 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1714 | msg_controllen = tswapal(target_msgh->msg_controllen); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1715 | if (msg_controllen < sizeof (struct target_cmsghdr)) |
| 1716 | goto the_end; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1717 | target_cmsg_addr = tswapal(target_msgh->msg_control); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1718 | 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] | 1719 | target_cmsg_start = target_cmsg; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1720 | if (!target_cmsg) |
| 1721 | return -TARGET_EFAULT; |
| 1722 | |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1723 | while (cmsg && target_cmsg) { |
| 1724 | void *data = CMSG_DATA(cmsg); |
| 1725 | void *target_data = TARGET_CMSG_DATA(target_cmsg); |
| 1726 | |
Peter Maydell | ad762b9 | 2017-12-15 13:52:56 +0000 | [diff] [blame] | 1727 | int len = cmsg->cmsg_len - sizeof(struct cmsghdr); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1728 | int tgt_len, tgt_space; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1729 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1730 | /* We never copy a half-header but may copy half-data; |
| 1731 | * this is Linux's behaviour in put_cmsg(). Note that |
| 1732 | * truncation here is a guest problem (which we report |
| 1733 | * to the guest via the CTRUNC bit), unlike truncation |
| 1734 | * in target_to_host_cmsg, which is a QEMU bug. |
| 1735 | */ |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1736 | if (msg_controllen < sizeof(struct target_cmsghdr)) { |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1737 | target_msgh->msg_flags |= tswap32(MSG_CTRUNC); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1738 | break; |
| 1739 | } |
| 1740 | |
Petar Jovanovic | dbf4f79 | 2013-09-13 19:27:29 +0200 | [diff] [blame] | 1741 | if (cmsg->cmsg_level == SOL_SOCKET) { |
| 1742 | target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET); |
| 1743 | } else { |
| 1744 | target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level); |
| 1745 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1746 | target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1747 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1748 | /* Payload types which need a different size of payload on |
| 1749 | * the target must adjust tgt_len here. |
| 1750 | */ |
Peter Maydell | 309786c | 2018-05-18 19:47:15 +0100 | [diff] [blame] | 1751 | tgt_len = len; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1752 | switch (cmsg->cmsg_level) { |
| 1753 | case SOL_SOCKET: |
| 1754 | switch (cmsg->cmsg_type) { |
| 1755 | case SO_TIMESTAMP: |
| 1756 | tgt_len = sizeof(struct target_timeval); |
| 1757 | break; |
| 1758 | default: |
| 1759 | break; |
| 1760 | } |
Peter Maydell | 309786c | 2018-05-18 19:47:15 +0100 | [diff] [blame] | 1761 | break; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1762 | default: |
| 1763 | break; |
| 1764 | } |
| 1765 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1766 | if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) { |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1767 | target_msgh->msg_flags |= tswap32(MSG_CTRUNC); |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1768 | tgt_len = msg_controllen - sizeof(struct target_cmsghdr); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /* We must now copy-and-convert len bytes of payload |
| 1772 | * into tgt_len bytes of destination space. Bear in mind |
| 1773 | * that in both source and destination we may be dealing |
| 1774 | * with a truncated value! |
| 1775 | */ |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1776 | switch (cmsg->cmsg_level) { |
| 1777 | case SOL_SOCKET: |
| 1778 | switch (cmsg->cmsg_type) { |
| 1779 | case SCM_RIGHTS: |
| 1780 | { |
| 1781 | int *fd = (int *)data; |
| 1782 | int *target_fd = (int *)target_data; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1783 | int i, numfds = tgt_len / sizeof(int); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1784 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1785 | for (i = 0; i < numfds; i++) { |
| 1786 | __put_user(fd[i], target_fd + i); |
| 1787 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1788 | break; |
| 1789 | } |
| 1790 | case SO_TIMESTAMP: |
| 1791 | { |
| 1792 | struct timeval *tv = (struct timeval *)data; |
| 1793 | struct target_timeval *target_tv = |
| 1794 | (struct target_timeval *)target_data; |
Jing Huang | aebf5bc | 2012-07-24 14:01:42 +0000 | [diff] [blame] | 1795 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1796 | if (len != sizeof(struct timeval) || |
| 1797 | tgt_len != sizeof(struct target_timeval)) { |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1798 | goto unimplemented; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1799 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1800 | |
| 1801 | /* copy struct timeval to target */ |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1802 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1803 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1804 | break; |
| 1805 | } |
Huw Davies | 4bc2975 | 2014-04-17 14:02:48 +0100 | [diff] [blame] | 1806 | case SCM_CREDENTIALS: |
| 1807 | { |
| 1808 | struct ucred *cred = (struct ucred *)data; |
| 1809 | struct target_ucred *target_cred = |
| 1810 | (struct target_ucred *)target_data; |
| 1811 | |
| 1812 | __put_user(cred->pid, &target_cred->pid); |
| 1813 | __put_user(cred->uid, &target_cred->uid); |
| 1814 | __put_user(cred->gid, &target_cred->gid); |
| 1815 | break; |
| 1816 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1817 | default: |
| 1818 | goto unimplemented; |
| 1819 | } |
| 1820 | break; |
| 1821 | |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1822 | case SOL_IP: |
| 1823 | switch (cmsg->cmsg_type) { |
| 1824 | case IP_TTL: |
| 1825 | { |
| 1826 | uint32_t *v = (uint32_t *)data; |
| 1827 | uint32_t *t_int = (uint32_t *)target_data; |
| 1828 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1829 | if (len != sizeof(uint32_t) || |
| 1830 | tgt_len != sizeof(uint32_t)) { |
| 1831 | goto unimplemented; |
| 1832 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1833 | __put_user(*v, t_int); |
| 1834 | break; |
| 1835 | } |
| 1836 | case IP_RECVERR: |
| 1837 | { |
| 1838 | struct errhdr_t { |
| 1839 | struct sock_extended_err ee; |
| 1840 | struct sockaddr_in offender; |
| 1841 | }; |
| 1842 | struct errhdr_t *errh = (struct errhdr_t *)data; |
| 1843 | struct errhdr_t *target_errh = |
| 1844 | (struct errhdr_t *)target_data; |
| 1845 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1846 | if (len != sizeof(struct errhdr_t) || |
| 1847 | tgt_len != sizeof(struct errhdr_t)) { |
| 1848 | goto unimplemented; |
| 1849 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1850 | __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno); |
| 1851 | __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin); |
| 1852 | __put_user(errh->ee.ee_type, &target_errh->ee.ee_type); |
| 1853 | __put_user(errh->ee.ee_code, &target_errh->ee.ee_code); |
| 1854 | __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad); |
| 1855 | __put_user(errh->ee.ee_info, &target_errh->ee.ee_info); |
| 1856 | __put_user(errh->ee.ee_data, &target_errh->ee.ee_data); |
| 1857 | host_to_target_sockaddr((unsigned long) &target_errh->offender, |
| 1858 | (void *) &errh->offender, sizeof(errh->offender)); |
| 1859 | break; |
| 1860 | } |
| 1861 | default: |
| 1862 | goto unimplemented; |
| 1863 | } |
| 1864 | break; |
| 1865 | |
| 1866 | case SOL_IPV6: |
| 1867 | switch (cmsg->cmsg_type) { |
| 1868 | case IPV6_HOPLIMIT: |
| 1869 | { |
| 1870 | uint32_t *v = (uint32_t *)data; |
| 1871 | uint32_t *t_int = (uint32_t *)target_data; |
| 1872 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1873 | if (len != sizeof(uint32_t) || |
| 1874 | tgt_len != sizeof(uint32_t)) { |
| 1875 | goto unimplemented; |
| 1876 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1877 | __put_user(*v, t_int); |
| 1878 | break; |
| 1879 | } |
| 1880 | case IPV6_RECVERR: |
| 1881 | { |
| 1882 | struct errhdr6_t { |
| 1883 | struct sock_extended_err ee; |
| 1884 | struct sockaddr_in6 offender; |
| 1885 | }; |
| 1886 | struct errhdr6_t *errh = (struct errhdr6_t *)data; |
| 1887 | struct errhdr6_t *target_errh = |
| 1888 | (struct errhdr6_t *)target_data; |
| 1889 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1890 | if (len != sizeof(struct errhdr6_t) || |
| 1891 | tgt_len != sizeof(struct errhdr6_t)) { |
| 1892 | goto unimplemented; |
| 1893 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1894 | __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno); |
| 1895 | __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin); |
| 1896 | __put_user(errh->ee.ee_type, &target_errh->ee.ee_type); |
| 1897 | __put_user(errh->ee.ee_code, &target_errh->ee.ee_code); |
| 1898 | __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad); |
| 1899 | __put_user(errh->ee.ee_info, &target_errh->ee.ee_info); |
| 1900 | __put_user(errh->ee.ee_data, &target_errh->ee.ee_data); |
| 1901 | host_to_target_sockaddr((unsigned long) &target_errh->offender, |
| 1902 | (void *) &errh->offender, sizeof(errh->offender)); |
| 1903 | break; |
| 1904 | } |
| 1905 | default: |
| 1906 | goto unimplemented; |
| 1907 | } |
| 1908 | break; |
| 1909 | |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1910 | default: |
| 1911 | unimplemented: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 1912 | qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n", |
| 1913 | cmsg->cmsg_level, cmsg->cmsg_type); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1914 | memcpy(target_data, data, MIN(len, tgt_len)); |
| 1915 | if (tgt_len > len) { |
| 1916 | memset(target_data + len, 0, tgt_len - len); |
| 1917 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Peter Maydell | 7174970 | 2017-12-15 13:52:55 +0000 | [diff] [blame] | 1920 | target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len)); |
| 1921 | tgt_space = TARGET_CMSG_SPACE(tgt_len); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1922 | if (msg_controllen < tgt_space) { |
| 1923 | tgt_space = msg_controllen; |
| 1924 | } |
| 1925 | msg_controllen -= tgt_space; |
| 1926 | space += tgt_space; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1927 | cmsg = CMSG_NXTHDR(msgh, cmsg); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1928 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, |
| 1929 | target_cmsg_start); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1930 | } |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1931 | unlock_user(target_cmsg, target_cmsg_addr, space); |
| 1932 | the_end: |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1933 | target_msgh->msg_controllen = tswapal(space); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1934 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 1937 | /* do_setsockopt() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1938 | static abi_long do_setsockopt(int sockfd, int level, int optname, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 1939 | abi_ulong optval_addr, socklen_t optlen) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1940 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1941 | abi_long ret; |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 1942 | int val; |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 1943 | struct ip_mreqn *ip_mreq; |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 1944 | struct ip_mreq_source *ip_mreq_source; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1945 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 1946 | switch(level) { |
| 1947 | case SOL_TCP: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1948 | /* TCP options all take an 'int' value. */ |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1949 | if (optlen < sizeof(uint32_t)) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 1950 | return -TARGET_EINVAL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1951 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 1952 | if (get_user_u32(val, optval_addr)) |
| 1953 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 1954 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
| 1955 | break; |
| 1956 | case SOL_IP: |
| 1957 | switch(optname) { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 1958 | case IP_TOS: |
| 1959 | case IP_TTL: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 1960 | case IP_HDRINCL: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 1961 | case IP_ROUTER_ALERT: |
| 1962 | case IP_RECVOPTS: |
| 1963 | case IP_RETOPTS: |
| 1964 | case IP_PKTINFO: |
| 1965 | case IP_MTU_DISCOVER: |
| 1966 | case IP_RECVERR: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 1967 | case IP_RECVTTL: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 1968 | case IP_RECVTOS: |
| 1969 | #ifdef IP_FREEBIND |
| 1970 | case IP_FREEBIND: |
| 1971 | #endif |
| 1972 | case IP_MULTICAST_TTL: |
| 1973 | case IP_MULTICAST_LOOP: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 1974 | val = 0; |
| 1975 | if (optlen >= sizeof(uint32_t)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 1976 | if (get_user_u32(val, optval_addr)) |
| 1977 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 1978 | } else if (optlen >= 1) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 1979 | if (get_user_u8(val, optval_addr)) |
| 1980 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 1981 | } |
| 1982 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
| 1983 | break; |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 1984 | case IP_ADD_MEMBERSHIP: |
| 1985 | case IP_DROP_MEMBERSHIP: |
| 1986 | if (optlen < sizeof (struct target_ip_mreq) || |
| 1987 | optlen > sizeof (struct target_ip_mreqn)) |
| 1988 | return -TARGET_EINVAL; |
| 1989 | |
| 1990 | ip_mreq = (struct ip_mreqn *) alloca(optlen); |
| 1991 | target_to_host_ip_mreq(ip_mreq, optval_addr, optlen); |
| 1992 | ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen)); |
| 1993 | break; |
| 1994 | |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 1995 | case IP_BLOCK_SOURCE: |
| 1996 | case IP_UNBLOCK_SOURCE: |
| 1997 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 1998 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 1999 | if (optlen != sizeof (struct target_ip_mreq_source)) |
| 2000 | return -TARGET_EINVAL; |
| 2001 | |
| 2002 | ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
| 2003 | ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen)); |
| 2004 | unlock_user (ip_mreq_source, optval_addr, 0); |
| 2005 | break; |
| 2006 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2007 | default: |
| 2008 | goto unimplemented; |
| 2009 | } |
| 2010 | break; |
Laurent Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2011 | case SOL_IPV6: |
| 2012 | switch (optname) { |
| 2013 | case IPV6_MTU_DISCOVER: |
| 2014 | case IPV6_MTU: |
| 2015 | case IPV6_V6ONLY: |
| 2016 | case IPV6_RECVPKTINFO: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2017 | case IPV6_UNICAST_HOPS: |
Laurent Vivier | 21749c4 | 2018-06-27 23:21:52 +0200 | [diff] [blame] | 2018 | case IPV6_MULTICAST_HOPS: |
| 2019 | case IPV6_MULTICAST_LOOP: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2020 | case IPV6_RECVERR: |
| 2021 | case IPV6_RECVHOPLIMIT: |
| 2022 | case IPV6_2292HOPLIMIT: |
| 2023 | case IPV6_CHECKSUM: |
Helge Deller | b9cce6d | 2019-03-10 12:12:16 +0100 | [diff] [blame] | 2024 | case IPV6_ADDRFORM: |
| 2025 | case IPV6_2292PKTINFO: |
| 2026 | case IPV6_RECVTCLASS: |
| 2027 | case IPV6_RECVRTHDR: |
| 2028 | case IPV6_2292RTHDR: |
| 2029 | case IPV6_RECVHOPOPTS: |
| 2030 | case IPV6_2292HOPOPTS: |
| 2031 | case IPV6_RECVDSTOPTS: |
| 2032 | case IPV6_2292DSTOPTS: |
| 2033 | case IPV6_TCLASS: |
| 2034 | #ifdef IPV6_RECVPATHMTU |
| 2035 | case IPV6_RECVPATHMTU: |
| 2036 | #endif |
| 2037 | #ifdef IPV6_TRANSPARENT |
| 2038 | case IPV6_TRANSPARENT: |
| 2039 | #endif |
| 2040 | #ifdef IPV6_FREEBIND |
| 2041 | case IPV6_FREEBIND: |
| 2042 | #endif |
| 2043 | #ifdef IPV6_RECVORIGDSTADDR |
| 2044 | case IPV6_RECVORIGDSTADDR: |
| 2045 | #endif |
Laurent Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2046 | val = 0; |
| 2047 | if (optlen < sizeof(uint32_t)) { |
| 2048 | return -TARGET_EINVAL; |
| 2049 | } |
| 2050 | if (get_user_u32(val, optval_addr)) { |
| 2051 | return -TARGET_EFAULT; |
| 2052 | } |
| 2053 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2054 | &val, sizeof(val))); |
| 2055 | break; |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2056 | case IPV6_PKTINFO: |
| 2057 | { |
| 2058 | struct in6_pktinfo pki; |
| 2059 | |
| 2060 | if (optlen < sizeof(pki)) { |
| 2061 | return -TARGET_EINVAL; |
| 2062 | } |
| 2063 | |
| 2064 | if (copy_from_user(&pki, optval_addr, sizeof(pki))) { |
| 2065 | return -TARGET_EFAULT; |
| 2066 | } |
| 2067 | |
| 2068 | pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex); |
| 2069 | |
| 2070 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2071 | &pki, sizeof(pki))); |
| 2072 | break; |
| 2073 | } |
Neng Chen | 22bf4ee | 2019-06-19 16:17:10 +0200 | [diff] [blame] | 2074 | case IPV6_ADD_MEMBERSHIP: |
| 2075 | case IPV6_DROP_MEMBERSHIP: |
| 2076 | { |
| 2077 | struct ipv6_mreq ipv6mreq; |
| 2078 | |
| 2079 | if (optlen < sizeof(ipv6mreq)) { |
| 2080 | return -TARGET_EINVAL; |
| 2081 | } |
| 2082 | |
| 2083 | if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) { |
| 2084 | return -TARGET_EFAULT; |
| 2085 | } |
| 2086 | |
| 2087 | ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface); |
| 2088 | |
| 2089 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2090 | &ipv6mreq, sizeof(ipv6mreq))); |
| 2091 | break; |
| 2092 | } |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2093 | default: |
| 2094 | goto unimplemented; |
| 2095 | } |
| 2096 | break; |
| 2097 | case SOL_ICMPV6: |
| 2098 | switch (optname) { |
| 2099 | case ICMPV6_FILTER: |
| 2100 | { |
| 2101 | struct icmp6_filter icmp6f; |
| 2102 | |
| 2103 | if (optlen > sizeof(icmp6f)) { |
| 2104 | optlen = sizeof(icmp6f); |
| 2105 | } |
| 2106 | |
| 2107 | if (copy_from_user(&icmp6f, optval_addr, optlen)) { |
| 2108 | return -TARGET_EFAULT; |
| 2109 | } |
| 2110 | |
| 2111 | for (val = 0; val < 8; val++) { |
| 2112 | icmp6f.data[val] = tswap32(icmp6f.data[val]); |
| 2113 | } |
| 2114 | |
| 2115 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2116 | &icmp6f, optlen)); |
| 2117 | break; |
| 2118 | } |
Laurent Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2119 | default: |
| 2120 | goto unimplemented; |
| 2121 | } |
| 2122 | break; |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 2123 | case SOL_RAW: |
| 2124 | switch (optname) { |
| 2125 | case ICMP_FILTER: |
Helge Deller | ee1ac3a | 2017-02-18 23:31:30 +0100 | [diff] [blame] | 2126 | case IPV6_CHECKSUM: |
| 2127 | /* those take an u32 value */ |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 2128 | if (optlen < sizeof(uint32_t)) { |
| 2129 | return -TARGET_EINVAL; |
| 2130 | } |
| 2131 | |
| 2132 | if (get_user_u32(val, optval_addr)) { |
| 2133 | return -TARGET_EFAULT; |
| 2134 | } |
| 2135 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2136 | &val, sizeof(val))); |
| 2137 | break; |
| 2138 | |
| 2139 | default: |
| 2140 | goto unimplemented; |
| 2141 | } |
| 2142 | break; |
Yunqiang Su | f31dddd | 2019-06-19 16:17:11 +0200 | [diff] [blame] | 2143 | #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE) |
| 2144 | case SOL_ALG: |
| 2145 | switch (optname) { |
| 2146 | case ALG_SET_KEY: |
| 2147 | { |
| 2148 | char *alg_key = g_malloc(optlen); |
| 2149 | |
| 2150 | if (!alg_key) { |
| 2151 | return -TARGET_ENOMEM; |
| 2152 | } |
| 2153 | if (copy_from_user(alg_key, optval_addr, optlen)) { |
| 2154 | g_free(alg_key); |
| 2155 | return -TARGET_EFAULT; |
| 2156 | } |
| 2157 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2158 | alg_key, optlen)); |
| 2159 | g_free(alg_key); |
| 2160 | break; |
| 2161 | } |
| 2162 | case ALG_SET_AEAD_AUTHSIZE: |
| 2163 | { |
| 2164 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2165 | NULL, optlen)); |
| 2166 | break; |
| 2167 | } |
| 2168 | default: |
| 2169 | goto unimplemented; |
| 2170 | } |
| 2171 | break; |
| 2172 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2173 | case TARGET_SOL_SOCKET: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2174 | switch (optname) { |
Laurent Vivier | 1b09aeb | 2013-01-01 08:24:11 +0000 | [diff] [blame] | 2175 | case TARGET_SO_RCVTIMEO: |
| 2176 | { |
| 2177 | struct timeval tv; |
| 2178 | |
| 2179 | optname = SO_RCVTIMEO; |
| 2180 | |
| 2181 | set_timeout: |
| 2182 | if (optlen != sizeof(struct target_timeval)) { |
| 2183 | return -TARGET_EINVAL; |
| 2184 | } |
| 2185 | |
| 2186 | if (copy_from_user_timeval(&tv, optval_addr)) { |
| 2187 | return -TARGET_EFAULT; |
| 2188 | } |
| 2189 | |
| 2190 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, |
| 2191 | &tv, sizeof(tv))); |
| 2192 | return ret; |
| 2193 | } |
| 2194 | case TARGET_SO_SNDTIMEO: |
| 2195 | optname = SO_SNDTIMEO; |
| 2196 | goto set_timeout; |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2197 | case TARGET_SO_ATTACH_FILTER: |
| 2198 | { |
| 2199 | struct target_sock_fprog *tfprog; |
| 2200 | struct target_sock_filter *tfilter; |
| 2201 | struct sock_fprog fprog; |
| 2202 | struct sock_filter *filter; |
| 2203 | int i; |
| 2204 | |
| 2205 | if (optlen != sizeof(*tfprog)) { |
| 2206 | return -TARGET_EINVAL; |
| 2207 | } |
| 2208 | if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) { |
| 2209 | return -TARGET_EFAULT; |
| 2210 | } |
| 2211 | if (!lock_user_struct(VERIFY_READ, tfilter, |
| 2212 | tswapal(tfprog->filter), 0)) { |
| 2213 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2214 | return -TARGET_EFAULT; |
| 2215 | } |
| 2216 | |
| 2217 | fprog.len = tswap16(tfprog->len); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2218 | filter = g_try_new(struct sock_filter, fprog.len); |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2219 | if (filter == NULL) { |
| 2220 | unlock_user_struct(tfilter, tfprog->filter, 1); |
| 2221 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2222 | return -TARGET_ENOMEM; |
| 2223 | } |
| 2224 | for (i = 0; i < fprog.len; i++) { |
| 2225 | filter[i].code = tswap16(tfilter[i].code); |
| 2226 | filter[i].jt = tfilter[i].jt; |
| 2227 | filter[i].jf = tfilter[i].jf; |
| 2228 | filter[i].k = tswap32(tfilter[i].k); |
| 2229 | } |
| 2230 | fprog.filter = filter; |
| 2231 | |
| 2232 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, |
| 2233 | SO_ATTACH_FILTER, &fprog, sizeof(fprog))); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2234 | g_free(filter); |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2235 | |
| 2236 | unlock_user_struct(tfilter, tfprog->filter, 1); |
| 2237 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2238 | return ret; |
| 2239 | } |
Joakim Tjernlund | 451aaf6 | 2014-07-12 15:47:06 +0200 | [diff] [blame] | 2240 | case TARGET_SO_BINDTODEVICE: |
| 2241 | { |
| 2242 | char *dev_ifname, *addr_ifname; |
| 2243 | |
| 2244 | if (optlen > IFNAMSIZ - 1) { |
| 2245 | optlen = IFNAMSIZ - 1; |
| 2246 | } |
| 2247 | dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
| 2248 | if (!dev_ifname) { |
| 2249 | return -TARGET_EFAULT; |
| 2250 | } |
| 2251 | optname = SO_BINDTODEVICE; |
| 2252 | addr_ifname = alloca(IFNAMSIZ); |
| 2253 | memcpy(addr_ifname, dev_ifname, optlen); |
| 2254 | addr_ifname[optlen] = 0; |
Chen Gang | fad6c58 | 2016-01-11 13:58:50 +0800 | [diff] [blame] | 2255 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, |
| 2256 | addr_ifname, optlen)); |
Joakim Tjernlund | 451aaf6 | 2014-07-12 15:47:06 +0200 | [diff] [blame] | 2257 | unlock_user (dev_ifname, optval_addr, 0); |
| 2258 | return ret; |
| 2259 | } |
Carlo Marcelo Arenas Belón | 83eb6e5 | 2018-08-24 01:56:01 -0700 | [diff] [blame] | 2260 | case TARGET_SO_LINGER: |
| 2261 | { |
| 2262 | struct linger lg; |
| 2263 | struct target_linger *tlg; |
| 2264 | |
| 2265 | if (optlen != sizeof(struct target_linger)) { |
| 2266 | return -TARGET_EINVAL; |
| 2267 | } |
| 2268 | if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) { |
| 2269 | return -TARGET_EFAULT; |
| 2270 | } |
| 2271 | __get_user(lg.l_onoff, &tlg->l_onoff); |
| 2272 | __get_user(lg.l_linger, &tlg->l_linger); |
| 2273 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER, |
| 2274 | &lg, sizeof(lg))); |
| 2275 | unlock_user_struct(tlg, optval_addr, 0); |
| 2276 | return ret; |
| 2277 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2278 | /* Options with 'int' argument. */ |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2279 | case TARGET_SO_DEBUG: |
| 2280 | optname = SO_DEBUG; |
| 2281 | break; |
| 2282 | case TARGET_SO_REUSEADDR: |
| 2283 | optname = SO_REUSEADDR; |
| 2284 | break; |
Yunqiang Su | 113a9dd | 2018-10-30 13:55:08 +0100 | [diff] [blame] | 2285 | #ifdef SO_REUSEPORT |
| 2286 | case TARGET_SO_REUSEPORT: |
| 2287 | optname = SO_REUSEPORT; |
| 2288 | break; |
| 2289 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2290 | case TARGET_SO_TYPE: |
| 2291 | optname = SO_TYPE; |
| 2292 | break; |
| 2293 | case TARGET_SO_ERROR: |
| 2294 | optname = SO_ERROR; |
| 2295 | break; |
| 2296 | case TARGET_SO_DONTROUTE: |
| 2297 | optname = SO_DONTROUTE; |
| 2298 | break; |
| 2299 | case TARGET_SO_BROADCAST: |
| 2300 | optname = SO_BROADCAST; |
| 2301 | break; |
| 2302 | case TARGET_SO_SNDBUF: |
| 2303 | optname = SO_SNDBUF; |
| 2304 | break; |
Paul Burton | d79b6cc | 2014-06-22 11:25:35 +0100 | [diff] [blame] | 2305 | case TARGET_SO_SNDBUFFORCE: |
| 2306 | optname = SO_SNDBUFFORCE; |
| 2307 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2308 | case TARGET_SO_RCVBUF: |
| 2309 | optname = SO_RCVBUF; |
| 2310 | break; |
Paul Burton | d79b6cc | 2014-06-22 11:25:35 +0100 | [diff] [blame] | 2311 | case TARGET_SO_RCVBUFFORCE: |
| 2312 | optname = SO_RCVBUFFORCE; |
| 2313 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2314 | case TARGET_SO_KEEPALIVE: |
| 2315 | optname = SO_KEEPALIVE; |
| 2316 | break; |
| 2317 | case TARGET_SO_OOBINLINE: |
| 2318 | optname = SO_OOBINLINE; |
| 2319 | break; |
| 2320 | case TARGET_SO_NO_CHECK: |
| 2321 | optname = SO_NO_CHECK; |
| 2322 | break; |
| 2323 | case TARGET_SO_PRIORITY: |
| 2324 | optname = SO_PRIORITY; |
| 2325 | break; |
bellard | 5e83e8e | 2005-03-01 22:32:06 +0000 | [diff] [blame] | 2326 | #ifdef SO_BSDCOMPAT |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2327 | case TARGET_SO_BSDCOMPAT: |
| 2328 | optname = SO_BSDCOMPAT; |
| 2329 | break; |
bellard | 5e83e8e | 2005-03-01 22:32:06 +0000 | [diff] [blame] | 2330 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2331 | case TARGET_SO_PASSCRED: |
| 2332 | optname = SO_PASSCRED; |
| 2333 | break; |
Paul Burton | 82d0fe6 | 2014-06-22 11:25:36 +0100 | [diff] [blame] | 2334 | case TARGET_SO_PASSSEC: |
| 2335 | optname = SO_PASSSEC; |
| 2336 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2337 | case TARGET_SO_TIMESTAMP: |
| 2338 | optname = SO_TIMESTAMP; |
| 2339 | break; |
| 2340 | case TARGET_SO_RCVLOWAT: |
| 2341 | optname = SO_RCVLOWAT; |
| 2342 | break; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2343 | default: |
| 2344 | goto unimplemented; |
| 2345 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2346 | if (optlen < sizeof(uint32_t)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2347 | return -TARGET_EINVAL; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2348 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2349 | if (get_user_u32(val, optval_addr)) |
| 2350 | return -TARGET_EFAULT; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2351 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val))); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2352 | break; |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2353 | #ifdef SOL_NETLINK |
| 2354 | case SOL_NETLINK: |
| 2355 | switch (optname) { |
| 2356 | case NETLINK_PKTINFO: |
| 2357 | case NETLINK_ADD_MEMBERSHIP: |
| 2358 | case NETLINK_DROP_MEMBERSHIP: |
| 2359 | case NETLINK_BROADCAST_ERROR: |
| 2360 | case NETLINK_NO_ENOBUFS: |
| 2361 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 2362 | case NETLINK_LISTEN_ALL_NSID: |
| 2363 | case NETLINK_CAP_ACK: |
| 2364 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ |
| 2365 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) |
| 2366 | case NETLINK_EXT_ACK: |
| 2367 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2368 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) |
| 2369 | case NETLINK_GET_STRICT_CHK: |
| 2370 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2371 | break; |
| 2372 | default: |
| 2373 | goto unimplemented; |
| 2374 | } |
| 2375 | val = 0; |
| 2376 | if (optlen < sizeof(uint32_t)) { |
| 2377 | return -TARGET_EINVAL; |
| 2378 | } |
| 2379 | if (get_user_u32(val, optval_addr)) { |
| 2380 | return -TARGET_EFAULT; |
| 2381 | } |
| 2382 | ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val, |
| 2383 | sizeof(val))); |
| 2384 | break; |
| 2385 | #endif /* SOL_NETLINK */ |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2386 | default: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2387 | unimplemented: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 2388 | qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n", |
| 2389 | level, optname); |
ths | 6fa13c1 | 2007-12-18 02:41:04 +0000 | [diff] [blame] | 2390 | ret = -TARGET_ENOPROTOOPT; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2391 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2392 | return ret; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2395 | /* do_getsockopt() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2396 | static abi_long do_getsockopt(int sockfd, int level, int optname, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2397 | abi_ulong optval_addr, abi_ulong optlen) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2398 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2399 | abi_long ret; |
blueswir1 | b55266b | 2008-09-20 08:07:15 +0000 | [diff] [blame] | 2400 | int len, val; |
| 2401 | socklen_t lv; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2402 | |
| 2403 | switch(level) { |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2404 | case TARGET_SOL_SOCKET: |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2405 | level = SOL_SOCKET; |
| 2406 | switch (optname) { |
| 2407 | /* These don't just return a single integer */ |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2408 | case TARGET_SO_PEERNAME: |
| 2409 | goto unimplemented; |
Andreas Schwab | 405dc4c | 2019-05-13 11:06:26 +0200 | [diff] [blame] | 2410 | case TARGET_SO_RCVTIMEO: { |
| 2411 | struct timeval tv; |
| 2412 | socklen_t tvlen; |
| 2413 | |
| 2414 | optname = SO_RCVTIMEO; |
| 2415 | |
| 2416 | get_timeout: |
| 2417 | if (get_user_u32(len, optlen)) { |
| 2418 | return -TARGET_EFAULT; |
| 2419 | } |
| 2420 | if (len < 0) { |
| 2421 | return -TARGET_EINVAL; |
| 2422 | } |
| 2423 | |
| 2424 | tvlen = sizeof(tv); |
| 2425 | ret = get_errno(getsockopt(sockfd, level, optname, |
| 2426 | &tv, &tvlen)); |
| 2427 | if (ret < 0) { |
| 2428 | return ret; |
| 2429 | } |
| 2430 | if (len > sizeof(struct target_timeval)) { |
| 2431 | len = sizeof(struct target_timeval); |
| 2432 | } |
| 2433 | if (copy_to_user_timeval(optval_addr, &tv)) { |
| 2434 | return -TARGET_EFAULT; |
| 2435 | } |
| 2436 | if (put_user_u32(len, optlen)) { |
| 2437 | return -TARGET_EFAULT; |
| 2438 | } |
| 2439 | break; |
| 2440 | } |
| 2441 | case TARGET_SO_SNDTIMEO: |
| 2442 | optname = SO_SNDTIMEO; |
| 2443 | goto get_timeout; |
Akos PASZTORY | 583359a | 2011-11-14 15:09:49 +0200 | [diff] [blame] | 2444 | case TARGET_SO_PEERCRED: { |
| 2445 | struct ucred cr; |
| 2446 | socklen_t crlen; |
| 2447 | struct target_ucred *tcr; |
| 2448 | |
| 2449 | if (get_user_u32(len, optlen)) { |
| 2450 | return -TARGET_EFAULT; |
| 2451 | } |
| 2452 | if (len < 0) { |
| 2453 | return -TARGET_EINVAL; |
| 2454 | } |
| 2455 | |
| 2456 | crlen = sizeof(cr); |
| 2457 | ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED, |
| 2458 | &cr, &crlen)); |
| 2459 | if (ret < 0) { |
| 2460 | return ret; |
| 2461 | } |
| 2462 | if (len > crlen) { |
| 2463 | len = crlen; |
| 2464 | } |
| 2465 | if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) { |
| 2466 | return -TARGET_EFAULT; |
| 2467 | } |
| 2468 | __put_user(cr.pid, &tcr->pid); |
| 2469 | __put_user(cr.uid, &tcr->uid); |
| 2470 | __put_user(cr.gid, &tcr->gid); |
| 2471 | unlock_user_struct(tcr, optval_addr, 1); |
| 2472 | if (put_user_u32(len, optlen)) { |
| 2473 | return -TARGET_EFAULT; |
| 2474 | } |
| 2475 | break; |
| 2476 | } |
Laurent Vivier | 6d485a5 | 2020-02-04 22:19:01 +0100 | [diff] [blame] | 2477 | case TARGET_SO_PEERSEC: { |
| 2478 | char *name; |
| 2479 | |
| 2480 | if (get_user_u32(len, optlen)) { |
| 2481 | return -TARGET_EFAULT; |
| 2482 | } |
| 2483 | if (len < 0) { |
| 2484 | return -TARGET_EINVAL; |
| 2485 | } |
| 2486 | name = lock_user(VERIFY_WRITE, optval_addr, len, 0); |
| 2487 | if (!name) { |
| 2488 | return -TARGET_EFAULT; |
| 2489 | } |
| 2490 | lv = len; |
| 2491 | ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC, |
| 2492 | name, &lv)); |
| 2493 | if (put_user_u32(lv, optlen)) { |
| 2494 | ret = -TARGET_EFAULT; |
| 2495 | } |
| 2496 | unlock_user(name, optval_addr, lv); |
| 2497 | break; |
| 2498 | } |
Carlo Marcelo Arenas Belón | 83eb6e5 | 2018-08-24 01:56:01 -0700 | [diff] [blame] | 2499 | case TARGET_SO_LINGER: |
| 2500 | { |
| 2501 | struct linger lg; |
| 2502 | socklen_t lglen; |
| 2503 | struct target_linger *tlg; |
| 2504 | |
| 2505 | if (get_user_u32(len, optlen)) { |
| 2506 | return -TARGET_EFAULT; |
| 2507 | } |
| 2508 | if (len < 0) { |
| 2509 | return -TARGET_EINVAL; |
| 2510 | } |
| 2511 | |
| 2512 | lglen = sizeof(lg); |
| 2513 | ret = get_errno(getsockopt(sockfd, level, SO_LINGER, |
| 2514 | &lg, &lglen)); |
| 2515 | if (ret < 0) { |
| 2516 | return ret; |
| 2517 | } |
| 2518 | if (len > lglen) { |
| 2519 | len = lglen; |
| 2520 | } |
| 2521 | if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) { |
| 2522 | return -TARGET_EFAULT; |
| 2523 | } |
| 2524 | __put_user(lg.l_onoff, &tlg->l_onoff); |
| 2525 | __put_user(lg.l_linger, &tlg->l_linger); |
| 2526 | unlock_user_struct(tlg, optval_addr, 1); |
| 2527 | if (put_user_u32(len, optlen)) { |
| 2528 | return -TARGET_EFAULT; |
| 2529 | } |
| 2530 | break; |
| 2531 | } |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2532 | /* Options with 'int' argument. */ |
| 2533 | case TARGET_SO_DEBUG: |
| 2534 | optname = SO_DEBUG; |
| 2535 | goto int_case; |
| 2536 | case TARGET_SO_REUSEADDR: |
| 2537 | optname = SO_REUSEADDR; |
| 2538 | goto int_case; |
Yunqiang Su | 113a9dd | 2018-10-30 13:55:08 +0100 | [diff] [blame] | 2539 | #ifdef SO_REUSEPORT |
| 2540 | case TARGET_SO_REUSEPORT: |
| 2541 | optname = SO_REUSEPORT; |
| 2542 | goto int_case; |
| 2543 | #endif |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2544 | case TARGET_SO_TYPE: |
| 2545 | optname = SO_TYPE; |
| 2546 | goto int_case; |
| 2547 | case TARGET_SO_ERROR: |
| 2548 | optname = SO_ERROR; |
| 2549 | goto int_case; |
| 2550 | case TARGET_SO_DONTROUTE: |
| 2551 | optname = SO_DONTROUTE; |
| 2552 | goto int_case; |
| 2553 | case TARGET_SO_BROADCAST: |
| 2554 | optname = SO_BROADCAST; |
| 2555 | goto int_case; |
| 2556 | case TARGET_SO_SNDBUF: |
| 2557 | optname = SO_SNDBUF; |
| 2558 | goto int_case; |
| 2559 | case TARGET_SO_RCVBUF: |
| 2560 | optname = SO_RCVBUF; |
| 2561 | goto int_case; |
| 2562 | case TARGET_SO_KEEPALIVE: |
| 2563 | optname = SO_KEEPALIVE; |
| 2564 | goto int_case; |
| 2565 | case TARGET_SO_OOBINLINE: |
| 2566 | optname = SO_OOBINLINE; |
| 2567 | goto int_case; |
| 2568 | case TARGET_SO_NO_CHECK: |
| 2569 | optname = SO_NO_CHECK; |
| 2570 | goto int_case; |
| 2571 | case TARGET_SO_PRIORITY: |
| 2572 | optname = SO_PRIORITY; |
| 2573 | goto int_case; |
| 2574 | #ifdef SO_BSDCOMPAT |
| 2575 | case TARGET_SO_BSDCOMPAT: |
| 2576 | optname = SO_BSDCOMPAT; |
| 2577 | goto int_case; |
| 2578 | #endif |
| 2579 | case TARGET_SO_PASSCRED: |
| 2580 | optname = SO_PASSCRED; |
| 2581 | goto int_case; |
| 2582 | case TARGET_SO_TIMESTAMP: |
| 2583 | optname = SO_TIMESTAMP; |
| 2584 | goto int_case; |
| 2585 | case TARGET_SO_RCVLOWAT: |
| 2586 | optname = SO_RCVLOWAT; |
| 2587 | goto int_case; |
Paul Burton | aec1ca4 | 2014-06-22 11:25:34 +0100 | [diff] [blame] | 2588 | case TARGET_SO_ACCEPTCONN: |
| 2589 | optname = SO_ACCEPTCONN; |
| 2590 | goto int_case; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2591 | default: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2592 | goto int_case; |
| 2593 | } |
| 2594 | break; |
| 2595 | case SOL_TCP: |
| 2596 | /* TCP options all take an 'int' value. */ |
| 2597 | int_case: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2598 | if (get_user_u32(len, optlen)) |
| 2599 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2600 | if (len < 0) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2601 | return -TARGET_EINVAL; |
Mike Frysinger | 73160d9 | 2011-02-07 01:05:49 -0500 | [diff] [blame] | 2602 | lv = sizeof(lv); |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2603 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2604 | if (ret < 0) |
| 2605 | return ret; |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 2606 | if (optname == SO_TYPE) { |
| 2607 | val = host_to_target_sock_type(val); |
| 2608 | } |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2609 | if (len > lv) |
| 2610 | len = lv; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2611 | if (len == 4) { |
| 2612 | if (put_user_u32(val, optval_addr)) |
| 2613 | return -TARGET_EFAULT; |
| 2614 | } else { |
| 2615 | if (put_user_u8(val, optval_addr)) |
| 2616 | return -TARGET_EFAULT; |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2617 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2618 | if (put_user_u32(len, optlen)) |
| 2619 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | case SOL_IP: |
| 2622 | switch(optname) { |
| 2623 | case IP_TOS: |
| 2624 | case IP_TTL: |
| 2625 | case IP_HDRINCL: |
| 2626 | case IP_ROUTER_ALERT: |
| 2627 | case IP_RECVOPTS: |
| 2628 | case IP_RETOPTS: |
| 2629 | case IP_PKTINFO: |
| 2630 | case IP_MTU_DISCOVER: |
| 2631 | case IP_RECVERR: |
| 2632 | case IP_RECVTOS: |
| 2633 | #ifdef IP_FREEBIND |
| 2634 | case IP_FREEBIND: |
| 2635 | #endif |
| 2636 | case IP_MULTICAST_TTL: |
| 2637 | case IP_MULTICAST_LOOP: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2638 | if (get_user_u32(len, optlen)) |
| 2639 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2640 | if (len < 0) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2641 | return -TARGET_EINVAL; |
Mike Frysinger | 73160d9 | 2011-02-07 01:05:49 -0500 | [diff] [blame] | 2642 | lv = sizeof(lv); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2643 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2644 | if (ret < 0) |
| 2645 | return ret; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2646 | if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2647 | len = 1; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2648 | if (put_user_u32(len, optlen) |
| 2649 | || put_user_u8(val, optval_addr)) |
| 2650 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2651 | } else { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2652 | if (len > sizeof(int)) |
| 2653 | len = sizeof(int); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2654 | if (put_user_u32(len, optlen) |
| 2655 | || put_user_u32(val, optval_addr)) |
| 2656 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2657 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2658 | break; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2659 | default: |
ths | c02f499 | 2007-12-18 02:39:59 +0000 | [diff] [blame] | 2660 | ret = -TARGET_ENOPROTOOPT; |
| 2661 | break; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2662 | } |
| 2663 | break; |
Tom Deseyn | bd8ed48 | 2018-12-13 14:06:11 +0100 | [diff] [blame] | 2664 | case SOL_IPV6: |
| 2665 | switch (optname) { |
| 2666 | case IPV6_MTU_DISCOVER: |
| 2667 | case IPV6_MTU: |
| 2668 | case IPV6_V6ONLY: |
| 2669 | case IPV6_RECVPKTINFO: |
| 2670 | case IPV6_UNICAST_HOPS: |
| 2671 | case IPV6_MULTICAST_HOPS: |
| 2672 | case IPV6_MULTICAST_LOOP: |
| 2673 | case IPV6_RECVERR: |
| 2674 | case IPV6_RECVHOPLIMIT: |
| 2675 | case IPV6_2292HOPLIMIT: |
| 2676 | case IPV6_CHECKSUM: |
Helge Deller | b9cce6d | 2019-03-10 12:12:16 +0100 | [diff] [blame] | 2677 | case IPV6_ADDRFORM: |
| 2678 | case IPV6_2292PKTINFO: |
| 2679 | case IPV6_RECVTCLASS: |
| 2680 | case IPV6_RECVRTHDR: |
| 2681 | case IPV6_2292RTHDR: |
| 2682 | case IPV6_RECVHOPOPTS: |
| 2683 | case IPV6_2292HOPOPTS: |
| 2684 | case IPV6_RECVDSTOPTS: |
| 2685 | case IPV6_2292DSTOPTS: |
| 2686 | case IPV6_TCLASS: |
| 2687 | #ifdef IPV6_RECVPATHMTU |
| 2688 | case IPV6_RECVPATHMTU: |
| 2689 | #endif |
| 2690 | #ifdef IPV6_TRANSPARENT |
| 2691 | case IPV6_TRANSPARENT: |
| 2692 | #endif |
| 2693 | #ifdef IPV6_FREEBIND |
| 2694 | case IPV6_FREEBIND: |
| 2695 | #endif |
| 2696 | #ifdef IPV6_RECVORIGDSTADDR |
| 2697 | case IPV6_RECVORIGDSTADDR: |
| 2698 | #endif |
Tom Deseyn | bd8ed48 | 2018-12-13 14:06:11 +0100 | [diff] [blame] | 2699 | if (get_user_u32(len, optlen)) |
| 2700 | return -TARGET_EFAULT; |
| 2701 | if (len < 0) |
| 2702 | return -TARGET_EINVAL; |
| 2703 | lv = sizeof(lv); |
| 2704 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2705 | if (ret < 0) |
| 2706 | return ret; |
| 2707 | if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { |
| 2708 | len = 1; |
| 2709 | if (put_user_u32(len, optlen) |
| 2710 | || put_user_u8(val, optval_addr)) |
| 2711 | return -TARGET_EFAULT; |
| 2712 | } else { |
| 2713 | if (len > sizeof(int)) |
| 2714 | len = sizeof(int); |
| 2715 | if (put_user_u32(len, optlen) |
| 2716 | || put_user_u32(val, optval_addr)) |
| 2717 | return -TARGET_EFAULT; |
| 2718 | } |
| 2719 | break; |
| 2720 | default: |
| 2721 | ret = -TARGET_ENOPROTOOPT; |
| 2722 | break; |
| 2723 | } |
| 2724 | break; |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2725 | #ifdef SOL_NETLINK |
| 2726 | case SOL_NETLINK: |
| 2727 | switch (optname) { |
| 2728 | case NETLINK_PKTINFO: |
| 2729 | case NETLINK_BROADCAST_ERROR: |
| 2730 | case NETLINK_NO_ENOBUFS: |
| 2731 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 2732 | case NETLINK_LISTEN_ALL_NSID: |
| 2733 | case NETLINK_CAP_ACK: |
| 2734 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ |
| 2735 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) |
| 2736 | case NETLINK_EXT_ACK: |
| 2737 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2738 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) |
| 2739 | case NETLINK_GET_STRICT_CHK: |
| 2740 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ |
| 2741 | if (get_user_u32(len, optlen)) { |
| 2742 | return -TARGET_EFAULT; |
| 2743 | } |
| 2744 | if (len != sizeof(val)) { |
| 2745 | return -TARGET_EINVAL; |
| 2746 | } |
| 2747 | lv = len; |
| 2748 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2749 | if (ret < 0) { |
| 2750 | return ret; |
| 2751 | } |
| 2752 | if (put_user_u32(lv, optlen) |
| 2753 | || put_user_u32(val, optval_addr)) { |
| 2754 | return -TARGET_EFAULT; |
| 2755 | } |
| 2756 | break; |
| 2757 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 2758 | case NETLINK_LIST_MEMBERSHIPS: |
| 2759 | { |
| 2760 | uint32_t *results; |
| 2761 | int i; |
| 2762 | if (get_user_u32(len, optlen)) { |
| 2763 | return -TARGET_EFAULT; |
| 2764 | } |
| 2765 | if (len < 0) { |
| 2766 | return -TARGET_EINVAL; |
| 2767 | } |
| 2768 | results = lock_user(VERIFY_WRITE, optval_addr, len, 1); |
| 2769 | if (!results) { |
| 2770 | return -TARGET_EFAULT; |
| 2771 | } |
| 2772 | lv = len; |
| 2773 | ret = get_errno(getsockopt(sockfd, level, optname, results, &lv)); |
| 2774 | if (ret < 0) { |
| 2775 | unlock_user(results, optval_addr, 0); |
| 2776 | return ret; |
| 2777 | } |
| 2778 | /* swap host endianess to target endianess. */ |
| 2779 | for (i = 0; i < (len / sizeof(uint32_t)); i++) { |
| 2780 | results[i] = tswap32(results[i]); |
| 2781 | } |
| 2782 | if (put_user_u32(lv, optlen)) { |
| 2783 | return -TARGET_EFAULT; |
| 2784 | } |
| 2785 | unlock_user(results, optval_addr, 0); |
| 2786 | break; |
| 2787 | } |
| 2788 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ |
| 2789 | default: |
| 2790 | goto unimplemented; |
| 2791 | } |
Laurent Vivier | c0cb880 | 2019-11-12 11:50:55 +0100 | [diff] [blame] | 2792 | break; |
Josh Kunz | a2d8668 | 2019-10-29 15:43:10 -0700 | [diff] [blame] | 2793 | #endif /* SOL_NETLINK */ |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2794 | default: |
| 2795 | unimplemented: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 2796 | qemu_log_mask(LOG_UNIMP, |
| 2797 | "getsockopt level=%d optname=%d not yet supported\n", |
| 2798 | level, optname); |
ths | c02f499 | 2007-12-18 02:39:59 +0000 | [diff] [blame] | 2799 | ret = -TARGET_EOPNOTSUPP; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2800 | break; |
| 2801 | } |
| 2802 | return ret; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Max Filippov | 9ac2251 | 2018-04-04 17:30:41 -0700 | [diff] [blame] | 2805 | /* Convert target low/high pair representing file offset into the host |
| 2806 | * low/high pair. This function doesn't handle offsets bigger than 64 bits |
| 2807 | * as the kernel doesn't handle them either. |
| 2808 | */ |
| 2809 | static void target_to_host_low_high(abi_ulong tlow, |
| 2810 | abi_ulong thigh, |
| 2811 | unsigned long *hlow, |
| 2812 | unsigned long *hhigh) |
| 2813 | { |
| 2814 | uint64_t off = tlow | |
| 2815 | ((unsigned long long)thigh << TARGET_LONG_BITS / 2) << |
| 2816 | TARGET_LONG_BITS / 2; |
| 2817 | |
| 2818 | *hlow = off; |
| 2819 | *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2; |
| 2820 | } |
| 2821 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2822 | static struct iovec *lock_iovec(int type, abi_ulong target_addr, |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 2823 | abi_ulong count, int copy) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2824 | { |
| 2825 | struct target_iovec *target_vec; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2826 | struct iovec *vec; |
| 2827 | abi_ulong total_len, max_len; |
balrog | d732dcb | 2008-10-28 10:21:03 +0000 | [diff] [blame] | 2828 | int i; |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2829 | int err = 0; |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2830 | bool bad_address = false; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2831 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2832 | if (count == 0) { |
| 2833 | errno = 0; |
| 2834 | return NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2835 | } |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 2836 | if (count > IOV_MAX) { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2837 | errno = EINVAL; |
| 2838 | return NULL; |
| 2839 | } |
| 2840 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2841 | vec = g_try_new0(struct iovec, count); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2842 | if (vec == NULL) { |
| 2843 | errno = ENOMEM; |
| 2844 | return NULL; |
| 2845 | } |
| 2846 | |
| 2847 | target_vec = lock_user(VERIFY_READ, target_addr, |
| 2848 | count * sizeof(struct target_iovec), 1); |
| 2849 | if (target_vec == NULL) { |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2850 | err = EFAULT; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2851 | goto fail2; |
| 2852 | } |
| 2853 | |
| 2854 | /* ??? If host page size > target page size, this will result in a |
| 2855 | value larger than what we can actually support. */ |
| 2856 | max_len = 0x7fffffff & TARGET_PAGE_MASK; |
| 2857 | total_len = 0; |
| 2858 | |
| 2859 | for (i = 0; i < count; i++) { |
| 2860 | abi_ulong base = tswapal(target_vec[i].iov_base); |
| 2861 | abi_long len = tswapal(target_vec[i].iov_len); |
| 2862 | |
| 2863 | if (len < 0) { |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2864 | err = EINVAL; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2865 | goto fail; |
| 2866 | } else if (len == 0) { |
| 2867 | /* Zero length pointer is ignored. */ |
| 2868 | vec[i].iov_base = 0; |
| 2869 | } else { |
| 2870 | vec[i].iov_base = lock_user(type, base, len, copy); |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2871 | /* If the first buffer pointer is bad, this is a fault. But |
| 2872 | * subsequent bad buffers will result in a partial write; this |
| 2873 | * is realized by filling the vector with null pointers and |
| 2874 | * zero lengths. */ |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2875 | if (!vec[i].iov_base) { |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2876 | if (i == 0) { |
| 2877 | err = EFAULT; |
| 2878 | goto fail; |
| 2879 | } else { |
| 2880 | bad_address = true; |
| 2881 | } |
| 2882 | } |
| 2883 | if (bad_address) { |
| 2884 | len = 0; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2885 | } |
| 2886 | if (len > max_len - total_len) { |
| 2887 | len = max_len - total_len; |
| 2888 | } |
| 2889 | } |
| 2890 | vec[i].iov_len = len; |
| 2891 | total_len += len; |
| 2892 | } |
| 2893 | |
| 2894 | unlock_user(target_vec, target_addr, 0); |
| 2895 | return vec; |
| 2896 | |
| 2897 | fail: |
Chen Gang S | 7eff518 | 2015-01-23 18:01:09 +0800 | [diff] [blame] | 2898 | while (--i >= 0) { |
| 2899 | if (tswapal(target_vec[i].iov_len) > 0) { |
| 2900 | unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0); |
| 2901 | } |
| 2902 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2903 | unlock_user(target_vec, target_addr, 0); |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2904 | fail2: |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2905 | g_free(vec); |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2906 | errno = err; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2907 | return NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2910 | static void unlock_iovec(struct iovec *vec, abi_ulong target_addr, |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 2911 | abi_ulong count, int copy) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2912 | { |
| 2913 | struct target_iovec *target_vec; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2914 | int i; |
| 2915 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2916 | target_vec = lock_user(VERIFY_READ, target_addr, |
| 2917 | count * sizeof(struct target_iovec), 1); |
| 2918 | if (target_vec) { |
| 2919 | for (i = 0; i < count; i++) { |
| 2920 | abi_ulong base = tswapal(target_vec[i].iov_base); |
Chen Gang S | 71ec7ce | 2015-01-23 18:07:50 +0800 | [diff] [blame] | 2921 | abi_long len = tswapal(target_vec[i].iov_len); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2922 | if (len < 0) { |
| 2923 | break; |
| 2924 | } |
balrog | d732dcb | 2008-10-28 10:21:03 +0000 | [diff] [blame] | 2925 | unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); |
| 2926 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2927 | unlock_user(target_vec, target_addr, 0); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2928 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 2929 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2930 | g_free(vec); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2933 | static inline int target_to_host_sock_type(int *type) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2934 | { |
| 2935 | int host_type = 0; |
| 2936 | int target_type = *type; |
| 2937 | |
| 2938 | switch (target_type & TARGET_SOCK_TYPE_MASK) { |
| 2939 | case TARGET_SOCK_DGRAM: |
| 2940 | host_type = SOCK_DGRAM; |
| 2941 | break; |
| 2942 | case TARGET_SOCK_STREAM: |
| 2943 | host_type = SOCK_STREAM; |
| 2944 | break; |
| 2945 | default: |
| 2946 | host_type = target_type & TARGET_SOCK_TYPE_MASK; |
| 2947 | break; |
| 2948 | } |
| 2949 | if (target_type & TARGET_SOCK_CLOEXEC) { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2950 | #if defined(SOCK_CLOEXEC) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2951 | host_type |= SOCK_CLOEXEC; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2952 | #else |
| 2953 | return -TARGET_EINVAL; |
| 2954 | #endif |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2955 | } |
| 2956 | if (target_type & TARGET_SOCK_NONBLOCK) { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2957 | #if defined(SOCK_NONBLOCK) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2958 | host_type |= SOCK_NONBLOCK; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2959 | #elif !defined(O_NONBLOCK) |
| 2960 | return -TARGET_EINVAL; |
| 2961 | #endif |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2962 | } |
| 2963 | *type = host_type; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2964 | return 0; |
| 2965 | } |
| 2966 | |
| 2967 | /* Try to emulate socket type flags after socket creation. */ |
| 2968 | static int sock_flags_fixup(int fd, int target_type) |
| 2969 | { |
| 2970 | #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK) |
| 2971 | if (target_type & TARGET_SOCK_NONBLOCK) { |
| 2972 | int flags = fcntl(fd, F_GETFL); |
| 2973 | if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) { |
| 2974 | close(fd); |
| 2975 | return -TARGET_EINVAL; |
| 2976 | } |
| 2977 | } |
| 2978 | #endif |
| 2979 | return fd; |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2980 | } |
| 2981 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2982 | /* do_socket() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2983 | static abi_long do_socket(int domain, int type, int protocol) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2984 | { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2985 | int target_type = type; |
| 2986 | int ret; |
| 2987 | |
| 2988 | ret = target_to_host_sock_type(&type); |
| 2989 | if (ret) { |
| 2990 | return ret; |
| 2991 | } |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2992 | |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2993 | if (domain == PF_NETLINK && !( |
| 2994 | #ifdef CONFIG_RTNETLINK |
| 2995 | protocol == NETLINK_ROUTE || |
| 2996 | #endif |
| 2997 | protocol == NETLINK_KOBJECT_UEVENT || |
| 2998 | protocol == NETLINK_AUDIT)) { |
Josh Kunz | 71e2443 | 2020-07-06 17:10:36 -0700 | [diff] [blame] | 2999 | return -TARGET_EPROTONOSUPPORT; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3000 | } |
Laurent Vivier | ff626f2 | 2015-10-28 21:40:42 +0100 | [diff] [blame] | 3001 | |
| 3002 | if (domain == AF_PACKET || |
| 3003 | (domain == AF_INET && type == SOCK_PACKET)) { |
| 3004 | protocol = tswap16(protocol); |
| 3005 | } |
| 3006 | |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3007 | ret = get_errno(socket(domain, type, protocol)); |
| 3008 | if (ret >= 0) { |
| 3009 | ret = sock_flags_fixup(ret, target_type); |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 3010 | if (type == SOCK_PACKET) { |
| 3011 | /* Manage an obsolete case : |
| 3012 | * if socket type is SOCK_PACKET, bind by name |
| 3013 | */ |
| 3014 | fd_trans_register(ret, &target_packet_trans); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3015 | } else if (domain == PF_NETLINK) { |
| 3016 | switch (protocol) { |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 3017 | #ifdef CONFIG_RTNETLINK |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3018 | case NETLINK_ROUTE: |
| 3019 | fd_trans_register(ret, &target_netlink_route_trans); |
| 3020 | break; |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 3021 | #endif |
Laurent Vivier | b265620 | 2016-05-22 18:56:20 +0200 | [diff] [blame] | 3022 | case NETLINK_KOBJECT_UEVENT: |
| 3023 | /* nothing to do: messages are strings */ |
| 3024 | break; |
Laurent Vivier | 5ce9bb5 | 2016-05-22 18:56:21 +0200 | [diff] [blame] | 3025 | case NETLINK_AUDIT: |
| 3026 | fd_trans_register(ret, &target_netlink_audit_trans); |
| 3027 | break; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3028 | default: |
| 3029 | g_assert_not_reached(); |
| 3030 | } |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 3031 | } |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 3032 | } |
| 3033 | return ret; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3036 | /* do_bind() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3037 | static abi_long do_bind(int sockfd, abi_ulong target_addr, |
| 3038 | socklen_t addrlen) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3039 | { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3040 | void *addr; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3041 | abi_long ret; |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3042 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3043 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3044 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3045 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3046 | |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 3047 | addr = alloca(addrlen+1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3048 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3049 | ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3050 | if (ret) |
| 3051 | return ret; |
| 3052 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3053 | return get_errno(bind(sockfd, addr, addrlen)); |
| 3054 | } |
| 3055 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3056 | /* do_connect() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3057 | static abi_long do_connect(int sockfd, abi_ulong target_addr, |
| 3058 | socklen_t addrlen) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3059 | { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3060 | void *addr; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3061 | abi_long ret; |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3062 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3063 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3064 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3065 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3066 | |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3067 | addr = alloca(addrlen+1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3068 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3069 | ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3070 | if (ret) |
| 3071 | return ret; |
| 3072 | |
Peter Maydell | 2a3c761 | 2016-06-06 19:58:03 +0100 | [diff] [blame] | 3073 | return get_errno(safe_connect(sockfd, addr, addrlen)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3076 | /* do_sendrecvmsg_locked() Must return target values and target errnos. */ |
| 3077 | static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, |
| 3078 | int flags, int send) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3079 | { |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3080 | abi_long ret, len; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3081 | struct msghdr msg; |
Peter Maydell | dab32b3 | 2016-07-15 14:57:26 +0100 | [diff] [blame] | 3082 | abi_ulong count; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3083 | struct iovec *vec; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3084 | abi_ulong target_vec; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3085 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3086 | if (msgp->msg_name) { |
| 3087 | msg.msg_namelen = tswap32(msgp->msg_namelen); |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3088 | msg.msg_name = alloca(msg.msg_namelen+1); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3089 | ret = target_to_host_sockaddr(fd, msg.msg_name, |
| 3090 | tswapal(msgp->msg_name), |
| 3091 | msg.msg_namelen); |
Peter Maydell | 26a6fc9 | 2016-07-15 14:57:28 +0100 | [diff] [blame] | 3092 | if (ret == -TARGET_EFAULT) { |
| 3093 | /* For connected sockets msg_name and msg_namelen must |
| 3094 | * be ignored, so returning EFAULT immediately is wrong. |
| 3095 | * Instead, pass a bad msg_name to the host kernel, and |
| 3096 | * let it decide whether to return EFAULT or not. |
| 3097 | */ |
| 3098 | msg.msg_name = (void *)-1; |
| 3099 | } else if (ret) { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3100 | goto out2; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3101 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3102 | } else { |
| 3103 | msg.msg_name = NULL; |
| 3104 | msg.msg_namelen = 0; |
| 3105 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3106 | msg.msg_controllen = 2 * tswapal(msgp->msg_controllen); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3107 | msg.msg_control = alloca(msg.msg_controllen); |
Jonas Schievink | 1d3d1b2 | 2018-07-12 00:12:44 +0200 | [diff] [blame] | 3108 | memset(msg.msg_control, 0, msg.msg_controllen); |
| 3109 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3110 | msg.msg_flags = tswap32(msgp->msg_flags); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3111 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3112 | count = tswapal(msgp->msg_iovlen); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3113 | target_vec = tswapal(msgp->msg_iov); |
Peter Maydell | 97b0797 | 2016-07-15 14:57:27 +0100 | [diff] [blame] | 3114 | |
| 3115 | if (count > IOV_MAX) { |
| 3116 | /* sendrcvmsg returns a different errno for this condition than |
| 3117 | * readv/writev, so we must catch it here before lock_iovec() does. |
| 3118 | */ |
| 3119 | ret = -TARGET_EMSGSIZE; |
| 3120 | goto out2; |
| 3121 | } |
| 3122 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3123 | vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, |
| 3124 | target_vec, count, send); |
| 3125 | if (vec == NULL) { |
| 3126 | ret = -host_to_target_errno(errno); |
| 3127 | goto out2; |
| 3128 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3129 | msg.msg_iovlen = count; |
| 3130 | msg.msg_iov = vec; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3131 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3132 | if (send) { |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3133 | if (fd_trans_target_to_host_data(fd)) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3134 | void *host_msg; |
| 3135 | |
| 3136 | host_msg = g_malloc(msg.msg_iov->iov_len); |
| 3137 | memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len); |
| 3138 | ret = fd_trans_target_to_host_data(fd)(host_msg, |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3139 | msg.msg_iov->iov_len); |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3140 | if (ret >= 0) { |
| 3141 | msg.msg_iov->iov_base = host_msg; |
| 3142 | ret = get_errno(safe_sendmsg(fd, &msg, flags)); |
| 3143 | } |
| 3144 | g_free(host_msg); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3145 | } else { |
| 3146 | ret = target_to_host_cmsg(&msg, msgp); |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3147 | if (ret == 0) { |
| 3148 | ret = get_errno(safe_sendmsg(fd, &msg, flags)); |
| 3149 | } |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3150 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3151 | } else { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3152 | ret = get_errno(safe_recvmsg(fd, &msg, flags)); |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3153 | if (!is_error(ret)) { |
| 3154 | len = ret; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3155 | if (fd_trans_host_to_target_data(fd)) { |
| 3156 | 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] | 3157 | MIN(msg.msg_iov->iov_len, len)); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3158 | } else { |
| 3159 | ret = host_to_target_cmsg(msgp, &msg); |
| 3160 | } |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3161 | if (!is_error(ret)) { |
| 3162 | msgp->msg_namelen = tswap32(msg.msg_namelen); |
Andreas Schwab | 24894f3 | 2019-02-12 17:34:35 +0100 | [diff] [blame] | 3163 | msgp->msg_flags = tswap32(msg.msg_flags); |
Peter Maydell | 26a6fc9 | 2016-07-15 14:57:28 +0100 | [diff] [blame] | 3164 | if (msg.msg_name != NULL && msg.msg_name != (void *)-1) { |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3165 | ret = host_to_target_sockaddr(tswapal(msgp->msg_name), |
| 3166 | msg.msg_name, msg.msg_namelen); |
| 3167 | if (ret) { |
| 3168 | goto out; |
| 3169 | } |
| 3170 | } |
| 3171 | |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3172 | ret = len; |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3173 | } |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3174 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3175 | } |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3176 | |
| 3177 | out: |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3178 | unlock_iovec(vec, target_vec, count, !send); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3179 | out2: |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3180 | return ret; |
| 3181 | } |
| 3182 | |
| 3183 | static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg, |
| 3184 | int flags, int send) |
| 3185 | { |
| 3186 | abi_long ret; |
| 3187 | struct target_msghdr *msgp; |
| 3188 | |
| 3189 | if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE, |
| 3190 | msgp, |
| 3191 | target_msg, |
| 3192 | send ? 1 : 0)) { |
| 3193 | return -TARGET_EFAULT; |
| 3194 | } |
| 3195 | ret = do_sendrecvmsg_locked(fd, msgp, flags, send); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3196 | unlock_user_struct(msgp, target_msg, send ? 0 : 1); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3197 | return ret; |
| 3198 | } |
| 3199 | |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3200 | /* We don't rely on the C library to have sendmmsg/recvmmsg support, |
| 3201 | * so it might not have this *mmsg-specific flag either. |
| 3202 | */ |
| 3203 | #ifndef MSG_WAITFORONE |
| 3204 | #define MSG_WAITFORONE 0x10000 |
| 3205 | #endif |
| 3206 | |
| 3207 | static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec, |
| 3208 | unsigned int vlen, unsigned int flags, |
| 3209 | int send) |
| 3210 | { |
| 3211 | struct target_mmsghdr *mmsgp; |
| 3212 | abi_long ret = 0; |
| 3213 | int i; |
| 3214 | |
| 3215 | if (vlen > UIO_MAXIOV) { |
| 3216 | vlen = UIO_MAXIOV; |
| 3217 | } |
| 3218 | |
| 3219 | mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1); |
| 3220 | if (!mmsgp) { |
| 3221 | return -TARGET_EFAULT; |
| 3222 | } |
| 3223 | |
| 3224 | for (i = 0; i < vlen; i++) { |
| 3225 | ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send); |
| 3226 | if (is_error(ret)) { |
| 3227 | break; |
| 3228 | } |
| 3229 | mmsgp[i].msg_len = tswap32(ret); |
| 3230 | /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ |
| 3231 | if (flags & MSG_WAITFORONE) { |
| 3232 | flags |= MSG_DONTWAIT; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i); |
| 3237 | |
| 3238 | /* Return number of datagrams sent if we sent any at all; |
| 3239 | * otherwise return the error. |
| 3240 | */ |
| 3241 | if (i) { |
| 3242 | return i; |
| 3243 | } |
| 3244 | return ret; |
| 3245 | } |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3246 | |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3247 | /* do_accept4() Must return target values and target errnos. */ |
| 3248 | static abi_long do_accept4(int fd, abi_ulong target_addr, |
| 3249 | abi_ulong target_addrlen_addr, int flags) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3250 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3251 | socklen_t addrlen, ret_addrlen; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3252 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3253 | abi_long ret; |
Petar Jovanovic | d25295d | 2014-03-31 17:41:23 +0200 | [diff] [blame] | 3254 | int host_flags; |
| 3255 | |
| 3256 | host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3257 | |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3258 | if (target_addr == 0) { |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 3259 | return get_errno(safe_accept4(fd, NULL, NULL, host_flags)); |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3260 | } |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3261 | |
| 3262 | /* linux returns EINVAL if addrlen pointer is invalid */ |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3263 | if (get_user_u32(addrlen, target_addrlen_addr)) |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3264 | return -TARGET_EINVAL; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3265 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3266 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3267 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3268 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3269 | |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3270 | if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) |
| 3271 | return -TARGET_EINVAL; |
| 3272 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3273 | addr = alloca(addrlen); |
| 3274 | |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3275 | ret_addrlen = addrlen; |
| 3276 | ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3277 | if (!is_error(ret)) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3278 | host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); |
| 3279 | if (put_user_u32(ret_addrlen, target_addrlen_addr)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3280 | ret = -TARGET_EFAULT; |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3281 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3282 | } |
| 3283 | return ret; |
| 3284 | } |
| 3285 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3286 | /* do_getpeername() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3287 | static abi_long do_getpeername(int fd, abi_ulong target_addr, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3288 | abi_ulong target_addrlen_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3289 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3290 | socklen_t addrlen, ret_addrlen; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3291 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3292 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3293 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3294 | if (get_user_u32(addrlen, target_addrlen_addr)) |
| 3295 | return -TARGET_EFAULT; |
| 3296 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3297 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3298 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3299 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3300 | |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3301 | if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) |
| 3302 | return -TARGET_EFAULT; |
| 3303 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3304 | addr = alloca(addrlen); |
| 3305 | |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3306 | ret_addrlen = addrlen; |
| 3307 | ret = get_errno(getpeername(fd, addr, &ret_addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3308 | if (!is_error(ret)) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3309 | host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); |
| 3310 | if (put_user_u32(ret_addrlen, target_addrlen_addr)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3311 | ret = -TARGET_EFAULT; |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3312 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3313 | } |
| 3314 | return ret; |
| 3315 | } |
| 3316 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3317 | /* do_getsockname() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3318 | static abi_long do_getsockname(int fd, abi_ulong target_addr, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3319 | abi_ulong target_addrlen_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3320 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3321 | socklen_t addrlen, ret_addrlen; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3322 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3323 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3324 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3325 | if (get_user_u32(addrlen, target_addrlen_addr)) |
| 3326 | return -TARGET_EFAULT; |
| 3327 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3328 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3329 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3330 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3331 | |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3332 | if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) |
| 3333 | return -TARGET_EFAULT; |
| 3334 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3335 | addr = alloca(addrlen); |
| 3336 | |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3337 | ret_addrlen = addrlen; |
| 3338 | ret = get_errno(getsockname(fd, addr, &ret_addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3339 | if (!is_error(ret)) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3340 | host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); |
| 3341 | if (put_user_u32(ret_addrlen, target_addrlen_addr)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3342 | ret = -TARGET_EFAULT; |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3343 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3344 | } |
| 3345 | return ret; |
| 3346 | } |
| 3347 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3348 | /* do_socketpair() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3349 | static abi_long do_socketpair(int domain, int type, int protocol, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3350 | abi_ulong target_tab_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3351 | { |
| 3352 | int tab[2]; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3353 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3354 | |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3355 | target_to_host_sock_type(&type); |
| 3356 | |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3357 | ret = get_errno(socketpair(domain, type, protocol, tab)); |
| 3358 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3359 | if (put_user_s32(tab[0], target_tab_addr) |
| 3360 | || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0]))) |
| 3361 | ret = -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3362 | } |
| 3363 | return ret; |
| 3364 | } |
| 3365 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3366 | /* do_sendto() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3367 | static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags, |
| 3368 | abi_ulong target_addr, socklen_t addrlen) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3369 | { |
| 3370 | void *addr; |
| 3371 | void *host_msg; |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3372 | void *copy_msg = NULL; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3373 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3374 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3375 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3376 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3377 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3378 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3379 | host_msg = lock_user(VERIFY_READ, msg, len, 1); |
| 3380 | if (!host_msg) |
| 3381 | return -TARGET_EFAULT; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3382 | if (fd_trans_target_to_host_data(fd)) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3383 | copy_msg = host_msg; |
| 3384 | host_msg = g_malloc(len); |
| 3385 | memcpy(host_msg, copy_msg, len); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3386 | ret = fd_trans_target_to_host_data(fd)(host_msg, len); |
| 3387 | if (ret < 0) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3388 | goto fail; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3389 | } |
| 3390 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3391 | if (target_addr) { |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3392 | addr = alloca(addrlen+1); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3393 | ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3394 | if (ret) { |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3395 | goto fail; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3396 | } |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3397 | ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3398 | } else { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3399 | ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3400 | } |
Laurent Vivier | 7d61d89 | 2016-06-21 19:51:14 +0200 | [diff] [blame] | 3401 | fail: |
| 3402 | if (copy_msg) { |
| 3403 | g_free(host_msg); |
| 3404 | host_msg = copy_msg; |
| 3405 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3406 | unlock_user(host_msg, msg, 0); |
| 3407 | return ret; |
| 3408 | } |
| 3409 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3410 | /* do_recvfrom() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3411 | static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags, |
| 3412 | abi_ulong target_addr, |
| 3413 | abi_ulong target_addrlen) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3414 | { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3415 | socklen_t addrlen, ret_addrlen; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3416 | void *addr; |
| 3417 | void *host_msg; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3418 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3419 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3420 | host_msg = lock_user(VERIFY_WRITE, msg, len, 0); |
| 3421 | if (!host_msg) |
| 3422 | return -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3423 | if (target_addr) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3424 | if (get_user_u32(addrlen, target_addrlen)) { |
| 3425 | ret = -TARGET_EFAULT; |
| 3426 | goto fail; |
| 3427 | } |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3428 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3429 | ret = -TARGET_EINVAL; |
| 3430 | goto fail; |
| 3431 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3432 | addr = alloca(addrlen); |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3433 | ret_addrlen = addrlen; |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3434 | ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3435 | addr, &ret_addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3436 | } else { |
| 3437 | addr = NULL; /* To keep compiler quiet. */ |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3438 | addrlen = 0; /* To keep compiler quiet. */ |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3439 | ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3440 | } |
| 3441 | if (!is_error(ret)) { |
Laurent Vivier | c35e1f9 | 2016-06-21 19:51:15 +0200 | [diff] [blame] | 3442 | if (fd_trans_host_to_target_data(fd)) { |
Laurent Vivier | 2a03d3e | 2018-08-20 19:15:54 +0200 | [diff] [blame] | 3443 | abi_long trans; |
| 3444 | trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len)); |
| 3445 | if (is_error(trans)) { |
| 3446 | ret = trans; |
| 3447 | goto fail; |
| 3448 | } |
Laurent Vivier | c35e1f9 | 2016-06-21 19:51:15 +0200 | [diff] [blame] | 3449 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3450 | if (target_addr) { |
Andreas Schwab | cd81336 | 2019-02-14 12:43:40 +0100 | [diff] [blame] | 3451 | host_to_target_sockaddr(target_addr, addr, |
| 3452 | MIN(addrlen, ret_addrlen)); |
| 3453 | if (put_user_u32(ret_addrlen, target_addrlen)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3454 | ret = -TARGET_EFAULT; |
| 3455 | goto fail; |
| 3456 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3457 | } |
| 3458 | unlock_user(host_msg, msg, len); |
| 3459 | } else { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3460 | fail: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3461 | unlock_user(host_msg, msg, 0); |
| 3462 | } |
| 3463 | return ret; |
| 3464 | } |
| 3465 | |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 3466 | #ifdef TARGET_NR_socketcall |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3467 | /* do_socketcall() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3468 | static abi_long do_socketcall(int num, abi_ulong vptr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3469 | { |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3470 | static const unsigned nargs[] = { /* number of arguments per operation */ |
| 3471 | [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */ |
| 3472 | [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */ |
| 3473 | [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */ |
| 3474 | [TARGET_SYS_LISTEN] = 2, /* fd, backlog */ |
| 3475 | [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */ |
| 3476 | [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */ |
| 3477 | [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */ |
| 3478 | [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */ |
| 3479 | [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */ |
| 3480 | [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */ |
| 3481 | [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */ |
| 3482 | [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */ |
| 3483 | [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */ |
| 3484 | [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */ |
| 3485 | [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */ |
| 3486 | [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */ |
| 3487 | [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */ |
| 3488 | [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */ |
| 3489 | [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */ |
| 3490 | [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3491 | }; |
| 3492 | abi_long a[6]; /* max 6 args */ |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3493 | unsigned i; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3494 | |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3495 | /* check the range of the first argument num */ |
| 3496 | /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */ |
| 3497 | if (num < 1 || num > TARGET_SYS_SENDMMSG) { |
| 3498 | return -TARGET_EINVAL; |
| 3499 | } |
| 3500 | /* ensure we have space for args */ |
| 3501 | if (nargs[num] > ARRAY_SIZE(a)) { |
| 3502 | return -TARGET_EINVAL; |
| 3503 | } |
| 3504 | /* collect the arguments in a[] according to nargs[] */ |
| 3505 | for (i = 0; i < nargs[num]; ++i) { |
| 3506 | if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) { |
| 3507 | return -TARGET_EFAULT; |
André Hentschel | b9d36eb | 2014-01-06 20:18:52 +0100 | [diff] [blame] | 3508 | } |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3509 | } |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3510 | /* now when we have the args, invoke the appropriate underlying function */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3511 | switch (num) { |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3512 | case TARGET_SYS_SOCKET: /* domain, type, protocol */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3513 | return do_socket(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3514 | case TARGET_SYS_BIND: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3515 | return do_bind(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3516 | case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3517 | return do_connect(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3518 | case TARGET_SYS_LISTEN: /* sockfd, backlog */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3519 | return get_errno(listen(a[0], a[1])); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3520 | case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3521 | return do_accept4(a[0], a[1], a[2], 0); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3522 | case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3523 | return do_getsockname(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3524 | case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3525 | return do_getpeername(a[0], a[1], a[2]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3526 | case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3527 | return do_socketpair(a[0], a[1], a[2], a[3]); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3528 | case TARGET_SYS_SEND: /* sockfd, msg, len, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3529 | 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] | 3530 | case TARGET_SYS_RECV: /* sockfd, msg, len, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3531 | 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] | 3532 | case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3533 | 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] | 3534 | case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3535 | 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] | 3536 | case TARGET_SYS_SHUTDOWN: /* sockfd, how */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3537 | return get_errno(shutdown(a[0], a[1])); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3538 | case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3539 | 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] | 3540 | case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3541 | 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] | 3542 | case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */ |
| 3543 | return do_sendrecvmsg(a[0], a[1], a[2], 1); |
| 3544 | case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */ |
| 3545 | return do_sendrecvmsg(a[0], a[1], a[2], 0); |
| 3546 | case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */ |
| 3547 | return do_accept4(a[0], a[1], a[2], a[3]); |
| 3548 | case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */ |
| 3549 | return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0); |
| 3550 | case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */ |
| 3551 | return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3552 | default: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 3553 | qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num); |
Aleksandar Markovic | ff71a45 | 2016-09-22 18:56:57 +0200 | [diff] [blame] | 3554 | return -TARGET_EINVAL; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3555 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3556 | } |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 3557 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3558 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 3559 | #define N_SHM_REGIONS 32 |
| 3560 | |
| 3561 | static struct shm_region { |
Peter Maydell | b6e1787 | 2016-02-09 15:57:11 +0000 | [diff] [blame] | 3562 | abi_ulong start; |
| 3563 | abi_ulong size; |
| 3564 | bool in_use; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 3565 | } shm_regions[N_SHM_REGIONS]; |
| 3566 | |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3567 | #ifndef TARGET_SEMID64_DS |
| 3568 | /* asm-generic version of this struct */ |
| 3569 | struct target_semid64_ds |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3570 | { |
| 3571 | struct target_ipc_perm sem_perm; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3572 | abi_ulong sem_otime; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3573 | #if TARGET_ABI_BITS == 32 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3574 | abi_ulong __unused1; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3575 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3576 | abi_ulong sem_ctime; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3577 | #if TARGET_ABI_BITS == 32 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3578 | abi_ulong __unused2; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3579 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3580 | abi_ulong sem_nsems; |
| 3581 | abi_ulong __unused3; |
| 3582 | abi_ulong __unused4; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3583 | }; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3584 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3585 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3586 | static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip, |
| 3587 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3588 | { |
| 3589 | struct target_ipc_perm *target_ip; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3590 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3591 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3592 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3593 | return -TARGET_EFAULT; |
Michael S. Tsirkin | e8bbe36 | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 3594 | target_ip = &(target_sd->sem_perm); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3595 | host_ip->__key = tswap32(target_ip->__key); |
| 3596 | host_ip->uid = tswap32(target_ip->uid); |
| 3597 | host_ip->gid = tswap32(target_ip->gid); |
| 3598 | host_ip->cuid = tswap32(target_ip->cuid); |
| 3599 | host_ip->cgid = tswap32(target_ip->cgid); |
| 3600 | #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) |
| 3601 | host_ip->mode = tswap32(target_ip->mode); |
| 3602 | #else |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3603 | host_ip->mode = tswap16(target_ip->mode); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3604 | #endif |
| 3605 | #if defined(TARGET_PPC) |
| 3606 | host_ip->__seq = tswap32(target_ip->__seq); |
| 3607 | #else |
| 3608 | host_ip->__seq = tswap16(target_ip->__seq); |
| 3609 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3610 | unlock_user_struct(target_sd, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3611 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3614 | static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr, |
| 3615 | struct ipc_perm *host_ip) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3616 | { |
| 3617 | struct target_ipc_perm *target_ip; |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3618 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3619 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3620 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3621 | return -TARGET_EFAULT; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3622 | target_ip = &(target_sd->sem_perm); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3623 | target_ip->__key = tswap32(host_ip->__key); |
| 3624 | target_ip->uid = tswap32(host_ip->uid); |
| 3625 | target_ip->gid = tswap32(host_ip->gid); |
| 3626 | target_ip->cuid = tswap32(host_ip->cuid); |
| 3627 | target_ip->cgid = tswap32(host_ip->cgid); |
| 3628 | #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) |
| 3629 | target_ip->mode = tswap32(host_ip->mode); |
| 3630 | #else |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3631 | target_ip->mode = tswap16(host_ip->mode); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3632 | #endif |
| 3633 | #if defined(TARGET_PPC) |
| 3634 | target_ip->__seq = tswap32(host_ip->__seq); |
| 3635 | #else |
| 3636 | target_ip->__seq = tswap16(host_ip->__seq); |
| 3637 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3638 | unlock_user_struct(target_sd, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3639 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3642 | static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd, |
| 3643 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3644 | { |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3645 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3646 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3647 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3648 | return -TARGET_EFAULT; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3649 | if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr)) |
| 3650 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3651 | host_sd->sem_nsems = tswapal(target_sd->sem_nsems); |
| 3652 | host_sd->sem_otime = tswapal(target_sd->sem_otime); |
| 3653 | host_sd->sem_ctime = tswapal(target_sd->sem_ctime); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3654 | unlock_user_struct(target_sd, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3655 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3658 | static inline abi_long host_to_target_semid_ds(abi_ulong target_addr, |
| 3659 | struct semid_ds *host_sd) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3660 | { |
Peter Maydell | 005eb2a | 2016-07-15 16:50:47 +0100 | [diff] [blame] | 3661 | struct target_semid64_ds *target_sd; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3662 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3663 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3664 | return -TARGET_EFAULT; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3665 | 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] | 3666 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3667 | target_sd->sem_nsems = tswapal(host_sd->sem_nsems); |
| 3668 | target_sd->sem_otime = tswapal(host_sd->sem_otime); |
| 3669 | target_sd->sem_ctime = tswapal(host_sd->sem_ctime); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3670 | unlock_user_struct(target_sd, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3671 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3674 | struct target_seminfo { |
| 3675 | int semmap; |
| 3676 | int semmni; |
| 3677 | int semmns; |
| 3678 | int semmnu; |
| 3679 | int semmsl; |
| 3680 | int semopm; |
| 3681 | int semume; |
| 3682 | int semusz; |
| 3683 | int semvmx; |
| 3684 | int semaem; |
| 3685 | }; |
| 3686 | |
| 3687 | static inline abi_long host_to_target_seminfo(abi_ulong target_addr, |
| 3688 | struct seminfo *host_seminfo) |
| 3689 | { |
| 3690 | struct target_seminfo *target_seminfo; |
| 3691 | if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0)) |
| 3692 | return -TARGET_EFAULT; |
| 3693 | __put_user(host_seminfo->semmap, &target_seminfo->semmap); |
| 3694 | __put_user(host_seminfo->semmni, &target_seminfo->semmni); |
| 3695 | __put_user(host_seminfo->semmns, &target_seminfo->semmns); |
| 3696 | __put_user(host_seminfo->semmnu, &target_seminfo->semmnu); |
| 3697 | __put_user(host_seminfo->semmsl, &target_seminfo->semmsl); |
| 3698 | __put_user(host_seminfo->semopm, &target_seminfo->semopm); |
| 3699 | __put_user(host_seminfo->semume, &target_seminfo->semume); |
| 3700 | __put_user(host_seminfo->semusz, &target_seminfo->semusz); |
| 3701 | __put_user(host_seminfo->semvmx, &target_seminfo->semvmx); |
| 3702 | __put_user(host_seminfo->semaem, &target_seminfo->semaem); |
| 3703 | unlock_user_struct(target_seminfo, target_addr, 1); |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3707 | union semun { |
| 3708 | int val; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3709 | struct semid_ds *buf; |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3710 | unsigned short *array; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3711 | struct seminfo *__buf; |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3712 | }; |
| 3713 | |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3714 | union target_semun { |
| 3715 | int val; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3716 | abi_ulong buf; |
| 3717 | abi_ulong array; |
| 3718 | abi_ulong __buf; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3719 | }; |
| 3720 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3721 | static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array, |
| 3722 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3723 | { |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3724 | int nsems; |
| 3725 | unsigned short *array; |
| 3726 | union semun semun; |
| 3727 | struct semid_ds semid_ds; |
| 3728 | int i, ret; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3729 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3730 | semun.buf = &semid_ds; |
| 3731 | |
| 3732 | ret = semctl(semid, 0, IPC_STAT, semun); |
| 3733 | if (ret == -1) |
| 3734 | return get_errno(ret); |
| 3735 | |
| 3736 | nsems = semid_ds.sem_nsems; |
| 3737 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3738 | *host_array = g_try_new(unsigned short, nsems); |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3739 | if (!*host_array) { |
| 3740 | return -TARGET_ENOMEM; |
| 3741 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3742 | array = lock_user(VERIFY_READ, target_addr, |
| 3743 | nsems*sizeof(unsigned short), 1); |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3744 | if (!array) { |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3745 | g_free(*host_array); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3746 | return -TARGET_EFAULT; |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3747 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3748 | |
| 3749 | for(i=0; i<nsems; i++) { |
| 3750 | __get_user((*host_array)[i], &array[i]); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3751 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3752 | unlock_user(array, target_addr, 0); |
| 3753 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3754 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3757 | static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr, |
| 3758 | unsigned short **host_array) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3759 | { |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3760 | int nsems; |
| 3761 | unsigned short *array; |
| 3762 | union semun semun; |
| 3763 | struct semid_ds semid_ds; |
| 3764 | int i, ret; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3765 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3766 | semun.buf = &semid_ds; |
| 3767 | |
| 3768 | ret = semctl(semid, 0, IPC_STAT, semun); |
| 3769 | if (ret == -1) |
| 3770 | return get_errno(ret); |
| 3771 | |
| 3772 | nsems = semid_ds.sem_nsems; |
| 3773 | |
| 3774 | array = lock_user(VERIFY_WRITE, target_addr, |
| 3775 | nsems*sizeof(unsigned short), 0); |
| 3776 | if (!array) |
| 3777 | return -TARGET_EFAULT; |
| 3778 | |
| 3779 | for(i=0; i<nsems; i++) { |
| 3780 | __put_user((*host_array)[i], &array[i]); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3781 | } |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3782 | g_free(*host_array); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3783 | unlock_user(array, target_addr, 1); |
| 3784 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3785 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3788 | static inline abi_long do_semctl(int semid, int semnum, int cmd, |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 3789 | abi_ulong target_arg) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3790 | { |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 3791 | union target_semun target_su = { .buf = target_arg }; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3792 | union semun arg; |
| 3793 | struct semid_ds dsarg; |
vibi sreenivasan | 7b8118e | 2009-06-19 13:34:39 +0530 | [diff] [blame] | 3794 | unsigned short *array = NULL; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3795 | struct seminfo seminfo; |
| 3796 | abi_long ret = -TARGET_EINVAL; |
| 3797 | abi_long err; |
| 3798 | cmd &= 0xff; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3799 | |
| 3800 | switch( cmd ) { |
| 3801 | case GETVAL: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3802 | case SETVAL: |
Tom Musta | 5464bae | 2014-08-12 13:53:34 -0500 | [diff] [blame] | 3803 | /* In 64 bit cross-endian situations, we will erroneously pick up |
| 3804 | * the wrong half of the union for the "val" element. To rectify |
| 3805 | * this, the entire 8-byte structure is byteswapped, followed by |
| 3806 | * a swap of the 4 byte val field. In other cases, the data is |
| 3807 | * already in proper host byte order. */ |
| 3808 | if (sizeof(target_su.val) != (sizeof(target_su.buf))) { |
| 3809 | target_su.buf = tswapal(target_su.buf); |
| 3810 | arg.val = tswap32(target_su.val); |
| 3811 | } else { |
| 3812 | arg.val = target_su.val; |
| 3813 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3814 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3815 | break; |
| 3816 | case GETALL: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3817 | case SETALL: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3818 | err = target_to_host_semarray(semid, &array, target_su.array); |
| 3819 | if (err) |
| 3820 | return err; |
| 3821 | arg.array = array; |
| 3822 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3823 | err = host_to_target_semarray(semid, target_su.array, &array); |
| 3824 | if (err) |
| 3825 | return err; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3826 | break; |
| 3827 | case IPC_STAT: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3828 | case IPC_SET: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3829 | case SEM_STAT: |
| 3830 | err = target_to_host_semid_ds(&dsarg, target_su.buf); |
| 3831 | if (err) |
| 3832 | return err; |
| 3833 | arg.buf = &dsarg; |
| 3834 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3835 | err = host_to_target_semid_ds(target_su.buf, &dsarg); |
| 3836 | if (err) |
| 3837 | return err; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3838 | break; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3839 | case IPC_INFO: |
| 3840 | case SEM_INFO: |
| 3841 | arg.__buf = &seminfo; |
| 3842 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3843 | err = host_to_target_seminfo(target_su.__buf, &seminfo); |
| 3844 | if (err) |
| 3845 | return err; |
| 3846 | break; |
| 3847 | case IPC_RMID: |
| 3848 | case GETPID: |
| 3849 | case GETNCNT: |
| 3850 | case GETZCNT: |
| 3851 | ret = get_errno(semctl(semid, semnum, cmd, NULL)); |
| 3852 | break; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | return ret; |
| 3856 | } |
| 3857 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3858 | struct target_sembuf { |
| 3859 | unsigned short sem_num; |
| 3860 | short sem_op; |
| 3861 | short sem_flg; |
| 3862 | }; |
| 3863 | |
| 3864 | static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, |
| 3865 | abi_ulong target_addr, |
| 3866 | unsigned nsops) |
| 3867 | { |
| 3868 | struct target_sembuf *target_sembuf; |
| 3869 | int i; |
| 3870 | |
| 3871 | target_sembuf = lock_user(VERIFY_READ, target_addr, |
| 3872 | nsops*sizeof(struct target_sembuf), 1); |
| 3873 | if (!target_sembuf) |
| 3874 | return -TARGET_EFAULT; |
| 3875 | |
| 3876 | for(i=0; i<nsops; i++) { |
| 3877 | __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num); |
| 3878 | __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op); |
| 3879 | __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg); |
| 3880 | } |
| 3881 | |
| 3882 | unlock_user(target_sembuf, target_addr, 0); |
| 3883 | |
| 3884 | return 0; |
| 3885 | } |
| 3886 | |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 3887 | #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ |
| 3888 | defined(TARGET_NR_semtimedop) |
| 3889 | |
| 3890 | /* |
| 3891 | * This macro is required to handle the s390 variants, which passes the |
| 3892 | * arguments in a different order than default. |
| 3893 | */ |
| 3894 | #ifdef __s390x__ |
| 3895 | #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \ |
| 3896 | (__nsops), (__timeout), (__sops) |
| 3897 | #else |
| 3898 | #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \ |
| 3899 | (__nsops), 0, (__sops), (__timeout) |
| 3900 | #endif |
| 3901 | |
| 3902 | static inline abi_long do_semtimedop(int semid, |
| 3903 | abi_long ptr, |
| 3904 | unsigned nsops, |
| 3905 | abi_long timeout) |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3906 | { |
| 3907 | struct sembuf sops[nsops]; |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 3908 | struct timespec ts, *pts = NULL; |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 3909 | abi_long ret; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3910 | |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 3911 | if (timeout) { |
| 3912 | pts = &ts; |
| 3913 | if (target_to_host_timespec(pts, timeout)) { |
| 3914 | return -TARGET_EFAULT; |
| 3915 | } |
| 3916 | } |
| 3917 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3918 | if (target_to_host_sembuf(sops, ptr, nsops)) |
| 3919 | return -TARGET_EFAULT; |
| 3920 | |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 3921 | ret = -TARGET_ENOSYS; |
| 3922 | #ifdef __NR_semtimedop |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 3923 | ret = get_errno(safe_semtimedop(semid, sops, nsops, pts)); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 3924 | #endif |
| 3925 | #ifdef __NR_ipc |
| 3926 | if (ret == -TARGET_ENOSYS) { |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 3927 | ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, |
| 3928 | SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts))); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 3929 | } |
| 3930 | #endif |
| 3931 | return ret; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3932 | } |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 3933 | #endif |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3934 | |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3935 | struct target_msqid_ds |
| 3936 | { |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3937 | struct target_ipc_perm msg_perm; |
| 3938 | abi_ulong msg_stime; |
| 3939 | #if TARGET_ABI_BITS == 32 |
| 3940 | abi_ulong __unused1; |
| 3941 | #endif |
| 3942 | abi_ulong msg_rtime; |
| 3943 | #if TARGET_ABI_BITS == 32 |
| 3944 | abi_ulong __unused2; |
| 3945 | #endif |
| 3946 | abi_ulong msg_ctime; |
| 3947 | #if TARGET_ABI_BITS == 32 |
| 3948 | abi_ulong __unused3; |
| 3949 | #endif |
| 3950 | abi_ulong __msg_cbytes; |
| 3951 | abi_ulong msg_qnum; |
| 3952 | abi_ulong msg_qbytes; |
| 3953 | abi_ulong msg_lspid; |
| 3954 | abi_ulong msg_lrpid; |
| 3955 | abi_ulong __unused4; |
| 3956 | abi_ulong __unused5; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3957 | }; |
| 3958 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3959 | static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md, |
| 3960 | abi_ulong target_addr) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3961 | { |
| 3962 | struct target_msqid_ds *target_md; |
| 3963 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3964 | if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1)) |
| 3965 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3966 | if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr)) |
| 3967 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3968 | host_md->msg_stime = tswapal(target_md->msg_stime); |
| 3969 | host_md->msg_rtime = tswapal(target_md->msg_rtime); |
| 3970 | host_md->msg_ctime = tswapal(target_md->msg_ctime); |
| 3971 | host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes); |
| 3972 | host_md->msg_qnum = tswapal(target_md->msg_qnum); |
| 3973 | host_md->msg_qbytes = tswapal(target_md->msg_qbytes); |
| 3974 | host_md->msg_lspid = tswapal(target_md->msg_lspid); |
| 3975 | host_md->msg_lrpid = tswapal(target_md->msg_lrpid); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3976 | unlock_user_struct(target_md, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3977 | return 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3980 | static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr, |
| 3981 | struct msqid_ds *host_md) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3982 | { |
| 3983 | struct target_msqid_ds *target_md; |
| 3984 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3985 | if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) |
| 3986 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3987 | if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm))) |
| 3988 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3989 | target_md->msg_stime = tswapal(host_md->msg_stime); |
| 3990 | target_md->msg_rtime = tswapal(host_md->msg_rtime); |
| 3991 | target_md->msg_ctime = tswapal(host_md->msg_ctime); |
| 3992 | target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes); |
| 3993 | target_md->msg_qnum = tswapal(host_md->msg_qnum); |
| 3994 | target_md->msg_qbytes = tswapal(host_md->msg_qbytes); |
| 3995 | target_md->msg_lspid = tswapal(host_md->msg_lspid); |
| 3996 | target_md->msg_lrpid = tswapal(host_md->msg_lrpid); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3997 | unlock_user_struct(target_md, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3998 | return 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4001 | struct target_msginfo { |
| 4002 | int msgpool; |
| 4003 | int msgmap; |
| 4004 | int msgmax; |
| 4005 | int msgmnb; |
| 4006 | int msgmni; |
| 4007 | int msgssz; |
| 4008 | int msgtql; |
| 4009 | unsigned short int msgseg; |
| 4010 | }; |
| 4011 | |
| 4012 | static inline abi_long host_to_target_msginfo(abi_ulong target_addr, |
| 4013 | struct msginfo *host_msginfo) |
| 4014 | { |
| 4015 | struct target_msginfo *target_msginfo; |
| 4016 | if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0)) |
| 4017 | return -TARGET_EFAULT; |
| 4018 | __put_user(host_msginfo->msgpool, &target_msginfo->msgpool); |
| 4019 | __put_user(host_msginfo->msgmap, &target_msginfo->msgmap); |
| 4020 | __put_user(host_msginfo->msgmax, &target_msginfo->msgmax); |
| 4021 | __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb); |
| 4022 | __put_user(host_msginfo->msgmni, &target_msginfo->msgmni); |
| 4023 | __put_user(host_msginfo->msgssz, &target_msginfo->msgssz); |
| 4024 | __put_user(host_msginfo->msgtql, &target_msginfo->msgtql); |
| 4025 | __put_user(host_msginfo->msgseg, &target_msginfo->msgseg); |
| 4026 | unlock_user_struct(target_msginfo, target_addr, 1); |
aurel32 | 00b229a | 2008-10-24 13:12:52 +0000 | [diff] [blame] | 4027 | return 0; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4031 | { |
| 4032 | struct msqid_ds dsarg; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4033 | struct msginfo msginfo; |
| 4034 | abi_long ret = -TARGET_EINVAL; |
| 4035 | |
| 4036 | cmd &= 0xff; |
| 4037 | |
| 4038 | switch (cmd) { |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4039 | case IPC_STAT: |
| 4040 | case IPC_SET: |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4041 | case MSG_STAT: |
| 4042 | if (target_to_host_msqid_ds(&dsarg,ptr)) |
| 4043 | return -TARGET_EFAULT; |
| 4044 | ret = get_errno(msgctl(msgid, cmd, &dsarg)); |
| 4045 | if (host_to_target_msqid_ds(ptr,&dsarg)) |
| 4046 | return -TARGET_EFAULT; |
| 4047 | break; |
| 4048 | case IPC_RMID: |
| 4049 | ret = get_errno(msgctl(msgid, cmd, NULL)); |
| 4050 | break; |
| 4051 | case IPC_INFO: |
| 4052 | case MSG_INFO: |
| 4053 | ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo)); |
| 4054 | if (host_to_target_msginfo(ptr, &msginfo)) |
| 4055 | return -TARGET_EFAULT; |
| 4056 | break; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4057 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4058 | |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4059 | return ret; |
| 4060 | } |
| 4061 | |
| 4062 | struct target_msgbuf { |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4063 | abi_long mtype; |
| 4064 | char mtext[1]; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4065 | }; |
| 4066 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4067 | static inline abi_long do_msgsnd(int msqid, abi_long msgp, |
Tom Musta | edcc5f9 | 2014-08-12 13:53:37 -0500 | [diff] [blame] | 4068 | ssize_t msgsz, int msgflg) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4069 | { |
| 4070 | struct target_msgbuf *target_mb; |
| 4071 | struct msgbuf *host_mb; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4072 | abi_long ret = 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4073 | |
Tom Musta | edcc5f9 | 2014-08-12 13:53:37 -0500 | [diff] [blame] | 4074 | if (msgsz < 0) { |
| 4075 | return -TARGET_EINVAL; |
| 4076 | } |
| 4077 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4078 | if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0)) |
| 4079 | return -TARGET_EFAULT; |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4080 | host_mb = g_try_malloc(msgsz + sizeof(long)); |
zhanghailiang | 29e03fc | 2014-08-14 15:29:18 +0800 | [diff] [blame] | 4081 | if (!host_mb) { |
| 4082 | unlock_user_struct(target_mb, msgp, 0); |
| 4083 | return -TARGET_ENOMEM; |
| 4084 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 4085 | host_mb->mtype = (abi_long) tswapal(target_mb->mtype); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4086 | memcpy(host_mb->mtext, target_mb->mtext, msgsz); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4087 | ret = -TARGET_ENOSYS; |
| 4088 | #ifdef __NR_msgsnd |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 4089 | ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg)); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4090 | #endif |
| 4091 | #ifdef __NR_ipc |
| 4092 | if (ret == -TARGET_ENOSYS) { |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 4093 | #ifdef __s390x__ |
| 4094 | ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, |
| 4095 | host_mb)); |
| 4096 | #else |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4097 | ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, |
| 4098 | host_mb, 0)); |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 4099 | #endif |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4100 | } |
| 4101 | #endif |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4102 | g_free(host_mb); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4103 | unlock_user_struct(target_mb, msgp, 0); |
| 4104 | |
| 4105 | return ret; |
| 4106 | } |
| 4107 | |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 4108 | #ifdef __NR_ipc |
| 4109 | #if defined(__sparc__) |
| 4110 | /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */ |
| 4111 | #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp |
| 4112 | #elif defined(__s390x__) |
| 4113 | /* The s390 sys_ipc variant has only five parameters. */ |
| 4114 | #define MSGRCV_ARGS(__msgp, __msgtyp) \ |
| 4115 | ((long int[]){(long int)__msgp, __msgtyp}) |
| 4116 | #else |
| 4117 | #define MSGRCV_ARGS(__msgp, __msgtyp) \ |
| 4118 | ((long int[]){(long int)__msgp, __msgtyp}), 0 |
| 4119 | #endif |
| 4120 | #endif |
| 4121 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4122 | static inline abi_long do_msgrcv(int msqid, abi_long msgp, |
Peter Maydell | 99874f6 | 2016-05-20 19:00:56 +0100 | [diff] [blame] | 4123 | ssize_t msgsz, abi_long msgtyp, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4124 | int msgflg) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4125 | { |
| 4126 | struct target_msgbuf *target_mb; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4127 | char *target_mtext; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4128 | struct msgbuf *host_mb; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4129 | abi_long ret = 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4130 | |
Peter Maydell | 99874f6 | 2016-05-20 19:00:56 +0100 | [diff] [blame] | 4131 | if (msgsz < 0) { |
| 4132 | return -TARGET_EINVAL; |
| 4133 | } |
| 4134 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4135 | if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0)) |
| 4136 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4137 | |
Peter Maydell | 415d847 | 2016-05-20 19:00:57 +0100 | [diff] [blame] | 4138 | host_mb = g_try_malloc(msgsz + sizeof(long)); |
| 4139 | if (!host_mb) { |
| 4140 | ret = -TARGET_ENOMEM; |
| 4141 | goto end; |
| 4142 | } |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4143 | ret = -TARGET_ENOSYS; |
| 4144 | #ifdef __NR_msgrcv |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 4145 | ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg)); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4146 | #endif |
| 4147 | #ifdef __NR_ipc |
| 4148 | if (ret == -TARGET_ENOSYS) { |
| 4149 | ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz, |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 4150 | msgflg, MSGRCV_ARGS(host_mb, msgtyp))); |
Laurent Vivier | 524fa34 | 2019-05-29 10:48:04 +0200 | [diff] [blame] | 4151 | } |
| 4152 | #endif |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4153 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4154 | if (ret > 0) { |
| 4155 | abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong); |
| 4156 | target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0); |
| 4157 | if (!target_mtext) { |
| 4158 | ret = -TARGET_EFAULT; |
| 4159 | goto end; |
| 4160 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4161 | memcpy(target_mb->mtext, host_mb->mtext, ret); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4162 | unlock_user(target_mtext, target_mtext_addr, ret); |
| 4163 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4164 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 4165 | target_mb->mtype = tswapal(host_mb->mtype); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4166 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4167 | end: |
| 4168 | if (target_mb) |
| 4169 | unlock_user_struct(target_mb, msgp, 1); |
Jim Meyering | 0d07fe4 | 2012-08-22 13:55:53 +0200 | [diff] [blame] | 4170 | g_free(host_mb); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4171 | return ret; |
| 4172 | } |
| 4173 | |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4174 | static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd, |
| 4175 | abi_ulong target_addr) |
| 4176 | { |
| 4177 | struct target_shmid_ds *target_sd; |
| 4178 | |
| 4179 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 4180 | return -TARGET_EFAULT; |
| 4181 | if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr)) |
| 4182 | return -TARGET_EFAULT; |
| 4183 | __get_user(host_sd->shm_segsz, &target_sd->shm_segsz); |
| 4184 | __get_user(host_sd->shm_atime, &target_sd->shm_atime); |
| 4185 | __get_user(host_sd->shm_dtime, &target_sd->shm_dtime); |
| 4186 | __get_user(host_sd->shm_ctime, &target_sd->shm_ctime); |
| 4187 | __get_user(host_sd->shm_cpid, &target_sd->shm_cpid); |
| 4188 | __get_user(host_sd->shm_lpid, &target_sd->shm_lpid); |
| 4189 | __get_user(host_sd->shm_nattch, &target_sd->shm_nattch); |
| 4190 | unlock_user_struct(target_sd, target_addr, 0); |
| 4191 | return 0; |
| 4192 | } |
| 4193 | |
| 4194 | static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr, |
| 4195 | struct shmid_ds *host_sd) |
| 4196 | { |
| 4197 | struct target_shmid_ds *target_sd; |
| 4198 | |
| 4199 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 4200 | return -TARGET_EFAULT; |
| 4201 | if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm))) |
| 4202 | return -TARGET_EFAULT; |
| 4203 | __put_user(host_sd->shm_segsz, &target_sd->shm_segsz); |
| 4204 | __put_user(host_sd->shm_atime, &target_sd->shm_atime); |
| 4205 | __put_user(host_sd->shm_dtime, &target_sd->shm_dtime); |
| 4206 | __put_user(host_sd->shm_ctime, &target_sd->shm_ctime); |
| 4207 | __put_user(host_sd->shm_cpid, &target_sd->shm_cpid); |
| 4208 | __put_user(host_sd->shm_lpid, &target_sd->shm_lpid); |
| 4209 | __put_user(host_sd->shm_nattch, &target_sd->shm_nattch); |
| 4210 | unlock_user_struct(target_sd, target_addr, 1); |
| 4211 | return 0; |
| 4212 | } |
| 4213 | |
| 4214 | struct target_shminfo { |
| 4215 | abi_ulong shmmax; |
| 4216 | abi_ulong shmmin; |
| 4217 | abi_ulong shmmni; |
| 4218 | abi_ulong shmseg; |
| 4219 | abi_ulong shmall; |
| 4220 | }; |
| 4221 | |
| 4222 | static inline abi_long host_to_target_shminfo(abi_ulong target_addr, |
| 4223 | struct shminfo *host_shminfo) |
| 4224 | { |
| 4225 | struct target_shminfo *target_shminfo; |
| 4226 | if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0)) |
| 4227 | return -TARGET_EFAULT; |
| 4228 | __put_user(host_shminfo->shmmax, &target_shminfo->shmmax); |
| 4229 | __put_user(host_shminfo->shmmin, &target_shminfo->shmmin); |
| 4230 | __put_user(host_shminfo->shmmni, &target_shminfo->shmmni); |
| 4231 | __put_user(host_shminfo->shmseg, &target_shminfo->shmseg); |
| 4232 | __put_user(host_shminfo->shmall, &target_shminfo->shmall); |
| 4233 | unlock_user_struct(target_shminfo, target_addr, 1); |
| 4234 | return 0; |
| 4235 | } |
| 4236 | |
| 4237 | struct target_shm_info { |
| 4238 | int used_ids; |
| 4239 | abi_ulong shm_tot; |
| 4240 | abi_ulong shm_rss; |
| 4241 | abi_ulong shm_swp; |
| 4242 | abi_ulong swap_attempts; |
| 4243 | abi_ulong swap_successes; |
| 4244 | }; |
| 4245 | |
| 4246 | static inline abi_long host_to_target_shm_info(abi_ulong target_addr, |
| 4247 | struct shm_info *host_shm_info) |
| 4248 | { |
| 4249 | struct target_shm_info *target_shm_info; |
| 4250 | if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0)) |
| 4251 | return -TARGET_EFAULT; |
| 4252 | __put_user(host_shm_info->used_ids, &target_shm_info->used_ids); |
| 4253 | __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot); |
| 4254 | __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss); |
| 4255 | __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp); |
| 4256 | __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts); |
| 4257 | __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes); |
| 4258 | unlock_user_struct(target_shm_info, target_addr, 1); |
| 4259 | return 0; |
| 4260 | } |
| 4261 | |
| 4262 | static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf) |
| 4263 | { |
| 4264 | struct shmid_ds dsarg; |
| 4265 | struct shminfo shminfo; |
| 4266 | struct shm_info shm_info; |
| 4267 | abi_long ret = -TARGET_EINVAL; |
| 4268 | |
| 4269 | cmd &= 0xff; |
| 4270 | |
| 4271 | switch(cmd) { |
| 4272 | case IPC_STAT: |
| 4273 | case IPC_SET: |
| 4274 | case SHM_STAT: |
| 4275 | if (target_to_host_shmid_ds(&dsarg, buf)) |
| 4276 | return -TARGET_EFAULT; |
| 4277 | ret = get_errno(shmctl(shmid, cmd, &dsarg)); |
| 4278 | if (host_to_target_shmid_ds(buf, &dsarg)) |
| 4279 | return -TARGET_EFAULT; |
| 4280 | break; |
| 4281 | case IPC_INFO: |
| 4282 | ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo)); |
| 4283 | if (host_to_target_shminfo(buf, &shminfo)) |
| 4284 | return -TARGET_EFAULT; |
| 4285 | break; |
| 4286 | case SHM_INFO: |
| 4287 | ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info)); |
| 4288 | if (host_to_target_shm_info(buf, &shm_info)) |
| 4289 | return -TARGET_EFAULT; |
| 4290 | break; |
| 4291 | case IPC_RMID: |
| 4292 | case SHM_LOCK: |
| 4293 | case SHM_UNLOCK: |
| 4294 | ret = get_errno(shmctl(shmid, cmd, NULL)); |
| 4295 | break; |
| 4296 | } |
| 4297 | |
| 4298 | return ret; |
| 4299 | } |
| 4300 | |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4301 | #ifndef TARGET_FORCE_SHMLBA |
| 4302 | /* For most architectures, SHMLBA is the same as the page size; |
| 4303 | * some architectures have larger values, in which case they should |
| 4304 | * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function. |
| 4305 | * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA |
| 4306 | * and defining its own value for SHMLBA. |
| 4307 | * |
| 4308 | * The kernel also permits SHMLBA to be set by the architecture to a |
| 4309 | * value larger than the page size without setting __ARCH_FORCE_SHMLBA; |
| 4310 | * this means that addresses are rounded to the large size if |
| 4311 | * SHM_RND is set but addresses not aligned to that size are not rejected |
| 4312 | * as long as they are at least page-aligned. Since the only architecture |
| 4313 | * which uses this is ia64 this code doesn't provide for that oddity. |
| 4314 | */ |
| 4315 | static inline abi_ulong target_shmlba(CPUArchState *cpu_env) |
| 4316 | { |
| 4317 | return TARGET_PAGE_SIZE; |
| 4318 | } |
| 4319 | #endif |
| 4320 | |
| 4321 | static inline abi_ulong do_shmat(CPUArchState *cpu_env, |
| 4322 | int shmid, abi_ulong shmaddr, int shmflg) |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4323 | { |
| 4324 | abi_long raddr; |
| 4325 | void *host_raddr; |
| 4326 | struct shmid_ds shm_info; |
| 4327 | int i,ret; |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4328 | abi_ulong shmlba; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4329 | |
| 4330 | /* find out the length of the shared memory segment */ |
| 4331 | ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); |
| 4332 | if (is_error(ret)) { |
| 4333 | /* can't get length, bail out */ |
| 4334 | return ret; |
| 4335 | } |
| 4336 | |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4337 | shmlba = target_shmlba(cpu_env); |
| 4338 | |
| 4339 | if (shmaddr & (shmlba - 1)) { |
| 4340 | if (shmflg & SHM_RND) { |
| 4341 | shmaddr &= ~(shmlba - 1); |
| 4342 | } else { |
| 4343 | return -TARGET_EINVAL; |
| 4344 | } |
| 4345 | } |
Max Filippov | ebf9a36 | 2018-03-07 13:50:10 -0800 | [diff] [blame] | 4346 | if (!guest_range_valid(shmaddr, shm_info.shm_segsz)) { |
| 4347 | return -TARGET_EINVAL; |
| 4348 | } |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4349 | |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4350 | mmap_lock(); |
| 4351 | |
| 4352 | if (shmaddr) |
| 4353 | host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg); |
| 4354 | else { |
| 4355 | abi_ulong mmap_start; |
| 4356 | |
Richard Henderson | 30ab9ef | 2019-05-19 13:19:52 -0700 | [diff] [blame] | 4357 | /* In order to use the host shmat, we need to honor host SHMLBA. */ |
| 4358 | mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba)); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4359 | |
| 4360 | if (mmap_start == -1) { |
| 4361 | errno = ENOMEM; |
| 4362 | host_raddr = (void *)-1; |
| 4363 | } else |
| 4364 | host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP); |
| 4365 | } |
| 4366 | |
| 4367 | if (host_raddr == (void *)-1) { |
| 4368 | mmap_unlock(); |
| 4369 | return get_errno((long)host_raddr); |
| 4370 | } |
| 4371 | raddr=h2g((unsigned long)host_raddr); |
| 4372 | |
| 4373 | page_set_flags(raddr, raddr + shm_info.shm_segsz, |
| 4374 | PAGE_VALID | PAGE_READ | |
| 4375 | ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE)); |
| 4376 | |
| 4377 | for (i = 0; i < N_SHM_REGIONS; i++) { |
Peter Maydell | b6e1787 | 2016-02-09 15:57:11 +0000 | [diff] [blame] | 4378 | if (!shm_regions[i].in_use) { |
| 4379 | shm_regions[i].in_use = true; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4380 | shm_regions[i].start = raddr; |
| 4381 | shm_regions[i].size = shm_info.shm_segsz; |
| 4382 | break; |
| 4383 | } |
| 4384 | } |
| 4385 | |
| 4386 | mmap_unlock(); |
| 4387 | return raddr; |
| 4388 | |
| 4389 | } |
| 4390 | |
| 4391 | static inline abi_long do_shmdt(abi_ulong shmaddr) |
| 4392 | { |
| 4393 | int i; |
Max Filippov | 3c5f6a5 | 2018-02-28 14:16:04 -0800 | [diff] [blame] | 4394 | abi_long rv; |
| 4395 | |
| 4396 | mmap_lock(); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4397 | |
| 4398 | for (i = 0; i < N_SHM_REGIONS; ++i) { |
Peter Maydell | b6e1787 | 2016-02-09 15:57:11 +0000 | [diff] [blame] | 4399 | if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { |
| 4400 | shm_regions[i].in_use = false; |
takasi-y@ops.dti.ne.jp | e00ac24 | 2010-04-11 02:09:57 +0900 | [diff] [blame] | 4401 | page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4402 | break; |
| 4403 | } |
| 4404 | } |
Max Filippov | 3c5f6a5 | 2018-02-28 14:16:04 -0800 | [diff] [blame] | 4405 | rv = get_errno(shmdt(g2h(shmaddr))); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4406 | |
Max Filippov | 3c5f6a5 | 2018-02-28 14:16:04 -0800 | [diff] [blame] | 4407 | mmap_unlock(); |
| 4408 | |
| 4409 | return rv; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4410 | } |
| 4411 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4412 | #ifdef TARGET_NR_ipc |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4413 | /* ??? This only works with linear mappings. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4414 | /* do_ipc() must return target values and target errnos. */ |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4415 | static abi_long do_ipc(CPUArchState *cpu_env, |
| 4416 | unsigned int call, abi_long first, |
Tom Musta | 37ed095 | 2014-08-12 13:53:35 -0500 | [diff] [blame] | 4417 | abi_long second, abi_long third, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4418 | abi_long ptr, abi_long fifth) |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4419 | { |
| 4420 | int version; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4421 | abi_long ret = 0; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4422 | |
| 4423 | version = call >> 16; |
| 4424 | call &= 0xffff; |
| 4425 | |
| 4426 | switch (call) { |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4427 | case IPCOP_semop: |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 4428 | ret = do_semtimedop(first, ptr, second, 0); |
| 4429 | break; |
| 4430 | case IPCOP_semtimedop: |
| 4431 | /* |
| 4432 | * The s390 sys_ipc variant has only five parameters instead of six |
| 4433 | * (as for default variant) and the only difference is the handling of |
| 4434 | * SEMTIMEDOP where on s390 the third parameter is used as a pointer |
| 4435 | * to a struct timespec where the generic variant uses fifth parameter. |
| 4436 | */ |
| 4437 | #if defined(TARGET_S390X) |
| 4438 | ret = do_semtimedop(first, ptr, second, third); |
| 4439 | #else |
| 4440 | ret = do_semtimedop(first, ptr, second, fifth); |
| 4441 | #endif |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4442 | break; |
| 4443 | |
| 4444 | case IPCOP_semget: |
| 4445 | ret = get_errno(semget(first, second, third)); |
| 4446 | break; |
| 4447 | |
Tom Musta | 5d2fa8e | 2014-08-12 13:53:33 -0500 | [diff] [blame] | 4448 | case IPCOP_semctl: { |
| 4449 | /* The semun argument to semctl is passed by value, so dereference the |
| 4450 | * ptr argument. */ |
| 4451 | abi_ulong atptr; |
Tom Musta | 37ed095 | 2014-08-12 13:53:35 -0500 | [diff] [blame] | 4452 | get_user_ual(atptr, ptr); |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 4453 | ret = do_semctl(first, second, third, atptr); |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4454 | break; |
Tom Musta | 5d2fa8e | 2014-08-12 13:53:33 -0500 | [diff] [blame] | 4455 | } |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4456 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4457 | case IPCOP_msgget: |
| 4458 | ret = get_errno(msgget(first, second)); |
| 4459 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4460 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4461 | case IPCOP_msgsnd: |
| 4462 | ret = do_msgsnd(first, ptr, second, third); |
| 4463 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4464 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4465 | case IPCOP_msgctl: |
| 4466 | ret = do_msgctl(first, second, ptr); |
| 4467 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4468 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4469 | case IPCOP_msgrcv: |
| 4470 | switch (version) { |
| 4471 | case 0: |
| 4472 | { |
| 4473 | struct target_ipc_kludge { |
| 4474 | abi_long msgp; |
| 4475 | abi_long msgtyp; |
| 4476 | } *tmp; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4477 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4478 | if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) { |
| 4479 | ret = -TARGET_EFAULT; |
| 4480 | break; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4481 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4482 | |
Laurent Vivier | 79dd77d | 2012-12-20 11:00:11 +0000 | [diff] [blame] | 4483 | ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4484 | |
| 4485 | unlock_user_struct(tmp, ptr, 0); |
| 4486 | break; |
| 4487 | } |
| 4488 | default: |
| 4489 | ret = do_msgrcv(first, ptr, second, fifth, third); |
| 4490 | } |
| 4491 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4492 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4493 | case IPCOP_shmat: |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4494 | switch (version) { |
| 4495 | default: |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4496 | { |
| 4497 | abi_ulong raddr; |
Peter Maydell | ee8e761 | 2016-07-11 16:48:11 +0100 | [diff] [blame] | 4498 | raddr = do_shmat(cpu_env, first, ptr, second); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4499 | if (is_error(raddr)) |
| 4500 | return get_errno(raddr); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 4501 | if (put_user_ual(raddr, third)) |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4502 | return -TARGET_EFAULT; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4503 | break; |
| 4504 | } |
| 4505 | case 1: |
| 4506 | ret = -TARGET_EINVAL; |
| 4507 | break; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4508 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4509 | break; |
| 4510 | case IPCOP_shmdt: |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4511 | ret = do_shmdt(ptr); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4512 | break; |
| 4513 | |
| 4514 | case IPCOP_shmget: |
| 4515 | /* IPC_* flag values are the same on all linux platforms */ |
| 4516 | ret = get_errno(shmget(first, second, third)); |
| 4517 | break; |
| 4518 | |
| 4519 | /* IPC_* and SHM_* command values are the same on all linux platforms */ |
| 4520 | case IPCOP_shmctl: |
Petar Jovanovic | a292678 | 2013-10-30 14:46:32 +0100 | [diff] [blame] | 4521 | ret = do_shmctl(first, second, ptr); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4522 | break; |
| 4523 | default: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 4524 | qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n", |
| 4525 | call, version); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4526 | ret = -TARGET_ENOSYS; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4527 | break; |
| 4528 | } |
| 4529 | return ret; |
| 4530 | } |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4531 | #endif |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4532 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4533 | /* kernel structure types definitions */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4534 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4535 | #define STRUCT(name, ...) STRUCT_ ## name, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4536 | #define STRUCT_SPECIAL(name) STRUCT_ ## name, |
| 4537 | enum { |
| 4538 | #include "syscall_types.h" |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 4539 | STRUCT_MAX |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4540 | }; |
| 4541 | #undef STRUCT |
| 4542 | #undef STRUCT_SPECIAL |
| 4543 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4544 | #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL }; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4545 | #define STRUCT_SPECIAL(name) |
| 4546 | #include "syscall_types.h" |
| 4547 | #undef STRUCT |
| 4548 | #undef STRUCT_SPECIAL |
| 4549 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4550 | #define MAX_STRUCT_SIZE 4096 |
| 4551 | |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 4552 | #ifdef CONFIG_FIEMAP |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4553 | /* So fiemap access checks don't overflow on 32 bit systems. |
| 4554 | * This is very slightly smaller than the limit imposed by |
| 4555 | * the underlying kernel. |
| 4556 | */ |
| 4557 | #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \ |
| 4558 | / sizeof(struct fiemap_extent)) |
| 4559 | |
| 4560 | 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] | 4561 | int fd, int cmd, abi_long arg) |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4562 | { |
| 4563 | /* The parameter for this ioctl is a struct fiemap followed |
| 4564 | * by an array of struct fiemap_extent whose size is set |
| 4565 | * in fiemap->fm_extent_count. The array is filled in by the |
| 4566 | * ioctl. |
| 4567 | */ |
| 4568 | int target_size_in, target_size_out; |
| 4569 | struct fiemap *fm; |
| 4570 | const argtype *arg_type = ie->arg_type; |
| 4571 | const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) }; |
| 4572 | void *argptr, *p; |
| 4573 | abi_long ret; |
| 4574 | int i, extent_size = thunk_type_size(extent_arg_type, 0); |
| 4575 | uint32_t outbufsz; |
| 4576 | int free_fm = 0; |
| 4577 | |
| 4578 | assert(arg_type[0] == TYPE_PTR); |
| 4579 | assert(ie->access == IOC_RW); |
| 4580 | arg_type++; |
| 4581 | target_size_in = thunk_type_size(arg_type, 0); |
| 4582 | argptr = lock_user(VERIFY_READ, arg, target_size_in, 1); |
| 4583 | if (!argptr) { |
| 4584 | return -TARGET_EFAULT; |
| 4585 | } |
| 4586 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4587 | unlock_user(argptr, arg, 0); |
| 4588 | fm = (struct fiemap *)buf_temp; |
| 4589 | if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) { |
| 4590 | return -TARGET_EINVAL; |
| 4591 | } |
| 4592 | |
| 4593 | outbufsz = sizeof (*fm) + |
| 4594 | (sizeof(struct fiemap_extent) * fm->fm_extent_count); |
| 4595 | |
| 4596 | if (outbufsz > MAX_STRUCT_SIZE) { |
| 4597 | /* We can't fit all the extents into the fixed size buffer. |
| 4598 | * Allocate one that is large enough and use it instead. |
| 4599 | */ |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4600 | fm = g_try_malloc(outbufsz); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4601 | if (!fm) { |
| 4602 | return -TARGET_ENOMEM; |
| 4603 | } |
| 4604 | memcpy(fm, buf_temp, sizeof(struct fiemap)); |
| 4605 | free_fm = 1; |
| 4606 | } |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4607 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm)); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4608 | if (!is_error(ret)) { |
| 4609 | target_size_out = target_size_in; |
| 4610 | /* An extent_count of 0 means we were only counting the extents |
| 4611 | * so there are no structs to copy |
| 4612 | */ |
| 4613 | if (fm->fm_extent_count != 0) { |
| 4614 | target_size_out += fm->fm_mapped_extents * extent_size; |
| 4615 | } |
| 4616 | argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0); |
| 4617 | if (!argptr) { |
| 4618 | ret = -TARGET_EFAULT; |
| 4619 | } else { |
| 4620 | /* Convert the struct fiemap */ |
| 4621 | thunk_convert(argptr, fm, arg_type, THUNK_TARGET); |
| 4622 | if (fm->fm_extent_count != 0) { |
| 4623 | p = argptr + target_size_in; |
| 4624 | /* ...and then all the struct fiemap_extents */ |
| 4625 | for (i = 0; i < fm->fm_mapped_extents; i++) { |
| 4626 | thunk_convert(p, &fm->fm_extents[i], extent_arg_type, |
| 4627 | THUNK_TARGET); |
| 4628 | p += extent_size; |
| 4629 | } |
| 4630 | } |
| 4631 | unlock_user(argptr, arg, target_size_out); |
| 4632 | } |
| 4633 | } |
| 4634 | if (free_fm) { |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4635 | g_free(fm); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4636 | } |
| 4637 | return ret; |
| 4638 | } |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 4639 | #endif |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4640 | |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4641 | 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] | 4642 | int fd, int cmd, abi_long arg) |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4643 | { |
| 4644 | const argtype *arg_type = ie->arg_type; |
| 4645 | int target_size; |
| 4646 | void *argptr; |
| 4647 | int ret; |
| 4648 | struct ifconf *host_ifconf; |
| 4649 | uint32_t outbufsz; |
| 4650 | const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) }; |
| 4651 | int target_ifreq_size; |
| 4652 | int nb_ifreq; |
| 4653 | int free_buf = 0; |
| 4654 | int i; |
| 4655 | int target_ifc_len; |
| 4656 | abi_long target_ifc_buf; |
| 4657 | int host_ifc_len; |
| 4658 | char *host_ifc_buf; |
| 4659 | |
| 4660 | assert(arg_type[0] == TYPE_PTR); |
| 4661 | assert(ie->access == IOC_RW); |
| 4662 | |
| 4663 | arg_type++; |
| 4664 | target_size = thunk_type_size(arg_type, 0); |
| 4665 | |
| 4666 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4667 | if (!argptr) |
| 4668 | return -TARGET_EFAULT; |
| 4669 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4670 | unlock_user(argptr, arg, 0); |
| 4671 | |
| 4672 | host_ifconf = (struct ifconf *)(unsigned long)buf_temp; |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4673 | target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf; |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4674 | target_ifreq_size = thunk_type_size(ifreq_arg_type, 0); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4675 | |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4676 | if (target_ifc_buf != 0) { |
| 4677 | target_ifc_len = host_ifconf->ifc_len; |
| 4678 | nb_ifreq = target_ifc_len / target_ifreq_size; |
| 4679 | host_ifc_len = nb_ifreq * sizeof(struct ifreq); |
| 4680 | |
| 4681 | outbufsz = sizeof(*host_ifconf) + host_ifc_len; |
| 4682 | if (outbufsz > MAX_STRUCT_SIZE) { |
| 4683 | /* |
| 4684 | * We can't fit all the extents into the fixed size buffer. |
| 4685 | * Allocate one that is large enough and use it instead. |
| 4686 | */ |
| 4687 | host_ifconf = malloc(outbufsz); |
| 4688 | if (!host_ifconf) { |
| 4689 | return -TARGET_ENOMEM; |
| 4690 | } |
| 4691 | memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf)); |
| 4692 | free_buf = 1; |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4693 | } |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4694 | host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4695 | |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4696 | host_ifconf->ifc_len = host_ifc_len; |
| 4697 | } else { |
| 4698 | host_ifc_buf = NULL; |
| 4699 | } |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4700 | host_ifconf->ifc_buf = host_ifc_buf; |
| 4701 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4702 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf)); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4703 | if (!is_error(ret)) { |
| 4704 | /* convert host ifc_len to target ifc_len */ |
| 4705 | |
| 4706 | nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq); |
| 4707 | target_ifc_len = nb_ifreq * target_ifreq_size; |
| 4708 | host_ifconf->ifc_len = target_ifc_len; |
| 4709 | |
| 4710 | /* restore target ifc_buf */ |
| 4711 | |
| 4712 | host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf; |
| 4713 | |
| 4714 | /* copy struct ifconf to target user */ |
| 4715 | |
| 4716 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4717 | if (!argptr) |
| 4718 | return -TARGET_EFAULT; |
| 4719 | thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET); |
| 4720 | unlock_user(argptr, arg, target_size); |
| 4721 | |
Kan Li | 22e4a267 | 2018-10-24 20:13:03 +0000 | [diff] [blame] | 4722 | if (target_ifc_buf != 0) { |
| 4723 | /* copy ifreq[] to target user */ |
| 4724 | argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0); |
| 4725 | for (i = 0; i < nb_ifreq ; i++) { |
| 4726 | thunk_convert(argptr + i * target_ifreq_size, |
| 4727 | host_ifc_buf + i * sizeof(struct ifreq), |
| 4728 | ifreq_arg_type, THUNK_TARGET); |
| 4729 | } |
| 4730 | unlock_user(argptr, target_ifc_buf, target_ifc_len); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4731 | } |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | if (free_buf) { |
| 4735 | free(host_ifconf); |
| 4736 | } |
| 4737 | |
| 4738 | return ret; |
| 4739 | } |
| 4740 | |
Cortland Tölva | a133367 | 2018-10-08 09:35:21 -0700 | [diff] [blame] | 4741 | #if defined(CONFIG_USBFS) |
| 4742 | #if HOST_LONG_BITS > 64 |
| 4743 | #error USBDEVFS thunks do not support >64 bit hosts yet. |
| 4744 | #endif |
| 4745 | struct live_urb { |
| 4746 | uint64_t target_urb_adr; |
| 4747 | uint64_t target_buf_adr; |
| 4748 | char *target_buf_ptr; |
| 4749 | struct usbdevfs_urb host_urb; |
| 4750 | }; |
| 4751 | |
| 4752 | static GHashTable *usbdevfs_urb_hashtable(void) |
| 4753 | { |
| 4754 | static GHashTable *urb_hashtable; |
| 4755 | |
| 4756 | if (!urb_hashtable) { |
| 4757 | urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal); |
| 4758 | } |
| 4759 | return urb_hashtable; |
| 4760 | } |
| 4761 | |
| 4762 | static void urb_hashtable_insert(struct live_urb *urb) |
| 4763 | { |
| 4764 | GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); |
| 4765 | g_hash_table_insert(urb_hashtable, urb, urb); |
| 4766 | } |
| 4767 | |
| 4768 | static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr) |
| 4769 | { |
| 4770 | GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); |
| 4771 | return g_hash_table_lookup(urb_hashtable, &target_urb_adr); |
| 4772 | } |
| 4773 | |
| 4774 | static void urb_hashtable_remove(struct live_urb *urb) |
| 4775 | { |
| 4776 | GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); |
| 4777 | g_hash_table_remove(urb_hashtable, urb); |
| 4778 | } |
| 4779 | |
| 4780 | static abi_long |
| 4781 | do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 4782 | int fd, int cmd, abi_long arg) |
| 4783 | { |
| 4784 | const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) }; |
| 4785 | const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 }; |
| 4786 | struct live_urb *lurb; |
| 4787 | void *argptr; |
| 4788 | uint64_t hurb; |
| 4789 | int target_size; |
| 4790 | uintptr_t target_urb_adr; |
| 4791 | abi_long ret; |
| 4792 | |
| 4793 | target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET); |
| 4794 | |
| 4795 | memset(buf_temp, 0, sizeof(uint64_t)); |
| 4796 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
| 4797 | if (is_error(ret)) { |
| 4798 | return ret; |
| 4799 | } |
| 4800 | |
| 4801 | memcpy(&hurb, buf_temp, sizeof(uint64_t)); |
| 4802 | lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb)); |
| 4803 | if (!lurb->target_urb_adr) { |
| 4804 | return -TARGET_EFAULT; |
| 4805 | } |
| 4806 | urb_hashtable_remove(lurb); |
| 4807 | unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, |
| 4808 | lurb->host_urb.buffer_length); |
| 4809 | lurb->target_buf_ptr = NULL; |
| 4810 | |
| 4811 | /* restore the guest buffer pointer */ |
| 4812 | lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr; |
| 4813 | |
| 4814 | /* update the guest urb struct */ |
| 4815 | argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0); |
| 4816 | if (!argptr) { |
| 4817 | g_free(lurb); |
| 4818 | return -TARGET_EFAULT; |
| 4819 | } |
| 4820 | thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET); |
| 4821 | unlock_user(argptr, lurb->target_urb_adr, target_size); |
| 4822 | |
| 4823 | target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET); |
| 4824 | /* write back the urb handle */ |
| 4825 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4826 | if (!argptr) { |
| 4827 | g_free(lurb); |
| 4828 | return -TARGET_EFAULT; |
| 4829 | } |
| 4830 | |
| 4831 | /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */ |
| 4832 | target_urb_adr = lurb->target_urb_adr; |
| 4833 | thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET); |
| 4834 | unlock_user(argptr, arg, target_size); |
| 4835 | |
| 4836 | g_free(lurb); |
| 4837 | return ret; |
| 4838 | } |
| 4839 | |
| 4840 | static abi_long |
| 4841 | do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie, |
| 4842 | uint8_t *buf_temp __attribute__((unused)), |
| 4843 | int fd, int cmd, abi_long arg) |
| 4844 | { |
| 4845 | struct live_urb *lurb; |
| 4846 | |
| 4847 | /* map target address back to host URB with metadata. */ |
| 4848 | lurb = urb_hashtable_lookup(arg); |
| 4849 | if (!lurb) { |
| 4850 | return -TARGET_EFAULT; |
| 4851 | } |
| 4852 | return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); |
| 4853 | } |
| 4854 | |
| 4855 | static abi_long |
| 4856 | do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 4857 | int fd, int cmd, abi_long arg) |
| 4858 | { |
| 4859 | const argtype *arg_type = ie->arg_type; |
| 4860 | int target_size; |
| 4861 | abi_long ret; |
| 4862 | void *argptr; |
| 4863 | int rw_dir; |
| 4864 | struct live_urb *lurb; |
| 4865 | |
| 4866 | /* |
| 4867 | * each submitted URB needs to map to a unique ID for the |
| 4868 | * kernel, and that unique ID needs to be a pointer to |
| 4869 | * host memory. hence, we need to malloc for each URB. |
| 4870 | * isochronous transfers have a variable length struct. |
| 4871 | */ |
| 4872 | arg_type++; |
| 4873 | target_size = thunk_type_size(arg_type, THUNK_TARGET); |
| 4874 | |
| 4875 | /* construct host copy of urb and metadata */ |
| 4876 | lurb = g_try_malloc0(sizeof(struct live_urb)); |
| 4877 | if (!lurb) { |
| 4878 | return -TARGET_ENOMEM; |
| 4879 | } |
| 4880 | |
| 4881 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4882 | if (!argptr) { |
| 4883 | g_free(lurb); |
| 4884 | return -TARGET_EFAULT; |
| 4885 | } |
| 4886 | thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST); |
| 4887 | unlock_user(argptr, arg, 0); |
| 4888 | |
| 4889 | lurb->target_urb_adr = arg; |
| 4890 | lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer; |
| 4891 | |
| 4892 | /* buffer space used depends on endpoint type so lock the entire buffer */ |
| 4893 | /* control type urbs should check the buffer contents for true direction */ |
| 4894 | rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ; |
| 4895 | lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr, |
| 4896 | lurb->host_urb.buffer_length, 1); |
| 4897 | if (lurb->target_buf_ptr == NULL) { |
| 4898 | g_free(lurb); |
| 4899 | return -TARGET_EFAULT; |
| 4900 | } |
| 4901 | |
| 4902 | /* update buffer pointer in host copy */ |
| 4903 | lurb->host_urb.buffer = lurb->target_buf_ptr; |
| 4904 | |
| 4905 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); |
| 4906 | if (is_error(ret)) { |
| 4907 | unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0); |
| 4908 | g_free(lurb); |
| 4909 | } else { |
| 4910 | urb_hashtable_insert(lurb); |
| 4911 | } |
| 4912 | |
| 4913 | return ret; |
| 4914 | } |
| 4915 | #endif /* CONFIG_USBFS */ |
| 4916 | |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4917 | 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] | 4918 | int cmd, abi_long arg) |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4919 | { |
| 4920 | void *argptr; |
| 4921 | struct dm_ioctl *host_dm; |
| 4922 | abi_long guest_data; |
| 4923 | uint32_t guest_data_size; |
| 4924 | int target_size; |
| 4925 | const argtype *arg_type = ie->arg_type; |
| 4926 | abi_long ret; |
| 4927 | void *big_buf = NULL; |
| 4928 | char *host_data; |
| 4929 | |
| 4930 | arg_type++; |
| 4931 | target_size = thunk_type_size(arg_type, 0); |
| 4932 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4933 | if (!argptr) { |
| 4934 | ret = -TARGET_EFAULT; |
| 4935 | goto out; |
| 4936 | } |
| 4937 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4938 | unlock_user(argptr, arg, 0); |
| 4939 | |
| 4940 | /* buf_temp is too small, so fetch things into a bigger buffer */ |
| 4941 | big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2); |
| 4942 | memcpy(big_buf, buf_temp, target_size); |
| 4943 | buf_temp = big_buf; |
| 4944 | host_dm = big_buf; |
| 4945 | |
| 4946 | guest_data = arg + host_dm->data_start; |
| 4947 | if ((guest_data - arg) < 0) { |
Peter Maydell | f9757b1 | 2016-07-12 13:02:14 +0100 | [diff] [blame] | 4948 | ret = -TARGET_EINVAL; |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4949 | goto out; |
| 4950 | } |
| 4951 | guest_data_size = host_dm->data_size - host_dm->data_start; |
| 4952 | host_data = (char*)host_dm + host_dm->data_start; |
| 4953 | |
| 4954 | argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1); |
Peter Maydell | 3211215 | 2016-07-12 13:02:13 +0100 | [diff] [blame] | 4955 | if (!argptr) { |
| 4956 | ret = -TARGET_EFAULT; |
| 4957 | goto out; |
| 4958 | } |
| 4959 | |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4960 | switch (ie->host_cmd) { |
| 4961 | case DM_REMOVE_ALL: |
| 4962 | case DM_LIST_DEVICES: |
| 4963 | case DM_DEV_CREATE: |
| 4964 | case DM_DEV_REMOVE: |
| 4965 | case DM_DEV_SUSPEND: |
| 4966 | case DM_DEV_STATUS: |
| 4967 | case DM_DEV_WAIT: |
| 4968 | case DM_TABLE_STATUS: |
| 4969 | case DM_TABLE_CLEAR: |
| 4970 | case DM_TABLE_DEPS: |
| 4971 | case DM_LIST_VERSIONS: |
| 4972 | /* no input data */ |
| 4973 | break; |
| 4974 | case DM_DEV_RENAME: |
| 4975 | case DM_DEV_SET_GEOMETRY: |
| 4976 | /* data contains only strings */ |
| 4977 | memcpy(host_data, argptr, guest_data_size); |
| 4978 | break; |
| 4979 | case DM_TARGET_MSG: |
| 4980 | memcpy(host_data, argptr, guest_data_size); |
| 4981 | *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr); |
| 4982 | break; |
| 4983 | case DM_TABLE_LOAD: |
| 4984 | { |
| 4985 | void *gspec = argptr; |
| 4986 | void *cur_data = host_data; |
| 4987 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; |
| 4988 | int spec_size = thunk_type_size(arg_type, 0); |
| 4989 | int i; |
| 4990 | |
| 4991 | for (i = 0; i < host_dm->target_count; i++) { |
| 4992 | struct dm_target_spec *spec = cur_data; |
| 4993 | uint32_t next; |
| 4994 | int slen; |
| 4995 | |
| 4996 | thunk_convert(spec, gspec, arg_type, THUNK_HOST); |
| 4997 | slen = strlen((char*)gspec + spec_size) + 1; |
| 4998 | next = spec->next; |
| 4999 | spec->next = sizeof(*spec) + slen; |
| 5000 | strcpy((char*)&spec[1], gspec + spec_size); |
| 5001 | gspec += next; |
| 5002 | cur_data += spec->next; |
| 5003 | } |
| 5004 | break; |
| 5005 | } |
| 5006 | default: |
| 5007 | ret = -TARGET_EINVAL; |
Chen Gang S | dec0473 | 2015-01-25 08:00:42 +0800 | [diff] [blame] | 5008 | unlock_user(argptr, guest_data, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5009 | goto out; |
| 5010 | } |
| 5011 | unlock_user(argptr, guest_data, 0); |
| 5012 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5013 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5014 | if (!is_error(ret)) { |
| 5015 | guest_data = arg + host_dm->data_start; |
| 5016 | guest_data_size = host_dm->data_size - host_dm->data_start; |
| 5017 | argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0); |
| 5018 | switch (ie->host_cmd) { |
| 5019 | case DM_REMOVE_ALL: |
| 5020 | case DM_DEV_CREATE: |
| 5021 | case DM_DEV_REMOVE: |
| 5022 | case DM_DEV_RENAME: |
| 5023 | case DM_DEV_SUSPEND: |
| 5024 | case DM_DEV_STATUS: |
| 5025 | case DM_TABLE_LOAD: |
| 5026 | case DM_TABLE_CLEAR: |
| 5027 | case DM_TARGET_MSG: |
| 5028 | case DM_DEV_SET_GEOMETRY: |
| 5029 | /* no return data */ |
| 5030 | break; |
| 5031 | case DM_LIST_DEVICES: |
| 5032 | { |
| 5033 | struct dm_name_list *nl = (void*)host_dm + host_dm->data_start; |
| 5034 | uint32_t remaining_data = guest_data_size; |
| 5035 | void *cur_data = argptr; |
| 5036 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) }; |
| 5037 | int nl_size = 12; /* can't use thunk_size due to alignment */ |
| 5038 | |
| 5039 | while (1) { |
| 5040 | uint32_t next = nl->next; |
| 5041 | if (next) { |
| 5042 | nl->next = nl_size + (strlen(nl->name) + 1); |
| 5043 | } |
| 5044 | if (remaining_data < nl->next) { |
| 5045 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 5046 | break; |
| 5047 | } |
| 5048 | thunk_convert(cur_data, nl, arg_type, THUNK_TARGET); |
| 5049 | strcpy(cur_data + nl_size, nl->name); |
| 5050 | cur_data += nl->next; |
| 5051 | remaining_data -= nl->next; |
| 5052 | if (!next) { |
| 5053 | break; |
| 5054 | } |
| 5055 | nl = (void*)nl + next; |
| 5056 | } |
| 5057 | break; |
| 5058 | } |
| 5059 | case DM_DEV_WAIT: |
| 5060 | case DM_TABLE_STATUS: |
| 5061 | { |
| 5062 | struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start; |
| 5063 | void *cur_data = argptr; |
| 5064 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; |
| 5065 | int spec_size = thunk_type_size(arg_type, 0); |
| 5066 | int i; |
| 5067 | |
| 5068 | for (i = 0; i < host_dm->target_count; i++) { |
| 5069 | uint32_t next = spec->next; |
| 5070 | int slen = strlen((char*)&spec[1]) + 1; |
| 5071 | spec->next = (cur_data - argptr) + spec_size + slen; |
| 5072 | if (guest_data_size < spec->next) { |
| 5073 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 5074 | break; |
| 5075 | } |
| 5076 | thunk_convert(cur_data, spec, arg_type, THUNK_TARGET); |
| 5077 | strcpy(cur_data + spec_size, (char*)&spec[1]); |
| 5078 | cur_data = argptr + spec->next; |
| 5079 | spec = (void*)host_dm + host_dm->data_start + next; |
| 5080 | } |
| 5081 | break; |
| 5082 | } |
| 5083 | case DM_TABLE_DEPS: |
| 5084 | { |
| 5085 | void *hdata = (void*)host_dm + host_dm->data_start; |
| 5086 | int count = *(uint32_t*)hdata; |
| 5087 | uint64_t *hdev = hdata + 8; |
| 5088 | uint64_t *gdev = argptr + 8; |
| 5089 | int i; |
| 5090 | |
| 5091 | *(uint32_t*)argptr = tswap32(count); |
| 5092 | for (i = 0; i < count; i++) { |
| 5093 | *gdev = tswap64(*hdev); |
| 5094 | gdev++; |
| 5095 | hdev++; |
| 5096 | } |
| 5097 | break; |
| 5098 | } |
| 5099 | case DM_LIST_VERSIONS: |
| 5100 | { |
| 5101 | struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start; |
| 5102 | uint32_t remaining_data = guest_data_size; |
| 5103 | void *cur_data = argptr; |
| 5104 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) }; |
| 5105 | int vers_size = thunk_type_size(arg_type, 0); |
| 5106 | |
| 5107 | while (1) { |
| 5108 | uint32_t next = vers->next; |
| 5109 | if (next) { |
| 5110 | vers->next = vers_size + (strlen(vers->name) + 1); |
| 5111 | } |
| 5112 | if (remaining_data < vers->next) { |
| 5113 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 5114 | break; |
| 5115 | } |
| 5116 | thunk_convert(cur_data, vers, arg_type, THUNK_TARGET); |
| 5117 | strcpy(cur_data + vers_size, vers->name); |
| 5118 | cur_data += vers->next; |
| 5119 | remaining_data -= vers->next; |
| 5120 | if (!next) { |
| 5121 | break; |
| 5122 | } |
| 5123 | vers = (void*)vers + next; |
| 5124 | } |
| 5125 | break; |
| 5126 | } |
| 5127 | default: |
Chen Gang S | dec0473 | 2015-01-25 08:00:42 +0800 | [diff] [blame] | 5128 | unlock_user(argptr, guest_data, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5129 | ret = -TARGET_EINVAL; |
| 5130 | goto out; |
| 5131 | } |
| 5132 | unlock_user(argptr, guest_data, guest_data_size); |
| 5133 | |
| 5134 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 5135 | if (!argptr) { |
| 5136 | ret = -TARGET_EFAULT; |
| 5137 | goto out; |
| 5138 | } |
| 5139 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 5140 | unlock_user(argptr, arg, target_size); |
| 5141 | } |
| 5142 | out: |
Stefan Weil | ad11ad7 | 2012-09-04 22:14:19 +0200 | [diff] [blame] | 5143 | g_free(big_buf); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 5144 | return ret; |
| 5145 | } |
| 5146 | |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 5147 | 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] | 5148 | int cmd, abi_long arg) |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 5149 | { |
| 5150 | void *argptr; |
| 5151 | int target_size; |
| 5152 | const argtype *arg_type = ie->arg_type; |
| 5153 | const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) }; |
| 5154 | abi_long ret; |
| 5155 | |
| 5156 | struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp; |
| 5157 | struct blkpg_partition host_part; |
| 5158 | |
| 5159 | /* Read and convert blkpg */ |
| 5160 | arg_type++; |
| 5161 | target_size = thunk_type_size(arg_type, 0); |
| 5162 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5163 | if (!argptr) { |
| 5164 | ret = -TARGET_EFAULT; |
| 5165 | goto out; |
| 5166 | } |
| 5167 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 5168 | unlock_user(argptr, arg, 0); |
| 5169 | |
| 5170 | switch (host_blkpg->op) { |
| 5171 | case BLKPG_ADD_PARTITION: |
| 5172 | case BLKPG_DEL_PARTITION: |
| 5173 | /* payload is struct blkpg_partition */ |
| 5174 | break; |
| 5175 | default: |
| 5176 | /* Unknown opcode */ |
| 5177 | ret = -TARGET_EINVAL; |
| 5178 | goto out; |
| 5179 | } |
| 5180 | |
| 5181 | /* Read and convert blkpg->data */ |
| 5182 | arg = (abi_long)(uintptr_t)host_blkpg->data; |
| 5183 | target_size = thunk_type_size(part_arg_type, 0); |
| 5184 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5185 | if (!argptr) { |
| 5186 | ret = -TARGET_EFAULT; |
| 5187 | goto out; |
| 5188 | } |
| 5189 | thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST); |
| 5190 | unlock_user(argptr, arg, 0); |
| 5191 | |
| 5192 | /* Swizzle the data pointer to our local copy and call! */ |
| 5193 | host_blkpg->data = &host_part; |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5194 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg)); |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 5195 | |
| 5196 | out: |
| 5197 | return ret; |
| 5198 | } |
| 5199 | |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5200 | 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] | 5201 | int fd, int cmd, abi_long arg) |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5202 | { |
| 5203 | const argtype *arg_type = ie->arg_type; |
| 5204 | const StructEntry *se; |
| 5205 | const argtype *field_types; |
| 5206 | const int *dst_offsets, *src_offsets; |
| 5207 | int target_size; |
| 5208 | void *argptr; |
Marc-André Lureau | b78c522 | 2019-03-05 16:15:00 +0100 | [diff] [blame] | 5209 | abi_ulong *target_rt_dev_ptr = NULL; |
| 5210 | unsigned long *host_rt_dev_ptr = NULL; |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5211 | abi_long ret; |
| 5212 | int i; |
| 5213 | |
| 5214 | assert(ie->access == IOC_W); |
| 5215 | assert(*arg_type == TYPE_PTR); |
| 5216 | arg_type++; |
| 5217 | assert(*arg_type == TYPE_STRUCT); |
| 5218 | target_size = thunk_type_size(arg_type, 0); |
| 5219 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5220 | if (!argptr) { |
| 5221 | return -TARGET_EFAULT; |
| 5222 | } |
| 5223 | arg_type++; |
| 5224 | assert(*arg_type == (int)STRUCT_rtentry); |
| 5225 | se = struct_entries + *arg_type++; |
| 5226 | assert(se->convert[0] == NULL); |
| 5227 | /* convert struct here to be able to catch rt_dev string */ |
| 5228 | field_types = se->field_types; |
| 5229 | dst_offsets = se->field_offsets[THUNK_HOST]; |
| 5230 | src_offsets = se->field_offsets[THUNK_TARGET]; |
| 5231 | for (i = 0; i < se->nb_fields; i++) { |
| 5232 | if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) { |
| 5233 | assert(*field_types == TYPE_PTRVOID); |
| 5234 | target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]); |
| 5235 | host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]); |
| 5236 | if (*target_rt_dev_ptr != 0) { |
| 5237 | *host_rt_dev_ptr = (unsigned long)lock_user_string( |
| 5238 | tswapal(*target_rt_dev_ptr)); |
| 5239 | if (!*host_rt_dev_ptr) { |
| 5240 | unlock_user(argptr, arg, 0); |
| 5241 | return -TARGET_EFAULT; |
| 5242 | } |
| 5243 | } else { |
| 5244 | *host_rt_dev_ptr = 0; |
| 5245 | } |
| 5246 | field_types++; |
| 5247 | continue; |
| 5248 | } |
| 5249 | field_types = thunk_convert(buf_temp + dst_offsets[i], |
| 5250 | argptr + src_offsets[i], |
| 5251 | field_types, THUNK_HOST); |
| 5252 | } |
| 5253 | unlock_user(argptr, arg, 0); |
| 5254 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5255 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
Marc-André Lureau | b78c522 | 2019-03-05 16:15:00 +0100 | [diff] [blame] | 5256 | |
| 5257 | assert(host_rt_dev_ptr != NULL); |
| 5258 | assert(target_rt_dev_ptr != NULL); |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 5259 | if (*host_rt_dev_ptr != 0) { |
| 5260 | unlock_user((void *)*host_rt_dev_ptr, |
| 5261 | *target_rt_dev_ptr, 0); |
| 5262 | } |
| 5263 | return ret; |
| 5264 | } |
| 5265 | |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 5266 | 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] | 5267 | int fd, int cmd, abi_long arg) |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 5268 | { |
| 5269 | int sig = target_to_host_signal(arg); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5270 | return get_errno(safe_ioctl(fd, ie->host_cmd, sig)); |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 5271 | } |
| 5272 | |
Daniel P. Berrangé | 6d5d5dd | 2019-07-18 15:06:41 +0200 | [diff] [blame] | 5273 | static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5274 | int fd, int cmd, abi_long arg) |
| 5275 | { |
| 5276 | struct timeval tv; |
| 5277 | abi_long ret; |
| 5278 | |
| 5279 | ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv)); |
| 5280 | if (is_error(ret)) { |
| 5281 | return ret; |
| 5282 | } |
| 5283 | |
| 5284 | if (cmd == (int)TARGET_SIOCGSTAMP_OLD) { |
| 5285 | if (copy_to_user_timeval(arg, &tv)) { |
| 5286 | return -TARGET_EFAULT; |
| 5287 | } |
| 5288 | } else { |
| 5289 | if (copy_to_user_timeval64(arg, &tv)) { |
| 5290 | return -TARGET_EFAULT; |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | return ret; |
| 5295 | } |
| 5296 | |
| 5297 | static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5298 | int fd, int cmd, abi_long arg) |
| 5299 | { |
| 5300 | struct timespec ts; |
| 5301 | abi_long ret; |
| 5302 | |
| 5303 | ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts)); |
| 5304 | if (is_error(ret)) { |
| 5305 | return ret; |
| 5306 | } |
| 5307 | |
| 5308 | if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) { |
| 5309 | if (host_to_target_timespec(arg, &ts)) { |
| 5310 | return -TARGET_EFAULT; |
| 5311 | } |
| 5312 | } else{ |
| 5313 | if (host_to_target_timespec64(arg, &ts)) { |
| 5314 | return -TARGET_EFAULT; |
| 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | return ret; |
| 5319 | } |
| 5320 | |
Andreas Schwab | 2b74f62 | 2018-01-29 11:47:06 +0100 | [diff] [blame] | 5321 | #ifdef TIOCGPTPEER |
| 5322 | static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5323 | int fd, int cmd, abi_long arg) |
| 5324 | { |
| 5325 | int flags = target_to_host_bitmask(arg, fcntl_flags_tbl); |
| 5326 | return get_errno(safe_ioctl(fd, ie->host_cmd, flags)); |
| 5327 | } |
| 5328 | #endif |
| 5329 | |
Chen Gang | e865b97 | 2020-06-05 09:32:21 +0800 | [diff] [blame] | 5330 | #ifdef HAVE_DRM_H |
| 5331 | |
| 5332 | static void unlock_drm_version(struct drm_version *host_ver, |
| 5333 | struct target_drm_version *target_ver, |
| 5334 | bool copy) |
| 5335 | { |
| 5336 | unlock_user(host_ver->name, target_ver->name, |
| 5337 | copy ? host_ver->name_len : 0); |
| 5338 | unlock_user(host_ver->date, target_ver->date, |
| 5339 | copy ? host_ver->date_len : 0); |
| 5340 | unlock_user(host_ver->desc, target_ver->desc, |
| 5341 | copy ? host_ver->desc_len : 0); |
| 5342 | } |
| 5343 | |
| 5344 | static inline abi_long target_to_host_drmversion(struct drm_version *host_ver, |
| 5345 | struct target_drm_version *target_ver) |
| 5346 | { |
| 5347 | memset(host_ver, 0, sizeof(*host_ver)); |
| 5348 | |
| 5349 | __get_user(host_ver->name_len, &target_ver->name_len); |
| 5350 | if (host_ver->name_len) { |
| 5351 | host_ver->name = lock_user(VERIFY_WRITE, target_ver->name, |
| 5352 | target_ver->name_len, 0); |
| 5353 | if (!host_ver->name) { |
| 5354 | return -EFAULT; |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | __get_user(host_ver->date_len, &target_ver->date_len); |
| 5359 | if (host_ver->date_len) { |
| 5360 | host_ver->date = lock_user(VERIFY_WRITE, target_ver->date, |
| 5361 | target_ver->date_len, 0); |
| 5362 | if (!host_ver->date) { |
| 5363 | goto err; |
| 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | __get_user(host_ver->desc_len, &target_ver->desc_len); |
| 5368 | if (host_ver->desc_len) { |
| 5369 | host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc, |
| 5370 | target_ver->desc_len, 0); |
| 5371 | if (!host_ver->desc) { |
| 5372 | goto err; |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | return 0; |
| 5377 | err: |
| 5378 | unlock_drm_version(host_ver, target_ver, false); |
| 5379 | return -EFAULT; |
| 5380 | } |
| 5381 | |
| 5382 | static inline void host_to_target_drmversion( |
| 5383 | struct target_drm_version *target_ver, |
| 5384 | struct drm_version *host_ver) |
| 5385 | { |
| 5386 | __put_user(host_ver->version_major, &target_ver->version_major); |
| 5387 | __put_user(host_ver->version_minor, &target_ver->version_minor); |
| 5388 | __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel); |
| 5389 | __put_user(host_ver->name_len, &target_ver->name_len); |
| 5390 | __put_user(host_ver->date_len, &target_ver->date_len); |
| 5391 | __put_user(host_ver->desc_len, &target_ver->desc_len); |
| 5392 | unlock_drm_version(host_ver, target_ver, true); |
| 5393 | } |
| 5394 | |
| 5395 | static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp, |
| 5396 | int fd, int cmd, abi_long arg) |
| 5397 | { |
| 5398 | struct drm_version *ver; |
| 5399 | struct target_drm_version *target_ver; |
| 5400 | abi_long ret; |
| 5401 | |
| 5402 | switch (ie->host_cmd) { |
| 5403 | case DRM_IOCTL_VERSION: |
| 5404 | if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) { |
| 5405 | return -TARGET_EFAULT; |
| 5406 | } |
| 5407 | ver = (struct drm_version *)buf_temp; |
| 5408 | ret = target_to_host_drmversion(ver, target_ver); |
| 5409 | if (!is_error(ret)) { |
| 5410 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver)); |
| 5411 | if (is_error(ret)) { |
| 5412 | unlock_drm_version(ver, target_ver, false); |
| 5413 | } else { |
| 5414 | host_to_target_drmversion(target_ver, ver); |
| 5415 | } |
| 5416 | } |
| 5417 | unlock_user_struct(target_ver, arg, 0); |
| 5418 | return ret; |
| 5419 | } |
| 5420 | return -TARGET_ENOSYS; |
| 5421 | } |
| 5422 | |
| 5423 | #endif |
| 5424 | |
Filip Bozuta | 79482e59 | 2020-06-19 14:47:27 +0200 | [diff] [blame] | 5425 | IOCTLEntry ioctl_entries[] = { |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 5426 | #define IOCTL(cmd, access, ...) \ |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 5427 | { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } }, |
| 5428 | #define IOCTL_SPECIAL(cmd, access, dofn, ...) \ |
| 5429 | { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } }, |
Helge Deller | 8cb762f | 2016-12-06 16:24:03 +0100 | [diff] [blame] | 5430 | #define IOCTL_IGNORE(cmd) \ |
| 5431 | { TARGET_ ## cmd, 0, #cmd }, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5432 | #include "ioctls.h" |
| 5433 | { 0, 0, }, |
| 5434 | }; |
| 5435 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5436 | /* ??? Implement proper locking for ioctls. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 5437 | /* do_ioctl() Must return target values and target errnos. */ |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 5438 | static abi_long do_ioctl(int fd, int cmd, abi_long arg) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5439 | { |
| 5440 | const IOCTLEntry *ie; |
| 5441 | const argtype *arg_type; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5442 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5443 | uint8_t buf_temp[MAX_STRUCT_SIZE]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5444 | int target_size; |
| 5445 | void *argptr; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5446 | |
| 5447 | ie = ioctl_entries; |
| 5448 | for(;;) { |
| 5449 | if (ie->target_cmd == 0) { |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 5450 | qemu_log_mask( |
| 5451 | LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 5452 | return -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5453 | } |
| 5454 | if (ie->target_cmd == cmd) |
| 5455 | break; |
| 5456 | ie++; |
| 5457 | } |
| 5458 | arg_type = ie->arg_type; |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 5459 | if (ie->do_ioctl) { |
| 5460 | return ie->do_ioctl(ie, buf_temp, fd, cmd, arg); |
Helge Deller | 8cb762f | 2016-12-06 16:24:03 +0100 | [diff] [blame] | 5461 | } else if (!ie->host_cmd) { |
| 5462 | /* Some architectures define BSD ioctls in their headers |
| 5463 | that are not implemented in Linux. */ |
| 5464 | return -TARGET_ENOSYS; |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5467 | switch(arg_type[0]) { |
| 5468 | case TYPE_NULL: |
| 5469 | /* no argument */ |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5470 | ret = get_errno(safe_ioctl(fd, ie->host_cmd)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | case TYPE_PTRVOID: |
| 5473 | case TYPE_INT: |
Filip Bozuta | c858e53 | 2020-01-15 20:36:47 +0100 | [diff] [blame] | 5474 | case TYPE_LONG: |
| 5475 | case TYPE_ULONG: |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5476 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5477 | break; |
| 5478 | case TYPE_PTR: |
| 5479 | arg_type++; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5480 | target_size = thunk_type_size(arg_type, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5481 | switch(ie->access) { |
| 5482 | case IOC_R: |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5483 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5484 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5485 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 5486 | if (!argptr) |
| 5487 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5488 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 5489 | unlock_user(argptr, arg, target_size); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5490 | } |
| 5491 | break; |
| 5492 | case IOC_W: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5493 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5494 | if (!argptr) |
| 5495 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5496 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 5497 | unlock_user(argptr, arg, 0); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5498 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5499 | break; |
| 5500 | default: |
| 5501 | case IOC_RW: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5502 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 5503 | if (!argptr) |
| 5504 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5505 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 5506 | unlock_user(argptr, arg, 0); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 5507 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5508 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5509 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 5510 | if (!argptr) |
| 5511 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5512 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 5513 | unlock_user(argptr, arg, target_size); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5514 | } |
| 5515 | break; |
| 5516 | } |
| 5517 | break; |
| 5518 | default: |
Josh Kunz | 39be535 | 2020-02-03 18:54:13 -0800 | [diff] [blame] | 5519 | qemu_log_mask(LOG_UNIMP, |
| 5520 | "Unsupported ioctl type: cmd=0x%04lx type=%d\n", |
| 5521 | (long)cmd, arg_type[0]); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 5522 | ret = -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5523 | break; |
| 5524 | } |
| 5525 | return ret; |
| 5526 | } |
| 5527 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5528 | static const bitmask_transtbl iflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5529 | { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK }, |
| 5530 | { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT }, |
| 5531 | { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR }, |
| 5532 | { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK }, |
| 5533 | { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK }, |
| 5534 | { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP }, |
| 5535 | { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR }, |
| 5536 | { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR }, |
| 5537 | { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL }, |
| 5538 | { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC }, |
| 5539 | { TARGET_IXON, TARGET_IXON, IXON, IXON }, |
| 5540 | { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY }, |
| 5541 | { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF }, |
| 5542 | { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL }, |
| 5543 | { 0, 0, 0, 0 } |
| 5544 | }; |
| 5545 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5546 | static const bitmask_transtbl oflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5547 | { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST }, |
| 5548 | { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC }, |
| 5549 | { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR }, |
| 5550 | { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL }, |
| 5551 | { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR }, |
| 5552 | { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET }, |
| 5553 | { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL }, |
| 5554 | { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL }, |
| 5555 | { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 }, |
| 5556 | { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 }, |
| 5557 | { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 }, |
| 5558 | { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 }, |
| 5559 | { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 }, |
| 5560 | { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 }, |
| 5561 | { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 }, |
| 5562 | { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 }, |
| 5563 | { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 }, |
| 5564 | { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 }, |
| 5565 | { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 }, |
| 5566 | { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 }, |
| 5567 | { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 }, |
| 5568 | { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 }, |
| 5569 | { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 }, |
| 5570 | { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 }, |
| 5571 | { 0, 0, 0, 0 } |
| 5572 | }; |
| 5573 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5574 | static const bitmask_transtbl cflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5575 | { TARGET_CBAUD, TARGET_B0, CBAUD, B0 }, |
| 5576 | { TARGET_CBAUD, TARGET_B50, CBAUD, B50 }, |
| 5577 | { TARGET_CBAUD, TARGET_B75, CBAUD, B75 }, |
| 5578 | { TARGET_CBAUD, TARGET_B110, CBAUD, B110 }, |
| 5579 | { TARGET_CBAUD, TARGET_B134, CBAUD, B134 }, |
| 5580 | { TARGET_CBAUD, TARGET_B150, CBAUD, B150 }, |
| 5581 | { TARGET_CBAUD, TARGET_B200, CBAUD, B200 }, |
| 5582 | { TARGET_CBAUD, TARGET_B300, CBAUD, B300 }, |
| 5583 | { TARGET_CBAUD, TARGET_B600, CBAUD, B600 }, |
| 5584 | { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 }, |
| 5585 | { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 }, |
| 5586 | { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 }, |
| 5587 | { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 }, |
| 5588 | { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 }, |
| 5589 | { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 }, |
| 5590 | { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 }, |
| 5591 | { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 }, |
| 5592 | { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 }, |
| 5593 | { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 }, |
| 5594 | { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 }, |
| 5595 | { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 }, |
| 5596 | { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 }, |
| 5597 | { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 }, |
| 5598 | { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 }, |
| 5599 | { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB }, |
| 5600 | { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD }, |
| 5601 | { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB }, |
| 5602 | { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD }, |
| 5603 | { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL }, |
| 5604 | { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL }, |
| 5605 | { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS }, |
| 5606 | { 0, 0, 0, 0 } |
| 5607 | }; |
| 5608 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 5609 | static const bitmask_transtbl lflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5610 | { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG }, |
| 5611 | { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON }, |
| 5612 | { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE }, |
| 5613 | { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO }, |
| 5614 | { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE }, |
| 5615 | { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK }, |
| 5616 | { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL }, |
| 5617 | { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH }, |
| 5618 | { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP }, |
| 5619 | { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL }, |
| 5620 | { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT }, |
| 5621 | { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE }, |
| 5622 | { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO }, |
| 5623 | { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN }, |
| 5624 | { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN }, |
| 5625 | { 0, 0, 0, 0 } |
| 5626 | }; |
| 5627 | |
| 5628 | static void target_to_host_termios (void *dst, const void *src) |
| 5629 | { |
| 5630 | struct host_termios *host = dst; |
| 5631 | const struct target_termios *target = src; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5632 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5633 | host->c_iflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5634 | target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5635 | host->c_oflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5636 | target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5637 | host->c_cflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5638 | target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5639 | host->c_lflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5640 | target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl); |
| 5641 | host->c_line = target->c_line; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5642 | |
Arnaud Patard | 4460712 | 2009-04-21 17:39:08 +0300 | [diff] [blame] | 5643 | memset(host->c_cc, 0, sizeof(host->c_cc)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5644 | host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; |
| 5645 | host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5646 | host->c_cc[VERASE] = target->c_cc[TARGET_VERASE]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5647 | host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5648 | host->c_cc[VEOF] = target->c_cc[TARGET_VEOF]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5649 | host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5650 | host->c_cc[VMIN] = target->c_cc[TARGET_VMIN]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5651 | host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5652 | host->c_cc[VSTART] = target->c_cc[TARGET_VSTART]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5653 | host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; |
| 5654 | host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5655 | host->c_cc[VEOL] = target->c_cc[TARGET_VEOL]; |
| 5656 | host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT]; |
| 5657 | host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD]; |
| 5658 | host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE]; |
| 5659 | host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5660 | host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5661 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5662 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5663 | static void host_to_target_termios (void *dst, const void *src) |
| 5664 | { |
| 5665 | struct target_termios *target = dst; |
| 5666 | const struct host_termios *host = src; |
| 5667 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5668 | target->c_iflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5669 | tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5670 | target->c_oflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5671 | tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5672 | target->c_cflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5673 | tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5674 | target->c_lflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5675 | tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl)); |
| 5676 | target->c_line = host->c_line; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5677 | |
Arnaud Patard | 4460712 | 2009-04-21 17:39:08 +0300 | [diff] [blame] | 5678 | memset(target->c_cc, 0, sizeof(target->c_cc)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5679 | target->c_cc[TARGET_VINTR] = host->c_cc[VINTR]; |
| 5680 | target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT]; |
| 5681 | target->c_cc[TARGET_VERASE] = host->c_cc[VERASE]; |
| 5682 | target->c_cc[TARGET_VKILL] = host->c_cc[VKILL]; |
| 5683 | target->c_cc[TARGET_VEOF] = host->c_cc[VEOF]; |
| 5684 | target->c_cc[TARGET_VTIME] = host->c_cc[VTIME]; |
| 5685 | target->c_cc[TARGET_VMIN] = host->c_cc[VMIN]; |
| 5686 | target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC]; |
| 5687 | target->c_cc[TARGET_VSTART] = host->c_cc[VSTART]; |
| 5688 | target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP]; |
| 5689 | target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP]; |
| 5690 | target->c_cc[TARGET_VEOL] = host->c_cc[VEOL]; |
| 5691 | target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT]; |
| 5692 | target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD]; |
| 5693 | target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE]; |
| 5694 | target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT]; |
| 5695 | target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2]; |
| 5696 | } |
| 5697 | |
blueswir1 | 8e853dc | 2008-10-05 10:49:32 +0000 | [diff] [blame] | 5698 | static const StructEntry struct_termios_def = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5699 | .convert = { host_to_target_termios, target_to_host_termios }, |
| 5700 | .size = { sizeof(struct target_termios), sizeof(struct host_termios) }, |
| 5701 | .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) }, |
| 5702 | }; |
| 5703 | |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 5704 | static bitmask_transtbl mmap_flags_tbl[] = { |
Helge Deller | 541e169 | 2017-10-31 13:53:58 +0100 | [diff] [blame] | 5705 | { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED }, |
| 5706 | { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE }, |
| 5707 | { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED }, |
| 5708 | { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, |
| 5709 | MAP_ANONYMOUS, MAP_ANONYMOUS }, |
| 5710 | { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, |
| 5711 | MAP_GROWSDOWN, MAP_GROWSDOWN }, |
| 5712 | { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, |
| 5713 | MAP_DENYWRITE, MAP_DENYWRITE }, |
| 5714 | { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, |
| 5715 | MAP_EXECUTABLE, MAP_EXECUTABLE }, |
| 5716 | { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED }, |
| 5717 | { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, |
| 5718 | MAP_NORESERVE, MAP_NORESERVE }, |
| 5719 | { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB }, |
| 5720 | /* MAP_STACK had been ignored by the kernel for quite some time. |
| 5721 | Recognize it for the target insofar as we do not want to pass |
| 5722 | it through to the host. */ |
| 5723 | { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 }, |
| 5724 | { 0, 0, 0, 0 } |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 5725 | }; |
| 5726 | |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 5727 | /* |
| 5728 | * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64) |
| 5729 | * TARGET_I386 is defined if TARGET_X86_64 is defined |
| 5730 | */ |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5731 | #if defined(TARGET_I386) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5732 | |
| 5733 | /* NOTE: there is really one LDT for all the threads */ |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 5734 | static uint8_t *ldt_table; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5735 | |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5736 | static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5737 | { |
| 5738 | int size; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5739 | void *p; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5740 | |
| 5741 | if (!ldt_table) |
| 5742 | return 0; |
| 5743 | size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE; |
| 5744 | if (size > bytecount) |
| 5745 | size = bytecount; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5746 | p = lock_user(VERIFY_WRITE, ptr, size, 0); |
| 5747 | if (!p) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5748 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5749 | /* ??? Should this by byteswapped? */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5750 | memcpy(p, ldt_table, size); |
| 5751 | unlock_user(p, ptr, size); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5752 | return size; |
| 5753 | } |
| 5754 | |
| 5755 | /* XXX: add locking support */ |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5756 | static abi_long write_ldt(CPUX86State *env, |
| 5757 | abi_ulong ptr, unsigned long bytecount, int oldmode) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5758 | { |
| 5759 | struct target_modify_ldt_ldt_s ldt_info; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5760 | struct target_modify_ldt_ldt_s *target_ldt_info; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5761 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5762 | int seg_not_present, useable, lm; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5763 | uint32_t *lp, entry_1, entry_2; |
| 5764 | |
| 5765 | if (bytecount != sizeof(ldt_info)) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5766 | return -TARGET_EINVAL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5767 | if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1)) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5768 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5769 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5770 | ldt_info.base_addr = tswapal(target_ldt_info->base_addr); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5771 | ldt_info.limit = tswap32(target_ldt_info->limit); |
| 5772 | ldt_info.flags = tswap32(target_ldt_info->flags); |
| 5773 | unlock_user_struct(target_ldt_info, ptr, 0); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5774 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5775 | if (ldt_info.entry_number >= TARGET_LDT_ENTRIES) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5776 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5777 | seg_32bit = ldt_info.flags & 1; |
| 5778 | contents = (ldt_info.flags >> 1) & 3; |
| 5779 | read_exec_only = (ldt_info.flags >> 3) & 1; |
| 5780 | limit_in_pages = (ldt_info.flags >> 4) & 1; |
| 5781 | seg_not_present = (ldt_info.flags >> 5) & 1; |
| 5782 | useable = (ldt_info.flags >> 6) & 1; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5783 | #ifdef TARGET_ABI32 |
| 5784 | lm = 0; |
| 5785 | #else |
| 5786 | lm = (ldt_info.flags >> 7) & 1; |
| 5787 | #endif |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5788 | if (contents == 3) { |
| 5789 | if (oldmode) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5790 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5791 | if (seg_not_present == 0) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5792 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5793 | } |
| 5794 | /* allocate the LDT */ |
| 5795 | if (!ldt_table) { |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 5796 | env->ldt.base = target_mmap(0, |
| 5797 | TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE, |
| 5798 | PROT_READ|PROT_WRITE, |
| 5799 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 5800 | if (env->ldt.base == -1) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5801 | return -TARGET_ENOMEM; |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 5802 | memset(g2h(env->ldt.base), 0, |
| 5803 | TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5804 | env->ldt.limit = 0xffff; |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 5805 | ldt_table = g2h(env->ldt.base); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | /* NOTE: same code as Linux kernel */ |
| 5809 | /* Allow LDTs to be cleared by the user. */ |
| 5810 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 5811 | if (oldmode || |
| 5812 | (contents == 0 && |
| 5813 | read_exec_only == 1 && |
| 5814 | seg_32bit == 0 && |
| 5815 | limit_in_pages == 0 && |
| 5816 | seg_not_present == 1 && |
| 5817 | useable == 0 )) { |
| 5818 | entry_1 = 0; |
| 5819 | entry_2 = 0; |
| 5820 | goto install; |
| 5821 | } |
| 5822 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5823 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5824 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
| 5825 | (ldt_info.limit & 0x0ffff); |
| 5826 | entry_2 = (ldt_info.base_addr & 0xff000000) | |
| 5827 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | |
| 5828 | (ldt_info.limit & 0xf0000) | |
| 5829 | ((read_exec_only ^ 1) << 9) | |
| 5830 | (contents << 10) | |
| 5831 | ((seg_not_present ^ 1) << 15) | |
| 5832 | (seg_32bit << 22) | |
| 5833 | (limit_in_pages << 23) | |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5834 | (lm << 21) | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5835 | 0x7000; |
| 5836 | if (!oldmode) |
| 5837 | entry_2 |= (useable << 20); |
bellard | 14ae3ba | 2003-05-27 23:25:06 +0000 | [diff] [blame] | 5838 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5839 | /* Install the new entry ... */ |
| 5840 | install: |
| 5841 | lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3)); |
| 5842 | lp[0] = tswap32(entry_1); |
| 5843 | lp[1] = tswap32(entry_2); |
| 5844 | return 0; |
| 5845 | } |
| 5846 | |
| 5847 | /* specific and weird i386 syscalls */ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 5848 | static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, |
| 5849 | unsigned long bytecount) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5850 | { |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5851 | abi_long ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5852 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5853 | switch (func) { |
| 5854 | case 0: |
| 5855 | ret = read_ldt(ptr, bytecount); |
| 5856 | break; |
| 5857 | case 1: |
| 5858 | ret = write_ldt(env, ptr, bytecount, 1); |
| 5859 | break; |
| 5860 | case 0x11: |
| 5861 | ret = write_ldt(env, ptr, bytecount, 0); |
| 5862 | break; |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5863 | default: |
| 5864 | ret = -TARGET_ENOSYS; |
| 5865 | break; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5866 | } |
| 5867 | return ret; |
| 5868 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5869 | |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 5870 | #if defined(TARGET_ABI32) |
Alexander Graf | bc22eb4 | 2013-07-16 18:44:58 +0100 | [diff] [blame] | 5871 | abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr) |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5872 | { |
| 5873 | uint64_t *gdt_table = g2h(env->gdt.base); |
| 5874 | struct target_modify_ldt_ldt_s ldt_info; |
| 5875 | struct target_modify_ldt_ldt_s *target_ldt_info; |
| 5876 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
| 5877 | int seg_not_present, useable, lm; |
| 5878 | uint32_t *lp, entry_1, entry_2; |
| 5879 | int i; |
| 5880 | |
| 5881 | lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); |
| 5882 | if (!target_ldt_info) |
| 5883 | return -TARGET_EFAULT; |
| 5884 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5885 | ldt_info.base_addr = tswapal(target_ldt_info->base_addr); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5886 | ldt_info.limit = tswap32(target_ldt_info->limit); |
| 5887 | ldt_info.flags = tswap32(target_ldt_info->flags); |
| 5888 | if (ldt_info.entry_number == -1) { |
| 5889 | for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) { |
| 5890 | if (gdt_table[i] == 0) { |
| 5891 | ldt_info.entry_number = i; |
| 5892 | target_ldt_info->entry_number = tswap32(i); |
| 5893 | break; |
| 5894 | } |
| 5895 | } |
| 5896 | } |
| 5897 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 5898 | |
| 5899 | if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN || |
| 5900 | ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX) |
| 5901 | return -TARGET_EINVAL; |
| 5902 | seg_32bit = ldt_info.flags & 1; |
| 5903 | contents = (ldt_info.flags >> 1) & 3; |
| 5904 | read_exec_only = (ldt_info.flags >> 3) & 1; |
| 5905 | limit_in_pages = (ldt_info.flags >> 4) & 1; |
| 5906 | seg_not_present = (ldt_info.flags >> 5) & 1; |
| 5907 | useable = (ldt_info.flags >> 6) & 1; |
| 5908 | #ifdef TARGET_ABI32 |
| 5909 | lm = 0; |
| 5910 | #else |
| 5911 | lm = (ldt_info.flags >> 7) & 1; |
| 5912 | #endif |
| 5913 | |
| 5914 | if (contents == 3) { |
| 5915 | if (seg_not_present == 0) |
| 5916 | return -TARGET_EINVAL; |
| 5917 | } |
| 5918 | |
| 5919 | /* NOTE: same code as Linux kernel */ |
| 5920 | /* Allow LDTs to be cleared by the user. */ |
| 5921 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 5922 | if ((contents == 0 && |
| 5923 | read_exec_only == 1 && |
| 5924 | seg_32bit == 0 && |
| 5925 | limit_in_pages == 0 && |
| 5926 | seg_not_present == 1 && |
| 5927 | useable == 0 )) { |
| 5928 | entry_1 = 0; |
| 5929 | entry_2 = 0; |
| 5930 | goto install; |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
| 5935 | (ldt_info.limit & 0x0ffff); |
| 5936 | entry_2 = (ldt_info.base_addr & 0xff000000) | |
| 5937 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | |
| 5938 | (ldt_info.limit & 0xf0000) | |
| 5939 | ((read_exec_only ^ 1) << 9) | |
| 5940 | (contents << 10) | |
| 5941 | ((seg_not_present ^ 1) << 15) | |
| 5942 | (seg_32bit << 22) | |
| 5943 | (limit_in_pages << 23) | |
| 5944 | (useable << 20) | |
| 5945 | (lm << 21) | |
| 5946 | 0x7000; |
| 5947 | |
| 5948 | /* Install the new entry ... */ |
| 5949 | install: |
| 5950 | lp = (uint32_t *)(gdt_table + ldt_info.entry_number); |
| 5951 | lp[0] = tswap32(entry_1); |
| 5952 | lp[1] = tswap32(entry_2); |
| 5953 | return 0; |
| 5954 | } |
| 5955 | |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 5956 | static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr) |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5957 | { |
| 5958 | struct target_modify_ldt_ldt_s *target_ldt_info; |
| 5959 | uint64_t *gdt_table = g2h(env->gdt.base); |
| 5960 | uint32_t base_addr, limit, flags; |
| 5961 | int seg_32bit, contents, read_exec_only, limit_in_pages, idx; |
| 5962 | int seg_not_present, useable, lm; |
| 5963 | uint32_t *lp, entry_1, entry_2; |
| 5964 | |
| 5965 | lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); |
| 5966 | if (!target_ldt_info) |
| 5967 | return -TARGET_EFAULT; |
| 5968 | idx = tswap32(target_ldt_info->entry_number); |
| 5969 | if (idx < TARGET_GDT_ENTRY_TLS_MIN || |
| 5970 | idx > TARGET_GDT_ENTRY_TLS_MAX) { |
| 5971 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 5972 | return -TARGET_EINVAL; |
| 5973 | } |
| 5974 | lp = (uint32_t *)(gdt_table + idx); |
| 5975 | entry_1 = tswap32(lp[0]); |
| 5976 | entry_2 = tswap32(lp[1]); |
| 5977 | |
| 5978 | read_exec_only = ((entry_2 >> 9) & 1) ^ 1; |
| 5979 | contents = (entry_2 >> 10) & 3; |
| 5980 | seg_not_present = ((entry_2 >> 15) & 1) ^ 1; |
| 5981 | seg_32bit = (entry_2 >> 22) & 1; |
| 5982 | limit_in_pages = (entry_2 >> 23) & 1; |
| 5983 | useable = (entry_2 >> 20) & 1; |
| 5984 | #ifdef TARGET_ABI32 |
| 5985 | lm = 0; |
| 5986 | #else |
| 5987 | lm = (entry_2 >> 21) & 1; |
| 5988 | #endif |
| 5989 | flags = (seg_32bit << 0) | (contents << 1) | |
| 5990 | (read_exec_only << 3) | (limit_in_pages << 4) | |
| 5991 | (seg_not_present << 5) | (useable << 6) | (lm << 7); |
| 5992 | limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000); |
| 5993 | base_addr = (entry_1 >> 16) | |
| 5994 | (entry_2 & 0xff000000) | |
| 5995 | ((entry_2 & 0xff) << 16); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5996 | target_ldt_info->base_addr = tswapal(base_addr); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5997 | target_ldt_info->limit = tswap32(limit); |
| 5998 | target_ldt_info->flags = tswap32(flags); |
| 5999 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 6000 | return 0; |
| 6001 | } |
| 6002 | |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6003 | abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) |
| 6004 | { |
Helge Deller | 538fabc | 2020-04-25 00:00:33 +0200 | [diff] [blame] | 6005 | return -TARGET_ENOSYS; |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6006 | } |
| 6007 | #else |
Peter Maydell | 2667e71 | 2013-07-16 18:44:59 +0100 | [diff] [blame] | 6008 | abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6009 | { |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6010 | abi_long ret = 0; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6011 | abi_ulong val; |
| 6012 | int idx; |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6013 | |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6014 | switch(code) { |
| 6015 | case TARGET_ARCH_SET_GS: |
| 6016 | case TARGET_ARCH_SET_FS: |
| 6017 | if (code == TARGET_ARCH_SET_GS) |
| 6018 | idx = R_GS; |
| 6019 | else |
| 6020 | idx = R_FS; |
| 6021 | cpu_x86_load_seg(env, idx, 0); |
| 6022 | env->segs[idx].base = addr; |
| 6023 | break; |
| 6024 | case TARGET_ARCH_GET_GS: |
| 6025 | case TARGET_ARCH_GET_FS: |
| 6026 | if (code == TARGET_ARCH_GET_GS) |
| 6027 | idx = R_GS; |
| 6028 | else |
| 6029 | idx = R_FS; |
| 6030 | val = env->segs[idx].base; |
| 6031 | if (put_user(val, addr, abi_ulong)) |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6032 | ret = -TARGET_EFAULT; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6033 | break; |
| 6034 | default: |
| 6035 | ret = -TARGET_EINVAL; |
| 6036 | break; |
| 6037 | } |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 6038 | return ret; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6039 | } |
Laurent Vivier | 9d12f79 | 2020-03-10 11:33:55 +0100 | [diff] [blame] | 6040 | #endif /* defined(TARGET_ABI32 */ |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 6041 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6042 | #endif /* defined(TARGET_I386) */ |
| 6043 | |
Riku Voipio | 05098a9 | 2011-03-04 15:27:29 +0200 | [diff] [blame] | 6044 | #define NEW_STACK_SIZE 0x40000 |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6045 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6046 | |
| 6047 | static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER; |
| 6048 | typedef struct { |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6049 | CPUArchState *env; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6050 | pthread_mutex_t mutex; |
| 6051 | pthread_cond_t cond; |
| 6052 | pthread_t thread; |
| 6053 | uint32_t tid; |
| 6054 | abi_ulong child_tidptr; |
| 6055 | abi_ulong parent_tidptr; |
| 6056 | sigset_t sigmask; |
| 6057 | } new_thread_info; |
| 6058 | |
| 6059 | static void *clone_func(void *arg) |
| 6060 | { |
| 6061 | new_thread_info *info = arg; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6062 | CPUArchState *env; |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 6063 | CPUState *cpu; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 6064 | TaskState *ts; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6065 | |
Emilio G. Cota | 7090376 | 2015-08-23 20:23:41 -0400 | [diff] [blame] | 6066 | rcu_register_thread(); |
Emilio G. Cota | 3468b59 | 2017-07-19 18:57:58 -0400 | [diff] [blame] | 6067 | tcg_register_thread(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6068 | env = info->env; |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 6069 | cpu = env_cpu(env); |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 6070 | thread_cpu = cpu; |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6071 | ts = (TaskState *)cpu->opaque; |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 6072 | info->tid = sys_gettid(); |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 6073 | task_settid(ts); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6074 | if (info->child_tidptr) |
| 6075 | put_user_u32(info->tid, info->child_tidptr); |
| 6076 | if (info->parent_tidptr) |
| 6077 | put_user_u32(info->tid, info->parent_tidptr); |
Richard Henderson | 5ebdd77 | 2019-03-14 13:10:53 -0700 | [diff] [blame] | 6078 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6079 | /* Enable signals. */ |
| 6080 | sigprocmask(SIG_SETMASK, &info->sigmask, NULL); |
| 6081 | /* Signal to the parent that we're ready. */ |
| 6082 | pthread_mutex_lock(&info->mutex); |
| 6083 | pthread_cond_broadcast(&info->cond); |
| 6084 | pthread_mutex_unlock(&info->mutex); |
Emilio G. Cota | a4dd3d5 | 2017-10-18 18:01:41 -0400 | [diff] [blame] | 6085 | /* Wait until the parent has finished initializing the tls state. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6086 | pthread_mutex_lock(&clone_lock); |
| 6087 | pthread_mutex_unlock(&clone_lock); |
| 6088 | cpu_loop(env); |
| 6089 | /* never exits */ |
| 6090 | return NULL; |
| 6091 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6092 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 6093 | /* do_fork() Must return host values and target errnos (unlike most |
| 6094 | do_*() functions). */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6095 | static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6096 | abi_ulong parent_tidptr, target_ulong newtls, |
| 6097 | abi_ulong child_tidptr) |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6098 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 6099 | CPUState *cpu = env_cpu(env); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6100 | int ret; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 6101 | TaskState *ts; |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6102 | CPUState *new_cpu; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 6103 | CPUArchState *new_env; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6104 | sigset_t sigmask; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6105 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 6106 | flags &= ~CLONE_IGNORED_FLAGS; |
| 6107 | |
balrog | 436d124 | 2008-09-21 02:39:45 +0000 | [diff] [blame] | 6108 | /* Emulate vfork() with fork() */ |
| 6109 | if (flags & CLONE_VFORK) |
| 6110 | flags &= ~(CLONE_VFORK | CLONE_VM); |
| 6111 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6112 | if (flags & CLONE_VM) { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6113 | TaskState *parent_ts = (TaskState *)cpu->opaque; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6114 | new_thread_info info; |
| 6115 | pthread_attr_t attr; |
Peter Maydell | 24cb36a | 2013-07-16 18:45:00 +0100 | [diff] [blame] | 6116 | |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 6117 | if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) || |
| 6118 | (flags & CLONE_INVALID_THREAD_FLAGS)) { |
| 6119 | return -TARGET_EINVAL; |
| 6120 | } |
| 6121 | |
Markus Armbruster | c78d65e | 2015-09-14 13:53:03 +0200 | [diff] [blame] | 6122 | ts = g_new0(TaskState, 1); |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 6123 | init_task_state(ts); |
Max Filippov | 73a988d | 2018-03-30 06:19:58 -0700 | [diff] [blame] | 6124 | |
| 6125 | /* Grab a mutex so that thread setup appears atomic. */ |
| 6126 | pthread_mutex_lock(&clone_lock); |
| 6127 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6128 | /* we create a new CPU instance. */ |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 6129 | new_env = cpu_copy(env); |
pbrook | 6e68e07 | 2008-05-30 17:22:15 +0000 | [diff] [blame] | 6130 | /* Init regs that differ from the parent. */ |
Richard Henderson | 608999d | 2019-11-06 12:33:15 +0100 | [diff] [blame] | 6131 | cpu_clone_regs_child(new_env, newsp, flags); |
Richard Henderson | 07a6ecf | 2019-11-06 12:33:16 +0100 | [diff] [blame] | 6132 | cpu_clone_regs_parent(env, flags); |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 6133 | new_cpu = env_cpu(new_env); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6134 | new_cpu->opaque = ts; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 6135 | ts->bprm = parent_ts->bprm; |
| 6136 | ts->info = parent_ts->info; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 6137 | ts->signal_mask = parent_ts->signal_mask; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6138 | |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6139 | if (flags & CLONE_CHILD_CLEARTID) { |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6140 | ts->child_tidptr = child_tidptr; |
| 6141 | } |
| 6142 | |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6143 | if (flags & CLONE_SETTLS) { |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6144 | cpu_set_tls (new_env, newtls); |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6145 | } |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6146 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6147 | memset(&info, 0, sizeof(info)); |
| 6148 | pthread_mutex_init(&info.mutex, NULL); |
| 6149 | pthread_mutex_lock(&info.mutex); |
| 6150 | pthread_cond_init(&info.cond, NULL); |
| 6151 | info.env = new_env; |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6152 | if (flags & CLONE_CHILD_SETTID) { |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6153 | info.child_tidptr = child_tidptr; |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6154 | } |
| 6155 | if (flags & CLONE_PARENT_SETTID) { |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6156 | info.parent_tidptr = parent_tidptr; |
Peter Maydell | 7cfbd38 | 2016-08-02 18:41:26 +0100 | [diff] [blame] | 6157 | } |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6158 | |
| 6159 | ret = pthread_attr_init(&attr); |
Nathan Froyd | 48e15fc | 2010-10-29 07:48:57 -0700 | [diff] [blame] | 6160 | ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE); |
| 6161 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6162 | /* It is not safe to deliver signals until the child has finished |
| 6163 | initializing, so temporarily block all signals. */ |
| 6164 | sigfillset(&sigmask); |
| 6165 | sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask); |
Richard Henderson | 5ebdd77 | 2019-03-14 13:10:53 -0700 | [diff] [blame] | 6166 | cpu->random_seed = qemu_guest_random_seed_thread_part1(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6167 | |
Alex Bennée | b67cb68 | 2016-10-05 11:13:04 -0700 | [diff] [blame] | 6168 | /* If this is our first additional thread, we need to ensure we |
| 6169 | * generate code for parallel execution and flush old translations. |
| 6170 | */ |
| 6171 | if (!parallel_cpus) { |
| 6172 | parallel_cpus = true; |
| 6173 | tb_flush(cpu); |
| 6174 | } |
| 6175 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6176 | ret = pthread_create(&info.thread, &attr, clone_func, &info); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6177 | /* TODO: Free new CPU state if thread creation failed. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6178 | |
| 6179 | sigprocmask(SIG_SETMASK, &info.sigmask, NULL); |
| 6180 | pthread_attr_destroy(&attr); |
| 6181 | if (ret == 0) { |
| 6182 | /* Wait for the child to initialize. */ |
| 6183 | pthread_cond_wait(&info.cond, &info.mutex); |
| 6184 | ret = info.tid; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6185 | } else { |
| 6186 | ret = -1; |
| 6187 | } |
| 6188 | pthread_mutex_unlock(&info.mutex); |
| 6189 | pthread_cond_destroy(&info.cond); |
| 6190 | pthread_mutex_destroy(&info.mutex); |
| 6191 | pthread_mutex_unlock(&clone_lock); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6192 | } else { |
| 6193 | /* if no CLONE_VM, we consider it is a fork */ |
Peter Maydell | 5ea2fc8 | 2016-08-02 18:41:27 +0100 | [diff] [blame] | 6194 | if (flags & CLONE_INVALID_FORK_FLAGS) { |
| 6195 | return -TARGET_EINVAL; |
| 6196 | } |
| 6197 | |
| 6198 | /* We can't support custom termination signals */ |
| 6199 | if ((flags & CSIGNAL) != TARGET_SIGCHLD) { |
Timothy E Baldwin | 93b4eff | 2015-08-31 00:26:21 +0100 | [diff] [blame] | 6200 | return -TARGET_EINVAL; |
| 6201 | } |
Timothy E Baldwin | 7d92d34 | 2016-05-27 15:51:58 +0100 | [diff] [blame] | 6202 | |
| 6203 | if (block_signals()) { |
| 6204 | return -TARGET_ERESTARTSYS; |
| 6205 | } |
| 6206 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6207 | fork_start(); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6208 | ret = fork(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6209 | if (ret == 0) { |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 6210 | /* Child Process. */ |
Richard Henderson | 608999d | 2019-11-06 12:33:15 +0100 | [diff] [blame] | 6211 | cpu_clone_regs_child(env, newsp, flags); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6212 | fork_end(1); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 6213 | /* There is a race condition here. The parent process could |
| 6214 | theoretically read the TID in the child process before the child |
| 6215 | tid is set. This would require using either ptrace |
| 6216 | (not implemented) or having *_tidptr to point at a shared memory |
| 6217 | mapping. We can't repeat the spinlock hack used above because |
| 6218 | the child process gets its own copy of the lock. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6219 | if (flags & CLONE_CHILD_SETTID) |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 6220 | put_user_u32(sys_gettid(), child_tidptr); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6221 | if (flags & CLONE_PARENT_SETTID) |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 6222 | put_user_u32(sys_gettid(), parent_tidptr); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6223 | ts = (TaskState *)cpu->opaque; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6224 | if (flags & CLONE_SETTLS) |
| 6225 | cpu_set_tls (env, newtls); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6226 | if (flags & CLONE_CHILD_CLEARTID) |
| 6227 | ts->child_tidptr = child_tidptr; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6228 | } else { |
Richard Henderson | 07a6ecf | 2019-11-06 12:33:16 +0100 | [diff] [blame] | 6229 | cpu_clone_regs_parent(env, flags); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6230 | fork_end(0); |
| 6231 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6232 | } |
| 6233 | return ret; |
| 6234 | } |
| 6235 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6236 | /* warning : doesn't handle linux specific flags... */ |
| 6237 | static int target_to_host_fcntl_cmd(int cmd) |
| 6238 | { |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6239 | int ret; |
| 6240 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6241 | switch(cmd) { |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6242 | case TARGET_F_DUPFD: |
| 6243 | case TARGET_F_GETFD: |
| 6244 | case TARGET_F_SETFD: |
| 6245 | case TARGET_F_GETFL: |
| 6246 | case TARGET_F_SETFL: |
Andreas Schwab | 2d92c68 | 2020-05-25 09:59:28 +0200 | [diff] [blame] | 6247 | case TARGET_F_OFD_GETLK: |
| 6248 | case TARGET_F_OFD_SETLK: |
| 6249 | case TARGET_F_OFD_SETLKW: |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6250 | ret = cmd; |
| 6251 | break; |
| 6252 | case TARGET_F_GETLK: |
| 6253 | ret = F_GETLK64; |
| 6254 | break; |
| 6255 | case TARGET_F_SETLK: |
| 6256 | ret = F_SETLK64; |
| 6257 | break; |
| 6258 | case TARGET_F_SETLKW: |
| 6259 | ret = F_SETLKW64; |
| 6260 | break; |
| 6261 | case TARGET_F_GETOWN: |
| 6262 | ret = F_GETOWN; |
| 6263 | break; |
| 6264 | case TARGET_F_SETOWN: |
| 6265 | ret = F_SETOWN; |
| 6266 | break; |
| 6267 | case TARGET_F_GETSIG: |
| 6268 | ret = F_GETSIG; |
| 6269 | break; |
| 6270 | case TARGET_F_SETSIG: |
| 6271 | ret = F_SETSIG; |
| 6272 | break; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6273 | #if TARGET_ABI_BITS == 32 |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6274 | case TARGET_F_GETLK64: |
| 6275 | ret = F_GETLK64; |
| 6276 | break; |
| 6277 | case TARGET_F_SETLK64: |
| 6278 | ret = F_SETLK64; |
| 6279 | break; |
| 6280 | case TARGET_F_SETLKW64: |
| 6281 | ret = F_SETLKW64; |
| 6282 | break; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6283 | #endif |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6284 | case TARGET_F_SETLEASE: |
| 6285 | ret = F_SETLEASE; |
| 6286 | break; |
| 6287 | case TARGET_F_GETLEASE: |
| 6288 | ret = F_GETLEASE; |
| 6289 | break; |
malc | fbd5de9 | 2009-09-06 06:31:59 +0400 | [diff] [blame] | 6290 | #ifdef F_DUPFD_CLOEXEC |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6291 | case TARGET_F_DUPFD_CLOEXEC: |
| 6292 | ret = F_DUPFD_CLOEXEC; |
| 6293 | break; |
malc | fbd5de9 | 2009-09-06 06:31:59 +0400 | [diff] [blame] | 6294 | #endif |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6295 | case TARGET_F_NOTIFY: |
| 6296 | ret = F_NOTIFY; |
| 6297 | break; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6298 | #ifdef F_GETOWN_EX |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6299 | case TARGET_F_GETOWN_EX: |
| 6300 | ret = F_GETOWN_EX; |
| 6301 | break; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6302 | #endif |
| 6303 | #ifdef F_SETOWN_EX |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6304 | case TARGET_F_SETOWN_EX: |
| 6305 | ret = F_SETOWN_EX; |
| 6306 | break; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6307 | #endif |
Peter Maydell | ddf31aa | 2016-06-30 17:33:49 +0100 | [diff] [blame] | 6308 | #ifdef F_SETPIPE_SZ |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6309 | case TARGET_F_SETPIPE_SZ: |
| 6310 | ret = F_SETPIPE_SZ; |
| 6311 | break; |
| 6312 | case TARGET_F_GETPIPE_SZ: |
| 6313 | ret = F_GETPIPE_SZ; |
| 6314 | break; |
Peter Maydell | ddf31aa | 2016-06-30 17:33:49 +0100 | [diff] [blame] | 6315 | #endif |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6316 | default: |
| 6317 | ret = -TARGET_EINVAL; |
| 6318 | break; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6319 | } |
Shivaprasad G Bhat | 4a54576 | 2018-07-13 07:34:46 -0500 | [diff] [blame] | 6320 | |
| 6321 | #if defined(__powerpc64__) |
| 6322 | /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and |
| 6323 | * is not supported by kernel. The glibc fcntl call actually adjusts |
| 6324 | * them to 5, 6 and 7 before making the syscall(). Since we make the |
| 6325 | * syscall directly, adjust to what is supported by the kernel. |
| 6326 | */ |
| 6327 | if (ret >= F_GETLK64 && ret <= F_SETLKW64) { |
| 6328 | ret -= F_GETLK64 - 5; |
| 6329 | } |
| 6330 | #endif |
| 6331 | |
| 6332 | return ret; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6333 | } |
| 6334 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6335 | #define FLOCK_TRANSTBL \ |
| 6336 | switch (type) { \ |
| 6337 | TRANSTBL_CONVERT(F_RDLCK); \ |
| 6338 | TRANSTBL_CONVERT(F_WRLCK); \ |
| 6339 | TRANSTBL_CONVERT(F_UNLCK); \ |
| 6340 | TRANSTBL_CONVERT(F_EXLCK); \ |
| 6341 | TRANSTBL_CONVERT(F_SHLCK); \ |
| 6342 | } |
| 6343 | |
| 6344 | static int target_to_host_flock(int type) |
| 6345 | { |
| 6346 | #define TRANSTBL_CONVERT(a) case TARGET_##a: return a |
| 6347 | FLOCK_TRANSTBL |
| 6348 | #undef TRANSTBL_CONVERT |
| 6349 | return -TARGET_EINVAL; |
| 6350 | } |
| 6351 | |
| 6352 | static int host_to_target_flock(int type) |
| 6353 | { |
| 6354 | #define TRANSTBL_CONVERT(a) case a: return TARGET_##a |
| 6355 | FLOCK_TRANSTBL |
| 6356 | #undef TRANSTBL_CONVERT |
| 6357 | /* if we don't know how to convert the value coming |
| 6358 | * from the host we copy to the target field as-is |
| 6359 | */ |
| 6360 | return type; |
| 6361 | } |
Laurent Vivier | 2ba7f73 | 2013-01-10 21:42:48 +0100 | [diff] [blame] | 6362 | |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6363 | static inline abi_long copy_from_user_flock(struct flock64 *fl, |
| 6364 | abi_ulong target_flock_addr) |
| 6365 | { |
| 6366 | struct target_flock *target_fl; |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6367 | int l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6368 | |
| 6369 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 6370 | return -TARGET_EFAULT; |
| 6371 | } |
| 6372 | |
| 6373 | __get_user(l_type, &target_fl->l_type); |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6374 | l_type = target_to_host_flock(l_type); |
| 6375 | if (l_type < 0) { |
| 6376 | return l_type; |
| 6377 | } |
| 6378 | fl->l_type = l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6379 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 6380 | __get_user(fl->l_start, &target_fl->l_start); |
| 6381 | __get_user(fl->l_len, &target_fl->l_len); |
| 6382 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 6383 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 6384 | return 0; |
| 6385 | } |
| 6386 | |
| 6387 | static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr, |
| 6388 | const struct flock64 *fl) |
| 6389 | { |
| 6390 | struct target_flock *target_fl; |
| 6391 | short l_type; |
| 6392 | |
| 6393 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 6394 | return -TARGET_EFAULT; |
| 6395 | } |
| 6396 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6397 | l_type = host_to_target_flock(fl->l_type); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6398 | __put_user(l_type, &target_fl->l_type); |
| 6399 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 6400 | __put_user(fl->l_start, &target_fl->l_start); |
| 6401 | __put_user(fl->l_len, &target_fl->l_len); |
| 6402 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 6403 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 6404 | return 0; |
| 6405 | } |
| 6406 | |
| 6407 | typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr); |
| 6408 | typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl); |
| 6409 | |
| 6410 | #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32 |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6411 | static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl, |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6412 | abi_ulong target_flock_addr) |
| 6413 | { |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6414 | struct target_oabi_flock64 *target_fl; |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6415 | int l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6416 | |
| 6417 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 6418 | return -TARGET_EFAULT; |
| 6419 | } |
| 6420 | |
| 6421 | __get_user(l_type, &target_fl->l_type); |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6422 | l_type = target_to_host_flock(l_type); |
| 6423 | if (l_type < 0) { |
| 6424 | return l_type; |
| 6425 | } |
| 6426 | fl->l_type = l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6427 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 6428 | __get_user(fl->l_start, &target_fl->l_start); |
| 6429 | __get_user(fl->l_len, &target_fl->l_len); |
| 6430 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 6431 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 6432 | return 0; |
| 6433 | } |
| 6434 | |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6435 | static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr, |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6436 | const struct flock64 *fl) |
| 6437 | { |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 6438 | struct target_oabi_flock64 *target_fl; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6439 | short l_type; |
| 6440 | |
| 6441 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 6442 | return -TARGET_EFAULT; |
| 6443 | } |
| 6444 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6445 | l_type = host_to_target_flock(fl->l_type); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6446 | __put_user(l_type, &target_fl->l_type); |
| 6447 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 6448 | __put_user(fl->l_start, &target_fl->l_start); |
| 6449 | __put_user(fl->l_len, &target_fl->l_len); |
| 6450 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 6451 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 6452 | return 0; |
| 6453 | } |
| 6454 | #endif |
| 6455 | |
| 6456 | static inline abi_long copy_from_user_flock64(struct flock64 *fl, |
| 6457 | abi_ulong target_flock_addr) |
| 6458 | { |
| 6459 | struct target_flock64 *target_fl; |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6460 | int l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6461 | |
| 6462 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 6463 | return -TARGET_EFAULT; |
| 6464 | } |
| 6465 | |
| 6466 | __get_user(l_type, &target_fl->l_type); |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6467 | l_type = target_to_host_flock(l_type); |
| 6468 | if (l_type < 0) { |
| 6469 | return l_type; |
| 6470 | } |
| 6471 | fl->l_type = l_type; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6472 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 6473 | __get_user(fl->l_start, &target_fl->l_start); |
| 6474 | __get_user(fl->l_len, &target_fl->l_len); |
| 6475 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 6476 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 6477 | return 0; |
| 6478 | } |
| 6479 | |
| 6480 | static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr, |
| 6481 | const struct flock64 *fl) |
| 6482 | { |
| 6483 | struct target_flock64 *target_fl; |
| 6484 | short l_type; |
| 6485 | |
| 6486 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 6487 | return -TARGET_EFAULT; |
| 6488 | } |
| 6489 | |
Laurent Vivier | ae68ad9 | 2018-05-10 01:11:21 +0200 | [diff] [blame] | 6490 | l_type = host_to_target_flock(fl->l_type); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6491 | __put_user(l_type, &target_fl->l_type); |
| 6492 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 6493 | __put_user(fl->l_start, &target_fl->l_start); |
| 6494 | __put_user(fl->l_len, &target_fl->l_len); |
| 6495 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 6496 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 6497 | return 0; |
| 6498 | } |
| 6499 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6500 | static abi_long do_fcntl(int fd, int cmd, abi_ulong arg) |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6501 | { |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 6502 | struct flock64 fl64; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6503 | #ifdef F_GETOWN_EX |
| 6504 | struct f_owner_ex fox; |
| 6505 | struct target_f_owner_ex *target_fox; |
| 6506 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6507 | abi_long ret; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6508 | int host_cmd = target_to_host_fcntl_cmd(cmd); |
| 6509 | |
| 6510 | if (host_cmd == -TARGET_EINVAL) |
| 6511 | return host_cmd; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6512 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6513 | switch(cmd) { |
| 6514 | case TARGET_F_GETLK: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6515 | ret = copy_from_user_flock(&fl64, arg); |
| 6516 | if (ret) { |
| 6517 | return ret; |
| 6518 | } |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6519 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6520 | if (ret == 0) { |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6521 | ret = copy_to_user_flock(arg, &fl64); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6522 | } |
| 6523 | break; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6524 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6525 | case TARGET_F_SETLK: |
| 6526 | case TARGET_F_SETLKW: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6527 | ret = copy_from_user_flock(&fl64, arg); |
| 6528 | if (ret) { |
| 6529 | return ret; |
| 6530 | } |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6531 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6532 | break; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6533 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6534 | case TARGET_F_GETLK64: |
Andreas Schwab | 2d92c68 | 2020-05-25 09:59:28 +0200 | [diff] [blame] | 6535 | case TARGET_F_OFD_GETLK: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6536 | ret = copy_from_user_flock64(&fl64, arg); |
| 6537 | if (ret) { |
| 6538 | return ret; |
| 6539 | } |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6540 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 6541 | if (ret == 0) { |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6542 | ret = copy_to_user_flock64(arg, &fl64); |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 6543 | } |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 6544 | break; |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6545 | case TARGET_F_SETLK64: |
| 6546 | case TARGET_F_SETLKW64: |
Andreas Schwab | 2d92c68 | 2020-05-25 09:59:28 +0200 | [diff] [blame] | 6547 | case TARGET_F_OFD_SETLK: |
| 6548 | case TARGET_F_OFD_SETLKW: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6549 | ret = copy_from_user_flock64(&fl64, arg); |
| 6550 | if (ret) { |
| 6551 | return ret; |
| 6552 | } |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6553 | ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6554 | break; |
| 6555 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6556 | case TARGET_F_GETFL: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6557 | ret = get_errno(safe_fcntl(fd, host_cmd, arg)); |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 6558 | if (ret >= 0) { |
| 6559 | ret = host_to_target_bitmask(ret, fcntl_flags_tbl); |
| 6560 | } |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 6561 | break; |
| 6562 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6563 | case TARGET_F_SETFL: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6564 | ret = get_errno(safe_fcntl(fd, host_cmd, |
| 6565 | target_to_host_bitmask(arg, |
| 6566 | fcntl_flags_tbl))); |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6567 | break; |
| 6568 | |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6569 | #ifdef F_GETOWN_EX |
| 6570 | case TARGET_F_GETOWN_EX: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6571 | ret = get_errno(safe_fcntl(fd, host_cmd, &fox)); |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6572 | if (ret >= 0) { |
| 6573 | if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0)) |
| 6574 | return -TARGET_EFAULT; |
| 6575 | target_fox->type = tswap32(fox.type); |
| 6576 | target_fox->pid = tswap32(fox.pid); |
| 6577 | unlock_user_struct(target_fox, arg, 1); |
| 6578 | } |
| 6579 | break; |
| 6580 | #endif |
| 6581 | |
| 6582 | #ifdef F_SETOWN_EX |
| 6583 | case TARGET_F_SETOWN_EX: |
| 6584 | if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1)) |
| 6585 | return -TARGET_EFAULT; |
| 6586 | fox.type = tswap32(target_fox->type); |
| 6587 | fox.pid = tswap32(target_fox->pid); |
| 6588 | unlock_user_struct(target_fox, arg, 0); |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6589 | ret = get_errno(safe_fcntl(fd, host_cmd, &fox)); |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 6590 | break; |
| 6591 | #endif |
| 6592 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 6593 | case TARGET_F_SETOWN: |
| 6594 | case TARGET_F_GETOWN: |
| 6595 | case TARGET_F_SETSIG: |
| 6596 | case TARGET_F_GETSIG: |
Ulrich Hecht | 7e22e54 | 2009-07-24 19:10:27 +0200 | [diff] [blame] | 6597 | case TARGET_F_SETLEASE: |
| 6598 | case TARGET_F_GETLEASE: |
Peter Maydell | 7e3b92e | 2016-06-20 15:50:37 +0100 | [diff] [blame] | 6599 | case TARGET_F_SETPIPE_SZ: |
| 6600 | case TARGET_F_GETPIPE_SZ: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6601 | ret = get_errno(safe_fcntl(fd, host_cmd, arg)); |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 6602 | break; |
| 6603 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6604 | default: |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 6605 | ret = get_errno(safe_fcntl(fd, cmd, arg)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6606 | break; |
| 6607 | } |
| 6608 | return ret; |
| 6609 | } |
| 6610 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 6611 | #ifdef USE_UID16 |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 6612 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 6613 | static inline int high2lowuid(int uid) |
| 6614 | { |
| 6615 | if (uid > 65535) |
| 6616 | return 65534; |
| 6617 | else |
| 6618 | return uid; |
| 6619 | } |
| 6620 | |
| 6621 | static inline int high2lowgid(int gid) |
| 6622 | { |
| 6623 | if (gid > 65535) |
| 6624 | return 65534; |
| 6625 | else |
| 6626 | return gid; |
| 6627 | } |
| 6628 | |
| 6629 | static inline int low2highuid(int uid) |
| 6630 | { |
| 6631 | if ((int16_t)uid == -1) |
| 6632 | return -1; |
| 6633 | else |
| 6634 | return uid; |
| 6635 | } |
| 6636 | |
| 6637 | static inline int low2highgid(int gid) |
| 6638 | { |
| 6639 | if ((int16_t)gid == -1) |
| 6640 | return -1; |
| 6641 | else |
| 6642 | return gid; |
| 6643 | } |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 6644 | static inline int tswapid(int id) |
| 6645 | { |
| 6646 | return tswap16(id); |
| 6647 | } |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 6648 | |
| 6649 | #define put_user_id(x, gaddr) put_user_u16(x, gaddr) |
| 6650 | |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 6651 | #else /* !USE_UID16 */ |
| 6652 | static inline int high2lowuid(int uid) |
| 6653 | { |
| 6654 | return uid; |
| 6655 | } |
| 6656 | static inline int high2lowgid(int gid) |
| 6657 | { |
| 6658 | return gid; |
| 6659 | } |
| 6660 | static inline int low2highuid(int uid) |
| 6661 | { |
| 6662 | return uid; |
| 6663 | } |
| 6664 | static inline int low2highgid(int gid) |
| 6665 | { |
| 6666 | return gid; |
| 6667 | } |
| 6668 | static inline int tswapid(int id) |
| 6669 | { |
| 6670 | return tswap32(id); |
| 6671 | } |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 6672 | |
| 6673 | #define put_user_id(x, gaddr) put_user_u32(x, gaddr) |
| 6674 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 6675 | #endif /* USE_UID16 */ |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 6676 | |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 6677 | /* We must do direct syscalls for setting UID/GID, because we want to |
| 6678 | * implement the Linux system call semantics of "change only for this thread", |
| 6679 | * not the libc/POSIX semantics of "change for all threads in process". |
| 6680 | * (See http://ewontfix.com/17/ for more details.) |
| 6681 | * We use the 32-bit version of the syscalls if present; if it is not |
| 6682 | * then either the host architecture supports 32-bit UIDs natively with |
| 6683 | * the standard syscall, or the 16-bit UID is the best we can do. |
| 6684 | */ |
| 6685 | #ifdef __NR_setuid32 |
| 6686 | #define __NR_sys_setuid __NR_setuid32 |
| 6687 | #else |
| 6688 | #define __NR_sys_setuid __NR_setuid |
| 6689 | #endif |
| 6690 | #ifdef __NR_setgid32 |
| 6691 | #define __NR_sys_setgid __NR_setgid32 |
| 6692 | #else |
| 6693 | #define __NR_sys_setgid __NR_setgid |
| 6694 | #endif |
| 6695 | #ifdef __NR_setresuid32 |
| 6696 | #define __NR_sys_setresuid __NR_setresuid32 |
| 6697 | #else |
| 6698 | #define __NR_sys_setresuid __NR_setresuid |
| 6699 | #endif |
| 6700 | #ifdef __NR_setresgid32 |
| 6701 | #define __NR_sys_setresgid __NR_setresgid32 |
| 6702 | #else |
| 6703 | #define __NR_sys_setresgid __NR_setresgid |
| 6704 | #endif |
| 6705 | |
| 6706 | _syscall1(int, sys_setuid, uid_t, uid) |
| 6707 | _syscall1(int, sys_setgid, gid_t, gid) |
| 6708 | _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid) |
| 6709 | _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid) |
| 6710 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6711 | void syscall_init(void) |
| 6712 | { |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6713 | IOCTLEntry *ie; |
| 6714 | const argtype *arg_type; |
| 6715 | int size; |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 6716 | int i; |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6717 | |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 6718 | thunk_init(STRUCT_MAX); |
| 6719 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 6720 | #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6721 | #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] | 6722 | #include "syscall_types.h" |
| 6723 | #undef STRUCT |
| 6724 | #undef STRUCT_SPECIAL |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6725 | |
Peter Maydell | dd6e957 | 2012-07-23 08:07:22 +0000 | [diff] [blame] | 6726 | /* Build target_to_host_errno_table[] table from |
| 6727 | * host_to_target_errno_table[]. */ |
| 6728 | for (i = 0; i < ERRNO_TABLE_SIZE; i++) { |
| 6729 | target_to_host_errno_table[host_to_target_errno_table[i]] = i; |
| 6730 | } |
| 6731 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6732 | /* we patch the ioctl size if necessary. We rely on the fact that |
| 6733 | no ioctl has all the bits at '1' in the size field */ |
| 6734 | ie = ioctl_entries; |
| 6735 | while (ie->target_cmd != 0) { |
| 6736 | if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) == |
| 6737 | TARGET_IOC_SIZEMASK) { |
| 6738 | arg_type = ie->arg_type; |
| 6739 | if (arg_type[0] != TYPE_PTR) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6740 | fprintf(stderr, "cannot patch size for ioctl 0x%x\n", |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6741 | ie->target_cmd); |
| 6742 | exit(1); |
| 6743 | } |
| 6744 | arg_type++; |
| 6745 | size = thunk_type_size(arg_type, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6746 | ie->target_cmd = (ie->target_cmd & |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6747 | ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) | |
| 6748 | (size << TARGET_IOC_SIZESHIFT); |
| 6749 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 6750 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6751 | /* automatic consistency check if same arch */ |
balrog | 872ea0c | 2008-09-21 02:31:19 +0000 | [diff] [blame] | 6752 | #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \ |
| 6753 | (defined(__x86_64__) && defined(TARGET_X86_64)) |
| 6754 | if (unlikely(ie->target_cmd != ie->host_cmd)) { |
| 6755 | fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n", |
| 6756 | ie->name, ie->target_cmd, ie->host_cmd); |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 6757 | } |
| 6758 | #endif |
| 6759 | ie++; |
| 6760 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6761 | } |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 6762 | |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6763 | #ifdef TARGET_NR_truncate64 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6764 | static inline abi_long target_truncate64(void *cpu_env, const char *arg1, |
| 6765 | abi_long arg2, |
| 6766 | abi_long arg3, |
| 6767 | abi_long arg4) |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6768 | { |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 6769 | if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) { |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6770 | arg2 = arg3; |
| 6771 | arg3 = arg4; |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 6772 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6773 | return get_errno(truncate64(arg1, target_offset64(arg2, arg3))); |
| 6774 | } |
| 6775 | #endif |
| 6776 | |
| 6777 | #ifdef TARGET_NR_ftruncate64 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6778 | static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1, |
| 6779 | abi_long arg2, |
| 6780 | abi_long arg3, |
| 6781 | abi_long arg4) |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6782 | { |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 6783 | if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) { |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6784 | arg2 = arg3; |
| 6785 | arg3 = arg4; |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 6786 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6787 | return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3))); |
| 6788 | } |
| 6789 | #endif |
| 6790 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 6791 | #if defined(TARGET_NR_timer_settime) || \ |
| 6792 | (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)) |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 6793 | static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec, |
| 6794 | abi_ulong target_addr) |
| 6795 | { |
| 6796 | struct target_itimerspec *target_itspec; |
| 6797 | |
| 6798 | if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) { |
| 6799 | return -TARGET_EFAULT; |
| 6800 | } |
| 6801 | |
| 6802 | host_itspec->it_interval.tv_sec = |
| 6803 | tswapal(target_itspec->it_interval.tv_sec); |
| 6804 | host_itspec->it_interval.tv_nsec = |
| 6805 | tswapal(target_itspec->it_interval.tv_nsec); |
| 6806 | host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec); |
| 6807 | host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec); |
| 6808 | |
| 6809 | unlock_user_struct(target_itspec, target_addr, 1); |
| 6810 | return 0; |
| 6811 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 6812 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 6813 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 6814 | #if ((defined(TARGET_NR_timerfd_gettime) || \ |
| 6815 | defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \ |
| 6816 | defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime) |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 6817 | static inline abi_long host_to_target_itimerspec(abi_ulong target_addr, |
| 6818 | struct itimerspec *host_its) |
| 6819 | { |
| 6820 | struct target_itimerspec *target_itspec; |
| 6821 | |
| 6822 | if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) { |
| 6823 | return -TARGET_EFAULT; |
| 6824 | } |
| 6825 | |
| 6826 | target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec); |
| 6827 | target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec); |
| 6828 | |
| 6829 | target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec); |
| 6830 | target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec); |
| 6831 | |
| 6832 | unlock_user_struct(target_itspec, target_addr, 0); |
| 6833 | return 0; |
| 6834 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 6835 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 6836 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 6837 | #if defined(TARGET_NR_adjtimex) || \ |
| 6838 | (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)) |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 6839 | static inline abi_long target_to_host_timex(struct timex *host_tx, |
| 6840 | abi_long target_addr) |
| 6841 | { |
| 6842 | struct target_timex *target_tx; |
| 6843 | |
| 6844 | if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) { |
| 6845 | return -TARGET_EFAULT; |
| 6846 | } |
| 6847 | |
| 6848 | __get_user(host_tx->modes, &target_tx->modes); |
| 6849 | __get_user(host_tx->offset, &target_tx->offset); |
| 6850 | __get_user(host_tx->freq, &target_tx->freq); |
| 6851 | __get_user(host_tx->maxerror, &target_tx->maxerror); |
| 6852 | __get_user(host_tx->esterror, &target_tx->esterror); |
| 6853 | __get_user(host_tx->status, &target_tx->status); |
| 6854 | __get_user(host_tx->constant, &target_tx->constant); |
| 6855 | __get_user(host_tx->precision, &target_tx->precision); |
| 6856 | __get_user(host_tx->tolerance, &target_tx->tolerance); |
| 6857 | __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec); |
| 6858 | __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec); |
| 6859 | __get_user(host_tx->tick, &target_tx->tick); |
| 6860 | __get_user(host_tx->ppsfreq, &target_tx->ppsfreq); |
| 6861 | __get_user(host_tx->jitter, &target_tx->jitter); |
| 6862 | __get_user(host_tx->shift, &target_tx->shift); |
| 6863 | __get_user(host_tx->stabil, &target_tx->stabil); |
| 6864 | __get_user(host_tx->jitcnt, &target_tx->jitcnt); |
| 6865 | __get_user(host_tx->calcnt, &target_tx->calcnt); |
| 6866 | __get_user(host_tx->errcnt, &target_tx->errcnt); |
| 6867 | __get_user(host_tx->stbcnt, &target_tx->stbcnt); |
| 6868 | __get_user(host_tx->tai, &target_tx->tai); |
| 6869 | |
| 6870 | unlock_user_struct(target_tx, target_addr, 0); |
| 6871 | return 0; |
| 6872 | } |
| 6873 | |
| 6874 | static inline abi_long host_to_target_timex(abi_long target_addr, |
| 6875 | struct timex *host_tx) |
| 6876 | { |
| 6877 | struct target_timex *target_tx; |
| 6878 | |
| 6879 | if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) { |
| 6880 | return -TARGET_EFAULT; |
| 6881 | } |
| 6882 | |
| 6883 | __put_user(host_tx->modes, &target_tx->modes); |
| 6884 | __put_user(host_tx->offset, &target_tx->offset); |
| 6885 | __put_user(host_tx->freq, &target_tx->freq); |
| 6886 | __put_user(host_tx->maxerror, &target_tx->maxerror); |
| 6887 | __put_user(host_tx->esterror, &target_tx->esterror); |
| 6888 | __put_user(host_tx->status, &target_tx->status); |
| 6889 | __put_user(host_tx->constant, &target_tx->constant); |
| 6890 | __put_user(host_tx->precision, &target_tx->precision); |
| 6891 | __put_user(host_tx->tolerance, &target_tx->tolerance); |
| 6892 | __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec); |
| 6893 | __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec); |
| 6894 | __put_user(host_tx->tick, &target_tx->tick); |
| 6895 | __put_user(host_tx->ppsfreq, &target_tx->ppsfreq); |
| 6896 | __put_user(host_tx->jitter, &target_tx->jitter); |
| 6897 | __put_user(host_tx->shift, &target_tx->shift); |
| 6898 | __put_user(host_tx->stabil, &target_tx->stabil); |
| 6899 | __put_user(host_tx->jitcnt, &target_tx->jitcnt); |
| 6900 | __put_user(host_tx->calcnt, &target_tx->calcnt); |
| 6901 | __put_user(host_tx->errcnt, &target_tx->errcnt); |
| 6902 | __put_user(host_tx->stbcnt, &target_tx->stbcnt); |
| 6903 | __put_user(host_tx->tai, &target_tx->tai); |
| 6904 | |
| 6905 | unlock_user_struct(target_tx, target_addr, 1); |
| 6906 | return 0; |
| 6907 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 6908 | #endif |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 6909 | |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 6910 | static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp, |
| 6911 | abi_ulong target_addr) |
| 6912 | { |
| 6913 | struct target_sigevent *target_sevp; |
| 6914 | |
| 6915 | if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) { |
| 6916 | return -TARGET_EFAULT; |
| 6917 | } |
| 6918 | |
| 6919 | /* This union is awkward on 64 bit systems because it has a 32 bit |
| 6920 | * integer and a pointer in it; we follow the conversion approach |
| 6921 | * used for handling sigval types in signal.c so the guest should get |
| 6922 | * the correct value back even if we did a 64 bit byteswap and it's |
| 6923 | * using the 32 bit integer. |
| 6924 | */ |
| 6925 | host_sevp->sigev_value.sival_ptr = |
| 6926 | (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr); |
| 6927 | host_sevp->sigev_signo = |
| 6928 | target_to_host_signal(tswap32(target_sevp->sigev_signo)); |
| 6929 | host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify); |
| 6930 | host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid); |
| 6931 | |
| 6932 | unlock_user_struct(target_sevp, target_addr, 1); |
| 6933 | return 0; |
| 6934 | } |
| 6935 | |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 6936 | #if defined(TARGET_NR_mlockall) |
| 6937 | static inline int target_to_host_mlockall_arg(int arg) |
| 6938 | { |
| 6939 | int result = 0; |
| 6940 | |
| 6941 | if (arg & TARGET_MLOCKALL_MCL_CURRENT) { |
| 6942 | result |= MCL_CURRENT; |
| 6943 | } |
| 6944 | if (arg & TARGET_MLOCKALL_MCL_FUTURE) { |
| 6945 | result |= MCL_FUTURE; |
| 6946 | } |
| 6947 | return result; |
| 6948 | } |
| 6949 | #endif |
| 6950 | |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 6951 | #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \ |
| 6952 | defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \ |
| 6953 | defined(TARGET_NR_newfstatat)) |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6954 | static inline abi_long host_to_target_stat64(void *cpu_env, |
| 6955 | abi_ulong target_addr, |
| 6956 | struct stat *host_st) |
| 6957 | { |
Alexander Graf | 0970119 | 2013-09-03 20:12:15 +0100 | [diff] [blame] | 6958 | #if defined(TARGET_ARM) && defined(TARGET_ABI32) |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6959 | if (((CPUARMState *)cpu_env)->eabi) { |
| 6960 | struct target_eabi_stat64 *target_st; |
| 6961 | |
| 6962 | if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) |
| 6963 | return -TARGET_EFAULT; |
| 6964 | memset(target_st, 0, sizeof(struct target_eabi_stat64)); |
| 6965 | __put_user(host_st->st_dev, &target_st->st_dev); |
| 6966 | __put_user(host_st->st_ino, &target_st->st_ino); |
| 6967 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
| 6968 | __put_user(host_st->st_ino, &target_st->__st_ino); |
| 6969 | #endif |
| 6970 | __put_user(host_st->st_mode, &target_st->st_mode); |
| 6971 | __put_user(host_st->st_nlink, &target_st->st_nlink); |
| 6972 | __put_user(host_st->st_uid, &target_st->st_uid); |
| 6973 | __put_user(host_st->st_gid, &target_st->st_gid); |
| 6974 | __put_user(host_st->st_rdev, &target_st->st_rdev); |
| 6975 | __put_user(host_st->st_size, &target_st->st_size); |
| 6976 | __put_user(host_st->st_blksize, &target_st->st_blksize); |
| 6977 | __put_user(host_st->st_blocks, &target_st->st_blocks); |
| 6978 | __put_user(host_st->st_atime, &target_st->target_st_atime); |
| 6979 | __put_user(host_st->st_mtime, &target_st->target_st_mtime); |
| 6980 | __put_user(host_st->st_ctime, &target_st->target_st_ctime); |
Chen-Yu Tsai | 5f992db | 2019-05-23 00:21:47 +0800 | [diff] [blame] | 6981 | #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700 |
| 6982 | __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec); |
| 6983 | __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec); |
| 6984 | __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec); |
| 6985 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6986 | unlock_user_struct(target_st, target_addr, 1); |
| 6987 | } else |
| 6988 | #endif |
| 6989 | { |
Stefan Weil | 20d155b | 2013-10-30 22:52:24 +0100 | [diff] [blame] | 6990 | #if defined(TARGET_HAS_STRUCT_STAT64) |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6991 | struct target_stat64 *target_st; |
Stefan Weil | 20d155b | 2013-10-30 22:52:24 +0100 | [diff] [blame] | 6992 | #else |
| 6993 | struct target_stat *target_st; |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 6994 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6995 | |
| 6996 | if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) |
| 6997 | return -TARGET_EFAULT; |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 6998 | memset(target_st, 0, sizeof(*target_st)); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6999 | __put_user(host_st->st_dev, &target_st->st_dev); |
| 7000 | __put_user(host_st->st_ino, &target_st->st_ino); |
| 7001 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
| 7002 | __put_user(host_st->st_ino, &target_st->__st_ino); |
| 7003 | #endif |
| 7004 | __put_user(host_st->st_mode, &target_st->st_mode); |
| 7005 | __put_user(host_st->st_nlink, &target_st->st_nlink); |
| 7006 | __put_user(host_st->st_uid, &target_st->st_uid); |
| 7007 | __put_user(host_st->st_gid, &target_st->st_gid); |
| 7008 | __put_user(host_st->st_rdev, &target_st->st_rdev); |
| 7009 | /* XXX: better use of kernel struct */ |
| 7010 | __put_user(host_st->st_size, &target_st->st_size); |
| 7011 | __put_user(host_st->st_blksize, &target_st->st_blksize); |
| 7012 | __put_user(host_st->st_blocks, &target_st->st_blocks); |
| 7013 | __put_user(host_st->st_atime, &target_st->target_st_atime); |
| 7014 | __put_user(host_st->st_mtime, &target_st->target_st_mtime); |
| 7015 | __put_user(host_st->st_ctime, &target_st->target_st_ctime); |
Chen-Yu Tsai | 5f992db | 2019-05-23 00:21:47 +0800 | [diff] [blame] | 7016 | #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700 |
| 7017 | __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec); |
| 7018 | __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec); |
| 7019 | __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec); |
| 7020 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7021 | unlock_user_struct(target_st, target_addr, 1); |
| 7022 | } |
| 7023 | |
| 7024 | return 0; |
| 7025 | } |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 7026 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 7027 | |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 7028 | #if defined(TARGET_NR_statx) && defined(__NR_statx) |
| 7029 | static inline abi_long host_to_target_statx(struct target_statx *host_stx, |
| 7030 | abi_ulong target_addr) |
| 7031 | { |
| 7032 | struct target_statx *target_stx; |
| 7033 | |
| 7034 | if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) { |
| 7035 | return -TARGET_EFAULT; |
| 7036 | } |
| 7037 | memset(target_stx, 0, sizeof(*target_stx)); |
| 7038 | |
| 7039 | __put_user(host_stx->stx_mask, &target_stx->stx_mask); |
| 7040 | __put_user(host_stx->stx_blksize, &target_stx->stx_blksize); |
| 7041 | __put_user(host_stx->stx_attributes, &target_stx->stx_attributes); |
| 7042 | __put_user(host_stx->stx_nlink, &target_stx->stx_nlink); |
| 7043 | __put_user(host_stx->stx_uid, &target_stx->stx_uid); |
| 7044 | __put_user(host_stx->stx_gid, &target_stx->stx_gid); |
| 7045 | __put_user(host_stx->stx_mode, &target_stx->stx_mode); |
| 7046 | __put_user(host_stx->stx_ino, &target_stx->stx_ino); |
| 7047 | __put_user(host_stx->stx_size, &target_stx->stx_size); |
| 7048 | __put_user(host_stx->stx_blocks, &target_stx->stx_blocks); |
| 7049 | __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask); |
| 7050 | __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec); |
| 7051 | __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] | 7052 | __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec); |
| 7053 | __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec); |
| 7054 | __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec); |
| 7055 | __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec); |
| 7056 | __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec); |
| 7057 | __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] | 7058 | __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major); |
| 7059 | __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor); |
| 7060 | __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major); |
| 7061 | __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor); |
| 7062 | |
| 7063 | unlock_user_struct(target_stx, target_addr, 1); |
| 7064 | |
| 7065 | return 0; |
| 7066 | } |
| 7067 | #endif |
| 7068 | |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7069 | static int do_sys_futex(int *uaddr, int op, int val, |
| 7070 | const struct timespec *timeout, int *uaddr2, |
| 7071 | int val3) |
| 7072 | { |
| 7073 | #if HOST_LONG_BITS == 64 |
| 7074 | #if defined(__NR_futex) |
| 7075 | /* always a 64-bit time_t, it doesn't define _time64 version */ |
| 7076 | return sys_futex(uaddr, op, val, timeout, uaddr2, val3); |
| 7077 | |
| 7078 | #endif |
| 7079 | #else /* HOST_LONG_BITS == 64 */ |
| 7080 | #if defined(__NR_futex_time64) |
| 7081 | if (sizeof(timeout->tv_sec) == 8) { |
| 7082 | /* _time64 function on 32bit arch */ |
| 7083 | return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3); |
| 7084 | } |
| 7085 | #endif |
| 7086 | #if defined(__NR_futex) |
| 7087 | /* old function on 32bit arch */ |
| 7088 | return sys_futex(uaddr, op, val, timeout, uaddr2, val3); |
| 7089 | #endif |
| 7090 | #endif /* HOST_LONG_BITS == 64 */ |
| 7091 | g_assert_not_reached(); |
| 7092 | } |
| 7093 | |
| 7094 | static int do_safe_futex(int *uaddr, int op, int val, |
| 7095 | const struct timespec *timeout, int *uaddr2, |
| 7096 | int val3) |
| 7097 | { |
| 7098 | #if HOST_LONG_BITS == 64 |
| 7099 | #if defined(__NR_futex) |
| 7100 | /* always a 64-bit time_t, it doesn't define _time64 version */ |
| 7101 | return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3)); |
| 7102 | #endif |
| 7103 | #else /* HOST_LONG_BITS == 64 */ |
| 7104 | #if defined(__NR_futex_time64) |
| 7105 | if (sizeof(timeout->tv_sec) == 8) { |
| 7106 | /* _time64 function on 32bit arch */ |
| 7107 | return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2, |
| 7108 | val3)); |
| 7109 | } |
| 7110 | #endif |
| 7111 | #if defined(__NR_futex) |
| 7112 | /* old function on 32bit arch */ |
| 7113 | return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3)); |
| 7114 | #endif |
| 7115 | #endif /* HOST_LONG_BITS == 64 */ |
| 7116 | return -TARGET_ENOSYS; |
| 7117 | } |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 7118 | |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7119 | /* ??? Using host futex calls even when target atomic operations |
| 7120 | are not really atomic probably breaks things. However implementing |
| 7121 | futexes locally would make futexes shared between multiple processes |
| 7122 | tricky. However they're probably useless because guest atomic |
| 7123 | operations won't work either. */ |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7124 | #if defined(TARGET_NR_futex) |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 7125 | static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout, |
| 7126 | target_ulong uaddr2, int val3) |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7127 | { |
| 7128 | struct timespec ts, *pts; |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7129 | int base_op; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7130 | |
| 7131 | /* ??? We assume FUTEX_* constants are the same on both host |
| 7132 | and target. */ |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 7133 | #ifdef FUTEX_CMD_MASK |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7134 | base_op = op & FUTEX_CMD_MASK; |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 7135 | #else |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7136 | base_op = op; |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 7137 | #endif |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7138 | switch (base_op) { |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7139 | case FUTEX_WAIT: |
John Rigby | cce246e | 2013-02-23 16:14:07 -0700 | [diff] [blame] | 7140 | case FUTEX_WAIT_BITSET: |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7141 | if (timeout) { |
| 7142 | pts = &ts; |
| 7143 | target_to_host_timespec(pts, timeout); |
| 7144 | } else { |
| 7145 | pts = NULL; |
| 7146 | } |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7147 | return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7148 | case FUTEX_WAKE: |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7149 | return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7150 | case FUTEX_FD: |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7151 | return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7152 | case FUTEX_REQUEUE: |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7153 | case FUTEX_CMP_REQUEUE: |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 7154 | case FUTEX_WAKE_OP: |
| 7155 | /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the |
| 7156 | TIMEOUT parameter is interpreted as a uint32_t by the kernel. |
| 7157 | But the prototype takes a `struct timespec *'; insert casts |
| 7158 | to satisfy the compiler. We do not need to tswap TIMEOUT |
| 7159 | since it's not compared to guest memory. */ |
| 7160 | pts = (struct timespec *)(uintptr_t) timeout; |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7161 | return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2), |
| 7162 | (base_op == FUTEX_CMP_REQUEUE |
| 7163 | ? tswap32(val3) |
| 7164 | : val3)); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7165 | default: |
| 7166 | return -TARGET_ENOSYS; |
| 7167 | } |
| 7168 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 7169 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7170 | |
| 7171 | #if defined(TARGET_NR_futex_time64) |
| 7172 | static int do_futex_time64(target_ulong uaddr, int op, int val, target_ulong timeout, |
| 7173 | target_ulong uaddr2, int val3) |
| 7174 | { |
| 7175 | struct timespec ts, *pts; |
| 7176 | int base_op; |
| 7177 | |
| 7178 | /* ??? We assume FUTEX_* constants are the same on both host |
| 7179 | and target. */ |
| 7180 | #ifdef FUTEX_CMD_MASK |
| 7181 | base_op = op & FUTEX_CMD_MASK; |
| 7182 | #else |
| 7183 | base_op = op; |
| 7184 | #endif |
| 7185 | switch (base_op) { |
| 7186 | case FUTEX_WAIT: |
| 7187 | case FUTEX_WAIT_BITSET: |
| 7188 | if (timeout) { |
| 7189 | pts = &ts; |
| 7190 | target_to_host_timespec64(pts, timeout); |
| 7191 | } else { |
| 7192 | pts = NULL; |
| 7193 | } |
| 7194 | return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3); |
| 7195 | case FUTEX_WAKE: |
| 7196 | return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0); |
| 7197 | case FUTEX_FD: |
| 7198 | return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0); |
| 7199 | case FUTEX_REQUEUE: |
| 7200 | case FUTEX_CMP_REQUEUE: |
| 7201 | case FUTEX_WAKE_OP: |
| 7202 | /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the |
| 7203 | TIMEOUT parameter is interpreted as a uint32_t by the kernel. |
| 7204 | But the prototype takes a `struct timespec *'; insert casts |
| 7205 | to satisfy the compiler. We do not need to tswap TIMEOUT |
| 7206 | since it's not compared to guest memory. */ |
| 7207 | pts = (struct timespec *)(uintptr_t) timeout; |
| 7208 | return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2), |
| 7209 | (base_op == FUTEX_CMP_REQUEUE |
| 7210 | ? tswap32(val3) |
| 7211 | : val3)); |
| 7212 | default: |
| 7213 | return -TARGET_ENOSYS; |
| 7214 | } |
| 7215 | } |
| 7216 | #endif |
| 7217 | |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7218 | #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 7219 | static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname, |
| 7220 | abi_long handle, abi_long mount_id, |
| 7221 | abi_long flags) |
| 7222 | { |
| 7223 | struct file_handle *target_fh; |
| 7224 | struct file_handle *fh; |
| 7225 | int mid = 0; |
| 7226 | abi_long ret; |
| 7227 | char *name; |
| 7228 | unsigned int size, total_size; |
| 7229 | |
| 7230 | if (get_user_s32(size, handle)) { |
| 7231 | return -TARGET_EFAULT; |
| 7232 | } |
| 7233 | |
| 7234 | name = lock_user_string(pathname); |
| 7235 | if (!name) { |
| 7236 | return -TARGET_EFAULT; |
| 7237 | } |
| 7238 | |
| 7239 | total_size = sizeof(struct file_handle) + size; |
| 7240 | target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0); |
| 7241 | if (!target_fh) { |
| 7242 | unlock_user(name, pathname, 0); |
| 7243 | return -TARGET_EFAULT; |
| 7244 | } |
| 7245 | |
| 7246 | fh = g_malloc0(total_size); |
| 7247 | fh->handle_bytes = size; |
| 7248 | |
| 7249 | ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags)); |
| 7250 | unlock_user(name, pathname, 0); |
| 7251 | |
| 7252 | /* man name_to_handle_at(2): |
| 7253 | * Other than the use of the handle_bytes field, the caller should treat |
| 7254 | * the file_handle structure as an opaque data type |
| 7255 | */ |
| 7256 | |
| 7257 | memcpy(target_fh, fh, total_size); |
| 7258 | target_fh->handle_bytes = tswap32(fh->handle_bytes); |
| 7259 | target_fh->handle_type = tswap32(fh->handle_type); |
| 7260 | g_free(fh); |
| 7261 | unlock_user(target_fh, handle, total_size); |
| 7262 | |
| 7263 | if (put_user_s32(mid, mount_id)) { |
| 7264 | return -TARGET_EFAULT; |
| 7265 | } |
| 7266 | |
| 7267 | return ret; |
| 7268 | |
| 7269 | } |
| 7270 | #endif |
| 7271 | |
| 7272 | #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 7273 | static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle, |
| 7274 | abi_long flags) |
| 7275 | { |
| 7276 | struct file_handle *target_fh; |
| 7277 | struct file_handle *fh; |
| 7278 | unsigned int size, total_size; |
| 7279 | abi_long ret; |
| 7280 | |
| 7281 | if (get_user_s32(size, handle)) { |
| 7282 | return -TARGET_EFAULT; |
| 7283 | } |
| 7284 | |
| 7285 | total_size = sizeof(struct file_handle) + size; |
| 7286 | target_fh = lock_user(VERIFY_READ, handle, total_size, 1); |
| 7287 | if (!target_fh) { |
| 7288 | return -TARGET_EFAULT; |
| 7289 | } |
| 7290 | |
Thomas Huth | e9d49d5 | 2015-10-09 17:56:38 +0200 | [diff] [blame] | 7291 | fh = g_memdup(target_fh, total_size); |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7292 | fh->handle_bytes = size; |
| 7293 | fh->handle_type = tswap32(target_fh->handle_type); |
| 7294 | |
| 7295 | ret = get_errno(open_by_handle_at(mount_fd, fh, |
| 7296 | target_to_host_bitmask(flags, fcntl_flags_tbl))); |
| 7297 | |
| 7298 | g_free(fh); |
| 7299 | |
| 7300 | unlock_user(target_fh, handle, total_size); |
| 7301 | |
| 7302 | return ret; |
| 7303 | } |
| 7304 | #endif |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 7305 | |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7306 | #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4) |
| 7307 | |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7308 | static abi_long do_signalfd4(int fd, abi_long mask, int flags) |
| 7309 | { |
| 7310 | int host_flags; |
| 7311 | target_sigset_t *target_mask; |
| 7312 | sigset_t host_mask; |
| 7313 | abi_long ret; |
| 7314 | |
| 7315 | if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) { |
| 7316 | return -TARGET_EINVAL; |
| 7317 | } |
| 7318 | if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) { |
| 7319 | return -TARGET_EFAULT; |
| 7320 | } |
| 7321 | |
| 7322 | target_to_host_sigset(&host_mask, target_mask); |
| 7323 | |
| 7324 | host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); |
| 7325 | |
| 7326 | ret = get_errno(signalfd(fd, &host_mask, host_flags)); |
| 7327 | if (ret >= 0) { |
| 7328 | fd_trans_register(ret, &target_signalfd_trans); |
| 7329 | } |
| 7330 | |
| 7331 | unlock_user_struct(target_mask, mask, 0); |
| 7332 | |
| 7333 | return ret; |
| 7334 | } |
| 7335 | #endif |
| 7336 | |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 7337 | /* Map host to target signal numbers for the wait family of syscalls. |
| 7338 | Assume all other status bits are the same. */ |
Richard Henderson | a05c640 | 2012-09-15 11:34:20 -0700 | [diff] [blame] | 7339 | int host_to_target_waitstatus(int status) |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 7340 | { |
| 7341 | if (WIFSIGNALED(status)) { |
| 7342 | return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f); |
| 7343 | } |
| 7344 | if (WIFSTOPPED(status)) { |
| 7345 | return (host_to_target_signal(WSTOPSIG(status)) << 8) |
| 7346 | | (status & 0xff); |
| 7347 | } |
| 7348 | return status; |
| 7349 | } |
| 7350 | |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7351 | static int open_self_cmdline(void *cpu_env, int fd) |
| 7352 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 7353 | CPUState *cpu = env_cpu((CPUArchState *)cpu_env); |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7354 | struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm; |
| 7355 | int i; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7356 | |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7357 | for (i = 0; i < bprm->argc; i++) { |
| 7358 | size_t len = strlen(bprm->argv[i]) + 1; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7359 | |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7360 | if (write(fd, bprm->argv[i], len) != len) { |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7361 | return -1; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7362 | } |
| 7363 | } |
| 7364 | |
Andreas Schwab | 58de8b9 | 2017-03-20 12:31:55 +0100 | [diff] [blame] | 7365 | return 0; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7366 | } |
| 7367 | |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 7368 | static int open_self_maps(void *cpu_env, int fd) |
| 7369 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 7370 | CPUState *cpu = env_cpu((CPUArchState *)cpu_env); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 7371 | TaskState *ts = cpu->opaque; |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7372 | GSList *map_info = read_self_maps(); |
| 7373 | GSList *s; |
Alex Bennée | bb55173 | 2020-04-03 20:11:47 +0100 | [diff] [blame] | 7374 | int count; |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 7375 | |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7376 | for (s = map_info; s; s = g_slist_next(s)) { |
| 7377 | MapInfo *e = (MapInfo *) s->data; |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 7378 | |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7379 | if (h2g_valid(e->start)) { |
| 7380 | unsigned long min = e->start; |
| 7381 | unsigned long max = e->end; |
Mikhail Ilyin | d67f4aa | 2014-08-05 17:33:51 +0400 | [diff] [blame] | 7382 | int flags = page_get_flags(h2g(min)); |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7383 | const char *path; |
| 7384 | |
| 7385 | max = h2g_valid(max - 1) ? |
| 7386 | max : (uintptr_t) g2h(GUEST_ADDR_MAX) + 1; |
| 7387 | |
Mikhail Ilyin | d67f4aa | 2014-08-05 17:33:51 +0400 | [diff] [blame] | 7388 | if (page_check_range(h2g(min), max - min, flags) == -1) { |
| 7389 | continue; |
| 7390 | } |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7391 | |
Mikhail Ilyin | d67f4aa | 2014-08-05 17:33:51 +0400 | [diff] [blame] | 7392 | if (h2g(min) == ts->info->stack_limit) { |
Alex Bennée | bb55173 | 2020-04-03 20:11:47 +0100 | [diff] [blame] | 7393 | path = "[stack]"; |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7394 | } else { |
| 7395 | path = e->path; |
Mikhail Ilyin | d67f4aa | 2014-08-05 17:33:51 +0400 | [diff] [blame] | 7396 | } |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7397 | |
Alex Bennée | bb55173 | 2020-04-03 20:11:47 +0100 | [diff] [blame] | 7398 | count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr |
| 7399 | " %c%c%c%c %08" PRIx64 " %s %"PRId64, |
| 7400 | h2g(min), h2g(max - 1) + 1, |
| 7401 | e->is_read ? 'r' : '-', |
| 7402 | e->is_write ? 'w' : '-', |
| 7403 | e->is_exec ? 'x' : '-', |
| 7404 | e->is_priv ? 'p' : '-', |
| 7405 | (uint64_t) e->offset, e->dev, e->inode); |
| 7406 | if (path) { |
| 7407 | dprintf(fd, "%*s%s\n", 73 - count, "", path); |
| 7408 | } else { |
| 7409 | dprintf(fd, "\n"); |
| 7410 | } |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 7411 | } |
| 7412 | } |
| 7413 | |
Alex Bennée | 01ef6b9 | 2020-04-03 20:11:46 +0100 | [diff] [blame] | 7414 | free_self_maps(map_info); |
| 7415 | |
Richard Henderson | bf02adc | 2020-02-12 19:22:22 -0800 | [diff] [blame] | 7416 | #ifdef TARGET_VSYSCALL_PAGE |
| 7417 | /* |
| 7418 | * We only support execution from the vsyscall page. |
| 7419 | * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3. |
| 7420 | */ |
Alex Bennée | bb55173 | 2020-04-03 20:11:47 +0100 | [diff] [blame] | 7421 | count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx |
| 7422 | " --xp 00000000 00:00 0", |
| 7423 | TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE); |
| 7424 | dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]"); |
Richard Henderson | bf02adc | 2020-02-12 19:22:22 -0800 | [diff] [blame] | 7425 | #endif |
| 7426 | |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 7427 | return 0; |
| 7428 | } |
| 7429 | |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 7430 | static int open_self_stat(void *cpu_env, int fd) |
| 7431 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 7432 | CPUState *cpu = env_cpu((CPUArchState *)cpu_env); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 7433 | TaskState *ts = cpu->opaque; |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 7434 | g_autoptr(GString) buf = g_string_new(NULL); |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 7435 | int i; |
| 7436 | |
| 7437 | for (i = 0; i < 44; i++) { |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 7438 | if (i == 0) { |
| 7439 | /* pid */ |
| 7440 | g_string_printf(buf, FMT_pid " ", getpid()); |
| 7441 | } else if (i == 1) { |
| 7442 | /* app name */ |
| 7443 | gchar *bin = g_strrstr(ts->bprm->argv[0], "/"); |
| 7444 | bin = bin ? bin + 1 : ts->bprm->argv[0]; |
| 7445 | g_string_printf(buf, "(%.15s) ", bin); |
| 7446 | } else if (i == 27) { |
| 7447 | /* stack bottom */ |
| 7448 | g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack); |
| 7449 | } else { |
| 7450 | /* for the rest, there is MasterCard */ |
| 7451 | g_string_printf(buf, "0%c", i == 43 ? '\n' : ' '); |
| 7452 | } |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 7453 | |
Alex Bennée | 7ad4d5a | 2020-04-14 21:06:23 +0100 | [diff] [blame] | 7454 | if (write(fd, buf->str, buf->len) != buf->len) { |
| 7455 | return -1; |
| 7456 | } |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 7457 | } |
| 7458 | |
| 7459 | return 0; |
| 7460 | } |
| 7461 | |
Alexander Graf | 257450e | 2011-11-02 20:23:26 +0100 | [diff] [blame] | 7462 | static int open_self_auxv(void *cpu_env, int fd) |
| 7463 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 7464 | CPUState *cpu = env_cpu((CPUArchState *)cpu_env); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 7465 | TaskState *ts = cpu->opaque; |
Alexander Graf | 257450e | 2011-11-02 20:23:26 +0100 | [diff] [blame] | 7466 | abi_ulong auxv = ts->info->saved_auxv; |
| 7467 | abi_ulong len = ts->info->auxv_len; |
| 7468 | char *ptr; |
| 7469 | |
| 7470 | /* |
| 7471 | * Auxiliary vector is stored in target process stack. |
| 7472 | * read in whole auxv vector and copy it to file |
| 7473 | */ |
| 7474 | ptr = lock_user(VERIFY_READ, auxv, len, 0); |
| 7475 | if (ptr != NULL) { |
| 7476 | while (len > 0) { |
| 7477 | ssize_t r; |
| 7478 | r = write(fd, ptr, len); |
| 7479 | if (r <= 0) { |
| 7480 | break; |
| 7481 | } |
| 7482 | len -= r; |
| 7483 | ptr += r; |
| 7484 | } |
| 7485 | lseek(fd, 0, SEEK_SET); |
| 7486 | unlock_user(ptr, auxv, len); |
| 7487 | } |
| 7488 | |
| 7489 | return 0; |
| 7490 | } |
| 7491 | |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 7492 | static int is_proc_myself(const char *filename, const char *entry) |
| 7493 | { |
| 7494 | if (!strncmp(filename, "/proc/", strlen("/proc/"))) { |
| 7495 | filename += strlen("/proc/"); |
| 7496 | if (!strncmp(filename, "self/", strlen("self/"))) { |
| 7497 | filename += strlen("self/"); |
| 7498 | } else if (*filename >= '1' && *filename <= '9') { |
| 7499 | char myself[80]; |
| 7500 | snprintf(myself, sizeof(myself), "%d/", getpid()); |
| 7501 | if (!strncmp(filename, myself, strlen(myself))) { |
| 7502 | filename += strlen(myself); |
| 7503 | } else { |
| 7504 | return 0; |
| 7505 | } |
| 7506 | } else { |
| 7507 | return 0; |
| 7508 | } |
| 7509 | if (!strcmp(filename, entry)) { |
| 7510 | return 1; |
| 7511 | } |
| 7512 | } |
| 7513 | return 0; |
| 7514 | } |
| 7515 | |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 7516 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \ |
Helge Deller | 93a5661 | 2020-04-24 23:06:48 +0200 | [diff] [blame] | 7517 | defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA) |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7518 | static int is_proc(const char *filename, const char *entry) |
| 7519 | { |
| 7520 | return strcmp(filename, entry) == 0; |
| 7521 | } |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 7522 | #endif |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7523 | |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 7524 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7525 | static int open_net_route(void *cpu_env, int fd) |
| 7526 | { |
| 7527 | FILE *fp; |
| 7528 | char *line = NULL; |
| 7529 | size_t len = 0; |
| 7530 | ssize_t read; |
| 7531 | |
| 7532 | fp = fopen("/proc/net/route", "r"); |
| 7533 | if (fp == NULL) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 7534 | return -1; |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7535 | } |
| 7536 | |
| 7537 | /* read header */ |
| 7538 | |
| 7539 | read = getline(&line, &len, fp); |
| 7540 | dprintf(fd, "%s", line); |
| 7541 | |
| 7542 | /* read routes */ |
| 7543 | |
| 7544 | while ((read = getline(&line, &len, fp)) != -1) { |
| 7545 | char iface[16]; |
| 7546 | uint32_t dest, gw, mask; |
| 7547 | unsigned int flags, refcnt, use, metric, mtu, window, irtt; |
Peter Maydell | 9d0bd0c | 2019-02-05 17:42:07 +0000 | [diff] [blame] | 7548 | int fields; |
| 7549 | |
| 7550 | fields = sscanf(line, |
| 7551 | "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", |
| 7552 | iface, &dest, &gw, &flags, &refcnt, &use, &metric, |
| 7553 | &mask, &mtu, &window, &irtt); |
| 7554 | if (fields != 11) { |
| 7555 | continue; |
| 7556 | } |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7557 | dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", |
| 7558 | iface, tswap32(dest), tswap32(gw), flags, refcnt, use, |
| 7559 | metric, tswap32(mask), mtu, window, irtt); |
| 7560 | } |
| 7561 | |
| 7562 | free(line); |
| 7563 | fclose(fp); |
| 7564 | |
| 7565 | return 0; |
| 7566 | } |
| 7567 | #endif |
| 7568 | |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 7569 | #if defined(TARGET_SPARC) |
| 7570 | static int open_cpuinfo(void *cpu_env, int fd) |
| 7571 | { |
| 7572 | dprintf(fd, "type\t\t: sun4u\n"); |
| 7573 | return 0; |
| 7574 | } |
| 7575 | #endif |
| 7576 | |
Helge Deller | 93a5661 | 2020-04-24 23:06:48 +0200 | [diff] [blame] | 7577 | #if defined(TARGET_HPPA) |
| 7578 | static int open_cpuinfo(void *cpu_env, int fd) |
| 7579 | { |
| 7580 | dprintf(fd, "cpu family\t: PA-RISC 1.1e\n"); |
| 7581 | dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n"); |
| 7582 | dprintf(fd, "capabilities\t: os32\n"); |
| 7583 | dprintf(fd, "model\t\t: 9000/778/B160L\n"); |
| 7584 | dprintf(fd, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n"); |
| 7585 | return 0; |
| 7586 | } |
| 7587 | #endif |
| 7588 | |
Laurent Vivier | 4ab6713 | 2019-05-17 15:31:49 +0200 | [diff] [blame] | 7589 | #if defined(TARGET_M68K) |
| 7590 | static int open_hardware(void *cpu_env, int fd) |
| 7591 | { |
| 7592 | dprintf(fd, "Model:\t\tqemu-m68k\n"); |
| 7593 | return 0; |
| 7594 | } |
| 7595 | #endif |
| 7596 | |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 7597 | static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode) |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7598 | { |
| 7599 | struct fake_open { |
| 7600 | const char *filename; |
| 7601 | int (*fill)(void *cpu_env, int fd); |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7602 | int (*cmp)(const char *s1, const char *s2); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7603 | }; |
| 7604 | const struct fake_open *fake_open; |
| 7605 | static const struct fake_open fakes[] = { |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7606 | { "maps", open_self_maps, is_proc_myself }, |
| 7607 | { "stat", open_self_stat, is_proc_myself }, |
| 7608 | { "auxv", open_self_auxv, is_proc_myself }, |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 7609 | { "cmdline", open_self_cmdline, is_proc_myself }, |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7610 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) |
| 7611 | { "/proc/net/route", open_net_route, is_proc }, |
| 7612 | #endif |
Helge Deller | 93a5661 | 2020-04-24 23:06:48 +0200 | [diff] [blame] | 7613 | #if defined(TARGET_SPARC) || defined(TARGET_HPPA) |
Laurent Vivier | fff6938 | 2019-05-17 15:31:48 +0200 | [diff] [blame] | 7614 | { "/proc/cpuinfo", open_cpuinfo, is_proc }, |
| 7615 | #endif |
Laurent Vivier | 4ab6713 | 2019-05-17 15:31:49 +0200 | [diff] [blame] | 7616 | #if defined(TARGET_M68K) |
| 7617 | { "/proc/hardware", open_hardware, is_proc }, |
| 7618 | #endif |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7619 | { NULL, NULL, NULL } |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7620 | }; |
| 7621 | |
Maxim Ostapenko | aa07f5e | 2014-05-02 11:17:07 +0300 | [diff] [blame] | 7622 | if (is_proc_myself(pathname, "exe")) { |
| 7623 | int execfd = qemu_getauxval(AT_EXECFD); |
Timothy E Baldwin | c10a073 | 2016-05-12 18:47:48 +0100 | [diff] [blame] | 7624 | return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode); |
Maxim Ostapenko | aa07f5e | 2014-05-02 11:17:07 +0300 | [diff] [blame] | 7625 | } |
| 7626 | |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7627 | for (fake_open = fakes; fake_open->filename; fake_open++) { |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 7628 | if (fake_open->cmp(pathname, fake_open->filename)) { |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7629 | break; |
| 7630 | } |
| 7631 | } |
| 7632 | |
| 7633 | if (fake_open->filename) { |
| 7634 | const char *tmpdir; |
| 7635 | char filename[PATH_MAX]; |
| 7636 | int fd, r; |
| 7637 | |
| 7638 | /* create temporary file to map stat to */ |
| 7639 | tmpdir = getenv("TMPDIR"); |
| 7640 | if (!tmpdir) |
| 7641 | tmpdir = "/tmp"; |
| 7642 | snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); |
| 7643 | fd = mkstemp(filename); |
| 7644 | if (fd < 0) { |
| 7645 | return fd; |
| 7646 | } |
| 7647 | unlink(filename); |
| 7648 | |
| 7649 | if ((r = fake_open->fill(cpu_env, fd))) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 7650 | int e = errno; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7651 | close(fd); |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 7652 | errno = e; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7653 | return r; |
| 7654 | } |
| 7655 | lseek(fd, 0, SEEK_SET); |
| 7656 | |
| 7657 | return fd; |
| 7658 | } |
| 7659 | |
Timothy E Baldwin | c10a073 | 2016-05-12 18:47:48 +0100 | [diff] [blame] | 7660 | return safe_openat(dirfd, path(pathname), flags, mode); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 7661 | } |
| 7662 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 7663 | #define TIMER_MAGIC 0x0caf0000 |
| 7664 | #define TIMER_MAGIC_MASK 0xffff0000 |
| 7665 | |
| 7666 | /* Convert QEMU provided timer ID back to internal 16bit index format */ |
| 7667 | static target_timer_t get_timer_id(abi_long arg) |
| 7668 | { |
| 7669 | target_timer_t timerid = arg; |
| 7670 | |
| 7671 | if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) { |
| 7672 | return -TARGET_EINVAL; |
| 7673 | } |
| 7674 | |
| 7675 | timerid &= 0xffff; |
| 7676 | |
| 7677 | if (timerid >= ARRAY_SIZE(g_posix_timers)) { |
| 7678 | return -TARGET_EINVAL; |
| 7679 | } |
| 7680 | |
| 7681 | return timerid; |
| 7682 | } |
| 7683 | |
Samuel Thibault | 2e0a871 | 2018-01-09 21:16:43 +0100 | [diff] [blame] | 7684 | static int target_to_host_cpu_mask(unsigned long *host_mask, |
| 7685 | size_t host_size, |
| 7686 | abi_ulong target_addr, |
| 7687 | size_t target_size) |
| 7688 | { |
| 7689 | unsigned target_bits = sizeof(abi_ulong) * 8; |
| 7690 | unsigned host_bits = sizeof(*host_mask) * 8; |
| 7691 | abi_ulong *target_mask; |
| 7692 | unsigned i, j; |
| 7693 | |
| 7694 | assert(host_size >= target_size); |
| 7695 | |
| 7696 | target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1); |
| 7697 | if (!target_mask) { |
| 7698 | return -TARGET_EFAULT; |
| 7699 | } |
| 7700 | memset(host_mask, 0, host_size); |
| 7701 | |
| 7702 | for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) { |
| 7703 | unsigned bit = i * target_bits; |
| 7704 | abi_ulong val; |
| 7705 | |
| 7706 | __get_user(val, &target_mask[i]); |
| 7707 | for (j = 0; j < target_bits; j++, bit++) { |
| 7708 | if (val & (1UL << j)) { |
| 7709 | host_mask[bit / host_bits] |= 1UL << (bit % host_bits); |
| 7710 | } |
| 7711 | } |
| 7712 | } |
| 7713 | |
| 7714 | unlock_user(target_mask, target_addr, 0); |
| 7715 | return 0; |
| 7716 | } |
| 7717 | |
| 7718 | static int host_to_target_cpu_mask(const unsigned long *host_mask, |
| 7719 | size_t host_size, |
| 7720 | abi_ulong target_addr, |
| 7721 | size_t target_size) |
| 7722 | { |
| 7723 | unsigned target_bits = sizeof(abi_ulong) * 8; |
| 7724 | unsigned host_bits = sizeof(*host_mask) * 8; |
| 7725 | abi_ulong *target_mask; |
| 7726 | unsigned i, j; |
| 7727 | |
| 7728 | assert(host_size >= target_size); |
| 7729 | |
| 7730 | target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0); |
| 7731 | if (!target_mask) { |
| 7732 | return -TARGET_EFAULT; |
| 7733 | } |
| 7734 | |
| 7735 | for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) { |
| 7736 | unsigned bit = i * target_bits; |
| 7737 | abi_ulong val = 0; |
| 7738 | |
| 7739 | for (j = 0; j < target_bits; j++, bit++) { |
| 7740 | if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) { |
| 7741 | val |= 1UL << j; |
| 7742 | } |
| 7743 | } |
| 7744 | __put_user(val, &target_mask[i]); |
| 7745 | } |
| 7746 | |
| 7747 | unlock_user(target_mask, target_addr, target_size); |
| 7748 | return 0; |
| 7749 | } |
| 7750 | |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 7751 | /* This is an internal helper for do_syscall so that it is easier |
| 7752 | * to have a single return point, so that actions, such as logging |
| 7753 | * of syscall results, can be performed. |
| 7754 | * All errnos that do_syscall() returns must be -TARGET_<errcode>. |
| 7755 | */ |
| 7756 | static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, |
| 7757 | abi_long arg2, abi_long arg3, abi_long arg4, |
| 7758 | abi_long arg5, abi_long arg6, abi_long arg7, |
| 7759 | abi_long arg8) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7760 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 7761 | CPUState *cpu = env_cpu(cpu_env); |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7762 | abi_long ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 7763 | #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \ |
| 7764 | || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \ |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 7765 | || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \ |
| 7766 | || defined(TARGET_NR_statx) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7767 | struct stat st; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 7768 | #endif |
| 7769 | #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \ |
| 7770 | || defined(TARGET_NR_fstatfs) |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 7771 | struct statfs stfs; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 7772 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7773 | void *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7774 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7775 | switch(num) { |
| 7776 | case TARGET_NR_exit: |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 7777 | /* In old applications this may be used to implement _exit(2). |
| 7778 | However in threaded applictions it is used for thread termination, |
| 7779 | and _exit_group is used for application termination. |
| 7780 | Do thread termination if we have more then one thread. */ |
Timothy E Baldwin | a099588 | 2016-05-27 15:51:56 +0100 | [diff] [blame] | 7781 | |
| 7782 | if (block_signals()) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7783 | return -TARGET_ERESTARTSYS; |
Timothy E Baldwin | a099588 | 2016-05-27 15:51:56 +0100 | [diff] [blame] | 7784 | } |
| 7785 | |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 7786 | pthread_mutex_lock(&clone_lock); |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 7787 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 7788 | if (CPU_NEXT(first_cpu)) { |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 7789 | TaskState *ts = cpu->opaque; |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 7790 | |
Markus Armbruster | 5325cc3 | 2020-07-07 18:05:54 +0200 | [diff] [blame] | 7791 | object_property_set_bool(OBJECT(cpu), "realized", false, NULL); |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 7792 | object_unref(OBJECT(cpu)); |
| 7793 | /* |
| 7794 | * At this point the CPU should be unrealized and removed |
| 7795 | * from cpu lists. We can clean-up the rest of the thread |
| 7796 | * data without the lock held. |
| 7797 | */ |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 7798 | |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 7799 | pthread_mutex_unlock(&clone_lock); |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 7800 | |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 7801 | if (ts->child_tidptr) { |
| 7802 | put_user_u32(0, ts->child_tidptr); |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 7803 | do_sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX, |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 7804 | NULL, NULL, 0); |
| 7805 | } |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 7806 | thread_cpu = NULL; |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 7807 | g_free(ts); |
Emilio G. Cota | 7090376 | 2015-08-23 20:23:41 -0400 | [diff] [blame] | 7808 | rcu_unregister_thread(); |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 7809 | pthread_exit(NULL); |
| 7810 | } |
Alex Bennée | dd1f634 | 2016-09-30 22:31:01 +0100 | [diff] [blame] | 7811 | |
Alex Bennée | 1f81ce9 | 2020-05-20 15:05:39 +0100 | [diff] [blame] | 7812 | pthread_mutex_unlock(&clone_lock); |
Alex Bennée | 708b6a6 | 2018-06-22 17:09:10 +0100 | [diff] [blame] | 7813 | preexit_cleanup(cpu_env, arg1); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 7814 | _exit(arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7815 | return 0; /* avoid warning */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7816 | case TARGET_NR_read: |
Andreas Schwab | ba584f1 | 2019-03-05 17:45:05 +0100 | [diff] [blame] | 7817 | if (arg2 == 0 && arg3 == 0) { |
| 7818 | return get_errno(safe_read(arg1, 0, 0)); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7819 | } else { |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 7820 | if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7821 | return -TARGET_EFAULT; |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 7822 | ret = get_errno(safe_read(arg1, p, arg3)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7823 | if (ret >= 0 && |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 7824 | fd_trans_host_to_target_data(arg1)) { |
| 7825 | ret = fd_trans_host_to_target_data(arg1)(p, ret); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7826 | } |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 7827 | unlock_user(p, arg2, ret); |
| 7828 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7829 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7830 | case TARGET_NR_write: |
Tony Garnock-Jones | 58cfa6c | 2018-09-08 19:22:05 +0100 | [diff] [blame] | 7831 | if (arg2 == 0 && arg3 == 0) { |
| 7832 | return get_errno(safe_write(arg1, 0, 0)); |
| 7833 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7834 | if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7835 | return -TARGET_EFAULT; |
Laurent Vivier | 04b9bcf | 2017-03-01 10:37:47 +0100 | [diff] [blame] | 7836 | if (fd_trans_target_to_host_data(arg1)) { |
| 7837 | void *copy = g_malloc(arg3); |
| 7838 | memcpy(copy, p, arg3); |
| 7839 | ret = fd_trans_target_to_host_data(arg1)(copy, arg3); |
| 7840 | if (ret >= 0) { |
| 7841 | ret = get_errno(safe_write(arg1, copy, ret)); |
| 7842 | } |
| 7843 | g_free(copy); |
| 7844 | } else { |
| 7845 | ret = get_errno(safe_write(arg1, p, arg3)); |
| 7846 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7847 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7848 | return ret; |
| 7849 | |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7850 | #ifdef TARGET_NR_open |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7851 | case TARGET_NR_open: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7852 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7853 | return -TARGET_EFAULT; |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 7854 | ret = get_errno(do_openat(cpu_env, AT_FDCWD, p, |
| 7855 | target_to_host_bitmask(arg2, fcntl_flags_tbl), |
| 7856 | arg3)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7857 | fd_trans_unregister(ret); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7858 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7859 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7860 | #endif |
ths | 8242483 | 2007-09-24 09:21:55 +0000 | [diff] [blame] | 7861 | case TARGET_NR_openat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7862 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7863 | return -TARGET_EFAULT; |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 7864 | ret = get_errno(do_openat(cpu_env, arg1, p, |
| 7865 | target_to_host_bitmask(arg3, fcntl_flags_tbl), |
| 7866 | arg4)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7867 | fd_trans_unregister(ret); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7868 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7869 | return ret; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7870 | #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 7871 | case TARGET_NR_name_to_handle_at: |
| 7872 | ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7873 | return ret; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7874 | #endif |
| 7875 | #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 7876 | case TARGET_NR_open_by_handle_at: |
| 7877 | ret = do_open_by_handle_at(arg1, arg2, arg3); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7878 | fd_trans_unregister(ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7879 | return ret; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 7880 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7881 | case TARGET_NR_close: |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7882 | fd_trans_unregister(arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7883 | return get_errno(close(arg1)); |
| 7884 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7885 | case TARGET_NR_brk: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7886 | return do_brk(arg1); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7887 | #ifdef TARGET_NR_fork |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7888 | case TARGET_NR_fork: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7889 | 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] | 7890 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7891 | #ifdef TARGET_NR_waitpid |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7892 | case TARGET_NR_waitpid: |
| 7893 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7894 | int status; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 7895 | ret = get_errno(safe_wait4(arg1, &status, arg3, 0)); |
Alexander Graf | 5379557 | 2011-11-24 00:44:43 +0100 | [diff] [blame] | 7896 | if (!is_error(ret) && arg2 && ret |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 7897 | && put_user_s32(host_to_target_waitstatus(status), arg2)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7898 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7899 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7900 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7901 | #endif |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 7902 | #ifdef TARGET_NR_waitid |
| 7903 | case TARGET_NR_waitid: |
| 7904 | { |
| 7905 | siginfo_t info; |
| 7906 | info.si_pid = 0; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 7907 | ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL)); |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 7908 | if (!is_error(ret) && arg3 && info.si_pid != 0) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7909 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7910 | return -TARGET_EFAULT; |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 7911 | host_to_target_siginfo(p, &info); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7912 | unlock_user(p, arg3, sizeof(target_siginfo_t)); |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 7913 | } |
| 7914 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7915 | return ret; |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 7916 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7917 | #ifdef TARGET_NR_creat /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7918 | case TARGET_NR_creat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7919 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7920 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7921 | ret = get_errno(creat(p, arg2)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7922 | fd_trans_unregister(ret); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7923 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7924 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7925 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7926 | #ifdef TARGET_NR_link |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7927 | case TARGET_NR_link: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7928 | { |
| 7929 | void * p2; |
| 7930 | p = lock_user_string(arg1); |
| 7931 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7932 | if (!p || !p2) |
| 7933 | ret = -TARGET_EFAULT; |
| 7934 | else |
| 7935 | ret = get_errno(link(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7936 | unlock_user(p2, arg2, 0); |
| 7937 | unlock_user(p, arg1, 0); |
| 7938 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7939 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7940 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7941 | #if defined(TARGET_NR_linkat) |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 7942 | case TARGET_NR_linkat: |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 7943 | { |
| 7944 | void * p2 = NULL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7945 | if (!arg2 || !arg4) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7946 | return -TARGET_EFAULT; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 7947 | p = lock_user_string(arg2); |
| 7948 | p2 = lock_user_string(arg4); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7949 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 7950 | ret = -TARGET_EFAULT; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 7951 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7952 | ret = get_errno(linkat(arg1, p, arg3, p2, arg5)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7953 | unlock_user(p, arg2, 0); |
| 7954 | unlock_user(p2, arg4, 0); |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 7955 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7956 | return ret; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 7957 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7958 | #ifdef TARGET_NR_unlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7959 | case TARGET_NR_unlink: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7960 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7961 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7962 | ret = get_errno(unlink(p)); |
| 7963 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7964 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7965 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7966 | #if defined(TARGET_NR_unlinkat) |
ths | 8170f56 | 2007-09-24 09:24:11 +0000 | [diff] [blame] | 7967 | case TARGET_NR_unlinkat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7968 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7969 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7970 | ret = get_errno(unlinkat(arg1, p, arg3)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7971 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 7972 | return ret; |
balrog | b7d35e6 | 2007-12-12 00:40:24 +0000 | [diff] [blame] | 7973 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7974 | case TARGET_NR_execve: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 7975 | { |
| 7976 | char **argp, **envp; |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 7977 | int argc, envc; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7978 | abi_ulong gp; |
| 7979 | abi_ulong guest_argp; |
| 7980 | abi_ulong guest_envp; |
| 7981 | abi_ulong addr; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 7982 | char **q; |
Ulrich Hecht | a6f79cc | 2012-01-31 12:43:16 +0100 | [diff] [blame] | 7983 | int total_size = 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 7984 | |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 7985 | argc = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7986 | guest_argp = arg2; |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 7987 | for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) { |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 7988 | if (get_user_ual(addr, gp)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7989 | return -TARGET_EFAULT; |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 7990 | if (!addr) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7991 | break; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 7992 | argc++; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7993 | } |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 7994 | envc = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7995 | guest_envp = arg3; |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 7996 | for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) { |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 7997 | if (get_user_ual(addr, gp)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 7998 | return -TARGET_EFAULT; |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 7999 | if (!addr) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8000 | break; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 8001 | envc++; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8002 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 8003 | |
Prasad J Pandit | b936cb5 | 2017-03-07 12:51:47 +0530 | [diff] [blame] | 8004 | argp = g_new0(char *, argc + 1); |
| 8005 | envp = g_new0(char *, envc + 1); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 8006 | |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 8007 | for (gp = guest_argp, q = argp; gp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8008 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8009 | if (get_user_ual(addr, gp)) |
| 8010 | goto execve_efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8011 | if (!addr) |
| 8012 | break; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8013 | if (!(*q = lock_user_string(addr))) |
| 8014 | goto execve_efault; |
Ulrich Hecht | a6f79cc | 2012-01-31 12:43:16 +0100 | [diff] [blame] | 8015 | total_size += strlen(*q) + 1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8016 | } |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 8017 | *q = NULL; |
| 8018 | |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 8019 | for (gp = guest_envp, q = envp; gp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8020 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8021 | if (get_user_ual(addr, gp)) |
| 8022 | goto execve_efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8023 | if (!addr) |
| 8024 | break; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8025 | if (!(*q = lock_user_string(addr))) |
| 8026 | goto execve_efault; |
Ulrich Hecht | a6f79cc | 2012-01-31 12:43:16 +0100 | [diff] [blame] | 8027 | total_size += strlen(*q) + 1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8028 | } |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 8029 | *q = NULL; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 8030 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8031 | if (!(p = lock_user_string(arg1))) |
| 8032 | goto execve_efault; |
Timothy E Baldwin | ffdcbe2 | 2016-05-12 18:47:50 +0100 | [diff] [blame] | 8033 | /* Although execve() is not an interruptible syscall it is |
| 8034 | * a special case where we must use the safe_syscall wrapper: |
| 8035 | * if we allow a signal to happen before we make the host |
| 8036 | * syscall then we will 'lose' it, because at the point of |
| 8037 | * execve the process leaves QEMU's control. So we use the |
| 8038 | * safe syscall wrapper to ensure that we either take the |
| 8039 | * signal as a guest signal, or else it does not happen |
| 8040 | * before the execve completes and makes it the other |
| 8041 | * program's problem. |
| 8042 | */ |
| 8043 | ret = get_errno(safe_execve(p, argp, envp)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8044 | unlock_user(p, arg1, 0); |
| 8045 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8046 | goto execve_end; |
| 8047 | |
| 8048 | execve_efault: |
| 8049 | ret = -TARGET_EFAULT; |
| 8050 | |
| 8051 | execve_end: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8052 | for (gp = guest_argp, q = argp; *q; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8053 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8054 | if (get_user_ual(addr, gp) |
| 8055 | || !addr) |
| 8056 | break; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8057 | unlock_user(*q, addr, 0); |
| 8058 | } |
| 8059 | for (gp = guest_envp, q = envp; *q; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8060 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8061 | if (get_user_ual(addr, gp) |
| 8062 | || !addr) |
| 8063 | break; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8064 | unlock_user(*q, addr, 0); |
| 8065 | } |
Prasad J Pandit | b936cb5 | 2017-03-07 12:51:47 +0530 | [diff] [blame] | 8066 | |
| 8067 | g_free(argp); |
| 8068 | g_free(envp); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 8069 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8070 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8071 | case TARGET_NR_chdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8072 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8073 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8074 | ret = get_errno(chdir(p)); |
| 8075 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8076 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 8077 | #ifdef TARGET_NR_time |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8078 | case TARGET_NR_time: |
| 8079 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8080 | time_t host_time; |
| 8081 | ret = get_errno(time(&host_time)); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8082 | if (!is_error(ret) |
| 8083 | && arg1 |
| 8084 | && put_user_sal(host_time, arg1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8085 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8086 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8087 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 8088 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8089 | #ifdef TARGET_NR_mknod |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8090 | case TARGET_NR_mknod: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8091 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8092 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8093 | ret = get_errno(mknod(p, arg2, arg3)); |
| 8094 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8095 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8096 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8097 | #if defined(TARGET_NR_mknodat) |
ths | 75ac37a | 2007-09-24 09:23:05 +0000 | [diff] [blame] | 8098 | case TARGET_NR_mknodat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8099 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8100 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8101 | ret = get_errno(mknodat(arg1, p, arg3, arg4)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8102 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8103 | return ret; |
ths | 75ac37a | 2007-09-24 09:23:05 +0000 | [diff] [blame] | 8104 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8105 | #ifdef TARGET_NR_chmod |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8106 | case TARGET_NR_chmod: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8107 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8108 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8109 | ret = get_errno(chmod(p, arg2)); |
| 8110 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8111 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8112 | #endif |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 8113 | #ifdef TARGET_NR_lseek |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8114 | case TARGET_NR_lseek: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8115 | return get_errno(lseek(arg1, arg2, arg3)); |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 8116 | #endif |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 8117 | #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA) |
| 8118 | /* Alpha specific */ |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8119 | case TARGET_NR_getxpid: |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 8120 | ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid(); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8121 | return get_errno(getpid()); |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 8122 | #endif |
| 8123 | #ifdef TARGET_NR_getpid |
| 8124 | case TARGET_NR_getpid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8125 | return get_errno(getpid()); |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 8126 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8127 | case TARGET_NR_mount: |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 8128 | { |
| 8129 | /* need to look at the data field */ |
| 8130 | void *p2, *p3; |
| 8131 | |
| 8132 | if (arg1) { |
| 8133 | p = lock_user_string(arg1); |
| 8134 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8135 | return -TARGET_EFAULT; |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 8136 | } |
| 8137 | } else { |
| 8138 | p = NULL; |
| 8139 | } |
| 8140 | |
| 8141 | p2 = lock_user_string(arg2); |
| 8142 | if (!p2) { |
| 8143 | if (arg1) { |
| 8144 | unlock_user(p, arg1, 0); |
| 8145 | } |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8146 | return -TARGET_EFAULT; |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 8147 | } |
| 8148 | |
| 8149 | if (arg3) { |
| 8150 | p3 = lock_user_string(arg3); |
| 8151 | if (!p3) { |
| 8152 | if (arg1) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8153 | unlock_user(p, arg1, 0); |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 8154 | } |
| 8155 | unlock_user(p2, arg2, 0); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8156 | return -TARGET_EFAULT; |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 8157 | } |
| 8158 | } else { |
| 8159 | p3 = NULL; |
| 8160 | } |
| 8161 | |
| 8162 | /* FIXME - arg5 should be locked, but it isn't clear how to |
| 8163 | * do that since it's not guaranteed to be a NULL-terminated |
| 8164 | * string. |
| 8165 | */ |
| 8166 | if (!arg5) { |
| 8167 | ret = mount(p, p2, p3, (unsigned long)arg4, NULL); |
| 8168 | } else { |
| 8169 | ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)); |
| 8170 | } |
| 8171 | ret = get_errno(ret); |
| 8172 | |
| 8173 | if (arg1) { |
| 8174 | unlock_user(p, arg1, 0); |
| 8175 | } |
| 8176 | unlock_user(p2, arg2, 0); |
| 8177 | if (arg3) { |
| 8178 | unlock_user(p3, arg3, 0); |
| 8179 | } |
| 8180 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8181 | return ret; |
Laurent Vivier | 6eb9dbf | 2020-05-02 21:46:42 +0200 | [diff] [blame] | 8182 | #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount) |
| 8183 | #if defined(TARGET_NR_umount) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8184 | case TARGET_NR_umount: |
Laurent Vivier | 6eb9dbf | 2020-05-02 21:46:42 +0200 | [diff] [blame] | 8185 | #endif |
| 8186 | #if defined(TARGET_NR_oldumount) |
| 8187 | case TARGET_NR_oldumount: |
| 8188 | #endif |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8189 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8190 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8191 | ret = get_errno(umount(p)); |
| 8192 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8193 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8194 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8195 | #ifdef TARGET_NR_stime /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8196 | case TARGET_NR_stime: |
| 8197 | { |
Laurent Vivier | 0f1f2d4 | 2019-11-12 15:25:56 +0100 | [diff] [blame] | 8198 | struct timespec ts; |
| 8199 | ts.tv_nsec = 0; |
| 8200 | if (get_user_sal(ts.tv_sec, arg1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8201 | return -TARGET_EFAULT; |
Laurent Vivier | 0f1f2d4 | 2019-11-12 15:25:56 +0100 | [diff] [blame] | 8202 | } |
| 8203 | return get_errno(clock_settime(CLOCK_REALTIME, &ts)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8204 | } |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8205 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8206 | #ifdef TARGET_NR_alarm /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8207 | case TARGET_NR_alarm: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8208 | return alarm(arg1); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8209 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8210 | #ifdef TARGET_NR_pause /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8211 | case TARGET_NR_pause: |
Timothy E Baldwin | f59ec60 | 2016-05-27 15:51:55 +0100 | [diff] [blame] | 8212 | if (!block_signals()) { |
| 8213 | sigsuspend(&((TaskState *)cpu->opaque)->signal_mask); |
| 8214 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8215 | return -TARGET_EINTR; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8216 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8217 | #ifdef TARGET_NR_utime |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8218 | case TARGET_NR_utime: |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8219 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8220 | struct utimbuf tbuf, *host_tbuf; |
| 8221 | struct target_utimbuf *target_tbuf; |
| 8222 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8223 | if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8224 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 8225 | tbuf.actime = tswapal(target_tbuf->actime); |
| 8226 | tbuf.modtime = tswapal(target_tbuf->modtime); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8227 | unlock_user_struct(target_tbuf, arg2, 0); |
| 8228 | host_tbuf = &tbuf; |
bellard | f72e8ff | 2004-05-03 19:23:07 +0000 | [diff] [blame] | 8229 | } else { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8230 | host_tbuf = NULL; |
bellard | f72e8ff | 2004-05-03 19:23:07 +0000 | [diff] [blame] | 8231 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8232 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8233 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8234 | ret = get_errno(utime(p, host_tbuf)); |
| 8235 | unlock_user(p, arg1, 0); |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8236 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8237 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8238 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8239 | #ifdef TARGET_NR_utimes |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 8240 | case TARGET_NR_utimes: |
| 8241 | { |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 8242 | struct timeval *tvp, tv[2]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8243 | if (arg2) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 8244 | if (copy_from_user_timeval(&tv[0], arg2) |
| 8245 | || copy_from_user_timeval(&tv[1], |
| 8246 | arg2 + sizeof(struct target_timeval))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8247 | return -TARGET_EFAULT; |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 8248 | tvp = tv; |
| 8249 | } else { |
| 8250 | tvp = NULL; |
| 8251 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8252 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8253 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8254 | ret = get_errno(utimes(p, tvp)); |
| 8255 | unlock_user(p, arg1, 0); |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 8256 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8257 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8258 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8259 | #if defined(TARGET_NR_futimesat) |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 8260 | case TARGET_NR_futimesat: |
| 8261 | { |
| 8262 | struct timeval *tvp, tv[2]; |
| 8263 | if (arg3) { |
| 8264 | if (copy_from_user_timeval(&tv[0], arg3) |
| 8265 | || copy_from_user_timeval(&tv[1], |
| 8266 | arg3 + sizeof(struct target_timeval))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8267 | return -TARGET_EFAULT; |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 8268 | tvp = tv; |
| 8269 | } else { |
| 8270 | tvp = NULL; |
| 8271 | } |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8272 | if (!(p = lock_user_string(arg2))) { |
| 8273 | return -TARGET_EFAULT; |
| 8274 | } |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8275 | ret = get_errno(futimesat(arg1, path(p), tvp)); |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 8276 | unlock_user(p, arg2, 0); |
| 8277 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8278 | return ret; |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 8279 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8280 | #ifdef TARGET_NR_access |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8281 | case TARGET_NR_access: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8282 | if (!(p = lock_user_string(arg1))) { |
| 8283 | return -TARGET_EFAULT; |
| 8284 | } |
Ulrich Hecht | 719f908 | 2009-07-03 17:09:29 +0200 | [diff] [blame] | 8285 | ret = get_errno(access(path(p), arg2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8286 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8287 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8288 | #endif |
ths | 92a34c1 | 2007-09-24 09:27:49 +0000 | [diff] [blame] | 8289 | #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) |
| 8290 | case TARGET_NR_faccessat: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8291 | if (!(p = lock_user_string(arg2))) { |
| 8292 | return -TARGET_EFAULT; |
| 8293 | } |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8294 | ret = get_errno(faccessat(arg1, p, arg3, 0)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8295 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8296 | return ret; |
ths | 92a34c1 | 2007-09-24 09:27:49 +0000 | [diff] [blame] | 8297 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8298 | #ifdef TARGET_NR_nice /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8299 | case TARGET_NR_nice: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8300 | return get_errno(nice(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8301 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8302 | case TARGET_NR_sync: |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 8303 | sync(); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8304 | return 0; |
Aleksandar Markovic | 5a03cd0 | 2016-10-10 13:23:30 +0200 | [diff] [blame] | 8305 | #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS) |
| 8306 | case TARGET_NR_syncfs: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8307 | return get_errno(syncfs(arg1)); |
Aleksandar Markovic | 5a03cd0 | 2016-10-10 13:23:30 +0200 | [diff] [blame] | 8308 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8309 | case TARGET_NR_kill: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8310 | return get_errno(safe_kill(arg1, target_to_host_signal(arg2))); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8311 | #ifdef TARGET_NR_rename |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8312 | case TARGET_NR_rename: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8313 | { |
| 8314 | void *p2; |
| 8315 | p = lock_user_string(arg1); |
| 8316 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8317 | if (!p || !p2) |
| 8318 | ret = -TARGET_EFAULT; |
| 8319 | else |
| 8320 | ret = get_errno(rename(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8321 | unlock_user(p2, arg2, 0); |
| 8322 | unlock_user(p, arg1, 0); |
| 8323 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8324 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8325 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8326 | #if defined(TARGET_NR_renameat) |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 8327 | case TARGET_NR_renameat: |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 8328 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8329 | void *p2; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 8330 | p = lock_user_string(arg2); |
| 8331 | p2 = lock_user_string(arg4); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8332 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 8333 | ret = -TARGET_EFAULT; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 8334 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8335 | ret = get_errno(renameat(arg1, p, arg3, p2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8336 | unlock_user(p2, arg4, 0); |
| 8337 | unlock_user(p, arg2, 0); |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 8338 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8339 | return ret; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 8340 | #endif |
Andreas Schwab | 95d0307 | 2018-01-23 11:53:31 +0100 | [diff] [blame] | 8341 | #if defined(TARGET_NR_renameat2) |
| 8342 | case TARGET_NR_renameat2: |
| 8343 | { |
| 8344 | void *p2; |
| 8345 | p = lock_user_string(arg2); |
| 8346 | p2 = lock_user_string(arg4); |
| 8347 | if (!p || !p2) { |
| 8348 | ret = -TARGET_EFAULT; |
| 8349 | } else { |
| 8350 | ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5)); |
| 8351 | } |
| 8352 | unlock_user(p2, arg4, 0); |
| 8353 | unlock_user(p, arg2, 0); |
| 8354 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8355 | return ret; |
Andreas Schwab | 95d0307 | 2018-01-23 11:53:31 +0100 | [diff] [blame] | 8356 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8357 | #ifdef TARGET_NR_mkdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8358 | case TARGET_NR_mkdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8359 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8360 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8361 | ret = get_errno(mkdir(p, arg2)); |
| 8362 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8363 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8364 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8365 | #if defined(TARGET_NR_mkdirat) |
ths | 4472ad0 | 2007-09-24 09:22:32 +0000 | [diff] [blame] | 8366 | case TARGET_NR_mkdirat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8367 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8368 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8369 | ret = get_errno(mkdirat(arg1, p, arg3)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8370 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8371 | return ret; |
ths | 4472ad0 | 2007-09-24 09:22:32 +0000 | [diff] [blame] | 8372 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8373 | #ifdef TARGET_NR_rmdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8374 | case TARGET_NR_rmdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8375 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8376 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8377 | ret = get_errno(rmdir(p)); |
| 8378 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8379 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8380 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8381 | case TARGET_NR_dup: |
| 8382 | ret = get_errno(dup(arg1)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 8383 | if (ret >= 0) { |
| 8384 | fd_trans_dup(arg1, ret); |
| 8385 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8386 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8387 | #ifdef TARGET_NR_pipe |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8388 | case TARGET_NR_pipe: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8389 | return do_pipe(cpu_env, arg1, 0, 0); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8390 | #endif |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 8391 | #ifdef TARGET_NR_pipe2 |
| 8392 | case TARGET_NR_pipe2: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8393 | return do_pipe(cpu_env, arg1, |
| 8394 | target_to_host_bitmask(arg2, fcntl_flags_tbl), 1); |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 8395 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8396 | case TARGET_NR_times: |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 8397 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8398 | struct target_tms *tmsp; |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 8399 | struct tms tms; |
| 8400 | ret = get_errno(times(&tms)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8401 | if (arg1) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8402 | tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0); |
| 8403 | if (!tmsp) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8404 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 8405 | tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime)); |
| 8406 | tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime)); |
| 8407 | tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime)); |
| 8408 | tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime)); |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 8409 | } |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 8410 | if (!is_error(ret)) |
| 8411 | ret = host_to_target_clock_t(ret); |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 8412 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8413 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8414 | case TARGET_NR_acct: |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 8415 | if (arg1 == 0) { |
| 8416 | ret = get_errno(acct(NULL)); |
| 8417 | } else { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8418 | if (!(p = lock_user_string(arg1))) { |
| 8419 | return -TARGET_EFAULT; |
| 8420 | } |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 8421 | ret = get_errno(acct(path(p))); |
| 8422 | unlock_user(p, arg1, 0); |
| 8423 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8424 | return ret; |
Richard Henderson | 8070e7b | 2013-07-24 09:50:00 -1000 | [diff] [blame] | 8425 | #ifdef TARGET_NR_umount2 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8426 | case TARGET_NR_umount2: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8427 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8428 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8429 | ret = get_errno(umount2(p, arg2)); |
| 8430 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8431 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8432 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8433 | case TARGET_NR_ioctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8434 | return do_ioctl(arg1, arg2, arg3); |
Michael Clark | 47ae93c | 2018-03-03 01:31:11 +1300 | [diff] [blame] | 8435 | #ifdef TARGET_NR_fcntl |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8436 | case TARGET_NR_fcntl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8437 | return do_fcntl(arg1, arg2, arg3); |
Michael Clark | 47ae93c | 2018-03-03 01:31:11 +1300 | [diff] [blame] | 8438 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8439 | case TARGET_NR_setpgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8440 | return get_errno(setpgid(arg1, arg2)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8441 | case TARGET_NR_umask: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8442 | return get_errno(umask(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8443 | case TARGET_NR_chroot: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8444 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8445 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8446 | ret = get_errno(chroot(p)); |
| 8447 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8448 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8449 | #ifdef TARGET_NR_dup2 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8450 | case TARGET_NR_dup2: |
| 8451 | ret = get_errno(dup2(arg1, arg2)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 8452 | if (ret >= 0) { |
| 8453 | fd_trans_dup(arg1, arg2); |
| 8454 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8455 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8456 | #endif |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 8457 | #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3) |
| 8458 | case TARGET_NR_dup3: |
Peter Maydell | 10fa993 | 2017-12-15 15:18:00 +0000 | [diff] [blame] | 8459 | { |
| 8460 | int host_flags; |
| 8461 | |
| 8462 | if ((arg3 & ~TARGET_O_CLOEXEC) != 0) { |
| 8463 | return -EINVAL; |
| 8464 | } |
| 8465 | host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl); |
| 8466 | ret = get_errno(dup3(arg1, arg2, host_flags)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 8467 | if (ret >= 0) { |
| 8468 | fd_trans_dup(arg1, arg2); |
| 8469 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8470 | return ret; |
Peter Maydell | 10fa993 | 2017-12-15 15:18:00 +0000 | [diff] [blame] | 8471 | } |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 8472 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8473 | #ifdef TARGET_NR_getppid /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8474 | case TARGET_NR_getppid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8475 | return get_errno(getppid()); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8476 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8477 | #ifdef TARGET_NR_getpgrp |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8478 | case TARGET_NR_getpgrp: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8479 | return get_errno(getpgrp()); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8480 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8481 | case TARGET_NR_setsid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8482 | return get_errno(setsid()); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8483 | #ifdef TARGET_NR_sigaction |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8484 | case TARGET_NR_sigaction: |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8485 | { |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8486 | #if defined(TARGET_ALPHA) |
| 8487 | struct target_sigaction act, oact, *pact = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8488 | struct target_old_sigaction *old_act; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8489 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8490 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8491 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8492 | act._sa_handler = old_act->_sa_handler; |
| 8493 | target_siginitset(&act.sa_mask, old_act->sa_mask); |
| 8494 | act.sa_flags = old_act->sa_flags; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8495 | act.sa_restorer = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8496 | unlock_user_struct(old_act, arg2, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8497 | pact = &act; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8498 | } |
| 8499 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8500 | if (!is_error(ret) && arg3) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8501 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8502 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8503 | old_act->_sa_handler = oact._sa_handler; |
| 8504 | old_act->sa_mask = oact.sa_mask.sig[0]; |
| 8505 | old_act->sa_flags = oact.sa_flags; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8506 | unlock_user_struct(old_act, arg3, 1); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8507 | } |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8508 | #elif defined(TARGET_MIPS) |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 8509 | struct target_sigaction act, oact, *pact, *old_act; |
| 8510 | |
| 8511 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8512 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8513 | return -TARGET_EFAULT; |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 8514 | act._sa_handler = old_act->_sa_handler; |
| 8515 | target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]); |
| 8516 | act.sa_flags = old_act->sa_flags; |
| 8517 | unlock_user_struct(old_act, arg2, 0); |
| 8518 | pact = &act; |
| 8519 | } else { |
| 8520 | pact = NULL; |
| 8521 | } |
| 8522 | |
| 8523 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
| 8524 | |
| 8525 | if (!is_error(ret) && arg3) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8526 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8527 | return -TARGET_EFAULT; |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 8528 | old_act->_sa_handler = oact._sa_handler; |
| 8529 | old_act->sa_flags = oact.sa_flags; |
| 8530 | old_act->sa_mask.sig[0] = oact.sa_mask.sig[0]; |
| 8531 | old_act->sa_mask.sig[1] = 0; |
| 8532 | old_act->sa_mask.sig[2] = 0; |
| 8533 | old_act->sa_mask.sig[3] = 0; |
| 8534 | unlock_user_struct(old_act, arg3, 1); |
| 8535 | } |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8536 | #else |
| 8537 | struct target_old_sigaction *old_act; |
| 8538 | struct target_sigaction act, oact, *pact; |
| 8539 | if (arg2) { |
| 8540 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8541 | return -TARGET_EFAULT; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8542 | act._sa_handler = old_act->_sa_handler; |
| 8543 | target_siginitset(&act.sa_mask, old_act->sa_mask); |
| 8544 | act.sa_flags = old_act->sa_flags; |
| 8545 | act.sa_restorer = old_act->sa_restorer; |
Laurent Vivier | 5de154e | 2018-04-02 12:24:52 +0200 | [diff] [blame] | 8546 | #ifdef TARGET_ARCH_HAS_KA_RESTORER |
| 8547 | act.ka_restorer = 0; |
| 8548 | #endif |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8549 | unlock_user_struct(old_act, arg2, 0); |
| 8550 | pact = &act; |
| 8551 | } else { |
| 8552 | pact = NULL; |
| 8553 | } |
| 8554 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
| 8555 | if (!is_error(ret) && arg3) { |
| 8556 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8557 | return -TARGET_EFAULT; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8558 | old_act->_sa_handler = oact._sa_handler; |
| 8559 | old_act->sa_mask = oact.sa_mask.sig[0]; |
| 8560 | old_act->sa_flags = oact.sa_flags; |
| 8561 | old_act->sa_restorer = oact.sa_restorer; |
| 8562 | unlock_user_struct(old_act, arg3, 1); |
| 8563 | } |
ths | 388bb21 | 2007-05-13 13:58:00 +0000 | [diff] [blame] | 8564 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8565 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8566 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8567 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8568 | case TARGET_NR_rt_sigaction: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8569 | { |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8570 | #if defined(TARGET_ALPHA) |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8571 | /* For Alpha and SPARC this is a 5 argument syscall, with |
| 8572 | * a 'restorer' parameter which must be copied into the |
| 8573 | * sa_restorer field of the sigaction struct. |
| 8574 | * For Alpha that 'restorer' is arg5; for SPARC it is arg4, |
| 8575 | * and arg5 is the sigsetsize. |
| 8576 | * Alpha also has a separate rt_sigaction struct that it uses |
| 8577 | * here; SPARC uses the usual sigaction struct. |
| 8578 | */ |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8579 | struct target_rt_sigaction *rt_act; |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8580 | struct target_sigaction act, oact, *pact = 0; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8581 | |
| 8582 | if (arg4 != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8583 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8584 | } |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8585 | if (arg2) { |
| 8586 | if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8587 | return -TARGET_EFAULT; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8588 | act._sa_handler = rt_act->_sa_handler; |
| 8589 | act.sa_mask = rt_act->sa_mask; |
| 8590 | act.sa_flags = rt_act->sa_flags; |
| 8591 | act.sa_restorer = arg5; |
| 8592 | unlock_user_struct(rt_act, arg2, 0); |
| 8593 | pact = &act; |
| 8594 | } |
| 8595 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
| 8596 | if (!is_error(ret) && arg3) { |
| 8597 | if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8598 | return -TARGET_EFAULT; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8599 | rt_act->_sa_handler = oact._sa_handler; |
| 8600 | rt_act->sa_mask = oact.sa_mask; |
| 8601 | rt_act->sa_flags = oact.sa_flags; |
| 8602 | unlock_user_struct(rt_act, arg3, 1); |
| 8603 | } |
| 8604 | #else |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8605 | #ifdef TARGET_SPARC |
| 8606 | target_ulong restorer = arg4; |
| 8607 | target_ulong sigsetsize = arg5; |
| 8608 | #else |
| 8609 | target_ulong sigsetsize = arg4; |
| 8610 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8611 | struct target_sigaction *act; |
| 8612 | struct target_sigaction *oact; |
| 8613 | |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8614 | if (sigsetsize != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8615 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8616 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8617 | if (arg2) { |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8618 | if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8619 | return -TARGET_EFAULT; |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8620 | } |
Laurent Vivier | 5de154e | 2018-04-02 12:24:52 +0200 | [diff] [blame] | 8621 | #ifdef TARGET_ARCH_HAS_KA_RESTORER |
| 8622 | act->ka_restorer = restorer; |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8623 | #endif |
| 8624 | } else { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8625 | act = NULL; |
Peter Maydell | 78bfef7 | 2017-11-06 18:33:26 +0000 | [diff] [blame] | 8626 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8627 | if (arg3) { |
| 8628 | if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) { |
| 8629 | ret = -TARGET_EFAULT; |
| 8630 | goto rt_sigaction_fail; |
| 8631 | } |
| 8632 | } else |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8633 | oact = NULL; |
| 8634 | ret = get_errno(do_sigaction(arg1, act, oact)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8635 | rt_sigaction_fail: |
| 8636 | if (act) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8637 | unlock_user_struct(act, arg2, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8638 | if (oact) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8639 | unlock_user_struct(oact, arg3, 1); |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 8640 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8641 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8642 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8643 | #ifdef TARGET_NR_sgetmask /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8644 | case TARGET_NR_sgetmask: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8645 | { |
| 8646 | sigset_t cur_set; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8647 | abi_ulong target_set; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8648 | ret = do_sigprocmask(0, NULL, &cur_set); |
| 8649 | if (!ret) { |
| 8650 | host_to_target_old_sigset(&target_set, &cur_set); |
| 8651 | ret = target_set; |
| 8652 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8653 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8654 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8655 | #endif |
| 8656 | #ifdef TARGET_NR_ssetmask /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8657 | case TARGET_NR_ssetmask: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8658 | { |
Miloš Stojanović | a8617d8 | 2017-05-15 16:59:43 +0200 | [diff] [blame] | 8659 | sigset_t set, oset; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8660 | abi_ulong target_set = arg1; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8661 | target_to_host_old_sigset(&set, &target_set); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8662 | ret = do_sigprocmask(SIG_SETMASK, &set, &oset); |
| 8663 | if (!ret) { |
| 8664 | host_to_target_old_sigset(&target_set, &oset); |
| 8665 | ret = target_set; |
| 8666 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8667 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8668 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8669 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8670 | #ifdef TARGET_NR_sigprocmask |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8671 | case TARGET_NR_sigprocmask: |
| 8672 | { |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8673 | #if defined(TARGET_ALPHA) |
| 8674 | sigset_t set, oldset; |
| 8675 | abi_ulong mask; |
| 8676 | int how; |
| 8677 | |
| 8678 | switch (arg1) { |
| 8679 | case TARGET_SIG_BLOCK: |
| 8680 | how = SIG_BLOCK; |
| 8681 | break; |
| 8682 | case TARGET_SIG_UNBLOCK: |
| 8683 | how = SIG_UNBLOCK; |
| 8684 | break; |
| 8685 | case TARGET_SIG_SETMASK: |
| 8686 | how = SIG_SETMASK; |
| 8687 | break; |
| 8688 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 8689 | return -TARGET_EINVAL; |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8690 | } |
| 8691 | mask = arg2; |
| 8692 | target_to_host_old_sigset(&set, &mask); |
| 8693 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8694 | ret = do_sigprocmask(how, &set, &oldset); |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8695 | if (!is_error(ret)) { |
| 8696 | host_to_target_old_sigset(&mask, &oldset); |
| 8697 | ret = mask; |
Richard Henderson | 0229f5a | 2012-06-07 15:02:49 -0700 | [diff] [blame] | 8698 | ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */ |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8699 | } |
| 8700 | #else |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8701 | sigset_t set, oldset, *set_ptr; |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8702 | int how; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8703 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8704 | if (arg2) { |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8705 | switch (arg1) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8706 | case TARGET_SIG_BLOCK: |
| 8707 | how = SIG_BLOCK; |
| 8708 | break; |
| 8709 | case TARGET_SIG_UNBLOCK: |
| 8710 | how = SIG_UNBLOCK; |
| 8711 | break; |
| 8712 | case TARGET_SIG_SETMASK: |
| 8713 | how = SIG_SETMASK; |
| 8714 | break; |
| 8715 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 8716 | return -TARGET_EINVAL; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8717 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8718 | if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8719 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8720 | target_to_host_old_sigset(&set, p); |
| 8721 | unlock_user(p, arg2, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8722 | set_ptr = &set; |
| 8723 | } else { |
| 8724 | how = 0; |
| 8725 | set_ptr = NULL; |
| 8726 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8727 | ret = do_sigprocmask(how, set_ptr, &oldset); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8728 | if (!is_error(ret) && arg3) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8729 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8730 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8731 | host_to_target_old_sigset(p, &oldset); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8732 | unlock_user(p, arg3, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8733 | } |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 8734 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8735 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8736 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8737 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8738 | case TARGET_NR_rt_sigprocmask: |
| 8739 | { |
| 8740 | int how = arg1; |
| 8741 | sigset_t set, oldset, *set_ptr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8742 | |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8743 | if (arg4 != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8744 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8745 | } |
| 8746 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8747 | if (arg2) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8748 | switch(how) { |
| 8749 | case TARGET_SIG_BLOCK: |
| 8750 | how = SIG_BLOCK; |
| 8751 | break; |
| 8752 | case TARGET_SIG_UNBLOCK: |
| 8753 | how = SIG_UNBLOCK; |
| 8754 | break; |
| 8755 | case TARGET_SIG_SETMASK: |
| 8756 | how = SIG_SETMASK; |
| 8757 | break; |
| 8758 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 8759 | return -TARGET_EINVAL; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8760 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8761 | if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8762 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8763 | target_to_host_sigset(&set, p); |
| 8764 | unlock_user(p, arg2, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8765 | set_ptr = &set; |
| 8766 | } else { |
| 8767 | how = 0; |
| 8768 | set_ptr = NULL; |
| 8769 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8770 | ret = do_sigprocmask(how, set_ptr, &oldset); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8771 | if (!is_error(ret) && arg3) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8772 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8773 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8774 | host_to_target_sigset(p, &oldset); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8775 | unlock_user(p, arg3, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8776 | } |
| 8777 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8778 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8779 | #ifdef TARGET_NR_sigpending |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8780 | case TARGET_NR_sigpending: |
| 8781 | { |
| 8782 | sigset_t set; |
| 8783 | ret = get_errno(sigpending(&set)); |
| 8784 | if (!is_error(ret)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8785 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8786 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8787 | host_to_target_old_sigset(p, &set); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8788 | unlock_user(p, arg1, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8789 | } |
| 8790 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8791 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8792 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8793 | case TARGET_NR_rt_sigpending: |
| 8794 | { |
| 8795 | sigset_t set; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8796 | |
| 8797 | /* Yes, this check is >, not != like most. We follow the kernel's |
| 8798 | * logic and it does it like this because it implements |
| 8799 | * NR_sigpending through the same code path, and in that case |
| 8800 | * the old_sigset_t is smaller in size. |
| 8801 | */ |
| 8802 | if (arg2 > sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8803 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8804 | } |
| 8805 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8806 | ret = get_errno(sigpending(&set)); |
| 8807 | if (!is_error(ret)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8808 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8809 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8810 | host_to_target_sigset(p, &set); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8811 | unlock_user(p, arg1, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8812 | } |
| 8813 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8814 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8815 | #ifdef TARGET_NR_sigsuspend |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8816 | case TARGET_NR_sigsuspend: |
| 8817 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8818 | TaskState *ts = cpu->opaque; |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 8819 | #if defined(TARGET_ALPHA) |
| 8820 | abi_ulong mask = arg1; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8821 | target_to_host_old_sigset(&ts->sigsuspend_mask, &mask); |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 8822 | #else |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8823 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8824 | return -TARGET_EFAULT; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8825 | target_to_host_old_sigset(&ts->sigsuspend_mask, p); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8826 | unlock_user(p, arg1, 0); |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 8827 | #endif |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8828 | ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask, |
| 8829 | SIGSET_T_SIZE)); |
| 8830 | if (ret != -TARGET_ERESTARTSYS) { |
| 8831 | ts->in_sigsuspend = 1; |
| 8832 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8833 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8834 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8835 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8836 | case TARGET_NR_rt_sigsuspend: |
| 8837 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8838 | TaskState *ts = cpu->opaque; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8839 | |
| 8840 | if (arg2 != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8841 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8842 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8843 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8844 | return -TARGET_EFAULT; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8845 | target_to_host_sigset(&ts->sigsuspend_mask, p); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8846 | unlock_user(p, arg1, 0); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8847 | ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask, |
| 8848 | SIGSET_T_SIZE)); |
| 8849 | if (ret != -TARGET_ERESTARTSYS) { |
| 8850 | ts->in_sigsuspend = 1; |
| 8851 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8852 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8853 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 8854 | #ifdef TARGET_NR_rt_sigtimedwait |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8855 | case TARGET_NR_rt_sigtimedwait: |
| 8856 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8857 | sigset_t set; |
| 8858 | struct timespec uts, *puts; |
| 8859 | siginfo_t uinfo; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8860 | |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8861 | if (arg4 != sizeof(target_sigset_t)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8862 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 8863 | } |
| 8864 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 8865 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8866 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8867 | target_to_host_sigset(&set, p); |
| 8868 | unlock_user(p, arg1, 0); |
| 8869 | if (arg3) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8870 | puts = &uts; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8871 | target_to_host_timespec(puts, arg3); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8872 | } else { |
| 8873 | puts = NULL; |
| 8874 | } |
Peter Maydell | b3f8233 | 2016-06-06 19:58:08 +0100 | [diff] [blame] | 8875 | ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts, |
| 8876 | SIGSET_T_SIZE)); |
Petar Jovanovic | 974a196 | 2014-03-03 15:07:41 +0100 | [diff] [blame] | 8877 | if (!is_error(ret)) { |
| 8878 | if (arg2) { |
| 8879 | p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), |
| 8880 | 0); |
| 8881 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8882 | return -TARGET_EFAULT; |
Petar Jovanovic | 974a196 | 2014-03-03 15:07:41 +0100 | [diff] [blame] | 8883 | } |
| 8884 | host_to_target_siginfo(p, &uinfo); |
| 8885 | unlock_user(p, arg2, sizeof(target_siginfo_t)); |
| 8886 | } |
| 8887 | ret = host_to_target_signal(ret); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8888 | } |
| 8889 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8890 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 8891 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8892 | case TARGET_NR_rt_sigqueueinfo: |
| 8893 | { |
| 8894 | siginfo_t uinfo; |
Peter Maydell | 4debae6 | 2016-06-20 15:50:36 +0100 | [diff] [blame] | 8895 | |
| 8896 | p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1); |
| 8897 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8898 | return -TARGET_EFAULT; |
Peter Maydell | 4debae6 | 2016-06-20 15:50:36 +0100 | [diff] [blame] | 8899 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8900 | target_to_host_siginfo(&uinfo, p); |
Miloš Stojanović | d8b6d89 | 2017-05-15 16:59:44 +0200 | [diff] [blame] | 8901 | unlock_user(p, arg3, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8902 | ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo)); |
| 8903 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8904 | return ret; |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 8905 | case TARGET_NR_rt_tgsigqueueinfo: |
| 8906 | { |
| 8907 | siginfo_t uinfo; |
| 8908 | |
| 8909 | p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1); |
| 8910 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8911 | return -TARGET_EFAULT; |
Miloš Stojanović | cf8b8bf | 2017-05-15 16:59:46 +0200 | [diff] [blame] | 8912 | } |
| 8913 | target_to_host_siginfo(&uinfo, p); |
| 8914 | unlock_user(p, arg4, 0); |
| 8915 | ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo)); |
| 8916 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8917 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8918 | #ifdef TARGET_NR_sigreturn |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8919 | case TARGET_NR_sigreturn: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8920 | if (block_signals()) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8921 | return -TARGET_ERESTARTSYS; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8922 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8923 | return do_sigreturn(cpu_env); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8924 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8925 | case TARGET_NR_rt_sigreturn: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8926 | if (block_signals()) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8927 | return -TARGET_ERESTARTSYS; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 8928 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8929 | return do_rt_sigreturn(cpu_env); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8930 | case TARGET_NR_sethostname: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8931 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8932 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8933 | ret = get_errno(sethostname(p, arg2)); |
| 8934 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8935 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 8936 | #ifdef TARGET_NR_setrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8937 | case TARGET_NR_setrlimit: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8938 | { |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 8939 | int resource = target_to_host_resource(arg1); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8940 | struct target_rlimit *target_rlim; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8941 | struct rlimit rlim; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8942 | if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8943 | return -TARGET_EFAULT; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 8944 | rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur); |
| 8945 | rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8946 | unlock_user_struct(target_rlim, arg2, 0); |
Max Filippov | 5dfa88f | 2018-09-17 11:13:14 -0700 | [diff] [blame] | 8947 | /* |
| 8948 | * If we just passed through resource limit settings for memory then |
| 8949 | * they would also apply to QEMU's own allocations, and QEMU will |
| 8950 | * crash or hang or die if its allocations fail. Ideally we would |
| 8951 | * track the guest allocations in QEMU and apply the limits ourselves. |
| 8952 | * For now, just tell the guest the call succeeded but don't actually |
| 8953 | * limit anything. |
| 8954 | */ |
| 8955 | if (resource != RLIMIT_AS && |
| 8956 | resource != RLIMIT_DATA && |
| 8957 | resource != RLIMIT_STACK) { |
| 8958 | return get_errno(setrlimit(resource, &rlim)); |
| 8959 | } else { |
| 8960 | return 0; |
| 8961 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8962 | } |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 8963 | #endif |
| 8964 | #ifdef TARGET_NR_getrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8965 | case TARGET_NR_getrlimit: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8966 | { |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 8967 | int resource = target_to_host_resource(arg1); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8968 | struct target_rlimit *target_rlim; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8969 | struct rlimit rlim; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8970 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8971 | ret = get_errno(getrlimit(resource, &rlim)); |
| 8972 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8973 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 8974 | return -TARGET_EFAULT; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 8975 | target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); |
| 8976 | target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8977 | unlock_user_struct(target_rlim, arg2, 1); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8978 | } |
| 8979 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8980 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 8981 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8982 | case TARGET_NR_getrusage: |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 8983 | { |
| 8984 | struct rusage rusage; |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 8985 | ret = get_errno(getrusage(arg1, &rusage)); |
| 8986 | if (!is_error(ret)) { |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 8987 | ret = host_to_target_rusage(arg2, &rusage); |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 8988 | } |
| 8989 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 8990 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 8991 | #if defined(TARGET_NR_gettimeofday) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8992 | case TARGET_NR_gettimeofday: |
| 8993 | { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8994 | struct timeval tv; |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 8995 | struct timezone tz; |
| 8996 | |
| 8997 | ret = get_errno(gettimeofday(&tv, &tz)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8998 | if (!is_error(ret)) { |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 8999 | if (arg1 && copy_to_user_timeval(arg1, &tv)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9000 | return -TARGET_EFAULT; |
Richard Henderson | a52f5f8 | 2020-02-12 19:22:23 -0800 | [diff] [blame] | 9001 | } |
| 9002 | if (arg2 && copy_to_user_timezone(arg2, &tz)) { |
| 9003 | return -TARGET_EFAULT; |
| 9004 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9005 | } |
| 9006 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9007 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9008 | #endif |
| 9009 | #if defined(TARGET_NR_settimeofday) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9010 | case TARGET_NR_settimeofday: |
| 9011 | { |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 9012 | struct timeval tv, *ptv = NULL; |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 9013 | struct timezone tz, *ptz = NULL; |
| 9014 | |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 9015 | if (arg1) { |
| 9016 | if (copy_from_user_timeval(&tv, arg1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9017 | return -TARGET_EFAULT; |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 9018 | } |
| 9019 | ptv = &tv; |
| 9020 | } |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 9021 | |
| 9022 | if (arg2) { |
| 9023 | if (copy_from_user_timezone(&tz, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9024 | return -TARGET_EFAULT; |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 9025 | } |
| 9026 | ptz = &tz; |
| 9027 | } |
| 9028 | |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9029 | return get_errno(settimeofday(ptv, ptz)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9030 | } |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9031 | #endif |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 9032 | #if defined(TARGET_NR_select) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9033 | case TARGET_NR_select: |
Laurent Vivier | 5457dc9 | 2016-07-08 01:17:27 +0200 | [diff] [blame] | 9034 | #if defined(TARGET_WANT_NI_OLD_SELECT) |
| 9035 | /* some architectures used to have old_select here |
| 9036 | * but now ENOSYS it. |
| 9037 | */ |
| 9038 | ret = -TARGET_ENOSYS; |
| 9039 | #elif defined(TARGET_WANT_OLD_SYS_SELECT) |
| 9040 | ret = do_old_select(arg1); |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 9041 | #else |
Laurent Vivier | 5457dc9 | 2016-07-08 01:17:27 +0200 | [diff] [blame] | 9042 | ret = do_select(arg1, arg2, arg3, arg4, arg5); |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 9043 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9044 | return ret; |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 9045 | #endif |
Riku Voipio | 9e42382 | 2010-05-07 12:28:05 +0000 | [diff] [blame] | 9046 | #ifdef TARGET_NR_pselect6 |
| 9047 | case TARGET_NR_pselect6: |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9048 | { |
| 9049 | abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; |
| 9050 | fd_set rfds, wfds, efds; |
| 9051 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; |
| 9052 | struct timespec ts, *ts_ptr; |
| 9053 | |
| 9054 | /* |
| 9055 | * The 6th arg is actually two args smashed together, |
| 9056 | * so we cannot use the C library. |
| 9057 | */ |
| 9058 | sigset_t set; |
| 9059 | struct { |
| 9060 | sigset_t *set; |
| 9061 | size_t size; |
| 9062 | } sig, *sig_ptr; |
| 9063 | |
| 9064 | abi_ulong arg_sigset, arg_sigsize, *arg7; |
| 9065 | target_sigset_t *target_sigset; |
| 9066 | |
| 9067 | n = arg1; |
| 9068 | rfd_addr = arg2; |
| 9069 | wfd_addr = arg3; |
| 9070 | efd_addr = arg4; |
| 9071 | ts_addr = arg5; |
| 9072 | |
| 9073 | ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); |
| 9074 | if (ret) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9075 | return ret; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9076 | } |
| 9077 | ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); |
| 9078 | if (ret) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9079 | return ret; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9080 | } |
| 9081 | ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); |
| 9082 | if (ret) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9083 | return ret; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9084 | } |
| 9085 | |
| 9086 | /* |
| 9087 | * This takes a timespec, and not a timeval, so we cannot |
| 9088 | * use the do_select() helper ... |
| 9089 | */ |
| 9090 | if (ts_addr) { |
| 9091 | if (target_to_host_timespec(&ts, ts_addr)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9092 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9093 | } |
| 9094 | ts_ptr = &ts; |
| 9095 | } else { |
| 9096 | ts_ptr = NULL; |
| 9097 | } |
| 9098 | |
| 9099 | /* Extract the two packed args for the sigset */ |
| 9100 | if (arg6) { |
| 9101 | sig_ptr = &sig; |
Peter Maydell | b28a1f3 | 2016-05-27 15:51:47 +0100 | [diff] [blame] | 9102 | sig.size = SIGSET_T_SIZE; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9103 | |
| 9104 | arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); |
| 9105 | if (!arg7) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9106 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9107 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 9108 | arg_sigset = tswapal(arg7[0]); |
| 9109 | arg_sigsize = tswapal(arg7[1]); |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9110 | unlock_user(arg7, arg6, 0); |
| 9111 | |
| 9112 | if (arg_sigset) { |
| 9113 | sig.set = &set; |
Peter Maydell | 8f04eeb | 2011-06-28 12:21:57 +0100 | [diff] [blame] | 9114 | if (arg_sigsize != sizeof(*target_sigset)) { |
| 9115 | /* Like the kernel, we enforce correct size sigsets */ |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9116 | return -TARGET_EINVAL; |
Peter Maydell | 8f04eeb | 2011-06-28 12:21:57 +0100 | [diff] [blame] | 9117 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9118 | target_sigset = lock_user(VERIFY_READ, arg_sigset, |
| 9119 | sizeof(*target_sigset), 1); |
| 9120 | if (!target_sigset) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9121 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9122 | } |
| 9123 | target_to_host_sigset(&set, target_sigset); |
| 9124 | unlock_user(target_sigset, arg_sigset, 0); |
| 9125 | } else { |
| 9126 | sig.set = NULL; |
| 9127 | } |
| 9128 | } else { |
| 9129 | sig_ptr = NULL; |
| 9130 | } |
| 9131 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 9132 | ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, |
| 9133 | ts_ptr, sig_ptr)); |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9134 | |
| 9135 | if (!is_error(ret)) { |
| 9136 | if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9137 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9138 | if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9139 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9140 | if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9141 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9142 | |
| 9143 | if (ts_addr && host_to_target_timespec(ts_addr, &ts)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9144 | return -TARGET_EFAULT; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 9145 | } |
| 9146 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9147 | return ret; |
Riku Voipio | 9e42382 | 2010-05-07 12:28:05 +0000 | [diff] [blame] | 9148 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9149 | #ifdef TARGET_NR_symlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9150 | case TARGET_NR_symlink: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9151 | { |
| 9152 | void *p2; |
| 9153 | p = lock_user_string(arg1); |
| 9154 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9155 | if (!p || !p2) |
| 9156 | ret = -TARGET_EFAULT; |
| 9157 | else |
| 9158 | ret = get_errno(symlink(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9159 | unlock_user(p2, arg2, 0); |
| 9160 | unlock_user(p, arg1, 0); |
| 9161 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9162 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9163 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9164 | #if defined(TARGET_NR_symlinkat) |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 9165 | case TARGET_NR_symlinkat: |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 9166 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9167 | void *p2; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 9168 | p = lock_user_string(arg1); |
| 9169 | p2 = lock_user_string(arg3); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9170 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 9171 | ret = -TARGET_EFAULT; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 9172 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9173 | ret = get_errno(symlinkat(p, arg2, p2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9174 | unlock_user(p2, arg3, 0); |
| 9175 | unlock_user(p, arg1, 0); |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 9176 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9177 | return ret; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 9178 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9179 | #ifdef TARGET_NR_readlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9180 | case TARGET_NR_readlink: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9181 | { |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 9182 | void *p2; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9183 | p = lock_user_string(arg1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9184 | p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 9185 | if (!p || !p2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9186 | ret = -TARGET_EFAULT; |
Mike Frysinger | f17f498 | 2014-08-08 09:40:25 +0900 | [diff] [blame] | 9187 | } else if (!arg3) { |
| 9188 | /* Short circuit this for the magic exe check. */ |
| 9189 | ret = -TARGET_EINVAL; |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 9190 | } else if (is_proc_myself((const char *)p, "exe")) { |
| 9191 | char real[PATH_MAX], *temp; |
| 9192 | temp = realpath(exec_path, real); |
Mike Frysinger | f17f498 | 2014-08-08 09:40:25 +0900 | [diff] [blame] | 9193 | /* Return value is # of bytes that we wrote to the buffer. */ |
| 9194 | if (temp == NULL) { |
| 9195 | ret = get_errno(-1); |
| 9196 | } else { |
| 9197 | /* Don't worry about sign mismatch as earlier mapping |
| 9198 | * logic would have thrown a bad address error. */ |
| 9199 | ret = MIN(strlen(real), arg3); |
| 9200 | /* We cannot NUL terminate the string. */ |
| 9201 | memcpy(p2, real, ret); |
| 9202 | } |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 9203 | } else { |
| 9204 | ret = get_errno(readlink(path(p), p2, arg3)); |
aurel32 | d088d66 | 2009-01-30 20:09:01 +0000 | [diff] [blame] | 9205 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9206 | unlock_user(p2, arg2, ret); |
| 9207 | unlock_user(p, arg1, 0); |
| 9208 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9209 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9210 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9211 | #if defined(TARGET_NR_readlinkat) |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 9212 | case TARGET_NR_readlinkat: |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 9213 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9214 | void *p2; |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 9215 | p = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9216 | p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 9217 | if (!p || !p2) { |
| 9218 | ret = -TARGET_EFAULT; |
| 9219 | } else if (is_proc_myself((const char *)p, "exe")) { |
| 9220 | char real[PATH_MAX], *temp; |
| 9221 | temp = realpath(exec_path, real); |
| 9222 | ret = temp == NULL ? get_errno(-1) : strlen(real) ; |
| 9223 | snprintf((char *)p2, arg4, "%s", real); |
| 9224 | } else { |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9225 | ret = get_errno(readlinkat(arg1, path(p), p2, arg4)); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 9226 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9227 | unlock_user(p2, arg3, ret); |
| 9228 | unlock_user(p, arg2, 0); |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 9229 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9230 | return ret; |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 9231 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9232 | #ifdef TARGET_NR_swapon |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9233 | case TARGET_NR_swapon: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9234 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9235 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9236 | ret = get_errno(swapon(p, arg2)); |
| 9237 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9238 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9239 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9240 | case TARGET_NR_reboot: |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 9241 | if (arg3 == LINUX_REBOOT_CMD_RESTART2) { |
| 9242 | /* arg4 must be ignored in all other cases */ |
| 9243 | p = lock_user_string(arg4); |
| 9244 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9245 | return -TARGET_EFAULT; |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 9246 | } |
| 9247 | ret = get_errno(reboot(arg1, arg2, arg3, p)); |
| 9248 | unlock_user(p, arg4, 0); |
| 9249 | } else { |
| 9250 | ret = get_errno(reboot(arg1, arg2, arg3, NULL)); |
| 9251 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9252 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9253 | #ifdef TARGET_NR_mmap |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9254 | case TARGET_NR_mmap: |
Alexander Graf | 0970119 | 2013-09-03 20:12:15 +0100 | [diff] [blame] | 9255 | #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \ |
| 9256 | (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \ |
Ulrich Hecht | a4c075f | 2009-07-24 16:57:31 +0200 | [diff] [blame] | 9257 | defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \ |
| 9258 | || defined(TARGET_S390X) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9259 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9260 | abi_ulong *v; |
| 9261 | abi_ulong v1, v2, v3, v4, v5, v6; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9262 | if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9263 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 9264 | v1 = tswapal(v[0]); |
| 9265 | v2 = tswapal(v[1]); |
| 9266 | v3 = tswapal(v[2]); |
| 9267 | v4 = tswapal(v[3]); |
| 9268 | v5 = tswapal(v[4]); |
| 9269 | v6 = tswapal(v[5]); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9270 | unlock_user(v, arg1, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 9271 | ret = get_errno(target_mmap(v1, v2, v3, |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 9272 | target_to_host_bitmask(v4, mmap_flags_tbl), |
| 9273 | v5, v6)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9274 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9275 | #else |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 9276 | ret = get_errno(target_mmap(arg1, arg2, arg3, |
| 9277 | target_to_host_bitmask(arg4, mmap_flags_tbl), |
bellard | 6fb883e | 2003-07-09 17:12:39 +0000 | [diff] [blame] | 9278 | arg5, |
| 9279 | arg6)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9280 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9281 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9282 | #endif |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9283 | #ifdef TARGET_NR_mmap2 |
bellard | 6fb883e | 2003-07-09 17:12:39 +0000 | [diff] [blame] | 9284 | case TARGET_NR_mmap2: |
pbrook | bb7ec04 | 2008-03-25 22:28:25 +0000 | [diff] [blame] | 9285 | #ifndef MMAP_SHIFT |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 9286 | #define MMAP_SHIFT 12 |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 9287 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9288 | ret = target_mmap(arg1, arg2, arg3, |
| 9289 | target_to_host_bitmask(arg4, mmap_flags_tbl), |
| 9290 | arg5, arg6 << MMAP_SHIFT); |
| 9291 | return get_errno(ret); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9292 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9293 | case TARGET_NR_munmap: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9294 | return get_errno(target_munmap(arg1, arg2)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9295 | case TARGET_NR_mprotect: |
Paul Brook | 97374d3 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 9296 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 9297 | TaskState *ts = cpu->opaque; |
Paul Brook | 97374d3 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 9298 | /* Special hack to detect libc making the stack executable. */ |
| 9299 | if ((arg3 & PROT_GROWSDOWN) |
| 9300 | && arg1 >= ts->info->stack_limit |
| 9301 | && arg1 <= ts->info->start_stack) { |
| 9302 | arg3 &= ~PROT_GROWSDOWN; |
| 9303 | arg2 = arg2 + arg1 - ts->info->stack_limit; |
| 9304 | arg1 = ts->info->stack_limit; |
| 9305 | } |
| 9306 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9307 | return get_errno(target_mprotect(arg1, arg2, arg3)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9308 | #ifdef TARGET_NR_mremap |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9309 | case TARGET_NR_mremap: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9310 | return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9311 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9312 | /* ??? msync/mlock/munlock are broken for softmmu. */ |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9313 | #ifdef TARGET_NR_msync |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9314 | case TARGET_NR_msync: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9315 | return get_errno(msync(g2h(arg1), arg2, arg3)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9316 | #endif |
| 9317 | #ifdef TARGET_NR_mlock |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9318 | case TARGET_NR_mlock: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9319 | return get_errno(mlock(g2h(arg1), arg2)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9320 | #endif |
| 9321 | #ifdef TARGET_NR_munlock |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9322 | case TARGET_NR_munlock: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9323 | return get_errno(munlock(g2h(arg1), arg2)); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | #ifdef TARGET_NR_mlockall |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9326 | case TARGET_NR_mlockall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9327 | return get_errno(mlockall(target_to_host_mlockall_arg(arg1))); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9328 | #endif |
| 9329 | #ifdef TARGET_NR_munlockall |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9330 | case TARGET_NR_munlockall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9331 | return get_errno(munlockall()); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9332 | #endif |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9333 | #ifdef TARGET_NR_truncate |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9334 | case TARGET_NR_truncate: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9335 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9336 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9337 | ret = get_errno(truncate(p, arg2)); |
| 9338 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9339 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9340 | #endif |
| 9341 | #ifdef TARGET_NR_ftruncate |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9342 | case TARGET_NR_ftruncate: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9343 | return get_errno(ftruncate(arg1, arg2)); |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9344 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9345 | case TARGET_NR_fchmod: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9346 | return get_errno(fchmod(arg1, arg2)); |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9347 | #if defined(TARGET_NR_fchmodat) |
ths | 814d797 | 2007-09-24 09:26:51 +0000 | [diff] [blame] | 9348 | case TARGET_NR_fchmodat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9349 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9350 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9351 | ret = get_errno(fchmodat(arg1, p, arg3, 0)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9352 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9353 | return ret; |
ths | 814d797 | 2007-09-24 09:26:51 +0000 | [diff] [blame] | 9354 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9355 | case TARGET_NR_getpriority: |
Richard Henderson | 95c0982 | 2012-06-07 15:14:50 -0700 | [diff] [blame] | 9356 | /* Note that negative values are valid for getpriority, so we must |
| 9357 | differentiate based on errno settings. */ |
| 9358 | errno = 0; |
| 9359 | ret = getpriority(arg1, arg2); |
| 9360 | if (ret == -1 && errno != 0) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9361 | return -host_to_target_errno(errno); |
Richard Henderson | 95c0982 | 2012-06-07 15:14:50 -0700 | [diff] [blame] | 9362 | } |
| 9363 | #ifdef TARGET_ALPHA |
| 9364 | /* Return value is the unbiased priority. Signal no error. */ |
| 9365 | ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; |
| 9366 | #else |
| 9367 | /* Return value is a biased priority to avoid negative numbers. */ |
| 9368 | ret = 20 - ret; |
| 9369 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9370 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9371 | case TARGET_NR_setpriority: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9372 | return get_errno(setpriority(arg1, arg2, arg3)); |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9373 | #ifdef TARGET_NR_statfs |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9374 | case TARGET_NR_statfs: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9375 | if (!(p = lock_user_string(arg1))) { |
| 9376 | return -TARGET_EFAULT; |
| 9377 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9378 | ret = get_errno(statfs(path(p), &stfs)); |
| 9379 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9380 | convert_statfs: |
| 9381 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9382 | struct target_statfs *target_stfs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9383 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9384 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9385 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9386 | __put_user(stfs.f_type, &target_stfs->f_type); |
| 9387 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); |
| 9388 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); |
| 9389 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); |
| 9390 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); |
| 9391 | __put_user(stfs.f_files, &target_stfs->f_files); |
| 9392 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); |
| 9393 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); |
| 9394 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); |
| 9395 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 9396 | __put_user(stfs.f_frsize, &target_stfs->f_frsize); |
Shea Levy | d4247ec | 2018-03-01 06:15:00 -0500 | [diff] [blame] | 9397 | #ifdef _STATFS_F_FLAGS |
| 9398 | __put_user(stfs.f_flags, &target_stfs->f_flags); |
| 9399 | #else |
| 9400 | __put_user(0, &target_stfs->f_flags); |
| 9401 | #endif |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 9402 | memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9403 | unlock_user_struct(target_stfs, arg2, 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9404 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9405 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9406 | #endif |
| 9407 | #ifdef TARGET_NR_fstatfs |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9408 | case TARGET_NR_fstatfs: |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 9409 | ret = get_errno(fstatfs(arg1, &stfs)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9410 | goto convert_statfs; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9411 | #endif |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 9412 | #ifdef TARGET_NR_statfs64 |
| 9413 | case TARGET_NR_statfs64: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9414 | if (!(p = lock_user_string(arg1))) { |
| 9415 | return -TARGET_EFAULT; |
| 9416 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9417 | ret = get_errno(statfs(path(p), &stfs)); |
| 9418 | unlock_user(p, arg1, 0); |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 9419 | convert_statfs64: |
| 9420 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9421 | struct target_statfs64 *target_stfs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9422 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9423 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9424 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9425 | __put_user(stfs.f_type, &target_stfs->f_type); |
| 9426 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); |
| 9427 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); |
| 9428 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); |
| 9429 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); |
| 9430 | __put_user(stfs.f_files, &target_stfs->f_files); |
| 9431 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); |
| 9432 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); |
| 9433 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); |
| 9434 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 9435 | __put_user(stfs.f_frsize, &target_stfs->f_frsize); |
| 9436 | memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9437 | unlock_user_struct(target_stfs, arg3, 1); |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 9438 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9439 | return ret; |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 9440 | case TARGET_NR_fstatfs64: |
| 9441 | ret = get_errno(fstatfs(arg1, &stfs)); |
| 9442 | goto convert_statfs64; |
| 9443 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9444 | #ifdef TARGET_NR_socketcall |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9445 | case TARGET_NR_socketcall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9446 | return do_socketcall(arg1, arg2); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9447 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9448 | #ifdef TARGET_NR_accept |
| 9449 | case TARGET_NR_accept: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9450 | return do_accept4(arg1, arg2, arg3, 0); |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 9451 | #endif |
| 9452 | #ifdef TARGET_NR_accept4 |
| 9453 | case TARGET_NR_accept4: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9454 | return do_accept4(arg1, arg2, arg3, arg4); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9455 | #endif |
| 9456 | #ifdef TARGET_NR_bind |
| 9457 | case TARGET_NR_bind: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9458 | return do_bind(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9459 | #endif |
| 9460 | #ifdef TARGET_NR_connect |
| 9461 | case TARGET_NR_connect: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9462 | return do_connect(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9463 | #endif |
| 9464 | #ifdef TARGET_NR_getpeername |
| 9465 | case TARGET_NR_getpeername: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9466 | return do_getpeername(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9467 | #endif |
| 9468 | #ifdef TARGET_NR_getsockname |
| 9469 | case TARGET_NR_getsockname: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9470 | return do_getsockname(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9471 | #endif |
| 9472 | #ifdef TARGET_NR_getsockopt |
| 9473 | case TARGET_NR_getsockopt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9474 | return do_getsockopt(arg1, arg2, arg3, arg4, arg5); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9475 | #endif |
| 9476 | #ifdef TARGET_NR_listen |
| 9477 | case TARGET_NR_listen: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9478 | return get_errno(listen(arg1, arg2)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9479 | #endif |
| 9480 | #ifdef TARGET_NR_recv |
| 9481 | case TARGET_NR_recv: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9482 | return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9483 | #endif |
| 9484 | #ifdef TARGET_NR_recvfrom |
| 9485 | case TARGET_NR_recvfrom: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9486 | return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9487 | #endif |
| 9488 | #ifdef TARGET_NR_recvmsg |
| 9489 | case TARGET_NR_recvmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9490 | return do_sendrecvmsg(arg1, arg2, arg3, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9491 | #endif |
| 9492 | #ifdef TARGET_NR_send |
| 9493 | case TARGET_NR_send: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9494 | return do_sendto(arg1, arg2, arg3, arg4, 0, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9495 | #endif |
| 9496 | #ifdef TARGET_NR_sendmsg |
| 9497 | case TARGET_NR_sendmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9498 | return do_sendrecvmsg(arg1, arg2, arg3, 1); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9499 | #endif |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 9500 | #ifdef TARGET_NR_sendmmsg |
| 9501 | case TARGET_NR_sendmmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9502 | return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1); |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9503 | #endif |
| 9504 | #ifdef TARGET_NR_recvmmsg |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 9505 | case TARGET_NR_recvmmsg: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9506 | return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0); |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 9507 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9508 | #ifdef TARGET_NR_sendto |
| 9509 | case TARGET_NR_sendto: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9510 | return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9511 | #endif |
| 9512 | #ifdef TARGET_NR_shutdown |
| 9513 | case TARGET_NR_shutdown: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9514 | return get_errno(shutdown(arg1, arg2)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9515 | #endif |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 9516 | #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) |
| 9517 | case TARGET_NR_getrandom: |
| 9518 | p = lock_user(VERIFY_WRITE, arg1, arg2, 0); |
| 9519 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9520 | return -TARGET_EFAULT; |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 9521 | } |
| 9522 | ret = get_errno(getrandom(p, arg2, arg3)); |
| 9523 | unlock_user(p, arg1, ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9524 | return ret; |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 9525 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9526 | #ifdef TARGET_NR_socket |
| 9527 | case TARGET_NR_socket: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9528 | return do_socket(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9529 | #endif |
| 9530 | #ifdef TARGET_NR_socketpair |
| 9531 | case TARGET_NR_socketpair: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9532 | return do_socketpair(arg1, arg2, arg3, arg4); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9533 | #endif |
| 9534 | #ifdef TARGET_NR_setsockopt |
| 9535 | case TARGET_NR_setsockopt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9536 | return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 9537 | #endif |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9538 | #if defined(TARGET_NR_syslog) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9539 | case TARGET_NR_syslog: |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9540 | { |
| 9541 | int len = arg2; |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 9542 | |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9543 | switch (arg1) { |
| 9544 | case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */ |
| 9545 | case TARGET_SYSLOG_ACTION_OPEN: /* Open log */ |
| 9546 | case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ |
| 9547 | case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */ |
| 9548 | case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */ |
| 9549 | case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */ |
| 9550 | case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */ |
| 9551 | case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9552 | return get_errno(sys_syslog((int)arg1, NULL, (int)arg3)); |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9553 | case TARGET_SYSLOG_ACTION_READ: /* Read from log */ |
| 9554 | case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */ |
| 9555 | case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */ |
| 9556 | { |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9557 | if (len < 0) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9558 | return -TARGET_EINVAL; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9559 | } |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9560 | if (len == 0) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9561 | return 0; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9562 | } |
| 9563 | p = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 9564 | if (!p) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9565 | return -TARGET_EFAULT; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9566 | } |
| 9567 | ret = get_errno(sys_syslog((int)arg1, p, (int)arg3)); |
| 9568 | unlock_user(p, arg2, arg3); |
| 9569 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9570 | return ret; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9571 | default: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9572 | return -TARGET_EINVAL; |
Aleksandar Markovic | da2c8ad | 2016-09-22 18:56:58 +0200 | [diff] [blame] | 9573 | } |
| 9574 | } |
| 9575 | break; |
| 9576 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9577 | case TARGET_NR_setitimer: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9578 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9579 | struct itimerval value, ovalue, *pvalue; |
| 9580 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9581 | if (arg2) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9582 | pvalue = &value; |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 9583 | if (copy_from_user_timeval(&pvalue->it_interval, arg2) |
| 9584 | || copy_from_user_timeval(&pvalue->it_value, |
| 9585 | arg2 + sizeof(struct target_timeval))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9586 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9587 | } else { |
| 9588 | pvalue = NULL; |
| 9589 | } |
| 9590 | ret = get_errno(setitimer(arg1, pvalue, &ovalue)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9591 | if (!is_error(ret) && arg3) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 9592 | if (copy_to_user_timeval(arg3, |
| 9593 | &ovalue.it_interval) |
| 9594 | || copy_to_user_timeval(arg3 + sizeof(struct target_timeval), |
| 9595 | &ovalue.it_value)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9596 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9597 | } |
| 9598 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9599 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9600 | case TARGET_NR_getitimer: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9601 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9602 | struct itimerval value; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9603 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9604 | ret = get_errno(getitimer(arg1, &value)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9605 | if (!is_error(ret) && arg2) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 9606 | if (copy_to_user_timeval(arg2, |
| 9607 | &value.it_interval) |
| 9608 | || copy_to_user_timeval(arg2 + sizeof(struct target_timeval), |
| 9609 | &value.it_value)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9610 | return -TARGET_EFAULT; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 9611 | } |
| 9612 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9613 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9614 | #ifdef TARGET_NR_stat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9615 | case TARGET_NR_stat: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9616 | if (!(p = lock_user_string(arg1))) { |
| 9617 | return -TARGET_EFAULT; |
| 9618 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9619 | ret = get_errno(stat(path(p), &st)); |
| 9620 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9621 | goto do_stat; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9622 | #endif |
| 9623 | #ifdef TARGET_NR_lstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9624 | case TARGET_NR_lstat: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9625 | if (!(p = lock_user_string(arg1))) { |
| 9626 | return -TARGET_EFAULT; |
| 9627 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9628 | ret = get_errno(lstat(path(p), &st)); |
| 9629 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9630 | goto do_stat; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9631 | #endif |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9632 | #ifdef TARGET_NR_fstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9633 | case TARGET_NR_fstat: |
| 9634 | { |
| 9635 | ret = get_errno(fstat(arg1, &st)); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9636 | #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9637 | do_stat: |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9638 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9639 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9640 | struct target_stat *target_st; |
ths | e358465 | 2007-06-01 11:49:38 +0000 | [diff] [blame] | 9641 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9642 | if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9643 | return -TARGET_EFAULT; |
Ulrich Hecht | 1272791 | 2009-07-24 19:10:32 +0200 | [diff] [blame] | 9644 | memset(target_st, 0, sizeof(*target_st)); |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 9645 | __put_user(st.st_dev, &target_st->st_dev); |
| 9646 | __put_user(st.st_ino, &target_st->st_ino); |
| 9647 | __put_user(st.st_mode, &target_st->st_mode); |
| 9648 | __put_user(st.st_uid, &target_st->st_uid); |
| 9649 | __put_user(st.st_gid, &target_st->st_gid); |
| 9650 | __put_user(st.st_nlink, &target_st->st_nlink); |
| 9651 | __put_user(st.st_rdev, &target_st->st_rdev); |
| 9652 | __put_user(st.st_size, &target_st->st_size); |
| 9653 | __put_user(st.st_blksize, &target_st->st_blksize); |
| 9654 | __put_user(st.st_blocks, &target_st->st_blocks); |
| 9655 | __put_user(st.st_atime, &target_st->target_st_atime); |
| 9656 | __put_user(st.st_mtime, &target_st->target_st_mtime); |
| 9657 | __put_user(st.st_ctime, &target_st->target_st_ctime); |
Chen-Yu Tsai | 5f992db | 2019-05-23 00:21:47 +0800 | [diff] [blame] | 9658 | #if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \ |
| 9659 | defined(TARGET_STAT_HAVE_NSEC) |
| 9660 | __put_user(st.st_atim.tv_nsec, |
| 9661 | &target_st->target_st_atime_nsec); |
| 9662 | __put_user(st.st_mtim.tv_nsec, |
| 9663 | &target_st->target_st_mtime_nsec); |
| 9664 | __put_user(st.st_ctim.tv_nsec, |
| 9665 | &target_st->target_st_ctime_nsec); |
| 9666 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9667 | unlock_user_struct(target_st, arg2, 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9668 | } |
| 9669 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9670 | return ret; |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 9671 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9672 | case TARGET_NR_vhangup: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9673 | return get_errno(vhangup()); |
bellard | 42ad6ae | 2005-01-03 22:48:11 +0000 | [diff] [blame] | 9674 | #ifdef TARGET_NR_syscall |
| 9675 | case TARGET_NR_syscall: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9676 | return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5, |
| 9677 | arg6, arg7, arg8, 0); |
bellard | 42ad6ae | 2005-01-03 22:48:11 +0000 | [diff] [blame] | 9678 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9679 | #if defined(TARGET_NR_wait4) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9680 | case TARGET_NR_wait4: |
| 9681 | { |
| 9682 | int status; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9683 | abi_long status_ptr = arg2; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9684 | struct rusage rusage, *rusage_ptr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9685 | abi_ulong target_rusage = arg4; |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 9686 | abi_long rusage_err; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9687 | if (target_rusage) |
| 9688 | rusage_ptr = &rusage; |
| 9689 | else |
| 9690 | rusage_ptr = NULL; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 9691 | ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9692 | if (!is_error(ret)) { |
Alexander Graf | 5379557 | 2011-11-24 00:44:43 +0100 | [diff] [blame] | 9693 | if (status_ptr && ret) { |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 9694 | status = host_to_target_waitstatus(status); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 9695 | if (put_user_s32(status, status_ptr)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9696 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9697 | } |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 9698 | if (target_rusage) { |
| 9699 | rusage_err = host_to_target_rusage(target_rusage, &rusage); |
| 9700 | if (rusage_err) { |
| 9701 | ret = rusage_err; |
| 9702 | } |
| 9703 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9704 | } |
| 9705 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9706 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9707 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9708 | #ifdef TARGET_NR_swapoff |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9709 | case TARGET_NR_swapoff: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9710 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9711 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9712 | ret = get_errno(swapoff(p)); |
| 9713 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9714 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9715 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9716 | case TARGET_NR_sysinfo: |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 9717 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9718 | struct target_sysinfo *target_value; |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 9719 | struct sysinfo value; |
| 9720 | ret = get_errno(sysinfo(&value)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9721 | if (!is_error(ret) && arg1) |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 9722 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9723 | if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9724 | return -TARGET_EFAULT; |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 9725 | __put_user(value.uptime, &target_value->uptime); |
| 9726 | __put_user(value.loads[0], &target_value->loads[0]); |
| 9727 | __put_user(value.loads[1], &target_value->loads[1]); |
| 9728 | __put_user(value.loads[2], &target_value->loads[2]); |
| 9729 | __put_user(value.totalram, &target_value->totalram); |
| 9730 | __put_user(value.freeram, &target_value->freeram); |
| 9731 | __put_user(value.sharedram, &target_value->sharedram); |
| 9732 | __put_user(value.bufferram, &target_value->bufferram); |
| 9733 | __put_user(value.totalswap, &target_value->totalswap); |
| 9734 | __put_user(value.freeswap, &target_value->freeswap); |
| 9735 | __put_user(value.procs, &target_value->procs); |
| 9736 | __put_user(value.totalhigh, &target_value->totalhigh); |
| 9737 | __put_user(value.freehigh, &target_value->freehigh); |
| 9738 | __put_user(value.mem_unit, &target_value->mem_unit); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9739 | unlock_user_struct(target_value, arg1, 1); |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 9740 | } |
| 9741 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9742 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9743 | #ifdef TARGET_NR_ipc |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9744 | case TARGET_NR_ipc: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9745 | return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9746 | #endif |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 9747 | #ifdef TARGET_NR_semget |
| 9748 | case TARGET_NR_semget: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9749 | return get_errno(semget(arg1, arg2, arg3)); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 9750 | #endif |
| 9751 | #ifdef TARGET_NR_semop |
| 9752 | case TARGET_NR_semop: |
Matus Kysel | d8c08b1 | 2020-06-26 14:46:11 +0200 | [diff] [blame^] | 9753 | return do_semtimedop(arg1, arg2, arg3, 0); |
| 9754 | #endif |
| 9755 | #ifdef TARGET_NR_semtimedop |
| 9756 | case TARGET_NR_semtimedop: |
| 9757 | return do_semtimedop(arg1, arg2, arg3, arg4); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 9758 | #endif |
| 9759 | #ifdef TARGET_NR_semctl |
| 9760 | case TARGET_NR_semctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9761 | return do_semctl(arg1, arg2, arg3, arg4); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 9762 | #endif |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 9763 | #ifdef TARGET_NR_msgctl |
| 9764 | case TARGET_NR_msgctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9765 | return do_msgctl(arg1, arg2, arg3); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 9766 | #endif |
| 9767 | #ifdef TARGET_NR_msgget |
| 9768 | case TARGET_NR_msgget: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9769 | return get_errno(msgget(arg1, arg2)); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 9770 | #endif |
| 9771 | #ifdef TARGET_NR_msgrcv |
| 9772 | case TARGET_NR_msgrcv: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9773 | return do_msgrcv(arg1, arg2, arg3, arg4, arg5); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 9774 | #endif |
| 9775 | #ifdef TARGET_NR_msgsnd |
| 9776 | case TARGET_NR_msgsnd: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9777 | return do_msgsnd(arg1, arg2, arg3, arg4); |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 9778 | #endif |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 9779 | #ifdef TARGET_NR_shmget |
| 9780 | case TARGET_NR_shmget: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9781 | return get_errno(shmget(arg1, arg2, arg3)); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 9782 | #endif |
| 9783 | #ifdef TARGET_NR_shmctl |
| 9784 | case TARGET_NR_shmctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9785 | return do_shmctl(arg1, arg2, arg3); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 9786 | #endif |
| 9787 | #ifdef TARGET_NR_shmat |
| 9788 | case TARGET_NR_shmat: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9789 | return do_shmat(cpu_env, arg1, arg2, arg3); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef TARGET_NR_shmdt |
| 9792 | case TARGET_NR_shmdt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9793 | return do_shmdt(arg1); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 9794 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9795 | case TARGET_NR_fsync: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9796 | return get_errno(fsync(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9797 | case TARGET_NR_clone: |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 9798 | /* Linux manages to have three different orderings for its |
| 9799 | * arguments to clone(); the BACKWARDS and BACKWARDS2 defines |
| 9800 | * match the kernel's CONFIG_CLONE_* settings. |
| 9801 | * Microblaze is further special in that it uses a sixth |
| 9802 | * implicit argument to clone for the TLS pointer. |
| 9803 | */ |
| 9804 | #if defined(TARGET_MICROBLAZE) |
Edgar E. Iglesias | a5b3bdc | 2012-04-26 14:17:41 +0200 | [diff] [blame] | 9805 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5)); |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 9806 | #elif defined(TARGET_CLONE_BACKWARDS) |
| 9807 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5)); |
| 9808 | #elif defined(TARGET_CLONE_BACKWARDS2) |
Ulrich Hecht | a4c075f | 2009-07-24 16:57:31 +0200 | [diff] [blame] | 9809 | ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4)); |
aurel32 | 0b6d3ae | 2008-09-15 07:43:43 +0000 | [diff] [blame] | 9810 | #else |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 9811 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4)); |
aurel32 | 0b6d3ae | 2008-09-15 07:43:43 +0000 | [diff] [blame] | 9812 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9813 | return ret; |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 9814 | #ifdef __NR_exit_group |
| 9815 | /* new thread calls */ |
| 9816 | case TARGET_NR_exit_group: |
Alex Bennée | 708b6a6 | 2018-06-22 17:09:10 +0100 | [diff] [blame] | 9817 | preexit_cleanup(cpu_env, arg1); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9818 | return get_errno(exit_group(arg1)); |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 9819 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9820 | case TARGET_NR_setdomainname: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9821 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9822 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9823 | ret = get_errno(setdomainname(p, arg2)); |
| 9824 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9825 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9826 | case TARGET_NR_uname: |
| 9827 | /* no need to transcode because we use the linux syscall */ |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 9828 | { |
| 9829 | struct new_utsname * buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9830 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9831 | if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9832 | return -TARGET_EFAULT; |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 9833 | ret = get_errno(sys_uname(buf)); |
| 9834 | if (!is_error(ret)) { |
Peter Maydell | 332c978 | 2016-07-12 13:02:16 +0100 | [diff] [blame] | 9835 | /* Overwrite the native machine name with whatever is being |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 9836 | emulated. */ |
Philippe Mathieu-Daudé | 871f95c | 2017-07-24 15:27:47 -0300 | [diff] [blame] | 9837 | g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env), |
| 9838 | sizeof(buf->machine)); |
pbrook | c593722 | 2006-05-14 11:30:38 +0000 | [diff] [blame] | 9839 | /* Allow the user to override the reported release. */ |
Peter Maydell | 332c978 | 2016-07-12 13:02:16 +0100 | [diff] [blame] | 9840 | if (qemu_uname_release && *qemu_uname_release) { |
| 9841 | g_strlcpy(buf->release, qemu_uname_release, |
| 9842 | sizeof(buf->release)); |
| 9843 | } |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 9844 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9845 | unlock_user_struct(buf, arg1, 1); |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 9846 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9847 | return ret; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 9848 | #ifdef TARGET_I386 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9849 | case TARGET_NR_modify_ldt: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9850 | return do_modify_ldt(cpu_env, arg1, arg2, arg3); |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 9851 | #if !defined(TARGET_X86_64) |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9852 | case TARGET_NR_vm86: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9853 | return do_vm86(cpu_env, arg1, arg2); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 9854 | #endif |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 9855 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9856 | #if defined(TARGET_NR_adjtimex) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9857 | case TARGET_NR_adjtimex: |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 9858 | { |
| 9859 | struct timex host_buf; |
| 9860 | |
| 9861 | if (target_to_host_timex(&host_buf, arg1) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9862 | return -TARGET_EFAULT; |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 9863 | } |
| 9864 | ret = get_errno(adjtimex(&host_buf)); |
| 9865 | if (!is_error(ret)) { |
| 9866 | if (host_to_target_timex(arg1, &host_buf) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9867 | return -TARGET_EFAULT; |
Aleksandar Markovic | 19f59bc | 2016-09-22 18:56:50 +0200 | [diff] [blame] | 9868 | } |
| 9869 | } |
| 9870 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9871 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 9872 | #endif |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 9873 | #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME) |
| 9874 | case TARGET_NR_clock_adjtime: |
| 9875 | { |
| 9876 | struct timex htx, *phtx = &htx; |
| 9877 | |
| 9878 | if (target_to_host_timex(phtx, arg2) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9879 | return -TARGET_EFAULT; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 9880 | } |
| 9881 | ret = get_errno(clock_adjtime(arg1, phtx)); |
| 9882 | if (!is_error(ret) && phtx) { |
| 9883 | if (host_to_target_timex(arg2, phtx) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9884 | return -TARGET_EFAULT; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 9885 | } |
| 9886 | } |
| 9887 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9888 | return ret; |
Aleksandar Markovic | 38860a0 | 2016-10-10 13:23:29 +0200 | [diff] [blame] | 9889 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9890 | case TARGET_NR_getpgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9891 | return get_errno(getpgid(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9892 | case TARGET_NR_fchdir: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9893 | return get_errno(fchdir(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9894 | case TARGET_NR_personality: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9895 | return get_errno(personality(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9896 | #ifdef TARGET_NR__llseek /* Not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9897 | case TARGET_NR__llseek: |
| 9898 | { |
| 9899 | int64_t res; |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 9900 | #if !defined(__NR_llseek) |
Peter Maydell | 9fea273 | 2016-08-11 18:59:39 +0100 | [diff] [blame] | 9901 | res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5); |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 9902 | if (res == -1) { |
| 9903 | ret = get_errno(res); |
| 9904 | } else { |
| 9905 | ret = 0; |
| 9906 | } |
| 9907 | #else |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9908 | ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5)); |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 9909 | #endif |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 9910 | if ((ret == 0) && put_user_s64(res, arg4)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9911 | return -TARGET_EFAULT; |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 9912 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9913 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 9914 | return ret; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9915 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9916 | #ifdef TARGET_NR_getdents |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9917 | case TARGET_NR_getdents: |
Peter Maydell | 2b3f64c | 2018-04-19 13:57:40 +0100 | [diff] [blame] | 9918 | #ifdef EMULATE_GETDENTS_WITH_GETDENTS |
Ulrich Hecht | d83c873 | 2009-07-24 19:10:28 +0200 | [diff] [blame] | 9919 | #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9920 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9921 | struct target_dirent *target_dirp; |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9922 | struct linux_dirent *dirp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9923 | abi_long count = arg3; |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9924 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 9925 | dirp = g_try_malloc(count); |
| 9926 | if (!dirp) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 9927 | return -TARGET_ENOMEM; |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 9928 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 9929 | |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9930 | ret = get_errno(sys_getdents(arg1, dirp, count)); |
| 9931 | if (!is_error(ret)) { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9932 | struct linux_dirent *de; |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9933 | struct target_dirent *tde; |
| 9934 | int len = ret; |
| 9935 | int reclen, treclen; |
| 9936 | int count1, tnamelen; |
| 9937 | |
| 9938 | count1 = 0; |
| 9939 | de = dirp; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9940 | if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9941 | return -TARGET_EFAULT; |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9942 | tde = target_dirp; |
| 9943 | while (len > 0) { |
| 9944 | reclen = de->d_reclen; |
Dmitry V. Levin | 333858b | 2012-08-21 02:13:12 +0400 | [diff] [blame] | 9945 | tnamelen = reclen - offsetof(struct linux_dirent, d_name); |
| 9946 | assert(tnamelen >= 0); |
| 9947 | treclen = tnamelen + offsetof(struct target_dirent, d_name); |
| 9948 | assert(count1 + treclen <= count); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9949 | tde->d_reclen = tswap16(treclen); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 9950 | tde->d_ino = tswapal(de->d_ino); |
| 9951 | tde->d_off = tswapal(de->d_off); |
Dmitry V. Levin | 333858b | 2012-08-21 02:13:12 +0400 | [diff] [blame] | 9952 | memcpy(tde->d_name, de->d_name, tnamelen); |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9953 | de = (struct linux_dirent *)((char *)de + reclen); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9954 | len -= reclen; |
j_mayer | 1c5bf3b | 2007-04-14 12:17:59 +0000 | [diff] [blame] | 9955 | tde = (struct target_dirent *)((char *)tde + treclen); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9956 | count1 += treclen; |
| 9957 | } |
| 9958 | ret = count1; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9959 | unlock_user(target_dirp, arg2, ret); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9960 | } |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 9961 | g_free(dirp); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9962 | } |
| 9963 | #else |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9964 | { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9965 | struct linux_dirent *dirp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9966 | abi_long count = arg3; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9967 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9968 | if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9969 | return -TARGET_EFAULT; |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 9970 | ret = get_errno(sys_getdents(arg1, dirp, count)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9971 | if (!is_error(ret)) { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9972 | struct linux_dirent *de; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9973 | int len = ret; |
| 9974 | int reclen; |
| 9975 | de = dirp; |
| 9976 | while (len > 0) { |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 9977 | reclen = de->d_reclen; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9978 | if (reclen > len) |
| 9979 | break; |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 9980 | de->d_reclen = tswap16(reclen); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9981 | tswapls(&de->d_ino); |
| 9982 | tswapls(&de->d_off); |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9983 | de = (struct linux_dirent *)((char *)de + reclen); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9984 | len -= reclen; |
| 9985 | } |
| 9986 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9987 | unlock_user(dirp, arg2, ret); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9988 | } |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 9989 | #endif |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9990 | #else |
| 9991 | /* Implement getdents in terms of getdents64 */ |
| 9992 | { |
| 9993 | struct linux_dirent64 *dirp; |
| 9994 | abi_long count = arg3; |
| 9995 | |
| 9996 | dirp = lock_user(VERIFY_WRITE, arg2, count, 0); |
| 9997 | if (!dirp) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 9998 | return -TARGET_EFAULT; |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9999 | } |
| 10000 | ret = get_errno(sys_getdents64(arg1, dirp, count)); |
| 10001 | if (!is_error(ret)) { |
| 10002 | /* Convert the dirent64 structs to target dirent. We do this |
| 10003 | * in-place, since we can guarantee that a target_dirent is no |
| 10004 | * larger than a dirent64; however this means we have to be |
| 10005 | * careful to read everything before writing in the new format. |
| 10006 | */ |
| 10007 | struct linux_dirent64 *de; |
| 10008 | struct target_dirent *tde; |
| 10009 | int len = ret; |
| 10010 | int tlen = 0; |
| 10011 | |
| 10012 | de = dirp; |
| 10013 | tde = (struct target_dirent *)dirp; |
| 10014 | while (len > 0) { |
| 10015 | int namelen, treclen; |
| 10016 | int reclen = de->d_reclen; |
| 10017 | uint64_t ino = de->d_ino; |
| 10018 | int64_t off = de->d_off; |
| 10019 | uint8_t type = de->d_type; |
| 10020 | |
| 10021 | namelen = strlen(de->d_name); |
| 10022 | treclen = offsetof(struct target_dirent, d_name) |
| 10023 | + namelen + 2; |
| 10024 | treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long)); |
| 10025 | |
| 10026 | memmove(tde->d_name, de->d_name, namelen + 1); |
| 10027 | tde->d_ino = tswapal(ino); |
| 10028 | tde->d_off = tswapal(off); |
| 10029 | tde->d_reclen = tswap16(treclen); |
| 10030 | /* The target_dirent type is in what was formerly a padding |
| 10031 | * byte at the end of the structure: |
| 10032 | */ |
| 10033 | *(((char *)tde) + treclen - 1) = type; |
| 10034 | |
| 10035 | de = (struct linux_dirent64 *)((char *)de + reclen); |
| 10036 | tde = (struct target_dirent *)((char *)tde + treclen); |
| 10037 | len -= reclen; |
| 10038 | tlen += treclen; |
| 10039 | } |
| 10040 | ret = tlen; |
| 10041 | } |
| 10042 | unlock_user(dirp, arg2, ret); |
| 10043 | } |
| 10044 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10045 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10046 | #endif /* TARGET_NR_getdents */ |
ths | 3ae4320 | 2007-09-16 21:39:48 +0000 | [diff] [blame] | 10047 | #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 10048 | case TARGET_NR_getdents64: |
| 10049 | { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 10050 | struct linux_dirent64 *dirp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 10051 | abi_long count = arg3; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10052 | if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10053 | return -TARGET_EFAULT; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 10054 | ret = get_errno(sys_getdents64(arg1, dirp, count)); |
| 10055 | if (!is_error(ret)) { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 10056 | struct linux_dirent64 *de; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 10057 | int len = ret; |
| 10058 | int reclen; |
| 10059 | de = dirp; |
| 10060 | while (len > 0) { |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 10061 | reclen = de->d_reclen; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 10062 | if (reclen > len) |
| 10063 | break; |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 10064 | de->d_reclen = tswap16(reclen); |
bellard | 8582a53 | 2007-11-11 23:11:36 +0000 | [diff] [blame] | 10065 | tswap64s((uint64_t *)&de->d_ino); |
| 10066 | tswap64s((uint64_t *)&de->d_off); |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 10067 | de = (struct linux_dirent64 *)((char *)de + reclen); |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 10068 | len -= reclen; |
| 10069 | } |
| 10070 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10071 | unlock_user(dirp, arg2, ret); |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 10072 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10073 | return ret; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 10074 | #endif /* TARGET_NR_getdents64 */ |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 10075 | #if defined(TARGET_NR__newselect) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10076 | case TARGET_NR__newselect: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10077 | return do_select(arg1, arg2, arg3, arg4, arg5); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10078 | #endif |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10079 | #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) |
| 10080 | # ifdef TARGET_NR_poll |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10081 | case TARGET_NR_poll: |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10082 | # endif |
| 10083 | # ifdef TARGET_NR_ppoll |
| 10084 | case TARGET_NR_ppoll: |
| 10085 | # endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10086 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10087 | struct target_pollfd *target_pfd; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10088 | unsigned int nfds = arg2; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10089 | struct pollfd *pfd; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 10090 | unsigned int i; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10091 | |
Laurent Vivier | 3e24bb3 | 2015-10-06 01:20:48 +0200 | [diff] [blame] | 10092 | pfd = NULL; |
| 10093 | target_pfd = NULL; |
| 10094 | if (nfds) { |
Peter Maydell | ce9c139 | 2016-07-18 16:30:36 +0100 | [diff] [blame] | 10095 | if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10096 | return -TARGET_EINVAL; |
Peter Maydell | ce9c139 | 2016-07-18 16:30:36 +0100 | [diff] [blame] | 10097 | } |
| 10098 | |
Laurent Vivier | 3e24bb3 | 2015-10-06 01:20:48 +0200 | [diff] [blame] | 10099 | target_pfd = lock_user(VERIFY_WRITE, arg1, |
| 10100 | sizeof(struct target_pollfd) * nfds, 1); |
| 10101 | if (!target_pfd) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10102 | return -TARGET_EFAULT; |
Laurent Vivier | 3e24bb3 | 2015-10-06 01:20:48 +0200 | [diff] [blame] | 10103 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10104 | |
Laurent Vivier | 3e24bb3 | 2015-10-06 01:20:48 +0200 | [diff] [blame] | 10105 | pfd = alloca(sizeof(struct pollfd) * nfds); |
| 10106 | for (i = 0; i < nfds; i++) { |
| 10107 | pfd[i].fd = tswap32(target_pfd[i].fd); |
| 10108 | pfd[i].events = tswap16(target_pfd[i].events); |
| 10109 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10110 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10111 | |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 10112 | switch (num) { |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10113 | # ifdef TARGET_NR_ppoll |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 10114 | case TARGET_NR_ppoll: |
| 10115 | { |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10116 | struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; |
| 10117 | target_sigset_t *target_set; |
| 10118 | sigset_t _set, *set = &_set; |
| 10119 | |
| 10120 | if (arg3) { |
| 10121 | if (target_to_host_timespec(timeout_ts, arg3)) { |
| 10122 | unlock_user(target_pfd, arg1, 0); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10123 | return -TARGET_EFAULT; |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10124 | } |
| 10125 | } else { |
| 10126 | timeout_ts = NULL; |
| 10127 | } |
| 10128 | |
| 10129 | if (arg4) { |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10130 | if (arg5 != sizeof(target_sigset_t)) { |
| 10131 | unlock_user(target_pfd, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10132 | return -TARGET_EINVAL; |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 10133 | } |
| 10134 | |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10135 | target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); |
| 10136 | if (!target_set) { |
| 10137 | unlock_user(target_pfd, arg1, 0); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10138 | return -TARGET_EFAULT; |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10139 | } |
| 10140 | target_to_host_sigset(set, target_set); |
| 10141 | } else { |
| 10142 | set = NULL; |
| 10143 | } |
| 10144 | |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 10145 | ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, |
| 10146 | set, SIGSET_T_SIZE)); |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10147 | |
| 10148 | if (!is_error(ret) && arg3) { |
| 10149 | host_to_target_timespec(arg3, timeout_ts); |
| 10150 | } |
| 10151 | if (arg4) { |
| 10152 | unlock_user(target_set, arg4, 0); |
| 10153 | } |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 10154 | break; |
| 10155 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10156 | # endif |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 10157 | # ifdef TARGET_NR_poll |
| 10158 | case TARGET_NR_poll: |
| 10159 | { |
| 10160 | struct timespec ts, *pts; |
| 10161 | |
| 10162 | if (arg3 >= 0) { |
| 10163 | /* Convert ms to secs, ns */ |
| 10164 | ts.tv_sec = arg3 / 1000; |
| 10165 | ts.tv_nsec = (arg3 % 1000) * 1000000LL; |
| 10166 | pts = &ts; |
| 10167 | } else { |
| 10168 | /* -ve poll() timeout means "infinite" */ |
| 10169 | pts = NULL; |
| 10170 | } |
| 10171 | ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); |
| 10172 | break; |
| 10173 | } |
| 10174 | # endif |
| 10175 | default: |
| 10176 | g_assert_not_reached(); |
| 10177 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 10178 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10179 | if (!is_error(ret)) { |
| 10180 | for(i = 0; i < nfds; i++) { |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10181 | target_pfd[i].revents = tswap16(pfd[i].revents); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10182 | } |
| 10183 | } |
Peter Maydell | 30cb4cd | 2011-02-25 10:27:40 +0000 | [diff] [blame] | 10184 | unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10185 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10186 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10187 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10188 | case TARGET_NR_flock: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 10189 | /* NOTE: the flock constant seems to be the same for every |
| 10190 | Linux platform */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10191 | return get_errno(safe_flock(arg1, arg2)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10192 | case TARGET_NR_readv: |
| 10193 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 10194 | struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); |
| 10195 | if (vec != NULL) { |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 10196 | ret = get_errno(safe_readv(arg1, vec, arg3)); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 10197 | unlock_iovec(vec, arg2, arg3, 1); |
| 10198 | } else { |
| 10199 | ret = -host_to_target_errno(errno); |
| 10200 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10201 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10202 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10203 | case TARGET_NR_writev: |
| 10204 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 10205 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 10206 | if (vec != NULL) { |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 10207 | ret = get_errno(safe_writev(arg1, vec, arg3)); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 10208 | unlock_iovec(vec, arg2, arg3, 0); |
| 10209 | } else { |
| 10210 | ret = -host_to_target_errno(errno); |
| 10211 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10212 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10213 | return ret; |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 10214 | #if defined(TARGET_NR_preadv) |
| 10215 | case TARGET_NR_preadv: |
| 10216 | { |
| 10217 | struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); |
| 10218 | if (vec != NULL) { |
Max Filippov | 9ac2251 | 2018-04-04 17:30:41 -0700 | [diff] [blame] | 10219 | unsigned long low, high; |
| 10220 | |
| 10221 | target_to_host_low_high(arg4, arg5, &low, &high); |
| 10222 | ret = get_errno(safe_preadv(arg1, vec, arg3, low, high)); |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 10223 | unlock_iovec(vec, arg2, arg3, 1); |
| 10224 | } else { |
| 10225 | ret = -host_to_target_errno(errno); |
| 10226 | } |
| 10227 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10228 | return ret; |
Dejan Jovicevic | 0f26386 | 2016-10-11 11:52:46 +0200 | [diff] [blame] | 10229 | #endif |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 10230 | #if defined(TARGET_NR_pwritev) |
| 10231 | case TARGET_NR_pwritev: |
| 10232 | { |
| 10233 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 10234 | if (vec != NULL) { |
Max Filippov | 9ac2251 | 2018-04-04 17:30:41 -0700 | [diff] [blame] | 10235 | unsigned long low, high; |
| 10236 | |
| 10237 | target_to_host_low_high(arg4, arg5, &low, &high); |
| 10238 | ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high)); |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 10239 | unlock_iovec(vec, arg2, arg3, 0); |
| 10240 | } else { |
| 10241 | ret = -host_to_target_errno(errno); |
| 10242 | } |
| 10243 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10244 | return ret; |
Dejan Jovicevic | f8d00fb | 2016-10-11 11:52:47 +0200 | [diff] [blame] | 10245 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10246 | case TARGET_NR_getsid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10247 | return get_errno(getsid(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 10248 | #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10249 | case TARGET_NR_fdatasync: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10250 | return get_errno(fdatasync(arg1)); |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 10251 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10252 | #ifdef TARGET_NR__sysctl |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10253 | case TARGET_NR__sysctl: |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 10254 | /* We don't implement this, but ENOTDIR is always a safe |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 10255 | return value. */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10256 | return -TARGET_ENOTDIR; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10257 | #endif |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10258 | case TARGET_NR_sched_getaffinity: |
| 10259 | { |
| 10260 | unsigned int mask_size; |
| 10261 | unsigned long *mask; |
| 10262 | |
| 10263 | /* |
| 10264 | * sched_getaffinity needs multiples of ulong, so need to take |
| 10265 | * care of mismatches between target ulong and host ulong sizes. |
| 10266 | */ |
| 10267 | if (arg2 & (sizeof(abi_ulong) - 1)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10268 | return -TARGET_EINVAL; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10269 | } |
| 10270 | mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); |
| 10271 | |
| 10272 | mask = alloca(mask_size); |
Samuel Thibault | 2e0a871 | 2018-01-09 21:16:43 +0100 | [diff] [blame] | 10273 | memset(mask, 0, mask_size); |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10274 | ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask)); |
| 10275 | |
| 10276 | if (!is_error(ret)) { |
Peter Maydell | be3bd28 | 2014-05-15 14:40:23 +0100 | [diff] [blame] | 10277 | if (ret > arg2) { |
| 10278 | /* More data returned than the caller's buffer will fit. |
| 10279 | * This only happens if sizeof(abi_long) < sizeof(long) |
| 10280 | * and the caller passed us a buffer holding an odd number |
| 10281 | * of abi_longs. If the host kernel is actually using the |
| 10282 | * extra 4 bytes then fail EINVAL; otherwise we can just |
| 10283 | * ignore them and only copy the interesting part. |
| 10284 | */ |
| 10285 | int numcpus = sysconf(_SC_NPROCESSORS_CONF); |
| 10286 | if (numcpus > arg2 * 8) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10287 | return -TARGET_EINVAL; |
Peter Maydell | be3bd28 | 2014-05-15 14:40:23 +0100 | [diff] [blame] | 10288 | } |
| 10289 | ret = arg2; |
| 10290 | } |
| 10291 | |
Samuel Thibault | 5fdefcf | 2018-02-11 18:47:04 +0100 | [diff] [blame] | 10292 | if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10293 | return -TARGET_EFAULT; |
Samuel Thibault | 5fdefcf | 2018-02-11 18:47:04 +0100 | [diff] [blame] | 10294 | } |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10295 | } |
| 10296 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10297 | return ret; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10298 | case TARGET_NR_sched_setaffinity: |
| 10299 | { |
| 10300 | unsigned int mask_size; |
| 10301 | unsigned long *mask; |
| 10302 | |
| 10303 | /* |
| 10304 | * sched_setaffinity needs multiples of ulong, so need to take |
| 10305 | * care of mismatches between target ulong and host ulong sizes. |
| 10306 | */ |
| 10307 | if (arg2 & (sizeof(abi_ulong) - 1)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10308 | return -TARGET_EINVAL; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10309 | } |
| 10310 | mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10311 | mask = alloca(mask_size); |
Samuel Thibault | 2e0a871 | 2018-01-09 21:16:43 +0100 | [diff] [blame] | 10312 | |
| 10313 | ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2); |
| 10314 | if (ret) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10315 | return ret; |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10316 | } |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10317 | |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10318 | return get_errno(sys_sched_setaffinity(arg1, mask_size, mask)); |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 10319 | } |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 10320 | case TARGET_NR_getcpu: |
| 10321 | { |
| 10322 | unsigned cpu, node; |
| 10323 | ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL, |
| 10324 | arg2 ? &node : NULL, |
| 10325 | NULL)); |
| 10326 | if (is_error(ret)) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 10327 | return ret; |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 10328 | } |
| 10329 | if (arg1 && put_user_u32(cpu, arg1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10330 | return -TARGET_EFAULT; |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 10331 | } |
| 10332 | if (arg2 && put_user_u32(node, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10333 | return -TARGET_EFAULT; |
Samuel Thibault | b827c3e | 2018-01-12 09:14:35 +0100 | [diff] [blame] | 10334 | } |
| 10335 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10336 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10337 | case TARGET_NR_sched_setparam: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10338 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10339 | struct sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10340 | struct sched_param schp; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10341 | |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 10342 | if (arg2 == 0) { |
| 10343 | return -TARGET_EINVAL; |
| 10344 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10345 | if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10346 | return -TARGET_EFAULT; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10347 | schp.sched_priority = tswap32(target_schp->sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10348 | unlock_user_struct(target_schp, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10349 | return get_errno(sched_setparam(arg1, &schp)); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10350 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10351 | case TARGET_NR_sched_getparam: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10352 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10353 | struct sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10354 | struct sched_param schp; |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 10355 | |
| 10356 | if (arg2 == 0) { |
| 10357 | return -TARGET_EINVAL; |
| 10358 | } |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10359 | ret = get_errno(sched_getparam(arg1, &schp)); |
| 10360 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10361 | if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10362 | return -TARGET_EFAULT; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10363 | target_schp->sched_priority = tswap32(schp.sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10364 | unlock_user_struct(target_schp, arg2, 1); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10365 | } |
| 10366 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10367 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10368 | case TARGET_NR_sched_setscheduler: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10369 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10370 | struct sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10371 | struct sched_param schp; |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 10372 | if (arg3 == 0) { |
| 10373 | return -TARGET_EINVAL; |
| 10374 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10375 | if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10376 | return -TARGET_EFAULT; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10377 | schp.sched_priority = tswap32(target_schp->sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10378 | unlock_user_struct(target_schp, arg3, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10379 | return get_errno(sched_setscheduler(arg1, arg2, &schp)); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10380 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10381 | case TARGET_NR_sched_getscheduler: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10382 | return get_errno(sched_getscheduler(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10383 | case TARGET_NR_sched_yield: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10384 | return get_errno(sched_yield()); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10385 | case TARGET_NR_sched_get_priority_max: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10386 | return get_errno(sched_get_priority_max(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10387 | case TARGET_NR_sched_get_priority_min: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10388 | return get_errno(sched_get_priority_min(arg1)); |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10389 | #ifdef TARGET_NR_sched_rr_get_interval |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10390 | case TARGET_NR_sched_rr_get_interval: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10391 | { |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10392 | struct timespec ts; |
| 10393 | ret = get_errno(sched_rr_get_interval(arg1, &ts)); |
| 10394 | if (!is_error(ret)) { |
Tom Musta | d4290c4 | 2014-08-12 13:53:39 -0500 | [diff] [blame] | 10395 | ret = host_to_target_timespec(arg2, &ts); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10396 | } |
| 10397 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10398 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10399 | #endif |
| 10400 | #if defined(TARGET_NR_nanosleep) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10401 | case TARGET_NR_nanosleep: |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 10402 | { |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 10403 | struct timespec req, rem; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10404 | target_to_host_timespec(&req, arg1); |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 10405 | ret = get_errno(safe_nanosleep(&req, &rem)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10406 | if (is_error(ret) && arg2) { |
| 10407 | host_to_target_timespec(arg2, &rem); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 10408 | } |
| 10409 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10410 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 10411 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10412 | case TARGET_NR_prctl: |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10413 | switch (arg1) { |
| 10414 | case PR_GET_PDEATHSIG: |
| 10415 | { |
| 10416 | int deathsig; |
| 10417 | ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5)); |
| 10418 | if (!is_error(ret) && arg2 |
| 10419 | && put_user_ual(deathsig, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10420 | return -TARGET_EFAULT; |
ths | e557448 | 2007-02-11 20:03:13 +0000 | [diff] [blame] | 10421 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10422 | return ret; |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10423 | } |
Peter Maydell | db9526b | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10424 | #ifdef PR_GET_NAME |
| 10425 | case PR_GET_NAME: |
| 10426 | { |
| 10427 | void *name = lock_user(VERIFY_WRITE, arg2, 16, 1); |
| 10428 | if (!name) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10429 | return -TARGET_EFAULT; |
Peter Maydell | db9526b | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10430 | } |
| 10431 | ret = get_errno(prctl(arg1, (unsigned long)name, |
| 10432 | arg3, arg4, arg5)); |
| 10433 | unlock_user(name, arg2, 16); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10434 | return ret; |
Peter Maydell | db9526b | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10435 | } |
| 10436 | case PR_SET_NAME: |
| 10437 | { |
| 10438 | void *name = lock_user(VERIFY_READ, arg2, 16, 1); |
| 10439 | if (!name) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10440 | return -TARGET_EFAULT; |
Peter Maydell | db9526b | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10441 | } |
| 10442 | ret = get_errno(prctl(arg1, (unsigned long)name, |
| 10443 | arg3, arg4, arg5)); |
| 10444 | unlock_user(name, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10445 | return ret; |
Peter Maydell | db9526b | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10446 | } |
| 10447 | #endif |
Stefan Markovic | 5b702ff | 2018-10-12 12:57:35 +0200 | [diff] [blame] | 10448 | #ifdef TARGET_MIPS |
| 10449 | case TARGET_PR_GET_FP_MODE: |
Stefan Markovic | 64ea3d6 | 2018-10-26 13:17:43 +0200 | [diff] [blame] | 10450 | { |
| 10451 | CPUMIPSState *env = ((CPUMIPSState *)cpu_env); |
| 10452 | ret = 0; |
| 10453 | if (env->CP0_Status & (1 << CP0St_FR)) { |
| 10454 | ret |= TARGET_PR_FP_MODE_FR; |
| 10455 | } |
| 10456 | if (env->CP0_Config5 & (1 << CP0C5_FRE)) { |
| 10457 | ret |= TARGET_PR_FP_MODE_FRE; |
| 10458 | } |
| 10459 | return ret; |
| 10460 | } |
Stefan Markovic | 5b702ff | 2018-10-12 12:57:35 +0200 | [diff] [blame] | 10461 | case TARGET_PR_SET_FP_MODE: |
Stefan Markovic | 64ea3d6 | 2018-10-26 13:17:43 +0200 | [diff] [blame] | 10462 | { |
| 10463 | CPUMIPSState *env = ((CPUMIPSState *)cpu_env); |
| 10464 | bool old_fr = env->CP0_Status & (1 << CP0St_FR); |
Stefan Markovic | 6456c51 | 2018-11-14 14:37:08 +0100 | [diff] [blame] | 10465 | bool old_fre = env->CP0_Config5 & (1 << CP0C5_FRE); |
Stefan Markovic | 64ea3d6 | 2018-10-26 13:17:43 +0200 | [diff] [blame] | 10466 | bool new_fr = arg2 & TARGET_PR_FP_MODE_FR; |
| 10467 | bool new_fre = arg2 & TARGET_PR_FP_MODE_FRE; |
| 10468 | |
Stefan Markovic | 6456c51 | 2018-11-14 14:37:08 +0100 | [diff] [blame] | 10469 | const unsigned int known_bits = TARGET_PR_FP_MODE_FR | |
| 10470 | TARGET_PR_FP_MODE_FRE; |
| 10471 | |
| 10472 | /* If nothing to change, return right away, successfully. */ |
| 10473 | if (old_fr == new_fr && old_fre == new_fre) { |
| 10474 | return 0; |
| 10475 | } |
| 10476 | /* Check the value is valid */ |
| 10477 | if (arg2 & ~known_bits) { |
| 10478 | return -TARGET_EOPNOTSUPP; |
| 10479 | } |
| 10480 | /* Setting FRE without FR is not supported. */ |
| 10481 | if (new_fre && !new_fr) { |
| 10482 | return -TARGET_EOPNOTSUPP; |
| 10483 | } |
Stefan Markovic | 64ea3d6 | 2018-10-26 13:17:43 +0200 | [diff] [blame] | 10484 | if (new_fr && !(env->active_fpu.fcr0 & (1 << FCR0_F64))) { |
| 10485 | /* FR1 is not supported */ |
| 10486 | return -TARGET_EOPNOTSUPP; |
| 10487 | } |
| 10488 | if (!new_fr && (env->active_fpu.fcr0 & (1 << FCR0_F64)) |
| 10489 | && !(env->CP0_Status_rw_bitmask & (1 << CP0St_FR))) { |
| 10490 | /* cannot set FR=0 */ |
| 10491 | return -TARGET_EOPNOTSUPP; |
| 10492 | } |
| 10493 | if (new_fre && !(env->active_fpu.fcr0 & (1 << FCR0_FREP))) { |
| 10494 | /* Cannot set FRE=1 */ |
| 10495 | return -TARGET_EOPNOTSUPP; |
| 10496 | } |
| 10497 | |
| 10498 | int i; |
| 10499 | fpr_t *fpr = env->active_fpu.fpr; |
| 10500 | for (i = 0; i < 32 ; i += 2) { |
| 10501 | if (!old_fr && new_fr) { |
| 10502 | fpr[i].w[!FP_ENDIAN_IDX] = fpr[i + 1].w[FP_ENDIAN_IDX]; |
| 10503 | } else if (old_fr && !new_fr) { |
| 10504 | fpr[i + 1].w[FP_ENDIAN_IDX] = fpr[i].w[!FP_ENDIAN_IDX]; |
| 10505 | } |
| 10506 | } |
| 10507 | |
| 10508 | if (new_fr) { |
| 10509 | env->CP0_Status |= (1 << CP0St_FR); |
| 10510 | env->hflags |= MIPS_HFLAG_F64; |
| 10511 | } else { |
| 10512 | env->CP0_Status &= ~(1 << CP0St_FR); |
Stefan Markovic | 6456c51 | 2018-11-14 14:37:08 +0100 | [diff] [blame] | 10513 | env->hflags &= ~MIPS_HFLAG_F64; |
Stefan Markovic | 64ea3d6 | 2018-10-26 13:17:43 +0200 | [diff] [blame] | 10514 | } |
| 10515 | if (new_fre) { |
| 10516 | env->CP0_Config5 |= (1 << CP0C5_FRE); |
| 10517 | if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) { |
| 10518 | env->hflags |= MIPS_HFLAG_FRE; |
| 10519 | } |
| 10520 | } else { |
| 10521 | env->CP0_Config5 &= ~(1 << CP0C5_FRE); |
Stefan Markovic | 6456c51 | 2018-11-14 14:37:08 +0100 | [diff] [blame] | 10522 | env->hflags &= ~MIPS_HFLAG_FRE; |
Stefan Markovic | 64ea3d6 | 2018-10-26 13:17:43 +0200 | [diff] [blame] | 10523 | } |
| 10524 | |
| 10525 | return 0; |
| 10526 | } |
Stefan Markovic | 5b702ff | 2018-10-12 12:57:35 +0200 | [diff] [blame] | 10527 | #endif /* MIPS */ |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10528 | #ifdef TARGET_AARCH64 |
| 10529 | case TARGET_PR_SVE_SET_VL: |
Richard Henderson | adf92ea | 2018-08-16 14:05:28 +0100 | [diff] [blame] | 10530 | /* |
| 10531 | * We cannot support either PR_SVE_SET_VL_ONEXEC or |
| 10532 | * PR_SVE_VL_INHERIT. Note the kernel definition |
| 10533 | * of sve_vl_valid allows for VQ=512, i.e. VL=8192, |
| 10534 | * even though the current architectural maximum is VQ=16. |
| 10535 | */ |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10536 | ret = -TARGET_EINVAL; |
Richard Henderson | 2fc0cc0 | 2019-03-22 17:41:14 -0700 | [diff] [blame] | 10537 | if (cpu_isar_feature(aa64_sve, env_archcpu(cpu_env)) |
Richard Henderson | adf92ea | 2018-08-16 14:05:28 +0100 | [diff] [blame] | 10538 | && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) { |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10539 | CPUARMState *env = cpu_env; |
Richard Henderson | 2fc0cc0 | 2019-03-22 17:41:14 -0700 | [diff] [blame] | 10540 | ARMCPU *cpu = env_archcpu(env); |
Richard Henderson | adf92ea | 2018-08-16 14:05:28 +0100 | [diff] [blame] | 10541 | uint32_t vq, old_vq; |
| 10542 | |
| 10543 | old_vq = (env->vfp.zcr_el[1] & 0xf) + 1; |
| 10544 | vq = MAX(arg2 / 16, 1); |
| 10545 | vq = MIN(vq, cpu->sve_max_vq); |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10546 | |
| 10547 | if (vq < old_vq) { |
| 10548 | aarch64_sve_narrow_vq(env, vq); |
| 10549 | } |
| 10550 | env->vfp.zcr_el[1] = vq - 1; |
Richard Henderson | a8a79c7 | 2019-10-23 11:00:49 -0400 | [diff] [blame] | 10551 | arm_rebuild_hflags(env); |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10552 | ret = vq * 16; |
| 10553 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10554 | return ret; |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10555 | case TARGET_PR_SVE_GET_VL: |
| 10556 | ret = -TARGET_EINVAL; |
Richard Henderson | cd208a1 | 2018-10-24 07:50:17 +0100 | [diff] [blame] | 10557 | { |
Richard Henderson | 2fc0cc0 | 2019-03-22 17:41:14 -0700 | [diff] [blame] | 10558 | ARMCPU *cpu = env_archcpu(cpu_env); |
Richard Henderson | cd208a1 | 2018-10-24 07:50:17 +0100 | [diff] [blame] | 10559 | if (cpu_isar_feature(aa64_sve, cpu)) { |
| 10560 | ret = ((cpu->env.vfp.zcr_el[1] & 0xf) + 1) * 16; |
| 10561 | } |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10562 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10563 | return ret; |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10564 | case TARGET_PR_PAC_RESET_KEYS: |
| 10565 | { |
| 10566 | CPUARMState *env = cpu_env; |
Richard Henderson | 2fc0cc0 | 2019-03-22 17:41:14 -0700 | [diff] [blame] | 10567 | ARMCPU *cpu = env_archcpu(env); |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10568 | |
| 10569 | if (arg3 || arg4 || arg5) { |
| 10570 | return -TARGET_EINVAL; |
| 10571 | } |
| 10572 | if (cpu_isar_feature(aa64_pauth, cpu)) { |
| 10573 | int all = (TARGET_PR_PAC_APIAKEY | TARGET_PR_PAC_APIBKEY | |
| 10574 | TARGET_PR_PAC_APDAKEY | TARGET_PR_PAC_APDBKEY | |
| 10575 | TARGET_PR_PAC_APGAKEY); |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 10576 | int ret = 0; |
| 10577 | Error *err = NULL; |
| 10578 | |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10579 | if (arg2 == 0) { |
| 10580 | arg2 = all; |
| 10581 | } else if (arg2 & ~all) { |
| 10582 | return -TARGET_EINVAL; |
| 10583 | } |
| 10584 | if (arg2 & TARGET_PR_PAC_APIAKEY) { |
Richard Henderson | 108b3ba | 2019-03-14 17:28:32 -0700 | [diff] [blame] | 10585 | ret |= qemu_guest_getrandom(&env->keys.apia, |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 10586 | sizeof(ARMPACKey), &err); |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10587 | } |
| 10588 | if (arg2 & TARGET_PR_PAC_APIBKEY) { |
Richard Henderson | 108b3ba | 2019-03-14 17:28:32 -0700 | [diff] [blame] | 10589 | ret |= qemu_guest_getrandom(&env->keys.apib, |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 10590 | sizeof(ARMPACKey), &err); |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10591 | } |
| 10592 | if (arg2 & TARGET_PR_PAC_APDAKEY) { |
Richard Henderson | 108b3ba | 2019-03-14 17:28:32 -0700 | [diff] [blame] | 10593 | ret |= qemu_guest_getrandom(&env->keys.apda, |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 10594 | sizeof(ARMPACKey), &err); |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10595 | } |
| 10596 | if (arg2 & TARGET_PR_PAC_APDBKEY) { |
Richard Henderson | 108b3ba | 2019-03-14 17:28:32 -0700 | [diff] [blame] | 10597 | ret |= qemu_guest_getrandom(&env->keys.apdb, |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 10598 | sizeof(ARMPACKey), &err); |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10599 | } |
| 10600 | if (arg2 & TARGET_PR_PAC_APGAKEY) { |
Richard Henderson | 108b3ba | 2019-03-14 17:28:32 -0700 | [diff] [blame] | 10601 | ret |= qemu_guest_getrandom(&env->keys.apga, |
Richard Henderson | 51977e2 | 2019-03-12 19:22:20 -0700 | [diff] [blame] | 10602 | sizeof(ARMPACKey), &err); |
| 10603 | } |
| 10604 | if (ret != 0) { |
| 10605 | /* |
| 10606 | * Some unknown failure in the crypto. The best |
| 10607 | * we can do is log it and fail the syscall. |
| 10608 | * The real syscall cannot fail this way. |
| 10609 | */ |
| 10610 | qemu_log_mask(LOG_UNIMP, |
| 10611 | "PR_PAC_RESET_KEYS: Crypto failure: %s", |
| 10612 | error_get_pretty(err)); |
| 10613 | error_free(err); |
| 10614 | return -TARGET_EIO; |
Richard Henderson | bff63fb | 2019-02-05 16:52:39 +0000 | [diff] [blame] | 10615 | } |
| 10616 | return 0; |
| 10617 | } |
| 10618 | } |
| 10619 | return -TARGET_EINVAL; |
Richard Henderson | 85fc716 | 2018-03-09 17:09:43 +0000 | [diff] [blame] | 10620 | #endif /* AARCH64 */ |
James Cowgill | a8b154a | 2017-11-06 18:03:51 +0000 | [diff] [blame] | 10621 | case PR_GET_SECCOMP: |
| 10622 | case PR_SET_SECCOMP: |
| 10623 | /* Disable seccomp to prevent the target disabling syscalls we |
| 10624 | * need. */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10625 | return -TARGET_EINVAL; |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10626 | default: |
| 10627 | /* Most prctl options have no pointer arguments */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10628 | return get_errno(prctl(arg1, arg2, arg3, arg4, arg5)); |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 10629 | } |
ths | 39b9aae | 2007-02-11 18:36:44 +0000 | [diff] [blame] | 10630 | break; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 10631 | #ifdef TARGET_NR_arch_prctl |
| 10632 | case TARGET_NR_arch_prctl: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10633 | return do_arch_prctl(cpu_env, arg1, arg2); |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 10634 | #endif |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 10635 | #ifdef TARGET_NR_pread64 |
| 10636 | case TARGET_NR_pread64: |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 10637 | if (regpairs_aligned(cpu_env, num)) { |
Alexander Graf | ae017a5 | 2012-09-29 15:32:39 +0000 | [diff] [blame] | 10638 | arg4 = arg5; |
| 10639 | arg5 = arg6; |
| 10640 | } |
Peter Maydell | 2bd3f89 | 2019-01-08 18:49:00 +0000 | [diff] [blame] | 10641 | if (arg2 == 0 && arg3 == 0) { |
| 10642 | /* Special-case NULL buffer and zero length, which should succeed */ |
| 10643 | p = 0; |
| 10644 | } else { |
| 10645 | p = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 10646 | if (!p) { |
| 10647 | return -TARGET_EFAULT; |
| 10648 | } |
| 10649 | } |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 10650 | ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5))); |
| 10651 | unlock_user(p, arg2, ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10652 | return ret; |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 10653 | case TARGET_NR_pwrite64: |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 10654 | if (regpairs_aligned(cpu_env, num)) { |
Alexander Graf | ae017a5 | 2012-09-29 15:32:39 +0000 | [diff] [blame] | 10655 | arg4 = arg5; |
| 10656 | arg5 = arg6; |
| 10657 | } |
Peter Maydell | 2bd3f89 | 2019-01-08 18:49:00 +0000 | [diff] [blame] | 10658 | if (arg2 == 0 && arg3 == 0) { |
| 10659 | /* Special-case NULL buffer and zero length, which should succeed */ |
| 10660 | p = 0; |
| 10661 | } else { |
| 10662 | p = lock_user(VERIFY_READ, arg2, arg3, 1); |
| 10663 | if (!p) { |
| 10664 | return -TARGET_EFAULT; |
| 10665 | } |
| 10666 | } |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 10667 | ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5))); |
| 10668 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10669 | return ret; |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 10670 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10671 | case TARGET_NR_getcwd: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10672 | if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10673 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10674 | ret = get_errno(sys_getcwd1(p, arg2)); |
| 10675 | unlock_user(p, arg1, ret); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10676 | return ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10677 | case TARGET_NR_capget: |
| 10678 | case TARGET_NR_capset: |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 10679 | { |
| 10680 | struct target_user_cap_header *target_header; |
| 10681 | struct target_user_cap_data *target_data = NULL; |
| 10682 | struct __user_cap_header_struct header; |
| 10683 | struct __user_cap_data_struct data[2]; |
| 10684 | struct __user_cap_data_struct *dataptr = NULL; |
| 10685 | int i, target_datalen; |
| 10686 | int data_items = 1; |
| 10687 | |
| 10688 | if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10689 | return -TARGET_EFAULT; |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 10690 | } |
| 10691 | header.version = tswap32(target_header->version); |
| 10692 | header.pid = tswap32(target_header->pid); |
| 10693 | |
Peter Maydell | ec86487 | 2014-03-19 16:07:30 +0000 | [diff] [blame] | 10694 | if (header.version != _LINUX_CAPABILITY_VERSION) { |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 10695 | /* Version 2 and up takes pointer to two user_data structs */ |
| 10696 | data_items = 2; |
| 10697 | } |
| 10698 | |
| 10699 | target_datalen = sizeof(*target_data) * data_items; |
| 10700 | |
| 10701 | if (arg2) { |
| 10702 | if (num == TARGET_NR_capget) { |
| 10703 | target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0); |
| 10704 | } else { |
| 10705 | target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1); |
| 10706 | } |
| 10707 | if (!target_data) { |
| 10708 | unlock_user_struct(target_header, arg1, 0); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10709 | return -TARGET_EFAULT; |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 10710 | } |
| 10711 | |
| 10712 | if (num == TARGET_NR_capset) { |
| 10713 | for (i = 0; i < data_items; i++) { |
| 10714 | data[i].effective = tswap32(target_data[i].effective); |
| 10715 | data[i].permitted = tswap32(target_data[i].permitted); |
| 10716 | data[i].inheritable = tswap32(target_data[i].inheritable); |
| 10717 | } |
| 10718 | } |
| 10719 | |
| 10720 | dataptr = data; |
| 10721 | } |
| 10722 | |
| 10723 | if (num == TARGET_NR_capget) { |
| 10724 | ret = get_errno(capget(&header, dataptr)); |
| 10725 | } else { |
| 10726 | ret = get_errno(capset(&header, dataptr)); |
| 10727 | } |
| 10728 | |
| 10729 | /* The kernel always updates version for both capget and capset */ |
| 10730 | target_header->version = tswap32(header.version); |
| 10731 | unlock_user_struct(target_header, arg1, 1); |
| 10732 | |
| 10733 | if (arg2) { |
| 10734 | if (num == TARGET_NR_capget) { |
| 10735 | for (i = 0; i < data_items; i++) { |
| 10736 | target_data[i].effective = tswap32(data[i].effective); |
| 10737 | target_data[i].permitted = tswap32(data[i].permitted); |
| 10738 | target_data[i].inheritable = tswap32(data[i].inheritable); |
| 10739 | } |
| 10740 | unlock_user(target_data, arg2, target_datalen); |
| 10741 | } else { |
| 10742 | unlock_user(target_data, arg2, 0); |
| 10743 | } |
| 10744 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10745 | return ret; |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 10746 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10747 | case TARGET_NR_sigaltstack: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10748 | return do_sigaltstack(arg1, arg2, |
| 10749 | get_sp_from_cpustate((CPUArchState *)cpu_env)); |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10750 | |
| 10751 | #ifdef CONFIG_SENDFILE |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10752 | #ifdef TARGET_NR_sendfile |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10753 | case TARGET_NR_sendfile: |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10754 | { |
| 10755 | off_t *offp = NULL; |
| 10756 | off_t off; |
| 10757 | if (arg3) { |
| 10758 | ret = get_user_sal(off, arg3); |
| 10759 | if (is_error(ret)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10760 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10761 | } |
| 10762 | offp = &off; |
| 10763 | } |
| 10764 | ret = get_errno(sendfile(arg1, arg2, offp, arg4)); |
| 10765 | if (!is_error(ret) && arg3) { |
| 10766 | abi_long ret2 = put_user_sal(off, arg3); |
| 10767 | if (is_error(ret2)) { |
| 10768 | ret = ret2; |
| 10769 | } |
| 10770 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10771 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10772 | } |
Aleksandar Rikalo | 4f7f892 | 2018-08-02 16:16:00 +0200 | [diff] [blame] | 10773 | #endif |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10774 | #ifdef TARGET_NR_sendfile64 |
| 10775 | case TARGET_NR_sendfile64: |
| 10776 | { |
| 10777 | off_t *offp = NULL; |
| 10778 | off_t off; |
| 10779 | if (arg3) { |
| 10780 | ret = get_user_s64(off, arg3); |
| 10781 | if (is_error(ret)) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10782 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10783 | } |
| 10784 | offp = &off; |
| 10785 | } |
| 10786 | ret = get_errno(sendfile(arg1, arg2, offp, arg4)); |
| 10787 | if (!is_error(ret) && arg3) { |
| 10788 | abi_long ret2 = put_user_s64(off, arg3); |
| 10789 | if (is_error(ret2)) { |
| 10790 | ret = ret2; |
| 10791 | } |
| 10792 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10793 | return ret; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 10794 | } |
| 10795 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10796 | #endif |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 10797 | #ifdef TARGET_NR_vfork |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10798 | case TARGET_NR_vfork: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10799 | return get_errno(do_fork(cpu_env, |
| 10800 | CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD, |
| 10801 | 0, 0, 0, 0)); |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 10802 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10803 | #ifdef TARGET_NR_ugetrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10804 | case TARGET_NR_ugetrlimit: |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 10805 | { |
| 10806 | struct rlimit rlim; |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 10807 | int resource = target_to_host_resource(arg1); |
| 10808 | ret = get_errno(getrlimit(resource, &rlim)); |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 10809 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10810 | struct target_rlimit *target_rlim; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10811 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10812 | return -TARGET_EFAULT; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 10813 | target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); |
| 10814 | target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10815 | unlock_user_struct(target_rlim, arg2, 1); |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 10816 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10817 | return ret; |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 10818 | } |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10819 | #endif |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10820 | #ifdef TARGET_NR_truncate64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10821 | case TARGET_NR_truncate64: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10822 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10823 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10824 | ret = target_truncate64(cpu_env, p, arg2, arg3, arg4); |
| 10825 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10826 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10827 | #endif |
| 10828 | #ifdef TARGET_NR_ftruncate64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10829 | case TARGET_NR_ftruncate64: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10830 | return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10831 | #endif |
| 10832 | #ifdef TARGET_NR_stat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10833 | case TARGET_NR_stat64: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10834 | if (!(p = lock_user_string(arg1))) { |
| 10835 | return -TARGET_EFAULT; |
| 10836 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10837 | ret = get_errno(stat(path(p), &st)); |
| 10838 | unlock_user(p, arg1, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 10839 | if (!is_error(ret)) |
| 10840 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10841 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10842 | #endif |
| 10843 | #ifdef TARGET_NR_lstat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10844 | case TARGET_NR_lstat64: |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10845 | if (!(p = lock_user_string(arg1))) { |
| 10846 | return -TARGET_EFAULT; |
| 10847 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10848 | ret = get_errno(lstat(path(p), &st)); |
| 10849 | unlock_user(p, arg1, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 10850 | if (!is_error(ret)) |
| 10851 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10852 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10853 | #endif |
| 10854 | #ifdef TARGET_NR_fstat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10855 | case TARGET_NR_fstat64: |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 10856 | ret = get_errno(fstat(arg1, &st)); |
| 10857 | if (!is_error(ret)) |
| 10858 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10859 | return ret; |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 10860 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10861 | #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 10862 | #ifdef TARGET_NR_fstatat64 |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 10863 | case TARGET_NR_fstatat64: |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 10864 | #endif |
| 10865 | #ifdef TARGET_NR_newfstatat |
| 10866 | case TARGET_NR_newfstatat: |
| 10867 | #endif |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10868 | if (!(p = lock_user_string(arg2))) { |
| 10869 | return -TARGET_EFAULT; |
| 10870 | } |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 10871 | ret = get_errno(fstatat(arg1, path(p), &st, arg4)); |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10872 | unlock_user(p, arg2, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 10873 | if (!is_error(ret)) |
| 10874 | ret = host_to_target_stat64(cpu_env, arg3, &st); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10875 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10876 | #endif |
Aleksandar Rikalo | efa9218 | 2019-06-28 12:43:34 +0200 | [diff] [blame] | 10877 | #if defined(TARGET_NR_statx) |
| 10878 | case TARGET_NR_statx: |
| 10879 | { |
| 10880 | struct target_statx *target_stx; |
| 10881 | int dirfd = arg1; |
| 10882 | int flags = arg3; |
| 10883 | |
| 10884 | p = lock_user_string(arg2); |
| 10885 | if (p == NULL) { |
| 10886 | return -TARGET_EFAULT; |
| 10887 | } |
| 10888 | #if defined(__NR_statx) |
| 10889 | { |
| 10890 | /* |
| 10891 | * It is assumed that struct statx is architecture independent. |
| 10892 | */ |
| 10893 | struct target_statx host_stx; |
| 10894 | int mask = arg4; |
| 10895 | |
| 10896 | ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx)); |
| 10897 | if (!is_error(ret)) { |
| 10898 | if (host_to_target_statx(&host_stx, arg5) != 0) { |
| 10899 | unlock_user(p, arg2, 0); |
| 10900 | return -TARGET_EFAULT; |
| 10901 | } |
| 10902 | } |
| 10903 | |
| 10904 | if (ret != -TARGET_ENOSYS) { |
| 10905 | unlock_user(p, arg2, 0); |
| 10906 | return ret; |
| 10907 | } |
| 10908 | } |
| 10909 | #endif |
| 10910 | ret = get_errno(fstatat(dirfd, path(p), &st, flags)); |
| 10911 | unlock_user(p, arg2, 0); |
| 10912 | |
| 10913 | if (!is_error(ret)) { |
| 10914 | if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) { |
| 10915 | return -TARGET_EFAULT; |
| 10916 | } |
| 10917 | memset(target_stx, 0, sizeof(*target_stx)); |
| 10918 | __put_user(major(st.st_dev), &target_stx->stx_dev_major); |
| 10919 | __put_user(minor(st.st_dev), &target_stx->stx_dev_minor); |
| 10920 | __put_user(st.st_ino, &target_stx->stx_ino); |
| 10921 | __put_user(st.st_mode, &target_stx->stx_mode); |
| 10922 | __put_user(st.st_uid, &target_stx->stx_uid); |
| 10923 | __put_user(st.st_gid, &target_stx->stx_gid); |
| 10924 | __put_user(st.st_nlink, &target_stx->stx_nlink); |
| 10925 | __put_user(major(st.st_rdev), &target_stx->stx_rdev_major); |
| 10926 | __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor); |
| 10927 | __put_user(st.st_size, &target_stx->stx_size); |
| 10928 | __put_user(st.st_blksize, &target_stx->stx_blksize); |
| 10929 | __put_user(st.st_blocks, &target_stx->stx_blocks); |
| 10930 | __put_user(st.st_atime, &target_stx->stx_atime.tv_sec); |
| 10931 | __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec); |
| 10932 | __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec); |
| 10933 | unlock_user_struct(target_stx, arg5, 1); |
| 10934 | } |
| 10935 | } |
| 10936 | return ret; |
| 10937 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10938 | #ifdef TARGET_NR_lchown |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10939 | case TARGET_NR_lchown: |
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(lchown(p, low2highuid(arg2), low2highgid(arg3))); |
| 10943 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10944 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 10945 | #endif |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10946 | #ifdef TARGET_NR_getuid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10947 | case TARGET_NR_getuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10948 | return get_errno(high2lowuid(getuid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10949 | #endif |
| 10950 | #ifdef TARGET_NR_getgid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10951 | case TARGET_NR_getgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10952 | return get_errno(high2lowgid(getgid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10953 | #endif |
| 10954 | #ifdef TARGET_NR_geteuid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10955 | case TARGET_NR_geteuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10956 | return get_errno(high2lowuid(geteuid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10957 | #endif |
| 10958 | #ifdef TARGET_NR_getegid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10959 | case TARGET_NR_getegid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10960 | return get_errno(high2lowgid(getegid())); |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10961 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10962 | case TARGET_NR_setreuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10963 | return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10964 | case TARGET_NR_setregid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10965 | return get_errno(setregid(low2highgid(arg1), low2highgid(arg2))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10966 | case TARGET_NR_getgroups: |
| 10967 | { |
| 10968 | int gidsetsize = arg1; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10969 | target_id *target_grouplist; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10970 | gid_t *grouplist; |
| 10971 | int i; |
| 10972 | |
| 10973 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
| 10974 | ret = get_errno(getgroups(gidsetsize, grouplist)); |
balrog | cb3bc23 | 2008-09-20 02:08:13 +0000 | [diff] [blame] | 10975 | if (gidsetsize == 0) |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10976 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10977 | if (!is_error(ret)) { |
Andreas Schwab | 03903ff | 2013-04-09 05:41:33 +0000 | [diff] [blame] | 10978 | target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10979 | if (!target_grouplist) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 10980 | return -TARGET_EFAULT; |
balrog | a2155fc | 2008-09-20 02:12:08 +0000 | [diff] [blame] | 10981 | for(i = 0;i < ret; i++) |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10982 | target_grouplist[i] = tswapid(high2lowgid(grouplist[i])); |
Andreas Schwab | 03903ff | 2013-04-09 05:41:33 +0000 | [diff] [blame] | 10983 | unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10984 | } |
| 10985 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 10986 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10987 | case TARGET_NR_setgroups: |
| 10988 | { |
| 10989 | int gidsetsize = arg1; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 10990 | target_id *target_grouplist; |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 10991 | gid_t *grouplist = NULL; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10992 | int i; |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 10993 | if (gidsetsize) { |
| 10994 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
Andreas Schwab | 03903ff | 2013-04-09 05:41:33 +0000 | [diff] [blame] | 10995 | target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1); |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 10996 | if (!target_grouplist) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 10997 | return -TARGET_EFAULT; |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 10998 | } |
| 10999 | for (i = 0; i < gidsetsize; i++) { |
| 11000 | grouplist[i] = low2highgid(tswapid(target_grouplist[i])); |
| 11001 | } |
| 11002 | unlock_user(target_grouplist, arg2, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11003 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11004 | return get_errno(setgroups(gidsetsize, grouplist)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11005 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11006 | case TARGET_NR_fchown: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11007 | return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 11008 | #if defined(TARGET_NR_fchownat) |
ths | ccfa72b | 2007-09-24 09:23:34 +0000 | [diff] [blame] | 11009 | case TARGET_NR_fchownat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11010 | if (!(p = lock_user_string(arg2))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11011 | return -TARGET_EFAULT; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 11012 | ret = get_errno(fchownat(arg1, p, low2highuid(arg3), |
| 11013 | low2highgid(arg4), arg5)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11014 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11015 | return ret; |
ths | ccfa72b | 2007-09-24 09:23:34 +0000 | [diff] [blame] | 11016 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11017 | #ifdef TARGET_NR_setresuid |
| 11018 | case TARGET_NR_setresuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11019 | return get_errno(sys_setresuid(low2highuid(arg1), |
| 11020 | low2highuid(arg2), |
| 11021 | low2highuid(arg3))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11022 | #endif |
| 11023 | #ifdef TARGET_NR_getresuid |
| 11024 | case TARGET_NR_getresuid: |
| 11025 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11026 | uid_t ruid, euid, suid; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11027 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 11028 | if (!is_error(ret)) { |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 11029 | if (put_user_id(high2lowuid(ruid), arg1) |
| 11030 | || put_user_id(high2lowuid(euid), arg2) |
| 11031 | || put_user_id(high2lowuid(suid), arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11032 | return -TARGET_EFAULT; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11033 | } |
| 11034 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11035 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11036 | #endif |
| 11037 | #ifdef TARGET_NR_getresgid |
| 11038 | case TARGET_NR_setresgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11039 | return get_errno(sys_setresgid(low2highgid(arg1), |
| 11040 | low2highgid(arg2), |
| 11041 | low2highgid(arg3))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11042 | #endif |
| 11043 | #ifdef TARGET_NR_getresgid |
| 11044 | case TARGET_NR_getresgid: |
| 11045 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11046 | gid_t rgid, egid, sgid; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11047 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 11048 | if (!is_error(ret)) { |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 11049 | if (put_user_id(high2lowgid(rgid), arg1) |
| 11050 | || put_user_id(high2lowgid(egid), arg2) |
| 11051 | || put_user_id(high2lowgid(sgid), arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11052 | return -TARGET_EFAULT; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11053 | } |
| 11054 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11055 | return ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11056 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11057 | #ifdef TARGET_NR_chown |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11058 | case TARGET_NR_chown: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11059 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11060 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11061 | ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3))); |
| 11062 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11063 | return ret; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 11064 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11065 | case TARGET_NR_setuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11066 | return get_errno(sys_setuid(low2highuid(arg1))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11067 | case TARGET_NR_setgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11068 | return get_errno(sys_setgid(low2highgid(arg1))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11069 | case TARGET_NR_setfsuid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11070 | return get_errno(setfsuid(arg1)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11071 | case TARGET_NR_setfsgid: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11072 | return get_errno(setfsgid(arg1)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 11073 | |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11074 | #ifdef TARGET_NR_lchown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11075 | case TARGET_NR_lchown32: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11076 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11077 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11078 | ret = get_errno(lchown(p, arg2, arg3)); |
| 11079 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11080 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11081 | #endif |
| 11082 | #ifdef TARGET_NR_getuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11083 | case TARGET_NR_getuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11084 | return get_errno(getuid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11085 | #endif |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 11086 | |
| 11087 | #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA) |
| 11088 | /* Alpha specific */ |
| 11089 | case TARGET_NR_getxuid: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11090 | { |
| 11091 | uid_t euid; |
| 11092 | euid=geteuid(); |
| 11093 | ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid; |
| 11094 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11095 | return get_errno(getuid()); |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 11096 | #endif |
| 11097 | #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA) |
| 11098 | /* Alpha specific */ |
| 11099 | case TARGET_NR_getxgid: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11100 | { |
| 11101 | uid_t egid; |
| 11102 | egid=getegid(); |
| 11103 | ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid; |
| 11104 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11105 | return get_errno(getgid()); |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 11106 | #endif |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11107 | #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA) |
| 11108 | /* Alpha specific */ |
| 11109 | case TARGET_NR_osf_getsysinfo: |
| 11110 | ret = -TARGET_EOPNOTSUPP; |
| 11111 | switch (arg1) { |
| 11112 | case TARGET_GSI_IEEE_FP_CONTROL: |
| 11113 | { |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11114 | uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env); |
| 11115 | uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11116 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11117 | swcr &= ~SWCR_STATUS_MASK; |
| 11118 | swcr |= (fpcr >> 35) & SWCR_STATUS_MASK; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11119 | |
| 11120 | if (put_user_u64 (swcr, arg2)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11121 | return -TARGET_EFAULT; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11122 | ret = 0; |
| 11123 | } |
| 11124 | break; |
| 11125 | |
| 11126 | /* case GSI_IEEE_STATE_AT_SIGNAL: |
| 11127 | -- Not implemented in linux kernel. |
| 11128 | case GSI_UACPROC: |
| 11129 | -- Retrieves current unaligned access state; not much used. |
| 11130 | case GSI_PROC_TYPE: |
| 11131 | -- Retrieves implver information; surely not used. |
| 11132 | case GSI_GET_HWRPB: |
| 11133 | -- Grabs a copy of the HWRPB; surely not used. |
| 11134 | */ |
| 11135 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11136 | return ret; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11137 | #endif |
| 11138 | #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA) |
| 11139 | /* Alpha specific */ |
| 11140 | case TARGET_NR_osf_setsysinfo: |
| 11141 | ret = -TARGET_EOPNOTSUPP; |
| 11142 | switch (arg1) { |
| 11143 | case TARGET_SSI_IEEE_FP_CONTROL: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11144 | { |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11145 | uint64_t swcr, fpcr; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11146 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11147 | if (get_user_u64 (swcr, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11148 | return -TARGET_EFAULT; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11149 | } |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11150 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11151 | /* |
| 11152 | * The kernel calls swcr_update_status to update the |
| 11153 | * status bits from the fpcr at every point that it |
| 11154 | * could be queried. Therefore, we store the status |
| 11155 | * bits only in FPCR. |
| 11156 | */ |
| 11157 | ((CPUAlphaState *)cpu_env)->swcr |
| 11158 | = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11159 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11160 | fpcr = cpu_alpha_load_fpcr(cpu_env); |
| 11161 | fpcr &= ((uint64_t)FPCR_DYN_MASK << 32); |
| 11162 | fpcr |= alpha_ieee_swcr_to_fpcr(swcr); |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11163 | cpu_alpha_store_fpcr(cpu_env, fpcr); |
| 11164 | ret = 0; |
| 11165 | } |
| 11166 | break; |
| 11167 | |
| 11168 | case TARGET_SSI_IEEE_RAISE_EXCEPTION: |
| 11169 | { |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11170 | uint64_t exc, fpcr, fex; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11171 | |
| 11172 | if (get_user_u64(exc, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11173 | return -TARGET_EFAULT; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11174 | } |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11175 | exc &= SWCR_STATUS_MASK; |
| 11176 | fpcr = cpu_alpha_load_fpcr(cpu_env); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11177 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11178 | /* Old exceptions are not signaled. */ |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11179 | fex = alpha_ieee_fpcr_to_swcr(fpcr); |
| 11180 | fex = exc & ~fex; |
| 11181 | fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT; |
| 11182 | fex &= ((CPUArchState *)cpu_env)->swcr; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11183 | |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11184 | /* Update the hardware fpcr. */ |
| 11185 | fpcr |= alpha_ieee_swcr_to_fpcr(exc); |
| 11186 | cpu_alpha_store_fpcr(cpu_env, fpcr); |
| 11187 | |
| 11188 | if (fex) { |
| 11189 | int si_code = TARGET_FPE_FLTUNK; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11190 | target_siginfo_t info; |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11191 | |
| 11192 | if (fex & SWCR_TRAP_ENABLE_DNO) { |
| 11193 | si_code = TARGET_FPE_FLTUND; |
| 11194 | } |
| 11195 | if (fex & SWCR_TRAP_ENABLE_INE) { |
| 11196 | si_code = TARGET_FPE_FLTRES; |
| 11197 | } |
| 11198 | if (fex & SWCR_TRAP_ENABLE_UNF) { |
| 11199 | si_code = TARGET_FPE_FLTUND; |
| 11200 | } |
| 11201 | if (fex & SWCR_TRAP_ENABLE_OVF) { |
| 11202 | si_code = TARGET_FPE_FLTOVF; |
| 11203 | } |
| 11204 | if (fex & SWCR_TRAP_ENABLE_DZE) { |
| 11205 | si_code = TARGET_FPE_FLTDIV; |
| 11206 | } |
| 11207 | if (fex & SWCR_TRAP_ENABLE_INV) { |
| 11208 | si_code = TARGET_FPE_FLTINV; |
| 11209 | } |
| 11210 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 11211 | info.si_signo = SIGFPE; |
| 11212 | info.si_errno = 0; |
| 11213 | info.si_code = si_code; |
| 11214 | info._sifields._sigfault._addr |
| 11215 | = ((CPUArchState *)cpu_env)->pc; |
Peter Maydell | 9d2803f | 2016-07-28 16:44:46 +0100 | [diff] [blame] | 11216 | queue_signal((CPUArchState *)cpu_env, info.si_signo, |
| 11217 | QEMU_SI_FAULT, &info); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11218 | } |
Richard Henderson | 21ba856 | 2019-04-26 15:20:51 -0700 | [diff] [blame] | 11219 | ret = 0; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11220 | } |
| 11221 | break; |
| 11222 | |
| 11223 | /* case SSI_NVPAIRS: |
| 11224 | -- Used with SSIN_UACPROC to enable unaligned accesses. |
| 11225 | case SSI_IEEE_STATE_AT_SIGNAL: |
| 11226 | case SSI_IEEE_IGNORE_STATE_AT_SIGNAL: |
| 11227 | -- Not implemented in linux kernel |
| 11228 | */ |
| 11229 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11230 | return ret; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11231 | #endif |
| 11232 | #ifdef TARGET_NR_osf_sigprocmask |
| 11233 | /* Alpha specific. */ |
| 11234 | case TARGET_NR_osf_sigprocmask: |
| 11235 | { |
| 11236 | abi_ulong mask; |
Juan Quintela | bc088ba | 2011-06-16 17:37:10 +0100 | [diff] [blame] | 11237 | int how; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11238 | sigset_t set, oldset; |
| 11239 | |
| 11240 | switch(arg1) { |
| 11241 | case TARGET_SIG_BLOCK: |
| 11242 | how = SIG_BLOCK; |
| 11243 | break; |
| 11244 | case TARGET_SIG_UNBLOCK: |
| 11245 | how = SIG_UNBLOCK; |
| 11246 | break; |
| 11247 | case TARGET_SIG_SETMASK: |
| 11248 | how = SIG_SETMASK; |
| 11249 | break; |
| 11250 | default: |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11251 | return -TARGET_EINVAL; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11252 | } |
| 11253 | mask = arg2; |
| 11254 | target_to_host_old_sigset(&set, &mask); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 11255 | ret = do_sigprocmask(how, &set, &oldset); |
| 11256 | if (!ret) { |
| 11257 | host_to_target_old_sigset(&mask, &oldset); |
| 11258 | ret = mask; |
| 11259 | } |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11260 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11261 | return ret; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 11262 | #endif |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 11263 | |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11264 | #ifdef TARGET_NR_getgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11265 | case TARGET_NR_getgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11266 | return get_errno(getgid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11267 | #endif |
| 11268 | #ifdef TARGET_NR_geteuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11269 | case TARGET_NR_geteuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11270 | return get_errno(geteuid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11271 | #endif |
| 11272 | #ifdef TARGET_NR_getegid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11273 | case TARGET_NR_getegid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11274 | return get_errno(getegid()); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11275 | #endif |
| 11276 | #ifdef TARGET_NR_setreuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11277 | case TARGET_NR_setreuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11278 | return get_errno(setreuid(arg1, arg2)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11279 | #endif |
| 11280 | #ifdef TARGET_NR_setregid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11281 | case TARGET_NR_setregid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11282 | return get_errno(setregid(arg1, arg2)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11283 | #endif |
| 11284 | #ifdef TARGET_NR_getgroups32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11285 | case TARGET_NR_getgroups32: |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11286 | { |
| 11287 | int gidsetsize = arg1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11288 | uint32_t *target_grouplist; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11289 | gid_t *grouplist; |
| 11290 | int i; |
| 11291 | |
| 11292 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
| 11293 | ret = get_errno(getgroups(gidsetsize, grouplist)); |
balrog | cb3bc23 | 2008-09-20 02:08:13 +0000 | [diff] [blame] | 11294 | if (gidsetsize == 0) |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11295 | return ret; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11296 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11297 | target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0); |
| 11298 | if (!target_grouplist) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11299 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11300 | } |
balrog | a2155fc | 2008-09-20 02:12:08 +0000 | [diff] [blame] | 11301 | for(i = 0;i < ret; i++) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11302 | target_grouplist[i] = tswap32(grouplist[i]); |
| 11303 | unlock_user(target_grouplist, arg2, gidsetsize * 4); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11304 | } |
| 11305 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11306 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11307 | #endif |
| 11308 | #ifdef TARGET_NR_setgroups32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11309 | case TARGET_NR_setgroups32: |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11310 | { |
| 11311 | int gidsetsize = arg1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11312 | uint32_t *target_grouplist; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11313 | gid_t *grouplist; |
| 11314 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 11315 | |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11316 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11317 | target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1); |
| 11318 | if (!target_grouplist) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11319 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11320 | } |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11321 | for(i = 0;i < gidsetsize; i++) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11322 | grouplist[i] = tswap32(target_grouplist[i]); |
| 11323 | unlock_user(target_grouplist, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11324 | return get_errno(setgroups(gidsetsize, grouplist)); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 11325 | } |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11326 | #endif |
| 11327 | #ifdef TARGET_NR_fchown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11328 | case TARGET_NR_fchown32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11329 | return get_errno(fchown(arg1, arg2, arg3)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11330 | #endif |
| 11331 | #ifdef TARGET_NR_setresuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11332 | case TARGET_NR_setresuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11333 | return get_errno(sys_setresuid(arg1, arg2, arg3)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11334 | #endif |
| 11335 | #ifdef TARGET_NR_getresuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11336 | case TARGET_NR_getresuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 11337 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11338 | uid_t ruid, euid, suid; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 11339 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 11340 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 11341 | if (put_user_u32(ruid, arg1) |
| 11342 | || put_user_u32(euid, arg2) |
| 11343 | || put_user_u32(suid, arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11344 | return -TARGET_EFAULT; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 11345 | } |
| 11346 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11347 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11348 | #endif |
| 11349 | #ifdef TARGET_NR_setresgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11350 | case TARGET_NR_setresgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11351 | return get_errno(sys_setresgid(arg1, arg2, arg3)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11352 | #endif |
| 11353 | #ifdef TARGET_NR_getresgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11354 | case TARGET_NR_getresgid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 11355 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11356 | gid_t rgid, egid, sgid; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 11357 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 11358 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 11359 | if (put_user_u32(rgid, arg1) |
| 11360 | || put_user_u32(egid, arg2) |
| 11361 | || put_user_u32(sgid, arg3)) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11362 | return -TARGET_EFAULT; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 11363 | } |
| 11364 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11365 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | #ifdef TARGET_NR_chown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11368 | case TARGET_NR_chown32: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11369 | if (!(p = lock_user_string(arg1))) |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11370 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 11371 | ret = get_errno(chown(p, arg2, arg3)); |
| 11372 | unlock_user(p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11373 | return ret; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11374 | #endif |
| 11375 | #ifdef TARGET_NR_setuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11376 | case TARGET_NR_setuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11377 | return get_errno(sys_setuid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11378 | #endif |
| 11379 | #ifdef TARGET_NR_setgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11380 | case TARGET_NR_setgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11381 | return get_errno(sys_setgid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11382 | #endif |
| 11383 | #ifdef TARGET_NR_setfsuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11384 | case TARGET_NR_setfsuid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11385 | return get_errno(setfsuid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11386 | #endif |
| 11387 | #ifdef TARGET_NR_setfsgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11388 | case TARGET_NR_setfsgid32: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11389 | return get_errno(setfsgid(arg1)); |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 11390 | #endif |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 11391 | #ifdef TARGET_NR_mincore |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11392 | case TARGET_NR_mincore: |
aurel32 | 04bb9ac | 2008-10-01 21:46:41 +0000 | [diff] [blame] | 11393 | { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11394 | void *a = lock_user(VERIFY_READ, arg1, arg2, 0); |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 11395 | if (!a) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11396 | return -TARGET_ENOMEM; |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 11397 | } |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 11398 | p = lock_user_string(arg3); |
| 11399 | if (!p) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11400 | ret = -TARGET_EFAULT; |
| 11401 | } else { |
| 11402 | ret = get_errno(mincore(a, arg2, p)); |
| 11403 | unlock_user(p, arg3, ret); |
Franklin \"Snaipe\" Mathieu | 98a3331 | 2017-02-17 08:58:00 +0000 | [diff] [blame] | 11404 | } |
aurel32 | 04bb9ac | 2008-10-01 21:46:41 +0000 | [diff] [blame] | 11405 | unlock_user(a, arg1, 0); |
| 11406 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11407 | return ret; |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 11408 | #endif |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 11409 | #ifdef TARGET_NR_arm_fadvise64_64 |
| 11410 | case TARGET_NR_arm_fadvise64_64: |
Peter Maydell | e0156a9 | 2016-05-31 15:45:09 +0100 | [diff] [blame] | 11411 | /* arm_fadvise64_64 looks like fadvise64_64 but |
| 11412 | * with different argument order: fd, advice, offset, len |
| 11413 | * rather than the usual fd, offset, len, advice. |
| 11414 | * Note that offset and len are both 64-bit so appear as |
| 11415 | * pairs of 32-bit registers. |
| 11416 | */ |
| 11417 | ret = posix_fadvise(arg1, target_offset64(arg3, arg4), |
| 11418 | target_offset64(arg5, arg6), arg2); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11419 | return -host_to_target_errno(ret); |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 11420 | #endif |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11421 | |
| 11422 | #if TARGET_ABI_BITS == 32 |
| 11423 | |
| 11424 | #ifdef TARGET_NR_fadvise64_64 |
| 11425 | case TARGET_NR_fadvise64_64: |
Max Filippov | 64a563d | 2018-04-01 15:02:34 -0700 | [diff] [blame] | 11426 | #if defined(TARGET_PPC) || defined(TARGET_XTENSA) |
Laurent Vivier | 43046b5 | 2017-03-02 01:11:45 +0100 | [diff] [blame] | 11427 | /* 6 args: fd, advice, offset (high, low), len (high, low) */ |
| 11428 | ret = arg2; |
| 11429 | arg2 = arg3; |
| 11430 | arg3 = arg4; |
| 11431 | arg4 = arg5; |
| 11432 | arg5 = arg6; |
| 11433 | arg6 = ret; |
| 11434 | #else |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11435 | /* 6 args: fd, offset (high, low), len (high, low), advice */ |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 11436 | if (regpairs_aligned(cpu_env, num)) { |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11437 | /* offset is in (3,4), len in (5,6) and advice in 7 */ |
| 11438 | arg2 = arg3; |
| 11439 | arg3 = arg4; |
| 11440 | arg4 = arg5; |
| 11441 | arg5 = arg6; |
| 11442 | arg6 = arg7; |
| 11443 | } |
Laurent Vivier | 43046b5 | 2017-03-02 01:11:45 +0100 | [diff] [blame] | 11444 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11445 | ret = posix_fadvise(arg1, target_offset64(arg2, arg3), |
| 11446 | target_offset64(arg4, arg5), arg6); |
| 11447 | return -host_to_target_errno(ret); |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11448 | #endif |
| 11449 | |
| 11450 | #ifdef TARGET_NR_fadvise64 |
| 11451 | case TARGET_NR_fadvise64: |
| 11452 | /* 5 args: fd, offset (high, low), len, advice */ |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 11453 | if (regpairs_aligned(cpu_env, num)) { |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11454 | /* offset is in (3,4), len in 5 and advice in 6 */ |
| 11455 | arg2 = arg3; |
| 11456 | arg3 = arg4; |
| 11457 | arg4 = arg5; |
| 11458 | arg5 = arg6; |
| 11459 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11460 | ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5); |
| 11461 | return -host_to_target_errno(ret); |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11462 | #endif |
| 11463 | |
| 11464 | #else /* not a 32-bit ABI */ |
Peter Maydell | e0156a9 | 2016-05-31 15:45:09 +0100 | [diff] [blame] | 11465 | #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64) |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 11466 | #ifdef TARGET_NR_fadvise64_64 |
| 11467 | case TARGET_NR_fadvise64_64: |
| 11468 | #endif |
Ulrich Hecht | e72d2cc | 2009-07-24 19:10:31 +0200 | [diff] [blame] | 11469 | #ifdef TARGET_NR_fadvise64 |
| 11470 | case TARGET_NR_fadvise64: |
| 11471 | #endif |
| 11472 | #ifdef TARGET_S390X |
| 11473 | switch (arg4) { |
| 11474 | case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */ |
| 11475 | case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */ |
| 11476 | case 6: arg4 = POSIX_FADV_DONTNEED; break; |
| 11477 | case 7: arg4 = POSIX_FADV_NOREUSE; break; |
| 11478 | default: break; |
| 11479 | } |
| 11480 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11481 | return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4)); |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 11482 | #endif |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 11483 | #endif /* end of 64-bit ABI fadvise handling */ |
| 11484 | |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 11485 | #ifdef TARGET_NR_madvise |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11486 | case TARGET_NR_madvise: |
pbrook | 2483668 | 2006-04-16 14:14:53 +0000 | [diff] [blame] | 11487 | /* A straight passthrough may not be safe because qemu sometimes |
Lei Li | d2d6b85 | 2013-05-20 17:20:50 +0800 | [diff] [blame] | 11488 | turns private file-backed mappings into anonymous mappings. |
pbrook | 2483668 | 2006-04-16 14:14:53 +0000 | [diff] [blame] | 11489 | This will break MADV_DONTNEED. |
| 11490 | This is a hint, so ignoring and returning success is ok. */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11491 | return 0; |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 11492 | #endif |
Alex Bennée | bbf5f2a | 2020-04-03 20:11:40 +0100 | [diff] [blame] | 11493 | #ifdef TARGET_NR_fcntl64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11494 | case TARGET_NR_fcntl64: |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 11495 | { |
Alex Bennée | bbf5f2a | 2020-04-03 20:11:40 +0100 | [diff] [blame] | 11496 | int cmd; |
| 11497 | struct flock64 fl; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 11498 | from_flock64_fn *copyfrom = copy_from_user_flock64; |
| 11499 | to_flock64_fn *copyto = copy_to_user_flock64; |
| 11500 | |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 11501 | #ifdef TARGET_ARM |
Laurent Vivier | 7f254c5 | 2018-05-02 23:57:30 +0200 | [diff] [blame] | 11502 | if (!((CPUARMState *)cpu_env)->eabi) { |
| 11503 | copyfrom = copy_from_user_oabi_flock64; |
| 11504 | copyto = copy_to_user_oabi_flock64; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 11505 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 11506 | #endif |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 11507 | |
Alex Bennée | bbf5f2a | 2020-04-03 20:11:40 +0100 | [diff] [blame] | 11508 | cmd = target_to_host_fcntl_cmd(arg2); |
Peter Maydell | 31b6319 | 2011-12-05 23:11:50 +0000 | [diff] [blame] | 11509 | if (cmd == -TARGET_EINVAL) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11510 | return cmd; |
Peter Maydell | 31b6319 | 2011-12-05 23:11:50 +0000 | [diff] [blame] | 11511 | } |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 11512 | |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 11513 | switch(arg2) { |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 11514 | case TARGET_F_GETLK64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 11515 | ret = copyfrom(&fl, arg3); |
| 11516 | if (ret) { |
| 11517 | break; |
ths | 5813427 | 2007-03-31 18:59:32 +0000 | [diff] [blame] | 11518 | } |
Laurent Vivier | af8ab2b | 2018-07-13 14:58:05 +0200 | [diff] [blame] | 11519 | ret = get_errno(safe_fcntl(arg1, cmd, &fl)); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 11520 | if (ret == 0) { |
| 11521 | ret = copyto(arg3, &fl); |
| 11522 | } |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 11523 | break; |
| 11524 | |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 11525 | case TARGET_F_SETLK64: |
| 11526 | case TARGET_F_SETLKW64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 11527 | ret = copyfrom(&fl, arg3); |
| 11528 | if (ret) { |
| 11529 | break; |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 11530 | } |
Peter Maydell | 435da5e | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 11531 | ret = get_errno(safe_fcntl(arg1, cmd, &fl)); |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 11532 | break; |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 11533 | default: |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 11534 | ret = do_fcntl(arg1, arg2, arg3); |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 11535 | break; |
| 11536 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11537 | return ret; |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 11538 | } |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 11539 | #endif |
ths | 7d600c8 | 2006-12-08 01:32:58 +0000 | [diff] [blame] | 11540 | #ifdef TARGET_NR_cacheflush |
| 11541 | case TARGET_NR_cacheflush: |
| 11542 | /* self-modifying code is handled automatically, so nothing needed */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11543 | return 0; |
ths | 7d600c8 | 2006-12-08 01:32:58 +0000 | [diff] [blame] | 11544 | #endif |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 11545 | #ifdef TARGET_NR_getpagesize |
| 11546 | case TARGET_NR_getpagesize: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11547 | return TARGET_PAGE_SIZE; |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 11548 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11549 | case TARGET_NR_gettid: |
Daniel P. Berrangé | 71ba74f | 2019-03-20 16:18:42 +0000 | [diff] [blame] | 11550 | return get_errno(sys_gettid()); |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 11551 | #ifdef TARGET_NR_readahead |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11552 | case TARGET_NR_readahead: |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 11553 | #if TARGET_ABI_BITS == 32 |
James Clarke | 8bf8e9d | 2017-09-15 20:33:13 +0100 | [diff] [blame] | 11554 | if (regpairs_aligned(cpu_env, num)) { |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 11555 | arg2 = arg3; |
| 11556 | arg3 = arg4; |
| 11557 | arg4 = arg5; |
| 11558 | } |
Lena Djokic | 77c6850 | 2016-11-24 17:08:56 +0100 | [diff] [blame] | 11559 | ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4)); |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 11560 | #else |
| 11561 | ret = get_errno(readahead(arg1, arg2, arg3)); |
| 11562 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11563 | return ret; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 11564 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11565 | #ifdef CONFIG_ATTR |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11566 | #ifdef TARGET_NR_setxattr |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11567 | case TARGET_NR_listxattr: |
| 11568 | case TARGET_NR_llistxattr: |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 11569 | { |
| 11570 | void *p, *b = 0; |
| 11571 | if (arg2) { |
| 11572 | b = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 11573 | if (!b) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11574 | return -TARGET_EFAULT; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 11575 | } |
| 11576 | } |
| 11577 | p = lock_user_string(arg1); |
| 11578 | if (p) { |
| 11579 | if (num == TARGET_NR_listxattr) { |
| 11580 | ret = get_errno(listxattr(p, b, arg3)); |
| 11581 | } else { |
| 11582 | ret = get_errno(llistxattr(p, b, arg3)); |
| 11583 | } |
| 11584 | } else { |
| 11585 | ret = -TARGET_EFAULT; |
| 11586 | } |
| 11587 | unlock_user(p, arg1, 0); |
| 11588 | unlock_user(b, arg2, arg3); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11589 | return ret; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 11590 | } |
| 11591 | case TARGET_NR_flistxattr: |
| 11592 | { |
| 11593 | void *b = 0; |
| 11594 | if (arg2) { |
| 11595 | b = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 11596 | if (!b) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11597 | return -TARGET_EFAULT; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 11598 | } |
| 11599 | } |
| 11600 | ret = get_errno(flistxattr(arg1, b, arg3)); |
| 11601 | unlock_user(b, arg2, arg3); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11602 | return ret; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 11603 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11604 | case TARGET_NR_setxattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11605 | case TARGET_NR_lsetxattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11606 | { |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 11607 | void *p, *n, *v = 0; |
| 11608 | if (arg3) { |
| 11609 | v = lock_user(VERIFY_READ, arg3, arg4, 1); |
| 11610 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11611 | return -TARGET_EFAULT; |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 11612 | } |
| 11613 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11614 | p = lock_user_string(arg1); |
| 11615 | n = lock_user_string(arg2); |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 11616 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11617 | if (num == TARGET_NR_setxattr) { |
| 11618 | ret = get_errno(setxattr(p, n, v, arg4, arg5)); |
| 11619 | } else { |
| 11620 | ret = get_errno(lsetxattr(p, n, v, arg4, arg5)); |
| 11621 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11622 | } else { |
| 11623 | ret = -TARGET_EFAULT; |
| 11624 | } |
| 11625 | unlock_user(p, arg1, 0); |
| 11626 | unlock_user(n, arg2, 0); |
| 11627 | unlock_user(v, arg3, 0); |
| 11628 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11629 | return ret; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11630 | case TARGET_NR_fsetxattr: |
| 11631 | { |
| 11632 | void *n, *v = 0; |
| 11633 | if (arg3) { |
| 11634 | v = lock_user(VERIFY_READ, arg3, arg4, 1); |
| 11635 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11636 | return -TARGET_EFAULT; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11637 | } |
| 11638 | } |
| 11639 | n = lock_user_string(arg2); |
| 11640 | if (n) { |
| 11641 | ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5)); |
| 11642 | } else { |
| 11643 | ret = -TARGET_EFAULT; |
| 11644 | } |
| 11645 | unlock_user(n, arg2, 0); |
| 11646 | unlock_user(v, arg3, 0); |
| 11647 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11648 | return ret; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11649 | case TARGET_NR_getxattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11650 | case TARGET_NR_lgetxattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11651 | { |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 11652 | void *p, *n, *v = 0; |
| 11653 | if (arg3) { |
| 11654 | v = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
| 11655 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11656 | return -TARGET_EFAULT; |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 11657 | } |
| 11658 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11659 | p = lock_user_string(arg1); |
| 11660 | n = lock_user_string(arg2); |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 11661 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11662 | if (num == TARGET_NR_getxattr) { |
| 11663 | ret = get_errno(getxattr(p, n, v, arg4)); |
| 11664 | } else { |
| 11665 | ret = get_errno(lgetxattr(p, n, v, arg4)); |
| 11666 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11667 | } else { |
| 11668 | ret = -TARGET_EFAULT; |
| 11669 | } |
| 11670 | unlock_user(p, arg1, 0); |
| 11671 | unlock_user(n, arg2, 0); |
| 11672 | unlock_user(v, arg3, arg4); |
| 11673 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11674 | return ret; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11675 | case TARGET_NR_fgetxattr: |
| 11676 | { |
| 11677 | void *n, *v = 0; |
| 11678 | if (arg3) { |
| 11679 | v = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
| 11680 | if (!v) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11681 | return -TARGET_EFAULT; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11682 | } |
| 11683 | } |
| 11684 | n = lock_user_string(arg2); |
| 11685 | if (n) { |
| 11686 | ret = get_errno(fgetxattr(arg1, n, v, arg4)); |
| 11687 | } else { |
| 11688 | ret = -TARGET_EFAULT; |
| 11689 | } |
| 11690 | unlock_user(n, arg2, 0); |
| 11691 | unlock_user(v, arg3, arg4); |
| 11692 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11693 | return ret; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11694 | case TARGET_NR_removexattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11695 | case TARGET_NR_lremovexattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11696 | { |
| 11697 | void *p, *n; |
| 11698 | p = lock_user_string(arg1); |
| 11699 | n = lock_user_string(arg2); |
| 11700 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11701 | if (num == TARGET_NR_removexattr) { |
| 11702 | ret = get_errno(removexattr(p, n)); |
| 11703 | } else { |
| 11704 | ret = get_errno(lremovexattr(p, n)); |
| 11705 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11706 | } else { |
| 11707 | ret = -TARGET_EFAULT; |
| 11708 | } |
| 11709 | unlock_user(p, arg1, 0); |
| 11710 | unlock_user(n, arg2, 0); |
| 11711 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11712 | return ret; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 11713 | case TARGET_NR_fremovexattr: |
| 11714 | { |
| 11715 | void *n; |
| 11716 | n = lock_user_string(arg2); |
| 11717 | if (n) { |
| 11718 | ret = get_errno(fremovexattr(arg1, n)); |
| 11719 | } else { |
| 11720 | ret = -TARGET_EFAULT; |
| 11721 | } |
| 11722 | unlock_user(n, arg2, 0); |
| 11723 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11724 | return ret; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11725 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 11726 | #endif /* CONFIG_ATTR */ |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11727 | #ifdef TARGET_NR_set_thread_area |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11728 | case TARGET_NR_set_thread_area: |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 11729 | #if defined(TARGET_MIPS) |
Petar Jovanovic | d279279 | 2014-06-18 17:48:20 +0200 | [diff] [blame] | 11730 | ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1; |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11731 | return 0; |
edgar_igl | ef96779 | 2009-01-07 14:19:38 +0000 | [diff] [blame] | 11732 | #elif defined(TARGET_CRIS) |
| 11733 | if (arg1 & 0xff) |
| 11734 | ret = -TARGET_EINVAL; |
| 11735 | else { |
| 11736 | ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1; |
| 11737 | ret = 0; |
| 11738 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11739 | return ret; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 11740 | #elif defined(TARGET_I386) && defined(TARGET_ABI32) |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11741 | return do_set_thread_area(cpu_env, arg1); |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 11742 | #elif defined(TARGET_M68K) |
| 11743 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 11744 | TaskState *ts = cpu->opaque; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 11745 | ts->tp_value = arg1; |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11746 | return 0; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 11747 | } |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 11748 | #else |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 11749 | return -TARGET_ENOSYS; |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 11750 | #endif |
| 11751 | #endif |
| 11752 | #ifdef TARGET_NR_get_thread_area |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11753 | case TARGET_NR_get_thread_area: |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 11754 | #if defined(TARGET_I386) && defined(TARGET_ABI32) |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11755 | return do_get_thread_area(cpu_env, arg1); |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 11756 | #elif defined(TARGET_M68K) |
| 11757 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 11758 | TaskState *ts = cpu->opaque; |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11759 | return ts->tp_value; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 11760 | } |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 11761 | #else |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 11762 | return -TARGET_ENOSYS; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 11763 | #endif |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 11764 | #endif |
bellard | 48dc41e | 2006-06-21 18:15:50 +0000 | [diff] [blame] | 11765 | #ifdef TARGET_NR_getdomainname |
| 11766 | case TARGET_NR_getdomainname: |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 11767 | return -TARGET_ENOSYS; |
bellard | 48dc41e | 2006-06-21 18:15:50 +0000 | [diff] [blame] | 11768 | #endif |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 11769 | |
Max Filippov | 12e3340 | 2018-04-01 13:13:49 -0700 | [diff] [blame] | 11770 | #ifdef TARGET_NR_clock_settime |
| 11771 | case TARGET_NR_clock_settime: |
| 11772 | { |
| 11773 | struct timespec ts; |
| 11774 | |
| 11775 | ret = target_to_host_timespec(&ts, arg2); |
| 11776 | if (!is_error(ret)) { |
| 11777 | ret = get_errno(clock_settime(arg1, &ts)); |
| 11778 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11779 | return ret; |
Max Filippov | 12e3340 | 2018-04-01 13:13:49 -0700 | [diff] [blame] | 11780 | } |
| 11781 | #endif |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 11782 | #ifdef TARGET_NR_clock_settime64 |
| 11783 | case TARGET_NR_clock_settime64: |
| 11784 | { |
| 11785 | struct timespec ts; |
| 11786 | |
| 11787 | ret = target_to_host_timespec64(&ts, arg2); |
| 11788 | if (!is_error(ret)) { |
| 11789 | ret = get_errno(clock_settime(arg1, &ts)); |
| 11790 | } |
| 11791 | return ret; |
| 11792 | } |
| 11793 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 11794 | #ifdef TARGET_NR_clock_gettime |
| 11795 | case TARGET_NR_clock_gettime: |
| 11796 | { |
| 11797 | struct timespec ts; |
| 11798 | ret = get_errno(clock_gettime(arg1, &ts)); |
| 11799 | if (!is_error(ret)) { |
Max Filippov | b9f9908 | 2018-04-01 13:14:04 -0700 | [diff] [blame] | 11800 | ret = host_to_target_timespec(arg2, &ts); |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 11801 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11802 | return ret; |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 11803 | } |
| 11804 | #endif |
Alistair Francis | c6c8d10 | 2020-03-12 15:13:53 -0700 | [diff] [blame] | 11805 | #ifdef TARGET_NR_clock_gettime64 |
| 11806 | case TARGET_NR_clock_gettime64: |
| 11807 | { |
| 11808 | struct timespec ts; |
| 11809 | ret = get_errno(clock_gettime(arg1, &ts)); |
| 11810 | if (!is_error(ret)) { |
| 11811 | ret = host_to_target_timespec64(arg2, &ts); |
| 11812 | } |
| 11813 | return ret; |
| 11814 | } |
| 11815 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 11816 | #ifdef TARGET_NR_clock_getres |
| 11817 | case TARGET_NR_clock_getres: |
| 11818 | { |
| 11819 | struct timespec ts; |
| 11820 | ret = get_errno(clock_getres(arg1, &ts)); |
| 11821 | if (!is_error(ret)) { |
| 11822 | host_to_target_timespec(arg2, &ts); |
| 11823 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11824 | return ret; |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 11825 | } |
| 11826 | #endif |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 11827 | #ifdef TARGET_NR_clock_nanosleep |
| 11828 | case TARGET_NR_clock_nanosleep: |
| 11829 | { |
| 11830 | struct timespec ts; |
| 11831 | target_to_host_timespec(&ts, arg3); |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 11832 | ret = get_errno(safe_clock_nanosleep(arg1, arg2, |
| 11833 | &ts, arg4 ? &ts : NULL)); |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 11834 | if (arg4) |
| 11835 | host_to_target_timespec(arg4, &ts); |
Tom Musta | 8fbe8fd | 2014-08-12 13:53:41 -0500 | [diff] [blame] | 11836 | |
| 11837 | #if defined(TARGET_PPC) |
| 11838 | /* clock_nanosleep is odd in that it returns positive errno values. |
| 11839 | * On PPC, CR0 bit 3 should be set in such a situation. */ |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 11840 | if (ret && ret != -TARGET_ERESTARTSYS) { |
Tom Musta | 8fbe8fd | 2014-08-12 13:53:41 -0500 | [diff] [blame] | 11841 | ((CPUPPCState *)cpu_env)->crf[0] |= 1; |
| 11842 | } |
| 11843 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11844 | return ret; |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 11845 | } |
| 11846 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 11847 | |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 11848 | #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) |
| 11849 | case TARGET_NR_set_tid_address: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11850 | return get_errno(set_tid_address((int *)g2h(arg1))); |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 11851 | #endif |
| 11852 | |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 11853 | case TARGET_NR_tkill: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11854 | return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2))); |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 11855 | |
ths | 7145557 | 2007-06-21 21:45:30 +0000 | [diff] [blame] | 11856 | case TARGET_NR_tgkill: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11857 | return get_errno(safe_tgkill((int)arg1, (int)arg2, |
| 11858 | target_to_host_signal(arg3))); |
ths | 7145557 | 2007-06-21 21:45:30 +0000 | [diff] [blame] | 11859 | |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 11860 | #ifdef TARGET_NR_set_robust_list |
| 11861 | case TARGET_NR_set_robust_list: |
Peter Maydell | e9a970a | 2013-02-08 04:34:54 +0000 | [diff] [blame] | 11862 | case TARGET_NR_get_robust_list: |
| 11863 | /* The ABI for supporting robust futexes has userspace pass |
| 11864 | * the kernel a pointer to a linked list which is updated by |
| 11865 | * userspace after the syscall; the list is walked by the kernel |
| 11866 | * when the thread exits. Since the linked list in QEMU guest |
| 11867 | * memory isn't a valid linked list for the host and we have |
| 11868 | * no way to reliably intercept the thread-death event, we can't |
| 11869 | * support these. Silently return ENOSYS so that guest userspace |
| 11870 | * falls back to a non-robust futex implementation (which should |
| 11871 | * be OK except in the corner case of the guest crashing while |
| 11872 | * holding a mutex that is shared with another process via |
| 11873 | * shared memory). |
| 11874 | */ |
Richard Henderson | 10f45d9 | 2018-08-18 12:01:07 -0700 | [diff] [blame] | 11875 | return -TARGET_ENOSYS; |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 11876 | #endif |
| 11877 | |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 11878 | #if defined(TARGET_NR_utimensat) |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 11879 | case TARGET_NR_utimensat: |
| 11880 | { |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 11881 | struct timespec *tsp, ts[2]; |
| 11882 | if (!arg3) { |
| 11883 | tsp = NULL; |
| 11884 | } else { |
| 11885 | target_to_host_timespec(ts, arg3); |
| 11886 | target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec)); |
| 11887 | tsp = ts; |
| 11888 | } |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 11889 | if (!arg2) |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 11890 | ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 11891 | else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11892 | if (!(p = lock_user_string(arg2))) { |
Richard Henderson | 259841c | 2018-08-18 12:01:09 -0700 | [diff] [blame] | 11893 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11894 | } |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 11895 | ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11896 | unlock_user(p, arg2, 0); |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 11897 | } |
| 11898 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11899 | return ret; |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 11900 | #endif |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11901 | #ifdef TARGET_NR_futex |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 11902 | case TARGET_NR_futex: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11903 | return do_futex(arg1, arg2, arg3, arg4, arg5, arg6); |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11904 | #endif |
Alistair Francis | 1469029 | 2020-03-18 15:47:01 -0700 | [diff] [blame] | 11905 | #ifdef TARGET_NR_futex_time64 |
| 11906 | case TARGET_NR_futex_time64: |
| 11907 | return do_futex_time64(arg1, arg2, arg3, arg4, arg5, arg6); |
| 11908 | #endif |
aurel32 | dbfe4c3 | 2009-04-08 21:29:30 +0000 | [diff] [blame] | 11909 | #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 11910 | case TARGET_NR_inotify_init: |
| 11911 | ret = get_errno(sys_inotify_init()); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 11912 | if (ret >= 0) { |
| 11913 | fd_trans_register(ret, &target_inotify_trans); |
| 11914 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11915 | return ret; |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 11916 | #endif |
Stefan Weil | a1606b0 | 2010-03-28 11:44:41 +0200 | [diff] [blame] | 11917 | #ifdef CONFIG_INOTIFY1 |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 11918 | #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1) |
| 11919 | case TARGET_NR_inotify_init1: |
Lena Djokic | fea243e | 2016-11-24 17:08:53 +0100 | [diff] [blame] | 11920 | ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1, |
| 11921 | fcntl_flags_tbl))); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 11922 | if (ret >= 0) { |
| 11923 | fd_trans_register(ret, &target_inotify_trans); |
| 11924 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11925 | return ret; |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 11926 | #endif |
Stefan Weil | a1606b0 | 2010-03-28 11:44:41 +0200 | [diff] [blame] | 11927 | #endif |
aurel32 | dbfe4c3 | 2009-04-08 21:29:30 +0000 | [diff] [blame] | 11928 | #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 11929 | case TARGET_NR_inotify_add_watch: |
| 11930 | p = lock_user_string(arg2); |
| 11931 | ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3)); |
| 11932 | unlock_user(p, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11933 | return ret; |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 11934 | #endif |
aurel32 | dbfe4c3 | 2009-04-08 21:29:30 +0000 | [diff] [blame] | 11935 | #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 11936 | case TARGET_NR_inotify_rm_watch: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11937 | return get_errno(sys_inotify_rm_watch(arg1, arg2)); |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 11938 | #endif |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 11939 | |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 11940 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11941 | case TARGET_NR_mq_open: |
| 11942 | { |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 11943 | struct mq_attr posix_mq_attr; |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 11944 | struct mq_attr *pposix_mq_attr; |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 11945 | int host_flags; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11946 | |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 11947 | host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl); |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 11948 | pposix_mq_attr = NULL; |
| 11949 | if (arg4) { |
| 11950 | if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11951 | return -TARGET_EFAULT; |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 11952 | } |
| 11953 | pposix_mq_attr = &posix_mq_attr; |
Tom Musta | b6ce1f6 | 2014-08-12 13:53:36 -0500 | [diff] [blame] | 11954 | } |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 11955 | p = lock_user_string(arg1 - 1); |
| 11956 | if (!p) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 11957 | return -TARGET_EFAULT; |
Aleksandar Markovic | c7536ab | 2016-09-22 18:56:55 +0200 | [diff] [blame] | 11958 | } |
Lena Djokic | 2640077 | 2016-11-24 17:08:58 +0100 | [diff] [blame] | 11959 | ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11960 | unlock_user (p, arg1, 0); |
| 11961 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11962 | return ret; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11963 | |
| 11964 | case TARGET_NR_mq_unlink: |
| 11965 | p = lock_user_string(arg1 - 1); |
Peter Maydell | 3211215 | 2016-07-12 13:02:13 +0100 | [diff] [blame] | 11966 | if (!p) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11967 | return -TARGET_EFAULT; |
Peter Maydell | 3211215 | 2016-07-12 13:02:13 +0100 | [diff] [blame] | 11968 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11969 | ret = get_errno(mq_unlink(p)); |
| 11970 | unlock_user (p, arg1, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11971 | return ret; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11972 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11973 | #ifdef TARGET_NR_mq_timedsend |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11974 | case TARGET_NR_mq_timedsend: |
| 11975 | { |
| 11976 | struct timespec ts; |
| 11977 | |
| 11978 | p = lock_user (VERIFY_READ, arg2, arg3, 1); |
| 11979 | if (arg5 != 0) { |
| 11980 | target_to_host_timespec(&ts, arg5); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 11981 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11982 | host_to_target_timespec(arg5, &ts); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 11983 | } else { |
| 11984 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11985 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11986 | unlock_user (p, arg2, arg3); |
| 11987 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 11988 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11989 | #endif |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11990 | |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 11991 | #ifdef TARGET_NR_mq_timedreceive |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 11992 | case TARGET_NR_mq_timedreceive: |
| 11993 | { |
| 11994 | struct timespec ts; |
| 11995 | unsigned int prio; |
| 11996 | |
| 11997 | p = lock_user (VERIFY_READ, arg2, arg3, 1); |
| 11998 | if (arg5 != 0) { |
| 11999 | target_to_host_timespec(&ts, arg5); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 12000 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 12001 | &prio, &ts)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12002 | host_to_target_timespec(arg5, &ts); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 12003 | } else { |
| 12004 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 12005 | &prio, NULL)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12006 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12007 | unlock_user (p, arg2, arg3); |
| 12008 | if (arg4 != 0) |
| 12009 | put_user_u32(prio, arg4); |
| 12010 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12011 | return ret; |
Alistair Francis | 859e8a8 | 2020-03-12 15:13:49 -0700 | [diff] [blame] | 12012 | #endif |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12013 | |
| 12014 | /* Not implemented for now... */ |
| 12015 | /* case TARGET_NR_mq_notify: */ |
| 12016 | /* break; */ |
| 12017 | |
| 12018 | case TARGET_NR_mq_getsetattr: |
| 12019 | { |
| 12020 | struct mq_attr posix_mq_attr_in, posix_mq_attr_out; |
| 12021 | ret = 0; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12022 | if (arg2 != 0) { |
| 12023 | copy_from_user_mq_attr(&posix_mq_attr_in, arg2); |
Max Filippov | a23ea40 | 2018-03-31 08:20:15 -0700 | [diff] [blame] | 12024 | ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in, |
| 12025 | &posix_mq_attr_out)); |
| 12026 | } else if (arg3 != 0) { |
| 12027 | ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12028 | } |
Max Filippov | a23ea40 | 2018-03-31 08:20:15 -0700 | [diff] [blame] | 12029 | if (ret == 0 && arg3 != 0) { |
| 12030 | copy_to_user_mq_attr(arg3, &posix_mq_attr_out); |
| 12031 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12032 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12033 | return ret; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 12034 | #endif |
| 12035 | |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12036 | #ifdef CONFIG_SPLICE |
| 12037 | #ifdef TARGET_NR_tee |
| 12038 | case TARGET_NR_tee: |
| 12039 | { |
| 12040 | ret = get_errno(tee(arg1,arg2,arg3,arg4)); |
| 12041 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12042 | return ret; |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12043 | #endif |
| 12044 | #ifdef TARGET_NR_splice |
| 12045 | case TARGET_NR_splice: |
| 12046 | { |
| 12047 | loff_t loff_in, loff_out; |
| 12048 | loff_t *ploff_in = NULL, *ploff_out = NULL; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12049 | if (arg2) { |
| 12050 | if (get_user_u64(loff_in, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12051 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12052 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12053 | ploff_in = &loff_in; |
| 12054 | } |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12055 | if (arg4) { |
| 12056 | if (get_user_u64(loff_out, arg4)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12057 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12058 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12059 | ploff_out = &loff_out; |
| 12060 | } |
| 12061 | ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6)); |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12062 | if (arg2) { |
| 12063 | if (put_user_u64(loff_in, arg2)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12064 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12065 | } |
| 12066 | } |
| 12067 | if (arg4) { |
| 12068 | if (put_user_u64(loff_out, arg4)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12069 | return -TARGET_EFAULT; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 12070 | } |
| 12071 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12072 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12073 | return ret; |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12074 | #endif |
| 12075 | #ifdef TARGET_NR_vmsplice |
| 12076 | case TARGET_NR_vmsplice: |
| 12077 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 12078 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 12079 | if (vec != NULL) { |
| 12080 | ret = get_errno(vmsplice(arg1, vec, arg3, arg4)); |
| 12081 | unlock_iovec(vec, arg2, arg3, 0); |
| 12082 | } else { |
| 12083 | ret = -host_to_target_errno(errno); |
| 12084 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12085 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12086 | return ret; |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 12087 | #endif |
| 12088 | #endif /* CONFIG_SPLICE */ |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 12089 | #ifdef CONFIG_EVENTFD |
| 12090 | #if defined(TARGET_NR_eventfd) |
| 12091 | case TARGET_NR_eventfd: |
| 12092 | ret = get_errno(eventfd(arg1, 0)); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 12093 | if (ret >= 0) { |
| 12094 | fd_trans_register(ret, &target_eventfd_trans); |
| 12095 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12096 | return ret; |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 12097 | #endif |
| 12098 | #if defined(TARGET_NR_eventfd2) |
| 12099 | case TARGET_NR_eventfd2: |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 12100 | { |
| 12101 | int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)); |
| 12102 | if (arg2 & TARGET_O_NONBLOCK) { |
| 12103 | host_flags |= O_NONBLOCK; |
| 12104 | } |
| 12105 | if (arg2 & TARGET_O_CLOEXEC) { |
| 12106 | host_flags |= O_CLOEXEC; |
| 12107 | } |
| 12108 | ret = get_errno(eventfd(arg1, host_flags)); |
Philippe Mathieu-Daudé | b929f7e | 2017-07-26 23:42:19 -0300 | [diff] [blame] | 12109 | if (ret >= 0) { |
| 12110 | fd_trans_register(ret, &target_eventfd_trans); |
| 12111 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12112 | return ret; |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 12113 | } |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 12114 | #endif |
| 12115 | #endif /* CONFIG_EVENTFD */ |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 12116 | #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate) |
| 12117 | case TARGET_NR_fallocate: |
Alexander Graf | 20249ae | 2012-02-06 21:37:07 +0100 | [diff] [blame] | 12118 | #if TARGET_ABI_BITS == 32 |
| 12119 | ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4), |
| 12120 | target_offset64(arg5, arg6))); |
| 12121 | #else |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 12122 | ret = get_errno(fallocate(arg1, arg2, arg3, arg4)); |
Alexander Graf | 20249ae | 2012-02-06 21:37:07 +0100 | [diff] [blame] | 12123 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12124 | return ret; |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 12125 | #endif |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12126 | #if defined(CONFIG_SYNC_FILE_RANGE) |
| 12127 | #if defined(TARGET_NR_sync_file_range) |
| 12128 | case TARGET_NR_sync_file_range: |
| 12129 | #if TARGET_ABI_BITS == 32 |
Riku Voipio | bfcedc5 | 2011-06-20 16:24:39 +0300 | [diff] [blame] | 12130 | #if defined(TARGET_MIPS) |
| 12131 | ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), |
| 12132 | target_offset64(arg5, arg6), arg7)); |
| 12133 | #else |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12134 | ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3), |
| 12135 | target_offset64(arg4, arg5), arg6)); |
Riku Voipio | bfcedc5 | 2011-06-20 16:24:39 +0300 | [diff] [blame] | 12136 | #endif /* !TARGET_MIPS */ |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12137 | #else |
| 12138 | ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4)); |
| 12139 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12140 | return ret; |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12141 | #endif |
Laurent Vivier | 5bcb498 | 2020-03-10 11:33:50 +0100 | [diff] [blame] | 12142 | #if defined(TARGET_NR_sync_file_range2) || \ |
| 12143 | defined(TARGET_NR_arm_sync_file_range) |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12144 | #if defined(TARGET_NR_sync_file_range2) |
| 12145 | case TARGET_NR_sync_file_range2: |
Laurent Vivier | 5bcb498 | 2020-03-10 11:33:50 +0100 | [diff] [blame] | 12146 | #endif |
| 12147 | #if defined(TARGET_NR_arm_sync_file_range) |
| 12148 | case TARGET_NR_arm_sync_file_range: |
| 12149 | #endif |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12150 | /* This is like sync_file_range but the arguments are reordered */ |
| 12151 | #if TARGET_ABI_BITS == 32 |
| 12152 | ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), |
| 12153 | target_offset64(arg5, arg6), arg2)); |
| 12154 | #else |
| 12155 | ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2)); |
| 12156 | #endif |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12157 | return ret; |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 12158 | #endif |
| 12159 | #endif |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 12160 | #if defined(TARGET_NR_signalfd4) |
| 12161 | case TARGET_NR_signalfd4: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12162 | return do_signalfd4(arg1, arg2, arg4); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 12163 | #endif |
| 12164 | #if defined(TARGET_NR_signalfd) |
| 12165 | case TARGET_NR_signalfd: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12166 | return do_signalfd4(arg1, arg2, 0); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 12167 | #endif |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12168 | #if defined(CONFIG_EPOLL) |
| 12169 | #if defined(TARGET_NR_epoll_create) |
| 12170 | case TARGET_NR_epoll_create: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12171 | return get_errno(epoll_create(arg1)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12172 | #endif |
| 12173 | #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1) |
| 12174 | case TARGET_NR_epoll_create1: |
Sergei Trofimovich | 386d386 | 2020-04-15 23:05:08 +0100 | [diff] [blame] | 12175 | 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] | 12176 | #endif |
| 12177 | #if defined(TARGET_NR_epoll_ctl) |
| 12178 | case TARGET_NR_epoll_ctl: |
| 12179 | { |
| 12180 | struct epoll_event ep; |
| 12181 | struct epoll_event *epp = 0; |
| 12182 | if (arg4) { |
| 12183 | struct target_epoll_event *target_ep; |
| 12184 | if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12185 | return -TARGET_EFAULT; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12186 | } |
| 12187 | ep.events = tswap32(target_ep->events); |
| 12188 | /* The epoll_data_t union is just opaque data to the kernel, |
| 12189 | * so we transfer all 64 bits across and need not worry what |
| 12190 | * actual data type it is. |
| 12191 | */ |
| 12192 | ep.data.u64 = tswap64(target_ep->data.u64); |
| 12193 | unlock_user_struct(target_ep, arg4, 0); |
| 12194 | epp = &ep; |
| 12195 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12196 | return get_errno(epoll_ctl(arg1, arg2, arg3, epp)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12197 | } |
| 12198 | #endif |
| 12199 | |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 12200 | #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12201 | #if defined(TARGET_NR_epoll_wait) |
| 12202 | case TARGET_NR_epoll_wait: |
| 12203 | #endif |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 12204 | #if defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12205 | case TARGET_NR_epoll_pwait: |
| 12206 | #endif |
| 12207 | { |
| 12208 | struct target_epoll_event *target_ep; |
| 12209 | struct epoll_event *ep; |
| 12210 | int epfd = arg1; |
| 12211 | int maxevents = arg3; |
| 12212 | int timeout = arg4; |
| 12213 | |
Peter Maydell | 2ba7fae3 | 2016-07-18 15:35:59 +0100 | [diff] [blame] | 12214 | if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12215 | return -TARGET_EINVAL; |
Peter Maydell | 2ba7fae3 | 2016-07-18 15:35:59 +0100 | [diff] [blame] | 12216 | } |
| 12217 | |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12218 | target_ep = lock_user(VERIFY_WRITE, arg2, |
| 12219 | maxevents * sizeof(struct target_epoll_event), 1); |
| 12220 | if (!target_ep) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12221 | return -TARGET_EFAULT; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12222 | } |
| 12223 | |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 12224 | ep = g_try_new(struct epoll_event, maxevents); |
| 12225 | if (!ep) { |
| 12226 | unlock_user(target_ep, arg2, 0); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12227 | return -TARGET_ENOMEM; |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 12228 | } |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12229 | |
| 12230 | switch (num) { |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 12231 | #if defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12232 | case TARGET_NR_epoll_pwait: |
| 12233 | { |
| 12234 | target_sigset_t *target_set; |
| 12235 | sigset_t _set, *set = &_set; |
| 12236 | |
| 12237 | if (arg5) { |
Peter Maydell | c815701 | 2016-06-30 14:23:24 +0100 | [diff] [blame] | 12238 | if (arg6 != sizeof(target_sigset_t)) { |
| 12239 | ret = -TARGET_EINVAL; |
| 12240 | break; |
| 12241 | } |
| 12242 | |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12243 | target_set = lock_user(VERIFY_READ, arg5, |
| 12244 | sizeof(target_sigset_t), 1); |
| 12245 | if (!target_set) { |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 12246 | ret = -TARGET_EFAULT; |
| 12247 | break; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12248 | } |
| 12249 | target_to_host_sigset(set, target_set); |
| 12250 | unlock_user(target_set, arg5, 0); |
| 12251 | } else { |
| 12252 | set = NULL; |
| 12253 | } |
| 12254 | |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 12255 | ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, |
| 12256 | set, SIGSET_T_SIZE)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12257 | break; |
| 12258 | } |
| 12259 | #endif |
| 12260 | #if defined(TARGET_NR_epoll_wait) |
| 12261 | case TARGET_NR_epoll_wait: |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 12262 | ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, |
| 12263 | NULL, 0)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12264 | break; |
| 12265 | #endif |
| 12266 | default: |
| 12267 | ret = -TARGET_ENOSYS; |
| 12268 | } |
| 12269 | if (!is_error(ret)) { |
| 12270 | int i; |
| 12271 | for (i = 0; i < ret; i++) { |
| 12272 | target_ep[i].events = tswap32(ep[i].events); |
| 12273 | target_ep[i].data.u64 = tswap64(ep[i].data.u64); |
| 12274 | } |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 12275 | unlock_user(target_ep, arg2, |
| 12276 | ret * sizeof(struct target_epoll_event)); |
| 12277 | } else { |
| 12278 | unlock_user(target_ep, arg2, 0); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12279 | } |
Peter Maydell | 04c95f4 | 2016-07-18 15:36:00 +0100 | [diff] [blame] | 12280 | g_free(ep); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12281 | return ret; |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 12282 | } |
| 12283 | #endif |
| 12284 | #endif |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 12285 | #ifdef TARGET_NR_prlimit64 |
| 12286 | case TARGET_NR_prlimit64: |
| 12287 | { |
| 12288 | /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */ |
| 12289 | struct target_rlimit64 *target_rnew, *target_rold; |
| 12290 | struct host_rlimit64 rnew, rold, *rnewp = 0; |
Felix Janda | 9501801 | 2014-12-02 22:11:17 +0100 | [diff] [blame] | 12291 | int resource = target_to_host_resource(arg2); |
Tobias Koch | 055d92f | 2020-03-05 21:24:00 +0100 | [diff] [blame] | 12292 | |
| 12293 | if (arg3 && (resource != RLIMIT_AS && |
| 12294 | resource != RLIMIT_DATA && |
| 12295 | resource != RLIMIT_STACK)) { |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 12296 | if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12297 | return -TARGET_EFAULT; |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 12298 | } |
| 12299 | rnew.rlim_cur = tswap64(target_rnew->rlim_cur); |
| 12300 | rnew.rlim_max = tswap64(target_rnew->rlim_max); |
| 12301 | unlock_user_struct(target_rnew, arg3, 0); |
| 12302 | rnewp = &rnew; |
| 12303 | } |
| 12304 | |
Felix Janda | 9501801 | 2014-12-02 22:11:17 +0100 | [diff] [blame] | 12305 | ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0)); |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 12306 | if (!is_error(ret) && arg4) { |
| 12307 | if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12308 | return -TARGET_EFAULT; |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 12309 | } |
| 12310 | target_rold->rlim_cur = tswap64(rold.rlim_cur); |
| 12311 | target_rold->rlim_max = tswap64(rold.rlim_max); |
| 12312 | unlock_user_struct(target_rold, arg4, 1); |
| 12313 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12314 | return ret; |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 12315 | } |
| 12316 | #endif |
Richard Henderson | 3d21d29 | 2012-09-15 13:20:46 -0700 | [diff] [blame] | 12317 | #ifdef TARGET_NR_gethostname |
| 12318 | case TARGET_NR_gethostname: |
| 12319 | { |
| 12320 | char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0); |
| 12321 | if (name) { |
| 12322 | ret = get_errno(gethostname(name, arg2)); |
| 12323 | unlock_user(name, arg1, arg2); |
| 12324 | } else { |
| 12325 | ret = -TARGET_EFAULT; |
| 12326 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12327 | return ret; |
Richard Henderson | 3d21d29 | 2012-09-15 13:20:46 -0700 | [diff] [blame] | 12328 | } |
| 12329 | #endif |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 12330 | #ifdef TARGET_NR_atomic_cmpxchg_32 |
| 12331 | case TARGET_NR_atomic_cmpxchg_32: |
| 12332 | { |
| 12333 | /* should use start_exclusive from main.c */ |
| 12334 | abi_ulong mem_value; |
| 12335 | if (get_user_u32(mem_value, arg6)) { |
| 12336 | target_siginfo_t info; |
| 12337 | info.si_signo = SIGSEGV; |
| 12338 | info.si_errno = 0; |
| 12339 | info.si_code = TARGET_SEGV_MAPERR; |
| 12340 | info._sifields._sigfault._addr = arg6; |
Peter Maydell | 9d2803f | 2016-07-28 16:44:46 +0100 | [diff] [blame] | 12341 | queue_signal((CPUArchState *)cpu_env, info.si_signo, |
| 12342 | QEMU_SI_FAULT, &info); |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 12343 | ret = 0xdeadbeef; |
| 12344 | |
| 12345 | } |
| 12346 | if (mem_value == arg2) |
| 12347 | put_user_u32(arg1, arg6); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12348 | return mem_value; |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 12349 | } |
| 12350 | #endif |
| 12351 | #ifdef TARGET_NR_atomic_barrier |
| 12352 | case TARGET_NR_atomic_barrier: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12353 | /* Like the kernel implementation and the |
| 12354 | qemu arm barrier, no-op this? */ |
| 12355 | return 0; |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 12356 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12357 | |
| 12358 | #ifdef TARGET_NR_timer_create |
| 12359 | case TARGET_NR_timer_create: |
| 12360 | { |
| 12361 | /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */ |
| 12362 | |
| 12363 | struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12364 | |
| 12365 | int clkid = arg1; |
| 12366 | int timer_index = next_free_host_timer(); |
| 12367 | |
| 12368 | if (timer_index < 0) { |
| 12369 | ret = -TARGET_EAGAIN; |
| 12370 | } else { |
| 12371 | timer_t *phtimer = g_posix_timers + timer_index; |
| 12372 | |
| 12373 | if (arg2) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12374 | phost_sevp = &host_sevp; |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 12375 | ret = target_to_host_sigevent(phost_sevp, arg2); |
| 12376 | if (ret != 0) { |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12377 | return ret; |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 12378 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12379 | } |
| 12380 | |
| 12381 | ret = get_errno(timer_create(clkid, phost_sevp, phtimer)); |
| 12382 | if (ret) { |
| 12383 | phtimer = NULL; |
| 12384 | } else { |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12385 | if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12386 | return -TARGET_EFAULT; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12387 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12388 | } |
| 12389 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12390 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12391 | } |
| 12392 | #endif |
| 12393 | |
| 12394 | #ifdef TARGET_NR_timer_settime |
| 12395 | case TARGET_NR_timer_settime: |
| 12396 | { |
| 12397 | /* args: timer_t timerid, int flags, const struct itimerspec *new_value, |
| 12398 | * struct itimerspec * old_value */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12399 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12400 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12401 | if (timerid < 0) { |
| 12402 | ret = timerid; |
| 12403 | } else if (arg3 == 0) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12404 | ret = -TARGET_EINVAL; |
| 12405 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12406 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12407 | struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; |
| 12408 | |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 12409 | if (target_to_host_itimerspec(&hspec_new, arg3)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12410 | return -TARGET_EFAULT; |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 12411 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12412 | ret = get_errno( |
| 12413 | timer_settime(htimer, arg2, &hspec_new, &hspec_old)); |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 12414 | if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12415 | return -TARGET_EFAULT; |
Marc-André Lureau | 40c80b5 | 2017-01-19 10:15:32 -0500 | [diff] [blame] | 12416 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12417 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12418 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12419 | } |
| 12420 | #endif |
| 12421 | |
| 12422 | #ifdef TARGET_NR_timer_gettime |
| 12423 | case TARGET_NR_timer_gettime: |
| 12424 | { |
| 12425 | /* args: timer_t timerid, struct itimerspec *curr_value */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12426 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12427 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12428 | if (timerid < 0) { |
| 12429 | ret = timerid; |
| 12430 | } else if (!arg2) { |
| 12431 | ret = -TARGET_EFAULT; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12432 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12433 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12434 | struct itimerspec hspec; |
| 12435 | ret = get_errno(timer_gettime(htimer, &hspec)); |
| 12436 | |
| 12437 | if (host_to_target_itimerspec(arg2, &hspec)) { |
| 12438 | ret = -TARGET_EFAULT; |
| 12439 | } |
| 12440 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12441 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12442 | } |
| 12443 | #endif |
| 12444 | |
| 12445 | #ifdef TARGET_NR_timer_getoverrun |
| 12446 | case TARGET_NR_timer_getoverrun: |
| 12447 | { |
| 12448 | /* args: timer_t timerid */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12449 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12450 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12451 | if (timerid < 0) { |
| 12452 | ret = timerid; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12453 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12454 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12455 | ret = get_errno(timer_getoverrun(htimer)); |
| 12456 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12457 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12458 | } |
| 12459 | #endif |
| 12460 | |
| 12461 | #ifdef TARGET_NR_timer_delete |
| 12462 | case TARGET_NR_timer_delete: |
| 12463 | { |
| 12464 | /* args: timer_t timerid */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12465 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12466 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 12467 | if (timerid < 0) { |
| 12468 | ret = timerid; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12469 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12470 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12471 | ret = get_errno(timer_delete(htimer)); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 12472 | g_posix_timers[timerid] = 0; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12473 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12474 | return ret; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 12475 | } |
| 12476 | #endif |
| 12477 | |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12478 | #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD) |
| 12479 | case TARGET_NR_timerfd_create: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12480 | return get_errno(timerfd_create(arg1, |
| 12481 | target_to_host_bitmask(arg2, fcntl_flags_tbl))); |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12482 | #endif |
| 12483 | |
| 12484 | #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD) |
| 12485 | case TARGET_NR_timerfd_gettime: |
| 12486 | { |
| 12487 | struct itimerspec its_curr; |
| 12488 | |
| 12489 | ret = get_errno(timerfd_gettime(arg1, &its_curr)); |
| 12490 | |
| 12491 | if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12492 | return -TARGET_EFAULT; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12493 | } |
| 12494 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12495 | return ret; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12496 | #endif |
| 12497 | |
| 12498 | #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD) |
| 12499 | case TARGET_NR_timerfd_settime: |
| 12500 | { |
| 12501 | struct itimerspec its_new, its_old, *p_new; |
| 12502 | |
| 12503 | if (arg3) { |
| 12504 | if (target_to_host_itimerspec(&its_new, arg3)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12505 | return -TARGET_EFAULT; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12506 | } |
| 12507 | p_new = &its_new; |
| 12508 | } else { |
| 12509 | p_new = NULL; |
| 12510 | } |
| 12511 | |
| 12512 | ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old)); |
| 12513 | |
| 12514 | if (arg4 && host_to_target_itimerspec(arg4, &its_old)) { |
Richard Henderson | 2852aaf | 2018-08-18 12:01:06 -0700 | [diff] [blame] | 12515 | return -TARGET_EFAULT; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12516 | } |
| 12517 | } |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12518 | return ret; |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 12519 | #endif |
| 12520 | |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 12521 | #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) |
| 12522 | case TARGET_NR_ioprio_get: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12523 | return get_errno(ioprio_get(arg1, arg2)); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 12524 | #endif |
| 12525 | |
| 12526 | #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) |
| 12527 | case TARGET_NR_ioprio_set: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12528 | return get_errno(ioprio_set(arg1, arg2, arg3)); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 12529 | #endif |
| 12530 | |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 12531 | #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS) |
| 12532 | case TARGET_NR_setns: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12533 | return get_errno(setns(arg1, arg2)); |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 12534 | #endif |
| 12535 | #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS) |
| 12536 | case TARGET_NR_unshare: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12537 | return get_errno(unshare(arg1)); |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 12538 | #endif |
Laurent Vivier | 2f14788 | 2016-09-25 22:20:20 +0200 | [diff] [blame] | 12539 | #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp) |
| 12540 | case TARGET_NR_kcmp: |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12541 | return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5)); |
Laurent Vivier | 2f14788 | 2016-09-25 22:20:20 +0200 | [diff] [blame] | 12542 | #endif |
Richard Henderson | fa97e38 | 2018-07-18 13:06:48 -0700 | [diff] [blame] | 12543 | #ifdef TARGET_NR_swapcontext |
| 12544 | case TARGET_NR_swapcontext: |
| 12545 | /* PowerPC specific. */ |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12546 | return do_swapcontext(cpu_env, arg1, arg2, arg3); |
Richard Henderson | fa97e38 | 2018-07-18 13:06:48 -0700 | [diff] [blame] | 12547 | #endif |
Shu-Chun Weng | 9bdfa4d | 2019-08-19 11:09:47 -0700 | [diff] [blame] | 12548 | #ifdef TARGET_NR_memfd_create |
| 12549 | case TARGET_NR_memfd_create: |
| 12550 | p = lock_user_string(arg1); |
| 12551 | if (!p) { |
| 12552 | return -TARGET_EFAULT; |
| 12553 | } |
| 12554 | ret = get_errno(memfd_create(p, arg2)); |
| 12555 | fd_trans_unregister(ret); |
| 12556 | unlock_user(p, arg1, 0); |
| 12557 | return ret; |
| 12558 | #endif |
Andreas Schwab | 8500476 | 2019-05-13 11:02:53 +0200 | [diff] [blame] | 12559 | #if defined TARGET_NR_membarrier && defined __NR_membarrier |
| 12560 | case TARGET_NR_membarrier: |
| 12561 | return get_errno(membarrier(arg1, arg2)); |
| 12562 | #endif |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 12563 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12564 | default: |
Philippe Mathieu-Daudé | 122f9c8 | 2018-07-06 12:51:25 -0300 | [diff] [blame] | 12565 | qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); |
Richard Henderson | 72eb7ea | 2018-08-18 12:01:05 -0700 | [diff] [blame] | 12566 | return -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12567 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 12568 | return ret; |
| 12569 | } |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 12570 | |
| 12571 | abi_long do_syscall(void *cpu_env, int num, abi_long arg1, |
| 12572 | abi_long arg2, abi_long arg3, abi_long arg4, |
| 12573 | abi_long arg5, abi_long arg6, abi_long arg7, |
| 12574 | abi_long arg8) |
| 12575 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 12576 | CPUState *cpu = env_cpu(cpu_env); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 12577 | abi_long ret; |
| 12578 | |
| 12579 | #ifdef DEBUG_ERESTARTSYS |
| 12580 | /* Debug-only code for exercising the syscall-restart code paths |
| 12581 | * in the per-architecture cpu main loops: restart every syscall |
| 12582 | * the guest makes once before letting it through. |
| 12583 | */ |
| 12584 | { |
| 12585 | static bool flag; |
| 12586 | flag = !flag; |
| 12587 | if (flag) { |
| 12588 | return -TARGET_ERESTARTSYS; |
| 12589 | } |
| 12590 | } |
| 12591 | #endif |
| 12592 | |
Emilio G. Cota | c36f7a6 | 2018-10-21 13:27:44 -0400 | [diff] [blame] | 12593 | record_syscall_start(cpu, num, arg1, |
| 12594 | arg2, arg3, arg4, arg5, arg6, arg7, arg8); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 12595 | |
Josh Kunz | 4b25a50 | 2020-02-03 18:54:14 -0800 | [diff] [blame] | 12596 | if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 12597 | print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); |
Josh Kunz | 4b25a50 | 2020-02-03 18:54:14 -0800 | [diff] [blame] | 12598 | } |
| 12599 | |
| 12600 | ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4, |
| 12601 | arg5, arg6, arg7, arg8); |
| 12602 | |
| 12603 | if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { |
Filip Bozuta | c84be71 | 2020-06-19 14:33:26 +0200 | [diff] [blame] | 12604 | print_syscall_ret(num, ret, arg1, arg2, arg3, arg4, arg5, arg6); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 12605 | } |
| 12606 | |
Emilio G. Cota | c36f7a6 | 2018-10-21 13:27:44 -0400 | [diff] [blame] | 12607 | record_syscall_return(cpu, num, ret); |
Richard Henderson | dc1ce18 | 2018-08-18 12:01:04 -0700 | [diff] [blame] | 12608 | return ret; |
| 12609 | } |