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" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 23 | #include <elf.h> |
| 24 | #include <endian.h> |
John Spencer | c56dc77 | 2012-12-10 07:59:46 +0100 | [diff] [blame] | 25 | #include <grp.h> |
ths | d08d3bb | 2007-03-19 13:09:22 +0000 | [diff] [blame] | 26 | #include <sys/ipc.h> |
| 27 | #include <sys/msg.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 28 | #include <sys/wait.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 29 | #include <sys/mount.h> |
John Spencer | 586b0be | 2012-12-26 00:49:49 +0100 | [diff] [blame] | 30 | #include <sys/file.h> |
| 31 | #include <sys/fsuid.h> |
| 32 | #include <sys/personality.h> |
ths | 39b9aae | 2007-02-11 18:36:44 +0000 | [diff] [blame] | 33 | #include <sys/prctl.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 34 | #include <sys/resource.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 35 | #include <sys/swap.h> |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 36 | #include <linux/capability.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 37 | #include <sched.h> |
Aurelien Jarno | 60e9924 | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 38 | #ifdef __ia64__ |
| 39 | int __clone2(int (*fn)(void *), void *child_stack_base, |
| 40 | size_t stack_size, int flags, void *arg, ...); |
| 41 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 42 | #include <sys/socket.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> |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 45 | #include <sys/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> |
Stefan Weil | 5a61cb6 | 2011-09-08 17:55:32 +0200 | [diff] [blame] | 58 | #include "qemu-common.h" |
Riku Voipio | d80a190 | 2014-10-01 16:05:46 +0300 | [diff] [blame] | 59 | #ifdef CONFIG_TIMERFD |
| 60 | #include <sys/timerfd.h> |
| 61 | #endif |
Juan Quintela | 9788c9c | 2009-07-27 16:13:02 +0200 | [diff] [blame] | 62 | #ifdef TARGET_GPROF |
aurel32 | 6d946cd | 2008-11-06 16:15:18 +0000 | [diff] [blame] | 63 | #include <sys/gmon.h> |
| 64 | #endif |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 65 | #ifdef CONFIG_EVENTFD |
| 66 | #include <sys/eventfd.h> |
| 67 | #endif |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 68 | #ifdef CONFIG_EPOLL |
| 69 | #include <sys/epoll.h> |
| 70 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_ATTR |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 72 | #include "qemu/xattr.h" |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 73 | #endif |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 74 | #ifdef CONFIG_SENDFILE |
| 75 | #include <sys/sendfile.h> |
| 76 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 77 | |
| 78 | #define termios host_termios |
| 79 | #define winsize host_winsize |
| 80 | #define termio host_termio |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 81 | #define sgttyb host_sgttyb /* same as target */ |
| 82 | #define tchars host_tchars /* same as target */ |
| 83 | #define ltchars host_ltchars /* same as target */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 84 | |
| 85 | #include <linux/termios.h> |
| 86 | #include <linux/unistd.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 87 | #include <linux/cdrom.h> |
| 88 | #include <linux/hdreg.h> |
| 89 | #include <linux/soundcard.h> |
bellard | 19b84f3 | 2003-05-08 15:41:49 +0000 | [diff] [blame] | 90 | #include <linux/kd.h> |
balrog | 8fbd6b5 | 2008-09-20 03:03:09 +0000 | [diff] [blame] | 91 | #include <linux/mtio.h> |
Martin Mohring | 350d177 | 2009-05-04 21:21:41 +0300 | [diff] [blame] | 92 | #include <linux/fs.h> |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 93 | #if defined(CONFIG_FIEMAP) |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 94 | #include <linux/fiemap.h> |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 95 | #endif |
Ulrich Hecht | f7680a5 | 2009-10-16 17:00:44 +0200 | [diff] [blame] | 96 | #include <linux/fb.h> |
| 97 | #include <linux/vt.h> |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 98 | #include <linux/dm-ioctl.h> |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 99 | #include <linux/reboot.h> |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 100 | #include <linux/route.h> |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 101 | #include <linux/filter.h> |
Andreas Färber | fff8c53 | 2014-01-18 07:38:30 +0100 | [diff] [blame] | 102 | #include <linux/blkpg.h> |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 103 | #include <linux/netlink.h> |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 104 | #ifdef CONFIG_RTNETLINK |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 105 | #include <linux/rtnetlink.h> |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 106 | #endif |
Laurent Vivier | 5ce9bb5 | 2016-05-22 18:56:21 +0200 | [diff] [blame] | 107 | #include <linux/audit.h> |
pbrook | d7e4036 | 2008-05-23 16:06:43 +0000 | [diff] [blame] | 108 | #include "linux_loop.h" |
Riku Voipio | 18cb008 | 2014-02-19 12:59:58 +0200 | [diff] [blame] | 109 | #include "uname.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 110 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 111 | #include "qemu.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 112 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 113 | #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \ |
| 114 | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID) |
pbrook | 30813ce | 2008-06-02 15:45:44 +0000 | [diff] [blame] | 115 | |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 116 | //#define DEBUG |
Timothy E Baldwin | 71a8f7f | 2016-05-12 18:47:45 +0100 | [diff] [blame] | 117 | /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted |
| 118 | * once. This exercises the codepaths for restart. |
| 119 | */ |
| 120 | //#define DEBUG_ERESTARTSYS |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 121 | |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 122 | //#include <linux/msdos_fs.h> |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 123 | #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2]) |
| 124 | #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2]) |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 125 | |
Peter Maydell | b28a1f3 | 2016-05-27 15:51:47 +0100 | [diff] [blame] | 126 | /* This is the size of the host kernel's sigset_t, needed where we make |
| 127 | * direct system calls that take a sigset_t pointer and a size. |
| 128 | */ |
| 129 | #define SIGSET_T_SIZE (_NSIG / 8) |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 130 | |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 131 | #undef _syscall0 |
| 132 | #undef _syscall1 |
| 133 | #undef _syscall2 |
| 134 | #undef _syscall3 |
| 135 | #undef _syscall4 |
| 136 | #undef _syscall5 |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 137 | #undef _syscall6 |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 138 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 139 | #define _syscall0(type,name) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 140 | static type name (void) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 141 | { \ |
| 142 | return syscall(__NR_##name); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 145 | #define _syscall1(type,name,type1,arg1) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 146 | static type name (type1 arg1) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 147 | { \ |
| 148 | return syscall(__NR_##name, arg1); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 149 | } |
| 150 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 151 | #define _syscall2(type,name,type1,arg1,type2,arg2) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 152 | static type name (type1 arg1,type2 arg2) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 153 | { \ |
| 154 | return syscall(__NR_##name, arg1, arg2); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 157 | #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 158 | static type name (type1 arg1,type2 arg2,type3 arg3) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 159 | { \ |
| 160 | return syscall(__NR_##name, arg1, arg2, arg3); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 161 | } |
| 162 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 163 | #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 164 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 165 | { \ |
| 166 | return syscall(__NR_##name, arg1, arg2, arg3, arg4); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 169 | #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
| 170 | type5,arg5) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 171 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 172 | { \ |
| 173 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 174 | } |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 175 | |
| 176 | |
| 177 | #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ |
| 178 | type5,arg5,type6,arg6) \ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 179 | static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \ |
| 180 | type6 arg6) \ |
bellard | 83fcb51 | 2006-06-14 13:37:16 +0000 | [diff] [blame] | 181 | { \ |
| 182 | return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ |
| 183 | } |
| 184 | |
bellard | 70a194b | 2003-08-11 22:20:16 +0000 | [diff] [blame] | 185 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 186 | #define __NR_sys_uname __NR_uname |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 187 | #define __NR_sys_getcwd1 __NR_getcwd |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 188 | #define __NR_sys_getdents __NR_getdents |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 189 | #define __NR_sys_getdents64 __NR_getdents64 |
ths | c6cda17 | 2007-10-09 03:42:34 +0000 | [diff] [blame] | 190 | #define __NR_sys_getpriority __NR_getpriority |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 191 | #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 192 | #define __NR_sys_syslog __NR_syslog |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 193 | #define __NR_sys_futex __NR_futex |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 194 | #define __NR_sys_inotify_init __NR_inotify_init |
| 195 | #define __NR_sys_inotify_add_watch __NR_inotify_add_watch |
| 196 | #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 197 | |
Alexander Graf | 42a39fb | 2011-04-15 17:32:45 +0200 | [diff] [blame] | 198 | #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \ |
| 199 | defined(__s390x__) |
bellard | 9af9eaa | 2003-04-07 21:34:41 +0000 | [diff] [blame] | 200 | #define __NR__llseek __NR_lseek |
| 201 | #endif |
| 202 | |
James Hogan | a29e5ba | 2014-03-25 21:51:08 +0000 | [diff] [blame] | 203 | /* Newer kernel ports have llseek() instead of _llseek() */ |
| 204 | #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek) |
| 205 | #define TARGET_NR__llseek TARGET_NR_llseek |
| 206 | #endif |
| 207 | |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 208 | #ifdef __NR_gettid |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 209 | _syscall0(int, gettid) |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 210 | #else |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 211 | /* This is a replacement for the host gettid() and must return a host |
| 212 | errno. */ |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 213 | static int gettid(void) { |
| 214 | return -ENOSYS; |
| 215 | } |
| 216 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 217 | #if defined(TARGET_NR_getdents) && defined(__NR_getdents) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 218 | _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count); |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 219 | #endif |
| 220 | #if !defined(__NR_getdents) || \ |
| 221 | (defined(TARGET_NR_getdents64) && defined(__NR_getdents64)) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 222 | _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count); |
| 223 | #endif |
Richard Henderson | d35b261 | 2010-06-04 12:14:10 -0700 | [diff] [blame] | 224 | #if defined(TARGET_NR__llseek) && defined(__NR_llseek) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 225 | _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, |
| 226 | loff_t *, res, uint, wh); |
| 227 | #endif |
| 228 | _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo) |
| 229 | _syscall3(int,sys_syslog,int,type,char*,bufp,int,len) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 230 | #ifdef __NR_exit_group |
| 231 | _syscall1(int,exit_group,int,error_code) |
| 232 | #endif |
| 233 | #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) |
| 234 | _syscall1(int,set_tid_address,int *,tidptr) |
| 235 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 236 | #if defined(TARGET_NR_futex) && defined(__NR_futex) |
| 237 | _syscall6(int,sys_futex,int *,uaddr,int,op,int,val, |
| 238 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
| 239 | #endif |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 240 | #define __NR_sys_sched_getaffinity __NR_sched_getaffinity |
| 241 | _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len, |
| 242 | unsigned long *, user_mask_ptr); |
| 243 | #define __NR_sys_sched_setaffinity __NR_sched_setaffinity |
| 244 | _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len, |
| 245 | unsigned long *, user_mask_ptr); |
Alexander Graf | 0f6b4d2 | 2011-09-27 14:39:42 +0200 | [diff] [blame] | 246 | _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, |
| 247 | void *, arg); |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 248 | _syscall2(int, capget, struct __user_cap_header_struct *, header, |
| 249 | struct __user_cap_data_struct *, data); |
| 250 | _syscall2(int, capset, struct __user_cap_header_struct *, header, |
| 251 | struct __user_cap_data_struct *, data); |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 252 | #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) |
| 253 | _syscall2(int, ioprio_get, int, which, int, who) |
| 254 | #endif |
| 255 | #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) |
| 256 | _syscall3(int, ioprio_set, int, which, int, who, int, ioprio) |
| 257 | #endif |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 258 | #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) |
| 259 | _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags) |
| 260 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 261 | |
| 262 | static bitmask_transtbl fcntl_flags_tbl[] = { |
| 263 | { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, }, |
| 264 | { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, }, |
| 265 | { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, }, |
| 266 | { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, }, |
| 267 | { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, }, |
| 268 | { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, }, |
| 269 | { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, }, |
| 270 | { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, }, |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 271 | { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, }, |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 272 | { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, }, |
| 273 | { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, }, |
| 274 | { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, }, |
| 275 | { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, }, |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 276 | #if defined(O_DIRECT) |
| 277 | { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, }, |
| 278 | #endif |
Richard Henderson | afc8763 | 2012-07-25 14:30:34 -0700 | [diff] [blame] | 279 | #if defined(O_NOATIME) |
| 280 | { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME }, |
| 281 | #endif |
| 282 | #if defined(O_CLOEXEC) |
| 283 | { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC }, |
| 284 | #endif |
| 285 | #if defined(O_PATH) |
| 286 | { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH }, |
| 287 | #endif |
| 288 | /* Don't terminate the list prematurely on 64-bit host+guest. */ |
| 289 | #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0 |
| 290 | { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, }, |
| 291 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 292 | { 0, 0, 0, 0 } |
| 293 | }; |
| 294 | |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 295 | typedef abi_long (*TargetFdDataFunc)(void *, size_t); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 296 | typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 297 | typedef struct TargetFdTrans { |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 298 | TargetFdDataFunc host_to_target_data; |
| 299 | TargetFdDataFunc target_to_host_data; |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 300 | TargetFdAddrFunc target_to_host_addr; |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 301 | } TargetFdTrans; |
| 302 | |
| 303 | static TargetFdTrans **target_fd_trans; |
| 304 | |
| 305 | static unsigned int target_fd_max; |
| 306 | |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 307 | static TargetFdDataFunc fd_trans_target_to_host_data(int fd) |
| 308 | { |
| 309 | if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) { |
| 310 | return target_fd_trans[fd]->target_to_host_data; |
| 311 | } |
| 312 | return NULL; |
| 313 | } |
| 314 | |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 315 | static TargetFdDataFunc fd_trans_host_to_target_data(int fd) |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 316 | { |
Laurent Vivier | 861d72c | 2015-10-28 21:40:46 +0100 | [diff] [blame] | 317 | if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) { |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 318 | return target_fd_trans[fd]->host_to_target_data; |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 319 | } |
| 320 | return NULL; |
| 321 | } |
| 322 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 323 | static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd) |
| 324 | { |
Laurent Vivier | 861d72c | 2015-10-28 21:40:46 +0100 | [diff] [blame] | 325 | if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) { |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 326 | return target_fd_trans[fd]->target_to_host_addr; |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 331 | static void fd_trans_register(int fd, TargetFdTrans *trans) |
| 332 | { |
| 333 | unsigned int oldmax; |
| 334 | |
| 335 | if (fd >= target_fd_max) { |
| 336 | oldmax = target_fd_max; |
| 337 | target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */ |
Laurent Vivier | 5089c7c | 2016-01-18 23:50:45 +0100 | [diff] [blame] | 338 | target_fd_trans = g_renew(TargetFdTrans *, |
| 339 | target_fd_trans, target_fd_max); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 340 | memset((void *)(target_fd_trans + oldmax), 0, |
| 341 | (target_fd_max - oldmax) * sizeof(TargetFdTrans *)); |
| 342 | } |
| 343 | target_fd_trans[fd] = trans; |
| 344 | } |
| 345 | |
| 346 | static void fd_trans_unregister(int fd) |
| 347 | { |
| 348 | if (fd >= 0 && fd < target_fd_max) { |
| 349 | target_fd_trans[fd] = NULL; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static void fd_trans_dup(int oldfd, int newfd) |
| 354 | { |
| 355 | fd_trans_unregister(newfd); |
| 356 | if (oldfd < target_fd_max && target_fd_trans[oldfd]) { |
| 357 | fd_trans_register(newfd, target_fd_trans[oldfd]); |
| 358 | } |
| 359 | } |
| 360 | |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 361 | static int sys_getcwd1(char *buf, size_t size) |
| 362 | { |
| 363 | if (getcwd(buf, size) == NULL) { |
| 364 | /* getcwd() sets errno */ |
| 365 | return (-1); |
| 366 | } |
aurel32 | aaf4ad3 | 2009-04-16 14:17:14 +0000 | [diff] [blame] | 367 | return strlen(buf)+1; |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 370 | #ifdef TARGET_NR_utimensat |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 371 | #ifdef CONFIG_UTIMENSAT |
| 372 | static int sys_utimensat(int dirfd, const char *pathname, |
| 373 | const struct timespec times[2], int flags) |
| 374 | { |
| 375 | if (pathname == NULL) |
| 376 | return futimens(dirfd, times); |
| 377 | else |
| 378 | return utimensat(dirfd, pathname, times, flags); |
| 379 | } |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 380 | #elif defined(__NR_utimensat) |
| 381 | #define __NR_sys_utimensat __NR_utimensat |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 382 | _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, |
| 383 | const struct timespec *,tsp,int,flags) |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 384 | #else |
| 385 | static int sys_utimensat(int dirfd, const char *pathname, |
| 386 | const struct timespec times[2], int flags) |
| 387 | { |
| 388 | errno = ENOSYS; |
| 389 | return -1; |
| 390 | } |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 391 | #endif |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 392 | #endif /* TARGET_NR_utimensat */ |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 393 | |
| 394 | #ifdef CONFIG_INOTIFY |
aurel32 | 8690e42 | 2009-04-17 13:50:32 +0000 | [diff] [blame] | 395 | #include <sys/inotify.h> |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 396 | |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 397 | #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 398 | static int sys_inotify_init(void) |
| 399 | { |
| 400 | return (inotify_init()); |
| 401 | } |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 402 | #endif |
| 403 | #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 404 | static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask) |
| 405 | { |
| 406 | return (inotify_add_watch(fd, pathname, mask)); |
| 407 | } |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 408 | #endif |
| 409 | #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch) |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 410 | static int sys_inotify_rm_watch(int fd, int32_t wd) |
| 411 | { |
aurel32 | 8690e42 | 2009-04-17 13:50:32 +0000 | [diff] [blame] | 412 | return (inotify_rm_watch(fd, wd)); |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 413 | } |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 414 | #endif |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 415 | #ifdef CONFIG_INOTIFY1 |
| 416 | #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1) |
| 417 | static int sys_inotify_init1(int flags) |
| 418 | { |
| 419 | return (inotify_init1(flags)); |
| 420 | } |
| 421 | #endif |
| 422 | #endif |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 423 | #else |
| 424 | /* Userspace can usually survive runtime without inotify */ |
| 425 | #undef TARGET_NR_inotify_init |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 426 | #undef TARGET_NR_inotify_init1 |
aurel32 | 3b3f24a | 2009-04-15 16:12:13 +0000 | [diff] [blame] | 427 | #undef TARGET_NR_inotify_add_watch |
| 428 | #undef TARGET_NR_inotify_rm_watch |
| 429 | #endif /* CONFIG_INOTIFY */ |
| 430 | |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 431 | #if defined(TARGET_NR_prlimit64) |
| 432 | #ifndef __NR_prlimit64 |
| 433 | # define __NR_prlimit64 -1 |
| 434 | #endif |
| 435 | #define __NR_sys_prlimit64 __NR_prlimit64 |
| 436 | /* The glibc rlimit structure may not be that used by the underlying syscall */ |
| 437 | struct host_rlimit64 { |
| 438 | uint64_t rlim_cur; |
| 439 | uint64_t rlim_max; |
| 440 | }; |
| 441 | _syscall4(int, sys_prlimit64, pid_t, pid, int, resource, |
| 442 | const struct host_rlimit64 *, new_limit, |
| 443 | struct host_rlimit64 *, old_limit) |
| 444 | #endif |
| 445 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 446 | |
| 447 | #if defined(TARGET_NR_timer_create) |
| 448 | /* Maxiumum of 32 active POSIX timers allowed at any one time. */ |
| 449 | static timer_t g_posix_timers[32] = { 0, } ; |
| 450 | |
| 451 | static inline int next_free_host_timer(void) |
| 452 | { |
| 453 | int k ; |
| 454 | /* FIXME: Does finding the next free slot require a lock? */ |
| 455 | for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) { |
| 456 | if (g_posix_timers[k] == 0) { |
| 457 | g_posix_timers[k] = (timer_t) 1; |
| 458 | return k; |
| 459 | } |
| 460 | } |
| 461 | return -1; |
| 462 | } |
| 463 | #endif |
| 464 | |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 465 | /* 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] | 466 | #ifdef TARGET_ARM |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 467 | static inline int regpairs_aligned(void *cpu_env) { |
| 468 | return ((((CPUARMState *)cpu_env)->eabi) == 1) ; |
| 469 | } |
| 470 | #elif defined(TARGET_MIPS) |
| 471 | static inline int regpairs_aligned(void *cpu_env) { return 1; } |
Alexander Graf | 4a1def4 | 2012-09-29 15:32:38 +0000 | [diff] [blame] | 472 | #elif defined(TARGET_PPC) && !defined(TARGET_PPC64) |
| 473 | /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs |
| 474 | * of registers which translates to the same as ARM/MIPS, because we start with |
| 475 | * r3 as arg1 */ |
| 476 | static inline int regpairs_aligned(void *cpu_env) { return 1; } |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 477 | #else |
| 478 | static inline int regpairs_aligned(void *cpu_env) { return 0; } |
| 479 | #endif |
| 480 | |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 481 | #define ERRNO_TABLE_SIZE 1200 |
| 482 | |
| 483 | /* target_to_host_errno_table[] is initialized from |
| 484 | * host_to_target_errno_table[] in syscall_init(). */ |
| 485 | static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = { |
| 486 | }; |
| 487 | |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 488 | /* |
ths | fe8f096 | 2007-07-12 10:59:21 +0000 | [diff] [blame] | 489 | * 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] | 490 | * minus the errnos that are not actually generic to all archs. |
| 491 | */ |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 492 | static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = { |
Chen Gang | 08703b9 | 2015-09-07 10:35:06 +0800 | [diff] [blame] | 493 | [EAGAIN] = TARGET_EAGAIN, |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 494 | [EIDRM] = TARGET_EIDRM, |
| 495 | [ECHRNG] = TARGET_ECHRNG, |
| 496 | [EL2NSYNC] = TARGET_EL2NSYNC, |
| 497 | [EL3HLT] = TARGET_EL3HLT, |
| 498 | [EL3RST] = TARGET_EL3RST, |
| 499 | [ELNRNG] = TARGET_ELNRNG, |
| 500 | [EUNATCH] = TARGET_EUNATCH, |
| 501 | [ENOCSI] = TARGET_ENOCSI, |
| 502 | [EL2HLT] = TARGET_EL2HLT, |
| 503 | [EDEADLK] = TARGET_EDEADLK, |
| 504 | [ENOLCK] = TARGET_ENOLCK, |
| 505 | [EBADE] = TARGET_EBADE, |
| 506 | [EBADR] = TARGET_EBADR, |
| 507 | [EXFULL] = TARGET_EXFULL, |
| 508 | [ENOANO] = TARGET_ENOANO, |
| 509 | [EBADRQC] = TARGET_EBADRQC, |
| 510 | [EBADSLT] = TARGET_EBADSLT, |
| 511 | [EBFONT] = TARGET_EBFONT, |
| 512 | [ENOSTR] = TARGET_ENOSTR, |
| 513 | [ENODATA] = TARGET_ENODATA, |
| 514 | [ETIME] = TARGET_ETIME, |
| 515 | [ENOSR] = TARGET_ENOSR, |
| 516 | [ENONET] = TARGET_ENONET, |
| 517 | [ENOPKG] = TARGET_ENOPKG, |
| 518 | [EREMOTE] = TARGET_EREMOTE, |
| 519 | [ENOLINK] = TARGET_ENOLINK, |
| 520 | [EADV] = TARGET_EADV, |
| 521 | [ESRMNT] = TARGET_ESRMNT, |
| 522 | [ECOMM] = TARGET_ECOMM, |
| 523 | [EPROTO] = TARGET_EPROTO, |
| 524 | [EDOTDOT] = TARGET_EDOTDOT, |
| 525 | [EMULTIHOP] = TARGET_EMULTIHOP, |
| 526 | [EBADMSG] = TARGET_EBADMSG, |
| 527 | [ENAMETOOLONG] = TARGET_ENAMETOOLONG, |
| 528 | [EOVERFLOW] = TARGET_EOVERFLOW, |
| 529 | [ENOTUNIQ] = TARGET_ENOTUNIQ, |
| 530 | [EBADFD] = TARGET_EBADFD, |
| 531 | [EREMCHG] = TARGET_EREMCHG, |
| 532 | [ELIBACC] = TARGET_ELIBACC, |
| 533 | [ELIBBAD] = TARGET_ELIBBAD, |
| 534 | [ELIBSCN] = TARGET_ELIBSCN, |
| 535 | [ELIBMAX] = TARGET_ELIBMAX, |
| 536 | [ELIBEXEC] = TARGET_ELIBEXEC, |
| 537 | [EILSEQ] = TARGET_EILSEQ, |
| 538 | [ENOSYS] = TARGET_ENOSYS, |
| 539 | [ELOOP] = TARGET_ELOOP, |
| 540 | [ERESTART] = TARGET_ERESTART, |
| 541 | [ESTRPIPE] = TARGET_ESTRPIPE, |
| 542 | [ENOTEMPTY] = TARGET_ENOTEMPTY, |
| 543 | [EUSERS] = TARGET_EUSERS, |
| 544 | [ENOTSOCK] = TARGET_ENOTSOCK, |
| 545 | [EDESTADDRREQ] = TARGET_EDESTADDRREQ, |
| 546 | [EMSGSIZE] = TARGET_EMSGSIZE, |
| 547 | [EPROTOTYPE] = TARGET_EPROTOTYPE, |
| 548 | [ENOPROTOOPT] = TARGET_ENOPROTOOPT, |
| 549 | [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT, |
| 550 | [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT, |
| 551 | [EOPNOTSUPP] = TARGET_EOPNOTSUPP, |
| 552 | [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT, |
| 553 | [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT, |
| 554 | [EADDRINUSE] = TARGET_EADDRINUSE, |
| 555 | [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL, |
| 556 | [ENETDOWN] = TARGET_ENETDOWN, |
| 557 | [ENETUNREACH] = TARGET_ENETUNREACH, |
| 558 | [ENETRESET] = TARGET_ENETRESET, |
| 559 | [ECONNABORTED] = TARGET_ECONNABORTED, |
| 560 | [ECONNRESET] = TARGET_ECONNRESET, |
| 561 | [ENOBUFS] = TARGET_ENOBUFS, |
| 562 | [EISCONN] = TARGET_EISCONN, |
| 563 | [ENOTCONN] = TARGET_ENOTCONN, |
| 564 | [EUCLEAN] = TARGET_EUCLEAN, |
| 565 | [ENOTNAM] = TARGET_ENOTNAM, |
| 566 | [ENAVAIL] = TARGET_ENAVAIL, |
| 567 | [EISNAM] = TARGET_EISNAM, |
| 568 | [EREMOTEIO] = TARGET_EREMOTEIO, |
| 569 | [ESHUTDOWN] = TARGET_ESHUTDOWN, |
| 570 | [ETOOMANYREFS] = TARGET_ETOOMANYREFS, |
| 571 | [ETIMEDOUT] = TARGET_ETIMEDOUT, |
| 572 | [ECONNREFUSED] = TARGET_ECONNREFUSED, |
| 573 | [EHOSTDOWN] = TARGET_EHOSTDOWN, |
| 574 | [EHOSTUNREACH] = TARGET_EHOSTUNREACH, |
| 575 | [EALREADY] = TARGET_EALREADY, |
| 576 | [EINPROGRESS] = TARGET_EINPROGRESS, |
| 577 | [ESTALE] = TARGET_ESTALE, |
| 578 | [ECANCELED] = TARGET_ECANCELED, |
| 579 | [ENOMEDIUM] = TARGET_ENOMEDIUM, |
| 580 | [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 581 | #ifdef ENOKEY |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 582 | [ENOKEY] = TARGET_ENOKEY, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 583 | #endif |
| 584 | #ifdef EKEYEXPIRED |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 585 | [EKEYEXPIRED] = TARGET_EKEYEXPIRED, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 586 | #endif |
| 587 | #ifdef EKEYREVOKED |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 588 | [EKEYREVOKED] = TARGET_EKEYREVOKED, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 589 | #endif |
| 590 | #ifdef EKEYREJECTED |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 591 | [EKEYREJECTED] = TARGET_EKEYREJECTED, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 592 | #endif |
| 593 | #ifdef EOWNERDEAD |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 594 | [EOWNERDEAD] = TARGET_EOWNERDEAD, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 595 | #endif |
| 596 | #ifdef ENOTRECOVERABLE |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 597 | [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE, |
ths | b7fe5db | 2007-07-23 15:37:46 +0000 | [diff] [blame] | 598 | #endif |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 599 | }; |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 600 | |
| 601 | static inline int host_to_target_errno(int err) |
| 602 | { |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 603 | if (err >= 0 && err < ERRNO_TABLE_SIZE && |
| 604 | host_to_target_errno_table[err]) { |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 605 | return host_to_target_errno_table[err]; |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 606 | } |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 607 | return err; |
| 608 | } |
| 609 | |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 610 | static inline int target_to_host_errno(int err) |
| 611 | { |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 612 | if (err >= 0 && err < ERRNO_TABLE_SIZE && |
| 613 | target_to_host_errno_table[err]) { |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 614 | return target_to_host_errno_table[err]; |
Timothy E Baldwin | 2466119 | 2016-05-12 18:47:25 +0100 | [diff] [blame] | 615 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 616 | return err; |
| 617 | } |
| 618 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 619 | static inline abi_long get_errno(abi_long ret) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 620 | { |
| 621 | if (ret == -1) |
ths | 637947f | 2007-06-01 12:09:19 +0000 | [diff] [blame] | 622 | return -host_to_target_errno(errno); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 623 | else |
| 624 | return ret; |
| 625 | } |
| 626 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 627 | static inline int is_error(abi_long ret) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 628 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 629 | return (abi_ulong)ret >= (abi_ulong)(-4096); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Peter Maydell | 7dcdaea | 2016-06-06 19:58:18 +0100 | [diff] [blame] | 632 | const char *target_strerror(int err) |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 633 | { |
Peter Maydell | da2a34f | 2016-06-06 19:58:19 +0100 | [diff] [blame] | 634 | if (err == TARGET_ERESTARTSYS) { |
| 635 | return "To be restarted"; |
| 636 | } |
| 637 | if (err == TARGET_QEMU_ESIGRETURN) { |
| 638 | return "Successful exit from sigreturn"; |
| 639 | } |
| 640 | |
Alexander Graf | 962b289 | 2011-11-21 12:04:07 +0100 | [diff] [blame] | 641 | if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) { |
| 642 | return NULL; |
| 643 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 644 | return strerror(target_to_host_errno(err)); |
| 645 | } |
| 646 | |
Timothy E Baldwin | 4d330ce | 2016-05-12 18:47:46 +0100 | [diff] [blame] | 647 | #define safe_syscall0(type, name) \ |
| 648 | static type safe_##name(void) \ |
| 649 | { \ |
| 650 | return safe_syscall(__NR_##name); \ |
| 651 | } |
| 652 | |
| 653 | #define safe_syscall1(type, name, type1, arg1) \ |
| 654 | static type safe_##name(type1 arg1) \ |
| 655 | { \ |
| 656 | return safe_syscall(__NR_##name, arg1); \ |
| 657 | } |
| 658 | |
| 659 | #define safe_syscall2(type, name, type1, arg1, type2, arg2) \ |
| 660 | static type safe_##name(type1 arg1, type2 arg2) \ |
| 661 | { \ |
| 662 | return safe_syscall(__NR_##name, arg1, arg2); \ |
| 663 | } |
| 664 | |
| 665 | #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ |
| 666 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \ |
| 667 | { \ |
| 668 | return safe_syscall(__NR_##name, arg1, arg2, arg3); \ |
| 669 | } |
| 670 | |
| 671 | #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 672 | type4, arg4) \ |
| 673 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ |
| 674 | { \ |
| 675 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \ |
| 676 | } |
| 677 | |
| 678 | #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 679 | type4, arg4, type5, arg5) \ |
| 680 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ |
| 681 | type5 arg5) \ |
| 682 | { \ |
| 683 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ |
| 684 | } |
| 685 | |
| 686 | #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \ |
| 687 | type4, arg4, type5, arg5, type6, arg6) \ |
| 688 | static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ |
| 689 | type5 arg5, type6 arg6) \ |
| 690 | { \ |
| 691 | return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ |
| 692 | } |
| 693 | |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 694 | safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count) |
| 695 | 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] | 696 | safe_syscall4(int, openat, int, dirfd, const char *, pathname, \ |
| 697 | int, flags, mode_t, mode) |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 698 | safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \ |
| 699 | struct rusage *, rusage) |
| 700 | safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ |
| 701 | int, options, struct rusage *, rusage) |
Timothy E Baldwin | ffdcbe2 | 2016-05-12 18:47:50 +0100 | [diff] [blame] | 702 | safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 703 | safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ |
| 704 | fd_set *, exceptfds, struct timespec *, timeout, void *, sig) |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 705 | safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, |
| 706 | struct timespec *, tsp, const sigset_t *, sigmask, |
| 707 | size_t, sigsetsize) |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 708 | safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events, |
| 709 | int, maxevents, int, timeout, const sigset_t *, sigmask, |
| 710 | size_t, sigsetsize) |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 711 | safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \ |
| 712 | const struct timespec *,timeout,int *,uaddr2,int,val3) |
Peter Maydell | 2fe4fba | 2016-05-27 15:51:48 +0100 | [diff] [blame] | 713 | safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize) |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 714 | safe_syscall2(int, kill, pid_t, pid, int, sig) |
| 715 | safe_syscall2(int, tkill, int, tid, int, sig) |
| 716 | safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig) |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 717 | safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt) |
| 718 | safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt) |
Peter Maydell | 2a3c761 | 2016-06-06 19:58:03 +0100 | [diff] [blame] | 719 | safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr, |
| 720 | socklen_t, addrlen) |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 721 | safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len, |
| 722 | int, flags, const struct sockaddr *, addr, socklen_t, addrlen) |
| 723 | safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len, |
| 724 | int, flags, struct sockaddr *, addr, socklen_t *, addrlen) |
| 725 | safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags) |
| 726 | safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags) |
Peter Maydell | 2a84598 | 2016-06-06 19:58:07 +0100 | [diff] [blame] | 727 | safe_syscall2(int, flock, int, fd, int, operation) |
Peter Maydell | b3f8233 | 2016-06-06 19:58:08 +0100 | [diff] [blame] | 728 | safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo, |
| 729 | const struct timespec *, uts, size_t, sigsetsize) |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 730 | safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len, |
| 731 | int, flags) |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 732 | safe_syscall2(int, nanosleep, const struct timespec *, req, |
| 733 | struct timespec *, rem) |
| 734 | #ifdef TARGET_NR_clock_nanosleep |
| 735 | safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags, |
| 736 | const struct timespec *, req, struct timespec *, rem) |
| 737 | #endif |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 738 | #ifdef __NR_msgsnd |
| 739 | safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz, |
| 740 | int, flags) |
| 741 | safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz, |
| 742 | long, msgtype, int, flags) |
Peter Maydell | ffb7ee7 | 2016-06-06 19:58:12 +0100 | [diff] [blame] | 743 | safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops, |
| 744 | unsigned, nsops, const struct timespec *, timeout) |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 745 | #else |
| 746 | /* This host kernel architecture uses a single ipc syscall; fake up |
| 747 | * wrappers for the sub-operations to hide this implementation detail. |
| 748 | * Annoyingly we can't include linux/ipc.h to get the constant definitions |
| 749 | * for the call parameter because some structs in there conflict with the |
| 750 | * sys/ipc.h ones. So we just define them here, and rely on them being |
| 751 | * the same for all host architectures. |
| 752 | */ |
Peter Maydell | ffb7ee7 | 2016-06-06 19:58:12 +0100 | [diff] [blame] | 753 | #define Q_SEMTIMEDOP 4 |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 754 | #define Q_MSGSND 11 |
| 755 | #define Q_MSGRCV 12 |
| 756 | #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP)) |
| 757 | |
| 758 | safe_syscall6(int, ipc, int, call, long, first, long, second, long, third, |
| 759 | void *, ptr, long, fifth) |
| 760 | static int safe_msgsnd(int msgid, const void *msgp, size_t sz, int flags) |
| 761 | { |
| 762 | return safe_ipc(Q_IPCCALL(0, Q_MSGSND), msgid, sz, flags, (void *)msgp, 0); |
| 763 | } |
| 764 | static int safe_msgrcv(int msgid, void *msgp, size_t sz, long type, int flags) |
| 765 | { |
| 766 | return safe_ipc(Q_IPCCALL(1, Q_MSGRCV), msgid, sz, flags, msgp, type); |
| 767 | } |
Peter Maydell | ffb7ee7 | 2016-06-06 19:58:12 +0100 | [diff] [blame] | 768 | static int safe_semtimedop(int semid, struct sembuf *tsops, unsigned nsops, |
| 769 | const struct timespec *timeout) |
| 770 | { |
| 771 | return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP), semid, nsops, 0, tsops, |
| 772 | (long)timeout); |
| 773 | } |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 774 | #endif |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 775 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
| 776 | safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr, |
| 777 | size_t, len, unsigned, prio, const struct timespec *, timeout) |
| 778 | safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr, |
| 779 | size_t, len, unsigned *, prio, const struct timespec *, timeout) |
| 780 | #endif |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 781 | /* We do ioctl like this rather than via safe_syscall3 to preserve the |
| 782 | * "third argument might be integer or pointer or not present" behaviour of |
| 783 | * the libc function. |
| 784 | */ |
| 785 | #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__) |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 786 | |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 787 | static inline int host_to_target_sock_type(int host_type) |
| 788 | { |
| 789 | int target_type; |
| 790 | |
| 791 | switch (host_type & 0xf /* SOCK_TYPE_MASK */) { |
| 792 | case SOCK_DGRAM: |
| 793 | target_type = TARGET_SOCK_DGRAM; |
| 794 | break; |
| 795 | case SOCK_STREAM: |
| 796 | target_type = TARGET_SOCK_STREAM; |
| 797 | break; |
| 798 | default: |
| 799 | target_type = host_type & 0xf /* SOCK_TYPE_MASK */; |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | #if defined(SOCK_CLOEXEC) |
| 804 | if (host_type & SOCK_CLOEXEC) { |
| 805 | target_type |= TARGET_SOCK_CLOEXEC; |
| 806 | } |
| 807 | #endif |
| 808 | |
| 809 | #if defined(SOCK_NONBLOCK) |
| 810 | if (host_type & SOCK_NONBLOCK) { |
| 811 | target_type |= TARGET_SOCK_NONBLOCK; |
| 812 | } |
| 813 | #endif |
| 814 | |
| 815 | return target_type; |
| 816 | } |
| 817 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 818 | static abi_ulong target_brk; |
| 819 | static abi_ulong target_original_brk; |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 820 | static abi_ulong brk_page; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 821 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 822 | void target_set_brk(abi_ulong new_brk) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 823 | { |
blueswir1 | 4c1de73 | 2007-07-07 20:45:44 +0000 | [diff] [blame] | 824 | target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 825 | brk_page = HOST_PAGE_ALIGN(target_brk); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 826 | } |
| 827 | |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 828 | //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0) |
| 829 | #define DEBUGF_BRK(message, args...) |
| 830 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 831 | /* do_brk() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 832 | abi_long do_brk(abi_ulong new_brk) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 833 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 834 | abi_long mapped_addr; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 835 | int new_alloc_size; |
| 836 | |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 837 | DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 838 | |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 839 | if (!new_brk) { |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 840 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 841 | return target_brk; |
| 842 | } |
| 843 | if (new_brk < target_original_brk) { |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 844 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n", |
| 845 | target_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 846 | return target_brk; |
| 847 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 848 | |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 849 | /* If the new brk is less than the highest page reserved to the |
| 850 | * target heap allocation, set it and we're almost done... */ |
| 851 | if (new_brk <= brk_page) { |
| 852 | /* Heap contents are initialized to zero, as for anonymous |
| 853 | * mapped pages. */ |
| 854 | if (new_brk > target_brk) { |
| 855 | memset(g2h(target_brk), 0, new_brk - target_brk); |
| 856 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 857 | target_brk = new_brk; |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 858 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 859 | return target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 862 | /* We need to allocate more memory after the brk... Note that |
| 863 | * we don't use MAP_FIXED because that will map over the top of |
| 864 | * any existing mapping (like the one with the host libc or qemu |
| 865 | * itself); instead we treat "mapped but at wrong address" as |
| 866 | * a failure and unmap again. |
| 867 | */ |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 868 | new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 869 | mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 870 | PROT_READ|PROT_WRITE, |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 871 | MAP_ANON|MAP_PRIVATE, 0, 0)); |
| 872 | |
| 873 | if (mapped_addr == brk_page) { |
CĂ©dric VINCENT | 70afc34 | 2011-08-26 10:56:50 +0200 | [diff] [blame] | 874 | /* Heap contents are initialized to zero, as for anonymous |
| 875 | * mapped pages. Technically the new pages are already |
| 876 | * initialized to zero since they *are* anonymous mapped |
| 877 | * pages, however we have to take care with the contents that |
| 878 | * come from the remaining part of the previous page: it may |
| 879 | * contains garbage data due to a previous heap usage (grown |
| 880 | * then shrunken). */ |
| 881 | memset(g2h(target_brk), 0, brk_page - target_brk); |
| 882 | |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 883 | target_brk = new_brk; |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 884 | brk_page = HOST_PAGE_ALIGN(target_brk); |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 885 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n", |
| 886 | target_brk); |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 887 | return target_brk; |
| 888 | } else if (mapped_addr != -1) { |
| 889 | /* Mapped but at wrong address, meaning there wasn't actually |
| 890 | * enough space for this brk. |
| 891 | */ |
| 892 | target_munmap(mapped_addr, new_alloc_size); |
| 893 | mapped_addr = -1; |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 894 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk); |
vincent | 4d1de87 | 2011-06-14 21:56:33 +0000 | [diff] [blame] | 895 | } |
| 896 | else { |
Paul Brook | 3a0c6c4 | 2012-02-09 19:04:27 +0000 | [diff] [blame] | 897 | DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk); |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 898 | } |
balrog | 7ab240a | 2008-04-26 12:17:34 +0000 | [diff] [blame] | 899 | |
Richard Henderson | 7dd46c0 | 2010-05-03 10:07:49 -0700 | [diff] [blame] | 900 | #if defined(TARGET_ALPHA) |
| 901 | /* We (partially) emulate OSF/1 on Alpha, which requires we |
| 902 | return a proper errno, not an unchanged brk value. */ |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 903 | return -TARGET_ENOMEM; |
Richard Henderson | 7dd46c0 | 2010-05-03 10:07:49 -0700 | [diff] [blame] | 904 | #endif |
Peter Maydell | 00faf08 | 2011-04-18 16:34:24 +0100 | [diff] [blame] | 905 | /* For everything else, return the previous break. */ |
balrog | 7ab240a | 2008-04-26 12:17:34 +0000 | [diff] [blame] | 906 | return target_brk; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 907 | } |
| 908 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 909 | static inline abi_long copy_from_user_fdset(fd_set *fds, |
| 910 | abi_ulong target_fds_addr, |
| 911 | int n) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 912 | { |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 913 | int i, nw, j, k; |
| 914 | abi_ulong b, *target_fds; |
| 915 | |
Laurent Vivier | b1b2db2 | 2016-05-31 18:36:02 +0200 | [diff] [blame] | 916 | nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 917 | if (!(target_fds = lock_user(VERIFY_READ, |
| 918 | target_fds_addr, |
| 919 | sizeof(abi_ulong) * nw, |
| 920 | 1))) |
| 921 | return -TARGET_EFAULT; |
| 922 | |
| 923 | FD_ZERO(fds); |
| 924 | k = 0; |
| 925 | for (i = 0; i < nw; i++) { |
| 926 | /* grab the abi_ulong */ |
| 927 | __get_user(b, &target_fds[i]); |
| 928 | for (j = 0; j < TARGET_ABI_BITS; j++) { |
| 929 | /* check the bit inside the abi_ulong */ |
| 930 | if ((b >> j) & 1) |
| 931 | FD_SET(k, fds); |
| 932 | k++; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 933 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 934 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 935 | |
| 936 | unlock_user(target_fds, target_fds_addr, 0); |
| 937 | |
| 938 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 941 | static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr, |
| 942 | abi_ulong target_fds_addr, |
| 943 | int n) |
| 944 | { |
| 945 | if (target_fds_addr) { |
| 946 | if (copy_from_user_fdset(fds, target_fds_addr, n)) |
| 947 | return -TARGET_EFAULT; |
| 948 | *fds_ptr = fds; |
| 949 | } else { |
| 950 | *fds_ptr = NULL; |
| 951 | } |
| 952 | return 0; |
| 953 | } |
| 954 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 955 | static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr, |
| 956 | const fd_set *fds, |
| 957 | int n) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 958 | { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 959 | int i, nw, j, k; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 960 | abi_long v; |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 961 | abi_ulong *target_fds; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 962 | |
Laurent Vivier | b1b2db2 | 2016-05-31 18:36:02 +0200 | [diff] [blame] | 963 | nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 964 | if (!(target_fds = lock_user(VERIFY_WRITE, |
| 965 | target_fds_addr, |
| 966 | sizeof(abi_ulong) * nw, |
| 967 | 0))) |
| 968 | return -TARGET_EFAULT; |
| 969 | |
| 970 | k = 0; |
| 971 | for (i = 0; i < nw; i++) { |
| 972 | v = 0; |
| 973 | for (j = 0; j < TARGET_ABI_BITS; j++) { |
Andreas Schwab | 9ab709b | 2013-04-09 01:02:34 +0000 | [diff] [blame] | 974 | v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j); |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 975 | k++; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 976 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 977 | __put_user(v, &target_fds[i]); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 978 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 979 | |
| 980 | unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw); |
| 981 | |
| 982 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 983 | } |
| 984 | |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 985 | #if defined(__alpha__) |
| 986 | #define HOST_HZ 1024 |
| 987 | #else |
| 988 | #define HOST_HZ 100 |
| 989 | #endif |
| 990 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 991 | static inline abi_long host_to_target_clock_t(long ticks) |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 992 | { |
| 993 | #if HOST_HZ == TARGET_HZ |
| 994 | return ticks; |
| 995 | #else |
| 996 | return ((int64_t)ticks * TARGET_HZ) / HOST_HZ; |
| 997 | #endif |
| 998 | } |
| 999 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1000 | static inline abi_long host_to_target_rusage(abi_ulong target_addr, |
| 1001 | const struct rusage *rusage) |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 1002 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1003 | struct target_rusage *target_rusage; |
| 1004 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1005 | if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) |
| 1006 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1007 | target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec); |
| 1008 | target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec); |
| 1009 | target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec); |
| 1010 | target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec); |
| 1011 | target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss); |
| 1012 | target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss); |
| 1013 | target_rusage->ru_idrss = tswapal(rusage->ru_idrss); |
| 1014 | target_rusage->ru_isrss = tswapal(rusage->ru_isrss); |
| 1015 | target_rusage->ru_minflt = tswapal(rusage->ru_minflt); |
| 1016 | target_rusage->ru_majflt = tswapal(rusage->ru_majflt); |
| 1017 | target_rusage->ru_nswap = tswapal(rusage->ru_nswap); |
| 1018 | target_rusage->ru_inblock = tswapal(rusage->ru_inblock); |
| 1019 | target_rusage->ru_oublock = tswapal(rusage->ru_oublock); |
| 1020 | target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd); |
| 1021 | target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv); |
| 1022 | target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals); |
| 1023 | target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw); |
| 1024 | target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1025 | unlock_user_struct(target_rusage, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1026 | |
| 1027 | return 0; |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1030 | 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] | 1031 | { |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1032 | abi_ulong target_rlim_swap; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1033 | rlim_t result; |
| 1034 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1035 | target_rlim_swap = tswapal(target_rlim); |
| 1036 | if (target_rlim_swap == TARGET_RLIM_INFINITY) |
| 1037 | return RLIM_INFINITY; |
| 1038 | |
| 1039 | result = target_rlim_swap; |
| 1040 | if (target_rlim_swap != (rlim_t)result) |
| 1041 | return RLIM_INFINITY; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1042 | |
| 1043 | return result; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1044 | } |
| 1045 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1046 | 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] | 1047 | { |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1048 | abi_ulong target_rlim_swap; |
| 1049 | abi_ulong result; |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1050 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1051 | if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim) |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1052 | target_rlim_swap = TARGET_RLIM_INFINITY; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1053 | else |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1054 | target_rlim_swap = rlim; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1055 | result = tswapal(target_rlim_swap); |
Wesley W. Terpstra | 95b33b2 | 2011-07-12 14:38:22 +0300 | [diff] [blame] | 1056 | |
| 1057 | return result; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 1058 | } |
| 1059 | |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 1060 | static inline int target_to_host_resource(int code) |
| 1061 | { |
| 1062 | switch (code) { |
| 1063 | case TARGET_RLIMIT_AS: |
| 1064 | return RLIMIT_AS; |
| 1065 | case TARGET_RLIMIT_CORE: |
| 1066 | return RLIMIT_CORE; |
| 1067 | case TARGET_RLIMIT_CPU: |
| 1068 | return RLIMIT_CPU; |
| 1069 | case TARGET_RLIMIT_DATA: |
| 1070 | return RLIMIT_DATA; |
| 1071 | case TARGET_RLIMIT_FSIZE: |
| 1072 | return RLIMIT_FSIZE; |
| 1073 | case TARGET_RLIMIT_LOCKS: |
| 1074 | return RLIMIT_LOCKS; |
| 1075 | case TARGET_RLIMIT_MEMLOCK: |
| 1076 | return RLIMIT_MEMLOCK; |
| 1077 | case TARGET_RLIMIT_MSGQUEUE: |
| 1078 | return RLIMIT_MSGQUEUE; |
| 1079 | case TARGET_RLIMIT_NICE: |
| 1080 | return RLIMIT_NICE; |
| 1081 | case TARGET_RLIMIT_NOFILE: |
| 1082 | return RLIMIT_NOFILE; |
| 1083 | case TARGET_RLIMIT_NPROC: |
| 1084 | return RLIMIT_NPROC; |
| 1085 | case TARGET_RLIMIT_RSS: |
| 1086 | return RLIMIT_RSS; |
| 1087 | case TARGET_RLIMIT_RTPRIO: |
| 1088 | return RLIMIT_RTPRIO; |
| 1089 | case TARGET_RLIMIT_SIGPENDING: |
| 1090 | return RLIMIT_SIGPENDING; |
| 1091 | case TARGET_RLIMIT_STACK: |
| 1092 | return RLIMIT_STACK; |
| 1093 | default: |
| 1094 | return code; |
| 1095 | } |
| 1096 | } |
| 1097 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1098 | static inline abi_long copy_from_user_timeval(struct timeval *tv, |
| 1099 | abi_ulong target_tv_addr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1100 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1101 | struct target_timeval *target_tv; |
| 1102 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1103 | if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1104 | return -TARGET_EFAULT; |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1105 | |
| 1106 | __get_user(tv->tv_sec, &target_tv->tv_sec); |
| 1107 | __get_user(tv->tv_usec, &target_tv->tv_usec); |
| 1108 | |
| 1109 | unlock_user_struct(target_tv, target_tv_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1110 | |
| 1111 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1114 | static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr, |
| 1115 | const struct timeval *tv) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1116 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1117 | struct target_timeval *target_tv; |
| 1118 | |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1119 | if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1120 | return -TARGET_EFAULT; |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1121 | |
| 1122 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1123 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
| 1124 | |
| 1125 | unlock_user_struct(target_tv, target_tv_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1126 | |
| 1127 | return 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 1130 | static inline abi_long copy_from_user_timezone(struct timezone *tz, |
| 1131 | abi_ulong target_tz_addr) |
| 1132 | { |
| 1133 | struct target_timezone *target_tz; |
| 1134 | |
| 1135 | if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) { |
| 1136 | return -TARGET_EFAULT; |
| 1137 | } |
| 1138 | |
| 1139 | __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); |
| 1140 | __get_user(tz->tz_dsttime, &target_tz->tz_dsttime); |
| 1141 | |
| 1142 | unlock_user_struct(target_tz, target_tz_addr, 0); |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 1147 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
| 1148 | #include <mqueue.h> |
| 1149 | |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 1150 | static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr, |
| 1151 | abi_ulong target_mq_attr_addr) |
| 1152 | { |
| 1153 | struct target_mq_attr *target_mq_attr; |
| 1154 | |
| 1155 | if (!lock_user_struct(VERIFY_READ, target_mq_attr, |
| 1156 | target_mq_attr_addr, 1)) |
| 1157 | return -TARGET_EFAULT; |
| 1158 | |
| 1159 | __get_user(attr->mq_flags, &target_mq_attr->mq_flags); |
| 1160 | __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); |
| 1161 | __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); |
| 1162 | __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); |
| 1163 | |
| 1164 | unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0); |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr, |
| 1170 | const struct mq_attr *attr) |
| 1171 | { |
| 1172 | struct target_mq_attr *target_mq_attr; |
| 1173 | |
| 1174 | if (!lock_user_struct(VERIFY_WRITE, target_mq_attr, |
| 1175 | target_mq_attr_addr, 0)) |
| 1176 | return -TARGET_EFAULT; |
| 1177 | |
| 1178 | __put_user(attr->mq_flags, &target_mq_attr->mq_flags); |
| 1179 | __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); |
| 1180 | __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); |
| 1181 | __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); |
| 1182 | |
| 1183 | unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 1187 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1188 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1189 | #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 1190 | /* do_select() must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1191 | static abi_long do_select(int n, |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1192 | abi_ulong rfd_addr, abi_ulong wfd_addr, |
| 1193 | abi_ulong efd_addr, abi_ulong target_tv_addr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1194 | { |
| 1195 | fd_set rfds, wfds, efds; |
| 1196 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1197 | struct timeval tv; |
| 1198 | struct timespec ts, *ts_ptr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 1199 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1200 | |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1201 | ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); |
| 1202 | if (ret) { |
| 1203 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1204 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1205 | ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); |
| 1206 | if (ret) { |
| 1207 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1208 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1209 | ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); |
| 1210 | if (ret) { |
| 1211 | return ret; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1212 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1213 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1214 | if (target_tv_addr) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 1215 | if (copy_from_user_timeval(&tv, target_tv_addr)) |
| 1216 | return -TARGET_EFAULT; |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1217 | ts.tv_sec = tv.tv_sec; |
| 1218 | ts.tv_nsec = tv.tv_usec * 1000; |
| 1219 | ts_ptr = &ts; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1220 | } else { |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1221 | ts_ptr = NULL; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1222 | } |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1223 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1224 | ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, |
| 1225 | ts_ptr, NULL)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1226 | |
ths | 26edcf4 | 2007-12-09 02:25:24 +0000 | [diff] [blame] | 1227 | if (!is_error(ret)) { |
| 1228 | if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) |
| 1229 | return -TARGET_EFAULT; |
| 1230 | if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) |
| 1231 | return -TARGET_EFAULT; |
| 1232 | if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) |
| 1233 | return -TARGET_EFAULT; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1234 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 1235 | if (target_tv_addr) { |
| 1236 | tv.tv_sec = ts.tv_sec; |
| 1237 | tv.tv_usec = ts.tv_nsec / 1000; |
| 1238 | if (copy_to_user_timeval(target_tv_addr, &tv)) { |
| 1239 | return -TARGET_EFAULT; |
| 1240 | } |
| 1241 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1242 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1243 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1244 | return ret; |
| 1245 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 1246 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1247 | |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1248 | static abi_long do_pipe2(int host_pipe[], int flags) |
| 1249 | { |
| 1250 | #ifdef CONFIG_PIPE2 |
| 1251 | return pipe2(host_pipe, flags); |
| 1252 | #else |
| 1253 | return -ENOSYS; |
| 1254 | #endif |
| 1255 | } |
| 1256 | |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1257 | static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, |
| 1258 | int flags, int is_pipe2) |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1259 | { |
| 1260 | int host_pipe[2]; |
| 1261 | abi_long ret; |
| 1262 | ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe); |
| 1263 | |
| 1264 | if (is_error(ret)) |
| 1265 | return get_errno(ret); |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1266 | |
| 1267 | /* Several targets have special calling conventions for the original |
| 1268 | pipe syscall, but didn't replicate this into the pipe2 syscall. */ |
| 1269 | if (!is_pipe2) { |
| 1270 | #if defined(TARGET_ALPHA) |
| 1271 | ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1]; |
| 1272 | return host_pipe[0]; |
| 1273 | #elif defined(TARGET_MIPS) |
| 1274 | ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1]; |
| 1275 | return host_pipe[0]; |
| 1276 | #elif defined(TARGET_SH4) |
takasi-y@ops.dti.ne.jp | 597c021 | 2010-02-18 00:35:03 +0900 | [diff] [blame] | 1277 | ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1]; |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1278 | return host_pipe[0]; |
Peter Maydell | 82f05b6 | 2013-07-06 17:39:48 +0100 | [diff] [blame] | 1279 | #elif defined(TARGET_SPARC) |
| 1280 | ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1]; |
| 1281 | return host_pipe[0]; |
takasi-y@ops.dti.ne.jp | 597c021 | 2010-02-18 00:35:03 +0900 | [diff] [blame] | 1282 | #endif |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1285 | if (put_user_s32(host_pipe[0], pipedes) |
| 1286 | || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0]))) |
| 1287 | return -TARGET_EFAULT; |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 1288 | return get_errno(ret); |
| 1289 | } |
| 1290 | |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 1291 | static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, |
| 1292 | abi_ulong target_addr, |
| 1293 | socklen_t len) |
| 1294 | { |
| 1295 | struct target_ip_mreqn *target_smreqn; |
| 1296 | |
| 1297 | target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1); |
| 1298 | if (!target_smreqn) |
| 1299 | return -TARGET_EFAULT; |
| 1300 | mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr; |
| 1301 | mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr; |
| 1302 | if (len == sizeof(struct target_ip_mreqn)) |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1303 | mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex); |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 1304 | unlock_user(target_smreqn, target_addr, 0); |
| 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 1309 | static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1310 | abi_ulong target_addr, |
| 1311 | socklen_t len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1312 | { |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1313 | const socklen_t unix_maxlen = sizeof (struct sockaddr_un); |
| 1314 | sa_family_t sa_family; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1315 | struct target_sockaddr *target_saddr; |
| 1316 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 1317 | if (fd_trans_target_to_host_addr(fd)) { |
| 1318 | return fd_trans_target_to_host_addr(fd)(addr, target_addr, len); |
| 1319 | } |
| 1320 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1321 | target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); |
| 1322 | if (!target_saddr) |
| 1323 | return -TARGET_EFAULT; |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1324 | |
| 1325 | sa_family = tswap16(target_saddr->sa_family); |
| 1326 | |
| 1327 | /* Oops. The caller might send a incomplete sun_path; sun_path |
| 1328 | * must be terminated by \0 (see the manual page), but |
| 1329 | * unfortunately it is quite common to specify sockaddr_un |
| 1330 | * length as "strlen(x->sun_path)" while it should be |
| 1331 | * "strlen(...) + 1". We'll fix that here if needed. |
| 1332 | * Linux kernel has a similar feature. |
| 1333 | */ |
| 1334 | |
| 1335 | if (sa_family == AF_UNIX) { |
| 1336 | if (len < unix_maxlen && len > 0) { |
| 1337 | char *cp = (char*)target_saddr; |
| 1338 | |
| 1339 | if ( cp[len-1] && !cp[len] ) |
| 1340 | len++; |
| 1341 | } |
| 1342 | if (len > unix_maxlen) |
| 1343 | len = unix_maxlen; |
| 1344 | } |
| 1345 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1346 | memcpy(addr, target_saddr, len); |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 1347 | addr->sa_family = sa_family; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1348 | if (sa_family == AF_NETLINK) { |
| 1349 | struct sockaddr_nl *nladdr; |
| 1350 | |
| 1351 | nladdr = (struct sockaddr_nl *)addr; |
| 1352 | nladdr->nl_pid = tswap32(nladdr->nl_pid); |
| 1353 | nladdr->nl_groups = tswap32(nladdr->nl_groups); |
| 1354 | } else if (sa_family == AF_PACKET) { |
Joakim Tjernlund | 33a29b5 | 2014-07-12 15:47:07 +0200 | [diff] [blame] | 1355 | struct target_sockaddr_ll *lladdr; |
| 1356 | |
| 1357 | lladdr = (struct target_sockaddr_ll *)addr; |
| 1358 | lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex); |
| 1359 | lladdr->sll_hatype = tswap16(lladdr->sll_hatype); |
| 1360 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1361 | unlock_user(target_saddr, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1362 | |
| 1363 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1366 | static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, |
| 1367 | struct sockaddr *addr, |
| 1368 | socklen_t len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1369 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1370 | struct target_sockaddr *target_saddr; |
| 1371 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1372 | target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0); |
| 1373 | if (!target_saddr) |
| 1374 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1375 | memcpy(target_saddr, addr, len); |
| 1376 | target_saddr->sa_family = tswap16(addr->sa_family); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1377 | if (addr->sa_family == AF_NETLINK) { |
| 1378 | struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr; |
| 1379 | target_nl->nl_pid = tswap32(target_nl->nl_pid); |
| 1380 | target_nl->nl_groups = tswap32(target_nl->nl_groups); |
| 1381 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1382 | unlock_user(target_saddr, target_addr, len); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 1383 | |
| 1384 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1387 | static inline abi_long target_to_host_cmsg(struct msghdr *msgh, |
| 1388 | struct target_msghdr *target_msgh) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1389 | { |
| 1390 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1391 | abi_long msg_controllen; |
| 1392 | abi_ulong target_cmsg_addr; |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1393 | struct target_cmsghdr *target_cmsg, *target_cmsg_start; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1394 | socklen_t space = 0; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1395 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1396 | msg_controllen = tswapal(target_msgh->msg_controllen); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1397 | if (msg_controllen < sizeof (struct target_cmsghdr)) |
| 1398 | goto the_end; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1399 | target_cmsg_addr = tswapal(target_msgh->msg_control); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1400 | 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] | 1401 | target_cmsg_start = target_cmsg; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1402 | if (!target_cmsg) |
| 1403 | return -TARGET_EFAULT; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1404 | |
| 1405 | while (cmsg && target_cmsg) { |
| 1406 | void *data = CMSG_DATA(cmsg); |
| 1407 | void *target_data = TARGET_CMSG_DATA(target_cmsg); |
| 1408 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1409 | int len = tswapal(target_cmsg->cmsg_len) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1410 | - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr)); |
| 1411 | |
| 1412 | space += CMSG_SPACE(len); |
| 1413 | if (space > msgh->msg_controllen) { |
| 1414 | space -= CMSG_SPACE(len); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1415 | /* This is a QEMU bug, since we allocated the payload |
| 1416 | * area ourselves (unlike overflow in host-to-target |
| 1417 | * conversion, which is just the guest giving us a buffer |
| 1418 | * that's too small). It can't happen for the payload types |
| 1419 | * we currently support; if it becomes an issue in future |
| 1420 | * we would need to improve our allocation strategy to |
| 1421 | * something more intelligent than "twice the size of the |
| 1422 | * target buffer we're reading from". |
| 1423 | */ |
bellard | 31febb7 | 2005-12-18 20:03:27 +0000 | [diff] [blame] | 1424 | gemu_log("Host cmsg overflow\n"); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | } |
| 1427 | |
Petar Jovanovic | dbf4f79 | 2013-09-13 19:27:29 +0200 | [diff] [blame] | 1428 | if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) { |
| 1429 | cmsg->cmsg_level = SOL_SOCKET; |
| 1430 | } else { |
| 1431 | cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level); |
| 1432 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1433 | cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type); |
| 1434 | cmsg->cmsg_len = CMSG_LEN(len); |
| 1435 | |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1436 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1437 | int *fd = (int *)data; |
| 1438 | int *target_fd = (int *)target_data; |
| 1439 | int i, numfds = len / sizeof(int); |
| 1440 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1441 | for (i = 0; i < numfds; i++) { |
| 1442 | __get_user(fd[i], target_fd + i); |
| 1443 | } |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1444 | } else if (cmsg->cmsg_level == SOL_SOCKET |
| 1445 | && cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 1446 | struct ucred *cred = (struct ucred *)data; |
| 1447 | struct target_ucred *target_cred = |
| 1448 | (struct target_ucred *)target_data; |
| 1449 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1450 | __get_user(cred->pid, &target_cred->pid); |
| 1451 | __get_user(cred->uid, &target_cred->uid); |
| 1452 | __get_user(cred->gid, &target_cred->gid); |
Alex Suykov | 30b8b68 | 2014-12-23 07:52:58 +0200 | [diff] [blame] | 1453 | } else { |
| 1454 | gemu_log("Unsupported ancillary data: %d/%d\n", |
| 1455 | cmsg->cmsg_level, cmsg->cmsg_type); |
| 1456 | memcpy(data, target_data, len); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | cmsg = CMSG_NXTHDR(msgh, cmsg); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1460 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, |
| 1461 | target_cmsg_start); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1462 | } |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1463 | unlock_user(target_cmsg, target_cmsg_addr, 0); |
| 1464 | the_end: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1465 | msgh->msg_controllen = space; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1466 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1469 | static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh, |
| 1470 | struct msghdr *msgh) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1471 | { |
| 1472 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1473 | abi_long msg_controllen; |
| 1474 | abi_ulong target_cmsg_addr; |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1475 | struct target_cmsghdr *target_cmsg, *target_cmsg_start; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1476 | socklen_t space = 0; |
| 1477 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1478 | msg_controllen = tswapal(target_msgh->msg_controllen); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1479 | if (msg_controllen < sizeof (struct target_cmsghdr)) |
| 1480 | goto the_end; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1481 | target_cmsg_addr = tswapal(target_msgh->msg_control); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1482 | 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] | 1483 | target_cmsg_start = target_cmsg; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1484 | if (!target_cmsg) |
| 1485 | return -TARGET_EFAULT; |
| 1486 | |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1487 | while (cmsg && target_cmsg) { |
| 1488 | void *data = CMSG_DATA(cmsg); |
| 1489 | void *target_data = TARGET_CMSG_DATA(target_cmsg); |
| 1490 | |
| 1491 | int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr)); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1492 | int tgt_len, tgt_space; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1493 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1494 | /* We never copy a half-header but may copy half-data; |
| 1495 | * this is Linux's behaviour in put_cmsg(). Note that |
| 1496 | * truncation here is a guest problem (which we report |
| 1497 | * to the guest via the CTRUNC bit), unlike truncation |
| 1498 | * in target_to_host_cmsg, which is a QEMU bug. |
| 1499 | */ |
| 1500 | if (msg_controllen < sizeof(struct cmsghdr)) { |
| 1501 | target_msgh->msg_flags |= tswap32(MSG_CTRUNC); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | } |
| 1504 | |
Petar Jovanovic | dbf4f79 | 2013-09-13 19:27:29 +0200 | [diff] [blame] | 1505 | if (cmsg->cmsg_level == SOL_SOCKET) { |
| 1506 | target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET); |
| 1507 | } else { |
| 1508 | target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level); |
| 1509 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1510 | target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1511 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1512 | tgt_len = TARGET_CMSG_LEN(len); |
| 1513 | |
| 1514 | /* Payload types which need a different size of payload on |
| 1515 | * the target must adjust tgt_len here. |
| 1516 | */ |
| 1517 | switch (cmsg->cmsg_level) { |
| 1518 | case SOL_SOCKET: |
| 1519 | switch (cmsg->cmsg_type) { |
| 1520 | case SO_TIMESTAMP: |
| 1521 | tgt_len = sizeof(struct target_timeval); |
| 1522 | break; |
| 1523 | default: |
| 1524 | break; |
| 1525 | } |
| 1526 | default: |
| 1527 | break; |
| 1528 | } |
| 1529 | |
| 1530 | if (msg_controllen < tgt_len) { |
| 1531 | target_msgh->msg_flags |= tswap32(MSG_CTRUNC); |
| 1532 | tgt_len = msg_controllen; |
| 1533 | } |
| 1534 | |
| 1535 | /* We must now copy-and-convert len bytes of payload |
| 1536 | * into tgt_len bytes of destination space. Bear in mind |
| 1537 | * that in both source and destination we may be dealing |
| 1538 | * with a truncated value! |
| 1539 | */ |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1540 | switch (cmsg->cmsg_level) { |
| 1541 | case SOL_SOCKET: |
| 1542 | switch (cmsg->cmsg_type) { |
| 1543 | case SCM_RIGHTS: |
| 1544 | { |
| 1545 | int *fd = (int *)data; |
| 1546 | int *target_fd = (int *)target_data; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1547 | int i, numfds = tgt_len / sizeof(int); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1548 | |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1549 | for (i = 0; i < numfds; i++) { |
| 1550 | __put_user(fd[i], target_fd + i); |
| 1551 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1552 | break; |
| 1553 | } |
| 1554 | case SO_TIMESTAMP: |
| 1555 | { |
| 1556 | struct timeval *tv = (struct timeval *)data; |
| 1557 | struct target_timeval *target_tv = |
| 1558 | (struct target_timeval *)target_data; |
Jing Huang | aebf5bc | 2012-07-24 14:01:42 +0000 | [diff] [blame] | 1559 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1560 | if (len != sizeof(struct timeval) || |
| 1561 | tgt_len != sizeof(struct target_timeval)) { |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1562 | goto unimplemented; |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1563 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1564 | |
| 1565 | /* copy struct timeval to target */ |
Peter Maydell | 876e23c | 2015-05-26 19:46:32 +0100 | [diff] [blame] | 1566 | __put_user(tv->tv_sec, &target_tv->tv_sec); |
| 1567 | __put_user(tv->tv_usec, &target_tv->tv_usec); |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1568 | break; |
| 1569 | } |
Huw Davies | 4bc2975 | 2014-04-17 14:02:48 +0100 | [diff] [blame] | 1570 | case SCM_CREDENTIALS: |
| 1571 | { |
| 1572 | struct ucred *cred = (struct ucred *)data; |
| 1573 | struct target_ucred *target_cred = |
| 1574 | (struct target_ucred *)target_data; |
| 1575 | |
| 1576 | __put_user(cred->pid, &target_cred->pid); |
| 1577 | __put_user(cred->uid, &target_cred->uid); |
| 1578 | __put_user(cred->gid, &target_cred->gid); |
| 1579 | break; |
| 1580 | } |
Huw Davies | 52b6549 | 2014-04-17 14:02:47 +0100 | [diff] [blame] | 1581 | default: |
| 1582 | goto unimplemented; |
| 1583 | } |
| 1584 | break; |
| 1585 | |
| 1586 | default: |
| 1587 | unimplemented: |
Jing Huang | aebf5bc | 2012-07-24 14:01:42 +0000 | [diff] [blame] | 1588 | gemu_log("Unsupported ancillary data: %d/%d\n", |
| 1589 | cmsg->cmsg_level, cmsg->cmsg_type); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1590 | memcpy(target_data, data, MIN(len, tgt_len)); |
| 1591 | if (tgt_len > len) { |
| 1592 | memset(target_data + len, 0, tgt_len - len); |
| 1593 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1596 | target_cmsg->cmsg_len = tswapal(tgt_len); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1597 | tgt_space = TARGET_CMSG_SPACE(len); |
Peter Maydell | c2aeb25 | 2015-05-26 19:46:31 +0100 | [diff] [blame] | 1598 | if (msg_controllen < tgt_space) { |
| 1599 | tgt_space = msg_controllen; |
| 1600 | } |
| 1601 | msg_controllen -= tgt_space; |
| 1602 | space += tgt_space; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1603 | cmsg = CMSG_NXTHDR(msgh, cmsg); |
Jonathan Neuschäfer | ee10458 | 2015-09-03 07:27:26 +0200 | [diff] [blame] | 1604 | target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, |
| 1605 | target_cmsg_start); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1606 | } |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1607 | unlock_user(target_cmsg, target_cmsg_addr, space); |
| 1608 | the_end: |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 1609 | target_msgh->msg_controllen = tswapal(space); |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 1610 | return 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1613 | static void tswap_nlmsghdr(struct nlmsghdr *nlh) |
| 1614 | { |
| 1615 | nlh->nlmsg_len = tswap32(nlh->nlmsg_len); |
| 1616 | nlh->nlmsg_type = tswap16(nlh->nlmsg_type); |
| 1617 | nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags); |
| 1618 | nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq); |
| 1619 | nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid); |
| 1620 | } |
| 1621 | |
| 1622 | static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh, |
| 1623 | size_t len, |
| 1624 | abi_long (*host_to_target_nlmsg) |
| 1625 | (struct nlmsghdr *)) |
| 1626 | { |
| 1627 | uint32_t nlmsg_len; |
| 1628 | abi_long ret; |
| 1629 | |
| 1630 | while (len > sizeof(struct nlmsghdr)) { |
| 1631 | |
| 1632 | nlmsg_len = nlh->nlmsg_len; |
| 1633 | if (nlmsg_len < sizeof(struct nlmsghdr) || |
| 1634 | nlmsg_len > len) { |
| 1635 | break; |
| 1636 | } |
| 1637 | |
| 1638 | switch (nlh->nlmsg_type) { |
| 1639 | case NLMSG_DONE: |
| 1640 | tswap_nlmsghdr(nlh); |
| 1641 | return 0; |
| 1642 | case NLMSG_NOOP: |
| 1643 | break; |
| 1644 | case NLMSG_ERROR: |
| 1645 | { |
| 1646 | struct nlmsgerr *e = NLMSG_DATA(nlh); |
| 1647 | e->error = tswap32(e->error); |
| 1648 | tswap_nlmsghdr(&e->msg); |
| 1649 | tswap_nlmsghdr(nlh); |
| 1650 | return 0; |
| 1651 | } |
| 1652 | default: |
| 1653 | ret = host_to_target_nlmsg(nlh); |
| 1654 | if (ret < 0) { |
| 1655 | tswap_nlmsghdr(nlh); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | break; |
| 1659 | } |
| 1660 | tswap_nlmsghdr(nlh); |
| 1661 | len -= NLMSG_ALIGN(nlmsg_len); |
| 1662 | nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len)); |
| 1663 | } |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh, |
| 1668 | size_t len, |
| 1669 | abi_long (*target_to_host_nlmsg) |
| 1670 | (struct nlmsghdr *)) |
| 1671 | { |
| 1672 | int ret; |
| 1673 | |
| 1674 | while (len > sizeof(struct nlmsghdr)) { |
| 1675 | if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) || |
| 1676 | tswap32(nlh->nlmsg_len) > len) { |
| 1677 | break; |
| 1678 | } |
| 1679 | tswap_nlmsghdr(nlh); |
| 1680 | switch (nlh->nlmsg_type) { |
| 1681 | case NLMSG_DONE: |
| 1682 | return 0; |
| 1683 | case NLMSG_NOOP: |
| 1684 | break; |
| 1685 | case NLMSG_ERROR: |
| 1686 | { |
| 1687 | struct nlmsgerr *e = NLMSG_DATA(nlh); |
| 1688 | e->error = tswap32(e->error); |
| 1689 | tswap_nlmsghdr(&e->msg); |
| 1690 | } |
| 1691 | default: |
| 1692 | ret = target_to_host_nlmsg(nlh); |
| 1693 | if (ret < 0) { |
| 1694 | return ret; |
| 1695 | } |
| 1696 | } |
| 1697 | len -= NLMSG_ALIGN(nlh->nlmsg_len); |
| 1698 | nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len)); |
| 1699 | } |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 1703 | #ifdef CONFIG_RTNETLINK |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 1704 | static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr, |
| 1705 | size_t len, |
| 1706 | abi_long (*host_to_target_rtattr) |
| 1707 | (struct rtattr *)) |
| 1708 | { |
| 1709 | unsigned short rta_len; |
| 1710 | abi_long ret; |
| 1711 | |
| 1712 | while (len > sizeof(struct rtattr)) { |
| 1713 | rta_len = rtattr->rta_len; |
| 1714 | if (rta_len < sizeof(struct rtattr) || |
| 1715 | rta_len > len) { |
| 1716 | break; |
| 1717 | } |
| 1718 | ret = host_to_target_rtattr(rtattr); |
| 1719 | rtattr->rta_len = tswap16(rtattr->rta_len); |
| 1720 | rtattr->rta_type = tswap16(rtattr->rta_type); |
| 1721 | if (ret < 0) { |
| 1722 | return ret; |
| 1723 | } |
| 1724 | len -= RTA_ALIGN(rta_len); |
| 1725 | rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len)); |
| 1726 | } |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr) |
| 1731 | { |
| 1732 | uint32_t *u32; |
| 1733 | struct rtnl_link_stats *st; |
| 1734 | struct rtnl_link_stats64 *st64; |
| 1735 | struct rtnl_link_ifmap *map; |
| 1736 | |
| 1737 | switch (rtattr->rta_type) { |
| 1738 | /* binary stream */ |
| 1739 | case IFLA_ADDRESS: |
| 1740 | case IFLA_BROADCAST: |
| 1741 | /* string */ |
| 1742 | case IFLA_IFNAME: |
| 1743 | case IFLA_QDISC: |
| 1744 | break; |
| 1745 | /* uin8_t */ |
| 1746 | case IFLA_OPERSTATE: |
| 1747 | case IFLA_LINKMODE: |
| 1748 | case IFLA_CARRIER: |
| 1749 | case IFLA_PROTO_DOWN: |
| 1750 | break; |
| 1751 | /* uint32_t */ |
| 1752 | case IFLA_MTU: |
| 1753 | case IFLA_LINK: |
| 1754 | case IFLA_WEIGHT: |
| 1755 | case IFLA_TXQLEN: |
| 1756 | case IFLA_CARRIER_CHANGES: |
| 1757 | case IFLA_NUM_RX_QUEUES: |
| 1758 | case IFLA_NUM_TX_QUEUES: |
| 1759 | case IFLA_PROMISCUITY: |
| 1760 | case IFLA_EXT_MASK: |
| 1761 | case IFLA_LINK_NETNSID: |
| 1762 | case IFLA_GROUP: |
| 1763 | case IFLA_MASTER: |
| 1764 | case IFLA_NUM_VF: |
| 1765 | u32 = RTA_DATA(rtattr); |
| 1766 | *u32 = tswap32(*u32); |
| 1767 | break; |
| 1768 | /* struct rtnl_link_stats */ |
| 1769 | case IFLA_STATS: |
| 1770 | st = RTA_DATA(rtattr); |
| 1771 | st->rx_packets = tswap32(st->rx_packets); |
| 1772 | st->tx_packets = tswap32(st->tx_packets); |
| 1773 | st->rx_bytes = tswap32(st->rx_bytes); |
| 1774 | st->tx_bytes = tswap32(st->tx_bytes); |
| 1775 | st->rx_errors = tswap32(st->rx_errors); |
| 1776 | st->tx_errors = tswap32(st->tx_errors); |
| 1777 | st->rx_dropped = tswap32(st->rx_dropped); |
| 1778 | st->tx_dropped = tswap32(st->tx_dropped); |
| 1779 | st->multicast = tswap32(st->multicast); |
| 1780 | st->collisions = tswap32(st->collisions); |
| 1781 | |
| 1782 | /* detailed rx_errors: */ |
| 1783 | st->rx_length_errors = tswap32(st->rx_length_errors); |
| 1784 | st->rx_over_errors = tswap32(st->rx_over_errors); |
| 1785 | st->rx_crc_errors = tswap32(st->rx_crc_errors); |
| 1786 | st->rx_frame_errors = tswap32(st->rx_frame_errors); |
| 1787 | st->rx_fifo_errors = tswap32(st->rx_fifo_errors); |
| 1788 | st->rx_missed_errors = tswap32(st->rx_missed_errors); |
| 1789 | |
| 1790 | /* detailed tx_errors */ |
| 1791 | st->tx_aborted_errors = tswap32(st->tx_aborted_errors); |
| 1792 | st->tx_carrier_errors = tswap32(st->tx_carrier_errors); |
| 1793 | st->tx_fifo_errors = tswap32(st->tx_fifo_errors); |
| 1794 | st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors); |
| 1795 | st->tx_window_errors = tswap32(st->tx_window_errors); |
| 1796 | |
| 1797 | /* for cslip etc */ |
| 1798 | st->rx_compressed = tswap32(st->rx_compressed); |
| 1799 | st->tx_compressed = tswap32(st->tx_compressed); |
| 1800 | break; |
| 1801 | /* struct rtnl_link_stats64 */ |
| 1802 | case IFLA_STATS64: |
| 1803 | st64 = RTA_DATA(rtattr); |
| 1804 | st64->rx_packets = tswap64(st64->rx_packets); |
| 1805 | st64->tx_packets = tswap64(st64->tx_packets); |
| 1806 | st64->rx_bytes = tswap64(st64->rx_bytes); |
| 1807 | st64->tx_bytes = tswap64(st64->tx_bytes); |
| 1808 | st64->rx_errors = tswap64(st64->rx_errors); |
| 1809 | st64->tx_errors = tswap64(st64->tx_errors); |
| 1810 | st64->rx_dropped = tswap64(st64->rx_dropped); |
| 1811 | st64->tx_dropped = tswap64(st64->tx_dropped); |
| 1812 | st64->multicast = tswap64(st64->multicast); |
| 1813 | st64->collisions = tswap64(st64->collisions); |
| 1814 | |
| 1815 | /* detailed rx_errors: */ |
| 1816 | st64->rx_length_errors = tswap64(st64->rx_length_errors); |
| 1817 | st64->rx_over_errors = tswap64(st64->rx_over_errors); |
| 1818 | st64->rx_crc_errors = tswap64(st64->rx_crc_errors); |
| 1819 | st64->rx_frame_errors = tswap64(st64->rx_frame_errors); |
| 1820 | st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors); |
| 1821 | st64->rx_missed_errors = tswap64(st64->rx_missed_errors); |
| 1822 | |
| 1823 | /* detailed tx_errors */ |
| 1824 | st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors); |
| 1825 | st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors); |
| 1826 | st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors); |
| 1827 | st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors); |
| 1828 | st64->tx_window_errors = tswap64(st64->tx_window_errors); |
| 1829 | |
| 1830 | /* for cslip etc */ |
| 1831 | st64->rx_compressed = tswap64(st64->rx_compressed); |
| 1832 | st64->tx_compressed = tswap64(st64->tx_compressed); |
| 1833 | break; |
| 1834 | /* struct rtnl_link_ifmap */ |
| 1835 | case IFLA_MAP: |
| 1836 | map = RTA_DATA(rtattr); |
| 1837 | map->mem_start = tswap64(map->mem_start); |
| 1838 | map->mem_end = tswap64(map->mem_end); |
| 1839 | map->base_addr = tswap64(map->base_addr); |
| 1840 | map->irq = tswap16(map->irq); |
| 1841 | break; |
| 1842 | /* nested */ |
| 1843 | case IFLA_AF_SPEC: |
| 1844 | case IFLA_LINKINFO: |
| 1845 | /* FIXME: implement nested type */ |
| 1846 | gemu_log("Unimplemented nested type %d\n", rtattr->rta_type); |
| 1847 | break; |
| 1848 | default: |
| 1849 | gemu_log("Unknown host IFLA type: %d\n", rtattr->rta_type); |
| 1850 | break; |
| 1851 | } |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
| 1855 | static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr) |
| 1856 | { |
| 1857 | uint32_t *u32; |
| 1858 | struct ifa_cacheinfo *ci; |
| 1859 | |
| 1860 | switch (rtattr->rta_type) { |
| 1861 | /* binary: depends on family type */ |
| 1862 | case IFA_ADDRESS: |
| 1863 | case IFA_LOCAL: |
| 1864 | break; |
| 1865 | /* string */ |
| 1866 | case IFA_LABEL: |
| 1867 | break; |
| 1868 | /* u32 */ |
| 1869 | case IFA_FLAGS: |
| 1870 | case IFA_BROADCAST: |
| 1871 | u32 = RTA_DATA(rtattr); |
| 1872 | *u32 = tswap32(*u32); |
| 1873 | break; |
| 1874 | /* struct ifa_cacheinfo */ |
| 1875 | case IFA_CACHEINFO: |
| 1876 | ci = RTA_DATA(rtattr); |
| 1877 | ci->ifa_prefered = tswap32(ci->ifa_prefered); |
| 1878 | ci->ifa_valid = tswap32(ci->ifa_valid); |
| 1879 | ci->cstamp = tswap32(ci->cstamp); |
| 1880 | ci->tstamp = tswap32(ci->tstamp); |
| 1881 | break; |
| 1882 | default: |
| 1883 | gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type); |
| 1884 | break; |
| 1885 | } |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
| 1889 | static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr) |
| 1890 | { |
| 1891 | uint32_t *u32; |
| 1892 | switch (rtattr->rta_type) { |
| 1893 | /* binary: depends on family type */ |
| 1894 | case RTA_GATEWAY: |
| 1895 | case RTA_DST: |
| 1896 | case RTA_PREFSRC: |
| 1897 | break; |
| 1898 | /* u32 */ |
| 1899 | case RTA_PRIORITY: |
| 1900 | case RTA_TABLE: |
| 1901 | case RTA_OIF: |
| 1902 | u32 = RTA_DATA(rtattr); |
| 1903 | *u32 = tswap32(*u32); |
| 1904 | break; |
| 1905 | default: |
| 1906 | gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type); |
| 1907 | break; |
| 1908 | } |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
| 1912 | static abi_long host_to_target_link_rtattr(struct rtattr *rtattr, |
| 1913 | uint32_t rtattr_len) |
| 1914 | { |
| 1915 | return host_to_target_for_each_rtattr(rtattr, rtattr_len, |
| 1916 | host_to_target_data_link_rtattr); |
| 1917 | } |
| 1918 | |
| 1919 | static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr, |
| 1920 | uint32_t rtattr_len) |
| 1921 | { |
| 1922 | return host_to_target_for_each_rtattr(rtattr, rtattr_len, |
| 1923 | host_to_target_data_addr_rtattr); |
| 1924 | } |
| 1925 | |
| 1926 | static abi_long host_to_target_route_rtattr(struct rtattr *rtattr, |
| 1927 | uint32_t rtattr_len) |
| 1928 | { |
| 1929 | return host_to_target_for_each_rtattr(rtattr, rtattr_len, |
| 1930 | host_to_target_data_route_rtattr); |
| 1931 | } |
| 1932 | |
| 1933 | static abi_long host_to_target_data_route(struct nlmsghdr *nlh) |
| 1934 | { |
| 1935 | uint32_t nlmsg_len; |
| 1936 | struct ifinfomsg *ifi; |
| 1937 | struct ifaddrmsg *ifa; |
| 1938 | struct rtmsg *rtm; |
| 1939 | |
| 1940 | nlmsg_len = nlh->nlmsg_len; |
| 1941 | switch (nlh->nlmsg_type) { |
| 1942 | case RTM_NEWLINK: |
| 1943 | case RTM_DELLINK: |
| 1944 | case RTM_GETLINK: |
| 1945 | ifi = NLMSG_DATA(nlh); |
| 1946 | ifi->ifi_type = tswap16(ifi->ifi_type); |
| 1947 | ifi->ifi_index = tswap32(ifi->ifi_index); |
| 1948 | ifi->ifi_flags = tswap32(ifi->ifi_flags); |
| 1949 | ifi->ifi_change = tswap32(ifi->ifi_change); |
| 1950 | host_to_target_link_rtattr(IFLA_RTA(ifi), |
| 1951 | nlmsg_len - NLMSG_LENGTH(sizeof(*ifi))); |
| 1952 | break; |
| 1953 | case RTM_NEWADDR: |
| 1954 | case RTM_DELADDR: |
| 1955 | case RTM_GETADDR: |
| 1956 | ifa = NLMSG_DATA(nlh); |
| 1957 | ifa->ifa_index = tswap32(ifa->ifa_index); |
| 1958 | host_to_target_addr_rtattr(IFA_RTA(ifa), |
| 1959 | nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 1960 | break; |
| 1961 | case RTM_NEWROUTE: |
| 1962 | case RTM_DELROUTE: |
| 1963 | case RTM_GETROUTE: |
| 1964 | rtm = NLMSG_DATA(nlh); |
| 1965 | rtm->rtm_flags = tswap32(rtm->rtm_flags); |
| 1966 | host_to_target_route_rtattr(RTM_RTA(rtm), |
| 1967 | nlmsg_len - NLMSG_LENGTH(sizeof(*rtm))); |
| 1968 | break; |
| 1969 | default: |
| 1970 | return -TARGET_EINVAL; |
| 1971 | } |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh, |
| 1976 | size_t len) |
| 1977 | { |
| 1978 | return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route); |
| 1979 | } |
| 1980 | |
| 1981 | static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr, |
| 1982 | size_t len, |
| 1983 | abi_long (*target_to_host_rtattr) |
| 1984 | (struct rtattr *)) |
| 1985 | { |
| 1986 | abi_long ret; |
| 1987 | |
| 1988 | while (len >= sizeof(struct rtattr)) { |
| 1989 | if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) || |
| 1990 | tswap16(rtattr->rta_len) > len) { |
| 1991 | break; |
| 1992 | } |
| 1993 | rtattr->rta_len = tswap16(rtattr->rta_len); |
| 1994 | rtattr->rta_type = tswap16(rtattr->rta_type); |
| 1995 | ret = target_to_host_rtattr(rtattr); |
| 1996 | if (ret < 0) { |
| 1997 | return ret; |
| 1998 | } |
| 1999 | len -= RTA_ALIGN(rtattr->rta_len); |
| 2000 | rtattr = (struct rtattr *)(((char *)rtattr) + |
| 2001 | RTA_ALIGN(rtattr->rta_len)); |
| 2002 | } |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
| 2006 | static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) |
| 2007 | { |
| 2008 | switch (rtattr->rta_type) { |
| 2009 | default: |
| 2010 | gemu_log("Unknown target IFLA type: %d\n", rtattr->rta_type); |
| 2011 | break; |
| 2012 | } |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr) |
| 2017 | { |
| 2018 | switch (rtattr->rta_type) { |
| 2019 | /* binary: depends on family type */ |
| 2020 | case IFA_LOCAL: |
| 2021 | case IFA_ADDRESS: |
| 2022 | break; |
| 2023 | default: |
| 2024 | gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type); |
| 2025 | break; |
| 2026 | } |
| 2027 | return 0; |
| 2028 | } |
| 2029 | |
| 2030 | static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr) |
| 2031 | { |
| 2032 | uint32_t *u32; |
| 2033 | switch (rtattr->rta_type) { |
| 2034 | /* binary: depends on family type */ |
| 2035 | case RTA_DST: |
| 2036 | case RTA_SRC: |
| 2037 | case RTA_GATEWAY: |
| 2038 | break; |
| 2039 | /* u32 */ |
| 2040 | case RTA_OIF: |
| 2041 | u32 = RTA_DATA(rtattr); |
| 2042 | *u32 = tswap32(*u32); |
| 2043 | break; |
| 2044 | default: |
| 2045 | gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type); |
| 2046 | break; |
| 2047 | } |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
| 2051 | static void target_to_host_link_rtattr(struct rtattr *rtattr, |
| 2052 | uint32_t rtattr_len) |
| 2053 | { |
| 2054 | target_to_host_for_each_rtattr(rtattr, rtattr_len, |
| 2055 | target_to_host_data_link_rtattr); |
| 2056 | } |
| 2057 | |
| 2058 | static void target_to_host_addr_rtattr(struct rtattr *rtattr, |
| 2059 | uint32_t rtattr_len) |
| 2060 | { |
| 2061 | target_to_host_for_each_rtattr(rtattr, rtattr_len, |
| 2062 | target_to_host_data_addr_rtattr); |
| 2063 | } |
| 2064 | |
| 2065 | static void target_to_host_route_rtattr(struct rtattr *rtattr, |
| 2066 | uint32_t rtattr_len) |
| 2067 | { |
| 2068 | target_to_host_for_each_rtattr(rtattr, rtattr_len, |
| 2069 | target_to_host_data_route_rtattr); |
| 2070 | } |
| 2071 | |
| 2072 | static abi_long target_to_host_data_route(struct nlmsghdr *nlh) |
| 2073 | { |
| 2074 | struct ifinfomsg *ifi; |
| 2075 | struct ifaddrmsg *ifa; |
| 2076 | struct rtmsg *rtm; |
| 2077 | |
| 2078 | switch (nlh->nlmsg_type) { |
| 2079 | case RTM_GETLINK: |
| 2080 | break; |
| 2081 | case RTM_NEWLINK: |
| 2082 | case RTM_DELLINK: |
| 2083 | ifi = NLMSG_DATA(nlh); |
| 2084 | ifi->ifi_type = tswap16(ifi->ifi_type); |
| 2085 | ifi->ifi_index = tswap32(ifi->ifi_index); |
| 2086 | ifi->ifi_flags = tswap32(ifi->ifi_flags); |
| 2087 | ifi->ifi_change = tswap32(ifi->ifi_change); |
| 2088 | target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len - |
| 2089 | NLMSG_LENGTH(sizeof(*ifi))); |
| 2090 | break; |
| 2091 | case RTM_GETADDR: |
| 2092 | case RTM_NEWADDR: |
| 2093 | case RTM_DELADDR: |
| 2094 | ifa = NLMSG_DATA(nlh); |
| 2095 | ifa->ifa_index = tswap32(ifa->ifa_index); |
| 2096 | target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len - |
| 2097 | NLMSG_LENGTH(sizeof(*ifa))); |
| 2098 | break; |
| 2099 | case RTM_GETROUTE: |
| 2100 | break; |
| 2101 | case RTM_NEWROUTE: |
| 2102 | case RTM_DELROUTE: |
| 2103 | rtm = NLMSG_DATA(nlh); |
| 2104 | rtm->rtm_flags = tswap32(rtm->rtm_flags); |
| 2105 | target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len - |
| 2106 | NLMSG_LENGTH(sizeof(*rtm))); |
| 2107 | break; |
| 2108 | default: |
| 2109 | return -TARGET_EOPNOTSUPP; |
| 2110 | } |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len) |
| 2115 | { |
| 2116 | return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route); |
| 2117 | } |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2118 | #endif /* CONFIG_RTNETLINK */ |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2119 | |
Laurent Vivier | 5ce9bb5 | 2016-05-22 18:56:21 +0200 | [diff] [blame] | 2120 | static abi_long host_to_target_data_audit(struct nlmsghdr *nlh) |
| 2121 | { |
| 2122 | switch (nlh->nlmsg_type) { |
| 2123 | default: |
| 2124 | gemu_log("Unknown host audit message type %d\n", |
| 2125 | nlh->nlmsg_type); |
| 2126 | return -TARGET_EINVAL; |
| 2127 | } |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh, |
| 2132 | size_t len) |
| 2133 | { |
| 2134 | return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit); |
| 2135 | } |
| 2136 | |
| 2137 | static abi_long target_to_host_data_audit(struct nlmsghdr *nlh) |
| 2138 | { |
| 2139 | switch (nlh->nlmsg_type) { |
| 2140 | case AUDIT_USER: |
| 2141 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
| 2142 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: |
| 2143 | break; |
| 2144 | default: |
| 2145 | gemu_log("Unknown target audit message type %d\n", |
| 2146 | nlh->nlmsg_type); |
| 2147 | return -TARGET_EINVAL; |
| 2148 | } |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len) |
| 2154 | { |
| 2155 | return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit); |
| 2156 | } |
| 2157 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2158 | /* do_setsockopt() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2159 | static abi_long do_setsockopt(int sockfd, int level, int optname, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2160 | abi_ulong optval_addr, socklen_t optlen) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2161 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2162 | abi_long ret; |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 2163 | int val; |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2164 | struct ip_mreqn *ip_mreq; |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 2165 | struct ip_mreq_source *ip_mreq_source; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2166 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2167 | switch(level) { |
| 2168 | case SOL_TCP: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2169 | /* TCP options all take an 'int' value. */ |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2170 | if (optlen < sizeof(uint32_t)) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2171 | return -TARGET_EINVAL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2172 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2173 | if (get_user_u32(val, optval_addr)) |
| 2174 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2175 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
| 2176 | break; |
| 2177 | case SOL_IP: |
| 2178 | switch(optname) { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2179 | case IP_TOS: |
| 2180 | case IP_TTL: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2181 | case IP_HDRINCL: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2182 | case IP_ROUTER_ALERT: |
| 2183 | case IP_RECVOPTS: |
| 2184 | case IP_RETOPTS: |
| 2185 | case IP_PKTINFO: |
| 2186 | case IP_MTU_DISCOVER: |
| 2187 | case IP_RECVERR: |
| 2188 | case IP_RECVTOS: |
| 2189 | #ifdef IP_FREEBIND |
| 2190 | case IP_FREEBIND: |
| 2191 | #endif |
| 2192 | case IP_MULTICAST_TTL: |
| 2193 | case IP_MULTICAST_LOOP: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2194 | val = 0; |
| 2195 | if (optlen >= sizeof(uint32_t)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2196 | if (get_user_u32(val, optval_addr)) |
| 2197 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2198 | } else if (optlen >= 1) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2199 | if (get_user_u8(val, optval_addr)) |
| 2200 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2201 | } |
| 2202 | ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); |
| 2203 | break; |
Lionel Landwerlin | b975b83 | 2009-04-25 23:30:19 +0200 | [diff] [blame] | 2204 | case IP_ADD_MEMBERSHIP: |
| 2205 | case IP_DROP_MEMBERSHIP: |
| 2206 | if (optlen < sizeof (struct target_ip_mreq) || |
| 2207 | optlen > sizeof (struct target_ip_mreqn)) |
| 2208 | return -TARGET_EINVAL; |
| 2209 | |
| 2210 | ip_mreq = (struct ip_mreqn *) alloca(optlen); |
| 2211 | target_to_host_ip_mreq(ip_mreq, optval_addr, optlen); |
| 2212 | ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen)); |
| 2213 | break; |
| 2214 | |
Lionel Landwerlin | 6e3cb58 | 2009-04-25 23:31:18 +0200 | [diff] [blame] | 2215 | case IP_BLOCK_SOURCE: |
| 2216 | case IP_UNBLOCK_SOURCE: |
| 2217 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 2218 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 2219 | if (optlen != sizeof (struct target_ip_mreq_source)) |
| 2220 | return -TARGET_EINVAL; |
| 2221 | |
| 2222 | ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
| 2223 | ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen)); |
| 2224 | unlock_user (ip_mreq_source, optval_addr, 0); |
| 2225 | break; |
| 2226 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2227 | default: |
| 2228 | goto unimplemented; |
| 2229 | } |
| 2230 | break; |
Laurent Vivier | 0d78b3b | 2013-08-30 01:46:43 +0200 | [diff] [blame] | 2231 | case SOL_IPV6: |
| 2232 | switch (optname) { |
| 2233 | case IPV6_MTU_DISCOVER: |
| 2234 | case IPV6_MTU: |
| 2235 | case IPV6_V6ONLY: |
| 2236 | case IPV6_RECVPKTINFO: |
| 2237 | val = 0; |
| 2238 | if (optlen < sizeof(uint32_t)) { |
| 2239 | return -TARGET_EINVAL; |
| 2240 | } |
| 2241 | if (get_user_u32(val, optval_addr)) { |
| 2242 | return -TARGET_EFAULT; |
| 2243 | } |
| 2244 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2245 | &val, sizeof(val))); |
| 2246 | break; |
| 2247 | default: |
| 2248 | goto unimplemented; |
| 2249 | } |
| 2250 | break; |
Jing Huang | 920394d | 2012-07-24 13:59:23 +0000 | [diff] [blame] | 2251 | case SOL_RAW: |
| 2252 | switch (optname) { |
| 2253 | case ICMP_FILTER: |
| 2254 | /* struct icmp_filter takes an u32 value */ |
| 2255 | if (optlen < sizeof(uint32_t)) { |
| 2256 | return -TARGET_EINVAL; |
| 2257 | } |
| 2258 | |
| 2259 | if (get_user_u32(val, optval_addr)) { |
| 2260 | return -TARGET_EFAULT; |
| 2261 | } |
| 2262 | ret = get_errno(setsockopt(sockfd, level, optname, |
| 2263 | &val, sizeof(val))); |
| 2264 | break; |
| 2265 | |
| 2266 | default: |
| 2267 | goto unimplemented; |
| 2268 | } |
| 2269 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2270 | case TARGET_SOL_SOCKET: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2271 | switch (optname) { |
Laurent Vivier | 1b09aeb | 2013-01-01 08:24:11 +0000 | [diff] [blame] | 2272 | case TARGET_SO_RCVTIMEO: |
| 2273 | { |
| 2274 | struct timeval tv; |
| 2275 | |
| 2276 | optname = SO_RCVTIMEO; |
| 2277 | |
| 2278 | set_timeout: |
| 2279 | if (optlen != sizeof(struct target_timeval)) { |
| 2280 | return -TARGET_EINVAL; |
| 2281 | } |
| 2282 | |
| 2283 | if (copy_from_user_timeval(&tv, optval_addr)) { |
| 2284 | return -TARGET_EFAULT; |
| 2285 | } |
| 2286 | |
| 2287 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, |
| 2288 | &tv, sizeof(tv))); |
| 2289 | return ret; |
| 2290 | } |
| 2291 | case TARGET_SO_SNDTIMEO: |
| 2292 | optname = SO_SNDTIMEO; |
| 2293 | goto set_timeout; |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2294 | case TARGET_SO_ATTACH_FILTER: |
| 2295 | { |
| 2296 | struct target_sock_fprog *tfprog; |
| 2297 | struct target_sock_filter *tfilter; |
| 2298 | struct sock_fprog fprog; |
| 2299 | struct sock_filter *filter; |
| 2300 | int i; |
| 2301 | |
| 2302 | if (optlen != sizeof(*tfprog)) { |
| 2303 | return -TARGET_EINVAL; |
| 2304 | } |
| 2305 | if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) { |
| 2306 | return -TARGET_EFAULT; |
| 2307 | } |
| 2308 | if (!lock_user_struct(VERIFY_READ, tfilter, |
| 2309 | tswapal(tfprog->filter), 0)) { |
| 2310 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2311 | return -TARGET_EFAULT; |
| 2312 | } |
| 2313 | |
| 2314 | fprog.len = tswap16(tfprog->len); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2315 | filter = g_try_new(struct sock_filter, fprog.len); |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2316 | if (filter == NULL) { |
| 2317 | unlock_user_struct(tfilter, tfprog->filter, 1); |
| 2318 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2319 | return -TARGET_ENOMEM; |
| 2320 | } |
| 2321 | for (i = 0; i < fprog.len; i++) { |
| 2322 | filter[i].code = tswap16(tfilter[i].code); |
| 2323 | filter[i].jt = tfilter[i].jt; |
| 2324 | filter[i].jf = tfilter[i].jf; |
| 2325 | filter[i].k = tswap32(tfilter[i].k); |
| 2326 | } |
| 2327 | fprog.filter = filter; |
| 2328 | |
| 2329 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, |
| 2330 | SO_ATTACH_FILTER, &fprog, sizeof(fprog))); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2331 | g_free(filter); |
Laurent Vivier | f57d419 | 2013-08-30 01:46:41 +0200 | [diff] [blame] | 2332 | |
| 2333 | unlock_user_struct(tfilter, tfprog->filter, 1); |
| 2334 | unlock_user_struct(tfprog, optval_addr, 1); |
| 2335 | return ret; |
| 2336 | } |
Joakim Tjernlund | 451aaf6 | 2014-07-12 15:47:06 +0200 | [diff] [blame] | 2337 | case TARGET_SO_BINDTODEVICE: |
| 2338 | { |
| 2339 | char *dev_ifname, *addr_ifname; |
| 2340 | |
| 2341 | if (optlen > IFNAMSIZ - 1) { |
| 2342 | optlen = IFNAMSIZ - 1; |
| 2343 | } |
| 2344 | dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1); |
| 2345 | if (!dev_ifname) { |
| 2346 | return -TARGET_EFAULT; |
| 2347 | } |
| 2348 | optname = SO_BINDTODEVICE; |
| 2349 | addr_ifname = alloca(IFNAMSIZ); |
| 2350 | memcpy(addr_ifname, dev_ifname, optlen); |
| 2351 | addr_ifname[optlen] = 0; |
Chen Gang | fad6c58 | 2016-01-11 13:58:50 +0800 | [diff] [blame] | 2352 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, |
| 2353 | addr_ifname, optlen)); |
Joakim Tjernlund | 451aaf6 | 2014-07-12 15:47:06 +0200 | [diff] [blame] | 2354 | unlock_user (dev_ifname, optval_addr, 0); |
| 2355 | return ret; |
| 2356 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2357 | /* Options with 'int' argument. */ |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2358 | case TARGET_SO_DEBUG: |
| 2359 | optname = SO_DEBUG; |
| 2360 | break; |
| 2361 | case TARGET_SO_REUSEADDR: |
| 2362 | optname = SO_REUSEADDR; |
| 2363 | break; |
| 2364 | case TARGET_SO_TYPE: |
| 2365 | optname = SO_TYPE; |
| 2366 | break; |
| 2367 | case TARGET_SO_ERROR: |
| 2368 | optname = SO_ERROR; |
| 2369 | break; |
| 2370 | case TARGET_SO_DONTROUTE: |
| 2371 | optname = SO_DONTROUTE; |
| 2372 | break; |
| 2373 | case TARGET_SO_BROADCAST: |
| 2374 | optname = SO_BROADCAST; |
| 2375 | break; |
| 2376 | case TARGET_SO_SNDBUF: |
| 2377 | optname = SO_SNDBUF; |
| 2378 | break; |
Paul Burton | d79b6cc | 2014-06-22 11:25:35 +0100 | [diff] [blame] | 2379 | case TARGET_SO_SNDBUFFORCE: |
| 2380 | optname = SO_SNDBUFFORCE; |
| 2381 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2382 | case TARGET_SO_RCVBUF: |
| 2383 | optname = SO_RCVBUF; |
| 2384 | break; |
Paul Burton | d79b6cc | 2014-06-22 11:25:35 +0100 | [diff] [blame] | 2385 | case TARGET_SO_RCVBUFFORCE: |
| 2386 | optname = SO_RCVBUFFORCE; |
| 2387 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2388 | case TARGET_SO_KEEPALIVE: |
| 2389 | optname = SO_KEEPALIVE; |
| 2390 | break; |
| 2391 | case TARGET_SO_OOBINLINE: |
| 2392 | optname = SO_OOBINLINE; |
| 2393 | break; |
| 2394 | case TARGET_SO_NO_CHECK: |
| 2395 | optname = SO_NO_CHECK; |
| 2396 | break; |
| 2397 | case TARGET_SO_PRIORITY: |
| 2398 | optname = SO_PRIORITY; |
| 2399 | break; |
bellard | 5e83e8e | 2005-03-01 22:32:06 +0000 | [diff] [blame] | 2400 | #ifdef SO_BSDCOMPAT |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2401 | case TARGET_SO_BSDCOMPAT: |
| 2402 | optname = SO_BSDCOMPAT; |
| 2403 | break; |
bellard | 5e83e8e | 2005-03-01 22:32:06 +0000 | [diff] [blame] | 2404 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2405 | case TARGET_SO_PASSCRED: |
| 2406 | optname = SO_PASSCRED; |
| 2407 | break; |
Paul Burton | 82d0fe6 | 2014-06-22 11:25:36 +0100 | [diff] [blame] | 2408 | case TARGET_SO_PASSSEC: |
| 2409 | optname = SO_PASSSEC; |
| 2410 | break; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2411 | case TARGET_SO_TIMESTAMP: |
| 2412 | optname = SO_TIMESTAMP; |
| 2413 | break; |
| 2414 | case TARGET_SO_RCVLOWAT: |
| 2415 | optname = SO_RCVLOWAT; |
| 2416 | break; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2417 | break; |
| 2418 | default: |
| 2419 | goto unimplemented; |
| 2420 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2421 | if (optlen < sizeof(uint32_t)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2422 | return -TARGET_EINVAL; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2423 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2424 | if (get_user_u32(val, optval_addr)) |
| 2425 | return -TARGET_EFAULT; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2426 | ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val))); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2427 | break; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2428 | default: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2429 | unimplemented: |
Stefan Weil | b2bedb2 | 2011-09-12 22:33:01 +0200 | [diff] [blame] | 2430 | gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname); |
ths | 6fa13c1 | 2007-12-18 02:41:04 +0000 | [diff] [blame] | 2431 | ret = -TARGET_ENOPROTOOPT; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2432 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2433 | return ret; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2436 | /* do_getsockopt() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2437 | static abi_long do_getsockopt(int sockfd, int level, int optname, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2438 | abi_ulong optval_addr, abi_ulong optlen) |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2439 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2440 | abi_long ret; |
blueswir1 | b55266b | 2008-09-20 08:07:15 +0000 | [diff] [blame] | 2441 | int len, val; |
| 2442 | socklen_t lv; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2443 | |
| 2444 | switch(level) { |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2445 | case TARGET_SOL_SOCKET: |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2446 | level = SOL_SOCKET; |
| 2447 | switch (optname) { |
| 2448 | /* These don't just return a single integer */ |
| 2449 | case TARGET_SO_LINGER: |
| 2450 | case TARGET_SO_RCVTIMEO: |
| 2451 | case TARGET_SO_SNDTIMEO: |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2452 | case TARGET_SO_PEERNAME: |
| 2453 | goto unimplemented; |
Akos PASZTORY | 583359a | 2011-11-14 15:09:49 +0200 | [diff] [blame] | 2454 | case TARGET_SO_PEERCRED: { |
| 2455 | struct ucred cr; |
| 2456 | socklen_t crlen; |
| 2457 | struct target_ucred *tcr; |
| 2458 | |
| 2459 | if (get_user_u32(len, optlen)) { |
| 2460 | return -TARGET_EFAULT; |
| 2461 | } |
| 2462 | if (len < 0) { |
| 2463 | return -TARGET_EINVAL; |
| 2464 | } |
| 2465 | |
| 2466 | crlen = sizeof(cr); |
| 2467 | ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED, |
| 2468 | &cr, &crlen)); |
| 2469 | if (ret < 0) { |
| 2470 | return ret; |
| 2471 | } |
| 2472 | if (len > crlen) { |
| 2473 | len = crlen; |
| 2474 | } |
| 2475 | if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) { |
| 2476 | return -TARGET_EFAULT; |
| 2477 | } |
| 2478 | __put_user(cr.pid, &tcr->pid); |
| 2479 | __put_user(cr.uid, &tcr->uid); |
| 2480 | __put_user(cr.gid, &tcr->gid); |
| 2481 | unlock_user_struct(tcr, optval_addr, 1); |
| 2482 | if (put_user_u32(len, optlen)) { |
| 2483 | return -TARGET_EFAULT; |
| 2484 | } |
| 2485 | break; |
| 2486 | } |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2487 | /* Options with 'int' argument. */ |
| 2488 | case TARGET_SO_DEBUG: |
| 2489 | optname = SO_DEBUG; |
| 2490 | goto int_case; |
| 2491 | case TARGET_SO_REUSEADDR: |
| 2492 | optname = SO_REUSEADDR; |
| 2493 | goto int_case; |
| 2494 | case TARGET_SO_TYPE: |
| 2495 | optname = SO_TYPE; |
| 2496 | goto int_case; |
| 2497 | case TARGET_SO_ERROR: |
| 2498 | optname = SO_ERROR; |
| 2499 | goto int_case; |
| 2500 | case TARGET_SO_DONTROUTE: |
| 2501 | optname = SO_DONTROUTE; |
| 2502 | goto int_case; |
| 2503 | case TARGET_SO_BROADCAST: |
| 2504 | optname = SO_BROADCAST; |
| 2505 | goto int_case; |
| 2506 | case TARGET_SO_SNDBUF: |
| 2507 | optname = SO_SNDBUF; |
| 2508 | goto int_case; |
| 2509 | case TARGET_SO_RCVBUF: |
| 2510 | optname = SO_RCVBUF; |
| 2511 | goto int_case; |
| 2512 | case TARGET_SO_KEEPALIVE: |
| 2513 | optname = SO_KEEPALIVE; |
| 2514 | goto int_case; |
| 2515 | case TARGET_SO_OOBINLINE: |
| 2516 | optname = SO_OOBINLINE; |
| 2517 | goto int_case; |
| 2518 | case TARGET_SO_NO_CHECK: |
| 2519 | optname = SO_NO_CHECK; |
| 2520 | goto int_case; |
| 2521 | case TARGET_SO_PRIORITY: |
| 2522 | optname = SO_PRIORITY; |
| 2523 | goto int_case; |
| 2524 | #ifdef SO_BSDCOMPAT |
| 2525 | case TARGET_SO_BSDCOMPAT: |
| 2526 | optname = SO_BSDCOMPAT; |
| 2527 | goto int_case; |
| 2528 | #endif |
| 2529 | case TARGET_SO_PASSCRED: |
| 2530 | optname = SO_PASSCRED; |
| 2531 | goto int_case; |
| 2532 | case TARGET_SO_TIMESTAMP: |
| 2533 | optname = SO_TIMESTAMP; |
| 2534 | goto int_case; |
| 2535 | case TARGET_SO_RCVLOWAT: |
| 2536 | optname = SO_RCVLOWAT; |
| 2537 | goto int_case; |
Paul Burton | aec1ca4 | 2014-06-22 11:25:34 +0100 | [diff] [blame] | 2538 | case TARGET_SO_ACCEPTCONN: |
| 2539 | optname = SO_ACCEPTCONN; |
| 2540 | goto int_case; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2541 | default: |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2542 | goto int_case; |
| 2543 | } |
| 2544 | break; |
| 2545 | case SOL_TCP: |
| 2546 | /* TCP options all take an 'int' value. */ |
| 2547 | int_case: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2548 | if (get_user_u32(len, optlen)) |
| 2549 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2550 | if (len < 0) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2551 | return -TARGET_EINVAL; |
Mike Frysinger | 73160d9 | 2011-02-07 01:05:49 -0500 | [diff] [blame] | 2552 | lv = sizeof(lv); |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2553 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2554 | if (ret < 0) |
| 2555 | return ret; |
Paul Burton | 8289d11 | 2014-06-22 11:25:33 +0100 | [diff] [blame] | 2556 | if (optname == SO_TYPE) { |
| 2557 | val = host_to_target_sock_type(val); |
| 2558 | } |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2559 | if (len > lv) |
| 2560 | len = lv; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2561 | if (len == 4) { |
| 2562 | if (put_user_u32(val, optval_addr)) |
| 2563 | return -TARGET_EFAULT; |
| 2564 | } else { |
| 2565 | if (put_user_u8(val, optval_addr)) |
| 2566 | return -TARGET_EFAULT; |
Jamie Lentin | f3b974c | 2010-11-26 15:04:08 +0200 | [diff] [blame] | 2567 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2568 | if (put_user_u32(len, optlen)) |
| 2569 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | case SOL_IP: |
| 2572 | switch(optname) { |
| 2573 | case IP_TOS: |
| 2574 | case IP_TTL: |
| 2575 | case IP_HDRINCL: |
| 2576 | case IP_ROUTER_ALERT: |
| 2577 | case IP_RECVOPTS: |
| 2578 | case IP_RETOPTS: |
| 2579 | case IP_PKTINFO: |
| 2580 | case IP_MTU_DISCOVER: |
| 2581 | case IP_RECVERR: |
| 2582 | case IP_RECVTOS: |
| 2583 | #ifdef IP_FREEBIND |
| 2584 | case IP_FREEBIND: |
| 2585 | #endif |
| 2586 | case IP_MULTICAST_TTL: |
| 2587 | case IP_MULTICAST_LOOP: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2588 | if (get_user_u32(len, optlen)) |
| 2589 | return -TARGET_EFAULT; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2590 | if (len < 0) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2591 | return -TARGET_EINVAL; |
Mike Frysinger | 73160d9 | 2011-02-07 01:05:49 -0500 | [diff] [blame] | 2592 | lv = sizeof(lv); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2593 | ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); |
| 2594 | if (ret < 0) |
| 2595 | return ret; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2596 | if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2597 | len = 1; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2598 | if (put_user_u32(len, optlen) |
| 2599 | || put_user_u8(val, optval_addr)) |
| 2600 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2601 | } else { |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2602 | if (len > sizeof(int)) |
| 2603 | len = sizeof(int); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 2604 | if (put_user_u32(len, optlen) |
| 2605 | || put_user_u32(val, optval_addr)) |
| 2606 | return -TARGET_EFAULT; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2607 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2608 | break; |
bellard | 2efbe91 | 2005-07-23 15:10:20 +0000 | [diff] [blame] | 2609 | default: |
ths | c02f499 | 2007-12-18 02:39:59 +0000 | [diff] [blame] | 2610 | ret = -TARGET_ENOPROTOOPT; |
| 2611 | break; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2612 | } |
| 2613 | break; |
| 2614 | default: |
| 2615 | unimplemented: |
| 2616 | gemu_log("getsockopt level=%d optname=%d not yet supported\n", |
| 2617 | level, optname); |
ths | c02f499 | 2007-12-18 02:39:59 +0000 | [diff] [blame] | 2618 | ret = -TARGET_EOPNOTSUPP; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 2619 | break; |
| 2620 | } |
| 2621 | return ret; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2624 | static struct iovec *lock_iovec(int type, abi_ulong target_addr, |
| 2625 | int count, int copy) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2626 | { |
| 2627 | struct target_iovec *target_vec; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2628 | struct iovec *vec; |
| 2629 | abi_ulong total_len, max_len; |
balrog | d732dcb | 2008-10-28 10:21:03 +0000 | [diff] [blame] | 2630 | int i; |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2631 | int err = 0; |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2632 | bool bad_address = false; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2633 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2634 | if (count == 0) { |
| 2635 | errno = 0; |
| 2636 | return NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2637 | } |
Peter Maydell | dfae8e0 | 2013-02-08 07:58:41 +0000 | [diff] [blame] | 2638 | if (count < 0 || count > IOV_MAX) { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2639 | errno = EINVAL; |
| 2640 | return NULL; |
| 2641 | } |
| 2642 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2643 | vec = g_try_new0(struct iovec, count); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2644 | if (vec == NULL) { |
| 2645 | errno = ENOMEM; |
| 2646 | return NULL; |
| 2647 | } |
| 2648 | |
| 2649 | target_vec = lock_user(VERIFY_READ, target_addr, |
| 2650 | count * sizeof(struct target_iovec), 1); |
| 2651 | if (target_vec == NULL) { |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2652 | err = EFAULT; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2653 | goto fail2; |
| 2654 | } |
| 2655 | |
| 2656 | /* ??? If host page size > target page size, this will result in a |
| 2657 | value larger than what we can actually support. */ |
| 2658 | max_len = 0x7fffffff & TARGET_PAGE_MASK; |
| 2659 | total_len = 0; |
| 2660 | |
| 2661 | for (i = 0; i < count; i++) { |
| 2662 | abi_ulong base = tswapal(target_vec[i].iov_base); |
| 2663 | abi_long len = tswapal(target_vec[i].iov_len); |
| 2664 | |
| 2665 | if (len < 0) { |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2666 | err = EINVAL; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2667 | goto fail; |
| 2668 | } else if (len == 0) { |
| 2669 | /* Zero length pointer is ignored. */ |
| 2670 | vec[i].iov_base = 0; |
| 2671 | } else { |
| 2672 | vec[i].iov_base = lock_user(type, base, len, copy); |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2673 | /* If the first buffer pointer is bad, this is a fault. But |
| 2674 | * subsequent bad buffers will result in a partial write; this |
| 2675 | * is realized by filling the vector with null pointers and |
| 2676 | * zero lengths. */ |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2677 | if (!vec[i].iov_base) { |
Tom Musta | 29560a6 | 2014-08-12 13:53:43 -0500 | [diff] [blame] | 2678 | if (i == 0) { |
| 2679 | err = EFAULT; |
| 2680 | goto fail; |
| 2681 | } else { |
| 2682 | bad_address = true; |
| 2683 | } |
| 2684 | } |
| 2685 | if (bad_address) { |
| 2686 | len = 0; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2687 | } |
| 2688 | if (len > max_len - total_len) { |
| 2689 | len = max_len - total_len; |
| 2690 | } |
| 2691 | } |
| 2692 | vec[i].iov_len = len; |
| 2693 | total_len += len; |
| 2694 | } |
| 2695 | |
| 2696 | unlock_user(target_vec, target_addr, 0); |
| 2697 | return vec; |
| 2698 | |
| 2699 | fail: |
Chen Gang S | 7eff518 | 2015-01-23 18:01:09 +0800 | [diff] [blame] | 2700 | while (--i >= 0) { |
| 2701 | if (tswapal(target_vec[i].iov_len) > 0) { |
| 2702 | unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0); |
| 2703 | } |
| 2704 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2705 | unlock_user(target_vec, target_addr, 0); |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2706 | fail2: |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2707 | g_free(vec); |
Peter Maydell | 501bb4b | 2014-02-17 18:55:33 +0000 | [diff] [blame] | 2708 | errno = err; |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2709 | return NULL; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2712 | static void unlock_iovec(struct iovec *vec, abi_ulong target_addr, |
| 2713 | int count, int copy) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2714 | { |
| 2715 | struct target_iovec *target_vec; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2716 | int i; |
| 2717 | |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2718 | target_vec = lock_user(VERIFY_READ, target_addr, |
| 2719 | count * sizeof(struct target_iovec), 1); |
| 2720 | if (target_vec) { |
| 2721 | for (i = 0; i < count; i++) { |
| 2722 | abi_ulong base = tswapal(target_vec[i].iov_base); |
Chen Gang S | 71ec7ce | 2015-01-23 18:07:50 +0800 | [diff] [blame] | 2723 | abi_long len = tswapal(target_vec[i].iov_len); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2724 | if (len < 0) { |
| 2725 | break; |
| 2726 | } |
balrog | d732dcb | 2008-10-28 10:21:03 +0000 | [diff] [blame] | 2727 | unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); |
| 2728 | } |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2729 | unlock_user(target_vec, target_addr, 0); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2730 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 2731 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 2732 | g_free(vec); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2735 | static inline int target_to_host_sock_type(int *type) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2736 | { |
| 2737 | int host_type = 0; |
| 2738 | int target_type = *type; |
| 2739 | |
| 2740 | switch (target_type & TARGET_SOCK_TYPE_MASK) { |
| 2741 | case TARGET_SOCK_DGRAM: |
| 2742 | host_type = SOCK_DGRAM; |
| 2743 | break; |
| 2744 | case TARGET_SOCK_STREAM: |
| 2745 | host_type = SOCK_STREAM; |
| 2746 | break; |
| 2747 | default: |
| 2748 | host_type = target_type & TARGET_SOCK_TYPE_MASK; |
| 2749 | break; |
| 2750 | } |
| 2751 | if (target_type & TARGET_SOCK_CLOEXEC) { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2752 | #if defined(SOCK_CLOEXEC) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2753 | host_type |= SOCK_CLOEXEC; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2754 | #else |
| 2755 | return -TARGET_EINVAL; |
| 2756 | #endif |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2757 | } |
| 2758 | if (target_type & TARGET_SOCK_NONBLOCK) { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2759 | #if defined(SOCK_NONBLOCK) |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2760 | host_type |= SOCK_NONBLOCK; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2761 | #elif !defined(O_NONBLOCK) |
| 2762 | return -TARGET_EINVAL; |
| 2763 | #endif |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2764 | } |
| 2765 | *type = host_type; |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2766 | return 0; |
| 2767 | } |
| 2768 | |
| 2769 | /* Try to emulate socket type flags after socket creation. */ |
| 2770 | static int sock_flags_fixup(int fd, int target_type) |
| 2771 | { |
| 2772 | #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK) |
| 2773 | if (target_type & TARGET_SOCK_NONBLOCK) { |
| 2774 | int flags = fcntl(fd, F_GETFL); |
| 2775 | if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) { |
| 2776 | close(fd); |
| 2777 | return -TARGET_EINVAL; |
| 2778 | } |
| 2779 | } |
| 2780 | #endif |
| 2781 | return fd; |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 2784 | static abi_long packet_target_to_host_sockaddr(void *host_addr, |
| 2785 | abi_ulong target_addr, |
| 2786 | socklen_t len) |
| 2787 | { |
| 2788 | struct sockaddr *addr = host_addr; |
| 2789 | struct target_sockaddr *target_saddr; |
| 2790 | |
| 2791 | target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); |
| 2792 | if (!target_saddr) { |
| 2793 | return -TARGET_EFAULT; |
| 2794 | } |
| 2795 | |
| 2796 | memcpy(addr, target_saddr, len); |
| 2797 | addr->sa_family = tswap16(target_saddr->sa_family); |
| 2798 | /* spkt_protocol is big-endian */ |
| 2799 | |
| 2800 | unlock_user(target_saddr, target_addr, 0); |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
| 2804 | static TargetFdTrans target_packet_trans = { |
| 2805 | .target_to_host_addr = packet_target_to_host_sockaddr, |
| 2806 | }; |
| 2807 | |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2808 | #ifdef CONFIG_RTNETLINK |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2809 | static abi_long netlink_route_target_to_host(void *buf, size_t len) |
| 2810 | { |
| 2811 | return target_to_host_nlmsg_route(buf, len); |
| 2812 | } |
| 2813 | |
| 2814 | static abi_long netlink_route_host_to_target(void *buf, size_t len) |
| 2815 | { |
| 2816 | return host_to_target_nlmsg_route(buf, len); |
| 2817 | } |
| 2818 | |
| 2819 | static TargetFdTrans target_netlink_route_trans = { |
| 2820 | .target_to_host_data = netlink_route_target_to_host, |
| 2821 | .host_to_target_data = netlink_route_host_to_target, |
| 2822 | }; |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2823 | #endif /* CONFIG_RTNETLINK */ |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2824 | |
Laurent Vivier | 5ce9bb5 | 2016-05-22 18:56:21 +0200 | [diff] [blame] | 2825 | static abi_long netlink_audit_target_to_host(void *buf, size_t len) |
| 2826 | { |
| 2827 | return target_to_host_nlmsg_audit(buf, len); |
| 2828 | } |
| 2829 | |
| 2830 | static abi_long netlink_audit_host_to_target(void *buf, size_t len) |
| 2831 | { |
| 2832 | return host_to_target_nlmsg_audit(buf, len); |
| 2833 | } |
| 2834 | |
| 2835 | static TargetFdTrans target_netlink_audit_trans = { |
| 2836 | .target_to_host_data = netlink_audit_target_to_host, |
| 2837 | .host_to_target_data = netlink_audit_host_to_target, |
| 2838 | }; |
| 2839 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2840 | /* do_socket() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2841 | static abi_long do_socket(int domain, int type, int protocol) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2842 | { |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2843 | int target_type = type; |
| 2844 | int ret; |
| 2845 | |
| 2846 | ret = target_to_host_sock_type(&type); |
| 2847 | if (ret) { |
| 2848 | return ret; |
| 2849 | } |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 2850 | |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2851 | if (domain == PF_NETLINK && !( |
| 2852 | #ifdef CONFIG_RTNETLINK |
| 2853 | protocol == NETLINK_ROUTE || |
| 2854 | #endif |
| 2855 | protocol == NETLINK_KOBJECT_UEVENT || |
| 2856 | protocol == NETLINK_AUDIT)) { |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2857 | return -EPFNOSUPPORT; |
| 2858 | } |
Laurent Vivier | ff626f2 | 2015-10-28 21:40:42 +0100 | [diff] [blame] | 2859 | |
| 2860 | if (domain == AF_PACKET || |
| 2861 | (domain == AF_INET && type == SOCK_PACKET)) { |
| 2862 | protocol = tswap16(protocol); |
| 2863 | } |
| 2864 | |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2865 | ret = get_errno(socket(domain, type, protocol)); |
| 2866 | if (ret >= 0) { |
| 2867 | ret = sock_flags_fixup(ret, target_type); |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 2868 | if (type == SOCK_PACKET) { |
| 2869 | /* Manage an obsolete case : |
| 2870 | * if socket type is SOCK_PACKET, bind by name |
| 2871 | */ |
| 2872 | fd_trans_register(ret, &target_packet_trans); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2873 | } else if (domain == PF_NETLINK) { |
| 2874 | switch (protocol) { |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2875 | #ifdef CONFIG_RTNETLINK |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2876 | case NETLINK_ROUTE: |
| 2877 | fd_trans_register(ret, &target_netlink_route_trans); |
| 2878 | break; |
Laurent Vivier | 575b22b | 2016-06-02 22:14:15 +0200 | [diff] [blame] | 2879 | #endif |
Laurent Vivier | b265620 | 2016-05-22 18:56:20 +0200 | [diff] [blame] | 2880 | case NETLINK_KOBJECT_UEVENT: |
| 2881 | /* nothing to do: messages are strings */ |
| 2882 | break; |
Laurent Vivier | 5ce9bb5 | 2016-05-22 18:56:21 +0200 | [diff] [blame] | 2883 | case NETLINK_AUDIT: |
| 2884 | fd_trans_register(ret, &target_netlink_audit_trans); |
| 2885 | break; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2886 | default: |
| 2887 | g_assert_not_reached(); |
| 2888 | } |
Laurent Vivier | 0cf2272 | 2015-10-28 21:40:45 +0100 | [diff] [blame] | 2889 | } |
Edgar E. Iglesias | 53d09b7 | 2013-09-23 14:11:53 +0200 | [diff] [blame] | 2890 | } |
| 2891 | return ret; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2894 | /* do_bind() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2895 | static abi_long do_bind(int sockfd, abi_ulong target_addr, |
| 2896 | socklen_t addrlen) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2897 | { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2898 | void *addr; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 2899 | abi_long ret; |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2900 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 2901 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2902 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 2903 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2904 | |
aurel32 | 607175e | 2009-04-15 16:11:59 +0000 | [diff] [blame] | 2905 | addr = alloca(addrlen+1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2906 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 2907 | ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 2908 | if (ret) |
| 2909 | return ret; |
| 2910 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2911 | return get_errno(bind(sockfd, addr, addrlen)); |
| 2912 | } |
| 2913 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 2914 | /* do_connect() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2915 | static abi_long do_connect(int sockfd, abi_ulong target_addr, |
| 2916 | socklen_t addrlen) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2917 | { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2918 | void *addr; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 2919 | abi_long ret; |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2920 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 2921 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2922 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 2923 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 2924 | |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 2925 | addr = alloca(addrlen+1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2926 | |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 2927 | ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 2928 | if (ret) |
| 2929 | return ret; |
| 2930 | |
Peter Maydell | 2a3c761 | 2016-06-06 19:58:03 +0100 | [diff] [blame] | 2931 | return get_errno(safe_connect(sockfd, addr, addrlen)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 2934 | /* do_sendrecvmsg_locked() Must return target values and target errnos. */ |
| 2935 | static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, |
| 2936 | int flags, int send) |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2937 | { |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 2938 | abi_long ret, len; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2939 | struct msghdr msg; |
| 2940 | int count; |
| 2941 | struct iovec *vec; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 2942 | abi_ulong target_vec; |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2943 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2944 | if (msgp->msg_name) { |
| 2945 | msg.msg_namelen = tswap32(msgp->msg_namelen); |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 2946 | msg.msg_name = alloca(msg.msg_namelen+1); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 2947 | ret = target_to_host_sockaddr(fd, msg.msg_name, |
| 2948 | tswapal(msgp->msg_name), |
| 2949 | msg.msg_namelen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 2950 | if (ret) { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2951 | goto out2; |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 2952 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2953 | } else { |
| 2954 | msg.msg_name = NULL; |
| 2955 | msg.msg_namelen = 0; |
| 2956 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 2957 | msg.msg_controllen = 2 * tswapal(msgp->msg_controllen); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2958 | msg.msg_control = alloca(msg.msg_controllen); |
| 2959 | msg.msg_flags = tswap32(msgp->msg_flags); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2960 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 2961 | count = tswapal(msgp->msg_iovlen); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 2962 | target_vec = tswapal(msgp->msg_iov); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 2963 | vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, |
| 2964 | target_vec, count, send); |
| 2965 | if (vec == NULL) { |
| 2966 | ret = -host_to_target_errno(errno); |
| 2967 | goto out2; |
| 2968 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2969 | msg.msg_iovlen = count; |
| 2970 | msg.msg_iov = vec; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2971 | |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2972 | if (send) { |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2973 | if (fd_trans_target_to_host_data(fd)) { |
| 2974 | ret = fd_trans_target_to_host_data(fd)(msg.msg_iov->iov_base, |
| 2975 | msg.msg_iov->iov_len); |
| 2976 | } else { |
| 2977 | ret = target_to_host_cmsg(&msg, msgp); |
| 2978 | } |
| 2979 | if (ret == 0) { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 2980 | ret = get_errno(safe_sendmsg(fd, &msg, flags)); |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2981 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 2982 | } else { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 2983 | ret = get_errno(safe_recvmsg(fd, &msg, flags)); |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 2984 | if (!is_error(ret)) { |
| 2985 | len = ret; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 2986 | if (fd_trans_host_to_target_data(fd)) { |
| 2987 | ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base, |
| 2988 | msg.msg_iov->iov_len); |
| 2989 | } else { |
| 2990 | ret = host_to_target_cmsg(msgp, &msg); |
| 2991 | } |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 2992 | if (!is_error(ret)) { |
| 2993 | msgp->msg_namelen = tswap32(msg.msg_namelen); |
| 2994 | if (msg.msg_name != NULL) { |
| 2995 | ret = host_to_target_sockaddr(tswapal(msgp->msg_name), |
| 2996 | msg.msg_name, msg.msg_namelen); |
| 2997 | if (ret) { |
| 2998 | goto out; |
| 2999 | } |
| 3000 | } |
| 3001 | |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3002 | ret = len; |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3003 | } |
balrog | 6de645c | 2008-10-28 10:26:29 +0000 | [diff] [blame] | 3004 | } |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3005 | } |
Jing Huang | ca61906 | 2012-07-24 13:58:02 +0000 | [diff] [blame] | 3006 | |
| 3007 | out: |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3008 | unlock_iovec(vec, target_vec, count, !send); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 3009 | out2: |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3010 | return ret; |
| 3011 | } |
| 3012 | |
| 3013 | static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg, |
| 3014 | int flags, int send) |
| 3015 | { |
| 3016 | abi_long ret; |
| 3017 | struct target_msghdr *msgp; |
| 3018 | |
| 3019 | if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE, |
| 3020 | msgp, |
| 3021 | target_msg, |
| 3022 | send ? 1 : 0)) { |
| 3023 | return -TARGET_EFAULT; |
| 3024 | } |
| 3025 | ret = do_sendrecvmsg_locked(fd, msgp, flags, send); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3026 | unlock_user_struct(msgp, target_msg, send ? 0 : 1); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 3027 | return ret; |
| 3028 | } |
| 3029 | |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3030 | /* We don't rely on the C library to have sendmmsg/recvmmsg support, |
| 3031 | * so it might not have this *mmsg-specific flag either. |
| 3032 | */ |
| 3033 | #ifndef MSG_WAITFORONE |
| 3034 | #define MSG_WAITFORONE 0x10000 |
| 3035 | #endif |
| 3036 | |
| 3037 | static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec, |
| 3038 | unsigned int vlen, unsigned int flags, |
| 3039 | int send) |
| 3040 | { |
| 3041 | struct target_mmsghdr *mmsgp; |
| 3042 | abi_long ret = 0; |
| 3043 | int i; |
| 3044 | |
| 3045 | if (vlen > UIO_MAXIOV) { |
| 3046 | vlen = UIO_MAXIOV; |
| 3047 | } |
| 3048 | |
| 3049 | mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1); |
| 3050 | if (!mmsgp) { |
| 3051 | return -TARGET_EFAULT; |
| 3052 | } |
| 3053 | |
| 3054 | for (i = 0; i < vlen; i++) { |
| 3055 | ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send); |
| 3056 | if (is_error(ret)) { |
| 3057 | break; |
| 3058 | } |
| 3059 | mmsgp[i].msg_len = tswap32(ret); |
| 3060 | /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ |
| 3061 | if (flags & MSG_WAITFORONE) { |
| 3062 | flags |= MSG_DONTWAIT; |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i); |
| 3067 | |
| 3068 | /* Return number of datagrams sent if we sent any at all; |
| 3069 | * otherwise return the error. |
| 3070 | */ |
| 3071 | if (i) { |
| 3072 | return i; |
| 3073 | } |
| 3074 | return ret; |
| 3075 | } |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 3076 | |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3077 | /* do_accept4() Must return target values and target errnos. */ |
| 3078 | static abi_long do_accept4(int fd, abi_ulong target_addr, |
| 3079 | abi_ulong target_addrlen_addr, int flags) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3080 | { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3081 | socklen_t addrlen; |
| 3082 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3083 | abi_long ret; |
Petar Jovanovic | d25295d | 2014-03-31 17:41:23 +0200 | [diff] [blame] | 3084 | int host_flags; |
| 3085 | |
| 3086 | host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3087 | |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3088 | if (target_addr == 0) { |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 3089 | return get_errno(safe_accept4(fd, NULL, NULL, host_flags)); |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 3090 | } |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3091 | |
| 3092 | /* linux returns EINVAL if addrlen pointer is invalid */ |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3093 | if (get_user_u32(addrlen, target_addrlen_addr)) |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3094 | return -TARGET_EINVAL; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3095 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3096 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3097 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3098 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3099 | |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3100 | if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) |
| 3101 | return -TARGET_EINVAL; |
| 3102 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3103 | addr = alloca(addrlen); |
| 3104 | |
Peter Maydell | ff6dc13 | 2016-06-06 19:58:13 +0100 | [diff] [blame] | 3105 | ret = get_errno(safe_accept4(fd, addr, &addrlen, host_flags)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3106 | if (!is_error(ret)) { |
| 3107 | host_to_target_sockaddr(target_addr, addr, addrlen); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3108 | if (put_user_u32(addrlen, target_addrlen_addr)) |
| 3109 | ret = -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3110 | } |
| 3111 | return ret; |
| 3112 | } |
| 3113 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3114 | /* do_getpeername() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3115 | static abi_long do_getpeername(int fd, abi_ulong target_addr, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3116 | abi_ulong target_addrlen_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3117 | { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3118 | socklen_t addrlen; |
| 3119 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3120 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3121 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3122 | if (get_user_u32(addrlen, target_addrlen_addr)) |
| 3123 | return -TARGET_EFAULT; |
| 3124 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3125 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3126 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3127 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3128 | |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3129 | if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) |
| 3130 | return -TARGET_EFAULT; |
| 3131 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3132 | addr = alloca(addrlen); |
| 3133 | |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3134 | ret = get_errno(getpeername(fd, addr, &addrlen)); |
| 3135 | if (!is_error(ret)) { |
| 3136 | host_to_target_sockaddr(target_addr, addr, addrlen); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3137 | if (put_user_u32(addrlen, target_addrlen_addr)) |
| 3138 | ret = -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3139 | } |
| 3140 | return ret; |
| 3141 | } |
| 3142 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3143 | /* do_getsockname() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3144 | static abi_long do_getsockname(int fd, abi_ulong target_addr, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3145 | abi_ulong target_addrlen_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3146 | { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3147 | socklen_t addrlen; |
| 3148 | void *addr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3149 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3150 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3151 | if (get_user_u32(addrlen, target_addrlen_addr)) |
| 3152 | return -TARGET_EFAULT; |
| 3153 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3154 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3155 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3156 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3157 | |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3158 | if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) |
| 3159 | return -TARGET_EFAULT; |
| 3160 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3161 | addr = alloca(addrlen); |
| 3162 | |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3163 | ret = get_errno(getsockname(fd, addr, &addrlen)); |
| 3164 | if (!is_error(ret)) { |
| 3165 | host_to_target_sockaddr(target_addr, addr, addrlen); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3166 | if (put_user_u32(addrlen, target_addrlen_addr)) |
| 3167 | ret = -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3168 | } |
| 3169 | return ret; |
| 3170 | } |
| 3171 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3172 | /* do_socketpair() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3173 | static abi_long do_socketpair(int domain, int type, int protocol, |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3174 | abi_ulong target_tab_addr) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3175 | { |
| 3176 | int tab[2]; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3177 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3178 | |
Petar Jovanovic | f651e6a | 2013-07-01 02:44:14 +0200 | [diff] [blame] | 3179 | target_to_host_sock_type(&type); |
| 3180 | |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3181 | ret = get_errno(socketpair(domain, type, protocol, tab)); |
| 3182 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3183 | if (put_user_s32(tab[0], target_tab_addr) |
| 3184 | || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0]))) |
| 3185 | ret = -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3186 | } |
| 3187 | return ret; |
| 3188 | } |
| 3189 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3190 | /* do_sendto() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3191 | static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags, |
| 3192 | abi_ulong target_addr, socklen_t addrlen) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3193 | { |
| 3194 | void *addr; |
| 3195 | void *host_msg; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3196 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3197 | |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3198 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3199 | return -TARGET_EINVAL; |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3200 | } |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3201 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3202 | host_msg = lock_user(VERIFY_READ, msg, len, 1); |
| 3203 | if (!host_msg) |
| 3204 | return -TARGET_EFAULT; |
Laurent Vivier | 6c5b564 | 2016-05-22 18:56:19 +0200 | [diff] [blame] | 3205 | if (fd_trans_target_to_host_data(fd)) { |
| 3206 | ret = fd_trans_target_to_host_data(fd)(host_msg, len); |
| 3207 | if (ret < 0) { |
| 3208 | unlock_user(host_msg, msg, 0); |
| 3209 | return ret; |
| 3210 | } |
| 3211 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3212 | if (target_addr) { |
Joakim Tjernlund | 2dd08df | 2014-07-11 17:18:03 +0200 | [diff] [blame] | 3213 | addr = alloca(addrlen+1); |
Laurent Vivier | 7b36f78 | 2015-10-28 21:40:44 +0100 | [diff] [blame] | 3214 | ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen); |
Arnaud Patard | 917507b | 2009-06-19 10:44:45 +0300 | [diff] [blame] | 3215 | if (ret) { |
| 3216 | unlock_user(host_msg, msg, 0); |
| 3217 | return ret; |
| 3218 | } |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3219 | ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3220 | } else { |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3221 | ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3222 | } |
| 3223 | unlock_user(host_msg, msg, 0); |
| 3224 | return ret; |
| 3225 | } |
| 3226 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3227 | /* do_recvfrom() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3228 | static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags, |
| 3229 | abi_ulong target_addr, |
| 3230 | abi_ulong target_addrlen) |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3231 | { |
| 3232 | socklen_t addrlen; |
| 3233 | void *addr; |
| 3234 | void *host_msg; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3235 | abi_long ret; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3236 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3237 | host_msg = lock_user(VERIFY_WRITE, msg, len, 0); |
| 3238 | if (!host_msg) |
| 3239 | return -TARGET_EFAULT; |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3240 | if (target_addr) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3241 | if (get_user_u32(addrlen, target_addrlen)) { |
| 3242 | ret = -TARGET_EFAULT; |
| 3243 | goto fail; |
| 3244 | } |
Blue Swirl | 3872425 | 2010-09-18 05:53:14 +0000 | [diff] [blame] | 3245 | if ((int)addrlen < 0) { |
aurel32 | 8f7aeaf | 2009-01-30 19:47:57 +0000 | [diff] [blame] | 3246 | ret = -TARGET_EINVAL; |
| 3247 | goto fail; |
| 3248 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3249 | addr = alloca(addrlen); |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3250 | ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, |
| 3251 | addr, &addrlen)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3252 | } else { |
| 3253 | addr = NULL; /* To keep compiler quiet. */ |
Peter Maydell | 6668753 | 2016-06-06 19:58:04 +0100 | [diff] [blame] | 3254 | ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0)); |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3255 | } |
| 3256 | if (!is_error(ret)) { |
| 3257 | if (target_addr) { |
| 3258 | host_to_target_sockaddr(target_addr, addr, addrlen); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3259 | if (put_user_u32(addrlen, target_addrlen)) { |
| 3260 | ret = -TARGET_EFAULT; |
| 3261 | goto fail; |
| 3262 | } |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3263 | } |
| 3264 | unlock_user(host_msg, msg, len); |
| 3265 | } else { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3266 | fail: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 3267 | unlock_user(host_msg, msg, 0); |
| 3268 | } |
| 3269 | return ret; |
| 3270 | } |
| 3271 | |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 3272 | #ifdef TARGET_NR_socketcall |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 3273 | /* do_socketcall() Must return target values and target errnos. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3274 | static abi_long do_socketcall(int num, abi_ulong vptr) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3275 | { |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3276 | static const unsigned ac[] = { /* number of arguments per call */ |
| 3277 | [SOCKOP_socket] = 3, /* domain, type, protocol */ |
| 3278 | [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */ |
| 3279 | [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */ |
| 3280 | [SOCKOP_listen] = 2, /* sockfd, backlog */ |
| 3281 | [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */ |
| 3282 | [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */ |
| 3283 | [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */ |
| 3284 | [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */ |
| 3285 | [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */ |
| 3286 | [SOCKOP_send] = 4, /* sockfd, msg, len, flags */ |
| 3287 | [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */ |
| 3288 | [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */ |
| 3289 | [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */ |
| 3290 | [SOCKOP_shutdown] = 2, /* sockfd, how */ |
| 3291 | [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */ |
| 3292 | [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */ |
John Paul Adrian Glaubitz | 5a53dc5 | 2015-12-24 19:59:59 +0100 | [diff] [blame] | 3293 | [SOCKOP_sendmmsg] = 4, /* sockfd, msgvec, vlen, flags */ |
| 3294 | [SOCKOP_recvmmsg] = 4, /* sockfd, msgvec, vlen, flags */ |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3295 | [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */ |
| 3296 | [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */ |
| 3297 | }; |
| 3298 | abi_long a[6]; /* max 6 args */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3299 | |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3300 | /* first, collect the arguments in a[] according to ac[] */ |
| 3301 | if (num >= 0 && num < ARRAY_SIZE(ac)) { |
| 3302 | unsigned i; |
| 3303 | assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */ |
| 3304 | for (i = 0; i < ac[num]; ++i) { |
| 3305 | if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) { |
André Hentschel | b9d36eb | 2014-01-06 20:18:52 +0100 | [diff] [blame] | 3306 | return -TARGET_EFAULT; |
| 3307 | } |
André Hentschel | b9d36eb | 2014-01-06 20:18:52 +0100 | [diff] [blame] | 3308 | } |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3309 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 3310 | |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3311 | /* now when we have the args, actually handle the call */ |
| 3312 | switch (num) { |
| 3313 | case SOCKOP_socket: /* domain, type, protocol */ |
| 3314 | return do_socket(a[0], a[1], a[2]); |
| 3315 | case SOCKOP_bind: /* sockfd, addr, addrlen */ |
| 3316 | return do_bind(a[0], a[1], a[2]); |
| 3317 | case SOCKOP_connect: /* sockfd, addr, addrlen */ |
| 3318 | return do_connect(a[0], a[1], a[2]); |
| 3319 | case SOCKOP_listen: /* sockfd, backlog */ |
| 3320 | return get_errno(listen(a[0], a[1])); |
| 3321 | case SOCKOP_accept: /* sockfd, addr, addrlen */ |
| 3322 | return do_accept4(a[0], a[1], a[2], 0); |
| 3323 | case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */ |
| 3324 | return do_accept4(a[0], a[1], a[2], a[3]); |
| 3325 | case SOCKOP_getsockname: /* sockfd, addr, addrlen */ |
| 3326 | return do_getsockname(a[0], a[1], a[2]); |
| 3327 | case SOCKOP_getpeername: /* sockfd, addr, addrlen */ |
| 3328 | return do_getpeername(a[0], a[1], a[2]); |
| 3329 | case SOCKOP_socketpair: /* domain, type, protocol, tab */ |
| 3330 | return do_socketpair(a[0], a[1], a[2], a[3]); |
| 3331 | case SOCKOP_send: /* sockfd, msg, len, flags */ |
| 3332 | return do_sendto(a[0], a[1], a[2], a[3], 0, 0); |
| 3333 | case SOCKOP_recv: /* sockfd, msg, len, flags */ |
| 3334 | return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0); |
| 3335 | case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */ |
| 3336 | return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 3337 | case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */ |
| 3338 | return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 3339 | case SOCKOP_shutdown: /* sockfd, how */ |
| 3340 | return get_errno(shutdown(a[0], a[1])); |
| 3341 | case SOCKOP_sendmsg: /* sockfd, msg, flags */ |
| 3342 | return do_sendrecvmsg(a[0], a[1], a[2], 1); |
| 3343 | case SOCKOP_recvmsg: /* sockfd, msg, flags */ |
| 3344 | return do_sendrecvmsg(a[0], a[1], a[2], 0); |
John Paul Adrian Glaubitz | 5a53dc5 | 2015-12-24 19:59:59 +0100 | [diff] [blame] | 3345 | case SOCKOP_sendmmsg: /* sockfd, msgvec, vlen, flags */ |
| 3346 | return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1); |
| 3347 | case SOCKOP_recvmmsg: /* sockfd, msgvec, vlen, flags */ |
| 3348 | return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0); |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3349 | case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */ |
| 3350 | return do_setsockopt(a[0], a[1], a[2], a[3], a[4]); |
| 3351 | case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */ |
| 3352 | return do_getsockopt(a[0], a[1], a[2], a[3], a[4]); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3353 | default: |
| 3354 | gemu_log("Unsupported socketcall: %d\n", num); |
Michael Tokarev | 62dc90c | 2014-01-17 14:23:51 +0400 | [diff] [blame] | 3355 | return -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3356 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3357 | } |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 3358 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3359 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 3360 | #define N_SHM_REGIONS 32 |
| 3361 | |
| 3362 | static struct shm_region { |
Peter Maydell | b6e1787 | 2016-02-09 15:57:11 +0000 | [diff] [blame] | 3363 | abi_ulong start; |
| 3364 | abi_ulong size; |
| 3365 | bool in_use; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 3366 | } shm_regions[N_SHM_REGIONS]; |
| 3367 | |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3368 | struct target_semid_ds |
| 3369 | { |
| 3370 | struct target_ipc_perm sem_perm; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3371 | abi_ulong sem_otime; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3372 | #if !defined(TARGET_PPC64) |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3373 | abi_ulong __unused1; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3374 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3375 | abi_ulong sem_ctime; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3376 | #if !defined(TARGET_PPC64) |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3377 | abi_ulong __unused2; |
Tom Musta | 0352734 | 2014-08-12 13:53:32 -0500 | [diff] [blame] | 3378 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3379 | abi_ulong sem_nsems; |
| 3380 | abi_ulong __unused3; |
| 3381 | abi_ulong __unused4; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3382 | }; |
| 3383 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3384 | static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip, |
| 3385 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3386 | { |
| 3387 | struct target_ipc_perm *target_ip; |
| 3388 | struct target_semid_ds *target_sd; |
| 3389 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3390 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3391 | return -TARGET_EFAULT; |
Michael S. Tsirkin | e8bbe36 | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 3392 | target_ip = &(target_sd->sem_perm); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3393 | host_ip->__key = tswap32(target_ip->__key); |
| 3394 | host_ip->uid = tswap32(target_ip->uid); |
| 3395 | host_ip->gid = tswap32(target_ip->gid); |
| 3396 | host_ip->cuid = tswap32(target_ip->cuid); |
| 3397 | host_ip->cgid = tswap32(target_ip->cgid); |
| 3398 | #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) |
| 3399 | host_ip->mode = tswap32(target_ip->mode); |
| 3400 | #else |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3401 | host_ip->mode = tswap16(target_ip->mode); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3402 | #endif |
| 3403 | #if defined(TARGET_PPC) |
| 3404 | host_ip->__seq = tswap32(target_ip->__seq); |
| 3405 | #else |
| 3406 | host_ip->__seq = tswap16(target_ip->__seq); |
| 3407 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3408 | unlock_user_struct(target_sd, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3409 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3412 | static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr, |
| 3413 | struct ipc_perm *host_ip) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3414 | { |
| 3415 | struct target_ipc_perm *target_ip; |
| 3416 | struct target_semid_ds *target_sd; |
| 3417 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3418 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3419 | return -TARGET_EFAULT; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3420 | target_ip = &(target_sd->sem_perm); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3421 | target_ip->__key = tswap32(host_ip->__key); |
| 3422 | target_ip->uid = tswap32(host_ip->uid); |
| 3423 | target_ip->gid = tswap32(host_ip->gid); |
| 3424 | target_ip->cuid = tswap32(host_ip->cuid); |
| 3425 | target_ip->cgid = tswap32(host_ip->cgid); |
| 3426 | #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) |
| 3427 | target_ip->mode = tswap32(host_ip->mode); |
| 3428 | #else |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3429 | target_ip->mode = tswap16(host_ip->mode); |
Petar Jovanovic | 55a2b16 | 2013-10-30 14:46:31 +0100 | [diff] [blame] | 3430 | #endif |
| 3431 | #if defined(TARGET_PPC) |
| 3432 | target_ip->__seq = tswap32(host_ip->__seq); |
| 3433 | #else |
| 3434 | target_ip->__seq = tswap16(host_ip->__seq); |
| 3435 | #endif |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3436 | unlock_user_struct(target_sd, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3437 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3440 | static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd, |
| 3441 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3442 | { |
| 3443 | struct target_semid_ds *target_sd; |
| 3444 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3445 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3446 | return -TARGET_EFAULT; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3447 | if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr)) |
| 3448 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3449 | host_sd->sem_nsems = tswapal(target_sd->sem_nsems); |
| 3450 | host_sd->sem_otime = tswapal(target_sd->sem_otime); |
| 3451 | host_sd->sem_ctime = tswapal(target_sd->sem_ctime); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3452 | unlock_user_struct(target_sd, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3453 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3456 | static inline abi_long host_to_target_semid_ds(abi_ulong target_addr, |
| 3457 | struct semid_ds *host_sd) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3458 | { |
| 3459 | struct target_semid_ds *target_sd; |
| 3460 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3461 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3462 | return -TARGET_EFAULT; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3463 | 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] | 3464 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3465 | target_sd->sem_nsems = tswapal(host_sd->sem_nsems); |
| 3466 | target_sd->sem_otime = tswapal(host_sd->sem_otime); |
| 3467 | target_sd->sem_ctime = tswapal(host_sd->sem_ctime); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3468 | unlock_user_struct(target_sd, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3469 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3472 | struct target_seminfo { |
| 3473 | int semmap; |
| 3474 | int semmni; |
| 3475 | int semmns; |
| 3476 | int semmnu; |
| 3477 | int semmsl; |
| 3478 | int semopm; |
| 3479 | int semume; |
| 3480 | int semusz; |
| 3481 | int semvmx; |
| 3482 | int semaem; |
| 3483 | }; |
| 3484 | |
| 3485 | static inline abi_long host_to_target_seminfo(abi_ulong target_addr, |
| 3486 | struct seminfo *host_seminfo) |
| 3487 | { |
| 3488 | struct target_seminfo *target_seminfo; |
| 3489 | if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0)) |
| 3490 | return -TARGET_EFAULT; |
| 3491 | __put_user(host_seminfo->semmap, &target_seminfo->semmap); |
| 3492 | __put_user(host_seminfo->semmni, &target_seminfo->semmni); |
| 3493 | __put_user(host_seminfo->semmns, &target_seminfo->semmns); |
| 3494 | __put_user(host_seminfo->semmnu, &target_seminfo->semmnu); |
| 3495 | __put_user(host_seminfo->semmsl, &target_seminfo->semmsl); |
| 3496 | __put_user(host_seminfo->semopm, &target_seminfo->semopm); |
| 3497 | __put_user(host_seminfo->semume, &target_seminfo->semume); |
| 3498 | __put_user(host_seminfo->semusz, &target_seminfo->semusz); |
| 3499 | __put_user(host_seminfo->semvmx, &target_seminfo->semvmx); |
| 3500 | __put_user(host_seminfo->semaem, &target_seminfo->semaem); |
| 3501 | unlock_user_struct(target_seminfo, target_addr, 1); |
| 3502 | return 0; |
| 3503 | } |
| 3504 | |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3505 | union semun { |
| 3506 | int val; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3507 | struct semid_ds *buf; |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3508 | unsigned short *array; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3509 | struct seminfo *__buf; |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 3510 | }; |
| 3511 | |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3512 | union target_semun { |
| 3513 | int val; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3514 | abi_ulong buf; |
| 3515 | abi_ulong array; |
| 3516 | abi_ulong __buf; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3517 | }; |
| 3518 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3519 | static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array, |
| 3520 | abi_ulong target_addr) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3521 | { |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3522 | int nsems; |
| 3523 | unsigned short *array; |
| 3524 | union semun semun; |
| 3525 | struct semid_ds semid_ds; |
| 3526 | int i, ret; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3527 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3528 | semun.buf = &semid_ds; |
| 3529 | |
| 3530 | ret = semctl(semid, 0, IPC_STAT, semun); |
| 3531 | if (ret == -1) |
| 3532 | return get_errno(ret); |
| 3533 | |
| 3534 | nsems = semid_ds.sem_nsems; |
| 3535 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3536 | *host_array = g_try_new(unsigned short, nsems); |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3537 | if (!*host_array) { |
| 3538 | return -TARGET_ENOMEM; |
| 3539 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3540 | array = lock_user(VERIFY_READ, target_addr, |
| 3541 | nsems*sizeof(unsigned short), 1); |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3542 | if (!array) { |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3543 | g_free(*host_array); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3544 | return -TARGET_EFAULT; |
Peter Maydell | 69d4c70 | 2014-02-17 18:55:34 +0000 | [diff] [blame] | 3545 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3546 | |
| 3547 | for(i=0; i<nsems; i++) { |
| 3548 | __get_user((*host_array)[i], &array[i]); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3549 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3550 | unlock_user(array, target_addr, 0); |
| 3551 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3552 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3555 | static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr, |
| 3556 | unsigned short **host_array) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3557 | { |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3558 | int nsems; |
| 3559 | unsigned short *array; |
| 3560 | union semun semun; |
| 3561 | struct semid_ds semid_ds; |
| 3562 | int i, ret; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3563 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3564 | semun.buf = &semid_ds; |
| 3565 | |
| 3566 | ret = semctl(semid, 0, IPC_STAT, semun); |
| 3567 | if (ret == -1) |
| 3568 | return get_errno(ret); |
| 3569 | |
| 3570 | nsems = semid_ds.sem_nsems; |
| 3571 | |
| 3572 | array = lock_user(VERIFY_WRITE, target_addr, |
| 3573 | nsems*sizeof(unsigned short), 0); |
| 3574 | if (!array) |
| 3575 | return -TARGET_EFAULT; |
| 3576 | |
| 3577 | for(i=0; i<nsems; i++) { |
| 3578 | __put_user((*host_array)[i], &array[i]); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3579 | } |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3580 | g_free(*host_array); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3581 | unlock_user(array, target_addr, 1); |
| 3582 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3583 | return 0; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3586 | static inline abi_long do_semctl(int semid, int semnum, int cmd, |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 3587 | abi_ulong target_arg) |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3588 | { |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 3589 | union target_semun target_su = { .buf = target_arg }; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3590 | union semun arg; |
| 3591 | struct semid_ds dsarg; |
vibi sreenivasan | 7b8118e | 2009-06-19 13:34:39 +0530 | [diff] [blame] | 3592 | unsigned short *array = NULL; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3593 | struct seminfo seminfo; |
| 3594 | abi_long ret = -TARGET_EINVAL; |
| 3595 | abi_long err; |
| 3596 | cmd &= 0xff; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3597 | |
| 3598 | switch( cmd ) { |
| 3599 | case GETVAL: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3600 | case SETVAL: |
Tom Musta | 5464bae | 2014-08-12 13:53:34 -0500 | [diff] [blame] | 3601 | /* In 64 bit cross-endian situations, we will erroneously pick up |
| 3602 | * the wrong half of the union for the "val" element. To rectify |
| 3603 | * this, the entire 8-byte structure is byteswapped, followed by |
| 3604 | * a swap of the 4 byte val field. In other cases, the data is |
| 3605 | * already in proper host byte order. */ |
| 3606 | if (sizeof(target_su.val) != (sizeof(target_su.buf))) { |
| 3607 | target_su.buf = tswapal(target_su.buf); |
| 3608 | arg.val = tswap32(target_su.val); |
| 3609 | } else { |
| 3610 | arg.val = target_su.val; |
| 3611 | } |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3612 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3613 | break; |
| 3614 | case GETALL: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3615 | case SETALL: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3616 | err = target_to_host_semarray(semid, &array, target_su.array); |
| 3617 | if (err) |
| 3618 | return err; |
| 3619 | arg.array = array; |
| 3620 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3621 | err = host_to_target_semarray(semid, target_su.array, &array); |
| 3622 | if (err) |
| 3623 | return err; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3624 | break; |
| 3625 | case IPC_STAT: |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3626 | case IPC_SET: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3627 | case SEM_STAT: |
| 3628 | err = target_to_host_semid_ds(&dsarg, target_su.buf); |
| 3629 | if (err) |
| 3630 | return err; |
| 3631 | arg.buf = &dsarg; |
| 3632 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3633 | err = host_to_target_semid_ds(target_su.buf, &dsarg); |
| 3634 | if (err) |
| 3635 | return err; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3636 | break; |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3637 | case IPC_INFO: |
| 3638 | case SEM_INFO: |
| 3639 | arg.__buf = &seminfo; |
| 3640 | ret = get_errno(semctl(semid, semnum, cmd, arg)); |
| 3641 | err = host_to_target_seminfo(target_su.__buf, &seminfo); |
| 3642 | if (err) |
| 3643 | return err; |
| 3644 | break; |
| 3645 | case IPC_RMID: |
| 3646 | case GETPID: |
| 3647 | case GETNCNT: |
| 3648 | case GETZCNT: |
| 3649 | ret = get_errno(semctl(semid, semnum, cmd, NULL)); |
| 3650 | break; |
ths | 3eb6b04 | 2007-06-03 14:26:27 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | return ret; |
| 3654 | } |
| 3655 | |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3656 | struct target_sembuf { |
| 3657 | unsigned short sem_num; |
| 3658 | short sem_op; |
| 3659 | short sem_flg; |
| 3660 | }; |
| 3661 | |
| 3662 | static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, |
| 3663 | abi_ulong target_addr, |
| 3664 | unsigned nsops) |
| 3665 | { |
| 3666 | struct target_sembuf *target_sembuf; |
| 3667 | int i; |
| 3668 | |
| 3669 | target_sembuf = lock_user(VERIFY_READ, target_addr, |
| 3670 | nsops*sizeof(struct target_sembuf), 1); |
| 3671 | if (!target_sembuf) |
| 3672 | return -TARGET_EFAULT; |
| 3673 | |
| 3674 | for(i=0; i<nsops; i++) { |
| 3675 | __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num); |
| 3676 | __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op); |
| 3677 | __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg); |
| 3678 | } |
| 3679 | |
| 3680 | unlock_user(target_sembuf, target_addr, 0); |
| 3681 | |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
| 3685 | static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops) |
| 3686 | { |
| 3687 | struct sembuf sops[nsops]; |
| 3688 | |
| 3689 | if (target_to_host_sembuf(sops, ptr, nsops)) |
| 3690 | return -TARGET_EFAULT; |
| 3691 | |
Peter Maydell | ffb7ee7 | 2016-06-06 19:58:12 +0100 | [diff] [blame] | 3692 | return get_errno(safe_semtimedop(semid, sops, nsops, NULL)); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3695 | struct target_msqid_ds |
| 3696 | { |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3697 | struct target_ipc_perm msg_perm; |
| 3698 | abi_ulong msg_stime; |
| 3699 | #if TARGET_ABI_BITS == 32 |
| 3700 | abi_ulong __unused1; |
| 3701 | #endif |
| 3702 | abi_ulong msg_rtime; |
| 3703 | #if TARGET_ABI_BITS == 32 |
| 3704 | abi_ulong __unused2; |
| 3705 | #endif |
| 3706 | abi_ulong msg_ctime; |
| 3707 | #if TARGET_ABI_BITS == 32 |
| 3708 | abi_ulong __unused3; |
| 3709 | #endif |
| 3710 | abi_ulong __msg_cbytes; |
| 3711 | abi_ulong msg_qnum; |
| 3712 | abi_ulong msg_qbytes; |
| 3713 | abi_ulong msg_lspid; |
| 3714 | abi_ulong msg_lrpid; |
| 3715 | abi_ulong __unused4; |
| 3716 | abi_ulong __unused5; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3717 | }; |
| 3718 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3719 | static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md, |
| 3720 | abi_ulong target_addr) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3721 | { |
| 3722 | struct target_msqid_ds *target_md; |
| 3723 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3724 | if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1)) |
| 3725 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3726 | if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr)) |
| 3727 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3728 | host_md->msg_stime = tswapal(target_md->msg_stime); |
| 3729 | host_md->msg_rtime = tswapal(target_md->msg_rtime); |
| 3730 | host_md->msg_ctime = tswapal(target_md->msg_ctime); |
| 3731 | host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes); |
| 3732 | host_md->msg_qnum = tswapal(target_md->msg_qnum); |
| 3733 | host_md->msg_qbytes = tswapal(target_md->msg_qbytes); |
| 3734 | host_md->msg_lspid = tswapal(target_md->msg_lspid); |
| 3735 | host_md->msg_lrpid = tswapal(target_md->msg_lrpid); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3736 | unlock_user_struct(target_md, target_addr, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3737 | return 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3740 | static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr, |
| 3741 | struct msqid_ds *host_md) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3742 | { |
| 3743 | struct target_msqid_ds *target_md; |
| 3744 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3745 | if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) |
| 3746 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3747 | if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm))) |
| 3748 | return -TARGET_EFAULT; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3749 | target_md->msg_stime = tswapal(host_md->msg_stime); |
| 3750 | target_md->msg_rtime = tswapal(host_md->msg_rtime); |
| 3751 | target_md->msg_ctime = tswapal(host_md->msg_ctime); |
| 3752 | target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes); |
| 3753 | target_md->msg_qnum = tswapal(host_md->msg_qnum); |
| 3754 | target_md->msg_qbytes = tswapal(host_md->msg_qbytes); |
| 3755 | target_md->msg_lspid = tswapal(host_md->msg_lspid); |
| 3756 | target_md->msg_lrpid = tswapal(host_md->msg_lrpid); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3757 | unlock_user_struct(target_md, target_addr, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3758 | return 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3761 | struct target_msginfo { |
| 3762 | int msgpool; |
| 3763 | int msgmap; |
| 3764 | int msgmax; |
| 3765 | int msgmnb; |
| 3766 | int msgmni; |
| 3767 | int msgssz; |
| 3768 | int msgtql; |
| 3769 | unsigned short int msgseg; |
| 3770 | }; |
| 3771 | |
| 3772 | static inline abi_long host_to_target_msginfo(abi_ulong target_addr, |
| 3773 | struct msginfo *host_msginfo) |
| 3774 | { |
| 3775 | struct target_msginfo *target_msginfo; |
| 3776 | if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0)) |
| 3777 | return -TARGET_EFAULT; |
| 3778 | __put_user(host_msginfo->msgpool, &target_msginfo->msgpool); |
| 3779 | __put_user(host_msginfo->msgmap, &target_msginfo->msgmap); |
| 3780 | __put_user(host_msginfo->msgmax, &target_msginfo->msgmax); |
| 3781 | __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb); |
| 3782 | __put_user(host_msginfo->msgmni, &target_msginfo->msgmni); |
| 3783 | __put_user(host_msginfo->msgssz, &target_msginfo->msgssz); |
| 3784 | __put_user(host_msginfo->msgtql, &target_msginfo->msgtql); |
| 3785 | __put_user(host_msginfo->msgseg, &target_msginfo->msgseg); |
| 3786 | unlock_user_struct(target_msginfo, target_addr, 1); |
aurel32 | 00b229a | 2008-10-24 13:12:52 +0000 | [diff] [blame] | 3787 | return 0; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3791 | { |
| 3792 | struct msqid_ds dsarg; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3793 | struct msginfo msginfo; |
| 3794 | abi_long ret = -TARGET_EINVAL; |
| 3795 | |
| 3796 | cmd &= 0xff; |
| 3797 | |
| 3798 | switch (cmd) { |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3799 | case IPC_STAT: |
| 3800 | case IPC_SET: |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3801 | case MSG_STAT: |
| 3802 | if (target_to_host_msqid_ds(&dsarg,ptr)) |
| 3803 | return -TARGET_EFAULT; |
| 3804 | ret = get_errno(msgctl(msgid, cmd, &dsarg)); |
| 3805 | if (host_to_target_msqid_ds(ptr,&dsarg)) |
| 3806 | return -TARGET_EFAULT; |
| 3807 | break; |
| 3808 | case IPC_RMID: |
| 3809 | ret = get_errno(msgctl(msgid, cmd, NULL)); |
| 3810 | break; |
| 3811 | case IPC_INFO: |
| 3812 | case MSG_INFO: |
| 3813 | ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo)); |
| 3814 | if (host_to_target_msginfo(ptr, &msginfo)) |
| 3815 | return -TARGET_EFAULT; |
| 3816 | break; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3817 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3818 | |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3819 | return ret; |
| 3820 | } |
| 3821 | |
| 3822 | struct target_msgbuf { |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3823 | abi_long mtype; |
| 3824 | char mtext[1]; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3825 | }; |
| 3826 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3827 | static inline abi_long do_msgsnd(int msqid, abi_long msgp, |
Tom Musta | edcc5f9 | 2014-08-12 13:53:37 -0500 | [diff] [blame] | 3828 | ssize_t msgsz, int msgflg) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3829 | { |
| 3830 | struct target_msgbuf *target_mb; |
| 3831 | struct msgbuf *host_mb; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3832 | abi_long ret = 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3833 | |
Tom Musta | edcc5f9 | 2014-08-12 13:53:37 -0500 | [diff] [blame] | 3834 | if (msgsz < 0) { |
| 3835 | return -TARGET_EINVAL; |
| 3836 | } |
| 3837 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3838 | if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0)) |
| 3839 | return -TARGET_EFAULT; |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3840 | host_mb = g_try_malloc(msgsz + sizeof(long)); |
zhanghailiang | 29e03fc | 2014-08-14 15:29:18 +0800 | [diff] [blame] | 3841 | if (!host_mb) { |
| 3842 | unlock_user_struct(target_mb, msgp, 0); |
| 3843 | return -TARGET_ENOMEM; |
| 3844 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3845 | host_mb->mtype = (abi_long) tswapal(target_mb->mtype); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3846 | memcpy(host_mb->mtext, target_mb->mtext, msgsz); |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 3847 | ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg)); |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 3848 | g_free(host_mb); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3849 | unlock_user_struct(target_mb, msgp, 0); |
| 3850 | |
| 3851 | return ret; |
| 3852 | } |
| 3853 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3854 | static inline abi_long do_msgrcv(int msqid, abi_long msgp, |
Peter Maydell | 99874f6 | 2016-05-20 19:00:56 +0100 | [diff] [blame] | 3855 | ssize_t msgsz, abi_long msgtyp, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3856 | int msgflg) |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3857 | { |
| 3858 | struct target_msgbuf *target_mb; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3859 | char *target_mtext; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3860 | struct msgbuf *host_mb; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 3861 | abi_long ret = 0; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3862 | |
Peter Maydell | 99874f6 | 2016-05-20 19:00:56 +0100 | [diff] [blame] | 3863 | if (msgsz < 0) { |
| 3864 | return -TARGET_EINVAL; |
| 3865 | } |
| 3866 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3867 | if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0)) |
| 3868 | return -TARGET_EFAULT; |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3869 | |
Peter Maydell | 415d847 | 2016-05-20 19:00:57 +0100 | [diff] [blame] | 3870 | host_mb = g_try_malloc(msgsz + sizeof(long)); |
| 3871 | if (!host_mb) { |
| 3872 | ret = -TARGET_ENOMEM; |
| 3873 | goto end; |
| 3874 | } |
Peter Maydell | 89f9fe4 | 2016-06-06 19:58:05 +0100 | [diff] [blame] | 3875 | ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg)); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3876 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3877 | if (ret > 0) { |
| 3878 | abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong); |
| 3879 | target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0); |
| 3880 | if (!target_mtext) { |
| 3881 | ret = -TARGET_EFAULT; |
| 3882 | goto end; |
| 3883 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3884 | memcpy(target_mb->mtext, host_mb->mtext, ret); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3885 | unlock_user(target_mtext, target_mtext_addr, ret); |
| 3886 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 3887 | |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 3888 | target_mb->mtype = tswapal(host_mb->mtype); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3889 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3890 | end: |
| 3891 | if (target_mb) |
| 3892 | unlock_user_struct(target_mb, msgp, 1); |
Jim Meyering | 0d07fe4 | 2012-08-22 13:55:53 +0200 | [diff] [blame] | 3893 | g_free(host_mb); |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 3894 | return ret; |
| 3895 | } |
| 3896 | |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 3897 | static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd, |
| 3898 | abi_ulong target_addr) |
| 3899 | { |
| 3900 | struct target_shmid_ds *target_sd; |
| 3901 | |
| 3902 | if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) |
| 3903 | return -TARGET_EFAULT; |
| 3904 | if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr)) |
| 3905 | return -TARGET_EFAULT; |
| 3906 | __get_user(host_sd->shm_segsz, &target_sd->shm_segsz); |
| 3907 | __get_user(host_sd->shm_atime, &target_sd->shm_atime); |
| 3908 | __get_user(host_sd->shm_dtime, &target_sd->shm_dtime); |
| 3909 | __get_user(host_sd->shm_ctime, &target_sd->shm_ctime); |
| 3910 | __get_user(host_sd->shm_cpid, &target_sd->shm_cpid); |
| 3911 | __get_user(host_sd->shm_lpid, &target_sd->shm_lpid); |
| 3912 | __get_user(host_sd->shm_nattch, &target_sd->shm_nattch); |
| 3913 | unlock_user_struct(target_sd, target_addr, 0); |
| 3914 | return 0; |
| 3915 | } |
| 3916 | |
| 3917 | static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr, |
| 3918 | struct shmid_ds *host_sd) |
| 3919 | { |
| 3920 | struct target_shmid_ds *target_sd; |
| 3921 | |
| 3922 | if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) |
| 3923 | return -TARGET_EFAULT; |
| 3924 | if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm))) |
| 3925 | return -TARGET_EFAULT; |
| 3926 | __put_user(host_sd->shm_segsz, &target_sd->shm_segsz); |
| 3927 | __put_user(host_sd->shm_atime, &target_sd->shm_atime); |
| 3928 | __put_user(host_sd->shm_dtime, &target_sd->shm_dtime); |
| 3929 | __put_user(host_sd->shm_ctime, &target_sd->shm_ctime); |
| 3930 | __put_user(host_sd->shm_cpid, &target_sd->shm_cpid); |
| 3931 | __put_user(host_sd->shm_lpid, &target_sd->shm_lpid); |
| 3932 | __put_user(host_sd->shm_nattch, &target_sd->shm_nattch); |
| 3933 | unlock_user_struct(target_sd, target_addr, 1); |
| 3934 | return 0; |
| 3935 | } |
| 3936 | |
| 3937 | struct target_shminfo { |
| 3938 | abi_ulong shmmax; |
| 3939 | abi_ulong shmmin; |
| 3940 | abi_ulong shmmni; |
| 3941 | abi_ulong shmseg; |
| 3942 | abi_ulong shmall; |
| 3943 | }; |
| 3944 | |
| 3945 | static inline abi_long host_to_target_shminfo(abi_ulong target_addr, |
| 3946 | struct shminfo *host_shminfo) |
| 3947 | { |
| 3948 | struct target_shminfo *target_shminfo; |
| 3949 | if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0)) |
| 3950 | return -TARGET_EFAULT; |
| 3951 | __put_user(host_shminfo->shmmax, &target_shminfo->shmmax); |
| 3952 | __put_user(host_shminfo->shmmin, &target_shminfo->shmmin); |
| 3953 | __put_user(host_shminfo->shmmni, &target_shminfo->shmmni); |
| 3954 | __put_user(host_shminfo->shmseg, &target_shminfo->shmseg); |
| 3955 | __put_user(host_shminfo->shmall, &target_shminfo->shmall); |
| 3956 | unlock_user_struct(target_shminfo, target_addr, 1); |
| 3957 | return 0; |
| 3958 | } |
| 3959 | |
| 3960 | struct target_shm_info { |
| 3961 | int used_ids; |
| 3962 | abi_ulong shm_tot; |
| 3963 | abi_ulong shm_rss; |
| 3964 | abi_ulong shm_swp; |
| 3965 | abi_ulong swap_attempts; |
| 3966 | abi_ulong swap_successes; |
| 3967 | }; |
| 3968 | |
| 3969 | static inline abi_long host_to_target_shm_info(abi_ulong target_addr, |
| 3970 | struct shm_info *host_shm_info) |
| 3971 | { |
| 3972 | struct target_shm_info *target_shm_info; |
| 3973 | if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0)) |
| 3974 | return -TARGET_EFAULT; |
| 3975 | __put_user(host_shm_info->used_ids, &target_shm_info->used_ids); |
| 3976 | __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot); |
| 3977 | __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss); |
| 3978 | __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp); |
| 3979 | __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts); |
| 3980 | __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes); |
| 3981 | unlock_user_struct(target_shm_info, target_addr, 1); |
| 3982 | return 0; |
| 3983 | } |
| 3984 | |
| 3985 | static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf) |
| 3986 | { |
| 3987 | struct shmid_ds dsarg; |
| 3988 | struct shminfo shminfo; |
| 3989 | struct shm_info shm_info; |
| 3990 | abi_long ret = -TARGET_EINVAL; |
| 3991 | |
| 3992 | cmd &= 0xff; |
| 3993 | |
| 3994 | switch(cmd) { |
| 3995 | case IPC_STAT: |
| 3996 | case IPC_SET: |
| 3997 | case SHM_STAT: |
| 3998 | if (target_to_host_shmid_ds(&dsarg, buf)) |
| 3999 | return -TARGET_EFAULT; |
| 4000 | ret = get_errno(shmctl(shmid, cmd, &dsarg)); |
| 4001 | if (host_to_target_shmid_ds(buf, &dsarg)) |
| 4002 | return -TARGET_EFAULT; |
| 4003 | break; |
| 4004 | case IPC_INFO: |
| 4005 | ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo)); |
| 4006 | if (host_to_target_shminfo(buf, &shminfo)) |
| 4007 | return -TARGET_EFAULT; |
| 4008 | break; |
| 4009 | case SHM_INFO: |
| 4010 | ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info)); |
| 4011 | if (host_to_target_shm_info(buf, &shm_info)) |
| 4012 | return -TARGET_EFAULT; |
| 4013 | break; |
| 4014 | case IPC_RMID: |
| 4015 | case SHM_LOCK: |
| 4016 | case SHM_UNLOCK: |
| 4017 | ret = get_errno(shmctl(shmid, cmd, NULL)); |
| 4018 | break; |
| 4019 | } |
| 4020 | |
| 4021 | return ret; |
| 4022 | } |
| 4023 | |
| 4024 | static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg) |
| 4025 | { |
| 4026 | abi_long raddr; |
| 4027 | void *host_raddr; |
| 4028 | struct shmid_ds shm_info; |
| 4029 | int i,ret; |
| 4030 | |
| 4031 | /* find out the length of the shared memory segment */ |
| 4032 | ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); |
| 4033 | if (is_error(ret)) { |
| 4034 | /* can't get length, bail out */ |
| 4035 | return ret; |
| 4036 | } |
| 4037 | |
| 4038 | mmap_lock(); |
| 4039 | |
| 4040 | if (shmaddr) |
| 4041 | host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg); |
| 4042 | else { |
| 4043 | abi_ulong mmap_start; |
| 4044 | |
| 4045 | mmap_start = mmap_find_vma(0, shm_info.shm_segsz); |
| 4046 | |
| 4047 | if (mmap_start == -1) { |
| 4048 | errno = ENOMEM; |
| 4049 | host_raddr = (void *)-1; |
| 4050 | } else |
| 4051 | host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP); |
| 4052 | } |
| 4053 | |
| 4054 | if (host_raddr == (void *)-1) { |
| 4055 | mmap_unlock(); |
| 4056 | return get_errno((long)host_raddr); |
| 4057 | } |
| 4058 | raddr=h2g((unsigned long)host_raddr); |
| 4059 | |
| 4060 | page_set_flags(raddr, raddr + shm_info.shm_segsz, |
| 4061 | PAGE_VALID | PAGE_READ | |
| 4062 | ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE)); |
| 4063 | |
| 4064 | for (i = 0; i < N_SHM_REGIONS; i++) { |
Peter Maydell | b6e1787 | 2016-02-09 15:57:11 +0000 | [diff] [blame] | 4065 | if (!shm_regions[i].in_use) { |
| 4066 | shm_regions[i].in_use = true; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4067 | shm_regions[i].start = raddr; |
| 4068 | shm_regions[i].size = shm_info.shm_segsz; |
| 4069 | break; |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | mmap_unlock(); |
| 4074 | return raddr; |
| 4075 | |
| 4076 | } |
| 4077 | |
| 4078 | static inline abi_long do_shmdt(abi_ulong shmaddr) |
| 4079 | { |
| 4080 | int i; |
| 4081 | |
| 4082 | for (i = 0; i < N_SHM_REGIONS; ++i) { |
Peter Maydell | b6e1787 | 2016-02-09 15:57:11 +0000 | [diff] [blame] | 4083 | if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { |
| 4084 | shm_regions[i].in_use = false; |
takasi-y@ops.dti.ne.jp | e00ac24 | 2010-04-11 02:09:57 +0900 | [diff] [blame] | 4085 | page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0); |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4086 | break; |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | return get_errno(shmdt(g2h(shmaddr))); |
| 4091 | } |
| 4092 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4093 | #ifdef TARGET_NR_ipc |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4094 | /* ??? This only works with linear mappings. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4095 | /* do_ipc() must return target values and target errnos. */ |
Tom Musta | 37ed095 | 2014-08-12 13:53:35 -0500 | [diff] [blame] | 4096 | static abi_long do_ipc(unsigned int call, abi_long first, |
| 4097 | abi_long second, abi_long third, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4098 | abi_long ptr, abi_long fifth) |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4099 | { |
| 4100 | int version; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4101 | abi_long ret = 0; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4102 | |
| 4103 | version = call >> 16; |
| 4104 | call &= 0xffff; |
| 4105 | |
| 4106 | switch (call) { |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4107 | case IPCOP_semop: |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 4108 | ret = do_semop(first, ptr, second); |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4109 | break; |
| 4110 | |
| 4111 | case IPCOP_semget: |
| 4112 | ret = get_errno(semget(first, second, third)); |
| 4113 | break; |
| 4114 | |
Tom Musta | 5d2fa8e | 2014-08-12 13:53:33 -0500 | [diff] [blame] | 4115 | case IPCOP_semctl: { |
| 4116 | /* The semun argument to semctl is passed by value, so dereference the |
| 4117 | * ptr argument. */ |
| 4118 | abi_ulong atptr; |
Tom Musta | 37ed095 | 2014-08-12 13:53:35 -0500 | [diff] [blame] | 4119 | get_user_ual(atptr, ptr); |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 4120 | ret = do_semctl(first, second, third, atptr); |
ths | fa29481 | 2007-02-02 22:05:00 +0000 | [diff] [blame] | 4121 | break; |
Tom Musta | 5d2fa8e | 2014-08-12 13:53:33 -0500 | [diff] [blame] | 4122 | } |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4123 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4124 | case IPCOP_msgget: |
| 4125 | ret = get_errno(msgget(first, second)); |
| 4126 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4127 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4128 | case IPCOP_msgsnd: |
| 4129 | ret = do_msgsnd(first, ptr, second, third); |
| 4130 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4131 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4132 | case IPCOP_msgctl: |
| 4133 | ret = do_msgctl(first, second, ptr); |
| 4134 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4135 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4136 | case IPCOP_msgrcv: |
| 4137 | switch (version) { |
| 4138 | case 0: |
| 4139 | { |
| 4140 | struct target_ipc_kludge { |
| 4141 | abi_long msgp; |
| 4142 | abi_long msgtyp; |
| 4143 | } *tmp; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4144 | |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4145 | if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) { |
| 4146 | ret = -TARGET_EFAULT; |
| 4147 | break; |
ths | 1bc012f | 2007-06-03 14:27:49 +0000 | [diff] [blame] | 4148 | } |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4149 | |
Laurent Vivier | 79dd77d | 2012-12-20 11:00:11 +0000 | [diff] [blame] | 4150 | ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third); |
aurel32 | 1c54ff9 | 2008-10-13 21:08:44 +0000 | [diff] [blame] | 4151 | |
| 4152 | unlock_user_struct(tmp, ptr, 0); |
| 4153 | break; |
| 4154 | } |
| 4155 | default: |
| 4156 | ret = do_msgrcv(first, ptr, second, fifth, third); |
| 4157 | } |
| 4158 | break; |
ths | d96372e | 2007-02-02 22:05:44 +0000 | [diff] [blame] | 4159 | |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4160 | case IPCOP_shmat: |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4161 | switch (version) { |
| 4162 | default: |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4163 | { |
| 4164 | abi_ulong raddr; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4165 | raddr = do_shmat(first, ptr, second); |
| 4166 | if (is_error(raddr)) |
| 4167 | return get_errno(raddr); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 4168 | if (put_user_ual(raddr, third)) |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4169 | return -TARGET_EFAULT; |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4170 | break; |
| 4171 | } |
| 4172 | case 1: |
| 4173 | ret = -TARGET_EINVAL; |
| 4174 | break; |
bellard | 5a4a898 | 2007-11-11 17:39:18 +0000 | [diff] [blame] | 4175 | } |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4176 | break; |
| 4177 | case IPCOP_shmdt: |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 4178 | ret = do_shmdt(ptr); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4179 | break; |
| 4180 | |
| 4181 | case IPCOP_shmget: |
| 4182 | /* IPC_* flag values are the same on all linux platforms */ |
| 4183 | ret = get_errno(shmget(first, second, third)); |
| 4184 | break; |
| 4185 | |
| 4186 | /* IPC_* and SHM_* command values are the same on all linux platforms */ |
| 4187 | case IPCOP_shmctl: |
Petar Jovanovic | a292678 | 2013-10-30 14:46:32 +0100 | [diff] [blame] | 4188 | ret = do_shmctl(first, second, ptr); |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4189 | break; |
| 4190 | default: |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4191 | gemu_log("Unsupported ipc call: %d (version %d)\n", call, version); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4192 | ret = -TARGET_ENOSYS; |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4193 | break; |
| 4194 | } |
| 4195 | return ret; |
| 4196 | } |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4197 | #endif |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 4198 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4199 | /* kernel structure types definitions */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4200 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4201 | #define STRUCT(name, ...) STRUCT_ ## name, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4202 | #define STRUCT_SPECIAL(name) STRUCT_ ## name, |
| 4203 | enum { |
| 4204 | #include "syscall_types.h" |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 4205 | STRUCT_MAX |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4206 | }; |
| 4207 | #undef STRUCT |
| 4208 | #undef STRUCT_SPECIAL |
| 4209 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4210 | #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL }; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4211 | #define STRUCT_SPECIAL(name) |
| 4212 | #include "syscall_types.h" |
| 4213 | #undef STRUCT |
| 4214 | #undef STRUCT_SPECIAL |
| 4215 | |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 4216 | typedef struct IOCTLEntry IOCTLEntry; |
| 4217 | |
| 4218 | typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp, |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 4219 | int fd, int cmd, abi_long arg); |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 4220 | |
| 4221 | struct IOCTLEntry { |
Ed Swierk | 9c6bf9c | 2014-12-16 12:55:31 -0800 | [diff] [blame] | 4222 | int target_cmd; |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 4223 | unsigned int host_cmd; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4224 | const char *name; |
| 4225 | int access; |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 4226 | do_ioctl_fn *do_ioctl; |
bellard | 1a9353d | 2003-03-16 20:28:50 +0000 | [diff] [blame] | 4227 | const argtype arg_type[5]; |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 4228 | }; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4229 | |
| 4230 | #define IOC_R 0x0001 |
| 4231 | #define IOC_W 0x0002 |
| 4232 | #define IOC_RW (IOC_R | IOC_W) |
| 4233 | |
| 4234 | #define MAX_STRUCT_SIZE 4096 |
| 4235 | |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 4236 | #ifdef CONFIG_FIEMAP |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4237 | /* So fiemap access checks don't overflow on 32 bit systems. |
| 4238 | * This is very slightly smaller than the limit imposed by |
| 4239 | * the underlying kernel. |
| 4240 | */ |
| 4241 | #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \ |
| 4242 | / sizeof(struct fiemap_extent)) |
| 4243 | |
| 4244 | 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] | 4245 | int fd, int cmd, abi_long arg) |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4246 | { |
| 4247 | /* The parameter for this ioctl is a struct fiemap followed |
| 4248 | * by an array of struct fiemap_extent whose size is set |
| 4249 | * in fiemap->fm_extent_count. The array is filled in by the |
| 4250 | * ioctl. |
| 4251 | */ |
| 4252 | int target_size_in, target_size_out; |
| 4253 | struct fiemap *fm; |
| 4254 | const argtype *arg_type = ie->arg_type; |
| 4255 | const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) }; |
| 4256 | void *argptr, *p; |
| 4257 | abi_long ret; |
| 4258 | int i, extent_size = thunk_type_size(extent_arg_type, 0); |
| 4259 | uint32_t outbufsz; |
| 4260 | int free_fm = 0; |
| 4261 | |
| 4262 | assert(arg_type[0] == TYPE_PTR); |
| 4263 | assert(ie->access == IOC_RW); |
| 4264 | arg_type++; |
| 4265 | target_size_in = thunk_type_size(arg_type, 0); |
| 4266 | argptr = lock_user(VERIFY_READ, arg, target_size_in, 1); |
| 4267 | if (!argptr) { |
| 4268 | return -TARGET_EFAULT; |
| 4269 | } |
| 4270 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4271 | unlock_user(argptr, arg, 0); |
| 4272 | fm = (struct fiemap *)buf_temp; |
| 4273 | if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) { |
| 4274 | return -TARGET_EINVAL; |
| 4275 | } |
| 4276 | |
| 4277 | outbufsz = sizeof (*fm) + |
| 4278 | (sizeof(struct fiemap_extent) * fm->fm_extent_count); |
| 4279 | |
| 4280 | if (outbufsz > MAX_STRUCT_SIZE) { |
| 4281 | /* We can't fit all the extents into the fixed size buffer. |
| 4282 | * Allocate one that is large enough and use it instead. |
| 4283 | */ |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4284 | fm = g_try_malloc(outbufsz); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4285 | if (!fm) { |
| 4286 | return -TARGET_ENOMEM; |
| 4287 | } |
| 4288 | memcpy(fm, buf_temp, sizeof(struct fiemap)); |
| 4289 | free_fm = 1; |
| 4290 | } |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4291 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm)); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4292 | if (!is_error(ret)) { |
| 4293 | target_size_out = target_size_in; |
| 4294 | /* An extent_count of 0 means we were only counting the extents |
| 4295 | * so there are no structs to copy |
| 4296 | */ |
| 4297 | if (fm->fm_extent_count != 0) { |
| 4298 | target_size_out += fm->fm_mapped_extents * extent_size; |
| 4299 | } |
| 4300 | argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0); |
| 4301 | if (!argptr) { |
| 4302 | ret = -TARGET_EFAULT; |
| 4303 | } else { |
| 4304 | /* Convert the struct fiemap */ |
| 4305 | thunk_convert(argptr, fm, arg_type, THUNK_TARGET); |
| 4306 | if (fm->fm_extent_count != 0) { |
| 4307 | p = argptr + target_size_in; |
| 4308 | /* ...and then all the struct fiemap_extents */ |
| 4309 | for (i = 0; i < fm->fm_mapped_extents; i++) { |
| 4310 | thunk_convert(p, &fm->fm_extents[i], extent_arg_type, |
| 4311 | THUNK_TARGET); |
| 4312 | p += extent_size; |
| 4313 | } |
| 4314 | } |
| 4315 | unlock_user(argptr, arg, target_size_out); |
| 4316 | } |
| 4317 | } |
| 4318 | if (free_fm) { |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 4319 | g_free(fm); |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4320 | } |
| 4321 | return ret; |
| 4322 | } |
Peter Maydell | dace20d | 2011-01-10 13:11:24 +0000 | [diff] [blame] | 4323 | #endif |
Peter Maydell | 285da2b | 2011-01-06 15:04:18 +0000 | [diff] [blame] | 4324 | |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4325 | 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] | 4326 | int fd, int cmd, abi_long arg) |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4327 | { |
| 4328 | const argtype *arg_type = ie->arg_type; |
| 4329 | int target_size; |
| 4330 | void *argptr; |
| 4331 | int ret; |
| 4332 | struct ifconf *host_ifconf; |
| 4333 | uint32_t outbufsz; |
| 4334 | const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) }; |
| 4335 | int target_ifreq_size; |
| 4336 | int nb_ifreq; |
| 4337 | int free_buf = 0; |
| 4338 | int i; |
| 4339 | int target_ifc_len; |
| 4340 | abi_long target_ifc_buf; |
| 4341 | int host_ifc_len; |
| 4342 | char *host_ifc_buf; |
| 4343 | |
| 4344 | assert(arg_type[0] == TYPE_PTR); |
| 4345 | assert(ie->access == IOC_RW); |
| 4346 | |
| 4347 | arg_type++; |
| 4348 | target_size = thunk_type_size(arg_type, 0); |
| 4349 | |
| 4350 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4351 | if (!argptr) |
| 4352 | return -TARGET_EFAULT; |
| 4353 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4354 | unlock_user(argptr, arg, 0); |
| 4355 | |
| 4356 | host_ifconf = (struct ifconf *)(unsigned long)buf_temp; |
| 4357 | target_ifc_len = host_ifconf->ifc_len; |
| 4358 | target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf; |
| 4359 | |
| 4360 | target_ifreq_size = thunk_type_size(ifreq_arg_type, 0); |
| 4361 | nb_ifreq = target_ifc_len / target_ifreq_size; |
| 4362 | host_ifc_len = nb_ifreq * sizeof(struct ifreq); |
| 4363 | |
| 4364 | outbufsz = sizeof(*host_ifconf) + host_ifc_len; |
| 4365 | if (outbufsz > MAX_STRUCT_SIZE) { |
| 4366 | /* We can't fit all the extents into the fixed size buffer. |
| 4367 | * Allocate one that is large enough and use it instead. |
| 4368 | */ |
| 4369 | host_ifconf = malloc(outbufsz); |
| 4370 | if (!host_ifconf) { |
| 4371 | return -TARGET_ENOMEM; |
| 4372 | } |
| 4373 | memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf)); |
| 4374 | free_buf = 1; |
| 4375 | } |
| 4376 | host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf); |
| 4377 | |
| 4378 | host_ifconf->ifc_len = host_ifc_len; |
| 4379 | host_ifconf->ifc_buf = host_ifc_buf; |
| 4380 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4381 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf)); |
Laurent Vivier | 059c2f2 | 2011-03-30 00:12:12 +0200 | [diff] [blame] | 4382 | if (!is_error(ret)) { |
| 4383 | /* convert host ifc_len to target ifc_len */ |
| 4384 | |
| 4385 | nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq); |
| 4386 | target_ifc_len = nb_ifreq * target_ifreq_size; |
| 4387 | host_ifconf->ifc_len = target_ifc_len; |
| 4388 | |
| 4389 | /* restore target ifc_buf */ |
| 4390 | |
| 4391 | host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf; |
| 4392 | |
| 4393 | /* copy struct ifconf to target user */ |
| 4394 | |
| 4395 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4396 | if (!argptr) |
| 4397 | return -TARGET_EFAULT; |
| 4398 | thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET); |
| 4399 | unlock_user(argptr, arg, target_size); |
| 4400 | |
| 4401 | /* copy ifreq[] to target user */ |
| 4402 | |
| 4403 | argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0); |
| 4404 | for (i = 0; i < nb_ifreq ; i++) { |
| 4405 | thunk_convert(argptr + i * target_ifreq_size, |
| 4406 | host_ifc_buf + i * sizeof(struct ifreq), |
| 4407 | ifreq_arg_type, THUNK_TARGET); |
| 4408 | } |
| 4409 | unlock_user(argptr, target_ifc_buf, target_ifc_len); |
| 4410 | } |
| 4411 | |
| 4412 | if (free_buf) { |
| 4413 | free(host_ifconf); |
| 4414 | } |
| 4415 | |
| 4416 | return ret; |
| 4417 | } |
| 4418 | |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4419 | 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] | 4420 | int cmd, abi_long arg) |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4421 | { |
| 4422 | void *argptr; |
| 4423 | struct dm_ioctl *host_dm; |
| 4424 | abi_long guest_data; |
| 4425 | uint32_t guest_data_size; |
| 4426 | int target_size; |
| 4427 | const argtype *arg_type = ie->arg_type; |
| 4428 | abi_long ret; |
| 4429 | void *big_buf = NULL; |
| 4430 | char *host_data; |
| 4431 | |
| 4432 | arg_type++; |
| 4433 | target_size = thunk_type_size(arg_type, 0); |
| 4434 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4435 | if (!argptr) { |
| 4436 | ret = -TARGET_EFAULT; |
| 4437 | goto out; |
| 4438 | } |
| 4439 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4440 | unlock_user(argptr, arg, 0); |
| 4441 | |
| 4442 | /* buf_temp is too small, so fetch things into a bigger buffer */ |
| 4443 | big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2); |
| 4444 | memcpy(big_buf, buf_temp, target_size); |
| 4445 | buf_temp = big_buf; |
| 4446 | host_dm = big_buf; |
| 4447 | |
| 4448 | guest_data = arg + host_dm->data_start; |
| 4449 | if ((guest_data - arg) < 0) { |
| 4450 | ret = -EINVAL; |
| 4451 | goto out; |
| 4452 | } |
| 4453 | guest_data_size = host_dm->data_size - host_dm->data_start; |
| 4454 | host_data = (char*)host_dm + host_dm->data_start; |
| 4455 | |
| 4456 | argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1); |
| 4457 | switch (ie->host_cmd) { |
| 4458 | case DM_REMOVE_ALL: |
| 4459 | case DM_LIST_DEVICES: |
| 4460 | case DM_DEV_CREATE: |
| 4461 | case DM_DEV_REMOVE: |
| 4462 | case DM_DEV_SUSPEND: |
| 4463 | case DM_DEV_STATUS: |
| 4464 | case DM_DEV_WAIT: |
| 4465 | case DM_TABLE_STATUS: |
| 4466 | case DM_TABLE_CLEAR: |
| 4467 | case DM_TABLE_DEPS: |
| 4468 | case DM_LIST_VERSIONS: |
| 4469 | /* no input data */ |
| 4470 | break; |
| 4471 | case DM_DEV_RENAME: |
| 4472 | case DM_DEV_SET_GEOMETRY: |
| 4473 | /* data contains only strings */ |
| 4474 | memcpy(host_data, argptr, guest_data_size); |
| 4475 | break; |
| 4476 | case DM_TARGET_MSG: |
| 4477 | memcpy(host_data, argptr, guest_data_size); |
| 4478 | *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr); |
| 4479 | break; |
| 4480 | case DM_TABLE_LOAD: |
| 4481 | { |
| 4482 | void *gspec = argptr; |
| 4483 | void *cur_data = host_data; |
| 4484 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; |
| 4485 | int spec_size = thunk_type_size(arg_type, 0); |
| 4486 | int i; |
| 4487 | |
| 4488 | for (i = 0; i < host_dm->target_count; i++) { |
| 4489 | struct dm_target_spec *spec = cur_data; |
| 4490 | uint32_t next; |
| 4491 | int slen; |
| 4492 | |
| 4493 | thunk_convert(spec, gspec, arg_type, THUNK_HOST); |
| 4494 | slen = strlen((char*)gspec + spec_size) + 1; |
| 4495 | next = spec->next; |
| 4496 | spec->next = sizeof(*spec) + slen; |
| 4497 | strcpy((char*)&spec[1], gspec + spec_size); |
| 4498 | gspec += next; |
| 4499 | cur_data += spec->next; |
| 4500 | } |
| 4501 | break; |
| 4502 | } |
| 4503 | default: |
| 4504 | ret = -TARGET_EINVAL; |
Chen Gang S | dec0473 | 2015-01-25 08:00:42 +0800 | [diff] [blame] | 4505 | unlock_user(argptr, guest_data, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4506 | goto out; |
| 4507 | } |
| 4508 | unlock_user(argptr, guest_data, 0); |
| 4509 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4510 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4511 | if (!is_error(ret)) { |
| 4512 | guest_data = arg + host_dm->data_start; |
| 4513 | guest_data_size = host_dm->data_size - host_dm->data_start; |
| 4514 | argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0); |
| 4515 | switch (ie->host_cmd) { |
| 4516 | case DM_REMOVE_ALL: |
| 4517 | case DM_DEV_CREATE: |
| 4518 | case DM_DEV_REMOVE: |
| 4519 | case DM_DEV_RENAME: |
| 4520 | case DM_DEV_SUSPEND: |
| 4521 | case DM_DEV_STATUS: |
| 4522 | case DM_TABLE_LOAD: |
| 4523 | case DM_TABLE_CLEAR: |
| 4524 | case DM_TARGET_MSG: |
| 4525 | case DM_DEV_SET_GEOMETRY: |
| 4526 | /* no return data */ |
| 4527 | break; |
| 4528 | case DM_LIST_DEVICES: |
| 4529 | { |
| 4530 | struct dm_name_list *nl = (void*)host_dm + host_dm->data_start; |
| 4531 | uint32_t remaining_data = guest_data_size; |
| 4532 | void *cur_data = argptr; |
| 4533 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) }; |
| 4534 | int nl_size = 12; /* can't use thunk_size due to alignment */ |
| 4535 | |
| 4536 | while (1) { |
| 4537 | uint32_t next = nl->next; |
| 4538 | if (next) { |
| 4539 | nl->next = nl_size + (strlen(nl->name) + 1); |
| 4540 | } |
| 4541 | if (remaining_data < nl->next) { |
| 4542 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 4543 | break; |
| 4544 | } |
| 4545 | thunk_convert(cur_data, nl, arg_type, THUNK_TARGET); |
| 4546 | strcpy(cur_data + nl_size, nl->name); |
| 4547 | cur_data += nl->next; |
| 4548 | remaining_data -= nl->next; |
| 4549 | if (!next) { |
| 4550 | break; |
| 4551 | } |
| 4552 | nl = (void*)nl + next; |
| 4553 | } |
| 4554 | break; |
| 4555 | } |
| 4556 | case DM_DEV_WAIT: |
| 4557 | case DM_TABLE_STATUS: |
| 4558 | { |
| 4559 | struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start; |
| 4560 | void *cur_data = argptr; |
| 4561 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; |
| 4562 | int spec_size = thunk_type_size(arg_type, 0); |
| 4563 | int i; |
| 4564 | |
| 4565 | for (i = 0; i < host_dm->target_count; i++) { |
| 4566 | uint32_t next = spec->next; |
| 4567 | int slen = strlen((char*)&spec[1]) + 1; |
| 4568 | spec->next = (cur_data - argptr) + spec_size + slen; |
| 4569 | if (guest_data_size < spec->next) { |
| 4570 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 4571 | break; |
| 4572 | } |
| 4573 | thunk_convert(cur_data, spec, arg_type, THUNK_TARGET); |
| 4574 | strcpy(cur_data + spec_size, (char*)&spec[1]); |
| 4575 | cur_data = argptr + spec->next; |
| 4576 | spec = (void*)host_dm + host_dm->data_start + next; |
| 4577 | } |
| 4578 | break; |
| 4579 | } |
| 4580 | case DM_TABLE_DEPS: |
| 4581 | { |
| 4582 | void *hdata = (void*)host_dm + host_dm->data_start; |
| 4583 | int count = *(uint32_t*)hdata; |
| 4584 | uint64_t *hdev = hdata + 8; |
| 4585 | uint64_t *gdev = argptr + 8; |
| 4586 | int i; |
| 4587 | |
| 4588 | *(uint32_t*)argptr = tswap32(count); |
| 4589 | for (i = 0; i < count; i++) { |
| 4590 | *gdev = tswap64(*hdev); |
| 4591 | gdev++; |
| 4592 | hdev++; |
| 4593 | } |
| 4594 | break; |
| 4595 | } |
| 4596 | case DM_LIST_VERSIONS: |
| 4597 | { |
| 4598 | struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start; |
| 4599 | uint32_t remaining_data = guest_data_size; |
| 4600 | void *cur_data = argptr; |
| 4601 | const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) }; |
| 4602 | int vers_size = thunk_type_size(arg_type, 0); |
| 4603 | |
| 4604 | while (1) { |
| 4605 | uint32_t next = vers->next; |
| 4606 | if (next) { |
| 4607 | vers->next = vers_size + (strlen(vers->name) + 1); |
| 4608 | } |
| 4609 | if (remaining_data < vers->next) { |
| 4610 | host_dm->flags |= DM_BUFFER_FULL_FLAG; |
| 4611 | break; |
| 4612 | } |
| 4613 | thunk_convert(cur_data, vers, arg_type, THUNK_TARGET); |
| 4614 | strcpy(cur_data + vers_size, vers->name); |
| 4615 | cur_data += vers->next; |
| 4616 | remaining_data -= vers->next; |
| 4617 | if (!next) { |
| 4618 | break; |
| 4619 | } |
| 4620 | vers = (void*)vers + next; |
| 4621 | } |
| 4622 | break; |
| 4623 | } |
| 4624 | default: |
Chen Gang S | dec0473 | 2015-01-25 08:00:42 +0800 | [diff] [blame] | 4625 | unlock_user(argptr, guest_data, 0); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4626 | ret = -TARGET_EINVAL; |
| 4627 | goto out; |
| 4628 | } |
| 4629 | unlock_user(argptr, guest_data, guest_data_size); |
| 4630 | |
| 4631 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4632 | if (!argptr) { |
| 4633 | ret = -TARGET_EFAULT; |
| 4634 | goto out; |
| 4635 | } |
| 4636 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 4637 | unlock_user(argptr, arg, target_size); |
| 4638 | } |
| 4639 | out: |
Stefan Weil | ad11ad7 | 2012-09-04 22:14:19 +0200 | [diff] [blame] | 4640 | g_free(big_buf); |
Alexander Graf | 56e904e | 2012-01-31 18:42:06 +0100 | [diff] [blame] | 4641 | return ret; |
| 4642 | } |
| 4643 | |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 4644 | 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] | 4645 | int cmd, abi_long arg) |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 4646 | { |
| 4647 | void *argptr; |
| 4648 | int target_size; |
| 4649 | const argtype *arg_type = ie->arg_type; |
| 4650 | const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) }; |
| 4651 | abi_long ret; |
| 4652 | |
| 4653 | struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp; |
| 4654 | struct blkpg_partition host_part; |
| 4655 | |
| 4656 | /* Read and convert blkpg */ |
| 4657 | arg_type++; |
| 4658 | target_size = thunk_type_size(arg_type, 0); |
| 4659 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4660 | if (!argptr) { |
| 4661 | ret = -TARGET_EFAULT; |
| 4662 | goto out; |
| 4663 | } |
| 4664 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4665 | unlock_user(argptr, arg, 0); |
| 4666 | |
| 4667 | switch (host_blkpg->op) { |
| 4668 | case BLKPG_ADD_PARTITION: |
| 4669 | case BLKPG_DEL_PARTITION: |
| 4670 | /* payload is struct blkpg_partition */ |
| 4671 | break; |
| 4672 | default: |
| 4673 | /* Unknown opcode */ |
| 4674 | ret = -TARGET_EINVAL; |
| 4675 | goto out; |
| 4676 | } |
| 4677 | |
| 4678 | /* Read and convert blkpg->data */ |
| 4679 | arg = (abi_long)(uintptr_t)host_blkpg->data; |
| 4680 | target_size = thunk_type_size(part_arg_type, 0); |
| 4681 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4682 | if (!argptr) { |
| 4683 | ret = -TARGET_EFAULT; |
| 4684 | goto out; |
| 4685 | } |
| 4686 | thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST); |
| 4687 | unlock_user(argptr, arg, 0); |
| 4688 | |
| 4689 | /* Swizzle the data pointer to our local copy and call! */ |
| 4690 | host_blkpg->data = &host_part; |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4691 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg)); |
Alexander Graf | a59b5e3 | 2014-08-22 13:15:50 +0200 | [diff] [blame] | 4692 | |
| 4693 | out: |
| 4694 | return ret; |
| 4695 | } |
| 4696 | |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 4697 | 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] | 4698 | int fd, int cmd, abi_long arg) |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 4699 | { |
| 4700 | const argtype *arg_type = ie->arg_type; |
| 4701 | const StructEntry *se; |
| 4702 | const argtype *field_types; |
| 4703 | const int *dst_offsets, *src_offsets; |
| 4704 | int target_size; |
| 4705 | void *argptr; |
| 4706 | abi_ulong *target_rt_dev_ptr; |
| 4707 | unsigned long *host_rt_dev_ptr; |
| 4708 | abi_long ret; |
| 4709 | int i; |
| 4710 | |
| 4711 | assert(ie->access == IOC_W); |
| 4712 | assert(*arg_type == TYPE_PTR); |
| 4713 | arg_type++; |
| 4714 | assert(*arg_type == TYPE_STRUCT); |
| 4715 | target_size = thunk_type_size(arg_type, 0); |
| 4716 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4717 | if (!argptr) { |
| 4718 | return -TARGET_EFAULT; |
| 4719 | } |
| 4720 | arg_type++; |
| 4721 | assert(*arg_type == (int)STRUCT_rtentry); |
| 4722 | se = struct_entries + *arg_type++; |
| 4723 | assert(se->convert[0] == NULL); |
| 4724 | /* convert struct here to be able to catch rt_dev string */ |
| 4725 | field_types = se->field_types; |
| 4726 | dst_offsets = se->field_offsets[THUNK_HOST]; |
| 4727 | src_offsets = se->field_offsets[THUNK_TARGET]; |
| 4728 | for (i = 0; i < se->nb_fields; i++) { |
| 4729 | if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) { |
| 4730 | assert(*field_types == TYPE_PTRVOID); |
| 4731 | target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]); |
| 4732 | host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]); |
| 4733 | if (*target_rt_dev_ptr != 0) { |
| 4734 | *host_rt_dev_ptr = (unsigned long)lock_user_string( |
| 4735 | tswapal(*target_rt_dev_ptr)); |
| 4736 | if (!*host_rt_dev_ptr) { |
| 4737 | unlock_user(argptr, arg, 0); |
| 4738 | return -TARGET_EFAULT; |
| 4739 | } |
| 4740 | } else { |
| 4741 | *host_rt_dev_ptr = 0; |
| 4742 | } |
| 4743 | field_types++; |
| 4744 | continue; |
| 4745 | } |
| 4746 | field_types = thunk_convert(buf_temp + dst_offsets[i], |
| 4747 | argptr + src_offsets[i], |
| 4748 | field_types, THUNK_HOST); |
| 4749 | } |
| 4750 | unlock_user(argptr, arg, 0); |
| 4751 | |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4752 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
Laurent Vivier | 7ff7b66 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 4753 | if (*host_rt_dev_ptr != 0) { |
| 4754 | unlock_user((void *)*host_rt_dev_ptr, |
| 4755 | *target_rt_dev_ptr, 0); |
| 4756 | } |
| 4757 | return ret; |
| 4758 | } |
| 4759 | |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 4760 | 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] | 4761 | int fd, int cmd, abi_long arg) |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 4762 | { |
| 4763 | int sig = target_to_host_signal(arg); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4764 | return get_errno(safe_ioctl(fd, ie->host_cmd, sig)); |
Paul Burton | ca56f5b | 2014-06-22 11:25:47 +0100 | [diff] [blame] | 4765 | } |
| 4766 | |
blueswir1 | 9f106a7 | 2008-10-05 10:52:52 +0000 | [diff] [blame] | 4767 | static IOCTLEntry ioctl_entries[] = { |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 4768 | #define IOCTL(cmd, access, ...) \ |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 4769 | { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } }, |
| 4770 | #define IOCTL_SPECIAL(cmd, access, dofn, ...) \ |
| 4771 | { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } }, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4772 | #include "ioctls.h" |
| 4773 | { 0, 0, }, |
| 4774 | }; |
| 4775 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4776 | /* ??? Implement proper locking for ioctls. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4777 | /* do_ioctl() Must return target values and target errnos. */ |
Laurent Vivier | 45c874e | 2015-06-16 00:35:28 +0200 | [diff] [blame] | 4778 | static abi_long do_ioctl(int fd, int cmd, abi_long arg) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4779 | { |
| 4780 | const IOCTLEntry *ie; |
| 4781 | const argtype *arg_type; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 4782 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4783 | uint8_t buf_temp[MAX_STRUCT_SIZE]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4784 | int target_size; |
| 4785 | void *argptr; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4786 | |
| 4787 | ie = ioctl_entries; |
| 4788 | for(;;) { |
| 4789 | if (ie->target_cmd == 0) { |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4790 | gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4791 | return -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4792 | } |
| 4793 | if (ie->target_cmd == cmd) |
| 4794 | break; |
| 4795 | ie++; |
| 4796 | } |
| 4797 | arg_type = ie->arg_type; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 4798 | #if defined(DEBUG) |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4799 | gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name); |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 4800 | #endif |
Peter Maydell | d2ef05b | 2011-01-06 15:04:17 +0000 | [diff] [blame] | 4801 | if (ie->do_ioctl) { |
| 4802 | return ie->do_ioctl(ie, buf_temp, fd, cmd, arg); |
| 4803 | } |
| 4804 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4805 | switch(arg_type[0]) { |
| 4806 | case TYPE_NULL: |
| 4807 | /* no argument */ |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4808 | ret = get_errno(safe_ioctl(fd, ie->host_cmd)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4809 | break; |
| 4810 | case TYPE_PTRVOID: |
| 4811 | case TYPE_INT: |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4812 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4813 | break; |
| 4814 | case TYPE_PTR: |
| 4815 | arg_type++; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4816 | target_size = thunk_type_size(arg_type, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4817 | switch(ie->access) { |
| 4818 | case IOC_R: |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4819 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4820 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4821 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4822 | if (!argptr) |
| 4823 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4824 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 4825 | unlock_user(argptr, arg, target_size); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4826 | } |
| 4827 | break; |
| 4828 | case IOC_W: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4829 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4830 | if (!argptr) |
| 4831 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4832 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4833 | unlock_user(argptr, arg, 0); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4834 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4835 | break; |
| 4836 | default: |
| 4837 | case IOC_RW: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4838 | argptr = lock_user(VERIFY_READ, arg, target_size, 1); |
| 4839 | if (!argptr) |
| 4840 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4841 | thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); |
| 4842 | unlock_user(argptr, arg, 0); |
Peter Maydell | 49ca6f3 | 2016-06-06 19:58:14 +0100 | [diff] [blame] | 4843 | ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4844 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 4845 | argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); |
| 4846 | if (!argptr) |
| 4847 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 4848 | thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); |
| 4849 | unlock_user(argptr, arg, target_size); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4850 | } |
| 4851 | break; |
| 4852 | } |
| 4853 | break; |
| 4854 | default: |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 4855 | gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", |
| 4856 | (long)cmd, arg_type[0]); |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 4857 | ret = -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | } |
| 4860 | return ret; |
| 4861 | } |
| 4862 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 4863 | static const bitmask_transtbl iflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4864 | { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK }, |
| 4865 | { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT }, |
| 4866 | { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR }, |
| 4867 | { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK }, |
| 4868 | { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK }, |
| 4869 | { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP }, |
| 4870 | { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR }, |
| 4871 | { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR }, |
| 4872 | { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL }, |
| 4873 | { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC }, |
| 4874 | { TARGET_IXON, TARGET_IXON, IXON, IXON }, |
| 4875 | { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY }, |
| 4876 | { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF }, |
| 4877 | { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL }, |
| 4878 | { 0, 0, 0, 0 } |
| 4879 | }; |
| 4880 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 4881 | static const bitmask_transtbl oflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4882 | { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST }, |
| 4883 | { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC }, |
| 4884 | { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR }, |
| 4885 | { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL }, |
| 4886 | { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR }, |
| 4887 | { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET }, |
| 4888 | { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL }, |
| 4889 | { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL }, |
| 4890 | { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 }, |
| 4891 | { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 }, |
| 4892 | { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 }, |
| 4893 | { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 }, |
| 4894 | { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 }, |
| 4895 | { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 }, |
| 4896 | { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 }, |
| 4897 | { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 }, |
| 4898 | { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 }, |
| 4899 | { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 }, |
| 4900 | { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 }, |
| 4901 | { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 }, |
| 4902 | { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 }, |
| 4903 | { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 }, |
| 4904 | { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 }, |
| 4905 | { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 }, |
| 4906 | { 0, 0, 0, 0 } |
| 4907 | }; |
| 4908 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 4909 | static const bitmask_transtbl cflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4910 | { TARGET_CBAUD, TARGET_B0, CBAUD, B0 }, |
| 4911 | { TARGET_CBAUD, TARGET_B50, CBAUD, B50 }, |
| 4912 | { TARGET_CBAUD, TARGET_B75, CBAUD, B75 }, |
| 4913 | { TARGET_CBAUD, TARGET_B110, CBAUD, B110 }, |
| 4914 | { TARGET_CBAUD, TARGET_B134, CBAUD, B134 }, |
| 4915 | { TARGET_CBAUD, TARGET_B150, CBAUD, B150 }, |
| 4916 | { TARGET_CBAUD, TARGET_B200, CBAUD, B200 }, |
| 4917 | { TARGET_CBAUD, TARGET_B300, CBAUD, B300 }, |
| 4918 | { TARGET_CBAUD, TARGET_B600, CBAUD, B600 }, |
| 4919 | { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 }, |
| 4920 | { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 }, |
| 4921 | { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 }, |
| 4922 | { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 }, |
| 4923 | { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 }, |
| 4924 | { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 }, |
| 4925 | { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 }, |
| 4926 | { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 }, |
| 4927 | { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 }, |
| 4928 | { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 }, |
| 4929 | { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 }, |
| 4930 | { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 }, |
| 4931 | { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 }, |
| 4932 | { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 }, |
| 4933 | { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 }, |
| 4934 | { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB }, |
| 4935 | { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD }, |
| 4936 | { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB }, |
| 4937 | { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD }, |
| 4938 | { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL }, |
| 4939 | { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL }, |
| 4940 | { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS }, |
| 4941 | { 0, 0, 0, 0 } |
| 4942 | }; |
| 4943 | |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 4944 | static const bitmask_transtbl lflag_tbl[] = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4945 | { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG }, |
| 4946 | { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON }, |
| 4947 | { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE }, |
| 4948 | { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO }, |
| 4949 | { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE }, |
| 4950 | { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK }, |
| 4951 | { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL }, |
| 4952 | { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH }, |
| 4953 | { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP }, |
| 4954 | { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL }, |
| 4955 | { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT }, |
| 4956 | { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE }, |
| 4957 | { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO }, |
| 4958 | { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN }, |
| 4959 | { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN }, |
| 4960 | { 0, 0, 0, 0 } |
| 4961 | }; |
| 4962 | |
| 4963 | static void target_to_host_termios (void *dst, const void *src) |
| 4964 | { |
| 4965 | struct host_termios *host = dst; |
| 4966 | const struct target_termios *target = src; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4967 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4968 | host->c_iflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4969 | target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4970 | host->c_oflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4971 | target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4972 | host->c_cflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4973 | target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4974 | host->c_lflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4975 | target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl); |
| 4976 | host->c_line = target->c_line; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4977 | |
Arnaud Patard | 4460712 | 2009-04-21 17:39:08 +0300 | [diff] [blame] | 4978 | memset(host->c_cc, 0, sizeof(host->c_cc)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4979 | host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; |
| 4980 | host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4981 | host->c_cc[VERASE] = target->c_cc[TARGET_VERASE]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4982 | host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4983 | host->c_cc[VEOF] = target->c_cc[TARGET_VEOF]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4984 | host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4985 | host->c_cc[VMIN] = target->c_cc[TARGET_VMIN]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4986 | host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4987 | host->c_cc[VSTART] = target->c_cc[TARGET_VSTART]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4988 | host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; |
| 4989 | host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4990 | host->c_cc[VEOL] = target->c_cc[TARGET_VEOL]; |
| 4991 | host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT]; |
| 4992 | host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD]; |
| 4993 | host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE]; |
| 4994 | host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT]; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4995 | host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4996 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4997 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4998 | static void host_to_target_termios (void *dst, const void *src) |
| 4999 | { |
| 5000 | struct target_termios *target = dst; |
| 5001 | const struct host_termios *host = src; |
| 5002 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5003 | target->c_iflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5004 | tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5005 | target->c_oflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5006 | tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5007 | target->c_cflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5008 | tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5009 | target->c_lflag = |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5010 | tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl)); |
| 5011 | target->c_line = host->c_line; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5012 | |
Arnaud Patard | 4460712 | 2009-04-21 17:39:08 +0300 | [diff] [blame] | 5013 | memset(target->c_cc, 0, sizeof(target->c_cc)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5014 | target->c_cc[TARGET_VINTR] = host->c_cc[VINTR]; |
| 5015 | target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT]; |
| 5016 | target->c_cc[TARGET_VERASE] = host->c_cc[VERASE]; |
| 5017 | target->c_cc[TARGET_VKILL] = host->c_cc[VKILL]; |
| 5018 | target->c_cc[TARGET_VEOF] = host->c_cc[VEOF]; |
| 5019 | target->c_cc[TARGET_VTIME] = host->c_cc[VTIME]; |
| 5020 | target->c_cc[TARGET_VMIN] = host->c_cc[VMIN]; |
| 5021 | target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC]; |
| 5022 | target->c_cc[TARGET_VSTART] = host->c_cc[VSTART]; |
| 5023 | target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP]; |
| 5024 | target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP]; |
| 5025 | target->c_cc[TARGET_VEOL] = host->c_cc[VEOL]; |
| 5026 | target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT]; |
| 5027 | target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD]; |
| 5028 | target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE]; |
| 5029 | target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT]; |
| 5030 | target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2]; |
| 5031 | } |
| 5032 | |
blueswir1 | 8e853dc | 2008-10-05 10:49:32 +0000 | [diff] [blame] | 5033 | static const StructEntry struct_termios_def = { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5034 | .convert = { host_to_target_termios, target_to_host_termios }, |
| 5035 | .size = { sizeof(struct target_termios), sizeof(struct host_termios) }, |
| 5036 | .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) }, |
| 5037 | }; |
| 5038 | |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 5039 | static bitmask_transtbl mmap_flags_tbl[] = { |
| 5040 | { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED }, |
| 5041 | { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE }, |
| 5042 | { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED }, |
| 5043 | { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS }, |
| 5044 | { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN }, |
| 5045 | { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE }, |
| 5046 | { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE }, |
| 5047 | { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED }, |
Christophe Lyon | e8efd8e | 2014-02-03 17:04:32 +0100 | [diff] [blame] | 5048 | { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE, |
| 5049 | MAP_NORESERVE }, |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 5050 | { 0, 0, 0, 0 } |
| 5051 | }; |
| 5052 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5053 | #if defined(TARGET_I386) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5054 | |
| 5055 | /* NOTE: there is really one LDT for all the threads */ |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 5056 | static uint8_t *ldt_table; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5057 | |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5058 | static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5059 | { |
| 5060 | int size; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5061 | void *p; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5062 | |
| 5063 | if (!ldt_table) |
| 5064 | return 0; |
| 5065 | size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE; |
| 5066 | if (size > bytecount) |
| 5067 | size = bytecount; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5068 | p = lock_user(VERIFY_WRITE, ptr, size, 0); |
| 5069 | if (!p) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5070 | return -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5071 | /* ??? Should this by byteswapped? */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5072 | memcpy(p, ldt_table, size); |
| 5073 | unlock_user(p, ptr, size); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5074 | return size; |
| 5075 | } |
| 5076 | |
| 5077 | /* XXX: add locking support */ |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5078 | static abi_long write_ldt(CPUX86State *env, |
| 5079 | abi_ulong ptr, unsigned long bytecount, int oldmode) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5080 | { |
| 5081 | struct target_modify_ldt_ldt_s ldt_info; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5082 | struct target_modify_ldt_ldt_s *target_ldt_info; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5083 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5084 | int seg_not_present, useable, lm; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5085 | uint32_t *lp, entry_1, entry_2; |
| 5086 | |
| 5087 | if (bytecount != sizeof(ldt_info)) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5088 | return -TARGET_EINVAL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 5089 | if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1)) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5090 | return -TARGET_EFAULT; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5091 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5092 | ldt_info.base_addr = tswapal(target_ldt_info->base_addr); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5093 | ldt_info.limit = tswap32(target_ldt_info->limit); |
| 5094 | ldt_info.flags = tswap32(target_ldt_info->flags); |
| 5095 | unlock_user_struct(target_ldt_info, ptr, 0); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5096 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5097 | if (ldt_info.entry_number >= TARGET_LDT_ENTRIES) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5098 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5099 | seg_32bit = ldt_info.flags & 1; |
| 5100 | contents = (ldt_info.flags >> 1) & 3; |
| 5101 | read_exec_only = (ldt_info.flags >> 3) & 1; |
| 5102 | limit_in_pages = (ldt_info.flags >> 4) & 1; |
| 5103 | seg_not_present = (ldt_info.flags >> 5) & 1; |
| 5104 | useable = (ldt_info.flags >> 6) & 1; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5105 | #ifdef TARGET_ABI32 |
| 5106 | lm = 0; |
| 5107 | #else |
| 5108 | lm = (ldt_info.flags >> 7) & 1; |
| 5109 | #endif |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5110 | if (contents == 3) { |
| 5111 | if (oldmode) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5112 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5113 | if (seg_not_present == 0) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5114 | return -TARGET_EINVAL; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5115 | } |
| 5116 | /* allocate the LDT */ |
| 5117 | if (!ldt_table) { |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 5118 | env->ldt.base = target_mmap(0, |
| 5119 | TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE, |
| 5120 | PROT_READ|PROT_WRITE, |
| 5121 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 5122 | if (env->ldt.base == -1) |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5123 | return -TARGET_ENOMEM; |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 5124 | memset(g2h(env->ldt.base), 0, |
| 5125 | TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5126 | env->ldt.limit = 0xffff; |
balrog | e441570 | 2008-11-10 02:55:33 +0000 | [diff] [blame] | 5127 | ldt_table = g2h(env->ldt.base); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | /* NOTE: same code as Linux kernel */ |
| 5131 | /* Allow LDTs to be cleared by the user. */ |
| 5132 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 5133 | if (oldmode || |
| 5134 | (contents == 0 && |
| 5135 | read_exec_only == 1 && |
| 5136 | seg_32bit == 0 && |
| 5137 | limit_in_pages == 0 && |
| 5138 | seg_not_present == 1 && |
| 5139 | useable == 0 )) { |
| 5140 | entry_1 = 0; |
| 5141 | entry_2 = 0; |
| 5142 | goto install; |
| 5143 | } |
| 5144 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5145 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5146 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
| 5147 | (ldt_info.limit & 0x0ffff); |
| 5148 | entry_2 = (ldt_info.base_addr & 0xff000000) | |
| 5149 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | |
| 5150 | (ldt_info.limit & 0xf0000) | |
| 5151 | ((read_exec_only ^ 1) << 9) | |
| 5152 | (contents << 10) | |
| 5153 | ((seg_not_present ^ 1) << 15) | |
| 5154 | (seg_32bit << 22) | |
| 5155 | (limit_in_pages << 23) | |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5156 | (lm << 21) | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5157 | 0x7000; |
| 5158 | if (!oldmode) |
| 5159 | entry_2 |= (useable << 20); |
bellard | 14ae3ba | 2003-05-27 23:25:06 +0000 | [diff] [blame] | 5160 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5161 | /* Install the new entry ... */ |
| 5162 | install: |
| 5163 | lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3)); |
| 5164 | lp[0] = tswap32(entry_1); |
| 5165 | lp[1] = tswap32(entry_2); |
| 5166 | return 0; |
| 5167 | } |
| 5168 | |
| 5169 | /* specific and weird i386 syscalls */ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 5170 | static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, |
| 5171 | unsigned long bytecount) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5172 | { |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5173 | abi_long ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5174 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5175 | switch (func) { |
| 5176 | case 0: |
| 5177 | ret = read_ldt(ptr, bytecount); |
| 5178 | break; |
| 5179 | case 1: |
| 5180 | ret = write_ldt(env, ptr, bytecount, 1); |
| 5181 | break; |
| 5182 | case 0x11: |
| 5183 | ret = write_ldt(env, ptr, bytecount, 0); |
| 5184 | break; |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 5185 | default: |
| 5186 | ret = -TARGET_ENOSYS; |
| 5187 | break; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 5188 | } |
| 5189 | return ret; |
| 5190 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5191 | |
blueswir1 | 4583f58 | 2008-08-24 10:35:55 +0000 | [diff] [blame] | 5192 | #if defined(TARGET_I386) && defined(TARGET_ABI32) |
Alexander Graf | bc22eb4 | 2013-07-16 18:44:58 +0100 | [diff] [blame] | 5193 | abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr) |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5194 | { |
| 5195 | uint64_t *gdt_table = g2h(env->gdt.base); |
| 5196 | struct target_modify_ldt_ldt_s ldt_info; |
| 5197 | struct target_modify_ldt_ldt_s *target_ldt_info; |
| 5198 | int seg_32bit, contents, read_exec_only, limit_in_pages; |
| 5199 | int seg_not_present, useable, lm; |
| 5200 | uint32_t *lp, entry_1, entry_2; |
| 5201 | int i; |
| 5202 | |
| 5203 | lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); |
| 5204 | if (!target_ldt_info) |
| 5205 | return -TARGET_EFAULT; |
| 5206 | ldt_info.entry_number = tswap32(target_ldt_info->entry_number); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5207 | ldt_info.base_addr = tswapal(target_ldt_info->base_addr); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5208 | ldt_info.limit = tswap32(target_ldt_info->limit); |
| 5209 | ldt_info.flags = tswap32(target_ldt_info->flags); |
| 5210 | if (ldt_info.entry_number == -1) { |
| 5211 | for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) { |
| 5212 | if (gdt_table[i] == 0) { |
| 5213 | ldt_info.entry_number = i; |
| 5214 | target_ldt_info->entry_number = tswap32(i); |
| 5215 | break; |
| 5216 | } |
| 5217 | } |
| 5218 | } |
| 5219 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 5220 | |
| 5221 | if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN || |
| 5222 | ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX) |
| 5223 | return -TARGET_EINVAL; |
| 5224 | seg_32bit = ldt_info.flags & 1; |
| 5225 | contents = (ldt_info.flags >> 1) & 3; |
| 5226 | read_exec_only = (ldt_info.flags >> 3) & 1; |
| 5227 | limit_in_pages = (ldt_info.flags >> 4) & 1; |
| 5228 | seg_not_present = (ldt_info.flags >> 5) & 1; |
| 5229 | useable = (ldt_info.flags >> 6) & 1; |
| 5230 | #ifdef TARGET_ABI32 |
| 5231 | lm = 0; |
| 5232 | #else |
| 5233 | lm = (ldt_info.flags >> 7) & 1; |
| 5234 | #endif |
| 5235 | |
| 5236 | if (contents == 3) { |
| 5237 | if (seg_not_present == 0) |
| 5238 | return -TARGET_EINVAL; |
| 5239 | } |
| 5240 | |
| 5241 | /* NOTE: same code as Linux kernel */ |
| 5242 | /* Allow LDTs to be cleared by the user. */ |
| 5243 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
| 5244 | if ((contents == 0 && |
| 5245 | read_exec_only == 1 && |
| 5246 | seg_32bit == 0 && |
| 5247 | limit_in_pages == 0 && |
| 5248 | seg_not_present == 1 && |
| 5249 | useable == 0 )) { |
| 5250 | entry_1 = 0; |
| 5251 | entry_2 = 0; |
| 5252 | goto install; |
| 5253 | } |
| 5254 | } |
| 5255 | |
| 5256 | entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | |
| 5257 | (ldt_info.limit & 0x0ffff); |
| 5258 | entry_2 = (ldt_info.base_addr & 0xff000000) | |
| 5259 | ((ldt_info.base_addr & 0x00ff0000) >> 16) | |
| 5260 | (ldt_info.limit & 0xf0000) | |
| 5261 | ((read_exec_only ^ 1) << 9) | |
| 5262 | (contents << 10) | |
| 5263 | ((seg_not_present ^ 1) << 15) | |
| 5264 | (seg_32bit << 22) | |
| 5265 | (limit_in_pages << 23) | |
| 5266 | (useable << 20) | |
| 5267 | (lm << 21) | |
| 5268 | 0x7000; |
| 5269 | |
| 5270 | /* Install the new entry ... */ |
| 5271 | install: |
| 5272 | lp = (uint32_t *)(gdt_table + ldt_info.entry_number); |
| 5273 | lp[0] = tswap32(entry_1); |
| 5274 | lp[1] = tswap32(entry_2); |
| 5275 | return 0; |
| 5276 | } |
| 5277 | |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 5278 | static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr) |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5279 | { |
| 5280 | struct target_modify_ldt_ldt_s *target_ldt_info; |
| 5281 | uint64_t *gdt_table = g2h(env->gdt.base); |
| 5282 | uint32_t base_addr, limit, flags; |
| 5283 | int seg_32bit, contents, read_exec_only, limit_in_pages, idx; |
| 5284 | int seg_not_present, useable, lm; |
| 5285 | uint32_t *lp, entry_1, entry_2; |
| 5286 | |
| 5287 | lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); |
| 5288 | if (!target_ldt_info) |
| 5289 | return -TARGET_EFAULT; |
| 5290 | idx = tswap32(target_ldt_info->entry_number); |
| 5291 | if (idx < TARGET_GDT_ENTRY_TLS_MIN || |
| 5292 | idx > TARGET_GDT_ENTRY_TLS_MAX) { |
| 5293 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 5294 | return -TARGET_EINVAL; |
| 5295 | } |
| 5296 | lp = (uint32_t *)(gdt_table + idx); |
| 5297 | entry_1 = tswap32(lp[0]); |
| 5298 | entry_2 = tswap32(lp[1]); |
| 5299 | |
| 5300 | read_exec_only = ((entry_2 >> 9) & 1) ^ 1; |
| 5301 | contents = (entry_2 >> 10) & 3; |
| 5302 | seg_not_present = ((entry_2 >> 15) & 1) ^ 1; |
| 5303 | seg_32bit = (entry_2 >> 22) & 1; |
| 5304 | limit_in_pages = (entry_2 >> 23) & 1; |
| 5305 | useable = (entry_2 >> 20) & 1; |
| 5306 | #ifdef TARGET_ABI32 |
| 5307 | lm = 0; |
| 5308 | #else |
| 5309 | lm = (entry_2 >> 21) & 1; |
| 5310 | #endif |
| 5311 | flags = (seg_32bit << 0) | (contents << 1) | |
| 5312 | (read_exec_only << 3) | (limit_in_pages << 4) | |
| 5313 | (seg_not_present << 5) | (useable << 6) | (lm << 7); |
| 5314 | limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000); |
| 5315 | base_addr = (entry_1 >> 16) | |
| 5316 | (entry_2 & 0xff000000) | |
| 5317 | ((entry_2 & 0xff) << 16); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 5318 | target_ldt_info->base_addr = tswapal(base_addr); |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5319 | target_ldt_info->limit = tswap32(limit); |
| 5320 | target_ldt_info->flags = tswap32(flags); |
| 5321 | unlock_user_struct(target_ldt_info, ptr, 1); |
| 5322 | return 0; |
| 5323 | } |
blueswir1 | 4583f58 | 2008-08-24 10:35:55 +0000 | [diff] [blame] | 5324 | #endif /* TARGET_I386 && TARGET_ABI32 */ |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 5325 | |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 5326 | #ifndef TARGET_ABI32 |
Peter Maydell | 2667e71 | 2013-07-16 18:44:59 +0100 | [diff] [blame] | 5327 | abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 5328 | { |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 5329 | abi_long ret = 0; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 5330 | abi_ulong val; |
| 5331 | int idx; |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 5332 | |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 5333 | switch(code) { |
| 5334 | case TARGET_ARCH_SET_GS: |
| 5335 | case TARGET_ARCH_SET_FS: |
| 5336 | if (code == TARGET_ARCH_SET_GS) |
| 5337 | idx = R_GS; |
| 5338 | else |
| 5339 | idx = R_FS; |
| 5340 | cpu_x86_load_seg(env, idx, 0); |
| 5341 | env->segs[idx].base = addr; |
| 5342 | break; |
| 5343 | case TARGET_ARCH_GET_GS: |
| 5344 | case TARGET_ARCH_GET_FS: |
| 5345 | if (code == TARGET_ARCH_GET_GS) |
| 5346 | idx = R_GS; |
| 5347 | else |
| 5348 | idx = R_FS; |
| 5349 | val = env->segs[idx].base; |
| 5350 | if (put_user(val, addr, abi_ulong)) |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 5351 | ret = -TARGET_EFAULT; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 5352 | break; |
| 5353 | default: |
| 5354 | ret = -TARGET_EINVAL; |
| 5355 | break; |
| 5356 | } |
Juan Quintela | 1add869 | 2011-06-16 17:37:09 +0100 | [diff] [blame] | 5357 | return ret; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 5358 | } |
| 5359 | #endif |
| 5360 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5361 | #endif /* defined(TARGET_I386) */ |
| 5362 | |
Riku Voipio | 05098a9 | 2011-03-04 15:27:29 +0200 | [diff] [blame] | 5363 | #define NEW_STACK_SIZE 0x40000 |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5364 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5365 | |
| 5366 | static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER; |
| 5367 | typedef struct { |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 5368 | CPUArchState *env; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5369 | pthread_mutex_t mutex; |
| 5370 | pthread_cond_t cond; |
| 5371 | pthread_t thread; |
| 5372 | uint32_t tid; |
| 5373 | abi_ulong child_tidptr; |
| 5374 | abi_ulong parent_tidptr; |
| 5375 | sigset_t sigmask; |
| 5376 | } new_thread_info; |
| 5377 | |
| 5378 | static void *clone_func(void *arg) |
| 5379 | { |
| 5380 | new_thread_info *info = arg; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 5381 | CPUArchState *env; |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 5382 | CPUState *cpu; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 5383 | TaskState *ts; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5384 | |
Emilio G. Cota | 7090376 | 2015-08-23 20:23:41 -0400 | [diff] [blame] | 5385 | rcu_register_thread(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5386 | env = info->env; |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 5387 | cpu = ENV_GET_CPU(env); |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 5388 | thread_cpu = cpu; |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 5389 | ts = (TaskState *)cpu->opaque; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5390 | info->tid = gettid(); |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 5391 | cpu->host_tid = info->tid; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 5392 | task_settid(ts); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5393 | if (info->child_tidptr) |
| 5394 | put_user_u32(info->tid, info->child_tidptr); |
| 5395 | if (info->parent_tidptr) |
| 5396 | put_user_u32(info->tid, info->parent_tidptr); |
| 5397 | /* Enable signals. */ |
| 5398 | sigprocmask(SIG_SETMASK, &info->sigmask, NULL); |
| 5399 | /* Signal to the parent that we're ready. */ |
| 5400 | pthread_mutex_lock(&info->mutex); |
| 5401 | pthread_cond_broadcast(&info->cond); |
| 5402 | pthread_mutex_unlock(&info->mutex); |
| 5403 | /* Wait until the parent has finshed initializing the tls state. */ |
| 5404 | pthread_mutex_lock(&clone_lock); |
| 5405 | pthread_mutex_unlock(&clone_lock); |
| 5406 | cpu_loop(env); |
| 5407 | /* never exits */ |
| 5408 | return NULL; |
| 5409 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5410 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 5411 | /* do_fork() Must return host values and target errnos (unlike most |
| 5412 | do_*() functions). */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 5413 | static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5414 | abi_ulong parent_tidptr, target_ulong newtls, |
| 5415 | abi_ulong child_tidptr) |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5416 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 5417 | CPUState *cpu = ENV_GET_CPU(env); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5418 | int ret; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 5419 | TaskState *ts; |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 5420 | CPUState *new_cpu; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 5421 | CPUArchState *new_env; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5422 | unsigned int nptl_flags; |
| 5423 | sigset_t sigmask; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5424 | |
balrog | 436d124 | 2008-09-21 02:39:45 +0000 | [diff] [blame] | 5425 | /* Emulate vfork() with fork() */ |
| 5426 | if (flags & CLONE_VFORK) |
| 5427 | flags &= ~(CLONE_VFORK | CLONE_VM); |
| 5428 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5429 | if (flags & CLONE_VM) { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 5430 | TaskState *parent_ts = (TaskState *)cpu->opaque; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5431 | new_thread_info info; |
| 5432 | pthread_attr_t attr; |
Peter Maydell | 24cb36a | 2013-07-16 18:45:00 +0100 | [diff] [blame] | 5433 | |
Markus Armbruster | c78d65e | 2015-09-14 13:53:03 +0200 | [diff] [blame] | 5434 | ts = g_new0(TaskState, 1); |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 5435 | init_task_state(ts); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5436 | /* we create a new CPU instance. */ |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 5437 | new_env = cpu_copy(env); |
pbrook | 6e68e07 | 2008-05-30 17:22:15 +0000 | [diff] [blame] | 5438 | /* Init regs that differ from the parent. */ |
| 5439 | cpu_clone_regs(new_env, newsp); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 5440 | new_cpu = ENV_GET_CPU(new_env); |
| 5441 | new_cpu->opaque = ts; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 5442 | ts->bprm = parent_ts->bprm; |
| 5443 | ts->info = parent_ts->info; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 5444 | ts->signal_mask = parent_ts->signal_mask; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5445 | nptl_flags = flags; |
| 5446 | flags &= ~CLONE_NPTL_FLAGS2; |
| 5447 | |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 5448 | if (nptl_flags & CLONE_CHILD_CLEARTID) { |
| 5449 | ts->child_tidptr = child_tidptr; |
| 5450 | } |
| 5451 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5452 | if (nptl_flags & CLONE_SETTLS) |
| 5453 | cpu_set_tls (new_env, newtls); |
| 5454 | |
| 5455 | /* Grab a mutex so that thread setup appears atomic. */ |
| 5456 | pthread_mutex_lock(&clone_lock); |
| 5457 | |
| 5458 | memset(&info, 0, sizeof(info)); |
| 5459 | pthread_mutex_init(&info.mutex, NULL); |
| 5460 | pthread_mutex_lock(&info.mutex); |
| 5461 | pthread_cond_init(&info.cond, NULL); |
| 5462 | info.env = new_env; |
| 5463 | if (nptl_flags & CLONE_CHILD_SETTID) |
| 5464 | info.child_tidptr = child_tidptr; |
| 5465 | if (nptl_flags & CLONE_PARENT_SETTID) |
| 5466 | info.parent_tidptr = parent_tidptr; |
| 5467 | |
| 5468 | ret = pthread_attr_init(&attr); |
Nathan Froyd | 48e15fc | 2010-10-29 07:48:57 -0700 | [diff] [blame] | 5469 | ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE); |
| 5470 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5471 | /* It is not safe to deliver signals until the child has finished |
| 5472 | initializing, so temporarily block all signals. */ |
| 5473 | sigfillset(&sigmask); |
| 5474 | sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask); |
| 5475 | |
| 5476 | ret = pthread_create(&info.thread, &attr, clone_func, &info); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 5477 | /* TODO: Free new CPU state if thread creation failed. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5478 | |
| 5479 | sigprocmask(SIG_SETMASK, &info.sigmask, NULL); |
| 5480 | pthread_attr_destroy(&attr); |
| 5481 | if (ret == 0) { |
| 5482 | /* Wait for the child to initialize. */ |
| 5483 | pthread_cond_wait(&info.cond, &info.mutex); |
| 5484 | ret = info.tid; |
| 5485 | if (flags & CLONE_PARENT_SETTID) |
| 5486 | put_user_u32(ret, parent_tidptr); |
| 5487 | } else { |
| 5488 | ret = -1; |
| 5489 | } |
| 5490 | pthread_mutex_unlock(&info.mutex); |
| 5491 | pthread_cond_destroy(&info.cond); |
| 5492 | pthread_mutex_destroy(&info.mutex); |
| 5493 | pthread_mutex_unlock(&clone_lock); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5494 | } else { |
| 5495 | /* if no CLONE_VM, we consider it is a fork */ |
Timothy E Baldwin | 93b4eff | 2015-08-31 00:26:21 +0100 | [diff] [blame] | 5496 | if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0) { |
| 5497 | return -TARGET_EINVAL; |
| 5498 | } |
Timothy E Baldwin | 7d92d34 | 2016-05-27 15:51:58 +0100 | [diff] [blame] | 5499 | |
| 5500 | if (block_signals()) { |
| 5501 | return -TARGET_ERESTARTSYS; |
| 5502 | } |
| 5503 | |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5504 | fork_start(); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5505 | ret = fork(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5506 | if (ret == 0) { |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 5507 | /* Child Process. */ |
Paolo Bonzini | a59629f | 2015-03-31 13:01:05 +0200 | [diff] [blame] | 5508 | rcu_after_fork(); |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5509 | cpu_clone_regs(env, newsp); |
| 5510 | fork_end(1); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 5511 | /* There is a race condition here. The parent process could |
| 5512 | theoretically read the TID in the child process before the child |
| 5513 | tid is set. This would require using either ptrace |
| 5514 | (not implemented) or having *_tidptr to point at a shared memory |
| 5515 | mapping. We can't repeat the spinlock hack used above because |
| 5516 | the child process gets its own copy of the lock. */ |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5517 | if (flags & CLONE_CHILD_SETTID) |
| 5518 | put_user_u32(gettid(), child_tidptr); |
| 5519 | if (flags & CLONE_PARENT_SETTID) |
| 5520 | put_user_u32(gettid(), parent_tidptr); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 5521 | ts = (TaskState *)cpu->opaque; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5522 | if (flags & CLONE_SETTLS) |
| 5523 | cpu_set_tls (env, newtls); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 5524 | if (flags & CLONE_CHILD_CLEARTID) |
| 5525 | ts->child_tidptr = child_tidptr; |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 5526 | } else { |
| 5527 | fork_end(0); |
| 5528 | } |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5529 | } |
| 5530 | return ret; |
| 5531 | } |
| 5532 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5533 | /* warning : doesn't handle linux specific flags... */ |
| 5534 | static int target_to_host_fcntl_cmd(int cmd) |
| 5535 | { |
| 5536 | switch(cmd) { |
| 5537 | case TARGET_F_DUPFD: |
| 5538 | case TARGET_F_GETFD: |
| 5539 | case TARGET_F_SETFD: |
| 5540 | case TARGET_F_GETFL: |
| 5541 | case TARGET_F_SETFL: |
| 5542 | return cmd; |
| 5543 | case TARGET_F_GETLK: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5544 | return F_GETLK64; |
| 5545 | case TARGET_F_SETLK: |
| 5546 | return F_SETLK64; |
| 5547 | case TARGET_F_SETLKW: |
| 5548 | return F_SETLKW64; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5549 | case TARGET_F_GETOWN: |
| 5550 | return F_GETOWN; |
| 5551 | case TARGET_F_SETOWN: |
| 5552 | return F_SETOWN; |
| 5553 | case TARGET_F_GETSIG: |
| 5554 | return F_GETSIG; |
| 5555 | case TARGET_F_SETSIG: |
| 5556 | return F_SETSIG; |
| 5557 | #if TARGET_ABI_BITS == 32 |
| 5558 | case TARGET_F_GETLK64: |
| 5559 | return F_GETLK64; |
| 5560 | case TARGET_F_SETLK64: |
| 5561 | return F_SETLK64; |
| 5562 | case TARGET_F_SETLKW64: |
| 5563 | return F_SETLKW64; |
| 5564 | #endif |
Ulrich Hecht | 7e22e54 | 2009-07-24 19:10:27 +0200 | [diff] [blame] | 5565 | case TARGET_F_SETLEASE: |
| 5566 | return F_SETLEASE; |
| 5567 | case TARGET_F_GETLEASE: |
| 5568 | return F_GETLEASE; |
malc | fbd5de9 | 2009-09-06 06:31:59 +0400 | [diff] [blame] | 5569 | #ifdef F_DUPFD_CLOEXEC |
Ulrich Hecht | 7e22e54 | 2009-07-24 19:10:27 +0200 | [diff] [blame] | 5570 | case TARGET_F_DUPFD_CLOEXEC: |
| 5571 | return F_DUPFD_CLOEXEC; |
malc | fbd5de9 | 2009-09-06 06:31:59 +0400 | [diff] [blame] | 5572 | #endif |
Ulrich Hecht | 7e22e54 | 2009-07-24 19:10:27 +0200 | [diff] [blame] | 5573 | case TARGET_F_NOTIFY: |
| 5574 | return F_NOTIFY; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 5575 | #ifdef F_GETOWN_EX |
| 5576 | case TARGET_F_GETOWN_EX: |
| 5577 | return F_GETOWN_EX; |
| 5578 | #endif |
| 5579 | #ifdef F_SETOWN_EX |
| 5580 | case TARGET_F_SETOWN_EX: |
| 5581 | return F_SETOWN_EX; |
| 5582 | #endif |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5583 | default: |
| 5584 | return -TARGET_EINVAL; |
| 5585 | } |
| 5586 | return -TARGET_EINVAL; |
| 5587 | } |
| 5588 | |
Laurent Vivier | 2ba7f73 | 2013-01-10 21:42:48 +0100 | [diff] [blame] | 5589 | #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a } |
| 5590 | static const bitmask_transtbl flock_tbl[] = { |
| 5591 | TRANSTBL_CONVERT(F_RDLCK), |
| 5592 | TRANSTBL_CONVERT(F_WRLCK), |
| 5593 | TRANSTBL_CONVERT(F_UNLCK), |
| 5594 | TRANSTBL_CONVERT(F_EXLCK), |
| 5595 | TRANSTBL_CONVERT(F_SHLCK), |
| 5596 | { 0, 0, 0, 0 } |
| 5597 | }; |
| 5598 | |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5599 | static inline abi_long copy_from_user_flock(struct flock64 *fl, |
| 5600 | abi_ulong target_flock_addr) |
| 5601 | { |
| 5602 | struct target_flock *target_fl; |
| 5603 | short l_type; |
| 5604 | |
| 5605 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 5606 | return -TARGET_EFAULT; |
| 5607 | } |
| 5608 | |
| 5609 | __get_user(l_type, &target_fl->l_type); |
| 5610 | fl->l_type = target_to_host_bitmask(l_type, flock_tbl); |
| 5611 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 5612 | __get_user(fl->l_start, &target_fl->l_start); |
| 5613 | __get_user(fl->l_len, &target_fl->l_len); |
| 5614 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 5615 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 5616 | return 0; |
| 5617 | } |
| 5618 | |
| 5619 | static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr, |
| 5620 | const struct flock64 *fl) |
| 5621 | { |
| 5622 | struct target_flock *target_fl; |
| 5623 | short l_type; |
| 5624 | |
| 5625 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 5626 | return -TARGET_EFAULT; |
| 5627 | } |
| 5628 | |
| 5629 | l_type = host_to_target_bitmask(fl->l_type, flock_tbl); |
| 5630 | __put_user(l_type, &target_fl->l_type); |
| 5631 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 5632 | __put_user(fl->l_start, &target_fl->l_start); |
| 5633 | __put_user(fl->l_len, &target_fl->l_len); |
| 5634 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 5635 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 5636 | return 0; |
| 5637 | } |
| 5638 | |
| 5639 | typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr); |
| 5640 | typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl); |
| 5641 | |
| 5642 | #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32 |
| 5643 | static inline abi_long copy_from_user_eabi_flock64(struct flock64 *fl, |
| 5644 | abi_ulong target_flock_addr) |
| 5645 | { |
| 5646 | struct target_eabi_flock64 *target_fl; |
| 5647 | short l_type; |
| 5648 | |
| 5649 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 5650 | return -TARGET_EFAULT; |
| 5651 | } |
| 5652 | |
| 5653 | __get_user(l_type, &target_fl->l_type); |
| 5654 | fl->l_type = target_to_host_bitmask(l_type, flock_tbl); |
| 5655 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 5656 | __get_user(fl->l_start, &target_fl->l_start); |
| 5657 | __get_user(fl->l_len, &target_fl->l_len); |
| 5658 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 5659 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 5660 | return 0; |
| 5661 | } |
| 5662 | |
| 5663 | static inline abi_long copy_to_user_eabi_flock64(abi_ulong target_flock_addr, |
| 5664 | const struct flock64 *fl) |
| 5665 | { |
| 5666 | struct target_eabi_flock64 *target_fl; |
| 5667 | short l_type; |
| 5668 | |
| 5669 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 5670 | return -TARGET_EFAULT; |
| 5671 | } |
| 5672 | |
| 5673 | l_type = host_to_target_bitmask(fl->l_type, flock_tbl); |
| 5674 | __put_user(l_type, &target_fl->l_type); |
| 5675 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 5676 | __put_user(fl->l_start, &target_fl->l_start); |
| 5677 | __put_user(fl->l_len, &target_fl->l_len); |
| 5678 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 5679 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 5680 | return 0; |
| 5681 | } |
| 5682 | #endif |
| 5683 | |
| 5684 | static inline abi_long copy_from_user_flock64(struct flock64 *fl, |
| 5685 | abi_ulong target_flock_addr) |
| 5686 | { |
| 5687 | struct target_flock64 *target_fl; |
| 5688 | short l_type; |
| 5689 | |
| 5690 | if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { |
| 5691 | return -TARGET_EFAULT; |
| 5692 | } |
| 5693 | |
| 5694 | __get_user(l_type, &target_fl->l_type); |
| 5695 | fl->l_type = target_to_host_bitmask(l_type, flock_tbl); |
| 5696 | __get_user(fl->l_whence, &target_fl->l_whence); |
| 5697 | __get_user(fl->l_start, &target_fl->l_start); |
| 5698 | __get_user(fl->l_len, &target_fl->l_len); |
| 5699 | __get_user(fl->l_pid, &target_fl->l_pid); |
| 5700 | unlock_user_struct(target_fl, target_flock_addr, 0); |
| 5701 | return 0; |
| 5702 | } |
| 5703 | |
| 5704 | static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr, |
| 5705 | const struct flock64 *fl) |
| 5706 | { |
| 5707 | struct target_flock64 *target_fl; |
| 5708 | short l_type; |
| 5709 | |
| 5710 | if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { |
| 5711 | return -TARGET_EFAULT; |
| 5712 | } |
| 5713 | |
| 5714 | l_type = host_to_target_bitmask(fl->l_type, flock_tbl); |
| 5715 | __put_user(l_type, &target_fl->l_type); |
| 5716 | __put_user(fl->l_whence, &target_fl->l_whence); |
| 5717 | __put_user(fl->l_start, &target_fl->l_start); |
| 5718 | __put_user(fl->l_len, &target_fl->l_len); |
| 5719 | __put_user(fl->l_pid, &target_fl->l_pid); |
| 5720 | unlock_user_struct(target_fl, target_flock_addr, 1); |
| 5721 | return 0; |
| 5722 | } |
| 5723 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5724 | static abi_long do_fcntl(int fd, int cmd, abi_ulong arg) |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5725 | { |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 5726 | struct flock64 fl64; |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 5727 | #ifdef F_GETOWN_EX |
| 5728 | struct f_owner_ex fox; |
| 5729 | struct target_f_owner_ex *target_fox; |
| 5730 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5731 | abi_long ret; |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5732 | int host_cmd = target_to_host_fcntl_cmd(cmd); |
| 5733 | |
| 5734 | if (host_cmd == -TARGET_EINVAL) |
| 5735 | return host_cmd; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 5736 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5737 | switch(cmd) { |
| 5738 | case TARGET_F_GETLK: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5739 | ret = copy_from_user_flock(&fl64, arg); |
| 5740 | if (ret) { |
| 5741 | return ret; |
| 5742 | } |
| 5743 | ret = get_errno(fcntl(fd, host_cmd, &fl64)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5744 | if (ret == 0) { |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5745 | ret = copy_to_user_flock(arg, &fl64); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5746 | } |
| 5747 | break; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5748 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5749 | case TARGET_F_SETLK: |
| 5750 | case TARGET_F_SETLKW: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5751 | ret = copy_from_user_flock(&fl64, arg); |
| 5752 | if (ret) { |
| 5753 | return ret; |
| 5754 | } |
| 5755 | ret = get_errno(fcntl(fd, host_cmd, &fl64)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5756 | break; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5757 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5758 | case TARGET_F_GETLK64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5759 | ret = copy_from_user_flock64(&fl64, arg); |
| 5760 | if (ret) { |
| 5761 | return ret; |
| 5762 | } |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5763 | ret = get_errno(fcntl(fd, host_cmd, &fl64)); |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 5764 | if (ret == 0) { |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5765 | ret = copy_to_user_flock64(arg, &fl64); |
ths | 43f238d | 2007-01-05 20:55:49 +0000 | [diff] [blame] | 5766 | } |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 5767 | break; |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5768 | case TARGET_F_SETLK64: |
| 5769 | case TARGET_F_SETLKW64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 5770 | ret = copy_from_user_flock64(&fl64, arg); |
| 5771 | if (ret) { |
| 5772 | return ret; |
| 5773 | } |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5774 | ret = get_errno(fcntl(fd, host_cmd, &fl64)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5775 | break; |
| 5776 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5777 | case TARGET_F_GETFL: |
| 5778 | ret = get_errno(fcntl(fd, host_cmd, arg)); |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 5779 | if (ret >= 0) { |
| 5780 | ret = host_to_target_bitmask(ret, fcntl_flags_tbl); |
| 5781 | } |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 5782 | break; |
| 5783 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5784 | case TARGET_F_SETFL: |
| 5785 | ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl))); |
| 5786 | break; |
| 5787 | |
Andreas Schwab | 8d5d300 | 2014-03-07 15:24:08 +0100 | [diff] [blame] | 5788 | #ifdef F_GETOWN_EX |
| 5789 | case TARGET_F_GETOWN_EX: |
| 5790 | ret = get_errno(fcntl(fd, host_cmd, &fox)); |
| 5791 | if (ret >= 0) { |
| 5792 | if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0)) |
| 5793 | return -TARGET_EFAULT; |
| 5794 | target_fox->type = tswap32(fox.type); |
| 5795 | target_fox->pid = tswap32(fox.pid); |
| 5796 | unlock_user_struct(target_fox, arg, 1); |
| 5797 | } |
| 5798 | break; |
| 5799 | #endif |
| 5800 | |
| 5801 | #ifdef F_SETOWN_EX |
| 5802 | case TARGET_F_SETOWN_EX: |
| 5803 | if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1)) |
| 5804 | return -TARGET_EFAULT; |
| 5805 | fox.type = tswap32(target_fox->type); |
| 5806 | fox.pid = tswap32(target_fox->pid); |
| 5807 | unlock_user_struct(target_fox, arg, 0); |
| 5808 | ret = get_errno(fcntl(fd, host_cmd, &fox)); |
| 5809 | break; |
| 5810 | #endif |
| 5811 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5812 | case TARGET_F_SETOWN: |
| 5813 | case TARGET_F_GETOWN: |
| 5814 | case TARGET_F_SETSIG: |
| 5815 | case TARGET_F_GETSIG: |
Ulrich Hecht | 7e22e54 | 2009-07-24 19:10:27 +0200 | [diff] [blame] | 5816 | case TARGET_F_SETLEASE: |
| 5817 | case TARGET_F_GETLEASE: |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 5818 | ret = get_errno(fcntl(fd, host_cmd, arg)); |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 5819 | break; |
| 5820 | |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5821 | default: |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 5822 | ret = get_errno(fcntl(fd, cmd, arg)); |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5823 | break; |
| 5824 | } |
| 5825 | return ret; |
| 5826 | } |
| 5827 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 5828 | #ifdef USE_UID16 |
bellard | 7775e9e | 2003-05-14 22:46:48 +0000 | [diff] [blame] | 5829 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 5830 | static inline int high2lowuid(int uid) |
| 5831 | { |
| 5832 | if (uid > 65535) |
| 5833 | return 65534; |
| 5834 | else |
| 5835 | return uid; |
| 5836 | } |
| 5837 | |
| 5838 | static inline int high2lowgid(int gid) |
| 5839 | { |
| 5840 | if (gid > 65535) |
| 5841 | return 65534; |
| 5842 | else |
| 5843 | return gid; |
| 5844 | } |
| 5845 | |
| 5846 | static inline int low2highuid(int uid) |
| 5847 | { |
| 5848 | if ((int16_t)uid == -1) |
| 5849 | return -1; |
| 5850 | else |
| 5851 | return uid; |
| 5852 | } |
| 5853 | |
| 5854 | static inline int low2highgid(int gid) |
| 5855 | { |
| 5856 | if ((int16_t)gid == -1) |
| 5857 | return -1; |
| 5858 | else |
| 5859 | return gid; |
| 5860 | } |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 5861 | static inline int tswapid(int id) |
| 5862 | { |
| 5863 | return tswap16(id); |
| 5864 | } |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 5865 | |
| 5866 | #define put_user_id(x, gaddr) put_user_u16(x, gaddr) |
| 5867 | |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 5868 | #else /* !USE_UID16 */ |
| 5869 | static inline int high2lowuid(int uid) |
| 5870 | { |
| 5871 | return uid; |
| 5872 | } |
| 5873 | static inline int high2lowgid(int gid) |
| 5874 | { |
| 5875 | return gid; |
| 5876 | } |
| 5877 | static inline int low2highuid(int uid) |
| 5878 | { |
| 5879 | return uid; |
| 5880 | } |
| 5881 | static inline int low2highgid(int gid) |
| 5882 | { |
| 5883 | return gid; |
| 5884 | } |
| 5885 | static inline int tswapid(int id) |
| 5886 | { |
| 5887 | return tswap32(id); |
| 5888 | } |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 5889 | |
| 5890 | #define put_user_id(x, gaddr) put_user_u32(x, gaddr) |
| 5891 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 5892 | #endif /* USE_UID16 */ |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 5893 | |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 5894 | /* We must do direct syscalls for setting UID/GID, because we want to |
| 5895 | * implement the Linux system call semantics of "change only for this thread", |
| 5896 | * not the libc/POSIX semantics of "change for all threads in process". |
| 5897 | * (See http://ewontfix.com/17/ for more details.) |
| 5898 | * We use the 32-bit version of the syscalls if present; if it is not |
| 5899 | * then either the host architecture supports 32-bit UIDs natively with |
| 5900 | * the standard syscall, or the 16-bit UID is the best we can do. |
| 5901 | */ |
| 5902 | #ifdef __NR_setuid32 |
| 5903 | #define __NR_sys_setuid __NR_setuid32 |
| 5904 | #else |
| 5905 | #define __NR_sys_setuid __NR_setuid |
| 5906 | #endif |
| 5907 | #ifdef __NR_setgid32 |
| 5908 | #define __NR_sys_setgid __NR_setgid32 |
| 5909 | #else |
| 5910 | #define __NR_sys_setgid __NR_setgid |
| 5911 | #endif |
| 5912 | #ifdef __NR_setresuid32 |
| 5913 | #define __NR_sys_setresuid __NR_setresuid32 |
| 5914 | #else |
| 5915 | #define __NR_sys_setresuid __NR_setresuid |
| 5916 | #endif |
| 5917 | #ifdef __NR_setresgid32 |
| 5918 | #define __NR_sys_setresgid __NR_setresgid32 |
| 5919 | #else |
| 5920 | #define __NR_sys_setresgid __NR_setresgid |
| 5921 | #endif |
| 5922 | |
| 5923 | _syscall1(int, sys_setuid, uid_t, uid) |
| 5924 | _syscall1(int, sys_setgid, gid_t, gid) |
| 5925 | _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid) |
| 5926 | _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid) |
| 5927 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5928 | void syscall_init(void) |
| 5929 | { |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5930 | IOCTLEntry *ie; |
| 5931 | const argtype *arg_type; |
| 5932 | int size; |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 5933 | int i; |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5934 | |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 5935 | thunk_init(STRUCT_MAX); |
| 5936 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 5937 | #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5938 | #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] | 5939 | #include "syscall_types.h" |
| 5940 | #undef STRUCT |
| 5941 | #undef STRUCT_SPECIAL |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5942 | |
Peter Maydell | dd6e957 | 2012-07-23 08:07:22 +0000 | [diff] [blame] | 5943 | /* Build target_to_host_errno_table[] table from |
| 5944 | * host_to_target_errno_table[]. */ |
| 5945 | for (i = 0; i < ERRNO_TABLE_SIZE; i++) { |
| 5946 | target_to_host_errno_table[host_to_target_errno_table[i]] = i; |
| 5947 | } |
| 5948 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5949 | /* we patch the ioctl size if necessary. We rely on the fact that |
| 5950 | no ioctl has all the bits at '1' in the size field */ |
| 5951 | ie = ioctl_entries; |
| 5952 | while (ie->target_cmd != 0) { |
| 5953 | if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) == |
| 5954 | TARGET_IOC_SIZEMASK) { |
| 5955 | arg_type = ie->arg_type; |
| 5956 | if (arg_type[0] != TYPE_PTR) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5957 | fprintf(stderr, "cannot patch size for ioctl 0x%x\n", |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5958 | ie->target_cmd); |
| 5959 | exit(1); |
| 5960 | } |
| 5961 | arg_type++; |
| 5962 | size = thunk_type_size(arg_type, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5963 | ie->target_cmd = (ie->target_cmd & |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5964 | ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) | |
| 5965 | (size << TARGET_IOC_SIZESHIFT); |
| 5966 | } |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 5967 | |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5968 | /* automatic consistency check if same arch */ |
balrog | 872ea0c | 2008-09-21 02:31:19 +0000 | [diff] [blame] | 5969 | #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \ |
| 5970 | (defined(__x86_64__) && defined(TARGET_X86_64)) |
| 5971 | if (unlikely(ie->target_cmd != ie->host_cmd)) { |
| 5972 | fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n", |
| 5973 | ie->name, ie->target_cmd, ie->host_cmd); |
bellard | 2ab83ea | 2003-06-15 19:56:46 +0000 | [diff] [blame] | 5974 | } |
| 5975 | #endif |
| 5976 | ie++; |
| 5977 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 5978 | } |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 5979 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5980 | #if TARGET_ABI_BITS == 32 |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 5981 | static inline uint64_t target_offset64(uint32_t word0, uint32_t word1) |
| 5982 | { |
ths | af325d3 | 2008-06-10 15:29:15 +0000 | [diff] [blame] | 5983 | #ifdef TARGET_WORDS_BIGENDIAN |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 5984 | return ((uint64_t)word0 << 32) | word1; |
| 5985 | #else |
| 5986 | return ((uint64_t)word1 << 32) | word0; |
| 5987 | #endif |
| 5988 | } |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5989 | #else /* TARGET_ABI_BITS == 32 */ |
j_mayer | 3240710 | 2007-09-26 23:01:49 +0000 | [diff] [blame] | 5990 | static inline uint64_t target_offset64(uint64_t word0, uint64_t word1) |
| 5991 | { |
| 5992 | return word0; |
| 5993 | } |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5994 | #endif /* TARGET_ABI_BITS != 32 */ |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 5995 | |
| 5996 | #ifdef TARGET_NR_truncate64 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 5997 | static inline abi_long target_truncate64(void *cpu_env, const char *arg1, |
| 5998 | abi_long arg2, |
| 5999 | abi_long arg3, |
| 6000 | abi_long arg4) |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6001 | { |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 6002 | if (regpairs_aligned(cpu_env)) { |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6003 | arg2 = arg3; |
| 6004 | arg3 = arg4; |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 6005 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6006 | return get_errno(truncate64(arg1, target_offset64(arg2, arg3))); |
| 6007 | } |
| 6008 | #endif |
| 6009 | |
| 6010 | #ifdef TARGET_NR_ftruncate64 |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6011 | static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1, |
| 6012 | abi_long arg2, |
| 6013 | abi_long arg3, |
| 6014 | abi_long arg4) |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6015 | { |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 6016 | if (regpairs_aligned(cpu_env)) { |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6017 | arg2 = arg3; |
| 6018 | arg3 = arg4; |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 6019 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 6020 | return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3))); |
| 6021 | } |
| 6022 | #endif |
| 6023 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6024 | static inline abi_long target_to_host_timespec(struct timespec *host_ts, |
| 6025 | abi_ulong target_addr) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6026 | { |
| 6027 | struct target_timespec *target_ts; |
| 6028 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6029 | if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) |
| 6030 | return -TARGET_EFAULT; |
Peter Maydell | c7e35da | 2016-05-19 12:01:40 +0100 | [diff] [blame] | 6031 | __get_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 6032 | __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6033 | unlock_user_struct(target_ts, target_addr, 0); |
bellard | b255bfa | 2008-05-10 21:51:02 +0000 | [diff] [blame] | 6034 | return 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6037 | static inline abi_long host_to_target_timespec(abi_ulong target_addr, |
| 6038 | struct timespec *host_ts) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6039 | { |
| 6040 | struct target_timespec *target_ts; |
| 6041 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6042 | if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) |
| 6043 | return -TARGET_EFAULT; |
Peter Maydell | c7e35da | 2016-05-19 12:01:40 +0100 | [diff] [blame] | 6044 | __put_user(host_ts->tv_sec, &target_ts->tv_sec); |
| 6045 | __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6046 | unlock_user_struct(target_ts, target_addr, 1); |
bellard | b255bfa | 2008-05-10 21:51:02 +0000 | [diff] [blame] | 6047 | return 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 6050 | static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec, |
| 6051 | abi_ulong target_addr) |
| 6052 | { |
| 6053 | struct target_itimerspec *target_itspec; |
| 6054 | |
| 6055 | if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) { |
| 6056 | return -TARGET_EFAULT; |
| 6057 | } |
| 6058 | |
| 6059 | host_itspec->it_interval.tv_sec = |
| 6060 | tswapal(target_itspec->it_interval.tv_sec); |
| 6061 | host_itspec->it_interval.tv_nsec = |
| 6062 | tswapal(target_itspec->it_interval.tv_nsec); |
| 6063 | host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec); |
| 6064 | host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec); |
| 6065 | |
| 6066 | unlock_user_struct(target_itspec, target_addr, 1); |
| 6067 | return 0; |
| 6068 | } |
| 6069 | |
| 6070 | static inline abi_long host_to_target_itimerspec(abi_ulong target_addr, |
| 6071 | struct itimerspec *host_its) |
| 6072 | { |
| 6073 | struct target_itimerspec *target_itspec; |
| 6074 | |
| 6075 | if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) { |
| 6076 | return -TARGET_EFAULT; |
| 6077 | } |
| 6078 | |
| 6079 | target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec); |
| 6080 | target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec); |
| 6081 | |
| 6082 | target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec); |
| 6083 | target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec); |
| 6084 | |
| 6085 | unlock_user_struct(target_itspec, target_addr, 0); |
| 6086 | return 0; |
| 6087 | } |
| 6088 | |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 6089 | static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp, |
| 6090 | abi_ulong target_addr) |
| 6091 | { |
| 6092 | struct target_sigevent *target_sevp; |
| 6093 | |
| 6094 | if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) { |
| 6095 | return -TARGET_EFAULT; |
| 6096 | } |
| 6097 | |
| 6098 | /* This union is awkward on 64 bit systems because it has a 32 bit |
| 6099 | * integer and a pointer in it; we follow the conversion approach |
| 6100 | * used for handling sigval types in signal.c so the guest should get |
| 6101 | * the correct value back even if we did a 64 bit byteswap and it's |
| 6102 | * using the 32 bit integer. |
| 6103 | */ |
| 6104 | host_sevp->sigev_value.sival_ptr = |
| 6105 | (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr); |
| 6106 | host_sevp->sigev_signo = |
| 6107 | target_to_host_signal(tswap32(target_sevp->sigev_signo)); |
| 6108 | host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify); |
| 6109 | host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid); |
| 6110 | |
| 6111 | unlock_user_struct(target_sevp, target_addr, 1); |
| 6112 | return 0; |
| 6113 | } |
| 6114 | |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 6115 | #if defined(TARGET_NR_mlockall) |
| 6116 | static inline int target_to_host_mlockall_arg(int arg) |
| 6117 | { |
| 6118 | int result = 0; |
| 6119 | |
| 6120 | if (arg & TARGET_MLOCKALL_MCL_CURRENT) { |
| 6121 | result |= MCL_CURRENT; |
| 6122 | } |
| 6123 | if (arg & TARGET_MLOCKALL_MCL_FUTURE) { |
| 6124 | result |= MCL_FUTURE; |
| 6125 | } |
| 6126 | return result; |
| 6127 | } |
| 6128 | #endif |
| 6129 | |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6130 | static inline abi_long host_to_target_stat64(void *cpu_env, |
| 6131 | abi_ulong target_addr, |
| 6132 | struct stat *host_st) |
| 6133 | { |
Alexander Graf | 0970119 | 2013-09-03 20:12:15 +0100 | [diff] [blame] | 6134 | #if defined(TARGET_ARM) && defined(TARGET_ABI32) |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6135 | if (((CPUARMState *)cpu_env)->eabi) { |
| 6136 | struct target_eabi_stat64 *target_st; |
| 6137 | |
| 6138 | if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) |
| 6139 | return -TARGET_EFAULT; |
| 6140 | memset(target_st, 0, sizeof(struct target_eabi_stat64)); |
| 6141 | __put_user(host_st->st_dev, &target_st->st_dev); |
| 6142 | __put_user(host_st->st_ino, &target_st->st_ino); |
| 6143 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
| 6144 | __put_user(host_st->st_ino, &target_st->__st_ino); |
| 6145 | #endif |
| 6146 | __put_user(host_st->st_mode, &target_st->st_mode); |
| 6147 | __put_user(host_st->st_nlink, &target_st->st_nlink); |
| 6148 | __put_user(host_st->st_uid, &target_st->st_uid); |
| 6149 | __put_user(host_st->st_gid, &target_st->st_gid); |
| 6150 | __put_user(host_st->st_rdev, &target_st->st_rdev); |
| 6151 | __put_user(host_st->st_size, &target_st->st_size); |
| 6152 | __put_user(host_st->st_blksize, &target_st->st_blksize); |
| 6153 | __put_user(host_st->st_blocks, &target_st->st_blocks); |
| 6154 | __put_user(host_st->st_atime, &target_st->target_st_atime); |
| 6155 | __put_user(host_st->st_mtime, &target_st->target_st_mtime); |
| 6156 | __put_user(host_st->st_ctime, &target_st->target_st_ctime); |
| 6157 | unlock_user_struct(target_st, target_addr, 1); |
| 6158 | } else |
| 6159 | #endif |
| 6160 | { |
Stefan Weil | 20d155b | 2013-10-30 22:52:24 +0100 | [diff] [blame] | 6161 | #if defined(TARGET_HAS_STRUCT_STAT64) |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6162 | struct target_stat64 *target_st; |
Stefan Weil | 20d155b | 2013-10-30 22:52:24 +0100 | [diff] [blame] | 6163 | #else |
| 6164 | struct target_stat *target_st; |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 6165 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6166 | |
| 6167 | if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) |
| 6168 | return -TARGET_EFAULT; |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 6169 | memset(target_st, 0, sizeof(*target_st)); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6170 | __put_user(host_st->st_dev, &target_st->st_dev); |
| 6171 | __put_user(host_st->st_ino, &target_st->st_ino); |
| 6172 | #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO |
| 6173 | __put_user(host_st->st_ino, &target_st->__st_ino); |
| 6174 | #endif |
| 6175 | __put_user(host_st->st_mode, &target_st->st_mode); |
| 6176 | __put_user(host_st->st_nlink, &target_st->st_nlink); |
| 6177 | __put_user(host_st->st_uid, &target_st->st_uid); |
| 6178 | __put_user(host_st->st_gid, &target_st->st_gid); |
| 6179 | __put_user(host_st->st_rdev, &target_st->st_rdev); |
| 6180 | /* XXX: better use of kernel struct */ |
| 6181 | __put_user(host_st->st_size, &target_st->st_size); |
| 6182 | __put_user(host_st->st_blksize, &target_st->st_blksize); |
| 6183 | __put_user(host_st->st_blocks, &target_st->st_blocks); |
| 6184 | __put_user(host_st->st_atime, &target_st->target_st_atime); |
| 6185 | __put_user(host_st->st_mtime, &target_st->target_st_mtime); |
| 6186 | __put_user(host_st->st_ctime, &target_st->target_st_ctime); |
| 6187 | unlock_user_struct(target_st, target_addr, 1); |
| 6188 | } |
| 6189 | |
| 6190 | return 0; |
| 6191 | } |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 6192 | |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6193 | /* ??? Using host futex calls even when target atomic operations |
| 6194 | are not really atomic probably breaks things. However implementing |
| 6195 | futexes locally would make futexes shared between multiple processes |
| 6196 | tricky. However they're probably useless because guest atomic |
| 6197 | operations won't work either. */ |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 6198 | static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout, |
| 6199 | target_ulong uaddr2, int val3) |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6200 | { |
| 6201 | struct timespec ts, *pts; |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 6202 | int base_op; |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6203 | |
| 6204 | /* ??? We assume FUTEX_* constants are the same on both host |
| 6205 | and target. */ |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 6206 | #ifdef FUTEX_CMD_MASK |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 6207 | base_op = op & FUTEX_CMD_MASK; |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 6208 | #else |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 6209 | base_op = op; |
Martin Mohring | a29ccd6 | 2009-05-04 21:34:56 +0300 | [diff] [blame] | 6210 | #endif |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 6211 | switch (base_op) { |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6212 | case FUTEX_WAIT: |
John Rigby | cce246e | 2013-02-23 16:14:07 -0700 | [diff] [blame] | 6213 | case FUTEX_WAIT_BITSET: |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6214 | if (timeout) { |
| 6215 | pts = &ts; |
| 6216 | target_to_host_timespec(pts, timeout); |
| 6217 | } else { |
| 6218 | pts = NULL; |
| 6219 | } |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 6220 | return get_errno(safe_futex(g2h(uaddr), op, tswap32(val), |
John Rigby | cce246e | 2013-02-23 16:14:07 -0700 | [diff] [blame] | 6221 | pts, NULL, val3)); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6222 | case FUTEX_WAKE: |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 6223 | return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0)); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6224 | case FUTEX_FD: |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 6225 | return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0)); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6226 | case FUTEX_REQUEUE: |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6227 | case FUTEX_CMP_REQUEUE: |
Nathan Froyd | a16aae0 | 2009-08-03 08:43:29 -0700 | [diff] [blame] | 6228 | case FUTEX_WAKE_OP: |
| 6229 | /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the |
| 6230 | TIMEOUT parameter is interpreted as a uint32_t by the kernel. |
| 6231 | But the prototype takes a `struct timespec *'; insert casts |
| 6232 | to satisfy the compiler. We do not need to tswap TIMEOUT |
| 6233 | since it's not compared to guest memory. */ |
| 6234 | pts = (struct timespec *)(uintptr_t) timeout; |
Peter Maydell | d509eeb | 2016-05-12 18:47:52 +0100 | [diff] [blame] | 6235 | return get_errno(safe_futex(g2h(uaddr), op, val, pts, |
| 6236 | g2h(uaddr2), |
| 6237 | (base_op == FUTEX_CMP_REQUEUE |
| 6238 | ? tswap32(val3) |
| 6239 | : val3))); |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6240 | default: |
| 6241 | return -TARGET_ENOSYS; |
| 6242 | } |
| 6243 | } |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 6244 | #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 6245 | static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname, |
| 6246 | abi_long handle, abi_long mount_id, |
| 6247 | abi_long flags) |
| 6248 | { |
| 6249 | struct file_handle *target_fh; |
| 6250 | struct file_handle *fh; |
| 6251 | int mid = 0; |
| 6252 | abi_long ret; |
| 6253 | char *name; |
| 6254 | unsigned int size, total_size; |
| 6255 | |
| 6256 | if (get_user_s32(size, handle)) { |
| 6257 | return -TARGET_EFAULT; |
| 6258 | } |
| 6259 | |
| 6260 | name = lock_user_string(pathname); |
| 6261 | if (!name) { |
| 6262 | return -TARGET_EFAULT; |
| 6263 | } |
| 6264 | |
| 6265 | total_size = sizeof(struct file_handle) + size; |
| 6266 | target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0); |
| 6267 | if (!target_fh) { |
| 6268 | unlock_user(name, pathname, 0); |
| 6269 | return -TARGET_EFAULT; |
| 6270 | } |
| 6271 | |
| 6272 | fh = g_malloc0(total_size); |
| 6273 | fh->handle_bytes = size; |
| 6274 | |
| 6275 | ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags)); |
| 6276 | unlock_user(name, pathname, 0); |
| 6277 | |
| 6278 | /* man name_to_handle_at(2): |
| 6279 | * Other than the use of the handle_bytes field, the caller should treat |
| 6280 | * the file_handle structure as an opaque data type |
| 6281 | */ |
| 6282 | |
| 6283 | memcpy(target_fh, fh, total_size); |
| 6284 | target_fh->handle_bytes = tswap32(fh->handle_bytes); |
| 6285 | target_fh->handle_type = tswap32(fh->handle_type); |
| 6286 | g_free(fh); |
| 6287 | unlock_user(target_fh, handle, total_size); |
| 6288 | |
| 6289 | if (put_user_s32(mid, mount_id)) { |
| 6290 | return -TARGET_EFAULT; |
| 6291 | } |
| 6292 | |
| 6293 | return ret; |
| 6294 | |
| 6295 | } |
| 6296 | #endif |
| 6297 | |
| 6298 | #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 6299 | static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle, |
| 6300 | abi_long flags) |
| 6301 | { |
| 6302 | struct file_handle *target_fh; |
| 6303 | struct file_handle *fh; |
| 6304 | unsigned int size, total_size; |
| 6305 | abi_long ret; |
| 6306 | |
| 6307 | if (get_user_s32(size, handle)) { |
| 6308 | return -TARGET_EFAULT; |
| 6309 | } |
| 6310 | |
| 6311 | total_size = sizeof(struct file_handle) + size; |
| 6312 | target_fh = lock_user(VERIFY_READ, handle, total_size, 1); |
| 6313 | if (!target_fh) { |
| 6314 | return -TARGET_EFAULT; |
| 6315 | } |
| 6316 | |
Thomas Huth | e9d49d5 | 2015-10-09 17:56:38 +0200 | [diff] [blame] | 6317 | fh = g_memdup(target_fh, total_size); |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 6318 | fh->handle_bytes = size; |
| 6319 | fh->handle_type = tswap32(target_fh->handle_type); |
| 6320 | |
| 6321 | ret = get_errno(open_by_handle_at(mount_fd, fh, |
| 6322 | target_to_host_bitmask(flags, fcntl_flags_tbl))); |
| 6323 | |
| 6324 | g_free(fh); |
| 6325 | |
| 6326 | unlock_user(target_fh, handle, total_size); |
| 6327 | |
| 6328 | return ret; |
| 6329 | } |
| 6330 | #endif |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 6331 | |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6332 | #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4) |
| 6333 | |
| 6334 | /* signalfd siginfo conversion */ |
| 6335 | |
| 6336 | static void |
| 6337 | host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo, |
| 6338 | const struct signalfd_siginfo *info) |
| 6339 | { |
| 6340 | int sig = host_to_target_signal(info->ssi_signo); |
| 6341 | |
| 6342 | /* linux/signalfd.h defines a ssi_addr_lsb |
| 6343 | * not defined in sys/signalfd.h but used by some kernels |
| 6344 | */ |
| 6345 | |
| 6346 | #ifdef BUS_MCEERR_AO |
| 6347 | if (tinfo->ssi_signo == SIGBUS && |
| 6348 | (tinfo->ssi_code == BUS_MCEERR_AR || |
| 6349 | tinfo->ssi_code == BUS_MCEERR_AO)) { |
| 6350 | uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1); |
| 6351 | uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1); |
| 6352 | *tssi_addr_lsb = tswap16(*ssi_addr_lsb); |
| 6353 | } |
| 6354 | #endif |
| 6355 | |
| 6356 | tinfo->ssi_signo = tswap32(sig); |
| 6357 | tinfo->ssi_errno = tswap32(tinfo->ssi_errno); |
| 6358 | tinfo->ssi_code = tswap32(info->ssi_code); |
| 6359 | tinfo->ssi_pid = tswap32(info->ssi_pid); |
| 6360 | tinfo->ssi_uid = tswap32(info->ssi_uid); |
| 6361 | tinfo->ssi_fd = tswap32(info->ssi_fd); |
| 6362 | tinfo->ssi_tid = tswap32(info->ssi_tid); |
| 6363 | tinfo->ssi_band = tswap32(info->ssi_band); |
| 6364 | tinfo->ssi_overrun = tswap32(info->ssi_overrun); |
| 6365 | tinfo->ssi_trapno = tswap32(info->ssi_trapno); |
| 6366 | tinfo->ssi_status = tswap32(info->ssi_status); |
| 6367 | tinfo->ssi_int = tswap32(info->ssi_int); |
| 6368 | tinfo->ssi_ptr = tswap64(info->ssi_ptr); |
| 6369 | tinfo->ssi_utime = tswap64(info->ssi_utime); |
| 6370 | tinfo->ssi_stime = tswap64(info->ssi_stime); |
| 6371 | tinfo->ssi_addr = tswap64(info->ssi_addr); |
| 6372 | } |
| 6373 | |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 6374 | static abi_long host_to_target_data_signalfd(void *buf, size_t len) |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6375 | { |
| 6376 | int i; |
| 6377 | |
| 6378 | for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) { |
| 6379 | host_to_target_signalfd_siginfo(buf + i, buf + i); |
| 6380 | } |
| 6381 | |
| 6382 | return len; |
| 6383 | } |
| 6384 | |
| 6385 | static TargetFdTrans target_signalfd_trans = { |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 6386 | .host_to_target_data = host_to_target_data_signalfd, |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6387 | }; |
| 6388 | |
| 6389 | static abi_long do_signalfd4(int fd, abi_long mask, int flags) |
| 6390 | { |
| 6391 | int host_flags; |
| 6392 | target_sigset_t *target_mask; |
| 6393 | sigset_t host_mask; |
| 6394 | abi_long ret; |
| 6395 | |
| 6396 | if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) { |
| 6397 | return -TARGET_EINVAL; |
| 6398 | } |
| 6399 | if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) { |
| 6400 | return -TARGET_EFAULT; |
| 6401 | } |
| 6402 | |
| 6403 | target_to_host_sigset(&host_mask, target_mask); |
| 6404 | |
| 6405 | host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); |
| 6406 | |
| 6407 | ret = get_errno(signalfd(fd, &host_mask, host_flags)); |
| 6408 | if (ret >= 0) { |
| 6409 | fd_trans_register(ret, &target_signalfd_trans); |
| 6410 | } |
| 6411 | |
| 6412 | unlock_user_struct(target_mask, mask, 0); |
| 6413 | |
| 6414 | return ret; |
| 6415 | } |
| 6416 | #endif |
| 6417 | |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 6418 | /* Map host to target signal numbers for the wait family of syscalls. |
| 6419 | Assume all other status bits are the same. */ |
Richard Henderson | a05c640 | 2012-09-15 11:34:20 -0700 | [diff] [blame] | 6420 | int host_to_target_waitstatus(int status) |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 6421 | { |
| 6422 | if (WIFSIGNALED(status)) { |
| 6423 | return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f); |
| 6424 | } |
| 6425 | if (WIFSTOPPED(status)) { |
| 6426 | return (host_to_target_signal(WSTOPSIG(status)) << 8) |
| 6427 | | (status & 0xff); |
| 6428 | } |
| 6429 | return status; |
| 6430 | } |
| 6431 | |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 6432 | static int open_self_cmdline(void *cpu_env, int fd) |
| 6433 | { |
| 6434 | int fd_orig = -1; |
| 6435 | bool word_skipped = false; |
| 6436 | |
| 6437 | fd_orig = open("/proc/self/cmdline", O_RDONLY); |
| 6438 | if (fd_orig < 0) { |
| 6439 | return fd_orig; |
| 6440 | } |
| 6441 | |
| 6442 | while (true) { |
| 6443 | ssize_t nb_read; |
| 6444 | char buf[128]; |
| 6445 | char *cp_buf = buf; |
| 6446 | |
| 6447 | nb_read = read(fd_orig, buf, sizeof(buf)); |
| 6448 | if (nb_read < 0) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6449 | int e = errno; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 6450 | fd_orig = close(fd_orig); |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6451 | errno = e; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 6452 | return -1; |
| 6453 | } else if (nb_read == 0) { |
| 6454 | break; |
| 6455 | } |
| 6456 | |
| 6457 | if (!word_skipped) { |
| 6458 | /* Skip the first string, which is the path to qemu-*-static |
| 6459 | instead of the actual command. */ |
| 6460 | cp_buf = memchr(buf, 0, sizeof(buf)); |
| 6461 | if (cp_buf) { |
| 6462 | /* Null byte found, skip one string */ |
| 6463 | cp_buf++; |
| 6464 | nb_read -= cp_buf - buf; |
| 6465 | word_skipped = true; |
| 6466 | } |
| 6467 | } |
| 6468 | |
| 6469 | if (word_skipped) { |
| 6470 | if (write(fd, cp_buf, nb_read) != nb_read) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6471 | int e = errno; |
zhanghailiang | 680dfde | 2014-08-22 16:23:51 +0800 | [diff] [blame] | 6472 | close(fd_orig); |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6473 | errno = e; |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 6474 | return -1; |
| 6475 | } |
| 6476 | } |
| 6477 | } |
| 6478 | |
| 6479 | return close(fd_orig); |
| 6480 | } |
| 6481 | |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 6482 | static int open_self_maps(void *cpu_env, int fd) |
| 6483 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6484 | CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env); |
| 6485 | TaskState *ts = cpu->opaque; |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 6486 | FILE *fp; |
| 6487 | char *line = NULL; |
| 6488 | size_t len = 0; |
| 6489 | ssize_t read; |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 6490 | |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 6491 | fp = fopen("/proc/self/maps", "r"); |
| 6492 | if (fp == NULL) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6493 | return -1; |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 6494 | } |
| 6495 | |
| 6496 | while ((read = getline(&line, &len, fp)) != -1) { |
| 6497 | int fields, dev_maj, dev_min, inode; |
| 6498 | uint64_t min, max, offset; |
| 6499 | char flag_r, flag_w, flag_x, flag_p; |
| 6500 | char path[512] = ""; |
| 6501 | fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d" |
| 6502 | " %512s", &min, &max, &flag_r, &flag_w, &flag_x, |
| 6503 | &flag_p, &offset, &dev_maj, &dev_min, &inode, path); |
| 6504 | |
| 6505 | if ((fields < 10) || (fields > 11)) { |
| 6506 | continue; |
| 6507 | } |
Mikhail Ilyin | d67f4aa | 2014-08-05 17:33:51 +0400 | [diff] [blame] | 6508 | if (h2g_valid(min)) { |
| 6509 | int flags = page_get_flags(h2g(min)); |
| 6510 | max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX); |
| 6511 | if (page_check_range(h2g(min), max - min, flags) == -1) { |
| 6512 | continue; |
| 6513 | } |
| 6514 | if (h2g(min) == ts->info->stack_limit) { |
| 6515 | pstrcpy(path, sizeof(path), " [stack]"); |
| 6516 | } |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 6517 | dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx |
Christophe Lyon | e24fed4 | 2013-04-02 14:03:38 +0200 | [diff] [blame] | 6518 | " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n", |
Mikhail Ilyin | d67f4aa | 2014-08-05 17:33:51 +0400 | [diff] [blame] | 6519 | h2g(min), h2g(max - 1) + 1, flag_r, flag_w, |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 6520 | flag_x, flag_p, offset, dev_maj, dev_min, inode, |
Christophe Lyon | e24fed4 | 2013-04-02 14:03:38 +0200 | [diff] [blame] | 6521 | path[0] ? " " : "", path); |
Alexander Graf | 1a49ef2 | 2012-05-01 16:30:28 +0100 | [diff] [blame] | 6522 | } |
| 6523 | } |
| 6524 | |
| 6525 | free(line); |
| 6526 | fclose(fp); |
| 6527 | |
Alexander Graf | 36c08d4 | 2011-11-02 20:23:24 +0100 | [diff] [blame] | 6528 | return 0; |
| 6529 | } |
| 6530 | |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 6531 | static int open_self_stat(void *cpu_env, int fd) |
| 6532 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6533 | CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env); |
| 6534 | TaskState *ts = cpu->opaque; |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 6535 | abi_ulong start_stack = ts->info->start_stack; |
| 6536 | int i; |
| 6537 | |
| 6538 | for (i = 0; i < 44; i++) { |
| 6539 | char buf[128]; |
| 6540 | int len; |
| 6541 | uint64_t val = 0; |
| 6542 | |
Fabio Erculiani | e0e65be | 2012-01-03 09:38:34 +0000 | [diff] [blame] | 6543 | if (i == 0) { |
| 6544 | /* pid */ |
| 6545 | val = getpid(); |
| 6546 | snprintf(buf, sizeof(buf), "%"PRId64 " ", val); |
| 6547 | } else if (i == 1) { |
| 6548 | /* app name */ |
| 6549 | snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]); |
| 6550 | } else if (i == 27) { |
| 6551 | /* stack bottom */ |
| 6552 | val = start_stack; |
| 6553 | snprintf(buf, sizeof(buf), "%"PRId64 " ", val); |
| 6554 | } else { |
| 6555 | /* for the rest, there is MasterCard */ |
| 6556 | snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' '); |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 6557 | } |
Fabio Erculiani | e0e65be | 2012-01-03 09:38:34 +0000 | [diff] [blame] | 6558 | |
Alexander Graf | 480b8e7 | 2011-11-02 20:23:25 +0100 | [diff] [blame] | 6559 | len = strlen(buf); |
| 6560 | if (write(fd, buf, len) != len) { |
| 6561 | return -1; |
| 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | return 0; |
| 6566 | } |
| 6567 | |
Alexander Graf | 257450e | 2011-11-02 20:23:26 +0100 | [diff] [blame] | 6568 | static int open_self_auxv(void *cpu_env, int fd) |
| 6569 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6570 | CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env); |
| 6571 | TaskState *ts = cpu->opaque; |
Alexander Graf | 257450e | 2011-11-02 20:23:26 +0100 | [diff] [blame] | 6572 | abi_ulong auxv = ts->info->saved_auxv; |
| 6573 | abi_ulong len = ts->info->auxv_len; |
| 6574 | char *ptr; |
| 6575 | |
| 6576 | /* |
| 6577 | * Auxiliary vector is stored in target process stack. |
| 6578 | * read in whole auxv vector and copy it to file |
| 6579 | */ |
| 6580 | ptr = lock_user(VERIFY_READ, auxv, len, 0); |
| 6581 | if (ptr != NULL) { |
| 6582 | while (len > 0) { |
| 6583 | ssize_t r; |
| 6584 | r = write(fd, ptr, len); |
| 6585 | if (r <= 0) { |
| 6586 | break; |
| 6587 | } |
| 6588 | len -= r; |
| 6589 | ptr += r; |
| 6590 | } |
| 6591 | lseek(fd, 0, SEEK_SET); |
| 6592 | unlock_user(ptr, auxv, len); |
| 6593 | } |
| 6594 | |
| 6595 | return 0; |
| 6596 | } |
| 6597 | |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 6598 | static int is_proc_myself(const char *filename, const char *entry) |
| 6599 | { |
| 6600 | if (!strncmp(filename, "/proc/", strlen("/proc/"))) { |
| 6601 | filename += strlen("/proc/"); |
| 6602 | if (!strncmp(filename, "self/", strlen("self/"))) { |
| 6603 | filename += strlen("self/"); |
| 6604 | } else if (*filename >= '1' && *filename <= '9') { |
| 6605 | char myself[80]; |
| 6606 | snprintf(myself, sizeof(myself), "%d/", getpid()); |
| 6607 | if (!strncmp(filename, myself, strlen(myself))) { |
| 6608 | filename += strlen(myself); |
| 6609 | } else { |
| 6610 | return 0; |
| 6611 | } |
| 6612 | } else { |
| 6613 | return 0; |
| 6614 | } |
| 6615 | if (!strcmp(filename, entry)) { |
| 6616 | return 1; |
| 6617 | } |
| 6618 | } |
| 6619 | return 0; |
| 6620 | } |
| 6621 | |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 6622 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) |
| 6623 | static int is_proc(const char *filename, const char *entry) |
| 6624 | { |
| 6625 | return strcmp(filename, entry) == 0; |
| 6626 | } |
| 6627 | |
| 6628 | static int open_net_route(void *cpu_env, int fd) |
| 6629 | { |
| 6630 | FILE *fp; |
| 6631 | char *line = NULL; |
| 6632 | size_t len = 0; |
| 6633 | ssize_t read; |
| 6634 | |
| 6635 | fp = fopen("/proc/net/route", "r"); |
| 6636 | if (fp == NULL) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6637 | return -1; |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | /* read header */ |
| 6641 | |
| 6642 | read = getline(&line, &len, fp); |
| 6643 | dprintf(fd, "%s", line); |
| 6644 | |
| 6645 | /* read routes */ |
| 6646 | |
| 6647 | while ((read = getline(&line, &len, fp)) != -1) { |
| 6648 | char iface[16]; |
| 6649 | uint32_t dest, gw, mask; |
| 6650 | unsigned int flags, refcnt, use, metric, mtu, window, irtt; |
| 6651 | sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", |
| 6652 | iface, &dest, &gw, &flags, &refcnt, &use, &metric, |
| 6653 | &mask, &mtu, &window, &irtt); |
| 6654 | dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", |
| 6655 | iface, tswap32(dest), tswap32(gw), flags, refcnt, use, |
| 6656 | metric, tswap32(mask), mtu, window, irtt); |
| 6657 | } |
| 6658 | |
| 6659 | free(line); |
| 6660 | fclose(fp); |
| 6661 | |
| 6662 | return 0; |
| 6663 | } |
| 6664 | #endif |
| 6665 | |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 6666 | 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] | 6667 | { |
| 6668 | struct fake_open { |
| 6669 | const char *filename; |
| 6670 | int (*fill)(void *cpu_env, int fd); |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 6671 | int (*cmp)(const char *s1, const char *s2); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6672 | }; |
| 6673 | const struct fake_open *fake_open; |
| 6674 | static const struct fake_open fakes[] = { |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 6675 | { "maps", open_self_maps, is_proc_myself }, |
| 6676 | { "stat", open_self_stat, is_proc_myself }, |
| 6677 | { "auxv", open_self_auxv, is_proc_myself }, |
Wim Vander Schelden | 76b9424 | 2014-06-18 11:02:39 +0200 | [diff] [blame] | 6678 | { "cmdline", open_self_cmdline, is_proc_myself }, |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 6679 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) |
| 6680 | { "/proc/net/route", open_net_route, is_proc }, |
| 6681 | #endif |
| 6682 | { NULL, NULL, NULL } |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6683 | }; |
| 6684 | |
Maxim Ostapenko | aa07f5e | 2014-05-02 11:17:07 +0300 | [diff] [blame] | 6685 | if (is_proc_myself(pathname, "exe")) { |
| 6686 | int execfd = qemu_getauxval(AT_EXECFD); |
Timothy E Baldwin | c10a073 | 2016-05-12 18:47:48 +0100 | [diff] [blame] | 6687 | return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode); |
Maxim Ostapenko | aa07f5e | 2014-05-02 11:17:07 +0300 | [diff] [blame] | 6688 | } |
| 6689 | |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6690 | for (fake_open = fakes; fake_open->filename; fake_open++) { |
Laurent Vivier | de6b993 | 2013-08-30 01:46:40 +0200 | [diff] [blame] | 6691 | if (fake_open->cmp(pathname, fake_open->filename)) { |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6692 | break; |
| 6693 | } |
| 6694 | } |
| 6695 | |
| 6696 | if (fake_open->filename) { |
| 6697 | const char *tmpdir; |
| 6698 | char filename[PATH_MAX]; |
| 6699 | int fd, r; |
| 6700 | |
| 6701 | /* create temporary file to map stat to */ |
| 6702 | tmpdir = getenv("TMPDIR"); |
| 6703 | if (!tmpdir) |
| 6704 | tmpdir = "/tmp"; |
| 6705 | snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); |
| 6706 | fd = mkstemp(filename); |
| 6707 | if (fd < 0) { |
| 6708 | return fd; |
| 6709 | } |
| 6710 | unlink(filename); |
| 6711 | |
| 6712 | if ((r = fake_open->fill(cpu_env, fd))) { |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6713 | int e = errno; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6714 | close(fd); |
Peter Maydell | a3ca7bb | 2016-05-12 18:47:26 +0100 | [diff] [blame] | 6715 | errno = e; |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6716 | return r; |
| 6717 | } |
| 6718 | lseek(fd, 0, SEEK_SET); |
| 6719 | |
| 6720 | return fd; |
| 6721 | } |
| 6722 | |
Timothy E Baldwin | c10a073 | 2016-05-12 18:47:48 +0100 | [diff] [blame] | 6723 | return safe_openat(dirfd, path(pathname), flags, mode); |
Alexander Graf | 3be14d0 | 2011-11-02 20:23:23 +0100 | [diff] [blame] | 6724 | } |
| 6725 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 6726 | #define TIMER_MAGIC 0x0caf0000 |
| 6727 | #define TIMER_MAGIC_MASK 0xffff0000 |
| 6728 | |
| 6729 | /* Convert QEMU provided timer ID back to internal 16bit index format */ |
| 6730 | static target_timer_t get_timer_id(abi_long arg) |
| 6731 | { |
| 6732 | target_timer_t timerid = arg; |
| 6733 | |
| 6734 | if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) { |
| 6735 | return -TARGET_EINVAL; |
| 6736 | } |
| 6737 | |
| 6738 | timerid &= 0xffff; |
| 6739 | |
| 6740 | if (timerid >= ARRAY_SIZE(g_posix_timers)) { |
| 6741 | return -TARGET_EINVAL; |
| 6742 | } |
| 6743 | |
| 6744 | return timerid; |
| 6745 | } |
| 6746 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 6747 | /* do_syscall() should always have a single exit point at the end so |
| 6748 | that actions, such as logging of syscall results, can be performed. |
| 6749 | All errnos that do_syscall() returns must be -TARGET_<errcode>. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6750 | abi_long do_syscall(void *cpu_env, int num, abi_long arg1, |
| 6751 | abi_long arg2, abi_long arg3, abi_long arg4, |
Peter Maydell | 5945cfc | 2011-06-16 17:37:13 +0100 | [diff] [blame] | 6752 | abi_long arg5, abi_long arg6, abi_long arg7, |
| 6753 | abi_long arg8) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6754 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 6755 | CPUState *cpu = ENV_GET_CPU(cpu_env); |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6756 | abi_long ret; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6757 | struct stat st; |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 6758 | struct statfs stfs; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6759 | void *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6760 | |
Timothy E Baldwin | 71a8f7f | 2016-05-12 18:47:45 +0100 | [diff] [blame] | 6761 | #if defined(DEBUG_ERESTARTSYS) |
| 6762 | /* Debug-only code for exercising the syscall-restart code paths |
| 6763 | * in the per-architecture cpu main loops: restart every syscall |
| 6764 | * the guest makes once before letting it through. |
| 6765 | */ |
| 6766 | { |
| 6767 | static int flag; |
| 6768 | |
| 6769 | flag = !flag; |
| 6770 | if (flag) { |
| 6771 | return -TARGET_ERESTARTSYS; |
| 6772 | } |
| 6773 | } |
| 6774 | #endif |
| 6775 | |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 6776 | #ifdef DEBUG |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 6777 | gemu_log("syscall %d", num); |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 6778 | #endif |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 6779 | if(do_strace) |
| 6780 | print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); |
| 6781 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6782 | switch(num) { |
| 6783 | case TARGET_NR_exit: |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6784 | /* In old applications this may be used to implement _exit(2). |
| 6785 | However in threaded applictions it is used for thread termination, |
| 6786 | and _exit_group is used for application termination. |
| 6787 | Do thread termination if we have more then one thread. */ |
Timothy E Baldwin | a099588 | 2016-05-27 15:51:56 +0100 | [diff] [blame] | 6788 | |
| 6789 | if (block_signals()) { |
| 6790 | ret = -TARGET_ERESTARTSYS; |
| 6791 | break; |
| 6792 | } |
| 6793 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 6794 | if (CPU_NEXT(first_cpu)) { |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6795 | TaskState *ts; |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6796 | |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6797 | cpu_list_lock(); |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6798 | /* Remove the CPU from the list. */ |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 6799 | QTAILQ_REMOVE(&cpus, cpu, node); |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6800 | cpu_list_unlock(); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6801 | ts = cpu->opaque; |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6802 | if (ts->child_tidptr) { |
| 6803 | put_user_u32(0, ts->child_tidptr); |
| 6804 | sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX, |
| 6805 | NULL, NULL, 0); |
| 6806 | } |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 6807 | thread_cpu = NULL; |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 6808 | object_unref(OBJECT(cpu)); |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6809 | g_free(ts); |
Emilio G. Cota | 7090376 | 2015-08-23 20:23:41 -0400 | [diff] [blame] | 6810 | rcu_unregister_thread(); |
Andreas Färber | 9b056fc | 2013-06-24 23:53:10 +0200 | [diff] [blame] | 6811 | pthread_exit(NULL); |
| 6812 | } |
Juan Quintela | 9788c9c | 2009-07-27 16:13:02 +0200 | [diff] [blame] | 6813 | #ifdef TARGET_GPROF |
bellard | 7d13299 | 2003-03-06 23:23:54 +0000 | [diff] [blame] | 6814 | _mcleanup(); |
| 6815 | #endif |
bellard | e900967 | 2005-04-26 20:42:36 +0000 | [diff] [blame] | 6816 | gdb_exit(cpu_env, arg1); |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 6817 | _exit(arg1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6818 | ret = 0; /* avoid warning */ |
| 6819 | break; |
| 6820 | case TARGET_NR_read: |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 6821 | if (arg3 == 0) |
| 6822 | ret = 0; |
| 6823 | else { |
| 6824 | if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) |
| 6825 | goto efault; |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 6826 | ret = get_errno(safe_read(arg1, p, arg3)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6827 | if (ret >= 0 && |
Laurent Vivier | 5d4d366 | 2015-10-28 21:40:43 +0100 | [diff] [blame] | 6828 | fd_trans_host_to_target_data(arg1)) { |
| 6829 | ret = fd_trans_host_to_target_data(arg1)(p, ret); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6830 | } |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 6831 | unlock_user(p, arg2, ret); |
| 6832 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6833 | break; |
| 6834 | case TARGET_NR_write: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6835 | if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) |
| 6836 | goto efault; |
Timothy E Baldwin | 50afd02 | 2016-05-12 18:47:47 +0100 | [diff] [blame] | 6837 | ret = get_errno(safe_write(arg1, p, arg3)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6838 | unlock_user(p, arg2, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6839 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6840 | #ifdef TARGET_NR_open |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6841 | case TARGET_NR_open: |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6842 | if (!(p = lock_user_string(arg1))) |
| 6843 | goto efault; |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 6844 | ret = get_errno(do_openat(cpu_env, AT_FDCWD, p, |
| 6845 | target_to_host_bitmask(arg2, fcntl_flags_tbl), |
| 6846 | arg3)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6847 | fd_trans_unregister(ret); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6848 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6849 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6850 | #endif |
ths | 8242483 | 2007-09-24 09:21:55 +0000 | [diff] [blame] | 6851 | case TARGET_NR_openat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6852 | if (!(p = lock_user_string(arg2))) |
| 6853 | goto efault; |
Riku Voipio | 0b2effd | 2014-08-06 10:36:37 +0300 | [diff] [blame] | 6854 | ret = get_errno(do_openat(cpu_env, arg1, p, |
| 6855 | target_to_host_bitmask(arg3, fcntl_flags_tbl), |
| 6856 | arg4)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6857 | fd_trans_unregister(ret); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6858 | unlock_user(p, arg2, 0); |
ths | 8242483 | 2007-09-24 09:21:55 +0000 | [diff] [blame] | 6859 | break; |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 6860 | #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 6861 | case TARGET_NR_name_to_handle_at: |
| 6862 | ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5); |
| 6863 | break; |
| 6864 | #endif |
| 6865 | #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) |
| 6866 | case TARGET_NR_open_by_handle_at: |
| 6867 | ret = do_open_by_handle_at(arg1, arg2, arg3); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6868 | fd_trans_unregister(ret); |
Laurent Vivier | 0f0426f | 2015-09-01 22:27:33 +0200 | [diff] [blame] | 6869 | break; |
| 6870 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6871 | case TARGET_NR_close: |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6872 | fd_trans_unregister(arg1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6873 | ret = get_errno(close(arg1)); |
| 6874 | break; |
| 6875 | case TARGET_NR_brk: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6876 | ret = do_brk(arg1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6877 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6878 | #ifdef TARGET_NR_fork |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6879 | case TARGET_NR_fork: |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 6880 | ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6881 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6882 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 6883 | #ifdef TARGET_NR_waitpid |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6884 | case TARGET_NR_waitpid: |
| 6885 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6886 | int status; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 6887 | ret = get_errno(safe_wait4(arg1, &status, arg3, 0)); |
Alexander Graf | 5379557 | 2011-11-24 00:44:43 +0100 | [diff] [blame] | 6888 | if (!is_error(ret) && arg2 && ret |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 6889 | && put_user_s32(host_to_target_waitstatus(status), arg2)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6890 | goto efault; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6891 | } |
| 6892 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 6893 | #endif |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 6894 | #ifdef TARGET_NR_waitid |
| 6895 | case TARGET_NR_waitid: |
| 6896 | { |
| 6897 | siginfo_t info; |
| 6898 | info.si_pid = 0; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 6899 | ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL)); |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 6900 | if (!is_error(ret) && arg3 && info.si_pid != 0) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 6901 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0))) |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 6902 | goto efault; |
| 6903 | host_to_target_siginfo(p, &info); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 6904 | unlock_user(p, arg3, sizeof(target_siginfo_t)); |
pbrook | f0cbb61 | 2008-05-30 18:20:05 +0000 | [diff] [blame] | 6905 | } |
| 6906 | } |
| 6907 | break; |
| 6908 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 6909 | #ifdef TARGET_NR_creat /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6910 | case TARGET_NR_creat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6911 | if (!(p = lock_user_string(arg1))) |
| 6912 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6913 | ret = get_errno(creat(p, arg2)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 6914 | fd_trans_unregister(ret); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6915 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6916 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 6917 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6918 | #ifdef TARGET_NR_link |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6919 | case TARGET_NR_link: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6920 | { |
| 6921 | void * p2; |
| 6922 | p = lock_user_string(arg1); |
| 6923 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6924 | if (!p || !p2) |
| 6925 | ret = -TARGET_EFAULT; |
| 6926 | else |
| 6927 | ret = get_errno(link(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6928 | unlock_user(p2, arg2, 0); |
| 6929 | unlock_user(p, arg1, 0); |
| 6930 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6931 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6932 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 6933 | #if defined(TARGET_NR_linkat) |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 6934 | case TARGET_NR_linkat: |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 6935 | { |
| 6936 | void * p2 = NULL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6937 | if (!arg2 || !arg4) |
| 6938 | goto efault; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 6939 | p = lock_user_string(arg2); |
| 6940 | p2 = lock_user_string(arg4); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6941 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 6942 | ret = -TARGET_EFAULT; |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 6943 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 6944 | ret = get_errno(linkat(arg1, p, arg3, p2, arg5)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6945 | unlock_user(p, arg2, 0); |
| 6946 | unlock_user(p2, arg4, 0); |
ths | 64f0ce4 | 2007-09-24 09:25:06 +0000 | [diff] [blame] | 6947 | } |
| 6948 | break; |
| 6949 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6950 | #ifdef TARGET_NR_unlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6951 | case TARGET_NR_unlink: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6952 | if (!(p = lock_user_string(arg1))) |
| 6953 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6954 | ret = get_errno(unlink(p)); |
| 6955 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6956 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 6957 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 6958 | #if defined(TARGET_NR_unlinkat) |
ths | 8170f56 | 2007-09-24 09:24:11 +0000 | [diff] [blame] | 6959 | case TARGET_NR_unlinkat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6960 | if (!(p = lock_user_string(arg2))) |
| 6961 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 6962 | ret = get_errno(unlinkat(arg1, p, arg3)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 6963 | unlock_user(p, arg2, 0); |
balrog | ed494d8 | 2007-12-11 23:23:52 +0000 | [diff] [blame] | 6964 | break; |
balrog | b7d35e6 | 2007-12-12 00:40:24 +0000 | [diff] [blame] | 6965 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 6966 | case TARGET_NR_execve: |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6967 | { |
| 6968 | char **argp, **envp; |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 6969 | int argc, envc; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 6970 | abi_ulong gp; |
| 6971 | abi_ulong guest_argp; |
| 6972 | abi_ulong guest_envp; |
| 6973 | abi_ulong addr; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6974 | char **q; |
Ulrich Hecht | a6f79cc | 2012-01-31 12:43:16 +0100 | [diff] [blame] | 6975 | int total_size = 0; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6976 | |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 6977 | argc = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6978 | guest_argp = arg2; |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 6979 | for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) { |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 6980 | if (get_user_ual(addr, gp)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6981 | goto efault; |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 6982 | if (!addr) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6983 | break; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6984 | argc++; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6985 | } |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 6986 | envc = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 6987 | guest_envp = arg3; |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 6988 | for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) { |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 6989 | if (get_user_ual(addr, gp)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6990 | goto efault; |
ths | 03aa197 | 2007-12-02 06:28:08 +0000 | [diff] [blame] | 6991 | if (!addr) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6992 | break; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6993 | envc++; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 6994 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6995 | |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 6996 | argp = alloca((argc + 1) * sizeof(void *)); |
| 6997 | envp = alloca((envc + 1) * sizeof(void *)); |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 6998 | |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 6999 | for (gp = guest_argp, q = argp; gp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7000 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7001 | if (get_user_ual(addr, gp)) |
| 7002 | goto execve_efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7003 | if (!addr) |
| 7004 | break; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7005 | if (!(*q = lock_user_string(addr))) |
| 7006 | goto execve_efault; |
Ulrich Hecht | a6f79cc | 2012-01-31 12:43:16 +0100 | [diff] [blame] | 7007 | total_size += strlen(*q) + 1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7008 | } |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 7009 | *q = NULL; |
| 7010 | |
pbrook | da94d26 | 2008-05-30 18:24:00 +0000 | [diff] [blame] | 7011 | for (gp = guest_envp, q = envp; gp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7012 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7013 | if (get_user_ual(addr, gp)) |
| 7014 | goto execve_efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7015 | if (!addr) |
| 7016 | break; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7017 | if (!(*q = lock_user_string(addr))) |
| 7018 | goto execve_efault; |
Ulrich Hecht | a6f79cc | 2012-01-31 12:43:16 +0100 | [diff] [blame] | 7019 | total_size += strlen(*q) + 1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7020 | } |
bellard | f7341ff | 2003-03-30 21:00:25 +0000 | [diff] [blame] | 7021 | *q = NULL; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 7022 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7023 | if (!(p = lock_user_string(arg1))) |
| 7024 | goto execve_efault; |
Timothy E Baldwin | ffdcbe2 | 2016-05-12 18:47:50 +0100 | [diff] [blame] | 7025 | /* Although execve() is not an interruptible syscall it is |
| 7026 | * a special case where we must use the safe_syscall wrapper: |
| 7027 | * if we allow a signal to happen before we make the host |
| 7028 | * syscall then we will 'lose' it, because at the point of |
| 7029 | * execve the process leaves QEMU's control. So we use the |
| 7030 | * safe syscall wrapper to ensure that we either take the |
| 7031 | * signal as a guest signal, or else it does not happen |
| 7032 | * before the execve completes and makes it the other |
| 7033 | * program's problem. |
| 7034 | */ |
| 7035 | ret = get_errno(safe_execve(p, argp, envp)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7036 | unlock_user(p, arg1, 0); |
| 7037 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7038 | goto execve_end; |
| 7039 | |
| 7040 | execve_efault: |
| 7041 | ret = -TARGET_EFAULT; |
| 7042 | |
| 7043 | execve_end: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7044 | for (gp = guest_argp, q = argp; *q; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7045 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7046 | if (get_user_ual(addr, gp) |
| 7047 | || !addr) |
| 7048 | break; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7049 | unlock_user(*q, addr, 0); |
| 7050 | } |
| 7051 | for (gp = guest_envp, q = envp; *q; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7052 | gp += sizeof(abi_ulong), q++) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7053 | if (get_user_ual(addr, gp) |
| 7054 | || !addr) |
| 7055 | break; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7056 | unlock_user(*q, addr, 0); |
| 7057 | } |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 7058 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7059 | break; |
| 7060 | case TARGET_NR_chdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7061 | if (!(p = lock_user_string(arg1))) |
| 7062 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7063 | ret = get_errno(chdir(p)); |
| 7064 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7065 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 7066 | #ifdef TARGET_NR_time |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7067 | case TARGET_NR_time: |
| 7068 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7069 | time_t host_time; |
| 7070 | ret = get_errno(time(&host_time)); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7071 | if (!is_error(ret) |
| 7072 | && arg1 |
| 7073 | && put_user_sal(host_time, arg1)) |
| 7074 | goto efault; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7075 | } |
| 7076 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 7077 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7078 | #ifdef TARGET_NR_mknod |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7079 | case TARGET_NR_mknod: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7080 | if (!(p = lock_user_string(arg1))) |
| 7081 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7082 | ret = get_errno(mknod(p, arg2, arg3)); |
| 7083 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7084 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7085 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7086 | #if defined(TARGET_NR_mknodat) |
ths | 75ac37a | 2007-09-24 09:23:05 +0000 | [diff] [blame] | 7087 | case TARGET_NR_mknodat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7088 | if (!(p = lock_user_string(arg2))) |
| 7089 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7090 | ret = get_errno(mknodat(arg1, p, arg3, arg4)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7091 | unlock_user(p, arg2, 0); |
ths | 75ac37a | 2007-09-24 09:23:05 +0000 | [diff] [blame] | 7092 | break; |
| 7093 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7094 | #ifdef TARGET_NR_chmod |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7095 | case TARGET_NR_chmod: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7096 | if (!(p = lock_user_string(arg1))) |
| 7097 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7098 | ret = get_errno(chmod(p, arg2)); |
| 7099 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7100 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7101 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7102 | #ifdef TARGET_NR_break |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7103 | case TARGET_NR_break: |
| 7104 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7105 | #endif |
| 7106 | #ifdef TARGET_NR_oldstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7107 | case TARGET_NR_oldstat: |
| 7108 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7109 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7110 | case TARGET_NR_lseek: |
| 7111 | ret = get_errno(lseek(arg1, arg2, arg3)); |
| 7112 | break; |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 7113 | #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA) |
| 7114 | /* Alpha specific */ |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7115 | case TARGET_NR_getxpid: |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 7116 | ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid(); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7117 | ret = get_errno(getpid()); |
| 7118 | break; |
Richard Henderson | 9231733 | 2010-05-03 10:07:53 -0700 | [diff] [blame] | 7119 | #endif |
| 7120 | #ifdef TARGET_NR_getpid |
| 7121 | case TARGET_NR_getpid: |
| 7122 | ret = get_errno(getpid()); |
| 7123 | break; |
| 7124 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7125 | case TARGET_NR_mount: |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 7126 | { |
| 7127 | /* need to look at the data field */ |
| 7128 | void *p2, *p3; |
| 7129 | |
| 7130 | if (arg1) { |
| 7131 | p = lock_user_string(arg1); |
| 7132 | if (!p) { |
| 7133 | goto efault; |
| 7134 | } |
| 7135 | } else { |
| 7136 | p = NULL; |
| 7137 | } |
| 7138 | |
| 7139 | p2 = lock_user_string(arg2); |
| 7140 | if (!p2) { |
| 7141 | if (arg1) { |
| 7142 | unlock_user(p, arg1, 0); |
| 7143 | } |
| 7144 | goto efault; |
| 7145 | } |
| 7146 | |
| 7147 | if (arg3) { |
| 7148 | p3 = lock_user_string(arg3); |
| 7149 | if (!p3) { |
| 7150 | if (arg1) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7151 | unlock_user(p, arg1, 0); |
Paul Burton | 356d771 | 2014-06-22 11:25:37 +0100 | [diff] [blame] | 7152 | } |
| 7153 | unlock_user(p2, arg2, 0); |
| 7154 | goto efault; |
| 7155 | } |
| 7156 | } else { |
| 7157 | p3 = NULL; |
| 7158 | } |
| 7159 | |
| 7160 | /* FIXME - arg5 should be locked, but it isn't clear how to |
| 7161 | * do that since it's not guaranteed to be a NULL-terminated |
| 7162 | * string. |
| 7163 | */ |
| 7164 | if (!arg5) { |
| 7165 | ret = mount(p, p2, p3, (unsigned long)arg4, NULL); |
| 7166 | } else { |
| 7167 | ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)); |
| 7168 | } |
| 7169 | ret = get_errno(ret); |
| 7170 | |
| 7171 | if (arg1) { |
| 7172 | unlock_user(p, arg1, 0); |
| 7173 | } |
| 7174 | unlock_user(p2, arg2, 0); |
| 7175 | if (arg3) { |
| 7176 | unlock_user(p3, arg3, 0); |
| 7177 | } |
| 7178 | } |
| 7179 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7180 | #ifdef TARGET_NR_umount |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7181 | case TARGET_NR_umount: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7182 | if (!(p = lock_user_string(arg1))) |
| 7183 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7184 | ret = get_errno(umount(p)); |
| 7185 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7186 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7187 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7188 | #ifdef TARGET_NR_stime /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7189 | case TARGET_NR_stime: |
| 7190 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7191 | time_t host_time; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 7192 | if (get_user_sal(host_time, arg1)) |
| 7193 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7194 | ret = get_errno(stime(&host_time)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7195 | } |
| 7196 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7197 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7198 | case TARGET_NR_ptrace: |
| 7199 | goto unimplemented; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7200 | #ifdef TARGET_NR_alarm /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7201 | case TARGET_NR_alarm: |
| 7202 | ret = alarm(arg1); |
| 7203 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7204 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7205 | #ifdef TARGET_NR_oldfstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7206 | case TARGET_NR_oldfstat: |
| 7207 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7208 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7209 | #ifdef TARGET_NR_pause /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7210 | case TARGET_NR_pause: |
Timothy E Baldwin | f59ec60 | 2016-05-27 15:51:55 +0100 | [diff] [blame] | 7211 | if (!block_signals()) { |
| 7212 | sigsuspend(&((TaskState *)cpu->opaque)->signal_mask); |
| 7213 | } |
| 7214 | ret = -TARGET_EINTR; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7215 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7216 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7217 | #ifdef TARGET_NR_utime |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7218 | case TARGET_NR_utime: |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7219 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7220 | struct utimbuf tbuf, *host_tbuf; |
| 7221 | struct target_utimbuf *target_tbuf; |
| 7222 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7223 | if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1)) |
| 7224 | goto efault; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 7225 | tbuf.actime = tswapal(target_tbuf->actime); |
| 7226 | tbuf.modtime = tswapal(target_tbuf->modtime); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7227 | unlock_user_struct(target_tbuf, arg2, 0); |
| 7228 | host_tbuf = &tbuf; |
bellard | f72e8ff | 2004-05-03 19:23:07 +0000 | [diff] [blame] | 7229 | } else { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7230 | host_tbuf = NULL; |
bellard | f72e8ff | 2004-05-03 19:23:07 +0000 | [diff] [blame] | 7231 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7232 | if (!(p = lock_user_string(arg1))) |
| 7233 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7234 | ret = get_errno(utime(p, host_tbuf)); |
| 7235 | unlock_user(p, arg1, 0); |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7236 | } |
| 7237 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7238 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7239 | #ifdef TARGET_NR_utimes |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 7240 | case TARGET_NR_utimes: |
| 7241 | { |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 7242 | struct timeval *tvp, tv[2]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7243 | if (arg2) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 7244 | if (copy_from_user_timeval(&tv[0], arg2) |
| 7245 | || copy_from_user_timeval(&tv[1], |
| 7246 | arg2 + sizeof(struct target_timeval))) |
| 7247 | goto efault; |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 7248 | tvp = tv; |
| 7249 | } else { |
| 7250 | tvp = NULL; |
| 7251 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7252 | if (!(p = lock_user_string(arg1))) |
| 7253 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7254 | ret = get_errno(utimes(p, tvp)); |
| 7255 | unlock_user(p, arg1, 0); |
bellard | 978a66f | 2004-12-06 22:58:05 +0000 | [diff] [blame] | 7256 | } |
| 7257 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7258 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7259 | #if defined(TARGET_NR_futimesat) |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 7260 | case TARGET_NR_futimesat: |
| 7261 | { |
| 7262 | struct timeval *tvp, tv[2]; |
| 7263 | if (arg3) { |
| 7264 | if (copy_from_user_timeval(&tv[0], arg3) |
| 7265 | || copy_from_user_timeval(&tv[1], |
| 7266 | arg3 + sizeof(struct target_timeval))) |
| 7267 | goto efault; |
| 7268 | tvp = tv; |
| 7269 | } else { |
| 7270 | tvp = NULL; |
| 7271 | } |
| 7272 | if (!(p = lock_user_string(arg2))) |
| 7273 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7274 | ret = get_errno(futimesat(arg1, path(p), tvp)); |
balrog | ac8a655 | 2008-09-20 02:25:39 +0000 | [diff] [blame] | 7275 | unlock_user(p, arg2, 0); |
| 7276 | } |
| 7277 | break; |
| 7278 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7279 | #ifdef TARGET_NR_stty |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7280 | case TARGET_NR_stty: |
| 7281 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7282 | #endif |
| 7283 | #ifdef TARGET_NR_gtty |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7284 | case TARGET_NR_gtty: |
| 7285 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7286 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7287 | #ifdef TARGET_NR_access |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7288 | case TARGET_NR_access: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7289 | if (!(p = lock_user_string(arg1))) |
| 7290 | goto efault; |
Ulrich Hecht | 719f908 | 2009-07-03 17:09:29 +0200 | [diff] [blame] | 7291 | ret = get_errno(access(path(p), arg2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7292 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7293 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7294 | #endif |
ths | 92a34c1 | 2007-09-24 09:27:49 +0000 | [diff] [blame] | 7295 | #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) |
| 7296 | case TARGET_NR_faccessat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7297 | if (!(p = lock_user_string(arg2))) |
| 7298 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7299 | ret = get_errno(faccessat(arg1, p, arg3, 0)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7300 | unlock_user(p, arg2, 0); |
ths | 92a34c1 | 2007-09-24 09:27:49 +0000 | [diff] [blame] | 7301 | break; |
| 7302 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7303 | #ifdef TARGET_NR_nice /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7304 | case TARGET_NR_nice: |
| 7305 | ret = get_errno(nice(arg1)); |
| 7306 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7307 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7308 | #ifdef TARGET_NR_ftime |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7309 | case TARGET_NR_ftime: |
| 7310 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7311 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7312 | case TARGET_NR_sync: |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 7313 | sync(); |
| 7314 | ret = 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7315 | break; |
| 7316 | case TARGET_NR_kill: |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 7317 | ret = get_errno(safe_kill(arg1, target_to_host_signal(arg2))); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7318 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7319 | #ifdef TARGET_NR_rename |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7320 | case TARGET_NR_rename: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7321 | { |
| 7322 | void *p2; |
| 7323 | p = lock_user_string(arg1); |
| 7324 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7325 | if (!p || !p2) |
| 7326 | ret = -TARGET_EFAULT; |
| 7327 | else |
| 7328 | ret = get_errno(rename(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7329 | unlock_user(p2, arg2, 0); |
| 7330 | unlock_user(p, arg1, 0); |
| 7331 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7332 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7333 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7334 | #if defined(TARGET_NR_renameat) |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 7335 | case TARGET_NR_renameat: |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 7336 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7337 | void *p2; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 7338 | p = lock_user_string(arg2); |
| 7339 | p2 = lock_user_string(arg4); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7340 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 7341 | ret = -TARGET_EFAULT; |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 7342 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7343 | ret = get_errno(renameat(arg1, p, arg3, p2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7344 | unlock_user(p2, arg4, 0); |
| 7345 | unlock_user(p, arg2, 0); |
ths | 722183f | 2007-09-24 09:24:37 +0000 | [diff] [blame] | 7346 | } |
| 7347 | break; |
| 7348 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7349 | #ifdef TARGET_NR_mkdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7350 | case TARGET_NR_mkdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7351 | if (!(p = lock_user_string(arg1))) |
| 7352 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7353 | ret = get_errno(mkdir(p, arg2)); |
| 7354 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7355 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7356 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7357 | #if defined(TARGET_NR_mkdirat) |
ths | 4472ad0 | 2007-09-24 09:22:32 +0000 | [diff] [blame] | 7358 | case TARGET_NR_mkdirat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7359 | if (!(p = lock_user_string(arg2))) |
| 7360 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 7361 | ret = get_errno(mkdirat(arg1, p, arg3)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7362 | unlock_user(p, arg2, 0); |
ths | 4472ad0 | 2007-09-24 09:22:32 +0000 | [diff] [blame] | 7363 | break; |
| 7364 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7365 | #ifdef TARGET_NR_rmdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7366 | case TARGET_NR_rmdir: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7367 | if (!(p = lock_user_string(arg1))) |
| 7368 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7369 | ret = get_errno(rmdir(p)); |
| 7370 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7371 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7372 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7373 | case TARGET_NR_dup: |
| 7374 | ret = get_errno(dup(arg1)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7375 | if (ret >= 0) { |
| 7376 | fd_trans_dup(arg1, ret); |
| 7377 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7378 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7379 | #ifdef TARGET_NR_pipe |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7380 | case TARGET_NR_pipe: |
Richard Henderson | fb41a66 | 2010-05-03 10:07:52 -0700 | [diff] [blame] | 7381 | ret = do_pipe(cpu_env, arg1, 0, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7382 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7383 | #endif |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 7384 | #ifdef TARGET_NR_pipe2 |
| 7385 | case TARGET_NR_pipe2: |
Richard Henderson | e7ea6cb | 2012-06-01 18:48:39 -0700 | [diff] [blame] | 7386 | ret = do_pipe(cpu_env, arg1, |
| 7387 | target_to_host_bitmask(arg2, fcntl_flags_tbl), 1); |
Riku Voipio | 099d6b0 | 2009-05-05 12:10:04 +0300 | [diff] [blame] | 7388 | break; |
| 7389 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7390 | case TARGET_NR_times: |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 7391 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7392 | struct target_tms *tmsp; |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 7393 | struct tms tms; |
| 7394 | ret = get_errno(times(&tms)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7395 | if (arg1) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7396 | tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0); |
| 7397 | if (!tmsp) |
| 7398 | goto efault; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 7399 | tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime)); |
| 7400 | tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime)); |
| 7401 | tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime)); |
| 7402 | tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime)); |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 7403 | } |
bellard | c596ed1 | 2003-07-13 17:32:31 +0000 | [diff] [blame] | 7404 | if (!is_error(ret)) |
| 7405 | ret = host_to_target_clock_t(ret); |
bellard | 32f36bc | 2003-03-30 21:29:48 +0000 | [diff] [blame] | 7406 | } |
| 7407 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7408 | #ifdef TARGET_NR_prof |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7409 | case TARGET_NR_prof: |
| 7410 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7411 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7412 | #ifdef TARGET_NR_signal |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7413 | case TARGET_NR_signal: |
| 7414 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7415 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7416 | case TARGET_NR_acct: |
aurel32 | 38d840e | 2009-01-30 19:48:17 +0000 | [diff] [blame] | 7417 | if (arg1 == 0) { |
| 7418 | ret = get_errno(acct(NULL)); |
| 7419 | } else { |
| 7420 | if (!(p = lock_user_string(arg1))) |
| 7421 | goto efault; |
| 7422 | ret = get_errno(acct(path(p))); |
| 7423 | unlock_user(p, arg1, 0); |
| 7424 | } |
pbrook | 2483668 | 2006-04-16 14:14:53 +0000 | [diff] [blame] | 7425 | break; |
Richard Henderson | 8070e7b | 2013-07-24 09:50:00 -1000 | [diff] [blame] | 7426 | #ifdef TARGET_NR_umount2 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7427 | case TARGET_NR_umount2: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7428 | if (!(p = lock_user_string(arg1))) |
| 7429 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7430 | ret = get_errno(umount2(p, arg2)); |
| 7431 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7432 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7433 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7434 | #ifdef TARGET_NR_lock |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7435 | case TARGET_NR_lock: |
| 7436 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7437 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7438 | case TARGET_NR_ioctl: |
| 7439 | ret = do_ioctl(arg1, arg2, arg3); |
| 7440 | break; |
| 7441 | case TARGET_NR_fcntl: |
bellard | 9ee1fa2 | 2007-11-11 15:11:19 +0000 | [diff] [blame] | 7442 | ret = do_fcntl(arg1, arg2, arg3); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7443 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7444 | #ifdef TARGET_NR_mpx |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7445 | case TARGET_NR_mpx: |
| 7446 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7447 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7448 | case TARGET_NR_setpgid: |
| 7449 | ret = get_errno(setpgid(arg1, arg2)); |
| 7450 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7451 | #ifdef TARGET_NR_ulimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7452 | case TARGET_NR_ulimit: |
| 7453 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7454 | #endif |
| 7455 | #ifdef TARGET_NR_oldolduname |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7456 | case TARGET_NR_oldolduname: |
| 7457 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 7458 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7459 | case TARGET_NR_umask: |
| 7460 | ret = get_errno(umask(arg1)); |
| 7461 | break; |
| 7462 | case TARGET_NR_chroot: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7463 | if (!(p = lock_user_string(arg1))) |
| 7464 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7465 | ret = get_errno(chroot(p)); |
| 7466 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7467 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7468 | #ifdef TARGET_NR_ustat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7469 | case TARGET_NR_ustat: |
| 7470 | goto unimplemented; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7471 | #endif |
| 7472 | #ifdef TARGET_NR_dup2 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7473 | case TARGET_NR_dup2: |
| 7474 | ret = get_errno(dup2(arg1, arg2)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7475 | if (ret >= 0) { |
| 7476 | fd_trans_dup(arg1, arg2); |
| 7477 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7478 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7479 | #endif |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 7480 | #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3) |
| 7481 | case TARGET_NR_dup3: |
| 7482 | ret = get_errno(dup3(arg1, arg2, arg3)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 7483 | if (ret >= 0) { |
| 7484 | fd_trans_dup(arg1, arg2); |
| 7485 | } |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 7486 | break; |
| 7487 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7488 | #ifdef TARGET_NR_getppid /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7489 | case TARGET_NR_getppid: |
| 7490 | ret = get_errno(getppid()); |
| 7491 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7492 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7493 | #ifdef TARGET_NR_getpgrp |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7494 | case TARGET_NR_getpgrp: |
| 7495 | ret = get_errno(getpgrp()); |
| 7496 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 7497 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7498 | case TARGET_NR_setsid: |
| 7499 | ret = get_errno(setsid()); |
| 7500 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7501 | #ifdef TARGET_NR_sigaction |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7502 | case TARGET_NR_sigaction: |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7503 | { |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 7504 | #if defined(TARGET_ALPHA) |
| 7505 | struct target_sigaction act, oact, *pact = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7506 | struct target_old_sigaction *old_act; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7507 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7508 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
| 7509 | goto efault; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7510 | act._sa_handler = old_act->_sa_handler; |
| 7511 | target_siginitset(&act.sa_mask, old_act->sa_mask); |
| 7512 | act.sa_flags = old_act->sa_flags; |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 7513 | act.sa_restorer = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7514 | unlock_user_struct(old_act, arg2, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7515 | pact = &act; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7516 | } |
| 7517 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7518 | if (!is_error(ret) && arg3) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7519 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
| 7520 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7521 | old_act->_sa_handler = oact._sa_handler; |
| 7522 | old_act->sa_mask = oact.sa_mask.sig[0]; |
| 7523 | old_act->sa_flags = oact.sa_flags; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7524 | unlock_user_struct(old_act, arg3, 1); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7525 | } |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 7526 | #elif defined(TARGET_MIPS) |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 7527 | struct target_sigaction act, oact, *pact, *old_act; |
| 7528 | |
| 7529 | if (arg2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7530 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
| 7531 | goto efault; |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 7532 | act._sa_handler = old_act->_sa_handler; |
| 7533 | target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]); |
| 7534 | act.sa_flags = old_act->sa_flags; |
| 7535 | unlock_user_struct(old_act, arg2, 0); |
| 7536 | pact = &act; |
| 7537 | } else { |
| 7538 | pact = NULL; |
| 7539 | } |
| 7540 | |
| 7541 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
| 7542 | |
| 7543 | if (!is_error(ret) && arg3) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7544 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
| 7545 | goto efault; |
bellard | 106ec87 | 2006-06-27 21:08:10 +0000 | [diff] [blame] | 7546 | old_act->_sa_handler = oact._sa_handler; |
| 7547 | old_act->sa_flags = oact.sa_flags; |
| 7548 | old_act->sa_mask.sig[0] = oact.sa_mask.sig[0]; |
| 7549 | old_act->sa_mask.sig[1] = 0; |
| 7550 | old_act->sa_mask.sig[2] = 0; |
| 7551 | old_act->sa_mask.sig[3] = 0; |
| 7552 | unlock_user_struct(old_act, arg3, 1); |
| 7553 | } |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 7554 | #else |
| 7555 | struct target_old_sigaction *old_act; |
| 7556 | struct target_sigaction act, oact, *pact; |
| 7557 | if (arg2) { |
| 7558 | if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) |
| 7559 | goto efault; |
| 7560 | act._sa_handler = old_act->_sa_handler; |
| 7561 | target_siginitset(&act.sa_mask, old_act->sa_mask); |
| 7562 | act.sa_flags = old_act->sa_flags; |
| 7563 | act.sa_restorer = old_act->sa_restorer; |
| 7564 | unlock_user_struct(old_act, arg2, 0); |
| 7565 | pact = &act; |
| 7566 | } else { |
| 7567 | pact = NULL; |
| 7568 | } |
| 7569 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
| 7570 | if (!is_error(ret) && arg3) { |
| 7571 | if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) |
| 7572 | goto efault; |
| 7573 | old_act->_sa_handler = oact._sa_handler; |
| 7574 | old_act->sa_mask = oact.sa_mask.sig[0]; |
| 7575 | old_act->sa_flags = oact.sa_flags; |
| 7576 | old_act->sa_restorer = oact.sa_restorer; |
| 7577 | unlock_user_struct(old_act, arg3, 1); |
| 7578 | } |
ths | 388bb21 | 2007-05-13 13:58:00 +0000 | [diff] [blame] | 7579 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7580 | } |
| 7581 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7582 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7583 | case TARGET_NR_rt_sigaction: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7584 | { |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 7585 | #if defined(TARGET_ALPHA) |
| 7586 | struct target_sigaction act, oact, *pact = 0; |
| 7587 | struct target_rt_sigaction *rt_act; |
| 7588 | /* ??? arg4 == sizeof(sigset_t). */ |
| 7589 | if (arg2) { |
| 7590 | if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1)) |
| 7591 | goto efault; |
| 7592 | act._sa_handler = rt_act->_sa_handler; |
| 7593 | act.sa_mask = rt_act->sa_mask; |
| 7594 | act.sa_flags = rt_act->sa_flags; |
| 7595 | act.sa_restorer = arg5; |
| 7596 | unlock_user_struct(rt_act, arg2, 0); |
| 7597 | pact = &act; |
| 7598 | } |
| 7599 | ret = get_errno(do_sigaction(arg1, pact, &oact)); |
| 7600 | if (!is_error(ret) && arg3) { |
| 7601 | if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0)) |
| 7602 | goto efault; |
| 7603 | rt_act->_sa_handler = oact._sa_handler; |
| 7604 | rt_act->sa_mask = oact.sa_mask; |
| 7605 | rt_act->sa_flags = oact.sa_flags; |
| 7606 | unlock_user_struct(rt_act, arg3, 1); |
| 7607 | } |
| 7608 | #else |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7609 | struct target_sigaction *act; |
| 7610 | struct target_sigaction *oact; |
| 7611 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7612 | if (arg2) { |
| 7613 | if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) |
| 7614 | goto efault; |
| 7615 | } else |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7616 | act = NULL; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7617 | if (arg3) { |
| 7618 | if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) { |
| 7619 | ret = -TARGET_EFAULT; |
| 7620 | goto rt_sigaction_fail; |
| 7621 | } |
| 7622 | } else |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7623 | oact = NULL; |
| 7624 | ret = get_errno(do_sigaction(arg1, act, oact)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7625 | rt_sigaction_fail: |
| 7626 | if (act) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7627 | unlock_user_struct(act, arg2, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7628 | if (oact) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7629 | unlock_user_struct(oact, arg3, 1); |
Richard Henderson | 6049f4f | 2009-12-27 18:30:03 -0800 | [diff] [blame] | 7630 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7631 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7632 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7633 | #ifdef TARGET_NR_sgetmask /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7634 | case TARGET_NR_sgetmask: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7635 | { |
| 7636 | sigset_t cur_set; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7637 | abi_ulong target_set; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7638 | ret = do_sigprocmask(0, NULL, &cur_set); |
| 7639 | if (!ret) { |
| 7640 | host_to_target_old_sigset(&target_set, &cur_set); |
| 7641 | ret = target_set; |
| 7642 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7643 | } |
| 7644 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7645 | #endif |
| 7646 | #ifdef TARGET_NR_ssetmask /* not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7647 | case TARGET_NR_ssetmask: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7648 | { |
| 7649 | sigset_t set, oset, cur_set; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7650 | abi_ulong target_set = arg1; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7651 | /* We only have one word of the new mask so we must read |
| 7652 | * the rest of it with do_sigprocmask() and OR in this word. |
| 7653 | * We are guaranteed that a do_sigprocmask() that only queries |
| 7654 | * the signal mask will not fail. |
| 7655 | */ |
| 7656 | ret = do_sigprocmask(0, NULL, &cur_set); |
| 7657 | assert(!ret); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7658 | target_to_host_old_sigset(&set, &target_set); |
| 7659 | sigorset(&set, &set, &cur_set); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7660 | ret = do_sigprocmask(SIG_SETMASK, &set, &oset); |
| 7661 | if (!ret) { |
| 7662 | host_to_target_old_sigset(&target_set, &oset); |
| 7663 | ret = target_set; |
| 7664 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7665 | } |
| 7666 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 7667 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7668 | #ifdef TARGET_NR_sigprocmask |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7669 | case TARGET_NR_sigprocmask: |
| 7670 | { |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 7671 | #if defined(TARGET_ALPHA) |
| 7672 | sigset_t set, oldset; |
| 7673 | abi_ulong mask; |
| 7674 | int how; |
| 7675 | |
| 7676 | switch (arg1) { |
| 7677 | case TARGET_SIG_BLOCK: |
| 7678 | how = SIG_BLOCK; |
| 7679 | break; |
| 7680 | case TARGET_SIG_UNBLOCK: |
| 7681 | how = SIG_UNBLOCK; |
| 7682 | break; |
| 7683 | case TARGET_SIG_SETMASK: |
| 7684 | how = SIG_SETMASK; |
| 7685 | break; |
| 7686 | default: |
| 7687 | ret = -TARGET_EINVAL; |
| 7688 | goto fail; |
| 7689 | } |
| 7690 | mask = arg2; |
| 7691 | target_to_host_old_sigset(&set, &mask); |
| 7692 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7693 | ret = do_sigprocmask(how, &set, &oldset); |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 7694 | if (!is_error(ret)) { |
| 7695 | host_to_target_old_sigset(&mask, &oldset); |
| 7696 | ret = mask; |
Richard Henderson | 0229f5a | 2012-06-07 15:02:49 -0700 | [diff] [blame] | 7697 | ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */ |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 7698 | } |
| 7699 | #else |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7700 | sigset_t set, oldset, *set_ptr; |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 7701 | int how; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7702 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7703 | if (arg2) { |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 7704 | switch (arg1) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7705 | case TARGET_SIG_BLOCK: |
| 7706 | how = SIG_BLOCK; |
| 7707 | break; |
| 7708 | case TARGET_SIG_UNBLOCK: |
| 7709 | how = SIG_UNBLOCK; |
| 7710 | break; |
| 7711 | case TARGET_SIG_SETMASK: |
| 7712 | how = SIG_SETMASK; |
| 7713 | break; |
| 7714 | default: |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 7715 | ret = -TARGET_EINVAL; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7716 | goto fail; |
| 7717 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7718 | if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7719 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7720 | target_to_host_old_sigset(&set, p); |
| 7721 | unlock_user(p, arg2, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7722 | set_ptr = &set; |
| 7723 | } else { |
| 7724 | how = 0; |
| 7725 | set_ptr = NULL; |
| 7726 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7727 | ret = do_sigprocmask(how, set_ptr, &oldset); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7728 | if (!is_error(ret) && arg3) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7729 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7730 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7731 | host_to_target_old_sigset(p, &oldset); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7732 | unlock_user(p, arg3, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7733 | } |
Richard Henderson | a5b3b13 | 2010-05-03 10:07:55 -0700 | [diff] [blame] | 7734 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7735 | } |
| 7736 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7737 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7738 | case TARGET_NR_rt_sigprocmask: |
| 7739 | { |
| 7740 | int how = arg1; |
| 7741 | sigset_t set, oldset, *set_ptr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7742 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7743 | if (arg2) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7744 | switch(how) { |
| 7745 | case TARGET_SIG_BLOCK: |
| 7746 | how = SIG_BLOCK; |
| 7747 | break; |
| 7748 | case TARGET_SIG_UNBLOCK: |
| 7749 | how = SIG_UNBLOCK; |
| 7750 | break; |
| 7751 | case TARGET_SIG_SETMASK: |
| 7752 | how = SIG_SETMASK; |
| 7753 | break; |
| 7754 | default: |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 7755 | ret = -TARGET_EINVAL; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7756 | goto fail; |
| 7757 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7758 | if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7759 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7760 | target_to_host_sigset(&set, p); |
| 7761 | unlock_user(p, arg2, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7762 | set_ptr = &set; |
| 7763 | } else { |
| 7764 | how = 0; |
| 7765 | set_ptr = NULL; |
| 7766 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7767 | ret = do_sigprocmask(how, set_ptr, &oldset); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7768 | if (!is_error(ret) && arg3) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7769 | if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7770 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7771 | host_to_target_sigset(p, &oldset); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7772 | unlock_user(p, arg3, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7773 | } |
| 7774 | } |
| 7775 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7776 | #ifdef TARGET_NR_sigpending |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7777 | case TARGET_NR_sigpending: |
| 7778 | { |
| 7779 | sigset_t set; |
| 7780 | ret = get_errno(sigpending(&set)); |
| 7781 | if (!is_error(ret)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7782 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7783 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7784 | host_to_target_old_sigset(p, &set); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7785 | unlock_user(p, arg1, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7786 | } |
| 7787 | } |
| 7788 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7789 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7790 | case TARGET_NR_rt_sigpending: |
| 7791 | { |
| 7792 | sigset_t set; |
| 7793 | ret = get_errno(sigpending(&set)); |
| 7794 | if (!is_error(ret)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7795 | if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7796 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7797 | host_to_target_sigset(p, &set); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7798 | unlock_user(p, arg1, sizeof(target_sigset_t)); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7799 | } |
| 7800 | } |
| 7801 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7802 | #ifdef TARGET_NR_sigsuspend |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7803 | case TARGET_NR_sigsuspend: |
| 7804 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7805 | TaskState *ts = cpu->opaque; |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 7806 | #if defined(TARGET_ALPHA) |
| 7807 | abi_ulong mask = arg1; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7808 | target_to_host_old_sigset(&ts->sigsuspend_mask, &mask); |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 7809 | #else |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7810 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7811 | goto efault; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7812 | target_to_host_old_sigset(&ts->sigsuspend_mask, p); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7813 | unlock_user(p, arg1, 0); |
Richard Henderson | f43ce12 | 2010-05-03 10:07:54 -0700 | [diff] [blame] | 7814 | #endif |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7815 | ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask, |
| 7816 | SIGSET_T_SIZE)); |
| 7817 | if (ret != -TARGET_ERESTARTSYS) { |
| 7818 | ts->in_sigsuspend = 1; |
| 7819 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7820 | } |
| 7821 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7822 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7823 | case TARGET_NR_rt_sigsuspend: |
| 7824 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7825 | TaskState *ts = cpu->opaque; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7826 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7827 | goto efault; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7828 | target_to_host_sigset(&ts->sigsuspend_mask, p); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7829 | unlock_user(p, arg1, 0); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7830 | ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask, |
| 7831 | SIGSET_T_SIZE)); |
| 7832 | if (ret != -TARGET_ERESTARTSYS) { |
| 7833 | ts->in_sigsuspend = 1; |
| 7834 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7835 | } |
| 7836 | break; |
| 7837 | case TARGET_NR_rt_sigtimedwait: |
| 7838 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7839 | sigset_t set; |
| 7840 | struct timespec uts, *puts; |
| 7841 | siginfo_t uinfo; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7842 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7843 | if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7844 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7845 | target_to_host_sigset(&set, p); |
| 7846 | unlock_user(p, arg1, 0); |
| 7847 | if (arg3) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7848 | puts = &uts; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7849 | target_to_host_timespec(puts, arg3); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7850 | } else { |
| 7851 | puts = NULL; |
| 7852 | } |
Peter Maydell | b3f8233 | 2016-06-06 19:58:08 +0100 | [diff] [blame] | 7853 | ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts, |
| 7854 | SIGSET_T_SIZE)); |
Petar Jovanovic | 974a196 | 2014-03-03 15:07:41 +0100 | [diff] [blame] | 7855 | if (!is_error(ret)) { |
| 7856 | if (arg2) { |
| 7857 | p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), |
| 7858 | 0); |
| 7859 | if (!p) { |
| 7860 | goto efault; |
| 7861 | } |
| 7862 | host_to_target_siginfo(p, &uinfo); |
| 7863 | unlock_user(p, arg2, sizeof(target_siginfo_t)); |
| 7864 | } |
| 7865 | ret = host_to_target_signal(ret); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7866 | } |
| 7867 | } |
| 7868 | break; |
| 7869 | case TARGET_NR_rt_sigqueueinfo: |
| 7870 | { |
| 7871 | siginfo_t uinfo; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 7872 | if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7873 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7874 | target_to_host_siginfo(&uinfo, p); |
| 7875 | unlock_user(p, arg1, 0); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7876 | ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo)); |
| 7877 | } |
| 7878 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7879 | #ifdef TARGET_NR_sigreturn |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7880 | case TARGET_NR_sigreturn: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7881 | if (block_signals()) { |
| 7882 | ret = -TARGET_ERESTARTSYS; |
| 7883 | } else { |
| 7884 | ret = do_sigreturn(cpu_env); |
| 7885 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7886 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 7887 | #endif |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7888 | case TARGET_NR_rt_sigreturn: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 7889 | if (block_signals()) { |
| 7890 | ret = -TARGET_ERESTARTSYS; |
| 7891 | } else { |
| 7892 | ret = do_rt_sigreturn(cpu_env); |
| 7893 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 7894 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7895 | case TARGET_NR_sethostname: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7896 | if (!(p = lock_user_string(arg1))) |
| 7897 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7898 | ret = get_errno(sethostname(p, arg2)); |
| 7899 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7900 | break; |
| 7901 | case TARGET_NR_setrlimit: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7902 | { |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 7903 | int resource = target_to_host_resource(arg1); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7904 | struct target_rlimit *target_rlim; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7905 | struct rlimit rlim; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7906 | if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) |
| 7907 | goto efault; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 7908 | rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur); |
| 7909 | rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7910 | unlock_user_struct(target_rlim, arg2, 0); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7911 | ret = get_errno(setrlimit(resource, &rlim)); |
| 7912 | } |
| 7913 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7914 | case TARGET_NR_getrlimit: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7915 | { |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 7916 | int resource = target_to_host_resource(arg1); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7917 | struct target_rlimit *target_rlim; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7918 | struct rlimit rlim; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7919 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7920 | ret = get_errno(getrlimit(resource, &rlim)); |
| 7921 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7922 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
| 7923 | goto efault; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 7924 | target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); |
| 7925 | target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7926 | unlock_user_struct(target_rlim, arg2, 1); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 7927 | } |
| 7928 | } |
| 7929 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7930 | case TARGET_NR_getrusage: |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 7931 | { |
| 7932 | struct rusage rusage; |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 7933 | ret = get_errno(getrusage(arg1, &rusage)); |
| 7934 | if (!is_error(ret)) { |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 7935 | ret = host_to_target_rusage(arg2, &rusage); |
bellard | b409186 | 2003-05-16 15:39:34 +0000 | [diff] [blame] | 7936 | } |
| 7937 | } |
| 7938 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7939 | case TARGET_NR_gettimeofday: |
| 7940 | { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7941 | struct timeval tv; |
| 7942 | ret = get_errno(gettimeofday(&tv, NULL)); |
| 7943 | if (!is_error(ret)) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 7944 | if (copy_to_user_timeval(arg1, &tv)) |
| 7945 | goto efault; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7946 | } |
| 7947 | } |
| 7948 | break; |
| 7949 | case TARGET_NR_settimeofday: |
| 7950 | { |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 7951 | struct timeval tv, *ptv = NULL; |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 7952 | struct timezone tz, *ptz = NULL; |
| 7953 | |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 7954 | if (arg1) { |
| 7955 | if (copy_from_user_timeval(&tv, arg1)) { |
| 7956 | goto efault; |
| 7957 | } |
| 7958 | ptv = &tv; |
| 7959 | } |
Paul Burton | ef4467e | 2014-06-22 11:25:40 +0100 | [diff] [blame] | 7960 | |
| 7961 | if (arg2) { |
| 7962 | if (copy_from_user_timezone(&tz, arg2)) { |
| 7963 | goto efault; |
| 7964 | } |
| 7965 | ptz = &tz; |
| 7966 | } |
| 7967 | |
Paul Burton | b67d803 | 2014-06-22 11:25:41 +0100 | [diff] [blame] | 7968 | ret = get_errno(settimeofday(ptv, ptz)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7969 | } |
| 7970 | break; |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 7971 | #if defined(TARGET_NR_select) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 7972 | case TARGET_NR_select: |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 7973 | #if defined(TARGET_S390X) || defined(TARGET_ALPHA) |
| 7974 | ret = do_select(arg1, arg2, arg3, arg4, arg5); |
| 7975 | #else |
bellard | f2674e3 | 2003-07-09 12:26:09 +0000 | [diff] [blame] | 7976 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7977 | struct target_sel_arg_struct *sel; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 7978 | abi_ulong inp, outp, exp, tvp; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7979 | long nsel; |
| 7980 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 7981 | if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) |
| 7982 | goto efault; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 7983 | nsel = tswapal(sel->n); |
| 7984 | inp = tswapal(sel->inp); |
| 7985 | outp = tswapal(sel->outp); |
| 7986 | exp = tswapal(sel->exp); |
| 7987 | tvp = tswapal(sel->tvp); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 7988 | unlock_user_struct(sel, arg1, 0); |
| 7989 | ret = do_select(nsel, inp, outp, exp, tvp); |
bellard | f2674e3 | 2003-07-09 12:26:09 +0000 | [diff] [blame] | 7990 | } |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 7991 | #endif |
bellard | f2674e3 | 2003-07-09 12:26:09 +0000 | [diff] [blame] | 7992 | break; |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 7993 | #endif |
Riku Voipio | 9e42382 | 2010-05-07 12:28:05 +0000 | [diff] [blame] | 7994 | #ifdef TARGET_NR_pselect6 |
| 7995 | case TARGET_NR_pselect6: |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 7996 | { |
| 7997 | abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; |
| 7998 | fd_set rfds, wfds, efds; |
| 7999 | fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; |
| 8000 | struct timespec ts, *ts_ptr; |
| 8001 | |
| 8002 | /* |
| 8003 | * The 6th arg is actually two args smashed together, |
| 8004 | * so we cannot use the C library. |
| 8005 | */ |
| 8006 | sigset_t set; |
| 8007 | struct { |
| 8008 | sigset_t *set; |
| 8009 | size_t size; |
| 8010 | } sig, *sig_ptr; |
| 8011 | |
| 8012 | abi_ulong arg_sigset, arg_sigsize, *arg7; |
| 8013 | target_sigset_t *target_sigset; |
| 8014 | |
| 8015 | n = arg1; |
| 8016 | rfd_addr = arg2; |
| 8017 | wfd_addr = arg3; |
| 8018 | efd_addr = arg4; |
| 8019 | ts_addr = arg5; |
| 8020 | |
| 8021 | ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); |
| 8022 | if (ret) { |
| 8023 | goto fail; |
| 8024 | } |
| 8025 | ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); |
| 8026 | if (ret) { |
| 8027 | goto fail; |
| 8028 | } |
| 8029 | ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); |
| 8030 | if (ret) { |
| 8031 | goto fail; |
| 8032 | } |
| 8033 | |
| 8034 | /* |
| 8035 | * This takes a timespec, and not a timeval, so we cannot |
| 8036 | * use the do_select() helper ... |
| 8037 | */ |
| 8038 | if (ts_addr) { |
| 8039 | if (target_to_host_timespec(&ts, ts_addr)) { |
| 8040 | goto efault; |
| 8041 | } |
| 8042 | ts_ptr = &ts; |
| 8043 | } else { |
| 8044 | ts_ptr = NULL; |
| 8045 | } |
| 8046 | |
| 8047 | /* Extract the two packed args for the sigset */ |
| 8048 | if (arg6) { |
| 8049 | sig_ptr = &sig; |
Peter Maydell | b28a1f3 | 2016-05-27 15:51:47 +0100 | [diff] [blame] | 8050 | sig.size = SIGSET_T_SIZE; |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 8051 | |
| 8052 | arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); |
| 8053 | if (!arg7) { |
| 8054 | goto efault; |
| 8055 | } |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 8056 | arg_sigset = tswapal(arg7[0]); |
| 8057 | arg_sigsize = tswapal(arg7[1]); |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 8058 | unlock_user(arg7, arg6, 0); |
| 8059 | |
| 8060 | if (arg_sigset) { |
| 8061 | sig.set = &set; |
Peter Maydell | 8f04eeb | 2011-06-28 12:21:57 +0100 | [diff] [blame] | 8062 | if (arg_sigsize != sizeof(*target_sigset)) { |
| 8063 | /* Like the kernel, we enforce correct size sigsets */ |
| 8064 | ret = -TARGET_EINVAL; |
| 8065 | goto fail; |
| 8066 | } |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 8067 | target_sigset = lock_user(VERIFY_READ, arg_sigset, |
| 8068 | sizeof(*target_sigset), 1); |
| 8069 | if (!target_sigset) { |
| 8070 | goto efault; |
| 8071 | } |
| 8072 | target_to_host_sigset(&set, target_sigset); |
| 8073 | unlock_user(target_sigset, arg_sigset, 0); |
| 8074 | } else { |
| 8075 | sig.set = NULL; |
| 8076 | } |
| 8077 | } else { |
| 8078 | sig_ptr = NULL; |
| 8079 | } |
| 8080 | |
Peter Maydell | 6df9d38 | 2016-05-12 18:47:51 +0100 | [diff] [blame] | 8081 | ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, |
| 8082 | ts_ptr, sig_ptr)); |
Mike Frysinger | 055e090 | 2011-06-03 17:01:49 -0400 | [diff] [blame] | 8083 | |
| 8084 | if (!is_error(ret)) { |
| 8085 | if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) |
| 8086 | goto efault; |
| 8087 | if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) |
| 8088 | goto efault; |
| 8089 | if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) |
| 8090 | goto efault; |
| 8091 | |
| 8092 | if (ts_addr && host_to_target_timespec(ts_addr, &ts)) |
| 8093 | goto efault; |
| 8094 | } |
| 8095 | } |
| 8096 | break; |
Riku Voipio | 9e42382 | 2010-05-07 12:28:05 +0000 | [diff] [blame] | 8097 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8098 | #ifdef TARGET_NR_symlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8099 | case TARGET_NR_symlink: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8100 | { |
| 8101 | void *p2; |
| 8102 | p = lock_user_string(arg1); |
| 8103 | p2 = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8104 | if (!p || !p2) |
| 8105 | ret = -TARGET_EFAULT; |
| 8106 | else |
| 8107 | ret = get_errno(symlink(p, p2)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8108 | unlock_user(p2, arg2, 0); |
| 8109 | unlock_user(p, arg1, 0); |
| 8110 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8111 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8112 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8113 | #if defined(TARGET_NR_symlinkat) |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 8114 | case TARGET_NR_symlinkat: |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 8115 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8116 | void *p2; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 8117 | p = lock_user_string(arg1); |
| 8118 | p2 = lock_user_string(arg3); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8119 | if (!p || !p2) |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 8120 | ret = -TARGET_EFAULT; |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 8121 | else |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8122 | ret = get_errno(symlinkat(p, arg2, p2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8123 | unlock_user(p2, arg3, 0); |
| 8124 | unlock_user(p, arg1, 0); |
ths | f0b6243 | 2007-09-24 09:25:40 +0000 | [diff] [blame] | 8125 | } |
| 8126 | break; |
| 8127 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8128 | #ifdef TARGET_NR_oldlstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8129 | case TARGET_NR_oldlstat: |
| 8130 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8131 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8132 | #ifdef TARGET_NR_readlink |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8133 | case TARGET_NR_readlink: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8134 | { |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8135 | void *p2; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8136 | p = lock_user_string(arg1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8137 | p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8138 | if (!p || !p2) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8139 | ret = -TARGET_EFAULT; |
Mike Frysinger | f17f498 | 2014-08-08 09:40:25 +0900 | [diff] [blame] | 8140 | } else if (!arg3) { |
| 8141 | /* Short circuit this for the magic exe check. */ |
| 8142 | ret = -TARGET_EINVAL; |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8143 | } else if (is_proc_myself((const char *)p, "exe")) { |
| 8144 | char real[PATH_MAX], *temp; |
| 8145 | temp = realpath(exec_path, real); |
Mike Frysinger | f17f498 | 2014-08-08 09:40:25 +0900 | [diff] [blame] | 8146 | /* Return value is # of bytes that we wrote to the buffer. */ |
| 8147 | if (temp == NULL) { |
| 8148 | ret = get_errno(-1); |
| 8149 | } else { |
| 8150 | /* Don't worry about sign mismatch as earlier mapping |
| 8151 | * logic would have thrown a bad address error. */ |
| 8152 | ret = MIN(strlen(real), arg3); |
| 8153 | /* We cannot NUL terminate the string. */ |
| 8154 | memcpy(p2, real, ret); |
| 8155 | } |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8156 | } else { |
| 8157 | ret = get_errno(readlink(path(p), p2, arg3)); |
aurel32 | d088d66 | 2009-01-30 20:09:01 +0000 | [diff] [blame] | 8158 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8159 | unlock_user(p2, arg2, ret); |
| 8160 | unlock_user(p, arg1, 0); |
| 8161 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8162 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8163 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8164 | #if defined(TARGET_NR_readlinkat) |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 8165 | case TARGET_NR_readlinkat: |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 8166 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8167 | void *p2; |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 8168 | p = lock_user_string(arg2); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8169 | p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8170 | if (!p || !p2) { |
| 8171 | ret = -TARGET_EFAULT; |
| 8172 | } else if (is_proc_myself((const char *)p, "exe")) { |
| 8173 | char real[PATH_MAX], *temp; |
| 8174 | temp = realpath(exec_path, real); |
| 8175 | ret = temp == NULL ? get_errno(-1) : strlen(real) ; |
| 8176 | snprintf((char *)p2, arg4, "%s", real); |
| 8177 | } else { |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8178 | ret = get_errno(readlinkat(arg1, path(p), p2, arg4)); |
Andreas Schwab | 463d8e7 | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 8179 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8180 | unlock_user(p2, arg3, ret); |
| 8181 | unlock_user(p, arg2, 0); |
ths | 5e0ccb1 | 2007-09-24 09:26:10 +0000 | [diff] [blame] | 8182 | } |
| 8183 | break; |
| 8184 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8185 | #ifdef TARGET_NR_uselib |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8186 | case TARGET_NR_uselib: |
| 8187 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8188 | #endif |
| 8189 | #ifdef TARGET_NR_swapon |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8190 | case TARGET_NR_swapon: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8191 | if (!(p = lock_user_string(arg1))) |
| 8192 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8193 | ret = get_errno(swapon(p, arg2)); |
| 8194 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8195 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8196 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8197 | case TARGET_NR_reboot: |
Laurent Vivier | c07ecc6 | 2013-01-07 11:40:06 +0000 | [diff] [blame] | 8198 | if (arg3 == LINUX_REBOOT_CMD_RESTART2) { |
| 8199 | /* arg4 must be ignored in all other cases */ |
| 8200 | p = lock_user_string(arg4); |
| 8201 | if (!p) { |
| 8202 | goto efault; |
| 8203 | } |
| 8204 | ret = get_errno(reboot(arg1, arg2, arg3, p)); |
| 8205 | unlock_user(p, arg4, 0); |
| 8206 | } else { |
| 8207 | ret = get_errno(reboot(arg1, arg2, arg3, NULL)); |
| 8208 | } |
Alexander Graf | 0f6b4d2 | 2011-09-27 14:39:42 +0200 | [diff] [blame] | 8209 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8210 | #ifdef TARGET_NR_readdir |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8211 | case TARGET_NR_readdir: |
| 8212 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8213 | #endif |
| 8214 | #ifdef TARGET_NR_mmap |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8215 | case TARGET_NR_mmap: |
Alexander Graf | 0970119 | 2013-09-03 20:12:15 +0100 | [diff] [blame] | 8216 | #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \ |
| 8217 | (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \ |
Ulrich Hecht | a4c075f | 2009-07-24 16:57:31 +0200 | [diff] [blame] | 8218 | defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \ |
| 8219 | || defined(TARGET_S390X) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8220 | { |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8221 | abi_ulong *v; |
| 8222 | abi_ulong v1, v2, v3, v4, v5, v6; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8223 | if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1))) |
| 8224 | goto efault; |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 8225 | v1 = tswapal(v[0]); |
| 8226 | v2 = tswapal(v[1]); |
| 8227 | v3 = tswapal(v[2]); |
| 8228 | v4 = tswapal(v[3]); |
| 8229 | v5 = tswapal(v[4]); |
| 8230 | v6 = tswapal(v[5]); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8231 | unlock_user(v, arg1, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8232 | ret = get_errno(target_mmap(v1, v2, v3, |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 8233 | target_to_host_bitmask(v4, mmap_flags_tbl), |
| 8234 | v5, v6)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8235 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8236 | #else |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8237 | ret = get_errno(target_mmap(arg1, arg2, arg3, |
| 8238 | target_to_host_bitmask(arg4, mmap_flags_tbl), |
bellard | 6fb883e | 2003-07-09 17:12:39 +0000 | [diff] [blame] | 8239 | arg5, |
| 8240 | arg6)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8241 | #endif |
bellard | 6fb883e | 2003-07-09 17:12:39 +0000 | [diff] [blame] | 8242 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8243 | #endif |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 8244 | #ifdef TARGET_NR_mmap2 |
bellard | 6fb883e | 2003-07-09 17:12:39 +0000 | [diff] [blame] | 8245 | case TARGET_NR_mmap2: |
pbrook | bb7ec04 | 2008-03-25 22:28:25 +0000 | [diff] [blame] | 8246 | #ifndef MMAP_SHIFT |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 8247 | #define MMAP_SHIFT 12 |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 8248 | #endif |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8249 | ret = get_errno(target_mmap(arg1, arg2, arg3, |
| 8250 | target_to_host_bitmask(arg4, mmap_flags_tbl), |
bellard | 5286db7 | 2003-06-05 00:57:30 +0000 | [diff] [blame] | 8251 | arg5, |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 8252 | arg6 << MMAP_SHIFT)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8253 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 8254 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8255 | case TARGET_NR_munmap: |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 8256 | ret = get_errno(target_munmap(arg1, arg2)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8257 | break; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8258 | case TARGET_NR_mprotect: |
Paul Brook | 97374d3 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 8259 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 8260 | TaskState *ts = cpu->opaque; |
Paul Brook | 97374d3 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 8261 | /* Special hack to detect libc making the stack executable. */ |
| 8262 | if ((arg3 & PROT_GROWSDOWN) |
| 8263 | && arg1 >= ts->info->stack_limit |
| 8264 | && arg1 <= ts->info->start_stack) { |
| 8265 | arg3 &= ~PROT_GROWSDOWN; |
| 8266 | arg2 = arg2 + arg1 - ts->info->stack_limit; |
| 8267 | arg1 = ts->info->stack_limit; |
| 8268 | } |
| 8269 | } |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 8270 | ret = get_errno(target_mprotect(arg1, arg2, arg3)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8271 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8272 | #ifdef TARGET_NR_mremap |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8273 | case TARGET_NR_mremap: |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 8274 | ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8275 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8276 | #endif |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8277 | /* ??? msync/mlock/munlock are broken for softmmu. */ |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8278 | #ifdef TARGET_NR_msync |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8279 | case TARGET_NR_msync: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8280 | ret = get_errno(msync(g2h(arg1), arg2, arg3)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8281 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8282 | #endif |
| 8283 | #ifdef TARGET_NR_mlock |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8284 | case TARGET_NR_mlock: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8285 | ret = get_errno(mlock(g2h(arg1), arg2)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8286 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8287 | #endif |
| 8288 | #ifdef TARGET_NR_munlock |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8289 | case TARGET_NR_munlock: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8290 | ret = get_errno(munlock(g2h(arg1), arg2)); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8291 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8292 | #endif |
| 8293 | #ifdef TARGET_NR_mlockall |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8294 | case TARGET_NR_mlockall: |
Tom Musta | 6f6a403 | 2014-08-12 13:53:42 -0500 | [diff] [blame] | 8295 | ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1))); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8296 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8297 | #endif |
| 8298 | #ifdef TARGET_NR_munlockall |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 8299 | case TARGET_NR_munlockall: |
| 8300 | ret = get_errno(munlockall()); |
| 8301 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8302 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8303 | case TARGET_NR_truncate: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8304 | if (!(p = lock_user_string(arg1))) |
| 8305 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8306 | ret = get_errno(truncate(p, arg2)); |
| 8307 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8308 | break; |
| 8309 | case TARGET_NR_ftruncate: |
| 8310 | ret = get_errno(ftruncate(arg1, arg2)); |
| 8311 | break; |
| 8312 | case TARGET_NR_fchmod: |
| 8313 | ret = get_errno(fchmod(arg1, arg2)); |
| 8314 | break; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8315 | #if defined(TARGET_NR_fchmodat) |
ths | 814d797 | 2007-09-24 09:26:51 +0000 | [diff] [blame] | 8316 | case TARGET_NR_fchmodat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8317 | if (!(p = lock_user_string(arg2))) |
| 8318 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8319 | ret = get_errno(fchmodat(arg1, p, arg3, 0)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8320 | unlock_user(p, arg2, 0); |
ths | 814d797 | 2007-09-24 09:26:51 +0000 | [diff] [blame] | 8321 | break; |
| 8322 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8323 | case TARGET_NR_getpriority: |
Richard Henderson | 95c0982 | 2012-06-07 15:14:50 -0700 | [diff] [blame] | 8324 | /* Note that negative values are valid for getpriority, so we must |
| 8325 | differentiate based on errno settings. */ |
| 8326 | errno = 0; |
| 8327 | ret = getpriority(arg1, arg2); |
| 8328 | if (ret == -1 && errno != 0) { |
| 8329 | ret = -host_to_target_errno(errno); |
| 8330 | break; |
| 8331 | } |
| 8332 | #ifdef TARGET_ALPHA |
| 8333 | /* Return value is the unbiased priority. Signal no error. */ |
| 8334 | ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; |
| 8335 | #else |
| 8336 | /* Return value is a biased priority to avoid negative numbers. */ |
| 8337 | ret = 20 - ret; |
| 8338 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8339 | break; |
| 8340 | case TARGET_NR_setpriority: |
| 8341 | ret = get_errno(setpriority(arg1, arg2, arg3)); |
| 8342 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8343 | #ifdef TARGET_NR_profil |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8344 | case TARGET_NR_profil: |
| 8345 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8346 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8347 | case TARGET_NR_statfs: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8348 | if (!(p = lock_user_string(arg1))) |
| 8349 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8350 | ret = get_errno(statfs(path(p), &stfs)); |
| 8351 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8352 | convert_statfs: |
| 8353 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8354 | struct target_statfs *target_stfs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8355 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8356 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0)) |
| 8357 | goto efault; |
| 8358 | __put_user(stfs.f_type, &target_stfs->f_type); |
| 8359 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); |
| 8360 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); |
| 8361 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); |
| 8362 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); |
| 8363 | __put_user(stfs.f_files, &target_stfs->f_files); |
| 8364 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); |
| 8365 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); |
| 8366 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); |
| 8367 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 8368 | __put_user(stfs.f_frsize, &target_stfs->f_frsize); |
| 8369 | memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8370 | unlock_user_struct(target_stfs, arg2, 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8371 | } |
| 8372 | break; |
| 8373 | case TARGET_NR_fstatfs: |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 8374 | ret = get_errno(fstatfs(arg1, &stfs)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8375 | goto convert_statfs; |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 8376 | #ifdef TARGET_NR_statfs64 |
| 8377 | case TARGET_NR_statfs64: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8378 | if (!(p = lock_user_string(arg1))) |
| 8379 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8380 | ret = get_errno(statfs(path(p), &stfs)); |
| 8381 | unlock_user(p, arg1, 0); |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 8382 | convert_statfs64: |
| 8383 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8384 | struct target_statfs64 *target_stfs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8385 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8386 | if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0)) |
| 8387 | goto efault; |
| 8388 | __put_user(stfs.f_type, &target_stfs->f_type); |
| 8389 | __put_user(stfs.f_bsize, &target_stfs->f_bsize); |
| 8390 | __put_user(stfs.f_blocks, &target_stfs->f_blocks); |
| 8391 | __put_user(stfs.f_bfree, &target_stfs->f_bfree); |
| 8392 | __put_user(stfs.f_bavail, &target_stfs->f_bavail); |
| 8393 | __put_user(stfs.f_files, &target_stfs->f_files); |
| 8394 | __put_user(stfs.f_ffree, &target_stfs->f_ffree); |
| 8395 | __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); |
| 8396 | __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); |
| 8397 | __put_user(stfs.f_namelen, &target_stfs->f_namelen); |
Alexander Graf | 229d337 | 2012-09-19 04:39:53 +0200 | [diff] [blame] | 8398 | __put_user(stfs.f_frsize, &target_stfs->f_frsize); |
| 8399 | memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8400 | unlock_user_struct(target_stfs, arg3, 1); |
bellard | 56c8f68 | 2005-11-28 22:28:41 +0000 | [diff] [blame] | 8401 | } |
| 8402 | break; |
| 8403 | case TARGET_NR_fstatfs64: |
| 8404 | ret = get_errno(fstatfs(arg1, &stfs)); |
| 8405 | goto convert_statfs64; |
| 8406 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8407 | #ifdef TARGET_NR_ioperm |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8408 | case TARGET_NR_ioperm: |
| 8409 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8410 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8411 | #ifdef TARGET_NR_socketcall |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8412 | case TARGET_NR_socketcall: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8413 | ret = do_socketcall(arg1, arg2); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8414 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8415 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8416 | #ifdef TARGET_NR_accept |
| 8417 | case TARGET_NR_accept: |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 8418 | ret = do_accept4(arg1, arg2, arg3, 0); |
| 8419 | break; |
| 8420 | #endif |
| 8421 | #ifdef TARGET_NR_accept4 |
| 8422 | case TARGET_NR_accept4: |
Peter Maydell | a94b498 | 2013-02-08 04:35:04 +0000 | [diff] [blame] | 8423 | ret = do_accept4(arg1, arg2, arg3, arg4); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8424 | break; |
| 8425 | #endif |
| 8426 | #ifdef TARGET_NR_bind |
| 8427 | case TARGET_NR_bind: |
| 8428 | ret = do_bind(arg1, arg2, arg3); |
| 8429 | break; |
| 8430 | #endif |
| 8431 | #ifdef TARGET_NR_connect |
| 8432 | case TARGET_NR_connect: |
| 8433 | ret = do_connect(arg1, arg2, arg3); |
| 8434 | break; |
| 8435 | #endif |
| 8436 | #ifdef TARGET_NR_getpeername |
| 8437 | case TARGET_NR_getpeername: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8438 | ret = do_getpeername(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8439 | break; |
| 8440 | #endif |
| 8441 | #ifdef TARGET_NR_getsockname |
| 8442 | case TARGET_NR_getsockname: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8443 | ret = do_getsockname(arg1, arg2, arg3); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8444 | break; |
| 8445 | #endif |
| 8446 | #ifdef TARGET_NR_getsockopt |
| 8447 | case TARGET_NR_getsockopt: |
| 8448 | ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5); |
| 8449 | break; |
| 8450 | #endif |
| 8451 | #ifdef TARGET_NR_listen |
| 8452 | case TARGET_NR_listen: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8453 | ret = get_errno(listen(arg1, arg2)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8454 | break; |
| 8455 | #endif |
| 8456 | #ifdef TARGET_NR_recv |
| 8457 | case TARGET_NR_recv: |
pbrook | 214201b | 2007-03-17 01:27:24 +0000 | [diff] [blame] | 8458 | ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8459 | break; |
| 8460 | #endif |
| 8461 | #ifdef TARGET_NR_recvfrom |
| 8462 | case TARGET_NR_recvfrom: |
pbrook | 214201b | 2007-03-17 01:27:24 +0000 | [diff] [blame] | 8463 | ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8464 | break; |
| 8465 | #endif |
| 8466 | #ifdef TARGET_NR_recvmsg |
| 8467 | case TARGET_NR_recvmsg: |
| 8468 | ret = do_sendrecvmsg(arg1, arg2, arg3, 0); |
| 8469 | break; |
| 8470 | #endif |
| 8471 | #ifdef TARGET_NR_send |
| 8472 | case TARGET_NR_send: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8473 | ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8474 | break; |
| 8475 | #endif |
| 8476 | #ifdef TARGET_NR_sendmsg |
| 8477 | case TARGET_NR_sendmsg: |
| 8478 | ret = do_sendrecvmsg(arg1, arg2, arg3, 1); |
| 8479 | break; |
| 8480 | #endif |
Alexander Graf | f19e00d | 2014-03-02 19:36:42 +0000 | [diff] [blame] | 8481 | #ifdef TARGET_NR_sendmmsg |
| 8482 | case TARGET_NR_sendmmsg: |
| 8483 | ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1); |
| 8484 | break; |
| 8485 | case TARGET_NR_recvmmsg: |
| 8486 | ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0); |
| 8487 | break; |
| 8488 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8489 | #ifdef TARGET_NR_sendto |
| 8490 | case TARGET_NR_sendto: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8491 | ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8492 | break; |
| 8493 | #endif |
| 8494 | #ifdef TARGET_NR_shutdown |
| 8495 | case TARGET_NR_shutdown: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8496 | ret = get_errno(shutdown(arg1, arg2)); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8497 | break; |
| 8498 | #endif |
Laurent Vivier | f894efd | 2016-02-21 10:56:23 +0100 | [diff] [blame] | 8499 | #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) |
| 8500 | case TARGET_NR_getrandom: |
| 8501 | p = lock_user(VERIFY_WRITE, arg1, arg2, 0); |
| 8502 | if (!p) { |
| 8503 | goto efault; |
| 8504 | } |
| 8505 | ret = get_errno(getrandom(p, arg2, arg3)); |
| 8506 | unlock_user(p, arg1, ret); |
| 8507 | break; |
| 8508 | #endif |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8509 | #ifdef TARGET_NR_socket |
| 8510 | case TARGET_NR_socket: |
| 8511 | ret = do_socket(arg1, arg2, arg3); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 8512 | fd_trans_unregister(ret); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8513 | break; |
| 8514 | #endif |
| 8515 | #ifdef TARGET_NR_socketpair |
| 8516 | case TARGET_NR_socketpair: |
pbrook | 1be9e1d | 2006-11-19 15:26:04 +0000 | [diff] [blame] | 8517 | ret = do_socketpair(arg1, arg2, arg3, arg4); |
bellard | 3532fa7 | 2006-06-24 15:06:03 +0000 | [diff] [blame] | 8518 | break; |
| 8519 | #endif |
| 8520 | #ifdef TARGET_NR_setsockopt |
| 8521 | case TARGET_NR_setsockopt: |
| 8522 | ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5); |
| 8523 | break; |
| 8524 | #endif |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 8525 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8526 | case TARGET_NR_syslog: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8527 | if (!(p = lock_user_string(arg2))) |
| 8528 | goto efault; |
ths | e557448 | 2007-02-11 20:03:13 +0000 | [diff] [blame] | 8529 | ret = get_errno(sys_syslog((int)arg1, p, (int)arg3)); |
| 8530 | unlock_user(p, arg2, 0); |
ths | 7494b0f | 2007-02-11 18:26:53 +0000 | [diff] [blame] | 8531 | break; |
| 8532 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8533 | case TARGET_NR_setitimer: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8534 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8535 | struct itimerval value, ovalue, *pvalue; |
| 8536 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8537 | if (arg2) { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8538 | pvalue = &value; |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 8539 | if (copy_from_user_timeval(&pvalue->it_interval, arg2) |
| 8540 | || copy_from_user_timeval(&pvalue->it_value, |
| 8541 | arg2 + sizeof(struct target_timeval))) |
| 8542 | goto efault; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8543 | } else { |
| 8544 | pvalue = NULL; |
| 8545 | } |
| 8546 | ret = get_errno(setitimer(arg1, pvalue, &ovalue)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8547 | if (!is_error(ret) && arg3) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 8548 | if (copy_to_user_timeval(arg3, |
| 8549 | &ovalue.it_interval) |
| 8550 | || copy_to_user_timeval(arg3 + sizeof(struct target_timeval), |
| 8551 | &ovalue.it_value)) |
| 8552 | goto efault; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8553 | } |
| 8554 | } |
| 8555 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8556 | case TARGET_NR_getitimer: |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8557 | { |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8558 | struct itimerval value; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8559 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8560 | ret = get_errno(getitimer(arg1, &value)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8561 | if (!is_error(ret) && arg2) { |
ths | 788f5ec | 2007-12-09 02:37:05 +0000 | [diff] [blame] | 8562 | if (copy_to_user_timeval(arg2, |
| 8563 | &value.it_interval) |
| 8564 | || copy_to_user_timeval(arg2 + sizeof(struct target_timeval), |
| 8565 | &value.it_value)) |
| 8566 | goto efault; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 8567 | } |
| 8568 | } |
| 8569 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8570 | #ifdef TARGET_NR_stat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8571 | case TARGET_NR_stat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8572 | if (!(p = lock_user_string(arg1))) |
| 8573 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8574 | ret = get_errno(stat(path(p), &st)); |
| 8575 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8576 | goto do_stat; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8577 | #endif |
| 8578 | #ifdef TARGET_NR_lstat |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8579 | case TARGET_NR_lstat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8580 | if (!(p = lock_user_string(arg1))) |
| 8581 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8582 | ret = get_errno(lstat(path(p), &st)); |
| 8583 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8584 | goto do_stat; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8585 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8586 | case TARGET_NR_fstat: |
| 8587 | { |
| 8588 | ret = get_errno(fstat(arg1, &st)); |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8589 | #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8590 | do_stat: |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8591 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8592 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8593 | struct target_stat *target_st; |
ths | e358465 | 2007-06-01 11:49:38 +0000 | [diff] [blame] | 8594 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8595 | if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) |
| 8596 | goto efault; |
Ulrich Hecht | 1272791 | 2009-07-24 19:10:32 +0200 | [diff] [blame] | 8597 | memset(target_st, 0, sizeof(*target_st)); |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 8598 | __put_user(st.st_dev, &target_st->st_dev); |
| 8599 | __put_user(st.st_ino, &target_st->st_ino); |
| 8600 | __put_user(st.st_mode, &target_st->st_mode); |
| 8601 | __put_user(st.st_uid, &target_st->st_uid); |
| 8602 | __put_user(st.st_gid, &target_st->st_gid); |
| 8603 | __put_user(st.st_nlink, &target_st->st_nlink); |
| 8604 | __put_user(st.st_rdev, &target_st->st_rdev); |
| 8605 | __put_user(st.st_size, &target_st->st_size); |
| 8606 | __put_user(st.st_blksize, &target_st->st_blksize); |
| 8607 | __put_user(st.st_blocks, &target_st->st_blocks); |
| 8608 | __put_user(st.st_atime, &target_st->target_st_atime); |
| 8609 | __put_user(st.st_mtime, &target_st->target_st_mtime); |
| 8610 | __put_user(st.st_ctime, &target_st->target_st_ctime); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8611 | unlock_user_struct(target_st, arg2, 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8612 | } |
| 8613 | } |
| 8614 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8615 | #ifdef TARGET_NR_olduname |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8616 | case TARGET_NR_olduname: |
| 8617 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8618 | #endif |
| 8619 | #ifdef TARGET_NR_iopl |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8620 | case TARGET_NR_iopl: |
| 8621 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8622 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8623 | case TARGET_NR_vhangup: |
| 8624 | ret = get_errno(vhangup()); |
| 8625 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8626 | #ifdef TARGET_NR_idle |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8627 | case TARGET_NR_idle: |
| 8628 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 8629 | #endif |
bellard | 42ad6ae | 2005-01-03 22:48:11 +0000 | [diff] [blame] | 8630 | #ifdef TARGET_NR_syscall |
| 8631 | case TARGET_NR_syscall: |
Peter Maydell | 5945cfc | 2011-06-16 17:37:13 +0100 | [diff] [blame] | 8632 | ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5, |
| 8633 | arg6, arg7, arg8, 0); |
| 8634 | break; |
bellard | 42ad6ae | 2005-01-03 22:48:11 +0000 | [diff] [blame] | 8635 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8636 | case TARGET_NR_wait4: |
| 8637 | { |
| 8638 | int status; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8639 | abi_long status_ptr = arg2; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8640 | struct rusage rusage, *rusage_ptr; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8641 | abi_ulong target_rusage = arg4; |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 8642 | abi_long rusage_err; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8643 | if (target_rusage) |
| 8644 | rusage_ptr = &rusage; |
| 8645 | else |
| 8646 | rusage_ptr = NULL; |
Timothy E Baldwin | 4af80a3 | 2016-05-12 18:47:49 +0100 | [diff] [blame] | 8647 | ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8648 | if (!is_error(ret)) { |
Alexander Graf | 5379557 | 2011-11-24 00:44:43 +0100 | [diff] [blame] | 8649 | if (status_ptr && ret) { |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 8650 | status = host_to_target_waitstatus(status); |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 8651 | if (put_user_s32(status, status_ptr)) |
| 8652 | goto efault; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8653 | } |
Petar Jovanovic | a39fb27 | 2014-04-08 19:24:30 +0200 | [diff] [blame] | 8654 | if (target_rusage) { |
| 8655 | rusage_err = host_to_target_rusage(target_rusage, &rusage); |
| 8656 | if (rusage_err) { |
| 8657 | ret = rusage_err; |
| 8658 | } |
| 8659 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8660 | } |
| 8661 | } |
| 8662 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8663 | #ifdef TARGET_NR_swapoff |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8664 | case TARGET_NR_swapoff: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8665 | if (!(p = lock_user_string(arg1))) |
| 8666 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8667 | ret = get_errno(swapoff(p)); |
| 8668 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8669 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8670 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8671 | case TARGET_NR_sysinfo: |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 8672 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8673 | struct target_sysinfo *target_value; |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 8674 | struct sysinfo value; |
| 8675 | ret = get_errno(sysinfo(&value)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8676 | if (!is_error(ret) && arg1) |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 8677 | { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8678 | if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0)) |
| 8679 | goto efault; |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 8680 | __put_user(value.uptime, &target_value->uptime); |
| 8681 | __put_user(value.loads[0], &target_value->loads[0]); |
| 8682 | __put_user(value.loads[1], &target_value->loads[1]); |
| 8683 | __put_user(value.loads[2], &target_value->loads[2]); |
| 8684 | __put_user(value.totalram, &target_value->totalram); |
| 8685 | __put_user(value.freeram, &target_value->freeram); |
| 8686 | __put_user(value.sharedram, &target_value->sharedram); |
| 8687 | __put_user(value.bufferram, &target_value->bufferram); |
| 8688 | __put_user(value.totalswap, &target_value->totalswap); |
| 8689 | __put_user(value.freeswap, &target_value->freeswap); |
| 8690 | __put_user(value.procs, &target_value->procs); |
| 8691 | __put_user(value.totalhigh, &target_value->totalhigh); |
| 8692 | __put_user(value.freehigh, &target_value->freehigh); |
| 8693 | __put_user(value.mem_unit, &target_value->mem_unit); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8694 | unlock_user_struct(target_value, arg1, 1); |
bellard | a5448a7 | 2004-06-19 16:59:03 +0000 | [diff] [blame] | 8695 | } |
| 8696 | } |
| 8697 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8698 | #ifdef TARGET_NR_ipc |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8699 | case TARGET_NR_ipc: |
bellard | 8853f86 | 2004-02-22 14:57:26 +0000 | [diff] [blame] | 8700 | ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6); |
| 8701 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8702 | #endif |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 8703 | #ifdef TARGET_NR_semget |
| 8704 | case TARGET_NR_semget: |
| 8705 | ret = get_errno(semget(arg1, arg2, arg3)); |
| 8706 | break; |
| 8707 | #endif |
| 8708 | #ifdef TARGET_NR_semop |
| 8709 | case TARGET_NR_semop: |
Petar Jovanovic | c7128c9 | 2013-03-21 07:57:36 +0000 | [diff] [blame] | 8710 | ret = do_semop(arg1, arg2, arg3); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 8711 | break; |
| 8712 | #endif |
| 8713 | #ifdef TARGET_NR_semctl |
| 8714 | case TARGET_NR_semctl: |
Stefan Weil | d1c002b | 2015-02-08 15:40:58 +0100 | [diff] [blame] | 8715 | ret = do_semctl(arg1, arg2, arg3, arg4); |
aurel32 | e528908 | 2009-04-18 16:16:12 +0000 | [diff] [blame] | 8716 | break; |
| 8717 | #endif |
aurel32 | eeb438c | 2008-10-13 21:08:55 +0000 | [diff] [blame] | 8718 | #ifdef TARGET_NR_msgctl |
| 8719 | case TARGET_NR_msgctl: |
| 8720 | ret = do_msgctl(arg1, arg2, arg3); |
| 8721 | break; |
| 8722 | #endif |
| 8723 | #ifdef TARGET_NR_msgget |
| 8724 | case TARGET_NR_msgget: |
| 8725 | ret = get_errno(msgget(arg1, arg2)); |
| 8726 | break; |
| 8727 | #endif |
| 8728 | #ifdef TARGET_NR_msgrcv |
| 8729 | case TARGET_NR_msgrcv: |
| 8730 | ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5); |
| 8731 | break; |
| 8732 | #endif |
| 8733 | #ifdef TARGET_NR_msgsnd |
| 8734 | case TARGET_NR_msgsnd: |
| 8735 | ret = do_msgsnd(arg1, arg2, arg3, arg4); |
| 8736 | break; |
| 8737 | #endif |
Riku Voipio | 88a8c98 | 2009-04-03 10:42:00 +0300 | [diff] [blame] | 8738 | #ifdef TARGET_NR_shmget |
| 8739 | case TARGET_NR_shmget: |
| 8740 | ret = get_errno(shmget(arg1, arg2, arg3)); |
| 8741 | break; |
| 8742 | #endif |
| 8743 | #ifdef TARGET_NR_shmctl |
| 8744 | case TARGET_NR_shmctl: |
| 8745 | ret = do_shmctl(arg1, arg2, arg3); |
| 8746 | break; |
| 8747 | #endif |
| 8748 | #ifdef TARGET_NR_shmat |
| 8749 | case TARGET_NR_shmat: |
| 8750 | ret = do_shmat(arg1, arg2, arg3); |
| 8751 | break; |
| 8752 | #endif |
| 8753 | #ifdef TARGET_NR_shmdt |
| 8754 | case TARGET_NR_shmdt: |
| 8755 | ret = do_shmdt(arg1); |
| 8756 | break; |
| 8757 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8758 | case TARGET_NR_fsync: |
| 8759 | ret = get_errno(fsync(arg1)); |
| 8760 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8761 | case TARGET_NR_clone: |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 8762 | /* Linux manages to have three different orderings for its |
| 8763 | * arguments to clone(); the BACKWARDS and BACKWARDS2 defines |
| 8764 | * match the kernel's CONFIG_CLONE_* settings. |
| 8765 | * Microblaze is further special in that it uses a sixth |
| 8766 | * implicit argument to clone for the TLS pointer. |
| 8767 | */ |
| 8768 | #if defined(TARGET_MICROBLAZE) |
Edgar E. Iglesias | a5b3bdc | 2012-04-26 14:17:41 +0200 | [diff] [blame] | 8769 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5)); |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 8770 | #elif defined(TARGET_CLONE_BACKWARDS) |
| 8771 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5)); |
| 8772 | #elif defined(TARGET_CLONE_BACKWARDS2) |
Ulrich Hecht | a4c075f | 2009-07-24 16:57:31 +0200 | [diff] [blame] | 8773 | ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4)); |
aurel32 | 0b6d3ae | 2008-09-15 07:43:43 +0000 | [diff] [blame] | 8774 | #else |
Peter Maydell | 4ce6243 | 2013-07-16 18:44:57 +0100 | [diff] [blame] | 8775 | ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4)); |
aurel32 | 0b6d3ae | 2008-09-15 07:43:43 +0000 | [diff] [blame] | 8776 | #endif |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 8777 | break; |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 8778 | #ifdef __NR_exit_group |
| 8779 | /* new thread calls */ |
| 8780 | case TARGET_NR_exit_group: |
Juan Quintela | 9788c9c | 2009-07-27 16:13:02 +0200 | [diff] [blame] | 8781 | #ifdef TARGET_GPROF |
aurel32 | 6d946cd | 2008-11-06 16:15:18 +0000 | [diff] [blame] | 8782 | _mcleanup(); |
| 8783 | #endif |
bellard | e900967 | 2005-04-26 20:42:36 +0000 | [diff] [blame] | 8784 | gdb_exit(cpu_env, arg1); |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 8785 | ret = get_errno(exit_group(arg1)); |
| 8786 | break; |
| 8787 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8788 | case TARGET_NR_setdomainname: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8789 | if (!(p = lock_user_string(arg1))) |
| 8790 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8791 | ret = get_errno(setdomainname(p, arg2)); |
| 8792 | unlock_user(p, arg1, 0); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8793 | break; |
| 8794 | case TARGET_NR_uname: |
| 8795 | /* no need to transcode because we use the linux syscall */ |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 8796 | { |
| 8797 | struct new_utsname * buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8798 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8799 | if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0)) |
| 8800 | goto efault; |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 8801 | ret = get_errno(sys_uname(buf)); |
| 8802 | if (!is_error(ret)) { |
| 8803 | /* Overrite the native machine name with whatever is being |
| 8804 | emulated. */ |
LoĂŻc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 8805 | strcpy (buf->machine, cpu_to_uname_machine(cpu_env)); |
pbrook | c593722 | 2006-05-14 11:30:38 +0000 | [diff] [blame] | 8806 | /* Allow the user to override the reported release. */ |
| 8807 | if (qemu_uname_release && *qemu_uname_release) |
| 8808 | strcpy (buf->release, qemu_uname_release); |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 8809 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8810 | unlock_user_struct(buf, arg1, 1); |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 8811 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8812 | break; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 8813 | #ifdef TARGET_I386 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8814 | case TARGET_NR_modify_ldt: |
bellard | 03acab6 | 2007-11-11 14:57:14 +0000 | [diff] [blame] | 8815 | ret = do_modify_ldt(cpu_env, arg1, arg2, arg3); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 8816 | break; |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 8817 | #if !defined(TARGET_X86_64) |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 8818 | case TARGET_NR_vm86old: |
| 8819 | goto unimplemented; |
| 8820 | case TARGET_NR_vm86: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8821 | ret = do_vm86(cpu_env, arg1, arg2); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 8822 | break; |
| 8823 | #endif |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 8824 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8825 | case TARGET_NR_adjtimex: |
| 8826 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8827 | #ifdef TARGET_NR_create_module |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8828 | case TARGET_NR_create_module: |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8829 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8830 | case TARGET_NR_init_module: |
| 8831 | case TARGET_NR_delete_module: |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8832 | #ifdef TARGET_NR_get_kernel_syms |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8833 | case TARGET_NR_get_kernel_syms: |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8834 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8835 | goto unimplemented; |
| 8836 | case TARGET_NR_quotactl: |
| 8837 | goto unimplemented; |
| 8838 | case TARGET_NR_getpgid: |
| 8839 | ret = get_errno(getpgid(arg1)); |
| 8840 | break; |
| 8841 | case TARGET_NR_fchdir: |
| 8842 | ret = get_errno(fchdir(arg1)); |
| 8843 | break; |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 8844 | #ifdef TARGET_NR_bdflush /* not on x86_64 */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8845 | case TARGET_NR_bdflush: |
| 8846 | goto unimplemented; |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 8847 | #endif |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8848 | #ifdef TARGET_NR_sysfs |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8849 | case TARGET_NR_sysfs: |
| 8850 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8851 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8852 | case TARGET_NR_personality: |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 8853 | ret = get_errno(personality(arg1)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8854 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8855 | #ifdef TARGET_NR_afs_syscall |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8856 | case TARGET_NR_afs_syscall: |
| 8857 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 8858 | #endif |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8859 | #ifdef TARGET_NR__llseek /* Not on alpha */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8860 | case TARGET_NR__llseek: |
| 8861 | { |
| 8862 | int64_t res; |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 8863 | #if !defined(__NR_llseek) |
| 8864 | res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5); |
| 8865 | if (res == -1) { |
| 8866 | ret = get_errno(res); |
| 8867 | } else { |
| 8868 | ret = 0; |
| 8869 | } |
| 8870 | #else |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8871 | ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5)); |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 8872 | #endif |
Peter Maydell | 0c1592d | 2011-02-22 13:02:26 +0000 | [diff] [blame] | 8873 | if ((ret == 0) && put_user_s64(res, arg4)) { |
| 8874 | goto efault; |
| 8875 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8876 | } |
| 8877 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 8878 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 8879 | #ifdef TARGET_NR_getdents |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8880 | case TARGET_NR_getdents: |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8881 | #ifdef __NR_getdents |
Ulrich Hecht | d83c873 | 2009-07-24 19:10:28 +0200 | [diff] [blame] | 8882 | #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8883 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8884 | struct target_dirent *target_dirp; |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 8885 | struct linux_dirent *dirp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8886 | abi_long count = arg3; |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8887 | |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 8888 | dirp = g_try_malloc(count); |
| 8889 | if (!dirp) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8890 | ret = -TARGET_ENOMEM; |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 8891 | goto fail; |
| 8892 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8893 | |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8894 | ret = get_errno(sys_getdents(arg1, dirp, count)); |
| 8895 | if (!is_error(ret)) { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 8896 | struct linux_dirent *de; |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8897 | struct target_dirent *tde; |
| 8898 | int len = ret; |
| 8899 | int reclen, treclen; |
| 8900 | int count1, tnamelen; |
| 8901 | |
| 8902 | count1 = 0; |
| 8903 | de = dirp; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8904 | if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
| 8905 | goto efault; |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8906 | tde = target_dirp; |
| 8907 | while (len > 0) { |
| 8908 | reclen = de->d_reclen; |
Dmitry V. Levin | 333858b | 2012-08-21 02:13:12 +0400 | [diff] [blame] | 8909 | tnamelen = reclen - offsetof(struct linux_dirent, d_name); |
| 8910 | assert(tnamelen >= 0); |
| 8911 | treclen = tnamelen + offsetof(struct target_dirent, d_name); |
| 8912 | assert(count1 + treclen <= count); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8913 | tde->d_reclen = tswap16(treclen); |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 8914 | tde->d_ino = tswapal(de->d_ino); |
| 8915 | tde->d_off = tswapal(de->d_off); |
Dmitry V. Levin | 333858b | 2012-08-21 02:13:12 +0400 | [diff] [blame] | 8916 | memcpy(tde->d_name, de->d_name, tnamelen); |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 8917 | de = (struct linux_dirent *)((char *)de + reclen); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8918 | len -= reclen; |
j_mayer | 1c5bf3b | 2007-04-14 12:17:59 +0000 | [diff] [blame] | 8919 | tde = (struct target_dirent *)((char *)tde + treclen); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8920 | count1 += treclen; |
| 8921 | } |
| 8922 | ret = count1; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8923 | unlock_user(target_dirp, arg2, ret); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8924 | } |
Harmandeep Kaur | 0e173b2 | 2015-10-06 21:47:12 +0530 | [diff] [blame] | 8925 | g_free(dirp); |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8926 | } |
| 8927 | #else |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8928 | { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 8929 | struct linux_dirent *dirp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 8930 | abi_long count = arg3; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 8931 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 8932 | if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
| 8933 | goto efault; |
bellard | 72f0390 | 2003-02-18 23:33:18 +0000 | [diff] [blame] | 8934 | ret = get_errno(sys_getdents(arg1, dirp, count)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8935 | if (!is_error(ret)) { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 8936 | struct linux_dirent *de; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8937 | int len = ret; |
| 8938 | int reclen; |
| 8939 | de = dirp; |
| 8940 | while (len > 0) { |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 8941 | reclen = de->d_reclen; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8942 | if (reclen > len) |
| 8943 | break; |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 8944 | de->d_reclen = tswap16(reclen); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8945 | tswapls(&de->d_ino); |
| 8946 | tswapls(&de->d_off); |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 8947 | de = (struct linux_dirent *)((char *)de + reclen); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8948 | len -= reclen; |
| 8949 | } |
| 8950 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 8951 | unlock_user(dirp, arg2, ret); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 8952 | } |
bellard | 4add45b | 2003-06-05 01:52:59 +0000 | [diff] [blame] | 8953 | #endif |
Peter Maydell | 3307e23 | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 8954 | #else |
| 8955 | /* Implement getdents in terms of getdents64 */ |
| 8956 | { |
| 8957 | struct linux_dirent64 *dirp; |
| 8958 | abi_long count = arg3; |
| 8959 | |
| 8960 | dirp = lock_user(VERIFY_WRITE, arg2, count, 0); |
| 8961 | if (!dirp) { |
| 8962 | goto efault; |
| 8963 | } |
| 8964 | ret = get_errno(sys_getdents64(arg1, dirp, count)); |
| 8965 | if (!is_error(ret)) { |
| 8966 | /* Convert the dirent64 structs to target dirent. We do this |
| 8967 | * in-place, since we can guarantee that a target_dirent is no |
| 8968 | * larger than a dirent64; however this means we have to be |
| 8969 | * careful to read everything before writing in the new format. |
| 8970 | */ |
| 8971 | struct linux_dirent64 *de; |
| 8972 | struct target_dirent *tde; |
| 8973 | int len = ret; |
| 8974 | int tlen = 0; |
| 8975 | |
| 8976 | de = dirp; |
| 8977 | tde = (struct target_dirent *)dirp; |
| 8978 | while (len > 0) { |
| 8979 | int namelen, treclen; |
| 8980 | int reclen = de->d_reclen; |
| 8981 | uint64_t ino = de->d_ino; |
| 8982 | int64_t off = de->d_off; |
| 8983 | uint8_t type = de->d_type; |
| 8984 | |
| 8985 | namelen = strlen(de->d_name); |
| 8986 | treclen = offsetof(struct target_dirent, d_name) |
| 8987 | + namelen + 2; |
| 8988 | treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long)); |
| 8989 | |
| 8990 | memmove(tde->d_name, de->d_name, namelen + 1); |
| 8991 | tde->d_ino = tswapal(ino); |
| 8992 | tde->d_off = tswapal(off); |
| 8993 | tde->d_reclen = tswap16(treclen); |
| 8994 | /* The target_dirent type is in what was formerly a padding |
| 8995 | * byte at the end of the structure: |
| 8996 | */ |
| 8997 | *(((char *)tde) + treclen - 1) = type; |
| 8998 | |
| 8999 | de = (struct linux_dirent64 *)((char *)de + reclen); |
| 9000 | tde = (struct target_dirent *)((char *)tde + treclen); |
| 9001 | len -= reclen; |
| 9002 | tlen += treclen; |
| 9003 | } |
| 9004 | ret = tlen; |
| 9005 | } |
| 9006 | unlock_user(dirp, arg2, ret); |
| 9007 | } |
| 9008 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9009 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9010 | #endif /* TARGET_NR_getdents */ |
ths | 3ae4320 | 2007-09-16 21:39:48 +0000 | [diff] [blame] | 9011 | #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9012 | case TARGET_NR_getdents64: |
| 9013 | { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9014 | struct linux_dirent64 *dirp; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 9015 | abi_long count = arg3; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9016 | if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0))) |
| 9017 | goto efault; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9018 | ret = get_errno(sys_getdents64(arg1, dirp, count)); |
| 9019 | if (!is_error(ret)) { |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9020 | struct linux_dirent64 *de; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9021 | int len = ret; |
| 9022 | int reclen; |
| 9023 | de = dirp; |
| 9024 | while (len > 0) { |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 9025 | reclen = de->d_reclen; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9026 | if (reclen > len) |
| 9027 | break; |
bellard | 8083a3e | 2003-03-24 23:12:16 +0000 | [diff] [blame] | 9028 | de->d_reclen = tswap16(reclen); |
bellard | 8582a53 | 2007-11-11 23:11:36 +0000 | [diff] [blame] | 9029 | tswap64s((uint64_t *)&de->d_ino); |
| 9030 | tswap64s((uint64_t *)&de->d_off); |
aurel32 | 6556a83 | 2008-10-13 21:08:17 +0000 | [diff] [blame] | 9031 | de = (struct linux_dirent64 *)((char *)de + reclen); |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9032 | len -= reclen; |
| 9033 | } |
| 9034 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9035 | unlock_user(dirp, arg2, ret); |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 9036 | } |
| 9037 | break; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 9038 | #endif /* TARGET_NR_getdents64 */ |
Laurent Vivier | 9468a5d | 2013-01-10 22:30:50 +0100 | [diff] [blame] | 9039 | #if defined(TARGET_NR__newselect) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9040 | case TARGET_NR__newselect: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9041 | ret = do_select(arg1, arg2, arg3, arg4, arg5); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9042 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9043 | #endif |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9044 | #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) |
| 9045 | # ifdef TARGET_NR_poll |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9046 | case TARGET_NR_poll: |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9047 | # endif |
| 9048 | # ifdef TARGET_NR_ppoll |
| 9049 | case TARGET_NR_ppoll: |
| 9050 | # endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9051 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9052 | struct target_pollfd *target_pfd; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9053 | unsigned int nfds = arg2; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9054 | struct pollfd *pfd; |
bellard | 7854b05 | 2003-03-29 17:22:23 +0000 | [diff] [blame] | 9055 | unsigned int i; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9056 | |
Laurent Vivier | 3e24bb3 | 2015-10-06 01:20:48 +0200 | [diff] [blame] | 9057 | pfd = NULL; |
| 9058 | target_pfd = NULL; |
| 9059 | if (nfds) { |
| 9060 | target_pfd = lock_user(VERIFY_WRITE, arg1, |
| 9061 | sizeof(struct target_pollfd) * nfds, 1); |
| 9062 | if (!target_pfd) { |
| 9063 | goto efault; |
| 9064 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9065 | |
Laurent Vivier | 3e24bb3 | 2015-10-06 01:20:48 +0200 | [diff] [blame] | 9066 | pfd = alloca(sizeof(struct pollfd) * nfds); |
| 9067 | for (i = 0; i < nfds; i++) { |
| 9068 | pfd[i].fd = tswap32(target_pfd[i].fd); |
| 9069 | pfd[i].events = tswap16(target_pfd[i].events); |
| 9070 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9071 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9072 | |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 9073 | switch (num) { |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9074 | # ifdef TARGET_NR_ppoll |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 9075 | case TARGET_NR_ppoll: |
| 9076 | { |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9077 | struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; |
| 9078 | target_sigset_t *target_set; |
| 9079 | sigset_t _set, *set = &_set; |
| 9080 | |
| 9081 | if (arg3) { |
| 9082 | if (target_to_host_timespec(timeout_ts, arg3)) { |
| 9083 | unlock_user(target_pfd, arg1, 0); |
| 9084 | goto efault; |
| 9085 | } |
| 9086 | } else { |
| 9087 | timeout_ts = NULL; |
| 9088 | } |
| 9089 | |
| 9090 | if (arg4) { |
| 9091 | target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); |
| 9092 | if (!target_set) { |
| 9093 | unlock_user(target_pfd, arg1, 0); |
| 9094 | goto efault; |
| 9095 | } |
| 9096 | target_to_host_sigset(set, target_set); |
| 9097 | } else { |
| 9098 | set = NULL; |
| 9099 | } |
| 9100 | |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 9101 | ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, |
| 9102 | set, SIGSET_T_SIZE)); |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9103 | |
| 9104 | if (!is_error(ret) && arg3) { |
| 9105 | host_to_target_timespec(arg3, timeout_ts); |
| 9106 | } |
| 9107 | if (arg4) { |
| 9108 | unlock_user(target_set, arg4, 0); |
| 9109 | } |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 9110 | break; |
| 9111 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9112 | # endif |
Peter Maydell | a613023 | 2016-06-06 19:58:10 +0100 | [diff] [blame] | 9113 | # ifdef TARGET_NR_poll |
| 9114 | case TARGET_NR_poll: |
| 9115 | { |
| 9116 | struct timespec ts, *pts; |
| 9117 | |
| 9118 | if (arg3 >= 0) { |
| 9119 | /* Convert ms to secs, ns */ |
| 9120 | ts.tv_sec = arg3 / 1000; |
| 9121 | ts.tv_nsec = (arg3 % 1000) * 1000000LL; |
| 9122 | pts = &ts; |
| 9123 | } else { |
| 9124 | /* -ve poll() timeout means "infinite" */ |
| 9125 | pts = NULL; |
| 9126 | } |
| 9127 | ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); |
| 9128 | break; |
| 9129 | } |
| 9130 | # endif |
| 9131 | default: |
| 9132 | g_assert_not_reached(); |
| 9133 | } |
Mike Frysinger | d8035d4 | 2011-02-07 01:05:51 -0500 | [diff] [blame] | 9134 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9135 | if (!is_error(ret)) { |
| 9136 | for(i = 0; i < nfds; i++) { |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9137 | target_pfd[i].revents = tswap16(pfd[i].revents); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9138 | } |
| 9139 | } |
Peter Maydell | 30cb4cd | 2011-02-25 10:27:40 +0000 | [diff] [blame] | 9140 | unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9141 | } |
| 9142 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9143 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9144 | case TARGET_NR_flock: |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 9145 | /* NOTE: the flock constant seems to be the same for every |
| 9146 | Linux platform */ |
Peter Maydell | 2a84598 | 2016-06-06 19:58:07 +0100 | [diff] [blame] | 9147 | ret = get_errno(safe_flock(arg1, arg2)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9148 | break; |
| 9149 | case TARGET_NR_readv: |
| 9150 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 9151 | struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); |
| 9152 | if (vec != NULL) { |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 9153 | ret = get_errno(safe_readv(arg1, vec, arg3)); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 9154 | unlock_iovec(vec, arg2, arg3, 1); |
| 9155 | } else { |
| 9156 | ret = -host_to_target_errno(errno); |
| 9157 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9158 | } |
| 9159 | break; |
| 9160 | case TARGET_NR_writev: |
| 9161 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 9162 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 9163 | if (vec != NULL) { |
Peter Maydell | 918c03e | 2016-06-06 19:58:02 +0100 | [diff] [blame] | 9164 | ret = get_errno(safe_writev(arg1, vec, arg3)); |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 9165 | unlock_iovec(vec, arg2, arg3, 0); |
| 9166 | } else { |
| 9167 | ret = -host_to_target_errno(errno); |
| 9168 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9169 | } |
| 9170 | break; |
| 9171 | case TARGET_NR_getsid: |
| 9172 | ret = get_errno(getsid(arg1)); |
| 9173 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9174 | #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9175 | case TARGET_NR_fdatasync: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9176 | ret = get_errno(fdatasync(arg1)); |
| 9177 | break; |
j_mayer | 7a3148a | 2007-04-05 07:13:51 +0000 | [diff] [blame] | 9178 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9179 | #ifdef TARGET_NR__sysctl |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9180 | case TARGET_NR__sysctl: |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 9181 | /* We don't implement this, but ENOTDIR is always a safe |
bellard | 29e619b | 2004-09-13 21:41:04 +0000 | [diff] [blame] | 9182 | return value. */ |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 9183 | ret = -TARGET_ENOTDIR; |
| 9184 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9185 | #endif |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 9186 | case TARGET_NR_sched_getaffinity: |
| 9187 | { |
| 9188 | unsigned int mask_size; |
| 9189 | unsigned long *mask; |
| 9190 | |
| 9191 | /* |
| 9192 | * sched_getaffinity needs multiples of ulong, so need to take |
| 9193 | * care of mismatches between target ulong and host ulong sizes. |
| 9194 | */ |
| 9195 | if (arg2 & (sizeof(abi_ulong) - 1)) { |
| 9196 | ret = -TARGET_EINVAL; |
| 9197 | break; |
| 9198 | } |
| 9199 | mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); |
| 9200 | |
| 9201 | mask = alloca(mask_size); |
| 9202 | ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask)); |
| 9203 | |
| 9204 | if (!is_error(ret)) { |
Peter Maydell | be3bd28 | 2014-05-15 14:40:23 +0100 | [diff] [blame] | 9205 | if (ret > arg2) { |
| 9206 | /* More data returned than the caller's buffer will fit. |
| 9207 | * This only happens if sizeof(abi_long) < sizeof(long) |
| 9208 | * and the caller passed us a buffer holding an odd number |
| 9209 | * of abi_longs. If the host kernel is actually using the |
| 9210 | * extra 4 bytes then fail EINVAL; otherwise we can just |
| 9211 | * ignore them and only copy the interesting part. |
| 9212 | */ |
| 9213 | int numcpus = sysconf(_SC_NPROCESSORS_CONF); |
| 9214 | if (numcpus > arg2 * 8) { |
| 9215 | ret = -TARGET_EINVAL; |
| 9216 | break; |
| 9217 | } |
| 9218 | ret = arg2; |
| 9219 | } |
| 9220 | |
Mike McCormack | cd18f05 | 2011-04-18 14:43:36 +0900 | [diff] [blame] | 9221 | if (copy_to_user(arg3, mask, ret)) { |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 9222 | goto efault; |
| 9223 | } |
Mike Frysinger | 737de1d | 2011-02-07 01:05:55 -0500 | [diff] [blame] | 9224 | } |
| 9225 | } |
| 9226 | break; |
| 9227 | case TARGET_NR_sched_setaffinity: |
| 9228 | { |
| 9229 | unsigned int mask_size; |
| 9230 | unsigned long *mask; |
| 9231 | |
| 9232 | /* |
| 9233 | * sched_setaffinity needs multiples of ulong, so need to take |
| 9234 | * care of mismatches between target ulong and host ulong sizes. |
| 9235 | */ |
| 9236 | if (arg2 & (sizeof(abi_ulong) - 1)) { |
| 9237 | ret = -TARGET_EINVAL; |
| 9238 | break; |
| 9239 | } |
| 9240 | mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); |
| 9241 | |
| 9242 | mask = alloca(mask_size); |
| 9243 | if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) { |
| 9244 | goto efault; |
| 9245 | } |
| 9246 | memcpy(mask, p, arg2); |
| 9247 | unlock_user_struct(p, arg2, 0); |
| 9248 | |
| 9249 | ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask)); |
| 9250 | } |
| 9251 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9252 | case TARGET_NR_sched_setparam: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9253 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9254 | struct sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9255 | struct sched_param schp; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9256 | |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 9257 | if (arg2 == 0) { |
| 9258 | return -TARGET_EINVAL; |
| 9259 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9260 | if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) |
| 9261 | goto efault; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9262 | schp.sched_priority = tswap32(target_schp->sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9263 | unlock_user_struct(target_schp, arg2, 0); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9264 | ret = get_errno(sched_setparam(arg1, &schp)); |
| 9265 | } |
| 9266 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9267 | case TARGET_NR_sched_getparam: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9268 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9269 | struct sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9270 | struct sched_param schp; |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 9271 | |
| 9272 | if (arg2 == 0) { |
| 9273 | return -TARGET_EINVAL; |
| 9274 | } |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9275 | ret = get_errno(sched_getparam(arg1, &schp)); |
| 9276 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9277 | if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) |
| 9278 | goto efault; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9279 | target_schp->sched_priority = tswap32(schp.sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9280 | unlock_user_struct(target_schp, arg2, 1); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9281 | } |
| 9282 | } |
| 9283 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9284 | case TARGET_NR_sched_setscheduler: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9285 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9286 | struct sched_param *target_schp; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9287 | struct sched_param schp; |
Tom Musta | a1d5c5b | 2014-08-12 13:53:38 -0500 | [diff] [blame] | 9288 | if (arg3 == 0) { |
| 9289 | return -TARGET_EINVAL; |
| 9290 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9291 | if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) |
| 9292 | goto efault; |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9293 | schp.sched_priority = tswap32(target_schp->sched_priority); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9294 | unlock_user_struct(target_schp, arg3, 0); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9295 | ret = get_errno(sched_setscheduler(arg1, arg2, &schp)); |
| 9296 | } |
| 9297 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9298 | case TARGET_NR_sched_getscheduler: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9299 | ret = get_errno(sched_getscheduler(arg1)); |
| 9300 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9301 | case TARGET_NR_sched_yield: |
| 9302 | ret = get_errno(sched_yield()); |
| 9303 | break; |
| 9304 | case TARGET_NR_sched_get_priority_max: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9305 | ret = get_errno(sched_get_priority_max(arg1)); |
| 9306 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9307 | case TARGET_NR_sched_get_priority_min: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9308 | ret = get_errno(sched_get_priority_min(arg1)); |
| 9309 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9310 | case TARGET_NR_sched_rr_get_interval: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9311 | { |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9312 | struct timespec ts; |
| 9313 | ret = get_errno(sched_rr_get_interval(arg1, &ts)); |
| 9314 | if (!is_error(ret)) { |
Tom Musta | d4290c4 | 2014-08-12 13:53:39 -0500 | [diff] [blame] | 9315 | ret = host_to_target_timespec(arg2, &ts); |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9316 | } |
| 9317 | } |
| 9318 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9319 | case TARGET_NR_nanosleep: |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 9320 | { |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 9321 | struct timespec req, rem; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9322 | target_to_host_timespec(&req, arg1); |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 9323 | ret = get_errno(safe_nanosleep(&req, &rem)); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9324 | if (is_error(ret) && arg2) { |
| 9325 | host_to_target_timespec(arg2, &rem); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 9326 | } |
| 9327 | } |
| 9328 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9329 | #ifdef TARGET_NR_query_module |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9330 | case TARGET_NR_query_module: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9331 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9332 | #endif |
| 9333 | #ifdef TARGET_NR_nfsservctl |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9334 | case TARGET_NR_nfsservctl: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9335 | goto unimplemented; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 9336 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9337 | case TARGET_NR_prctl: |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 9338 | switch (arg1) { |
| 9339 | case PR_GET_PDEATHSIG: |
| 9340 | { |
| 9341 | int deathsig; |
| 9342 | ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5)); |
| 9343 | if (!is_error(ret) && arg2 |
| 9344 | && put_user_ual(deathsig, arg2)) { |
| 9345 | goto efault; |
ths | e557448 | 2007-02-11 20:03:13 +0000 | [diff] [blame] | 9346 | } |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 9347 | break; |
| 9348 | } |
Peter Maydell | db9526b | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 9349 | #ifdef PR_GET_NAME |
| 9350 | case PR_GET_NAME: |
| 9351 | { |
| 9352 | void *name = lock_user(VERIFY_WRITE, arg2, 16, 1); |
| 9353 | if (!name) { |
| 9354 | goto efault; |
| 9355 | } |
| 9356 | ret = get_errno(prctl(arg1, (unsigned long)name, |
| 9357 | arg3, arg4, arg5)); |
| 9358 | unlock_user(name, arg2, 16); |
| 9359 | break; |
| 9360 | } |
| 9361 | case PR_SET_NAME: |
| 9362 | { |
| 9363 | void *name = lock_user(VERIFY_READ, arg2, 16, 1); |
| 9364 | if (!name) { |
| 9365 | goto efault; |
| 9366 | } |
| 9367 | ret = get_errno(prctl(arg1, (unsigned long)name, |
| 9368 | arg3, arg4, arg5)); |
| 9369 | unlock_user(name, arg2, 0); |
| 9370 | break; |
| 9371 | } |
| 9372 | #endif |
Peter Maydell | 1e6722f | 2012-02-03 14:48:03 +0000 | [diff] [blame] | 9373 | default: |
| 9374 | /* Most prctl options have no pointer arguments */ |
| 9375 | ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5)); |
| 9376 | break; |
| 9377 | } |
ths | 39b9aae | 2007-02-11 18:36:44 +0000 | [diff] [blame] | 9378 | break; |
bellard | d2fd1af | 2007-11-14 18:08:56 +0000 | [diff] [blame] | 9379 | #ifdef TARGET_NR_arch_prctl |
| 9380 | case TARGET_NR_arch_prctl: |
| 9381 | #if defined(TARGET_I386) && !defined(TARGET_ABI32) |
| 9382 | ret = do_arch_prctl(cpu_env, arg1, arg2); |
| 9383 | break; |
| 9384 | #else |
| 9385 | goto unimplemented; |
| 9386 | #endif |
| 9387 | #endif |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 9388 | #ifdef TARGET_NR_pread64 |
| 9389 | case TARGET_NR_pread64: |
Alexander Graf | ae017a5 | 2012-09-29 15:32:39 +0000 | [diff] [blame] | 9390 | if (regpairs_aligned(cpu_env)) { |
| 9391 | arg4 = arg5; |
| 9392 | arg5 = arg6; |
| 9393 | } |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 9394 | if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) |
| 9395 | goto efault; |
| 9396 | ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5))); |
| 9397 | unlock_user(p, arg2, ret); |
| 9398 | break; |
| 9399 | case TARGET_NR_pwrite64: |
Alexander Graf | ae017a5 | 2012-09-29 15:32:39 +0000 | [diff] [blame] | 9400 | if (regpairs_aligned(cpu_env)) { |
| 9401 | arg4 = arg5; |
| 9402 | arg5 = arg6; |
| 9403 | } |
aurel32 | f2c7ba1 | 2008-03-28 22:32:06 +0000 | [diff] [blame] | 9404 | if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) |
| 9405 | goto efault; |
| 9406 | ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5))); |
| 9407 | unlock_user(p, arg2, 0); |
| 9408 | break; |
| 9409 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9410 | case TARGET_NR_getcwd: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9411 | if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0))) |
| 9412 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9413 | ret = get_errno(sys_getcwd1(p, arg2)); |
| 9414 | unlock_user(p, arg1, ret); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9415 | break; |
| 9416 | case TARGET_NR_capget: |
| 9417 | case TARGET_NR_capset: |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 9418 | { |
| 9419 | struct target_user_cap_header *target_header; |
| 9420 | struct target_user_cap_data *target_data = NULL; |
| 9421 | struct __user_cap_header_struct header; |
| 9422 | struct __user_cap_data_struct data[2]; |
| 9423 | struct __user_cap_data_struct *dataptr = NULL; |
| 9424 | int i, target_datalen; |
| 9425 | int data_items = 1; |
| 9426 | |
| 9427 | if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) { |
| 9428 | goto efault; |
| 9429 | } |
| 9430 | header.version = tswap32(target_header->version); |
| 9431 | header.pid = tswap32(target_header->pid); |
| 9432 | |
Peter Maydell | ec86487 | 2014-03-19 16:07:30 +0000 | [diff] [blame] | 9433 | if (header.version != _LINUX_CAPABILITY_VERSION) { |
Peter Maydell | e0eb210 | 2014-03-17 12:15:35 +0000 | [diff] [blame] | 9434 | /* Version 2 and up takes pointer to two user_data structs */ |
| 9435 | data_items = 2; |
| 9436 | } |
| 9437 | |
| 9438 | target_datalen = sizeof(*target_data) * data_items; |
| 9439 | |
| 9440 | if (arg2) { |
| 9441 | if (num == TARGET_NR_capget) { |
| 9442 | target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0); |
| 9443 | } else { |
| 9444 | target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1); |
| 9445 | } |
| 9446 | if (!target_data) { |
| 9447 | unlock_user_struct(target_header, arg1, 0); |
| 9448 | goto efault; |
| 9449 | } |
| 9450 | |
| 9451 | if (num == TARGET_NR_capset) { |
| 9452 | for (i = 0; i < data_items; i++) { |
| 9453 | data[i].effective = tswap32(target_data[i].effective); |
| 9454 | data[i].permitted = tswap32(target_data[i].permitted); |
| 9455 | data[i].inheritable = tswap32(target_data[i].inheritable); |
| 9456 | } |
| 9457 | } |
| 9458 | |
| 9459 | dataptr = data; |
| 9460 | } |
| 9461 | |
| 9462 | if (num == TARGET_NR_capget) { |
| 9463 | ret = get_errno(capget(&header, dataptr)); |
| 9464 | } else { |
| 9465 | ret = get_errno(capset(&header, dataptr)); |
| 9466 | } |
| 9467 | |
| 9468 | /* The kernel always updates version for both capget and capset */ |
| 9469 | target_header->version = tswap32(header.version); |
| 9470 | unlock_user_struct(target_header, arg1, 1); |
| 9471 | |
| 9472 | if (arg2) { |
| 9473 | if (num == TARGET_NR_capget) { |
| 9474 | for (i = 0; i < data_items; i++) { |
| 9475 | target_data[i].effective = tswap32(data[i].effective); |
| 9476 | target_data[i].permitted = tswap32(data[i].permitted); |
| 9477 | target_data[i].inheritable = tswap32(data[i].inheritable); |
| 9478 | } |
| 9479 | unlock_user(target_data, arg2, target_datalen); |
| 9480 | } else { |
| 9481 | unlock_user(target_data, arg2, 0); |
| 9482 | } |
| 9483 | } |
| 9484 | break; |
| 9485 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9486 | case TARGET_NR_sigaltstack: |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 9487 | ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env)); |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 9488 | break; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 9489 | |
| 9490 | #ifdef CONFIG_SENDFILE |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9491 | case TARGET_NR_sendfile: |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 9492 | { |
| 9493 | off_t *offp = NULL; |
| 9494 | off_t off; |
| 9495 | if (arg3) { |
| 9496 | ret = get_user_sal(off, arg3); |
| 9497 | if (is_error(ret)) { |
| 9498 | break; |
| 9499 | } |
| 9500 | offp = &off; |
| 9501 | } |
| 9502 | ret = get_errno(sendfile(arg1, arg2, offp, arg4)); |
| 9503 | if (!is_error(ret) && arg3) { |
| 9504 | abi_long ret2 = put_user_sal(off, arg3); |
| 9505 | if (is_error(ret2)) { |
| 9506 | ret = ret2; |
| 9507 | } |
| 9508 | } |
| 9509 | break; |
| 9510 | } |
| 9511 | #ifdef TARGET_NR_sendfile64 |
| 9512 | case TARGET_NR_sendfile64: |
| 9513 | { |
| 9514 | off_t *offp = NULL; |
| 9515 | off_t off; |
| 9516 | if (arg3) { |
| 9517 | ret = get_user_s64(off, arg3); |
| 9518 | if (is_error(ret)) { |
| 9519 | break; |
| 9520 | } |
| 9521 | offp = &off; |
| 9522 | } |
| 9523 | ret = get_errno(sendfile(arg1, arg2, offp, arg4)); |
| 9524 | if (!is_error(ret) && arg3) { |
| 9525 | abi_long ret2 = put_user_s64(off, arg3); |
| 9526 | if (is_error(ret2)) { |
| 9527 | ret = ret2; |
| 9528 | } |
| 9529 | } |
| 9530 | break; |
| 9531 | } |
| 9532 | #endif |
| 9533 | #else |
| 9534 | case TARGET_NR_sendfile: |
Peter Maydell | 7edd2cf | 2013-04-21 13:30:03 +0100 | [diff] [blame] | 9535 | #ifdef TARGET_NR_sendfile64 |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 9536 | case TARGET_NR_sendfile64: |
| 9537 | #endif |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9538 | goto unimplemented; |
Peter Maydell | a8fd1ab | 2013-02-08 07:31:55 +0000 | [diff] [blame] | 9539 | #endif |
| 9540 | |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9541 | #ifdef TARGET_NR_getpmsg |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9542 | case TARGET_NR_getpmsg: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9543 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9544 | #endif |
| 9545 | #ifdef TARGET_NR_putpmsg |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9546 | case TARGET_NR_putpmsg: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 9547 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9548 | #endif |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 9549 | #ifdef TARGET_NR_vfork |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9550 | case TARGET_NR_vfork: |
pbrook | d865bab | 2008-06-07 22:12:17 +0000 | [diff] [blame] | 9551 | ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, |
| 9552 | 0, 0, 0, 0)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9553 | break; |
bellard | 048f6b4 | 2005-11-26 18:47:20 +0000 | [diff] [blame] | 9554 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9555 | #ifdef TARGET_NR_ugetrlimit |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9556 | case TARGET_NR_ugetrlimit: |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 9557 | { |
| 9558 | struct rlimit rlim; |
Wesley W. Terpstra | e22b701 | 2011-07-12 14:42:00 +0300 | [diff] [blame] | 9559 | int resource = target_to_host_resource(arg1); |
| 9560 | ret = get_errno(getrlimit(resource, &rlim)); |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 9561 | if (!is_error(ret)) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9562 | struct target_rlimit *target_rlim; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9563 | if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) |
| 9564 | goto efault; |
takasi-y@ops.dti.ne.jp | 81bbe90 | 2010-04-12 04:07:35 +0900 | [diff] [blame] | 9565 | target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); |
| 9566 | target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9567 | unlock_user_struct(target_rlim, arg2, 1); |
bellard | 728584b | 2003-04-29 20:43:36 +0000 | [diff] [blame] | 9568 | } |
| 9569 | break; |
| 9570 | } |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 9571 | #endif |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9572 | #ifdef TARGET_NR_truncate64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9573 | case TARGET_NR_truncate64: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9574 | if (!(p = lock_user_string(arg1))) |
| 9575 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9576 | ret = target_truncate64(cpu_env, p, arg2, arg3, arg4); |
| 9577 | unlock_user(p, arg1, 0); |
bellard | 667f38b | 2005-07-23 14:46:27 +0000 | [diff] [blame] | 9578 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9579 | #endif |
| 9580 | #ifdef TARGET_NR_ftruncate64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9581 | case TARGET_NR_ftruncate64: |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 9582 | ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4); |
bellard | 667f38b | 2005-07-23 14:46:27 +0000 | [diff] [blame] | 9583 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9584 | #endif |
| 9585 | #ifdef TARGET_NR_stat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9586 | case TARGET_NR_stat64: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9587 | if (!(p = lock_user_string(arg1))) |
| 9588 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9589 | ret = get_errno(stat(path(p), &st)); |
| 9590 | unlock_user(p, arg1, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 9591 | if (!is_error(ret)) |
| 9592 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
| 9593 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9594 | #endif |
| 9595 | #ifdef TARGET_NR_lstat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9596 | case TARGET_NR_lstat64: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9597 | if (!(p = lock_user_string(arg1))) |
| 9598 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9599 | ret = get_errno(lstat(path(p), &st)); |
| 9600 | unlock_user(p, arg1, 0); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 9601 | if (!is_error(ret)) |
| 9602 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
| 9603 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9604 | #endif |
| 9605 | #ifdef TARGET_NR_fstat64 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9606 | case TARGET_NR_fstat64: |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 9607 | ret = get_errno(fstat(arg1, &st)); |
| 9608 | if (!is_error(ret)) |
| 9609 | ret = host_to_target_stat64(cpu_env, arg2, &st); |
| 9610 | break; |
bellard | ec86b0f | 2003-04-11 00:15:04 +0000 | [diff] [blame] | 9611 | #endif |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9612 | #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 9613 | #ifdef TARGET_NR_fstatat64 |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 9614 | case TARGET_NR_fstatat64: |
aurel32 | 9d33b76 | 2009-04-08 23:07:05 +0000 | [diff] [blame] | 9615 | #endif |
| 9616 | #ifdef TARGET_NR_newfstatat |
| 9617 | case TARGET_NR_newfstatat: |
| 9618 | #endif |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 9619 | if (!(p = lock_user_string(arg2))) |
| 9620 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9621 | ret = get_errno(fstatat(arg1, path(p), &st, arg4)); |
balrog | 6a24a77 | 2008-09-20 02:23:36 +0000 | [diff] [blame] | 9622 | if (!is_error(ret)) |
| 9623 | ret = host_to_target_stat64(cpu_env, arg3, &st); |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 9624 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9625 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9626 | #ifdef TARGET_NR_lchown |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9627 | case TARGET_NR_lchown: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9628 | if (!(p = lock_user_string(arg1))) |
| 9629 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9630 | ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3))); |
| 9631 | unlock_user(p, arg1, 0); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9632 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9633 | #endif |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9634 | #ifdef TARGET_NR_getuid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9635 | case TARGET_NR_getuid: |
| 9636 | ret = get_errno(high2lowuid(getuid())); |
| 9637 | break; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9638 | #endif |
| 9639 | #ifdef TARGET_NR_getgid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9640 | case TARGET_NR_getgid: |
| 9641 | ret = get_errno(high2lowgid(getgid())); |
| 9642 | break; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9643 | #endif |
| 9644 | #ifdef TARGET_NR_geteuid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9645 | case TARGET_NR_geteuid: |
| 9646 | ret = get_errno(high2lowuid(geteuid())); |
| 9647 | break; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9648 | #endif |
| 9649 | #ifdef TARGET_NR_getegid |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9650 | case TARGET_NR_getegid: |
| 9651 | ret = get_errno(high2lowgid(getegid())); |
| 9652 | break; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9653 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9654 | case TARGET_NR_setreuid: |
| 9655 | ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2))); |
| 9656 | break; |
| 9657 | case TARGET_NR_setregid: |
| 9658 | ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2))); |
| 9659 | break; |
| 9660 | case TARGET_NR_getgroups: |
| 9661 | { |
| 9662 | int gidsetsize = arg1; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9663 | target_id *target_grouplist; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9664 | gid_t *grouplist; |
| 9665 | int i; |
| 9666 | |
| 9667 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
| 9668 | ret = get_errno(getgroups(gidsetsize, grouplist)); |
balrog | cb3bc23 | 2008-09-20 02:08:13 +0000 | [diff] [blame] | 9669 | if (gidsetsize == 0) |
| 9670 | break; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9671 | if (!is_error(ret)) { |
Andreas Schwab | 03903ff | 2013-04-09 05:41:33 +0000 | [diff] [blame] | 9672 | target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9673 | if (!target_grouplist) |
| 9674 | goto efault; |
balrog | a2155fc | 2008-09-20 02:12:08 +0000 | [diff] [blame] | 9675 | for(i = 0;i < ret; i++) |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9676 | target_grouplist[i] = tswapid(high2lowgid(grouplist[i])); |
Andreas Schwab | 03903ff | 2013-04-09 05:41:33 +0000 | [diff] [blame] | 9677 | unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id)); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9678 | } |
| 9679 | } |
| 9680 | break; |
| 9681 | case TARGET_NR_setgroups: |
| 9682 | { |
| 9683 | int gidsetsize = arg1; |
Riku Voipio | 0c866a7 | 2011-04-18 15:23:06 +0300 | [diff] [blame] | 9684 | target_id *target_grouplist; |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 9685 | gid_t *grouplist = NULL; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9686 | int i; |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 9687 | if (gidsetsize) { |
| 9688 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
Andreas Schwab | 03903ff | 2013-04-09 05:41:33 +0000 | [diff] [blame] | 9689 | target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1); |
Dillon Amburgey | f2b79ce | 2013-02-02 18:04:48 -0500 | [diff] [blame] | 9690 | if (!target_grouplist) { |
| 9691 | ret = -TARGET_EFAULT; |
| 9692 | goto fail; |
| 9693 | } |
| 9694 | for (i = 0; i < gidsetsize; i++) { |
| 9695 | grouplist[i] = low2highgid(tswapid(target_grouplist[i])); |
| 9696 | } |
| 9697 | unlock_user(target_grouplist, arg2, 0); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9698 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9699 | ret = get_errno(setgroups(gidsetsize, grouplist)); |
| 9700 | } |
| 9701 | break; |
| 9702 | case TARGET_NR_fchown: |
| 9703 | ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); |
| 9704 | break; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9705 | #if defined(TARGET_NR_fchownat) |
ths | ccfa72b | 2007-09-24 09:23:34 +0000 | [diff] [blame] | 9706 | case TARGET_NR_fchownat: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9707 | if (!(p = lock_user_string(arg2))) |
| 9708 | goto efault; |
Peter Maydell | c0d472b | 2013-06-12 16:20:21 +0100 | [diff] [blame] | 9709 | ret = get_errno(fchownat(arg1, p, low2highuid(arg3), |
| 9710 | low2highgid(arg4), arg5)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9711 | unlock_user(p, arg2, 0); |
ths | ccfa72b | 2007-09-24 09:23:34 +0000 | [diff] [blame] | 9712 | break; |
| 9713 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9714 | #ifdef TARGET_NR_setresuid |
| 9715 | case TARGET_NR_setresuid: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 9716 | ret = get_errno(sys_setresuid(low2highuid(arg1), |
| 9717 | low2highuid(arg2), |
| 9718 | low2highuid(arg3))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9719 | break; |
| 9720 | #endif |
| 9721 | #ifdef TARGET_NR_getresuid |
| 9722 | case TARGET_NR_getresuid: |
| 9723 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9724 | uid_t ruid, euid, suid; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9725 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 9726 | if (!is_error(ret)) { |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 9727 | if (put_user_id(high2lowuid(ruid), arg1) |
| 9728 | || put_user_id(high2lowuid(euid), arg2) |
| 9729 | || put_user_id(high2lowuid(suid), arg3)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 9730 | goto efault; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9731 | } |
| 9732 | } |
| 9733 | break; |
| 9734 | #endif |
| 9735 | #ifdef TARGET_NR_getresgid |
| 9736 | case TARGET_NR_setresgid: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 9737 | ret = get_errno(sys_setresgid(low2highgid(arg1), |
| 9738 | low2highgid(arg2), |
| 9739 | low2highgid(arg3))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9740 | break; |
| 9741 | #endif |
| 9742 | #ifdef TARGET_NR_getresgid |
| 9743 | case TARGET_NR_getresgid: |
| 9744 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9745 | gid_t rgid, egid, sgid; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9746 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 9747 | if (!is_error(ret)) { |
Peter Maydell | 76ca310 | 2014-03-02 19:36:41 +0000 | [diff] [blame] | 9748 | if (put_user_id(high2lowgid(rgid), arg1) |
| 9749 | || put_user_id(high2lowgid(egid), arg2) |
| 9750 | || put_user_id(high2lowgid(sgid), arg3)) |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 9751 | goto efault; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9752 | } |
| 9753 | } |
| 9754 | break; |
| 9755 | #endif |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9756 | #ifdef TARGET_NR_chown |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9757 | case TARGET_NR_chown: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9758 | if (!(p = lock_user_string(arg1))) |
| 9759 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9760 | ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3))); |
| 9761 | unlock_user(p, arg1, 0); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9762 | break; |
Chen Gang | 704eff6 | 2015-08-21 05:37:33 +0800 | [diff] [blame] | 9763 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9764 | case TARGET_NR_setuid: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 9765 | ret = get_errno(sys_setuid(low2highuid(arg1))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9766 | break; |
| 9767 | case TARGET_NR_setgid: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 9768 | ret = get_errno(sys_setgid(low2highgid(arg1))); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9769 | break; |
| 9770 | case TARGET_NR_setfsuid: |
| 9771 | ret = get_errno(setfsuid(arg1)); |
| 9772 | break; |
| 9773 | case TARGET_NR_setfsgid: |
| 9774 | ret = get_errno(setfsgid(arg1)); |
| 9775 | break; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 9776 | |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9777 | #ifdef TARGET_NR_lchown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9778 | case TARGET_NR_lchown32: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 9779 | if (!(p = lock_user_string(arg1))) |
| 9780 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 9781 | ret = get_errno(lchown(p, arg2, arg3)); |
| 9782 | unlock_user(p, arg1, 0); |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9783 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9784 | #endif |
| 9785 | #ifdef TARGET_NR_getuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9786 | case TARGET_NR_getuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9787 | ret = get_errno(getuid()); |
| 9788 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9789 | #endif |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 9790 | |
| 9791 | #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA) |
| 9792 | /* Alpha specific */ |
| 9793 | case TARGET_NR_getxuid: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9794 | { |
| 9795 | uid_t euid; |
| 9796 | euid=geteuid(); |
| 9797 | ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid; |
| 9798 | } |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 9799 | ret = get_errno(getuid()); |
| 9800 | break; |
| 9801 | #endif |
| 9802 | #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA) |
| 9803 | /* Alpha specific */ |
| 9804 | case TARGET_NR_getxgid: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9805 | { |
| 9806 | uid_t egid; |
| 9807 | egid=getegid(); |
| 9808 | ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid; |
| 9809 | } |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 9810 | ret = get_errno(getgid()); |
| 9811 | break; |
| 9812 | #endif |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9813 | #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA) |
| 9814 | /* Alpha specific */ |
| 9815 | case TARGET_NR_osf_getsysinfo: |
| 9816 | ret = -TARGET_EOPNOTSUPP; |
| 9817 | switch (arg1) { |
| 9818 | case TARGET_GSI_IEEE_FP_CONTROL: |
| 9819 | { |
| 9820 | uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env); |
| 9821 | |
| 9822 | /* Copied from linux ieee_fpcr_to_swcr. */ |
| 9823 | swcr = (fpcr >> 35) & SWCR_STATUS_MASK; |
| 9824 | swcr |= (fpcr >> 36) & SWCR_MAP_DMZ; |
| 9825 | swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV |
| 9826 | | SWCR_TRAP_ENABLE_DZE |
| 9827 | | SWCR_TRAP_ENABLE_OVF); |
| 9828 | swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF |
| 9829 | | SWCR_TRAP_ENABLE_INE); |
| 9830 | swcr |= (fpcr >> 47) & SWCR_MAP_UMZ; |
| 9831 | swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO; |
| 9832 | |
| 9833 | if (put_user_u64 (swcr, arg2)) |
| 9834 | goto efault; |
| 9835 | ret = 0; |
| 9836 | } |
| 9837 | break; |
| 9838 | |
| 9839 | /* case GSI_IEEE_STATE_AT_SIGNAL: |
| 9840 | -- Not implemented in linux kernel. |
| 9841 | case GSI_UACPROC: |
| 9842 | -- Retrieves current unaligned access state; not much used. |
| 9843 | case GSI_PROC_TYPE: |
| 9844 | -- Retrieves implver information; surely not used. |
| 9845 | case GSI_GET_HWRPB: |
| 9846 | -- Grabs a copy of the HWRPB; surely not used. |
| 9847 | */ |
| 9848 | } |
| 9849 | break; |
| 9850 | #endif |
| 9851 | #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA) |
| 9852 | /* Alpha specific */ |
| 9853 | case TARGET_NR_osf_setsysinfo: |
| 9854 | ret = -TARGET_EOPNOTSUPP; |
| 9855 | switch (arg1) { |
| 9856 | case TARGET_SSI_IEEE_FP_CONTROL: |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9857 | { |
| 9858 | uint64_t swcr, fpcr, orig_fpcr; |
| 9859 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 9860 | if (get_user_u64 (swcr, arg2)) { |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9861 | goto efault; |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 9862 | } |
| 9863 | orig_fpcr = cpu_alpha_load_fpcr(cpu_env); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9864 | fpcr = orig_fpcr & FPCR_DYN_MASK; |
| 9865 | |
| 9866 | /* Copied from linux ieee_swcr_to_fpcr. */ |
| 9867 | fpcr |= (swcr & SWCR_STATUS_MASK) << 35; |
| 9868 | fpcr |= (swcr & SWCR_MAP_DMZ) << 36; |
| 9869 | fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV |
| 9870 | | SWCR_TRAP_ENABLE_DZE |
| 9871 | | SWCR_TRAP_ENABLE_OVF)) << 48; |
| 9872 | fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF |
| 9873 | | SWCR_TRAP_ENABLE_INE)) << 57; |
| 9874 | fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0); |
| 9875 | fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41; |
| 9876 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 9877 | cpu_alpha_store_fpcr(cpu_env, fpcr); |
| 9878 | ret = 0; |
| 9879 | } |
| 9880 | break; |
| 9881 | |
| 9882 | case TARGET_SSI_IEEE_RAISE_EXCEPTION: |
| 9883 | { |
| 9884 | uint64_t exc, fpcr, orig_fpcr; |
| 9885 | int si_code; |
| 9886 | |
| 9887 | if (get_user_u64(exc, arg2)) { |
| 9888 | goto efault; |
| 9889 | } |
| 9890 | |
| 9891 | orig_fpcr = cpu_alpha_load_fpcr(cpu_env); |
| 9892 | |
| 9893 | /* We only add to the exception status here. */ |
| 9894 | fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35); |
| 9895 | |
| 9896 | cpu_alpha_store_fpcr(cpu_env, fpcr); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9897 | ret = 0; |
| 9898 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 9899 | /* Old exceptions are not signaled. */ |
| 9900 | fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9901 | |
Richard Henderson | 6e06d51 | 2012-06-01 09:08:21 -0700 | [diff] [blame] | 9902 | /* If any exceptions set by this call, |
| 9903 | and are unmasked, send a signal. */ |
| 9904 | si_code = 0; |
| 9905 | if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) { |
| 9906 | si_code = TARGET_FPE_FLTRES; |
| 9907 | } |
| 9908 | if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) { |
| 9909 | si_code = TARGET_FPE_FLTUND; |
| 9910 | } |
| 9911 | if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) { |
| 9912 | si_code = TARGET_FPE_FLTOVF; |
| 9913 | } |
| 9914 | if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) { |
| 9915 | si_code = TARGET_FPE_FLTDIV; |
| 9916 | } |
| 9917 | if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) { |
| 9918 | si_code = TARGET_FPE_FLTINV; |
| 9919 | } |
| 9920 | if (si_code != 0) { |
| 9921 | target_siginfo_t info; |
| 9922 | info.si_signo = SIGFPE; |
| 9923 | info.si_errno = 0; |
| 9924 | info.si_code = si_code; |
| 9925 | info._sifields._sigfault._addr |
| 9926 | = ((CPUArchState *)cpu_env)->pc; |
| 9927 | queue_signal((CPUArchState *)cpu_env, info.si_signo, &info); |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9928 | } |
| 9929 | } |
| 9930 | break; |
| 9931 | |
| 9932 | /* case SSI_NVPAIRS: |
| 9933 | -- Used with SSIN_UACPROC to enable unaligned accesses. |
| 9934 | case SSI_IEEE_STATE_AT_SIGNAL: |
| 9935 | case SSI_IEEE_IGNORE_STATE_AT_SIGNAL: |
| 9936 | -- Not implemented in linux kernel |
| 9937 | */ |
| 9938 | } |
| 9939 | break; |
| 9940 | #endif |
| 9941 | #ifdef TARGET_NR_osf_sigprocmask |
| 9942 | /* Alpha specific. */ |
| 9943 | case TARGET_NR_osf_sigprocmask: |
| 9944 | { |
| 9945 | abi_ulong mask; |
Juan Quintela | bc088ba | 2011-06-16 17:37:10 +0100 | [diff] [blame] | 9946 | int how; |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9947 | sigset_t set, oldset; |
| 9948 | |
| 9949 | switch(arg1) { |
| 9950 | case TARGET_SIG_BLOCK: |
| 9951 | how = SIG_BLOCK; |
| 9952 | break; |
| 9953 | case TARGET_SIG_UNBLOCK: |
| 9954 | how = SIG_UNBLOCK; |
| 9955 | break; |
| 9956 | case TARGET_SIG_SETMASK: |
| 9957 | how = SIG_SETMASK; |
| 9958 | break; |
| 9959 | default: |
| 9960 | ret = -TARGET_EINVAL; |
| 9961 | goto fail; |
| 9962 | } |
| 9963 | mask = arg2; |
| 9964 | target_to_host_old_sigset(&set, &mask); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 9965 | ret = do_sigprocmask(how, &set, &oldset); |
| 9966 | if (!ret) { |
| 9967 | host_to_target_old_sigset(&mask, &oldset); |
| 9968 | ret = mask; |
| 9969 | } |
Richard Henderson | ba0e276 | 2009-12-09 15:56:29 -0800 | [diff] [blame] | 9970 | } |
| 9971 | break; |
| 9972 | #endif |
aurel32 | 64b4d28 | 2008-11-14 17:20:15 +0000 | [diff] [blame] | 9973 | |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9974 | #ifdef TARGET_NR_getgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9975 | case TARGET_NR_getgid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9976 | ret = get_errno(getgid()); |
| 9977 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9978 | #endif |
| 9979 | #ifdef TARGET_NR_geteuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9980 | case TARGET_NR_geteuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9981 | ret = get_errno(geteuid()); |
| 9982 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9983 | #endif |
| 9984 | #ifdef TARGET_NR_getegid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9985 | case TARGET_NR_getegid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9986 | ret = get_errno(getegid()); |
| 9987 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef TARGET_NR_setreuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9990 | case TARGET_NR_setreuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9991 | ret = get_errno(setreuid(arg1, arg2)); |
| 9992 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9993 | #endif |
| 9994 | #ifdef TARGET_NR_setregid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 9995 | case TARGET_NR_setregid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 9996 | ret = get_errno(setregid(arg1, arg2)); |
| 9997 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 9998 | #endif |
| 9999 | #ifdef TARGET_NR_getgroups32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10000 | case TARGET_NR_getgroups32: |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10001 | { |
| 10002 | int gidsetsize = arg1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10003 | uint32_t *target_grouplist; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10004 | gid_t *grouplist; |
| 10005 | int i; |
| 10006 | |
| 10007 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
| 10008 | ret = get_errno(getgroups(gidsetsize, grouplist)); |
balrog | cb3bc23 | 2008-09-20 02:08:13 +0000 | [diff] [blame] | 10009 | if (gidsetsize == 0) |
| 10010 | break; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10011 | if (!is_error(ret)) { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10012 | target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0); |
| 10013 | if (!target_grouplist) { |
| 10014 | ret = -TARGET_EFAULT; |
| 10015 | goto fail; |
| 10016 | } |
balrog | a2155fc | 2008-09-20 02:12:08 +0000 | [diff] [blame] | 10017 | for(i = 0;i < ret; i++) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10018 | target_grouplist[i] = tswap32(grouplist[i]); |
| 10019 | unlock_user(target_grouplist, arg2, gidsetsize * 4); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10020 | } |
| 10021 | } |
| 10022 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10023 | #endif |
| 10024 | #ifdef TARGET_NR_setgroups32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10025 | case TARGET_NR_setgroups32: |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10026 | { |
| 10027 | int gidsetsize = arg1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10028 | uint32_t *target_grouplist; |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10029 | gid_t *grouplist; |
| 10030 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 10031 | |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10032 | grouplist = alloca(gidsetsize * sizeof(gid_t)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10033 | target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1); |
| 10034 | if (!target_grouplist) { |
| 10035 | ret = -TARGET_EFAULT; |
| 10036 | goto fail; |
| 10037 | } |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10038 | for(i = 0;i < gidsetsize; i++) |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10039 | grouplist[i] = tswap32(target_grouplist[i]); |
| 10040 | unlock_user(target_grouplist, arg2, 0); |
bellard | 99c475a | 2005-01-31 20:45:13 +0000 | [diff] [blame] | 10041 | ret = get_errno(setgroups(gidsetsize, grouplist)); |
| 10042 | } |
| 10043 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10044 | #endif |
| 10045 | #ifdef TARGET_NR_fchown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10046 | case TARGET_NR_fchown32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10047 | ret = get_errno(fchown(arg1, arg2, arg3)); |
| 10048 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10049 | #endif |
| 10050 | #ifdef TARGET_NR_setresuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10051 | case TARGET_NR_setresuid32: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 10052 | ret = get_errno(sys_setresuid(arg1, arg2, arg3)); |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10053 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10054 | #endif |
| 10055 | #ifdef TARGET_NR_getresuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10056 | case TARGET_NR_getresuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10057 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10058 | uid_t ruid, euid, suid; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10059 | ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 10060 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 10061 | if (put_user_u32(ruid, arg1) |
| 10062 | || put_user_u32(euid, arg2) |
| 10063 | || put_user_u32(suid, arg3)) |
| 10064 | goto efault; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10065 | } |
| 10066 | } |
| 10067 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10068 | #endif |
| 10069 | #ifdef TARGET_NR_setresgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10070 | case TARGET_NR_setresgid32: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 10071 | ret = get_errno(sys_setresgid(arg1, arg2, arg3)); |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10072 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10073 | #endif |
| 10074 | #ifdef TARGET_NR_getresgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10075 | case TARGET_NR_getresgid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10076 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10077 | gid_t rgid, egid, sgid; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10078 | ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 10079 | if (!is_error(ret)) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 10080 | if (put_user_u32(rgid, arg1) |
| 10081 | || put_user_u32(egid, arg2) |
| 10082 | || put_user_u32(sgid, arg3)) |
| 10083 | goto efault; |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10084 | } |
| 10085 | } |
| 10086 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10087 | #endif |
| 10088 | #ifdef TARGET_NR_chown32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10089 | case TARGET_NR_chown32: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10090 | if (!(p = lock_user_string(arg1))) |
| 10091 | goto efault; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 10092 | ret = get_errno(chown(p, arg2, arg3)); |
| 10093 | unlock_user(p, arg1, 0); |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10094 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10095 | #endif |
| 10096 | #ifdef TARGET_NR_setuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10097 | case TARGET_NR_setuid32: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 10098 | ret = get_errno(sys_setuid(arg1)); |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10099 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10100 | #endif |
| 10101 | #ifdef TARGET_NR_setgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10102 | case TARGET_NR_setgid32: |
Peter Maydell | fd6f779 | 2016-03-01 16:33:02 +0000 | [diff] [blame] | 10103 | ret = get_errno(sys_setgid(arg1)); |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10104 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10105 | #endif |
| 10106 | #ifdef TARGET_NR_setfsuid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10107 | case TARGET_NR_setfsuid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10108 | ret = get_errno(setfsuid(arg1)); |
| 10109 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10110 | #endif |
| 10111 | #ifdef TARGET_NR_setfsgid32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10112 | case TARGET_NR_setfsgid32: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10113 | ret = get_errno(setfsgid(arg1)); |
| 10114 | break; |
bellard | a315a14 | 2005-01-30 22:59:18 +0000 | [diff] [blame] | 10115 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 10116 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10117 | case TARGET_NR_pivot_root: |
bellard | b03c60f | 2003-03-23 17:19:56 +0000 | [diff] [blame] | 10118 | goto unimplemented; |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 10119 | #ifdef TARGET_NR_mincore |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10120 | case TARGET_NR_mincore: |
aurel32 | 04bb9ac | 2008-10-01 21:46:41 +0000 | [diff] [blame] | 10121 | { |
| 10122 | void *a; |
| 10123 | ret = -TARGET_EFAULT; |
| 10124 | if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0))) |
| 10125 | goto efault; |
| 10126 | if (!(p = lock_user_string(arg3))) |
| 10127 | goto mincore_fail; |
| 10128 | ret = get_errno(mincore(a, arg2, p)); |
| 10129 | unlock_user(p, arg3, ret); |
| 10130 | mincore_fail: |
| 10131 | unlock_user(a, arg1, 0); |
| 10132 | } |
| 10133 | break; |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 10134 | #endif |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 10135 | #ifdef TARGET_NR_arm_fadvise64_64 |
| 10136 | case TARGET_NR_arm_fadvise64_64: |
Peter Maydell | e0156a9 | 2016-05-31 15:45:09 +0100 | [diff] [blame] | 10137 | /* arm_fadvise64_64 looks like fadvise64_64 but |
| 10138 | * with different argument order: fd, advice, offset, len |
| 10139 | * rather than the usual fd, offset, len, advice. |
| 10140 | * Note that offset and len are both 64-bit so appear as |
| 10141 | * pairs of 32-bit registers. |
| 10142 | */ |
| 10143 | ret = posix_fadvise(arg1, target_offset64(arg3, arg4), |
| 10144 | target_offset64(arg5, arg6), arg2); |
| 10145 | ret = -host_to_target_errno(ret); |
| 10146 | break; |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 10147 | #endif |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 10148 | |
| 10149 | #if TARGET_ABI_BITS == 32 |
| 10150 | |
| 10151 | #ifdef TARGET_NR_fadvise64_64 |
| 10152 | case TARGET_NR_fadvise64_64: |
| 10153 | /* 6 args: fd, offset (high, low), len (high, low), advice */ |
| 10154 | if (regpairs_aligned(cpu_env)) { |
| 10155 | /* offset is in (3,4), len in (5,6) and advice in 7 */ |
| 10156 | arg2 = arg3; |
| 10157 | arg3 = arg4; |
| 10158 | arg4 = arg5; |
| 10159 | arg5 = arg6; |
| 10160 | arg6 = arg7; |
| 10161 | } |
| 10162 | ret = -host_to_target_errno(posix_fadvise(arg1, |
| 10163 | target_offset64(arg2, arg3), |
| 10164 | target_offset64(arg4, arg5), |
| 10165 | arg6)); |
| 10166 | break; |
| 10167 | #endif |
| 10168 | |
| 10169 | #ifdef TARGET_NR_fadvise64 |
| 10170 | case TARGET_NR_fadvise64: |
| 10171 | /* 5 args: fd, offset (high, low), len, advice */ |
| 10172 | if (regpairs_aligned(cpu_env)) { |
| 10173 | /* offset is in (3,4), len in 5 and advice in 6 */ |
| 10174 | arg2 = arg3; |
| 10175 | arg3 = arg4; |
| 10176 | arg4 = arg5; |
| 10177 | arg5 = arg6; |
| 10178 | } |
| 10179 | ret = -host_to_target_errno(posix_fadvise(arg1, |
| 10180 | target_offset64(arg2, arg3), |
| 10181 | arg4, arg5)); |
| 10182 | break; |
| 10183 | #endif |
| 10184 | |
| 10185 | #else /* not a 32-bit ABI */ |
Peter Maydell | e0156a9 | 2016-05-31 15:45:09 +0100 | [diff] [blame] | 10186 | #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64) |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 10187 | #ifdef TARGET_NR_fadvise64_64 |
| 10188 | case TARGET_NR_fadvise64_64: |
| 10189 | #endif |
Ulrich Hecht | e72d2cc | 2009-07-24 19:10:31 +0200 | [diff] [blame] | 10190 | #ifdef TARGET_NR_fadvise64 |
| 10191 | case TARGET_NR_fadvise64: |
| 10192 | #endif |
| 10193 | #ifdef TARGET_S390X |
| 10194 | switch (arg4) { |
| 10195 | case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */ |
| 10196 | case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */ |
| 10197 | case 6: arg4 = POSIX_FADV_DONTNEED; break; |
| 10198 | case 7: arg4 = POSIX_FADV_NOREUSE; break; |
| 10199 | default: break; |
| 10200 | } |
| 10201 | #endif |
Peter Maydell | 977d824 | 2016-05-31 15:45:11 +0100 | [diff] [blame] | 10202 | ret = -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4)); |
| 10203 | break; |
aurel32 | 408321b | 2008-10-01 21:46:32 +0000 | [diff] [blame] | 10204 | #endif |
Peter Maydell | badd3cd | 2016-05-31 15:45:10 +0100 | [diff] [blame] | 10205 | #endif /* end of 64-bit ABI fadvise handling */ |
| 10206 | |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 10207 | #ifdef TARGET_NR_madvise |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10208 | case TARGET_NR_madvise: |
pbrook | 2483668 | 2006-04-16 14:14:53 +0000 | [diff] [blame] | 10209 | /* A straight passthrough may not be safe because qemu sometimes |
Lei Li | d2d6b85 | 2013-05-20 17:20:50 +0800 | [diff] [blame] | 10210 | turns private file-backed mappings into anonymous mappings. |
pbrook | 2483668 | 2006-04-16 14:14:53 +0000 | [diff] [blame] | 10211 | This will break MADV_DONTNEED. |
| 10212 | This is a hint, so ignoring and returning success is ok. */ |
| 10213 | ret = get_errno(0); |
| 10214 | break; |
bellard | ffa65c3 | 2004-01-04 23:57:22 +0000 | [diff] [blame] | 10215 | #endif |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 10216 | #if TARGET_ABI_BITS == 32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10217 | case TARGET_NR_fcntl64: |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 10218 | { |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 10219 | int cmd; |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 10220 | struct flock64 fl; |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 10221 | from_flock64_fn *copyfrom = copy_from_user_flock64; |
| 10222 | to_flock64_fn *copyto = copy_to_user_flock64; |
| 10223 | |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 10224 | #ifdef TARGET_ARM |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 10225 | if (((CPUARMState *)cpu_env)->eabi) { |
| 10226 | copyfrom = copy_from_user_eabi_flock64; |
| 10227 | copyto = copy_to_user_eabi_flock64; |
| 10228 | } |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 10229 | #endif |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 10230 | |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 10231 | cmd = target_to_host_fcntl_cmd(arg2); |
Peter Maydell | 31b6319 | 2011-12-05 23:11:50 +0000 | [diff] [blame] | 10232 | if (cmd == -TARGET_EINVAL) { |
| 10233 | ret = cmd; |
| 10234 | break; |
| 10235 | } |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 10236 | |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 10237 | switch(arg2) { |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 10238 | case TARGET_F_GETLK64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 10239 | ret = copyfrom(&fl, arg3); |
| 10240 | if (ret) { |
| 10241 | break; |
ths | 5813427 | 2007-03-31 18:59:32 +0000 | [diff] [blame] | 10242 | } |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 10243 | ret = get_errno(fcntl(arg1, cmd, &fl)); |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 10244 | if (ret == 0) { |
| 10245 | ret = copyto(arg3, &fl); |
| 10246 | } |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 10247 | break; |
| 10248 | |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 10249 | case TARGET_F_SETLK64: |
| 10250 | case TARGET_F_SETLKW64: |
Peter Maydell | 213d3e9 | 2016-06-13 11:22:05 +0100 | [diff] [blame^] | 10251 | ret = copyfrom(&fl, arg3); |
| 10252 | if (ret) { |
| 10253 | break; |
pbrook | ce4defa | 2006-02-09 16:49:55 +0000 | [diff] [blame] | 10254 | } |
ths | b1e341e | 2007-03-20 21:50:52 +0000 | [diff] [blame] | 10255 | ret = get_errno(fcntl(arg1, cmd, &fl)); |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 10256 | break; |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 10257 | default: |
Arnaud Patard (Rtp) | 5f10681 | 2009-06-03 14:35:04 +0200 | [diff] [blame] | 10258 | ret = do_fcntl(arg1, arg2, arg3); |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 10259 | break; |
| 10260 | } |
bellard | 77e4672 | 2003-04-29 20:39:06 +0000 | [diff] [blame] | 10261 | break; |
| 10262 | } |
bellard | 60cd49d | 2003-03-16 22:53:56 +0000 | [diff] [blame] | 10263 | #endif |
ths | 7d600c8 | 2006-12-08 01:32:58 +0000 | [diff] [blame] | 10264 | #ifdef TARGET_NR_cacheflush |
| 10265 | case TARGET_NR_cacheflush: |
| 10266 | /* self-modifying code is handled automatically, so nothing needed */ |
| 10267 | ret = 0; |
| 10268 | break; |
| 10269 | #endif |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10270 | #ifdef TARGET_NR_security |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10271 | case TARGET_NR_security: |
| 10272 | goto unimplemented; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10273 | #endif |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 10274 | #ifdef TARGET_NR_getpagesize |
| 10275 | case TARGET_NR_getpagesize: |
| 10276 | ret = TARGET_PAGE_SIZE; |
| 10277 | break; |
| 10278 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10279 | case TARGET_NR_gettid: |
| 10280 | ret = get_errno(gettid()); |
| 10281 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10282 | #ifdef TARGET_NR_readahead |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10283 | case TARGET_NR_readahead: |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 10284 | #if TARGET_ABI_BITS == 32 |
Riku Voipio | 48e515d | 2011-07-12 15:40:51 +0300 | [diff] [blame] | 10285 | if (regpairs_aligned(cpu_env)) { |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 10286 | arg2 = arg3; |
| 10287 | arg3 = arg4; |
| 10288 | arg4 = arg5; |
| 10289 | } |
aurel32 | 2054ac9 | 2008-10-13 21:08:07 +0000 | [diff] [blame] | 10290 | ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4)); |
| 10291 | #else |
| 10292 | ret = get_errno(readahead(arg1, arg2, arg3)); |
| 10293 | #endif |
| 10294 | break; |
ths | e5febef | 2007-04-01 18:31:35 +0000 | [diff] [blame] | 10295 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10296 | #ifdef CONFIG_ATTR |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10297 | #ifdef TARGET_NR_setxattr |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10298 | case TARGET_NR_listxattr: |
| 10299 | case TARGET_NR_llistxattr: |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 10300 | { |
| 10301 | void *p, *b = 0; |
| 10302 | if (arg2) { |
| 10303 | b = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 10304 | if (!b) { |
| 10305 | ret = -TARGET_EFAULT; |
| 10306 | break; |
| 10307 | } |
| 10308 | } |
| 10309 | p = lock_user_string(arg1); |
| 10310 | if (p) { |
| 10311 | if (num == TARGET_NR_listxattr) { |
| 10312 | ret = get_errno(listxattr(p, b, arg3)); |
| 10313 | } else { |
| 10314 | ret = get_errno(llistxattr(p, b, arg3)); |
| 10315 | } |
| 10316 | } else { |
| 10317 | ret = -TARGET_EFAULT; |
| 10318 | } |
| 10319 | unlock_user(p, arg1, 0); |
| 10320 | unlock_user(b, arg2, arg3); |
Arnaud Patard | 6f932f9 | 2009-04-21 21:04:18 +0300 | [diff] [blame] | 10321 | break; |
Peter Maydell | fb5590f | 2011-12-14 15:37:19 +0000 | [diff] [blame] | 10322 | } |
| 10323 | case TARGET_NR_flistxattr: |
| 10324 | { |
| 10325 | void *b = 0; |
| 10326 | if (arg2) { |
| 10327 | b = lock_user(VERIFY_WRITE, arg2, arg3, 0); |
| 10328 | if (!b) { |
| 10329 | ret = -TARGET_EFAULT; |
| 10330 | break; |
| 10331 | } |
| 10332 | } |
| 10333 | ret = get_errno(flistxattr(arg1, b, arg3)); |
| 10334 | unlock_user(b, arg2, arg3); |
| 10335 | break; |
| 10336 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10337 | case TARGET_NR_setxattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10338 | case TARGET_NR_lsetxattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10339 | { |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 10340 | void *p, *n, *v = 0; |
| 10341 | if (arg3) { |
| 10342 | v = lock_user(VERIFY_READ, arg3, arg4, 1); |
| 10343 | if (!v) { |
| 10344 | ret = -TARGET_EFAULT; |
| 10345 | break; |
| 10346 | } |
| 10347 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10348 | p = lock_user_string(arg1); |
| 10349 | n = lock_user_string(arg2); |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 10350 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10351 | if (num == TARGET_NR_setxattr) { |
| 10352 | ret = get_errno(setxattr(p, n, v, arg4, arg5)); |
| 10353 | } else { |
| 10354 | ret = get_errno(lsetxattr(p, n, v, arg4, arg5)); |
| 10355 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10356 | } else { |
| 10357 | ret = -TARGET_EFAULT; |
| 10358 | } |
| 10359 | unlock_user(p, arg1, 0); |
| 10360 | unlock_user(n, arg2, 0); |
| 10361 | unlock_user(v, arg3, 0); |
| 10362 | } |
| 10363 | break; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10364 | case TARGET_NR_fsetxattr: |
| 10365 | { |
| 10366 | void *n, *v = 0; |
| 10367 | if (arg3) { |
| 10368 | v = lock_user(VERIFY_READ, arg3, arg4, 1); |
| 10369 | if (!v) { |
| 10370 | ret = -TARGET_EFAULT; |
| 10371 | break; |
| 10372 | } |
| 10373 | } |
| 10374 | n = lock_user_string(arg2); |
| 10375 | if (n) { |
| 10376 | ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5)); |
| 10377 | } else { |
| 10378 | ret = -TARGET_EFAULT; |
| 10379 | } |
| 10380 | unlock_user(n, arg2, 0); |
| 10381 | unlock_user(v, arg3, 0); |
| 10382 | } |
| 10383 | break; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10384 | case TARGET_NR_getxattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10385 | case TARGET_NR_lgetxattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10386 | { |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 10387 | void *p, *n, *v = 0; |
| 10388 | if (arg3) { |
| 10389 | v = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
| 10390 | if (!v) { |
| 10391 | ret = -TARGET_EFAULT; |
| 10392 | break; |
| 10393 | } |
| 10394 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10395 | p = lock_user_string(arg1); |
| 10396 | n = lock_user_string(arg2); |
Peter Maydell | e3c33ec | 2011-12-14 15:37:17 +0000 | [diff] [blame] | 10397 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10398 | if (num == TARGET_NR_getxattr) { |
| 10399 | ret = get_errno(getxattr(p, n, v, arg4)); |
| 10400 | } else { |
| 10401 | ret = get_errno(lgetxattr(p, n, v, arg4)); |
| 10402 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10403 | } else { |
| 10404 | ret = -TARGET_EFAULT; |
| 10405 | } |
| 10406 | unlock_user(p, arg1, 0); |
| 10407 | unlock_user(n, arg2, 0); |
| 10408 | unlock_user(v, arg3, arg4); |
| 10409 | } |
| 10410 | break; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10411 | case TARGET_NR_fgetxattr: |
| 10412 | { |
| 10413 | void *n, *v = 0; |
| 10414 | if (arg3) { |
| 10415 | v = lock_user(VERIFY_WRITE, arg3, arg4, 0); |
| 10416 | if (!v) { |
| 10417 | ret = -TARGET_EFAULT; |
| 10418 | break; |
| 10419 | } |
| 10420 | } |
| 10421 | n = lock_user_string(arg2); |
| 10422 | if (n) { |
| 10423 | ret = get_errno(fgetxattr(arg1, n, v, arg4)); |
| 10424 | } else { |
| 10425 | ret = -TARGET_EFAULT; |
| 10426 | } |
| 10427 | unlock_user(n, arg2, 0); |
| 10428 | unlock_user(v, arg3, arg4); |
| 10429 | } |
| 10430 | break; |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10431 | case TARGET_NR_removexattr: |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10432 | case TARGET_NR_lremovexattr: |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10433 | { |
| 10434 | void *p, *n; |
| 10435 | p = lock_user_string(arg1); |
| 10436 | n = lock_user_string(arg2); |
| 10437 | if (p && n) { |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10438 | if (num == TARGET_NR_removexattr) { |
| 10439 | ret = get_errno(removexattr(p, n)); |
| 10440 | } else { |
| 10441 | ret = get_errno(lremovexattr(p, n)); |
| 10442 | } |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10443 | } else { |
| 10444 | ret = -TARGET_EFAULT; |
| 10445 | } |
| 10446 | unlock_user(p, arg1, 0); |
| 10447 | unlock_user(n, arg2, 0); |
| 10448 | } |
| 10449 | break; |
Peter Maydell | 30297b5 | 2011-12-14 15:37:18 +0000 | [diff] [blame] | 10450 | case TARGET_NR_fremovexattr: |
| 10451 | { |
| 10452 | void *n; |
| 10453 | n = lock_user_string(arg2); |
| 10454 | if (n) { |
| 10455 | ret = get_errno(fremovexattr(arg1, n)); |
| 10456 | } else { |
| 10457 | ret = -TARGET_EFAULT; |
| 10458 | } |
| 10459 | unlock_user(n, arg2, 0); |
| 10460 | } |
| 10461 | break; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10462 | #endif |
An-Cheng Huang | a790ae3 | 2011-08-09 12:34:06 -0700 | [diff] [blame] | 10463 | #endif /* CONFIG_ATTR */ |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10464 | #ifdef TARGET_NR_set_thread_area |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10465 | case TARGET_NR_set_thread_area: |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 10466 | #if defined(TARGET_MIPS) |
Petar Jovanovic | d279279 | 2014-06-18 17:48:20 +0200 | [diff] [blame] | 10467 | ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1; |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 10468 | ret = 0; |
| 10469 | break; |
edgar_igl | ef96779 | 2009-01-07 14:19:38 +0000 | [diff] [blame] | 10470 | #elif defined(TARGET_CRIS) |
| 10471 | if (arg1 & 0xff) |
| 10472 | ret = -TARGET_EINVAL; |
| 10473 | else { |
| 10474 | ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1; |
| 10475 | ret = 0; |
| 10476 | } |
| 10477 | break; |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 10478 | #elif defined(TARGET_I386) && defined(TARGET_ABI32) |
| 10479 | ret = do_set_thread_area(cpu_env, arg1); |
| 10480 | break; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 10481 | #elif defined(TARGET_M68K) |
| 10482 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 10483 | TaskState *ts = cpu->opaque; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 10484 | ts->tp_value = arg1; |
Peter Maydell | 95c1eb1 | 2013-07-29 11:31:49 +0100 | [diff] [blame] | 10485 | ret = 0; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 10486 | break; |
| 10487 | } |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 10488 | #else |
| 10489 | goto unimplemented_nowarn; |
| 10490 | #endif |
| 10491 | #endif |
| 10492 | #ifdef TARGET_NR_get_thread_area |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10493 | case TARGET_NR_get_thread_area: |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 10494 | #if defined(TARGET_I386) && defined(TARGET_ABI32) |
| 10495 | ret = do_get_thread_area(cpu_env, arg1); |
Peter Maydell | d312bbe | 2013-07-16 18:44:56 +0100 | [diff] [blame] | 10496 | break; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 10497 | #elif defined(TARGET_M68K) |
| 10498 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 10499 | TaskState *ts = cpu->opaque; |
Peter Maydell | 1ccd937 | 2013-07-16 18:44:55 +0100 | [diff] [blame] | 10500 | ret = ts->tp_value; |
| 10501 | break; |
| 10502 | } |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 10503 | #else |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 10504 | goto unimplemented_nowarn; |
bellard | ebc0548 | 2003-09-30 21:08:41 +0000 | [diff] [blame] | 10505 | #endif |
bellard | 8d18e89 | 2007-11-14 15:18:40 +0000 | [diff] [blame] | 10506 | #endif |
bellard | 48dc41e | 2006-06-21 18:15:50 +0000 | [diff] [blame] | 10507 | #ifdef TARGET_NR_getdomainname |
| 10508 | case TARGET_NR_getdomainname: |
| 10509 | goto unimplemented_nowarn; |
| 10510 | #endif |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 10511 | |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 10512 | #ifdef TARGET_NR_clock_gettime |
| 10513 | case TARGET_NR_clock_gettime: |
| 10514 | { |
| 10515 | struct timespec ts; |
| 10516 | ret = get_errno(clock_gettime(arg1, &ts)); |
| 10517 | if (!is_error(ret)) { |
| 10518 | host_to_target_timespec(arg2, &ts); |
| 10519 | } |
| 10520 | break; |
| 10521 | } |
| 10522 | #endif |
| 10523 | #ifdef TARGET_NR_clock_getres |
| 10524 | case TARGET_NR_clock_getres: |
| 10525 | { |
| 10526 | struct timespec ts; |
| 10527 | ret = get_errno(clock_getres(arg1, &ts)); |
| 10528 | if (!is_error(ret)) { |
| 10529 | host_to_target_timespec(arg2, &ts); |
| 10530 | } |
| 10531 | break; |
| 10532 | } |
| 10533 | #endif |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 10534 | #ifdef TARGET_NR_clock_nanosleep |
| 10535 | case TARGET_NR_clock_nanosleep: |
| 10536 | { |
| 10537 | struct timespec ts; |
| 10538 | target_to_host_timespec(&ts, arg3); |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 10539 | ret = get_errno(safe_clock_nanosleep(arg1, arg2, |
| 10540 | &ts, arg4 ? &ts : NULL)); |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 10541 | if (arg4) |
| 10542 | host_to_target_timespec(arg4, &ts); |
Tom Musta | 8fbe8fd | 2014-08-12 13:53:41 -0500 | [diff] [blame] | 10543 | |
| 10544 | #if defined(TARGET_PPC) |
| 10545 | /* clock_nanosleep is odd in that it returns positive errno values. |
| 10546 | * On PPC, CR0 bit 3 should be set in such a situation. */ |
Peter Maydell | 9e51822 | 2016-06-06 19:58:09 +0100 | [diff] [blame] | 10547 | if (ret && ret != -TARGET_ERESTARTSYS) { |
Tom Musta | 8fbe8fd | 2014-08-12 13:53:41 -0500 | [diff] [blame] | 10548 | ((CPUPPCState *)cpu_env)->crf[0] |= 1; |
| 10549 | } |
| 10550 | #endif |
pbrook | 63d7651 | 2008-05-29 13:43:29 +0000 | [diff] [blame] | 10551 | break; |
| 10552 | } |
| 10553 | #endif |
ths | b5906f9 | 2007-03-19 13:32:45 +0000 | [diff] [blame] | 10554 | |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 10555 | #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) |
| 10556 | case TARGET_NR_set_tid_address: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10557 | ret = get_errno(set_tid_address((int *)g2h(arg1))); |
| 10558 | break; |
ths | 6f5b89a | 2007-03-02 20:48:00 +0000 | [diff] [blame] | 10559 | #endif |
| 10560 | |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 10561 | case TARGET_NR_tkill: |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 10562 | ret = get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2))); |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 10563 | break; |
ths | 4cae1d1 | 2007-07-12 11:06:53 +0000 | [diff] [blame] | 10564 | |
ths | 7145557 | 2007-06-21 21:45:30 +0000 | [diff] [blame] | 10565 | case TARGET_NR_tgkill: |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 10566 | ret = get_errno(safe_tgkill((int)arg1, (int)arg2, |
pbrook | 4cb0596 | 2008-05-30 18:05:19 +0000 | [diff] [blame] | 10567 | target_to_host_signal(arg3))); |
Peter Maydell | bef653d | 2016-05-27 15:51:57 +0100 | [diff] [blame] | 10568 | break; |
ths | 7145557 | 2007-06-21 21:45:30 +0000 | [diff] [blame] | 10569 | |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 10570 | #ifdef TARGET_NR_set_robust_list |
| 10571 | case TARGET_NR_set_robust_list: |
Peter Maydell | e9a970a | 2013-02-08 04:34:54 +0000 | [diff] [blame] | 10572 | case TARGET_NR_get_robust_list: |
| 10573 | /* The ABI for supporting robust futexes has userspace pass |
| 10574 | * the kernel a pointer to a linked list which is updated by |
| 10575 | * userspace after the syscall; the list is walked by the kernel |
| 10576 | * when the thread exits. Since the linked list in QEMU guest |
| 10577 | * memory isn't a valid linked list for the host and we have |
| 10578 | * no way to reliably intercept the thread-death event, we can't |
| 10579 | * support these. Silently return ENOSYS so that guest userspace |
| 10580 | * falls back to a non-robust futex implementation (which should |
| 10581 | * be OK except in the corner case of the guest crashing while |
| 10582 | * holding a mutex that is shared with another process via |
| 10583 | * shared memory). |
| 10584 | */ |
| 10585 | goto unimplemented_nowarn; |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 10586 | #endif |
| 10587 | |
Peter Maydell | 1acae9f | 2013-07-02 14:04:12 +0100 | [diff] [blame] | 10588 | #if defined(TARGET_NR_utimensat) |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 10589 | case TARGET_NR_utimensat: |
| 10590 | { |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 10591 | struct timespec *tsp, ts[2]; |
| 10592 | if (!arg3) { |
| 10593 | tsp = NULL; |
| 10594 | } else { |
| 10595 | target_to_host_timespec(ts, arg3); |
| 10596 | target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec)); |
| 10597 | tsp = ts; |
| 10598 | } |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 10599 | if (!arg2) |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 10600 | ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 10601 | else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10602 | if (!(p = lock_user_string(arg2))) { |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 10603 | ret = -TARGET_EFAULT; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10604 | goto fail; |
| 10605 | } |
Riku Voipio | ebc996f | 2009-04-21 15:01:51 +0300 | [diff] [blame] | 10606 | ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 10607 | unlock_user(p, arg2, 0); |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 10608 | } |
| 10609 | } |
| 10610 | break; |
| 10611 | #endif |
pbrook | bd0c566 | 2008-05-29 14:34:11 +0000 | [diff] [blame] | 10612 | case TARGET_NR_futex: |
| 10613 | ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6); |
| 10614 | break; |
aurel32 | dbfe4c3 | 2009-04-08 21:29:30 +0000 | [diff] [blame] | 10615 | #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 10616 | case TARGET_NR_inotify_init: |
| 10617 | ret = get_errno(sys_inotify_init()); |
| 10618 | break; |
| 10619 | #endif |
Stefan Weil | a1606b0 | 2010-03-28 11:44:41 +0200 | [diff] [blame] | 10620 | #ifdef CONFIG_INOTIFY1 |
Riku Voipio | c05c7a7 | 2010-03-26 15:25:11 +0000 | [diff] [blame] | 10621 | #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1) |
| 10622 | case TARGET_NR_inotify_init1: |
| 10623 | ret = get_errno(sys_inotify_init1(arg1)); |
| 10624 | break; |
| 10625 | #endif |
Stefan Weil | a1606b0 | 2010-03-28 11:44:41 +0200 | [diff] [blame] | 10626 | #endif |
aurel32 | dbfe4c3 | 2009-04-08 21:29:30 +0000 | [diff] [blame] | 10627 | #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 10628 | case TARGET_NR_inotify_add_watch: |
| 10629 | p = lock_user_string(arg2); |
| 10630 | ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3)); |
| 10631 | unlock_user(p, arg2, 0); |
| 10632 | break; |
| 10633 | #endif |
aurel32 | dbfe4c3 | 2009-04-08 21:29:30 +0000 | [diff] [blame] | 10634 | #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch) |
aurel32 | 39b5976 | 2008-10-01 21:46:50 +0000 | [diff] [blame] | 10635 | case TARGET_NR_inotify_rm_watch: |
| 10636 | ret = get_errno(sys_inotify_rm_watch(arg1, arg2)); |
| 10637 | break; |
| 10638 | #endif |
ths | 9007f0e | 2007-09-25 17:50:37 +0000 | [diff] [blame] | 10639 | |
Nathan Froyd | 8ec9cf8 | 2009-07-22 09:14:36 -0700 | [diff] [blame] | 10640 | #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10641 | case TARGET_NR_mq_open: |
| 10642 | { |
Tom Musta | b6ce1f6 | 2014-08-12 13:53:36 -0500 | [diff] [blame] | 10643 | struct mq_attr posix_mq_attr, *attrp; |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10644 | |
| 10645 | p = lock_user_string(arg1 - 1); |
Tom Musta | b6ce1f6 | 2014-08-12 13:53:36 -0500 | [diff] [blame] | 10646 | if (arg4 != 0) { |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10647 | copy_from_user_mq_attr (&posix_mq_attr, arg4); |
Tom Musta | b6ce1f6 | 2014-08-12 13:53:36 -0500 | [diff] [blame] | 10648 | attrp = &posix_mq_attr; |
| 10649 | } else { |
| 10650 | attrp = 0; |
| 10651 | } |
| 10652 | ret = get_errno(mq_open(p, arg2, arg3, attrp)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10653 | unlock_user (p, arg1, 0); |
| 10654 | } |
| 10655 | break; |
| 10656 | |
| 10657 | case TARGET_NR_mq_unlink: |
| 10658 | p = lock_user_string(arg1 - 1); |
| 10659 | ret = get_errno(mq_unlink(p)); |
| 10660 | unlock_user (p, arg1, 0); |
| 10661 | break; |
| 10662 | |
| 10663 | case TARGET_NR_mq_timedsend: |
| 10664 | { |
| 10665 | struct timespec ts; |
| 10666 | |
| 10667 | p = lock_user (VERIFY_READ, arg2, arg3, 1); |
| 10668 | if (arg5 != 0) { |
| 10669 | target_to_host_timespec(&ts, arg5); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 10670 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10671 | host_to_target_timespec(arg5, &ts); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 10672 | } else { |
| 10673 | ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10674 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10675 | unlock_user (p, arg2, arg3); |
| 10676 | } |
| 10677 | break; |
| 10678 | |
| 10679 | case TARGET_NR_mq_timedreceive: |
| 10680 | { |
| 10681 | struct timespec ts; |
| 10682 | unsigned int prio; |
| 10683 | |
| 10684 | p = lock_user (VERIFY_READ, arg2, arg3, 1); |
| 10685 | if (arg5 != 0) { |
| 10686 | target_to_host_timespec(&ts, arg5); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 10687 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 10688 | &prio, &ts)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10689 | host_to_target_timespec(arg5, &ts); |
Peter Maydell | d40ecd6 | 2016-06-06 19:58:06 +0100 | [diff] [blame] | 10690 | } else { |
| 10691 | ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, |
| 10692 | &prio, NULL)); |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10693 | } |
aurel32 | 24e1003 | 2009-04-15 16:11:43 +0000 | [diff] [blame] | 10694 | unlock_user (p, arg2, arg3); |
| 10695 | if (arg4 != 0) |
| 10696 | put_user_u32(prio, arg4); |
| 10697 | } |
| 10698 | break; |
| 10699 | |
| 10700 | /* Not implemented for now... */ |
| 10701 | /* case TARGET_NR_mq_notify: */ |
| 10702 | /* break; */ |
| 10703 | |
| 10704 | case TARGET_NR_mq_getsetattr: |
| 10705 | { |
| 10706 | struct mq_attr posix_mq_attr_in, posix_mq_attr_out; |
| 10707 | ret = 0; |
| 10708 | if (arg3 != 0) { |
| 10709 | ret = mq_getattr(arg1, &posix_mq_attr_out); |
| 10710 | copy_to_user_mq_attr(arg3, &posix_mq_attr_out); |
| 10711 | } |
| 10712 | if (arg2 != 0) { |
| 10713 | copy_from_user_mq_attr(&posix_mq_attr_in, arg2); |
| 10714 | ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out); |
| 10715 | } |
| 10716 | |
| 10717 | } |
| 10718 | break; |
| 10719 | #endif |
| 10720 | |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 10721 | #ifdef CONFIG_SPLICE |
| 10722 | #ifdef TARGET_NR_tee |
| 10723 | case TARGET_NR_tee: |
| 10724 | { |
| 10725 | ret = get_errno(tee(arg1,arg2,arg3,arg4)); |
| 10726 | } |
| 10727 | break; |
| 10728 | #endif |
| 10729 | #ifdef TARGET_NR_splice |
| 10730 | case TARGET_NR_splice: |
| 10731 | { |
| 10732 | loff_t loff_in, loff_out; |
| 10733 | loff_t *ploff_in = NULL, *ploff_out = NULL; |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 10734 | if (arg2) { |
| 10735 | if (get_user_u64(loff_in, arg2)) { |
| 10736 | goto efault; |
| 10737 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 10738 | ploff_in = &loff_in; |
| 10739 | } |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 10740 | if (arg4) { |
| 10741 | if (get_user_u64(loff_out, arg4)) { |
| 10742 | goto efault; |
| 10743 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 10744 | ploff_out = &loff_out; |
| 10745 | } |
| 10746 | ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6)); |
Andreas Schwab | 17644b3 | 2015-03-10 17:11:35 +0100 | [diff] [blame] | 10747 | if (arg2) { |
| 10748 | if (put_user_u64(loff_in, arg2)) { |
| 10749 | goto efault; |
| 10750 | } |
| 10751 | } |
| 10752 | if (arg4) { |
| 10753 | if (put_user_u64(loff_out, arg4)) { |
| 10754 | goto efault; |
| 10755 | } |
| 10756 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 10757 | } |
| 10758 | break; |
| 10759 | #endif |
| 10760 | #ifdef TARGET_NR_vmsplice |
| 10761 | case TARGET_NR_vmsplice: |
| 10762 | { |
Richard Henderson | f287b2c | 2012-09-15 13:20:25 -0700 | [diff] [blame] | 10763 | struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); |
| 10764 | if (vec != NULL) { |
| 10765 | ret = get_errno(vmsplice(arg1, vec, arg3, arg4)); |
| 10766 | unlock_iovec(vec, arg2, arg3, 0); |
| 10767 | } else { |
| 10768 | ret = -host_to_target_errno(errno); |
| 10769 | } |
vibisreenivasan | 3ce34df | 2009-05-16 18:32:41 +0530 | [diff] [blame] | 10770 | } |
| 10771 | break; |
| 10772 | #endif |
| 10773 | #endif /* CONFIG_SPLICE */ |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 10774 | #ifdef CONFIG_EVENTFD |
| 10775 | #if defined(TARGET_NR_eventfd) |
| 10776 | case TARGET_NR_eventfd: |
| 10777 | ret = get_errno(eventfd(arg1, 0)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 10778 | fd_trans_unregister(ret); |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 10779 | break; |
| 10780 | #endif |
| 10781 | #if defined(TARGET_NR_eventfd2) |
| 10782 | case TARGET_NR_eventfd2: |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 10783 | { |
| 10784 | int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)); |
| 10785 | if (arg2 & TARGET_O_NONBLOCK) { |
| 10786 | host_flags |= O_NONBLOCK; |
| 10787 | } |
| 10788 | if (arg2 & TARGET_O_CLOEXEC) { |
| 10789 | host_flags |= O_CLOEXEC; |
| 10790 | } |
| 10791 | ret = get_errno(eventfd(arg1, host_flags)); |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 10792 | fd_trans_unregister(ret); |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 10793 | break; |
Petar Jovanovic | 5947c69 | 2013-04-08 20:26:10 +0200 | [diff] [blame] | 10794 | } |
Riku Voipio | c2882b9 | 2009-08-12 15:08:24 +0300 | [diff] [blame] | 10795 | #endif |
| 10796 | #endif /* CONFIG_EVENTFD */ |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 10797 | #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate) |
| 10798 | case TARGET_NR_fallocate: |
Alexander Graf | 20249ae | 2012-02-06 21:37:07 +0100 | [diff] [blame] | 10799 | #if TARGET_ABI_BITS == 32 |
| 10800 | ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4), |
| 10801 | target_offset64(arg5, arg6))); |
| 10802 | #else |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 10803 | ret = get_errno(fallocate(arg1, arg2, arg3, arg4)); |
Alexander Graf | 20249ae | 2012-02-06 21:37:07 +0100 | [diff] [blame] | 10804 | #endif |
Ulrich Hecht | d092793 | 2009-09-17 20:22:14 +0300 | [diff] [blame] | 10805 | break; |
| 10806 | #endif |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 10807 | #if defined(CONFIG_SYNC_FILE_RANGE) |
| 10808 | #if defined(TARGET_NR_sync_file_range) |
| 10809 | case TARGET_NR_sync_file_range: |
| 10810 | #if TARGET_ABI_BITS == 32 |
Riku Voipio | bfcedc5 | 2011-06-20 16:24:39 +0300 | [diff] [blame] | 10811 | #if defined(TARGET_MIPS) |
| 10812 | ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), |
| 10813 | target_offset64(arg5, arg6), arg7)); |
| 10814 | #else |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 10815 | ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3), |
| 10816 | target_offset64(arg4, arg5), arg6)); |
Riku Voipio | bfcedc5 | 2011-06-20 16:24:39 +0300 | [diff] [blame] | 10817 | #endif /* !TARGET_MIPS */ |
Peter Maydell | c727f47 | 2011-01-06 11:05:10 +0000 | [diff] [blame] | 10818 | #else |
| 10819 | ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4)); |
| 10820 | #endif |
| 10821 | break; |
| 10822 | #endif |
| 10823 | #if defined(TARGET_NR_sync_file_range2) |
| 10824 | case TARGET_NR_sync_file_range2: |
| 10825 | /* This is like sync_file_range but the arguments are reordered */ |
| 10826 | #if TARGET_ABI_BITS == 32 |
| 10827 | ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), |
| 10828 | target_offset64(arg5, arg6), arg2)); |
| 10829 | #else |
| 10830 | ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2)); |
| 10831 | #endif |
| 10832 | break; |
| 10833 | #endif |
| 10834 | #endif |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 10835 | #if defined(TARGET_NR_signalfd4) |
| 10836 | case TARGET_NR_signalfd4: |
| 10837 | ret = do_signalfd4(arg1, arg2, arg4); |
| 10838 | break; |
| 10839 | #endif |
| 10840 | #if defined(TARGET_NR_signalfd) |
| 10841 | case TARGET_NR_signalfd: |
| 10842 | ret = do_signalfd4(arg1, arg2, 0); |
| 10843 | break; |
| 10844 | #endif |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 10845 | #if defined(CONFIG_EPOLL) |
| 10846 | #if defined(TARGET_NR_epoll_create) |
| 10847 | case TARGET_NR_epoll_create: |
| 10848 | ret = get_errno(epoll_create(arg1)); |
| 10849 | break; |
| 10850 | #endif |
| 10851 | #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1) |
| 10852 | case TARGET_NR_epoll_create1: |
| 10853 | ret = get_errno(epoll_create1(arg1)); |
| 10854 | break; |
| 10855 | #endif |
| 10856 | #if defined(TARGET_NR_epoll_ctl) |
| 10857 | case TARGET_NR_epoll_ctl: |
| 10858 | { |
| 10859 | struct epoll_event ep; |
| 10860 | struct epoll_event *epp = 0; |
| 10861 | if (arg4) { |
| 10862 | struct target_epoll_event *target_ep; |
| 10863 | if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) { |
| 10864 | goto efault; |
| 10865 | } |
| 10866 | ep.events = tswap32(target_ep->events); |
| 10867 | /* The epoll_data_t union is just opaque data to the kernel, |
| 10868 | * so we transfer all 64 bits across and need not worry what |
| 10869 | * actual data type it is. |
| 10870 | */ |
| 10871 | ep.data.u64 = tswap64(target_ep->data.u64); |
| 10872 | unlock_user_struct(target_ep, arg4, 0); |
| 10873 | epp = &ep; |
| 10874 | } |
| 10875 | ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp)); |
| 10876 | break; |
| 10877 | } |
| 10878 | #endif |
| 10879 | |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 10880 | #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 10881 | #if defined(TARGET_NR_epoll_wait) |
| 10882 | case TARGET_NR_epoll_wait: |
| 10883 | #endif |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 10884 | #if defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 10885 | case TARGET_NR_epoll_pwait: |
| 10886 | #endif |
| 10887 | { |
| 10888 | struct target_epoll_event *target_ep; |
| 10889 | struct epoll_event *ep; |
| 10890 | int epfd = arg1; |
| 10891 | int maxevents = arg3; |
| 10892 | int timeout = arg4; |
| 10893 | |
| 10894 | target_ep = lock_user(VERIFY_WRITE, arg2, |
| 10895 | maxevents * sizeof(struct target_epoll_event), 1); |
| 10896 | if (!target_ep) { |
| 10897 | goto efault; |
| 10898 | } |
| 10899 | |
| 10900 | ep = alloca(maxevents * sizeof(struct epoll_event)); |
| 10901 | |
| 10902 | switch (num) { |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 10903 | #if defined(TARGET_NR_epoll_pwait) |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 10904 | case TARGET_NR_epoll_pwait: |
| 10905 | { |
| 10906 | target_sigset_t *target_set; |
| 10907 | sigset_t _set, *set = &_set; |
| 10908 | |
| 10909 | if (arg5) { |
| 10910 | target_set = lock_user(VERIFY_READ, arg5, |
| 10911 | sizeof(target_sigset_t), 1); |
| 10912 | if (!target_set) { |
| 10913 | unlock_user(target_ep, arg2, 0); |
| 10914 | goto efault; |
| 10915 | } |
| 10916 | target_to_host_sigset(set, target_set); |
| 10917 | unlock_user(target_set, arg5, 0); |
| 10918 | } else { |
| 10919 | set = NULL; |
| 10920 | } |
| 10921 | |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 10922 | ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, |
| 10923 | set, SIGSET_T_SIZE)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 10924 | break; |
| 10925 | } |
| 10926 | #endif |
| 10927 | #if defined(TARGET_NR_epoll_wait) |
| 10928 | case TARGET_NR_epoll_wait: |
Peter Maydell | 227f021 | 2016-06-06 19:58:11 +0100 | [diff] [blame] | 10929 | ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, |
| 10930 | NULL, 0)); |
Peter Maydell | 3b6edd1 | 2011-02-15 18:35:05 +0000 | [diff] [blame] | 10931 | break; |
| 10932 | #endif |
| 10933 | default: |
| 10934 | ret = -TARGET_ENOSYS; |
| 10935 | } |
| 10936 | if (!is_error(ret)) { |
| 10937 | int i; |
| 10938 | for (i = 0; i < ret; i++) { |
| 10939 | target_ep[i].events = tswap32(ep[i].events); |
| 10940 | target_ep[i].data.u64 = tswap64(ep[i].data.u64); |
| 10941 | } |
| 10942 | } |
| 10943 | unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event)); |
| 10944 | break; |
| 10945 | } |
| 10946 | #endif |
| 10947 | #endif |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 10948 | #ifdef TARGET_NR_prlimit64 |
| 10949 | case TARGET_NR_prlimit64: |
| 10950 | { |
| 10951 | /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */ |
| 10952 | struct target_rlimit64 *target_rnew, *target_rold; |
| 10953 | struct host_rlimit64 rnew, rold, *rnewp = 0; |
Felix Janda | 9501801 | 2014-12-02 22:11:17 +0100 | [diff] [blame] | 10954 | int resource = target_to_host_resource(arg2); |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 10955 | if (arg3) { |
| 10956 | if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) { |
| 10957 | goto efault; |
| 10958 | } |
| 10959 | rnew.rlim_cur = tswap64(target_rnew->rlim_cur); |
| 10960 | rnew.rlim_max = tswap64(target_rnew->rlim_max); |
| 10961 | unlock_user_struct(target_rnew, arg3, 0); |
| 10962 | rnewp = &rnew; |
| 10963 | } |
| 10964 | |
Felix Janda | 9501801 | 2014-12-02 22:11:17 +0100 | [diff] [blame] | 10965 | ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0)); |
Peter Maydell | 163a05a | 2011-06-27 17:44:52 +0100 | [diff] [blame] | 10966 | if (!is_error(ret) && arg4) { |
| 10967 | if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) { |
| 10968 | goto efault; |
| 10969 | } |
| 10970 | target_rold->rlim_cur = tswap64(rold.rlim_cur); |
| 10971 | target_rold->rlim_max = tswap64(rold.rlim_max); |
| 10972 | unlock_user_struct(target_rold, arg4, 1); |
| 10973 | } |
| 10974 | break; |
| 10975 | } |
| 10976 | #endif |
Richard Henderson | 3d21d29 | 2012-09-15 13:20:46 -0700 | [diff] [blame] | 10977 | #ifdef TARGET_NR_gethostname |
| 10978 | case TARGET_NR_gethostname: |
| 10979 | { |
| 10980 | char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0); |
| 10981 | if (name) { |
| 10982 | ret = get_errno(gethostname(name, arg2)); |
| 10983 | unlock_user(name, arg1, arg2); |
| 10984 | } else { |
| 10985 | ret = -TARGET_EFAULT; |
| 10986 | } |
| 10987 | break; |
| 10988 | } |
| 10989 | #endif |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 10990 | #ifdef TARGET_NR_atomic_cmpxchg_32 |
| 10991 | case TARGET_NR_atomic_cmpxchg_32: |
| 10992 | { |
| 10993 | /* should use start_exclusive from main.c */ |
| 10994 | abi_ulong mem_value; |
| 10995 | if (get_user_u32(mem_value, arg6)) { |
| 10996 | target_siginfo_t info; |
| 10997 | info.si_signo = SIGSEGV; |
| 10998 | info.si_errno = 0; |
| 10999 | info.si_code = TARGET_SEGV_MAPERR; |
| 11000 | info._sifields._sigfault._addr = arg6; |
| 11001 | queue_signal((CPUArchState *)cpu_env, info.si_signo, &info); |
| 11002 | ret = 0xdeadbeef; |
| 11003 | |
| 11004 | } |
| 11005 | if (mem_value == arg2) |
| 11006 | put_user_u32(arg1, arg6); |
| 11007 | ret = mem_value; |
| 11008 | break; |
| 11009 | } |
| 11010 | #endif |
| 11011 | #ifdef TARGET_NR_atomic_barrier |
| 11012 | case TARGET_NR_atomic_barrier: |
| 11013 | { |
| 11014 | /* Like the kernel implementation and the qemu arm barrier, no-op this? */ |
Peter Maydell | 3b899ea | 2014-03-12 13:06:01 +0000 | [diff] [blame] | 11015 | ret = 0; |
Riku Voipio | 89aaf1a | 2013-07-24 09:44:26 +0300 | [diff] [blame] | 11016 | break; |
| 11017 | } |
| 11018 | #endif |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11019 | |
| 11020 | #ifdef TARGET_NR_timer_create |
| 11021 | case TARGET_NR_timer_create: |
| 11022 | { |
| 11023 | /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */ |
| 11024 | |
| 11025 | struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11026 | |
| 11027 | int clkid = arg1; |
| 11028 | int timer_index = next_free_host_timer(); |
| 11029 | |
| 11030 | if (timer_index < 0) { |
| 11031 | ret = -TARGET_EAGAIN; |
| 11032 | } else { |
| 11033 | timer_t *phtimer = g_posix_timers + timer_index; |
| 11034 | |
| 11035 | if (arg2) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11036 | phost_sevp = &host_sevp; |
Peter Maydell | c065976 | 2014-08-09 15:42:32 +0100 | [diff] [blame] | 11037 | ret = target_to_host_sigevent(phost_sevp, arg2); |
| 11038 | if (ret != 0) { |
| 11039 | break; |
| 11040 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11041 | } |
| 11042 | |
| 11043 | ret = get_errno(timer_create(clkid, phost_sevp, phtimer)); |
| 11044 | if (ret) { |
| 11045 | phtimer = NULL; |
| 11046 | } else { |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11047 | if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11048 | goto efault; |
| 11049 | } |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11050 | } |
| 11051 | } |
| 11052 | break; |
| 11053 | } |
| 11054 | #endif |
| 11055 | |
| 11056 | #ifdef TARGET_NR_timer_settime |
| 11057 | case TARGET_NR_timer_settime: |
| 11058 | { |
| 11059 | /* args: timer_t timerid, int flags, const struct itimerspec *new_value, |
| 11060 | * struct itimerspec * old_value */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11061 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11062 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11063 | if (timerid < 0) { |
| 11064 | ret = timerid; |
| 11065 | } else if (arg3 == 0) { |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11066 | ret = -TARGET_EINVAL; |
| 11067 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11068 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11069 | struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; |
| 11070 | |
| 11071 | target_to_host_itimerspec(&hspec_new, arg3); |
| 11072 | ret = get_errno( |
| 11073 | timer_settime(htimer, arg2, &hspec_new, &hspec_old)); |
| 11074 | host_to_target_itimerspec(arg2, &hspec_old); |
| 11075 | } |
| 11076 | break; |
| 11077 | } |
| 11078 | #endif |
| 11079 | |
| 11080 | #ifdef TARGET_NR_timer_gettime |
| 11081 | case TARGET_NR_timer_gettime: |
| 11082 | { |
| 11083 | /* args: timer_t timerid, struct itimerspec *curr_value */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11084 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11085 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11086 | if (timerid < 0) { |
| 11087 | ret = timerid; |
| 11088 | } else if (!arg2) { |
| 11089 | ret = -TARGET_EFAULT; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11090 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11091 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11092 | struct itimerspec hspec; |
| 11093 | ret = get_errno(timer_gettime(htimer, &hspec)); |
| 11094 | |
| 11095 | if (host_to_target_itimerspec(arg2, &hspec)) { |
| 11096 | ret = -TARGET_EFAULT; |
| 11097 | } |
| 11098 | } |
| 11099 | break; |
| 11100 | } |
| 11101 | #endif |
| 11102 | |
| 11103 | #ifdef TARGET_NR_timer_getoverrun |
| 11104 | case TARGET_NR_timer_getoverrun: |
| 11105 | { |
| 11106 | /* args: timer_t timerid */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11107 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11108 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11109 | if (timerid < 0) { |
| 11110 | ret = timerid; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11111 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11112 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11113 | ret = get_errno(timer_getoverrun(htimer)); |
| 11114 | } |
Laurent Vivier | e36800c | 2015-10-02 14:48:09 +0200 | [diff] [blame] | 11115 | fd_trans_unregister(ret); |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11116 | break; |
| 11117 | } |
| 11118 | #endif |
| 11119 | |
| 11120 | #ifdef TARGET_NR_timer_delete |
| 11121 | case TARGET_NR_timer_delete: |
| 11122 | { |
| 11123 | /* args: timer_t timerid */ |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11124 | target_timer_t timerid = get_timer_id(arg1); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11125 | |
Alexander Graf | aecc886 | 2014-11-10 21:33:03 +0100 | [diff] [blame] | 11126 | if (timerid < 0) { |
| 11127 | ret = timerid; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11128 | } else { |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11129 | timer_t htimer = g_posix_timers[timerid]; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11130 | ret = get_errno(timer_delete(htimer)); |
Alexander Graf | e52a99f | 2014-08-22 13:56:18 +0200 | [diff] [blame] | 11131 | g_posix_timers[timerid] = 0; |
Erik de Castro Lopo | f4f1e10 | 2013-11-29 18:39:23 +1100 | [diff] [blame] | 11132 | } |
| 11133 | break; |
| 11134 | } |
| 11135 | #endif |
| 11136 | |
Riku Voipio | 5183434 | 2014-06-22 11:25:42 +0100 | [diff] [blame] | 11137 | #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD) |
| 11138 | case TARGET_NR_timerfd_create: |
| 11139 | ret = get_errno(timerfd_create(arg1, |
| 11140 | target_to_host_bitmask(arg2, fcntl_flags_tbl))); |
| 11141 | break; |
| 11142 | #endif |
| 11143 | |
| 11144 | #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD) |
| 11145 | case TARGET_NR_timerfd_gettime: |
| 11146 | { |
| 11147 | struct itimerspec its_curr; |
| 11148 | |
| 11149 | ret = get_errno(timerfd_gettime(arg1, &its_curr)); |
| 11150 | |
| 11151 | if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) { |
| 11152 | goto efault; |
| 11153 | } |
| 11154 | } |
| 11155 | break; |
| 11156 | #endif |
| 11157 | |
| 11158 | #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD) |
| 11159 | case TARGET_NR_timerfd_settime: |
| 11160 | { |
| 11161 | struct itimerspec its_new, its_old, *p_new; |
| 11162 | |
| 11163 | if (arg3) { |
| 11164 | if (target_to_host_itimerspec(&its_new, arg3)) { |
| 11165 | goto efault; |
| 11166 | } |
| 11167 | p_new = &its_new; |
| 11168 | } else { |
| 11169 | p_new = NULL; |
| 11170 | } |
| 11171 | |
| 11172 | ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old)); |
| 11173 | |
| 11174 | if (arg4 && host_to_target_itimerspec(arg4, &its_old)) { |
| 11175 | goto efault; |
| 11176 | } |
| 11177 | } |
| 11178 | break; |
| 11179 | #endif |
| 11180 | |
Paul Burton | ab31cda | 2014-06-22 11:25:43 +0100 | [diff] [blame] | 11181 | #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) |
| 11182 | case TARGET_NR_ioprio_get: |
| 11183 | ret = get_errno(ioprio_get(arg1, arg2)); |
| 11184 | break; |
| 11185 | #endif |
| 11186 | |
| 11187 | #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) |
| 11188 | case TARGET_NR_ioprio_set: |
| 11189 | ret = get_errno(ioprio_set(arg1, arg2, arg3)); |
| 11190 | break; |
| 11191 | #endif |
| 11192 | |
Riku Voipio | 9af5c90 | 2014-08-12 15:58:57 +0300 | [diff] [blame] | 11193 | #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS) |
| 11194 | case TARGET_NR_setns: |
| 11195 | ret = get_errno(setns(arg1, arg2)); |
| 11196 | break; |
| 11197 | #endif |
| 11198 | #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS) |
| 11199 | case TARGET_NR_unshare: |
| 11200 | ret = get_errno(unshare(arg1)); |
| 11201 | break; |
| 11202 | #endif |
| 11203 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11204 | default: |
| 11205 | unimplemented: |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11206 | gemu_log("qemu: Unsupported syscall: %d\n", num); |
ths | 4f2b1fe | 2007-06-21 21:57:12 +0000 | [diff] [blame] | 11207 | #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list) |
bellard | 5cd4393 | 2003-03-29 16:54:36 +0000 | [diff] [blame] | 11208 | unimplemented_nowarn: |
bellard | 80a9d03 | 2005-01-03 23:31:27 +0000 | [diff] [blame] | 11209 | #endif |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 11210 | ret = -TARGET_ENOSYS; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11211 | break; |
| 11212 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11213 | fail: |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 11214 | #ifdef DEBUG |
Blue Swirl | 0bf9e31 | 2009-07-20 17:19:25 +0000 | [diff] [blame] | 11215 | gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret); |
bellard | c573ff6 | 2004-01-04 15:51:36 +0000 | [diff] [blame] | 11216 | #endif |
ths | b92c47c | 2007-11-01 00:07:38 +0000 | [diff] [blame] | 11217 | if(do_strace) |
| 11218 | print_syscall_ret(num, ret); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11219 | return ret; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 11220 | efault: |
| 11221 | ret = -TARGET_EFAULT; |
| 11222 | goto fail; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 11223 | } |