1 /* 2 * Linux syscalls 3 * 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 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 #define _ATFILE_SOURCE 20 #include "qemu/osdep.h" 21 #include "qemu/cutils.h" 22 #include "qemu/path.h" 23 #include "qemu/memfd.h" 24 #include "qemu/queue.h" 25 #include <elf.h> 26 #include <endian.h> 27 #include <grp.h> 28 #include <sys/ipc.h> 29 #include <sys/msg.h> 30 #include <sys/wait.h> 31 #include <sys/mount.h> 32 #include <sys/file.h> 33 #include <sys/fsuid.h> 34 #include <sys/personality.h> 35 #include <sys/prctl.h> 36 #include <sys/resource.h> 37 #include <sys/swap.h> 38 #include <linux/capability.h> 39 #include <sched.h> 40 #include <sys/timex.h> 41 #include <sys/socket.h> 42 #include <linux/sockios.h> 43 #include <sys/un.h> 44 #include <sys/uio.h> 45 #include <poll.h> 46 #include <sys/times.h> 47 #include <sys/shm.h> 48 #include <sys/sem.h> 49 #include <sys/statfs.h> 50 #include <utime.h> 51 #include <sys/sysinfo.h> 52 #include <sys/signalfd.h> 53 //#include <sys/user.h> 54 #include <netinet/in.h> 55 #include <netinet/ip.h> 56 #include <netinet/tcp.h> 57 #include <netinet/udp.h> 58 #include <linux/wireless.h> 59 #include <linux/icmp.h> 60 #include <linux/icmpv6.h> 61 #include <linux/if_tun.h> 62 #include <linux/in6.h> 63 #include <linux/errqueue.h> 64 #include <linux/random.h> 65 #ifdef CONFIG_TIMERFD 66 #include <sys/timerfd.h> 67 #endif 68 #ifdef CONFIG_EVENTFD 69 #include <sys/eventfd.h> 70 #endif 71 #ifdef CONFIG_EPOLL 72 #include <sys/epoll.h> 73 #endif 74 #ifdef CONFIG_ATTR 75 #include "qemu/xattr.h" 76 #endif 77 #ifdef CONFIG_SENDFILE 78 #include <sys/sendfile.h> 79 #endif 80 #ifdef HAVE_SYS_KCOV_H 81 #include <sys/kcov.h> 82 #endif 83 84 #define termios host_termios 85 #define winsize host_winsize 86 #define termio host_termio 87 #define sgttyb host_sgttyb /* same as target */ 88 #define tchars host_tchars /* same as target */ 89 #define ltchars host_ltchars /* same as target */ 90 91 #include <linux/termios.h> 92 #include <linux/unistd.h> 93 #include <linux/cdrom.h> 94 #include <linux/hdreg.h> 95 #include <linux/soundcard.h> 96 #include <linux/kd.h> 97 #include <linux/mtio.h> 98 #include <linux/fs.h> 99 #include <linux/fd.h> 100 #if defined(CONFIG_FIEMAP) 101 #include <linux/fiemap.h> 102 #endif 103 #include <linux/fb.h> 104 #if defined(CONFIG_USBFS) 105 #include <linux/usbdevice_fs.h> 106 #include <linux/usb/ch9.h> 107 #endif 108 #include <linux/vt.h> 109 #include <linux/dm-ioctl.h> 110 #include <linux/reboot.h> 111 #include <linux/route.h> 112 #include <linux/filter.h> 113 #include <linux/blkpg.h> 114 #include <netpacket/packet.h> 115 #include <linux/netlink.h> 116 #include <linux/if_alg.h> 117 #include <linux/rtc.h> 118 #include <sound/asound.h> 119 #ifdef HAVE_BTRFS_H 120 #include <linux/btrfs.h> 121 #endif 122 #ifdef HAVE_DRM_H 123 #include <libdrm/drm.h> 124 #include <libdrm/i915_drm.h> 125 #endif 126 #include "linux_loop.h" 127 #include "uname.h" 128 129 #include "qemu.h" 130 #include "user-internals.h" 131 #include "strace.h" 132 #include "signal-common.h" 133 #include "loader.h" 134 #include "user-mmap.h" 135 #include "user/safe-syscall.h" 136 #include "qemu/guest-random.h" 137 #include "qemu/selfmap.h" 138 #include "user/syscall-trace.h" 139 #include "special-errno.h" 140 #include "qapi/error.h" 141 #include "fd-trans.h" 142 #include "tcg/tcg.h" 143 144 #ifndef CLONE_IO 145 #define CLONE_IO 0x80000000 /* Clone io context */ 146 #endif 147 148 /* We can't directly call the host clone syscall, because this will 149 * badly confuse libc (breaking mutexes, for example). So we must 150 * divide clone flags into: 151 * * flag combinations that look like pthread_create() 152 * * flag combinations that look like fork() 153 * * flags we can implement within QEMU itself 154 * * flags we can't support and will return an error for 155 */ 156 /* For thread creation, all these flags must be present; for 157 * fork, none must be present. 158 */ 159 #define CLONE_THREAD_FLAGS \ 160 (CLONE_VM | CLONE_FS | CLONE_FILES | \ 161 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM) 162 163 /* These flags are ignored: 164 * CLONE_DETACHED is now ignored by the kernel; 165 * CLONE_IO is just an optimisation hint to the I/O scheduler 166 */ 167 #define CLONE_IGNORED_FLAGS \ 168 (CLONE_DETACHED | CLONE_IO) 169 170 /* Flags for fork which we can implement within QEMU itself */ 171 #define CLONE_OPTIONAL_FORK_FLAGS \ 172 (CLONE_SETTLS | CLONE_PARENT_SETTID | \ 173 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID) 174 175 /* Flags for thread creation which we can implement within QEMU itself */ 176 #define CLONE_OPTIONAL_THREAD_FLAGS \ 177 (CLONE_SETTLS | CLONE_PARENT_SETTID | \ 178 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT) 179 180 #define CLONE_INVALID_FORK_FLAGS \ 181 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS)) 182 183 #define CLONE_INVALID_THREAD_FLAGS \ 184 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \ 185 CLONE_IGNORED_FLAGS)) 186 187 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits 188 * have almost all been allocated. We cannot support any of 189 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC, 190 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED. 191 * The checks against the invalid thread masks above will catch these. 192 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.) 193 */ 194 195 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted 196 * once. This exercises the codepaths for restart. 197 */ 198 //#define DEBUG_ERESTARTSYS 199 200 //#include <linux/msdos_fs.h> 201 #define VFAT_IOCTL_READDIR_BOTH \ 202 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2) 203 #define VFAT_IOCTL_READDIR_SHORT \ 204 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2) 205 206 #undef _syscall0 207 #undef _syscall1 208 #undef _syscall2 209 #undef _syscall3 210 #undef _syscall4 211 #undef _syscall5 212 #undef _syscall6 213 214 #define _syscall0(type,name) \ 215 static type name (void) \ 216 { \ 217 return syscall(__NR_##name); \ 218 } 219 220 #define _syscall1(type,name,type1,arg1) \ 221 static type name (type1 arg1) \ 222 { \ 223 return syscall(__NR_##name, arg1); \ 224 } 225 226 #define _syscall2(type,name,type1,arg1,type2,arg2) \ 227 static type name (type1 arg1,type2 arg2) \ 228 { \ 229 return syscall(__NR_##name, arg1, arg2); \ 230 } 231 232 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ 233 static type name (type1 arg1,type2 arg2,type3 arg3) \ 234 { \ 235 return syscall(__NR_##name, arg1, arg2, arg3); \ 236 } 237 238 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ 239 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ 240 { \ 241 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \ 242 } 243 244 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 245 type5,arg5) \ 246 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ 247 { \ 248 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ 249 } 250 251 252 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 253 type5,arg5,type6,arg6) \ 254 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \ 255 type6 arg6) \ 256 { \ 257 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ 258 } 259 260 261 #define __NR_sys_uname __NR_uname 262 #define __NR_sys_getcwd1 __NR_getcwd 263 #define __NR_sys_getdents __NR_getdents 264 #define __NR_sys_getdents64 __NR_getdents64 265 #define __NR_sys_getpriority __NR_getpriority 266 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo 267 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo 268 #define __NR_sys_syslog __NR_syslog 269 #if defined(__NR_futex) 270 # define __NR_sys_futex __NR_futex 271 #endif 272 #if defined(__NR_futex_time64) 273 # define __NR_sys_futex_time64 __NR_futex_time64 274 #endif 275 #define __NR_sys_inotify_init __NR_inotify_init 276 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch 277 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch 278 #define __NR_sys_statx __NR_statx 279 280 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__) 281 #define __NR__llseek __NR_lseek 282 #endif 283 284 /* Newer kernel ports have llseek() instead of _llseek() */ 285 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek) 286 #define TARGET_NR__llseek TARGET_NR_llseek 287 #endif 288 289 /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */ 290 #ifndef TARGET_O_NONBLOCK_MASK 291 #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK 292 #endif 293 294 #define __NR_sys_gettid __NR_gettid 295 _syscall0(int, sys_gettid) 296 297 /* For the 64-bit guest on 32-bit host case we must emulate 298 * getdents using getdents64, because otherwise the host 299 * might hand us back more dirent records than we can fit 300 * into the guest buffer after structure format conversion. 301 * Otherwise we emulate getdents with getdents if the host has it. 302 */ 303 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS 304 #define EMULATE_GETDENTS_WITH_GETDENTS 305 #endif 306 307 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS) 308 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count); 309 #endif 310 #if (defined(TARGET_NR_getdents) && \ 311 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \ 312 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64)) 313 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count); 314 #endif 315 #if defined(TARGET_NR__llseek) && defined(__NR_llseek) 316 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, 317 loff_t *, res, uint, wh); 318 #endif 319 _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo) 320 _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig, 321 siginfo_t *, uinfo) 322 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len) 323 #ifdef __NR_exit_group 324 _syscall1(int,exit_group,int,error_code) 325 #endif 326 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) 327 _syscall1(int,set_tid_address,int *,tidptr) 328 #endif 329 #if defined(__NR_futex) 330 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val, 331 const struct timespec *,timeout,int *,uaddr2,int,val3) 332 #endif 333 #if defined(__NR_futex_time64) 334 _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val, 335 const struct timespec *,timeout,int *,uaddr2,int,val3) 336 #endif 337 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity 338 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len, 339 unsigned long *, user_mask_ptr); 340 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity 341 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len, 342 unsigned long *, user_mask_ptr); 343 /* sched_attr is not defined in glibc */ 344 struct sched_attr { 345 uint32_t size; 346 uint32_t sched_policy; 347 uint64_t sched_flags; 348 int32_t sched_nice; 349 uint32_t sched_priority; 350 uint64_t sched_runtime; 351 uint64_t sched_deadline; 352 uint64_t sched_period; 353 uint32_t sched_util_min; 354 uint32_t sched_util_max; 355 }; 356 #define __NR_sys_sched_getattr __NR_sched_getattr 357 _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr, 358 unsigned int, size, unsigned int, flags); 359 #define __NR_sys_sched_setattr __NR_sched_setattr 360 _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr, 361 unsigned int, flags); 362 #define __NR_sys_sched_getscheduler __NR_sched_getscheduler 363 _syscall1(int, sys_sched_getscheduler, pid_t, pid); 364 #define __NR_sys_sched_setscheduler __NR_sched_setscheduler 365 _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy, 366 const struct sched_param *, param); 367 #define __NR_sys_sched_getparam __NR_sched_getparam 368 _syscall2(int, sys_sched_getparam, pid_t, pid, 369 struct sched_param *, param); 370 #define __NR_sys_sched_setparam __NR_sched_setparam 371 _syscall2(int, sys_sched_setparam, pid_t, pid, 372 const struct sched_param *, param); 373 #define __NR_sys_getcpu __NR_getcpu 374 _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache); 375 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, 376 void *, arg); 377 _syscall2(int, capget, struct __user_cap_header_struct *, header, 378 struct __user_cap_data_struct *, data); 379 _syscall2(int, capset, struct __user_cap_header_struct *, header, 380 struct __user_cap_data_struct *, data); 381 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) 382 _syscall2(int, ioprio_get, int, which, int, who) 383 #endif 384 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) 385 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio) 386 #endif 387 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) 388 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags) 389 #endif 390 391 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp) 392 _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type, 393 unsigned long, idx1, unsigned long, idx2) 394 #endif 395 396 /* 397 * It is assumed that struct statx is architecture independent. 398 */ 399 #if defined(TARGET_NR_statx) && defined(__NR_statx) 400 _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags, 401 unsigned int, mask, struct target_statx *, statxbuf) 402 #endif 403 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier) 404 _syscall2(int, membarrier, int, cmd, int, flags) 405 #endif 406 407 static const bitmask_transtbl fcntl_flags_tbl[] = { 408 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, }, 409 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, }, 410 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, }, 411 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, }, 412 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, }, 413 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, }, 414 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, }, 415 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, }, 416 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, }, 417 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, }, 418 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, }, 419 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, }, 420 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, }, 421 #if defined(O_DIRECT) 422 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, }, 423 #endif 424 #if defined(O_NOATIME) 425 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME }, 426 #endif 427 #if defined(O_CLOEXEC) 428 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC }, 429 #endif 430 #if defined(O_PATH) 431 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH }, 432 #endif 433 #if defined(O_TMPFILE) 434 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE }, 435 #endif 436 /* Don't terminate the list prematurely on 64-bit host+guest. */ 437 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0 438 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, }, 439 #endif 440 { 0, 0, 0, 0 } 441 }; 442 443 _syscall2(int, sys_getcwd1, char *, buf, size_t, size) 444 445 #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64) 446 #if defined(__NR_utimensat) 447 #define __NR_sys_utimensat __NR_utimensat 448 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, 449 const struct timespec *,tsp,int,flags) 450 #else 451 static int sys_utimensat(int dirfd, const char *pathname, 452 const struct timespec times[2], int flags) 453 { 454 errno = ENOSYS; 455 return -1; 456 } 457 #endif 458 #endif /* TARGET_NR_utimensat */ 459 460 #ifdef TARGET_NR_renameat2 461 #if defined(__NR_renameat2) 462 #define __NR_sys_renameat2 __NR_renameat2 463 _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd, 464 const char *, new, unsigned int, flags) 465 #else 466 static int sys_renameat2(int oldfd, const char *old, 467 int newfd, const char *new, int flags) 468 { 469 if (flags == 0) { 470 return renameat(oldfd, old, newfd, new); 471 } 472 errno = ENOSYS; 473 return -1; 474 } 475 #endif 476 #endif /* TARGET_NR_renameat2 */ 477 478 #ifdef CONFIG_INOTIFY 479 #include <sys/inotify.h> 480 481 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init) 482 static int sys_inotify_init(void) 483 { 484 return (inotify_init()); 485 } 486 #endif 487 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) 488 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask) 489 { 490 return (inotify_add_watch(fd, pathname, mask)); 491 } 492 #endif 493 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch) 494 static int sys_inotify_rm_watch(int fd, int32_t wd) 495 { 496 return (inotify_rm_watch(fd, wd)); 497 } 498 #endif 499 #ifdef CONFIG_INOTIFY1 500 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1) 501 static int sys_inotify_init1(int flags) 502 { 503 return (inotify_init1(flags)); 504 } 505 #endif 506 #endif 507 #else 508 /* Userspace can usually survive runtime without inotify */ 509 #undef TARGET_NR_inotify_init 510 #undef TARGET_NR_inotify_init1 511 #undef TARGET_NR_inotify_add_watch 512 #undef TARGET_NR_inotify_rm_watch 513 #endif /* CONFIG_INOTIFY */ 514 515 #if defined(TARGET_NR_prlimit64) 516 #ifndef __NR_prlimit64 517 # define __NR_prlimit64 -1 518 #endif 519 #define __NR_sys_prlimit64 __NR_prlimit64 520 /* The glibc rlimit structure may not be that used by the underlying syscall */ 521 struct host_rlimit64 { 522 uint64_t rlim_cur; 523 uint64_t rlim_max; 524 }; 525 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource, 526 const struct host_rlimit64 *, new_limit, 527 struct host_rlimit64 *, old_limit) 528 #endif 529 530 531 #if defined(TARGET_NR_timer_create) 532 /* Maximum of 32 active POSIX timers allowed at any one time. */ 533 static timer_t g_posix_timers[32] = { 0, } ; 534 535 static inline int next_free_host_timer(void) 536 { 537 int k ; 538 /* FIXME: Does finding the next free slot require a lock? */ 539 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) { 540 if (g_posix_timers[k] == 0) { 541 g_posix_timers[k] = (timer_t) 1; 542 return k; 543 } 544 } 545 return -1; 546 } 547 #endif 548 549 static inline int host_to_target_errno(int host_errno) 550 { 551 switch (host_errno) { 552 #define E(X) case X: return TARGET_##X; 553 #include "errnos.c.inc" 554 #undef E 555 default: 556 return host_errno; 557 } 558 } 559 560 static inline int target_to_host_errno(int target_errno) 561 { 562 switch (target_errno) { 563 #define E(X) case TARGET_##X: return X; 564 #include "errnos.c.inc" 565 #undef E 566 default: 567 return target_errno; 568 } 569 } 570 571 static inline abi_long get_errno(abi_long ret) 572 { 573 if (ret == -1) 574 return -host_to_target_errno(errno); 575 else 576 return ret; 577 } 578 579 const char *target_strerror(int err) 580 { 581 if (err == QEMU_ERESTARTSYS) { 582 return "To be restarted"; 583 } 584 if (err == QEMU_ESIGRETURN) { 585 return "Successful exit from sigreturn"; 586 } 587 588 return strerror(target_to_host_errno(err)); 589 } 590 591 static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize) 592 { 593 int i; 594 uint8_t b; 595 if (usize <= ksize) { 596 return 1; 597 } 598 for (i = ksize; i < usize; i++) { 599 if (get_user_u8(b, addr + i)) { 600 return -TARGET_EFAULT; 601 } 602 if (b != 0) { 603 return 0; 604 } 605 } 606 return 1; 607 } 608 609 #define safe_syscall0(type, name) \ 610 static type safe_##name(void) \ 611 { \ 612 return safe_syscall(__NR_##name); \ 613 } 614 615 #define safe_syscall1(type, name, type1, arg1) \ 616 static type safe_##name(type1 arg1) \ 617 { \ 618 return safe_syscall(__NR_##name, arg1); \ 619 } 620 621 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \ 622 static type safe_##name(type1 arg1, type2 arg2) \ 623 { \ 624 return safe_syscall(__NR_##name, arg1, arg2); \ 625 } 626 627 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ 628 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \ 629 { \ 630 return safe_syscall(__NR_##name, arg1, arg2, arg3); \ 631 } 632 633 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \ 634 type4, arg4) \ 635 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ 636 { \ 637 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \ 638 } 639 640 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \ 641 type4, arg4, type5, arg5) \ 642 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 643 type5 arg5) \ 644 { \ 645 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ 646 } 647 648 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \ 649 type4, arg4, type5, arg5, type6, arg6) \ 650 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 651 type5 arg5, type6 arg6) \ 652 { \ 653 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ 654 } 655 656 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count) 657 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count) 658 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \ 659 int, flags, mode_t, mode) 660 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid) 661 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \ 662 struct rusage *, rusage) 663 #endif 664 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ 665 int, options, struct rusage *, rusage) 666 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) 667 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ 668 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) 669 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ 670 fd_set *, exceptfds, struct timespec *, timeout, void *, sig) 671 #endif 672 #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) 673 safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, 674 struct timespec *, tsp, const sigset_t *, sigmask, 675 size_t, sigsetsize) 676 #endif 677 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events, 678 int, maxevents, int, timeout, const sigset_t *, sigmask, 679 size_t, sigsetsize) 680 #if defined(__NR_futex) 681 safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \ 682 const struct timespec *,timeout,int *,uaddr2,int,val3) 683 #endif 684 #if defined(__NR_futex_time64) 685 safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \ 686 const struct timespec *,timeout,int *,uaddr2,int,val3) 687 #endif 688 safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize) 689 safe_syscall2(int, kill, pid_t, pid, int, sig) 690 safe_syscall2(int, tkill, int, tid, int, sig) 691 safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig) 692 safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt) 693 safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt) 694 safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, 695 unsigned long, pos_l, unsigned long, pos_h) 696 safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, 697 unsigned long, pos_l, unsigned long, pos_h) 698 safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr, 699 socklen_t, addrlen) 700 safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len, 701 int, flags, const struct sockaddr *, addr, socklen_t, addrlen) 702 safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len, 703 int, flags, struct sockaddr *, addr, socklen_t *, addrlen) 704 safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags) 705 safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags) 706 safe_syscall2(int, flock, int, fd, int, operation) 707 #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64) 708 safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo, 709 const struct timespec *, uts, size_t, sigsetsize) 710 #endif 711 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len, 712 int, flags) 713 #if defined(TARGET_NR_nanosleep) 714 safe_syscall2(int, nanosleep, const struct timespec *, req, 715 struct timespec *, rem) 716 #endif 717 #if defined(TARGET_NR_clock_nanosleep) || \ 718 defined(TARGET_NR_clock_nanosleep_time64) 719 safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags, 720 const struct timespec *, req, struct timespec *, rem) 721 #endif 722 #ifdef __NR_ipc 723 #ifdef __s390x__ 724 safe_syscall5(int, ipc, int, call, long, first, long, second, long, third, 725 void *, ptr) 726 #else 727 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third, 728 void *, ptr, long, fifth) 729 #endif 730 #endif 731 #ifdef __NR_msgsnd 732 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz, 733 int, flags) 734 #endif 735 #ifdef __NR_msgrcv 736 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz, 737 long, msgtype, int, flags) 738 #endif 739 #ifdef __NR_semtimedop 740 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops, 741 unsigned, nsops, const struct timespec *, timeout) 742 #endif 743 #if defined(TARGET_NR_mq_timedsend) || \ 744 defined(TARGET_NR_mq_timedsend_time64) 745 safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr, 746 size_t, len, unsigned, prio, const struct timespec *, timeout) 747 #endif 748 #if defined(TARGET_NR_mq_timedreceive) || \ 749 defined(TARGET_NR_mq_timedreceive_time64) 750 safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr, 751 size_t, len, unsigned *, prio, const struct timespec *, timeout) 752 #endif 753 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range) 754 safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff, 755 int, outfd, loff_t *, poutoff, size_t, length, 756 unsigned int, flags) 757 #endif 758 759 /* We do ioctl like this rather than via safe_syscall3 to preserve the 760 * "third argument might be integer or pointer or not present" behaviour of 761 * the libc function. 762 */ 763 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__) 764 /* Similarly for fcntl. Note that callers must always: 765 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK 766 * use the flock64 struct rather than unsuffixed flock 767 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts. 768 */ 769 #ifdef __NR_fcntl64 770 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__) 771 #else 772 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__) 773 #endif 774 775 static inline int host_to_target_sock_type(int host_type) 776 { 777 int target_type; 778 779 switch (host_type & 0xf /* SOCK_TYPE_MASK */) { 780 case SOCK_DGRAM: 781 target_type = TARGET_SOCK_DGRAM; 782 break; 783 case SOCK_STREAM: 784 target_type = TARGET_SOCK_STREAM; 785 break; 786 default: 787 target_type = host_type & 0xf /* SOCK_TYPE_MASK */; 788 break; 789 } 790 791 #if defined(SOCK_CLOEXEC) 792 if (host_type & SOCK_CLOEXEC) { 793 target_type |= TARGET_SOCK_CLOEXEC; 794 } 795 #endif 796 797 #if defined(SOCK_NONBLOCK) 798 if (host_type & SOCK_NONBLOCK) { 799 target_type |= TARGET_SOCK_NONBLOCK; 800 } 801 #endif 802 803 return target_type; 804 } 805 806 static abi_ulong target_brk; 807 static abi_ulong target_original_brk; 808 static abi_ulong brk_page; 809 810 void target_set_brk(abi_ulong new_brk) 811 { 812 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk); 813 brk_page = HOST_PAGE_ALIGN(target_brk); 814 } 815 816 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0) 817 #define DEBUGF_BRK(message, args...) 818 819 /* do_brk() must return target values and target errnos. */ 820 abi_long do_brk(abi_ulong new_brk) 821 { 822 abi_long mapped_addr; 823 abi_ulong new_alloc_size; 824 825 /* brk pointers are always untagged */ 826 827 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk); 828 829 if (!new_brk) { 830 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk); 831 return target_brk; 832 } 833 if (new_brk < target_original_brk) { 834 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n", 835 target_brk); 836 return target_brk; 837 } 838 839 /* If the new brk is less than the highest page reserved to the 840 * target heap allocation, set it and we're almost done... */ 841 if (new_brk <= brk_page) { 842 /* Heap contents are initialized to zero, as for anonymous 843 * mapped pages. */ 844 if (new_brk > target_brk) { 845 memset(g2h_untagged(target_brk), 0, new_brk - target_brk); 846 } 847 target_brk = new_brk; 848 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk); 849 return target_brk; 850 } 851 852 /* We need to allocate more memory after the brk... Note that 853 * we don't use MAP_FIXED because that will map over the top of 854 * any existing mapping (like the one with the host libc or qemu 855 * itself); instead we treat "mapped but at wrong address" as 856 * a failure and unmap again. 857 */ 858 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page); 859 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, 860 PROT_READ|PROT_WRITE, 861 MAP_ANON|MAP_PRIVATE, 0, 0)); 862 863 if (mapped_addr == brk_page) { 864 /* Heap contents are initialized to zero, as for anonymous 865 * mapped pages. Technically the new pages are already 866 * initialized to zero since they *are* anonymous mapped 867 * pages, however we have to take care with the contents that 868 * come from the remaining part of the previous page: it may 869 * contains garbage data due to a previous heap usage (grown 870 * then shrunken). */ 871 memset(g2h_untagged(target_brk), 0, brk_page - target_brk); 872 873 target_brk = new_brk; 874 brk_page = HOST_PAGE_ALIGN(target_brk); 875 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n", 876 target_brk); 877 return target_brk; 878 } else if (mapped_addr != -1) { 879 /* Mapped but at wrong address, meaning there wasn't actually 880 * enough space for this brk. 881 */ 882 target_munmap(mapped_addr, new_alloc_size); 883 mapped_addr = -1; 884 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk); 885 } 886 else { 887 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk); 888 } 889 890 #if defined(TARGET_ALPHA) 891 /* We (partially) emulate OSF/1 on Alpha, which requires we 892 return a proper errno, not an unchanged brk value. */ 893 return -TARGET_ENOMEM; 894 #endif 895 /* For everything else, return the previous break. */ 896 return target_brk; 897 } 898 899 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ 900 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) 901 static inline abi_long copy_from_user_fdset(fd_set *fds, 902 abi_ulong target_fds_addr, 903 int n) 904 { 905 int i, nw, j, k; 906 abi_ulong b, *target_fds; 907 908 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); 909 if (!(target_fds = lock_user(VERIFY_READ, 910 target_fds_addr, 911 sizeof(abi_ulong) * nw, 912 1))) 913 return -TARGET_EFAULT; 914 915 FD_ZERO(fds); 916 k = 0; 917 for (i = 0; i < nw; i++) { 918 /* grab the abi_ulong */ 919 __get_user(b, &target_fds[i]); 920 for (j = 0; j < TARGET_ABI_BITS; j++) { 921 /* check the bit inside the abi_ulong */ 922 if ((b >> j) & 1) 923 FD_SET(k, fds); 924 k++; 925 } 926 } 927 928 unlock_user(target_fds, target_fds_addr, 0); 929 930 return 0; 931 } 932 933 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr, 934 abi_ulong target_fds_addr, 935 int n) 936 { 937 if (target_fds_addr) { 938 if (copy_from_user_fdset(fds, target_fds_addr, n)) 939 return -TARGET_EFAULT; 940 *fds_ptr = fds; 941 } else { 942 *fds_ptr = NULL; 943 } 944 return 0; 945 } 946 947 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr, 948 const fd_set *fds, 949 int n) 950 { 951 int i, nw, j, k; 952 abi_long v; 953 abi_ulong *target_fds; 954 955 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS); 956 if (!(target_fds = lock_user(VERIFY_WRITE, 957 target_fds_addr, 958 sizeof(abi_ulong) * nw, 959 0))) 960 return -TARGET_EFAULT; 961 962 k = 0; 963 for (i = 0; i < nw; i++) { 964 v = 0; 965 for (j = 0; j < TARGET_ABI_BITS; j++) { 966 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j); 967 k++; 968 } 969 __put_user(v, &target_fds[i]); 970 } 971 972 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw); 973 974 return 0; 975 } 976 #endif 977 978 #if defined(__alpha__) 979 #define HOST_HZ 1024 980 #else 981 #define HOST_HZ 100 982 #endif 983 984 static inline abi_long host_to_target_clock_t(long ticks) 985 { 986 #if HOST_HZ == TARGET_HZ 987 return ticks; 988 #else 989 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ; 990 #endif 991 } 992 993 static inline abi_long host_to_target_rusage(abi_ulong target_addr, 994 const struct rusage *rusage) 995 { 996 struct target_rusage *target_rusage; 997 998 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) 999 return -TARGET_EFAULT; 1000 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec); 1001 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec); 1002 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec); 1003 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec); 1004 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss); 1005 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss); 1006 target_rusage->ru_idrss = tswapal(rusage->ru_idrss); 1007 target_rusage->ru_isrss = tswapal(rusage->ru_isrss); 1008 target_rusage->ru_minflt = tswapal(rusage->ru_minflt); 1009 target_rusage->ru_majflt = tswapal(rusage->ru_majflt); 1010 target_rusage->ru_nswap = tswapal(rusage->ru_nswap); 1011 target_rusage->ru_inblock = tswapal(rusage->ru_inblock); 1012 target_rusage->ru_oublock = tswapal(rusage->ru_oublock); 1013 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd); 1014 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv); 1015 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals); 1016 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw); 1017 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw); 1018 unlock_user_struct(target_rusage, target_addr, 1); 1019 1020 return 0; 1021 } 1022 1023 #ifdef TARGET_NR_setrlimit 1024 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim) 1025 { 1026 abi_ulong target_rlim_swap; 1027 rlim_t result; 1028 1029 target_rlim_swap = tswapal(target_rlim); 1030 if (target_rlim_swap == TARGET_RLIM_INFINITY) 1031 return RLIM_INFINITY; 1032 1033 result = target_rlim_swap; 1034 if (target_rlim_swap != (rlim_t)result) 1035 return RLIM_INFINITY; 1036 1037 return result; 1038 } 1039 #endif 1040 1041 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit) 1042 static inline abi_ulong host_to_target_rlim(rlim_t rlim) 1043 { 1044 abi_ulong target_rlim_swap; 1045 abi_ulong result; 1046 1047 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim) 1048 target_rlim_swap = TARGET_RLIM_INFINITY; 1049 else 1050 target_rlim_swap = rlim; 1051 result = tswapal(target_rlim_swap); 1052 1053 return result; 1054 } 1055 #endif 1056 1057 static inline int target_to_host_resource(int code) 1058 { 1059 switch (code) { 1060 case TARGET_RLIMIT_AS: 1061 return RLIMIT_AS; 1062 case TARGET_RLIMIT_CORE: 1063 return RLIMIT_CORE; 1064 case TARGET_RLIMIT_CPU: 1065 return RLIMIT_CPU; 1066 case TARGET_RLIMIT_DATA: 1067 return RLIMIT_DATA; 1068 case TARGET_RLIMIT_FSIZE: 1069 return RLIMIT_FSIZE; 1070 case TARGET_RLIMIT_LOCKS: 1071 return RLIMIT_LOCKS; 1072 case TARGET_RLIMIT_MEMLOCK: 1073 return RLIMIT_MEMLOCK; 1074 case TARGET_RLIMIT_MSGQUEUE: 1075 return RLIMIT_MSGQUEUE; 1076 case TARGET_RLIMIT_NICE: 1077 return RLIMIT_NICE; 1078 case TARGET_RLIMIT_NOFILE: 1079 return RLIMIT_NOFILE; 1080 case TARGET_RLIMIT_NPROC: 1081 return RLIMIT_NPROC; 1082 case TARGET_RLIMIT_RSS: 1083 return RLIMIT_RSS; 1084 case TARGET_RLIMIT_RTPRIO: 1085 return RLIMIT_RTPRIO; 1086 case TARGET_RLIMIT_SIGPENDING: 1087 return RLIMIT_SIGPENDING; 1088 case TARGET_RLIMIT_STACK: 1089 return RLIMIT_STACK; 1090 default: 1091 return code; 1092 } 1093 } 1094 1095 static inline abi_long copy_from_user_timeval(struct timeval *tv, 1096 abi_ulong target_tv_addr) 1097 { 1098 struct target_timeval *target_tv; 1099 1100 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) { 1101 return -TARGET_EFAULT; 1102 } 1103 1104 __get_user(tv->tv_sec, &target_tv->tv_sec); 1105 __get_user(tv->tv_usec, &target_tv->tv_usec); 1106 1107 unlock_user_struct(target_tv, target_tv_addr, 0); 1108 1109 return 0; 1110 } 1111 1112 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr, 1113 const struct timeval *tv) 1114 { 1115 struct target_timeval *target_tv; 1116 1117 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { 1118 return -TARGET_EFAULT; 1119 } 1120 1121 __put_user(tv->tv_sec, &target_tv->tv_sec); 1122 __put_user(tv->tv_usec, &target_tv->tv_usec); 1123 1124 unlock_user_struct(target_tv, target_tv_addr, 1); 1125 1126 return 0; 1127 } 1128 1129 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME) 1130 static inline abi_long copy_from_user_timeval64(struct timeval *tv, 1131 abi_ulong target_tv_addr) 1132 { 1133 struct target__kernel_sock_timeval *target_tv; 1134 1135 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) { 1136 return -TARGET_EFAULT; 1137 } 1138 1139 __get_user(tv->tv_sec, &target_tv->tv_sec); 1140 __get_user(tv->tv_usec, &target_tv->tv_usec); 1141 1142 unlock_user_struct(target_tv, target_tv_addr, 0); 1143 1144 return 0; 1145 } 1146 #endif 1147 1148 static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr, 1149 const struct timeval *tv) 1150 { 1151 struct target__kernel_sock_timeval *target_tv; 1152 1153 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { 1154 return -TARGET_EFAULT; 1155 } 1156 1157 __put_user(tv->tv_sec, &target_tv->tv_sec); 1158 __put_user(tv->tv_usec, &target_tv->tv_usec); 1159 1160 unlock_user_struct(target_tv, target_tv_addr, 1); 1161 1162 return 0; 1163 } 1164 1165 #if defined(TARGET_NR_futex) || \ 1166 defined(TARGET_NR_rt_sigtimedwait) || \ 1167 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \ 1168 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \ 1169 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \ 1170 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \ 1171 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \ 1172 defined(TARGET_NR_timer_settime) || \ 1173 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)) 1174 static inline abi_long target_to_host_timespec(struct timespec *host_ts, 1175 abi_ulong target_addr) 1176 { 1177 struct target_timespec *target_ts; 1178 1179 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { 1180 return -TARGET_EFAULT; 1181 } 1182 __get_user(host_ts->tv_sec, &target_ts->tv_sec); 1183 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); 1184 unlock_user_struct(target_ts, target_addr, 0); 1185 return 0; 1186 } 1187 #endif 1188 1189 #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ 1190 defined(TARGET_NR_timer_settime64) || \ 1191 defined(TARGET_NR_mq_timedsend_time64) || \ 1192 defined(TARGET_NR_mq_timedreceive_time64) || \ 1193 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \ 1194 defined(TARGET_NR_clock_nanosleep_time64) || \ 1195 defined(TARGET_NR_rt_sigtimedwait_time64) || \ 1196 defined(TARGET_NR_utimensat) || \ 1197 defined(TARGET_NR_utimensat_time64) || \ 1198 defined(TARGET_NR_semtimedop_time64) || \ 1199 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) 1200 static inline abi_long target_to_host_timespec64(struct timespec *host_ts, 1201 abi_ulong target_addr) 1202 { 1203 struct target__kernel_timespec *target_ts; 1204 1205 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { 1206 return -TARGET_EFAULT; 1207 } 1208 __get_user(host_ts->tv_sec, &target_ts->tv_sec); 1209 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); 1210 /* in 32bit mode, this drops the padding */ 1211 host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec; 1212 unlock_user_struct(target_ts, target_addr, 0); 1213 return 0; 1214 } 1215 #endif 1216 1217 static inline abi_long host_to_target_timespec(abi_ulong target_addr, 1218 struct timespec *host_ts) 1219 { 1220 struct target_timespec *target_ts; 1221 1222 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { 1223 return -TARGET_EFAULT; 1224 } 1225 __put_user(host_ts->tv_sec, &target_ts->tv_sec); 1226 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); 1227 unlock_user_struct(target_ts, target_addr, 1); 1228 return 0; 1229 } 1230 1231 static inline abi_long host_to_target_timespec64(abi_ulong target_addr, 1232 struct timespec *host_ts) 1233 { 1234 struct target__kernel_timespec *target_ts; 1235 1236 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { 1237 return -TARGET_EFAULT; 1238 } 1239 __put_user(host_ts->tv_sec, &target_ts->tv_sec); 1240 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); 1241 unlock_user_struct(target_ts, target_addr, 1); 1242 return 0; 1243 } 1244 1245 #if defined(TARGET_NR_gettimeofday) 1246 static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr, 1247 struct timezone *tz) 1248 { 1249 struct target_timezone *target_tz; 1250 1251 if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) { 1252 return -TARGET_EFAULT; 1253 } 1254 1255 __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); 1256 __put_user(tz->tz_dsttime, &target_tz->tz_dsttime); 1257 1258 unlock_user_struct(target_tz, target_tz_addr, 1); 1259 1260 return 0; 1261 } 1262 #endif 1263 1264 #if defined(TARGET_NR_settimeofday) 1265 static inline abi_long copy_from_user_timezone(struct timezone *tz, 1266 abi_ulong target_tz_addr) 1267 { 1268 struct target_timezone *target_tz; 1269 1270 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) { 1271 return -TARGET_EFAULT; 1272 } 1273 1274 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest); 1275 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime); 1276 1277 unlock_user_struct(target_tz, target_tz_addr, 0); 1278 1279 return 0; 1280 } 1281 #endif 1282 1283 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) 1284 #include <mqueue.h> 1285 1286 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr, 1287 abi_ulong target_mq_attr_addr) 1288 { 1289 struct target_mq_attr *target_mq_attr; 1290 1291 if (!lock_user_struct(VERIFY_READ, target_mq_attr, 1292 target_mq_attr_addr, 1)) 1293 return -TARGET_EFAULT; 1294 1295 __get_user(attr->mq_flags, &target_mq_attr->mq_flags); 1296 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); 1297 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); 1298 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); 1299 1300 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0); 1301 1302 return 0; 1303 } 1304 1305 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr, 1306 const struct mq_attr *attr) 1307 { 1308 struct target_mq_attr *target_mq_attr; 1309 1310 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr, 1311 target_mq_attr_addr, 0)) 1312 return -TARGET_EFAULT; 1313 1314 __put_user(attr->mq_flags, &target_mq_attr->mq_flags); 1315 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg); 1316 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize); 1317 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs); 1318 1319 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1); 1320 1321 return 0; 1322 } 1323 #endif 1324 1325 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) 1326 /* do_select() must return target values and target errnos. */ 1327 static abi_long do_select(int n, 1328 abi_ulong rfd_addr, abi_ulong wfd_addr, 1329 abi_ulong efd_addr, abi_ulong target_tv_addr) 1330 { 1331 fd_set rfds, wfds, efds; 1332 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; 1333 struct timeval tv; 1334 struct timespec ts, *ts_ptr; 1335 abi_long ret; 1336 1337 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); 1338 if (ret) { 1339 return ret; 1340 } 1341 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); 1342 if (ret) { 1343 return ret; 1344 } 1345 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); 1346 if (ret) { 1347 return ret; 1348 } 1349 1350 if (target_tv_addr) { 1351 if (copy_from_user_timeval(&tv, target_tv_addr)) 1352 return -TARGET_EFAULT; 1353 ts.tv_sec = tv.tv_sec; 1354 ts.tv_nsec = tv.tv_usec * 1000; 1355 ts_ptr = &ts; 1356 } else { 1357 ts_ptr = NULL; 1358 } 1359 1360 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, 1361 ts_ptr, NULL)); 1362 1363 if (!is_error(ret)) { 1364 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) 1365 return -TARGET_EFAULT; 1366 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) 1367 return -TARGET_EFAULT; 1368 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) 1369 return -TARGET_EFAULT; 1370 1371 if (target_tv_addr) { 1372 tv.tv_sec = ts.tv_sec; 1373 tv.tv_usec = ts.tv_nsec / 1000; 1374 if (copy_to_user_timeval(target_tv_addr, &tv)) { 1375 return -TARGET_EFAULT; 1376 } 1377 } 1378 } 1379 1380 return ret; 1381 } 1382 1383 #if defined(TARGET_WANT_OLD_SYS_SELECT) 1384 static abi_long do_old_select(abi_ulong arg1) 1385 { 1386 struct target_sel_arg_struct *sel; 1387 abi_ulong inp, outp, exp, tvp; 1388 long nsel; 1389 1390 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) { 1391 return -TARGET_EFAULT; 1392 } 1393 1394 nsel = tswapal(sel->n); 1395 inp = tswapal(sel->inp); 1396 outp = tswapal(sel->outp); 1397 exp = tswapal(sel->exp); 1398 tvp = tswapal(sel->tvp); 1399 1400 unlock_user_struct(sel, arg1, 0); 1401 1402 return do_select(nsel, inp, outp, exp, tvp); 1403 } 1404 #endif 1405 #endif 1406 1407 #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) 1408 static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, 1409 abi_long arg4, abi_long arg5, abi_long arg6, 1410 bool time64) 1411 { 1412 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; 1413 fd_set rfds, wfds, efds; 1414 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; 1415 struct timespec ts, *ts_ptr; 1416 abi_long ret; 1417 1418 /* 1419 * The 6th arg is actually two args smashed together, 1420 * so we cannot use the C library. 1421 */ 1422 sigset_t set; 1423 struct { 1424 sigset_t *set; 1425 size_t size; 1426 } sig, *sig_ptr; 1427 1428 abi_ulong arg_sigset, arg_sigsize, *arg7; 1429 target_sigset_t *target_sigset; 1430 1431 n = arg1; 1432 rfd_addr = arg2; 1433 wfd_addr = arg3; 1434 efd_addr = arg4; 1435 ts_addr = arg5; 1436 1437 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); 1438 if (ret) { 1439 return ret; 1440 } 1441 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); 1442 if (ret) { 1443 return ret; 1444 } 1445 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); 1446 if (ret) { 1447 return ret; 1448 } 1449 1450 /* 1451 * This takes a timespec, and not a timeval, so we cannot 1452 * use the do_select() helper ... 1453 */ 1454 if (ts_addr) { 1455 if (time64) { 1456 if (target_to_host_timespec64(&ts, ts_addr)) { 1457 return -TARGET_EFAULT; 1458 } 1459 } else { 1460 if (target_to_host_timespec(&ts, ts_addr)) { 1461 return -TARGET_EFAULT; 1462 } 1463 } 1464 ts_ptr = &ts; 1465 } else { 1466 ts_ptr = NULL; 1467 } 1468 1469 /* Extract the two packed args for the sigset */ 1470 if (arg6) { 1471 sig_ptr = &sig; 1472 sig.size = SIGSET_T_SIZE; 1473 1474 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); 1475 if (!arg7) { 1476 return -TARGET_EFAULT; 1477 } 1478 arg_sigset = tswapal(arg7[0]); 1479 arg_sigsize = tswapal(arg7[1]); 1480 unlock_user(arg7, arg6, 0); 1481 1482 if (arg_sigset) { 1483 sig.set = &set; 1484 if (arg_sigsize != sizeof(*target_sigset)) { 1485 /* Like the kernel, we enforce correct size sigsets */ 1486 return -TARGET_EINVAL; 1487 } 1488 target_sigset = lock_user(VERIFY_READ, arg_sigset, 1489 sizeof(*target_sigset), 1); 1490 if (!target_sigset) { 1491 return -TARGET_EFAULT; 1492 } 1493 target_to_host_sigset(&set, target_sigset); 1494 unlock_user(target_sigset, arg_sigset, 0); 1495 } else { 1496 sig.set = NULL; 1497 } 1498 } else { 1499 sig_ptr = NULL; 1500 } 1501 1502 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, 1503 ts_ptr, sig_ptr)); 1504 1505 if (!is_error(ret)) { 1506 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { 1507 return -TARGET_EFAULT; 1508 } 1509 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { 1510 return -TARGET_EFAULT; 1511 } 1512 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { 1513 return -TARGET_EFAULT; 1514 } 1515 if (time64) { 1516 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { 1517 return -TARGET_EFAULT; 1518 } 1519 } else { 1520 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { 1521 return -TARGET_EFAULT; 1522 } 1523 } 1524 } 1525 return ret; 1526 } 1527 #endif 1528 1529 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ 1530 defined(TARGET_NR_ppoll_time64) 1531 static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, 1532 abi_long arg4, abi_long arg5, bool ppoll, bool time64) 1533 { 1534 struct target_pollfd *target_pfd; 1535 unsigned int nfds = arg2; 1536 struct pollfd *pfd; 1537 unsigned int i; 1538 abi_long ret; 1539 1540 pfd = NULL; 1541 target_pfd = NULL; 1542 if (nfds) { 1543 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { 1544 return -TARGET_EINVAL; 1545 } 1546 target_pfd = lock_user(VERIFY_WRITE, arg1, 1547 sizeof(struct target_pollfd) * nfds, 1); 1548 if (!target_pfd) { 1549 return -TARGET_EFAULT; 1550 } 1551 1552 pfd = alloca(sizeof(struct pollfd) * nfds); 1553 for (i = 0; i < nfds; i++) { 1554 pfd[i].fd = tswap32(target_pfd[i].fd); 1555 pfd[i].events = tswap16(target_pfd[i].events); 1556 } 1557 } 1558 if (ppoll) { 1559 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; 1560 target_sigset_t *target_set; 1561 sigset_t _set, *set = &_set; 1562 1563 if (arg3) { 1564 if (time64) { 1565 if (target_to_host_timespec64(timeout_ts, arg3)) { 1566 unlock_user(target_pfd, arg1, 0); 1567 return -TARGET_EFAULT; 1568 } 1569 } else { 1570 if (target_to_host_timespec(timeout_ts, arg3)) { 1571 unlock_user(target_pfd, arg1, 0); 1572 return -TARGET_EFAULT; 1573 } 1574 } 1575 } else { 1576 timeout_ts = NULL; 1577 } 1578 1579 if (arg4) { 1580 if (arg5 != sizeof(target_sigset_t)) { 1581 unlock_user(target_pfd, arg1, 0); 1582 return -TARGET_EINVAL; 1583 } 1584 1585 target_set = lock_user(VERIFY_READ, arg4, 1586 sizeof(target_sigset_t), 1); 1587 if (!target_set) { 1588 unlock_user(target_pfd, arg1, 0); 1589 return -TARGET_EFAULT; 1590 } 1591 target_to_host_sigset(set, target_set); 1592 } else { 1593 set = NULL; 1594 } 1595 1596 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, 1597 set, SIGSET_T_SIZE)); 1598 1599 if (!is_error(ret) && arg3) { 1600 if (time64) { 1601 if (host_to_target_timespec64(arg3, timeout_ts)) { 1602 return -TARGET_EFAULT; 1603 } 1604 } else { 1605 if (host_to_target_timespec(arg3, timeout_ts)) { 1606 return -TARGET_EFAULT; 1607 } 1608 } 1609 } 1610 if (arg4) { 1611 unlock_user(target_set, arg4, 0); 1612 } 1613 } else { 1614 struct timespec ts, *pts; 1615 1616 if (arg3 >= 0) { 1617 /* Convert ms to secs, ns */ 1618 ts.tv_sec = arg3 / 1000; 1619 ts.tv_nsec = (arg3 % 1000) * 1000000LL; 1620 pts = &ts; 1621 } else { 1622 /* -ve poll() timeout means "infinite" */ 1623 pts = NULL; 1624 } 1625 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); 1626 } 1627 1628 if (!is_error(ret)) { 1629 for (i = 0; i < nfds; i++) { 1630 target_pfd[i].revents = tswap16(pfd[i].revents); 1631 } 1632 } 1633 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); 1634 return ret; 1635 } 1636 #endif 1637 1638 static abi_long do_pipe2(int host_pipe[], int flags) 1639 { 1640 #ifdef CONFIG_PIPE2 1641 return pipe2(host_pipe, flags); 1642 #else 1643 return -ENOSYS; 1644 #endif 1645 } 1646 1647 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, 1648 int flags, int is_pipe2) 1649 { 1650 int host_pipe[2]; 1651 abi_long ret; 1652 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe); 1653 1654 if (is_error(ret)) 1655 return get_errno(ret); 1656 1657 /* Several targets have special calling conventions for the original 1658 pipe syscall, but didn't replicate this into the pipe2 syscall. */ 1659 if (!is_pipe2) { 1660 #if defined(TARGET_ALPHA) 1661 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1]; 1662 return host_pipe[0]; 1663 #elif defined(TARGET_MIPS) 1664 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1]; 1665 return host_pipe[0]; 1666 #elif defined(TARGET_SH4) 1667 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1]; 1668 return host_pipe[0]; 1669 #elif defined(TARGET_SPARC) 1670 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1]; 1671 return host_pipe[0]; 1672 #endif 1673 } 1674 1675 if (put_user_s32(host_pipe[0], pipedes) 1676 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0]))) 1677 return -TARGET_EFAULT; 1678 return get_errno(ret); 1679 } 1680 1681 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, 1682 abi_ulong target_addr, 1683 socklen_t len) 1684 { 1685 struct target_ip_mreqn *target_smreqn; 1686 1687 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1); 1688 if (!target_smreqn) 1689 return -TARGET_EFAULT; 1690 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr; 1691 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr; 1692 if (len == sizeof(struct target_ip_mreqn)) 1693 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex); 1694 unlock_user(target_smreqn, target_addr, 0); 1695 1696 return 0; 1697 } 1698 1699 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, 1700 abi_ulong target_addr, 1701 socklen_t len) 1702 { 1703 const socklen_t unix_maxlen = sizeof (struct sockaddr_un); 1704 sa_family_t sa_family; 1705 struct target_sockaddr *target_saddr; 1706 1707 if (fd_trans_target_to_host_addr(fd)) { 1708 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len); 1709 } 1710 1711 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); 1712 if (!target_saddr) 1713 return -TARGET_EFAULT; 1714 1715 sa_family = tswap16(target_saddr->sa_family); 1716 1717 /* Oops. The caller might send a incomplete sun_path; sun_path 1718 * must be terminated by \0 (see the manual page), but 1719 * unfortunately it is quite common to specify sockaddr_un 1720 * length as "strlen(x->sun_path)" while it should be 1721 * "strlen(...) + 1". We'll fix that here if needed. 1722 * Linux kernel has a similar feature. 1723 */ 1724 1725 if (sa_family == AF_UNIX) { 1726 if (len < unix_maxlen && len > 0) { 1727 char *cp = (char*)target_saddr; 1728 1729 if ( cp[len-1] && !cp[len] ) 1730 len++; 1731 } 1732 if (len > unix_maxlen) 1733 len = unix_maxlen; 1734 } 1735 1736 memcpy(addr, target_saddr, len); 1737 addr->sa_family = sa_family; 1738 if (sa_family == AF_NETLINK) { 1739 struct sockaddr_nl *nladdr; 1740 1741 nladdr = (struct sockaddr_nl *)addr; 1742 nladdr->nl_pid = tswap32(nladdr->nl_pid); 1743 nladdr->nl_groups = tswap32(nladdr->nl_groups); 1744 } else if (sa_family == AF_PACKET) { 1745 struct target_sockaddr_ll *lladdr; 1746 1747 lladdr = (struct target_sockaddr_ll *)addr; 1748 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex); 1749 lladdr->sll_hatype = tswap16(lladdr->sll_hatype); 1750 } 1751 unlock_user(target_saddr, target_addr, 0); 1752 1753 return 0; 1754 } 1755 1756 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr, 1757 struct sockaddr *addr, 1758 socklen_t len) 1759 { 1760 struct target_sockaddr *target_saddr; 1761 1762 if (len == 0) { 1763 return 0; 1764 } 1765 assert(addr); 1766 1767 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0); 1768 if (!target_saddr) 1769 return -TARGET_EFAULT; 1770 memcpy(target_saddr, addr, len); 1771 if (len >= offsetof(struct target_sockaddr, sa_family) + 1772 sizeof(target_saddr->sa_family)) { 1773 target_saddr->sa_family = tswap16(addr->sa_family); 1774 } 1775 if (addr->sa_family == AF_NETLINK && 1776 len >= sizeof(struct target_sockaddr_nl)) { 1777 struct target_sockaddr_nl *target_nl = 1778 (struct target_sockaddr_nl *)target_saddr; 1779 target_nl->nl_pid = tswap32(target_nl->nl_pid); 1780 target_nl->nl_groups = tswap32(target_nl->nl_groups); 1781 } else if (addr->sa_family == AF_PACKET) { 1782 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr; 1783 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex); 1784 target_ll->sll_hatype = tswap16(target_ll->sll_hatype); 1785 } else if (addr->sa_family == AF_INET6 && 1786 len >= sizeof(struct target_sockaddr_in6)) { 1787 struct target_sockaddr_in6 *target_in6 = 1788 (struct target_sockaddr_in6 *)target_saddr; 1789 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id); 1790 } 1791 unlock_user(target_saddr, target_addr, len); 1792 1793 return 0; 1794 } 1795 1796 static inline abi_long target_to_host_cmsg(struct msghdr *msgh, 1797 struct target_msghdr *target_msgh) 1798 { 1799 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); 1800 abi_long msg_controllen; 1801 abi_ulong target_cmsg_addr; 1802 struct target_cmsghdr *target_cmsg, *target_cmsg_start; 1803 socklen_t space = 0; 1804 1805 msg_controllen = tswapal(target_msgh->msg_controllen); 1806 if (msg_controllen < sizeof (struct target_cmsghdr)) 1807 goto the_end; 1808 target_cmsg_addr = tswapal(target_msgh->msg_control); 1809 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1); 1810 target_cmsg_start = target_cmsg; 1811 if (!target_cmsg) 1812 return -TARGET_EFAULT; 1813 1814 while (cmsg && target_cmsg) { 1815 void *data = CMSG_DATA(cmsg); 1816 void *target_data = TARGET_CMSG_DATA(target_cmsg); 1817 1818 int len = tswapal(target_cmsg->cmsg_len) 1819 - sizeof(struct target_cmsghdr); 1820 1821 space += CMSG_SPACE(len); 1822 if (space > msgh->msg_controllen) { 1823 space -= CMSG_SPACE(len); 1824 /* This is a QEMU bug, since we allocated the payload 1825 * area ourselves (unlike overflow in host-to-target 1826 * conversion, which is just the guest giving us a buffer 1827 * that's too small). It can't happen for the payload types 1828 * we currently support; if it becomes an issue in future 1829 * we would need to improve our allocation strategy to 1830 * something more intelligent than "twice the size of the 1831 * target buffer we're reading from". 1832 */ 1833 qemu_log_mask(LOG_UNIMP, 1834 ("Unsupported ancillary data %d/%d: " 1835 "unhandled msg size\n"), 1836 tswap32(target_cmsg->cmsg_level), 1837 tswap32(target_cmsg->cmsg_type)); 1838 break; 1839 } 1840 1841 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) { 1842 cmsg->cmsg_level = SOL_SOCKET; 1843 } else { 1844 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level); 1845 } 1846 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type); 1847 cmsg->cmsg_len = CMSG_LEN(len); 1848 1849 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { 1850 int *fd = (int *)data; 1851 int *target_fd = (int *)target_data; 1852 int i, numfds = len / sizeof(int); 1853 1854 for (i = 0; i < numfds; i++) { 1855 __get_user(fd[i], target_fd + i); 1856 } 1857 } else if (cmsg->cmsg_level == SOL_SOCKET 1858 && cmsg->cmsg_type == SCM_CREDENTIALS) { 1859 struct ucred *cred = (struct ucred *)data; 1860 struct target_ucred *target_cred = 1861 (struct target_ucred *)target_data; 1862 1863 __get_user(cred->pid, &target_cred->pid); 1864 __get_user(cred->uid, &target_cred->uid); 1865 __get_user(cred->gid, &target_cred->gid); 1866 } else { 1867 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n", 1868 cmsg->cmsg_level, cmsg->cmsg_type); 1869 memcpy(data, target_data, len); 1870 } 1871 1872 cmsg = CMSG_NXTHDR(msgh, cmsg); 1873 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, 1874 target_cmsg_start); 1875 } 1876 unlock_user(target_cmsg, target_cmsg_addr, 0); 1877 the_end: 1878 msgh->msg_controllen = space; 1879 return 0; 1880 } 1881 1882 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh, 1883 struct msghdr *msgh) 1884 { 1885 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh); 1886 abi_long msg_controllen; 1887 abi_ulong target_cmsg_addr; 1888 struct target_cmsghdr *target_cmsg, *target_cmsg_start; 1889 socklen_t space = 0; 1890 1891 msg_controllen = tswapal(target_msgh->msg_controllen); 1892 if (msg_controllen < sizeof (struct target_cmsghdr)) 1893 goto the_end; 1894 target_cmsg_addr = tswapal(target_msgh->msg_control); 1895 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0); 1896 target_cmsg_start = target_cmsg; 1897 if (!target_cmsg) 1898 return -TARGET_EFAULT; 1899 1900 while (cmsg && target_cmsg) { 1901 void *data = CMSG_DATA(cmsg); 1902 void *target_data = TARGET_CMSG_DATA(target_cmsg); 1903 1904 int len = cmsg->cmsg_len - sizeof(struct cmsghdr); 1905 int tgt_len, tgt_space; 1906 1907 /* We never copy a half-header but may copy half-data; 1908 * this is Linux's behaviour in put_cmsg(). Note that 1909 * truncation here is a guest problem (which we report 1910 * to the guest via the CTRUNC bit), unlike truncation 1911 * in target_to_host_cmsg, which is a QEMU bug. 1912 */ 1913 if (msg_controllen < sizeof(struct target_cmsghdr)) { 1914 target_msgh->msg_flags |= tswap32(MSG_CTRUNC); 1915 break; 1916 } 1917 1918 if (cmsg->cmsg_level == SOL_SOCKET) { 1919 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET); 1920 } else { 1921 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level); 1922 } 1923 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type); 1924 1925 /* Payload types which need a different size of payload on 1926 * the target must adjust tgt_len here. 1927 */ 1928 tgt_len = len; 1929 switch (cmsg->cmsg_level) { 1930 case SOL_SOCKET: 1931 switch (cmsg->cmsg_type) { 1932 case SO_TIMESTAMP: 1933 tgt_len = sizeof(struct target_timeval); 1934 break; 1935 default: 1936 break; 1937 } 1938 break; 1939 default: 1940 break; 1941 } 1942 1943 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) { 1944 target_msgh->msg_flags |= tswap32(MSG_CTRUNC); 1945 tgt_len = msg_controllen - sizeof(struct target_cmsghdr); 1946 } 1947 1948 /* We must now copy-and-convert len bytes of payload 1949 * into tgt_len bytes of destination space. Bear in mind 1950 * that in both source and destination we may be dealing 1951 * with a truncated value! 1952 */ 1953 switch (cmsg->cmsg_level) { 1954 case SOL_SOCKET: 1955 switch (cmsg->cmsg_type) { 1956 case SCM_RIGHTS: 1957 { 1958 int *fd = (int *)data; 1959 int *target_fd = (int *)target_data; 1960 int i, numfds = tgt_len / sizeof(int); 1961 1962 for (i = 0; i < numfds; i++) { 1963 __put_user(fd[i], target_fd + i); 1964 } 1965 break; 1966 } 1967 case SO_TIMESTAMP: 1968 { 1969 struct timeval *tv = (struct timeval *)data; 1970 struct target_timeval *target_tv = 1971 (struct target_timeval *)target_data; 1972 1973 if (len != sizeof(struct timeval) || 1974 tgt_len != sizeof(struct target_timeval)) { 1975 goto unimplemented; 1976 } 1977 1978 /* copy struct timeval to target */ 1979 __put_user(tv->tv_sec, &target_tv->tv_sec); 1980 __put_user(tv->tv_usec, &target_tv->tv_usec); 1981 break; 1982 } 1983 case SCM_CREDENTIALS: 1984 { 1985 struct ucred *cred = (struct ucred *)data; 1986 struct target_ucred *target_cred = 1987 (struct target_ucred *)target_data; 1988 1989 __put_user(cred->pid, &target_cred->pid); 1990 __put_user(cred->uid, &target_cred->uid); 1991 __put_user(cred->gid, &target_cred->gid); 1992 break; 1993 } 1994 default: 1995 goto unimplemented; 1996 } 1997 break; 1998 1999 case SOL_IP: 2000 switch (cmsg->cmsg_type) { 2001 case IP_TTL: 2002 { 2003 uint32_t *v = (uint32_t *)data; 2004 uint32_t *t_int = (uint32_t *)target_data; 2005 2006 if (len != sizeof(uint32_t) || 2007 tgt_len != sizeof(uint32_t)) { 2008 goto unimplemented; 2009 } 2010 __put_user(*v, t_int); 2011 break; 2012 } 2013 case IP_RECVERR: 2014 { 2015 struct errhdr_t { 2016 struct sock_extended_err ee; 2017 struct sockaddr_in offender; 2018 }; 2019 struct errhdr_t *errh = (struct errhdr_t *)data; 2020 struct errhdr_t *target_errh = 2021 (struct errhdr_t *)target_data; 2022 2023 if (len != sizeof(struct errhdr_t) || 2024 tgt_len != sizeof(struct errhdr_t)) { 2025 goto unimplemented; 2026 } 2027 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno); 2028 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin); 2029 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type); 2030 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code); 2031 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad); 2032 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info); 2033 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data); 2034 host_to_target_sockaddr((unsigned long) &target_errh->offender, 2035 (void *) &errh->offender, sizeof(errh->offender)); 2036 break; 2037 } 2038 default: 2039 goto unimplemented; 2040 } 2041 break; 2042 2043 case SOL_IPV6: 2044 switch (cmsg->cmsg_type) { 2045 case IPV6_HOPLIMIT: 2046 { 2047 uint32_t *v = (uint32_t *)data; 2048 uint32_t *t_int = (uint32_t *)target_data; 2049 2050 if (len != sizeof(uint32_t) || 2051 tgt_len != sizeof(uint32_t)) { 2052 goto unimplemented; 2053 } 2054 __put_user(*v, t_int); 2055 break; 2056 } 2057 case IPV6_RECVERR: 2058 { 2059 struct errhdr6_t { 2060 struct sock_extended_err ee; 2061 struct sockaddr_in6 offender; 2062 }; 2063 struct errhdr6_t *errh = (struct errhdr6_t *)data; 2064 struct errhdr6_t *target_errh = 2065 (struct errhdr6_t *)target_data; 2066 2067 if (len != sizeof(struct errhdr6_t) || 2068 tgt_len != sizeof(struct errhdr6_t)) { 2069 goto unimplemented; 2070 } 2071 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno); 2072 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin); 2073 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type); 2074 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code); 2075 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad); 2076 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info); 2077 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data); 2078 host_to_target_sockaddr((unsigned long) &target_errh->offender, 2079 (void *) &errh->offender, sizeof(errh->offender)); 2080 break; 2081 } 2082 default: 2083 goto unimplemented; 2084 } 2085 break; 2086 2087 default: 2088 unimplemented: 2089 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n", 2090 cmsg->cmsg_level, cmsg->cmsg_type); 2091 memcpy(target_data, data, MIN(len, tgt_len)); 2092 if (tgt_len > len) { 2093 memset(target_data + len, 0, tgt_len - len); 2094 } 2095 } 2096 2097 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len)); 2098 tgt_space = TARGET_CMSG_SPACE(tgt_len); 2099 if (msg_controllen < tgt_space) { 2100 tgt_space = msg_controllen; 2101 } 2102 msg_controllen -= tgt_space; 2103 space += tgt_space; 2104 cmsg = CMSG_NXTHDR(msgh, cmsg); 2105 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg, 2106 target_cmsg_start); 2107 } 2108 unlock_user(target_cmsg, target_cmsg_addr, space); 2109 the_end: 2110 target_msgh->msg_controllen = tswapal(space); 2111 return 0; 2112 } 2113 2114 /* do_setsockopt() Must return target values and target errnos. */ 2115 static abi_long do_setsockopt(int sockfd, int level, int optname, 2116 abi_ulong optval_addr, socklen_t optlen) 2117 { 2118 abi_long ret; 2119 int val; 2120 struct ip_mreqn *ip_mreq; 2121 struct ip_mreq_source *ip_mreq_source; 2122 2123 switch(level) { 2124 case SOL_TCP: 2125 case SOL_UDP: 2126 /* TCP and UDP options all take an 'int' value. */ 2127 if (optlen < sizeof(uint32_t)) 2128 return -TARGET_EINVAL; 2129 2130 if (get_user_u32(val, optval_addr)) 2131 return -TARGET_EFAULT; 2132 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); 2133 break; 2134 case SOL_IP: 2135 switch(optname) { 2136 case IP_TOS: 2137 case IP_TTL: 2138 case IP_HDRINCL: 2139 case IP_ROUTER_ALERT: 2140 case IP_RECVOPTS: 2141 case IP_RETOPTS: 2142 case IP_PKTINFO: 2143 case IP_MTU_DISCOVER: 2144 case IP_RECVERR: 2145 case IP_RECVTTL: 2146 case IP_RECVTOS: 2147 #ifdef IP_FREEBIND 2148 case IP_FREEBIND: 2149 #endif 2150 case IP_MULTICAST_TTL: 2151 case IP_MULTICAST_LOOP: 2152 val = 0; 2153 if (optlen >= sizeof(uint32_t)) { 2154 if (get_user_u32(val, optval_addr)) 2155 return -TARGET_EFAULT; 2156 } else if (optlen >= 1) { 2157 if (get_user_u8(val, optval_addr)) 2158 return -TARGET_EFAULT; 2159 } 2160 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val))); 2161 break; 2162 case IP_ADD_MEMBERSHIP: 2163 case IP_DROP_MEMBERSHIP: 2164 if (optlen < sizeof (struct target_ip_mreq) || 2165 optlen > sizeof (struct target_ip_mreqn)) 2166 return -TARGET_EINVAL; 2167 2168 ip_mreq = (struct ip_mreqn *) alloca(optlen); 2169 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen); 2170 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen)); 2171 break; 2172 2173 case IP_BLOCK_SOURCE: 2174 case IP_UNBLOCK_SOURCE: 2175 case IP_ADD_SOURCE_MEMBERSHIP: 2176 case IP_DROP_SOURCE_MEMBERSHIP: 2177 if (optlen != sizeof (struct target_ip_mreq_source)) 2178 return -TARGET_EINVAL; 2179 2180 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1); 2181 if (!ip_mreq_source) { 2182 return -TARGET_EFAULT; 2183 } 2184 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen)); 2185 unlock_user (ip_mreq_source, optval_addr, 0); 2186 break; 2187 2188 default: 2189 goto unimplemented; 2190 } 2191 break; 2192 case SOL_IPV6: 2193 switch (optname) { 2194 case IPV6_MTU_DISCOVER: 2195 case IPV6_MTU: 2196 case IPV6_V6ONLY: 2197 case IPV6_RECVPKTINFO: 2198 case IPV6_UNICAST_HOPS: 2199 case IPV6_MULTICAST_HOPS: 2200 case IPV6_MULTICAST_LOOP: 2201 case IPV6_RECVERR: 2202 case IPV6_RECVHOPLIMIT: 2203 case IPV6_2292HOPLIMIT: 2204 case IPV6_CHECKSUM: 2205 case IPV6_ADDRFORM: 2206 case IPV6_2292PKTINFO: 2207 case IPV6_RECVTCLASS: 2208 case IPV6_RECVRTHDR: 2209 case IPV6_2292RTHDR: 2210 case IPV6_RECVHOPOPTS: 2211 case IPV6_2292HOPOPTS: 2212 case IPV6_RECVDSTOPTS: 2213 case IPV6_2292DSTOPTS: 2214 case IPV6_TCLASS: 2215 case IPV6_ADDR_PREFERENCES: 2216 #ifdef IPV6_RECVPATHMTU 2217 case IPV6_RECVPATHMTU: 2218 #endif 2219 #ifdef IPV6_TRANSPARENT 2220 case IPV6_TRANSPARENT: 2221 #endif 2222 #ifdef IPV6_FREEBIND 2223 case IPV6_FREEBIND: 2224 #endif 2225 #ifdef IPV6_RECVORIGDSTADDR 2226 case IPV6_RECVORIGDSTADDR: 2227 #endif 2228 val = 0; 2229 if (optlen < sizeof(uint32_t)) { 2230 return -TARGET_EINVAL; 2231 } 2232 if (get_user_u32(val, optval_addr)) { 2233 return -TARGET_EFAULT; 2234 } 2235 ret = get_errno(setsockopt(sockfd, level, optname, 2236 &val, sizeof(val))); 2237 break; 2238 case IPV6_PKTINFO: 2239 { 2240 struct in6_pktinfo pki; 2241 2242 if (optlen < sizeof(pki)) { 2243 return -TARGET_EINVAL; 2244 } 2245 2246 if (copy_from_user(&pki, optval_addr, sizeof(pki))) { 2247 return -TARGET_EFAULT; 2248 } 2249 2250 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex); 2251 2252 ret = get_errno(setsockopt(sockfd, level, optname, 2253 &pki, sizeof(pki))); 2254 break; 2255 } 2256 case IPV6_ADD_MEMBERSHIP: 2257 case IPV6_DROP_MEMBERSHIP: 2258 { 2259 struct ipv6_mreq ipv6mreq; 2260 2261 if (optlen < sizeof(ipv6mreq)) { 2262 return -TARGET_EINVAL; 2263 } 2264 2265 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) { 2266 return -TARGET_EFAULT; 2267 } 2268 2269 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface); 2270 2271 ret = get_errno(setsockopt(sockfd, level, optname, 2272 &ipv6mreq, sizeof(ipv6mreq))); 2273 break; 2274 } 2275 default: 2276 goto unimplemented; 2277 } 2278 break; 2279 case SOL_ICMPV6: 2280 switch (optname) { 2281 case ICMPV6_FILTER: 2282 { 2283 struct icmp6_filter icmp6f; 2284 2285 if (optlen > sizeof(icmp6f)) { 2286 optlen = sizeof(icmp6f); 2287 } 2288 2289 if (copy_from_user(&icmp6f, optval_addr, optlen)) { 2290 return -TARGET_EFAULT; 2291 } 2292 2293 for (val = 0; val < 8; val++) { 2294 icmp6f.data[val] = tswap32(icmp6f.data[val]); 2295 } 2296 2297 ret = get_errno(setsockopt(sockfd, level, optname, 2298 &icmp6f, optlen)); 2299 break; 2300 } 2301 default: 2302 goto unimplemented; 2303 } 2304 break; 2305 case SOL_RAW: 2306 switch (optname) { 2307 case ICMP_FILTER: 2308 case IPV6_CHECKSUM: 2309 /* those take an u32 value */ 2310 if (optlen < sizeof(uint32_t)) { 2311 return -TARGET_EINVAL; 2312 } 2313 2314 if (get_user_u32(val, optval_addr)) { 2315 return -TARGET_EFAULT; 2316 } 2317 ret = get_errno(setsockopt(sockfd, level, optname, 2318 &val, sizeof(val))); 2319 break; 2320 2321 default: 2322 goto unimplemented; 2323 } 2324 break; 2325 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE) 2326 case SOL_ALG: 2327 switch (optname) { 2328 case ALG_SET_KEY: 2329 { 2330 char *alg_key = g_malloc(optlen); 2331 2332 if (!alg_key) { 2333 return -TARGET_ENOMEM; 2334 } 2335 if (copy_from_user(alg_key, optval_addr, optlen)) { 2336 g_free(alg_key); 2337 return -TARGET_EFAULT; 2338 } 2339 ret = get_errno(setsockopt(sockfd, level, optname, 2340 alg_key, optlen)); 2341 g_free(alg_key); 2342 break; 2343 } 2344 case ALG_SET_AEAD_AUTHSIZE: 2345 { 2346 ret = get_errno(setsockopt(sockfd, level, optname, 2347 NULL, optlen)); 2348 break; 2349 } 2350 default: 2351 goto unimplemented; 2352 } 2353 break; 2354 #endif 2355 case TARGET_SOL_SOCKET: 2356 switch (optname) { 2357 case TARGET_SO_RCVTIMEO: 2358 { 2359 struct timeval tv; 2360 2361 optname = SO_RCVTIMEO; 2362 2363 set_timeout: 2364 if (optlen != sizeof(struct target_timeval)) { 2365 return -TARGET_EINVAL; 2366 } 2367 2368 if (copy_from_user_timeval(&tv, optval_addr)) { 2369 return -TARGET_EFAULT; 2370 } 2371 2372 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, 2373 &tv, sizeof(tv))); 2374 return ret; 2375 } 2376 case TARGET_SO_SNDTIMEO: 2377 optname = SO_SNDTIMEO; 2378 goto set_timeout; 2379 case TARGET_SO_ATTACH_FILTER: 2380 { 2381 struct target_sock_fprog *tfprog; 2382 struct target_sock_filter *tfilter; 2383 struct sock_fprog fprog; 2384 struct sock_filter *filter; 2385 int i; 2386 2387 if (optlen != sizeof(*tfprog)) { 2388 return -TARGET_EINVAL; 2389 } 2390 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) { 2391 return -TARGET_EFAULT; 2392 } 2393 if (!lock_user_struct(VERIFY_READ, tfilter, 2394 tswapal(tfprog->filter), 0)) { 2395 unlock_user_struct(tfprog, optval_addr, 1); 2396 return -TARGET_EFAULT; 2397 } 2398 2399 fprog.len = tswap16(tfprog->len); 2400 filter = g_try_new(struct sock_filter, fprog.len); 2401 if (filter == NULL) { 2402 unlock_user_struct(tfilter, tfprog->filter, 1); 2403 unlock_user_struct(tfprog, optval_addr, 1); 2404 return -TARGET_ENOMEM; 2405 } 2406 for (i = 0; i < fprog.len; i++) { 2407 filter[i].code = tswap16(tfilter[i].code); 2408 filter[i].jt = tfilter[i].jt; 2409 filter[i].jf = tfilter[i].jf; 2410 filter[i].k = tswap32(tfilter[i].k); 2411 } 2412 fprog.filter = filter; 2413 2414 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, 2415 SO_ATTACH_FILTER, &fprog, sizeof(fprog))); 2416 g_free(filter); 2417 2418 unlock_user_struct(tfilter, tfprog->filter, 1); 2419 unlock_user_struct(tfprog, optval_addr, 1); 2420 return ret; 2421 } 2422 case TARGET_SO_BINDTODEVICE: 2423 { 2424 char *dev_ifname, *addr_ifname; 2425 2426 if (optlen > IFNAMSIZ - 1) { 2427 optlen = IFNAMSIZ - 1; 2428 } 2429 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1); 2430 if (!dev_ifname) { 2431 return -TARGET_EFAULT; 2432 } 2433 optname = SO_BINDTODEVICE; 2434 addr_ifname = alloca(IFNAMSIZ); 2435 memcpy(addr_ifname, dev_ifname, optlen); 2436 addr_ifname[optlen] = 0; 2437 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, 2438 addr_ifname, optlen)); 2439 unlock_user (dev_ifname, optval_addr, 0); 2440 return ret; 2441 } 2442 case TARGET_SO_LINGER: 2443 { 2444 struct linger lg; 2445 struct target_linger *tlg; 2446 2447 if (optlen != sizeof(struct target_linger)) { 2448 return -TARGET_EINVAL; 2449 } 2450 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) { 2451 return -TARGET_EFAULT; 2452 } 2453 __get_user(lg.l_onoff, &tlg->l_onoff); 2454 __get_user(lg.l_linger, &tlg->l_linger); 2455 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER, 2456 &lg, sizeof(lg))); 2457 unlock_user_struct(tlg, optval_addr, 0); 2458 return ret; 2459 } 2460 /* Options with 'int' argument. */ 2461 case TARGET_SO_DEBUG: 2462 optname = SO_DEBUG; 2463 break; 2464 case TARGET_SO_REUSEADDR: 2465 optname = SO_REUSEADDR; 2466 break; 2467 #ifdef SO_REUSEPORT 2468 case TARGET_SO_REUSEPORT: 2469 optname = SO_REUSEPORT; 2470 break; 2471 #endif 2472 case TARGET_SO_TYPE: 2473 optname = SO_TYPE; 2474 break; 2475 case TARGET_SO_ERROR: 2476 optname = SO_ERROR; 2477 break; 2478 case TARGET_SO_DONTROUTE: 2479 optname = SO_DONTROUTE; 2480 break; 2481 case TARGET_SO_BROADCAST: 2482 optname = SO_BROADCAST; 2483 break; 2484 case TARGET_SO_SNDBUF: 2485 optname = SO_SNDBUF; 2486 break; 2487 case TARGET_SO_SNDBUFFORCE: 2488 optname = SO_SNDBUFFORCE; 2489 break; 2490 case TARGET_SO_RCVBUF: 2491 optname = SO_RCVBUF; 2492 break; 2493 case TARGET_SO_RCVBUFFORCE: 2494 optname = SO_RCVBUFFORCE; 2495 break; 2496 case TARGET_SO_KEEPALIVE: 2497 optname = SO_KEEPALIVE; 2498 break; 2499 case TARGET_SO_OOBINLINE: 2500 optname = SO_OOBINLINE; 2501 break; 2502 case TARGET_SO_NO_CHECK: 2503 optname = SO_NO_CHECK; 2504 break; 2505 case TARGET_SO_PRIORITY: 2506 optname = SO_PRIORITY; 2507 break; 2508 #ifdef SO_BSDCOMPAT 2509 case TARGET_SO_BSDCOMPAT: 2510 optname = SO_BSDCOMPAT; 2511 break; 2512 #endif 2513 case TARGET_SO_PASSCRED: 2514 optname = SO_PASSCRED; 2515 break; 2516 case TARGET_SO_PASSSEC: 2517 optname = SO_PASSSEC; 2518 break; 2519 case TARGET_SO_TIMESTAMP: 2520 optname = SO_TIMESTAMP; 2521 break; 2522 case TARGET_SO_RCVLOWAT: 2523 optname = SO_RCVLOWAT; 2524 break; 2525 default: 2526 goto unimplemented; 2527 } 2528 if (optlen < sizeof(uint32_t)) 2529 return -TARGET_EINVAL; 2530 2531 if (get_user_u32(val, optval_addr)) 2532 return -TARGET_EFAULT; 2533 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val))); 2534 break; 2535 #ifdef SOL_NETLINK 2536 case SOL_NETLINK: 2537 switch (optname) { 2538 case NETLINK_PKTINFO: 2539 case NETLINK_ADD_MEMBERSHIP: 2540 case NETLINK_DROP_MEMBERSHIP: 2541 case NETLINK_BROADCAST_ERROR: 2542 case NETLINK_NO_ENOBUFS: 2543 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) 2544 case NETLINK_LISTEN_ALL_NSID: 2545 case NETLINK_CAP_ACK: 2546 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ 2547 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) 2548 case NETLINK_EXT_ACK: 2549 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ 2550 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) 2551 case NETLINK_GET_STRICT_CHK: 2552 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ 2553 break; 2554 default: 2555 goto unimplemented; 2556 } 2557 val = 0; 2558 if (optlen < sizeof(uint32_t)) { 2559 return -TARGET_EINVAL; 2560 } 2561 if (get_user_u32(val, optval_addr)) { 2562 return -TARGET_EFAULT; 2563 } 2564 ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val, 2565 sizeof(val))); 2566 break; 2567 #endif /* SOL_NETLINK */ 2568 default: 2569 unimplemented: 2570 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n", 2571 level, optname); 2572 ret = -TARGET_ENOPROTOOPT; 2573 } 2574 return ret; 2575 } 2576 2577 /* do_getsockopt() Must return target values and target errnos. */ 2578 static abi_long do_getsockopt(int sockfd, int level, int optname, 2579 abi_ulong optval_addr, abi_ulong optlen) 2580 { 2581 abi_long ret; 2582 int len, val; 2583 socklen_t lv; 2584 2585 switch(level) { 2586 case TARGET_SOL_SOCKET: 2587 level = SOL_SOCKET; 2588 switch (optname) { 2589 /* These don't just return a single integer */ 2590 case TARGET_SO_PEERNAME: 2591 goto unimplemented; 2592 case TARGET_SO_RCVTIMEO: { 2593 struct timeval tv; 2594 socklen_t tvlen; 2595 2596 optname = SO_RCVTIMEO; 2597 2598 get_timeout: 2599 if (get_user_u32(len, optlen)) { 2600 return -TARGET_EFAULT; 2601 } 2602 if (len < 0) { 2603 return -TARGET_EINVAL; 2604 } 2605 2606 tvlen = sizeof(tv); 2607 ret = get_errno(getsockopt(sockfd, level, optname, 2608 &tv, &tvlen)); 2609 if (ret < 0) { 2610 return ret; 2611 } 2612 if (len > sizeof(struct target_timeval)) { 2613 len = sizeof(struct target_timeval); 2614 } 2615 if (copy_to_user_timeval(optval_addr, &tv)) { 2616 return -TARGET_EFAULT; 2617 } 2618 if (put_user_u32(len, optlen)) { 2619 return -TARGET_EFAULT; 2620 } 2621 break; 2622 } 2623 case TARGET_SO_SNDTIMEO: 2624 optname = SO_SNDTIMEO; 2625 goto get_timeout; 2626 case TARGET_SO_PEERCRED: { 2627 struct ucred cr; 2628 socklen_t crlen; 2629 struct target_ucred *tcr; 2630 2631 if (get_user_u32(len, optlen)) { 2632 return -TARGET_EFAULT; 2633 } 2634 if (len < 0) { 2635 return -TARGET_EINVAL; 2636 } 2637 2638 crlen = sizeof(cr); 2639 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED, 2640 &cr, &crlen)); 2641 if (ret < 0) { 2642 return ret; 2643 } 2644 if (len > crlen) { 2645 len = crlen; 2646 } 2647 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) { 2648 return -TARGET_EFAULT; 2649 } 2650 __put_user(cr.pid, &tcr->pid); 2651 __put_user(cr.uid, &tcr->uid); 2652 __put_user(cr.gid, &tcr->gid); 2653 unlock_user_struct(tcr, optval_addr, 1); 2654 if (put_user_u32(len, optlen)) { 2655 return -TARGET_EFAULT; 2656 } 2657 break; 2658 } 2659 case TARGET_SO_PEERSEC: { 2660 char *name; 2661 2662 if (get_user_u32(len, optlen)) { 2663 return -TARGET_EFAULT; 2664 } 2665 if (len < 0) { 2666 return -TARGET_EINVAL; 2667 } 2668 name = lock_user(VERIFY_WRITE, optval_addr, len, 0); 2669 if (!name) { 2670 return -TARGET_EFAULT; 2671 } 2672 lv = len; 2673 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC, 2674 name, &lv)); 2675 if (put_user_u32(lv, optlen)) { 2676 ret = -TARGET_EFAULT; 2677 } 2678 unlock_user(name, optval_addr, lv); 2679 break; 2680 } 2681 case TARGET_SO_LINGER: 2682 { 2683 struct linger lg; 2684 socklen_t lglen; 2685 struct target_linger *tlg; 2686 2687 if (get_user_u32(len, optlen)) { 2688 return -TARGET_EFAULT; 2689 } 2690 if (len < 0) { 2691 return -TARGET_EINVAL; 2692 } 2693 2694 lglen = sizeof(lg); 2695 ret = get_errno(getsockopt(sockfd, level, SO_LINGER, 2696 &lg, &lglen)); 2697 if (ret < 0) { 2698 return ret; 2699 } 2700 if (len > lglen) { 2701 len = lglen; 2702 } 2703 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) { 2704 return -TARGET_EFAULT; 2705 } 2706 __put_user(lg.l_onoff, &tlg->l_onoff); 2707 __put_user(lg.l_linger, &tlg->l_linger); 2708 unlock_user_struct(tlg, optval_addr, 1); 2709 if (put_user_u32(len, optlen)) { 2710 return -TARGET_EFAULT; 2711 } 2712 break; 2713 } 2714 /* Options with 'int' argument. */ 2715 case TARGET_SO_DEBUG: 2716 optname = SO_DEBUG; 2717 goto int_case; 2718 case TARGET_SO_REUSEADDR: 2719 optname = SO_REUSEADDR; 2720 goto int_case; 2721 #ifdef SO_REUSEPORT 2722 case TARGET_SO_REUSEPORT: 2723 optname = SO_REUSEPORT; 2724 goto int_case; 2725 #endif 2726 case TARGET_SO_TYPE: 2727 optname = SO_TYPE; 2728 goto int_case; 2729 case TARGET_SO_ERROR: 2730 optname = SO_ERROR; 2731 goto int_case; 2732 case TARGET_SO_DONTROUTE: 2733 optname = SO_DONTROUTE; 2734 goto int_case; 2735 case TARGET_SO_BROADCAST: 2736 optname = SO_BROADCAST; 2737 goto int_case; 2738 case TARGET_SO_SNDBUF: 2739 optname = SO_SNDBUF; 2740 goto int_case; 2741 case TARGET_SO_RCVBUF: 2742 optname = SO_RCVBUF; 2743 goto int_case; 2744 case TARGET_SO_KEEPALIVE: 2745 optname = SO_KEEPALIVE; 2746 goto int_case; 2747 case TARGET_SO_OOBINLINE: 2748 optname = SO_OOBINLINE; 2749 goto int_case; 2750 case TARGET_SO_NO_CHECK: 2751 optname = SO_NO_CHECK; 2752 goto int_case; 2753 case TARGET_SO_PRIORITY: 2754 optname = SO_PRIORITY; 2755 goto int_case; 2756 #ifdef SO_BSDCOMPAT 2757 case TARGET_SO_BSDCOMPAT: 2758 optname = SO_BSDCOMPAT; 2759 goto int_case; 2760 #endif 2761 case TARGET_SO_PASSCRED: 2762 optname = SO_PASSCRED; 2763 goto int_case; 2764 case TARGET_SO_TIMESTAMP: 2765 optname = SO_TIMESTAMP; 2766 goto int_case; 2767 case TARGET_SO_RCVLOWAT: 2768 optname = SO_RCVLOWAT; 2769 goto int_case; 2770 case TARGET_SO_ACCEPTCONN: 2771 optname = SO_ACCEPTCONN; 2772 goto int_case; 2773 case TARGET_SO_PROTOCOL: 2774 optname = SO_PROTOCOL; 2775 goto int_case; 2776 case TARGET_SO_DOMAIN: 2777 optname = SO_DOMAIN; 2778 goto int_case; 2779 default: 2780 goto int_case; 2781 } 2782 break; 2783 case SOL_TCP: 2784 case SOL_UDP: 2785 /* TCP and UDP options all take an 'int' value. */ 2786 int_case: 2787 if (get_user_u32(len, optlen)) 2788 return -TARGET_EFAULT; 2789 if (len < 0) 2790 return -TARGET_EINVAL; 2791 lv = sizeof(lv); 2792 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); 2793 if (ret < 0) 2794 return ret; 2795 if (optname == SO_TYPE) { 2796 val = host_to_target_sock_type(val); 2797 } 2798 if (len > lv) 2799 len = lv; 2800 if (len == 4) { 2801 if (put_user_u32(val, optval_addr)) 2802 return -TARGET_EFAULT; 2803 } else { 2804 if (put_user_u8(val, optval_addr)) 2805 return -TARGET_EFAULT; 2806 } 2807 if (put_user_u32(len, optlen)) 2808 return -TARGET_EFAULT; 2809 break; 2810 case SOL_IP: 2811 switch(optname) { 2812 case IP_TOS: 2813 case IP_TTL: 2814 case IP_HDRINCL: 2815 case IP_ROUTER_ALERT: 2816 case IP_RECVOPTS: 2817 case IP_RETOPTS: 2818 case IP_PKTINFO: 2819 case IP_MTU_DISCOVER: 2820 case IP_RECVERR: 2821 case IP_RECVTOS: 2822 #ifdef IP_FREEBIND 2823 case IP_FREEBIND: 2824 #endif 2825 case IP_MULTICAST_TTL: 2826 case IP_MULTICAST_LOOP: 2827 if (get_user_u32(len, optlen)) 2828 return -TARGET_EFAULT; 2829 if (len < 0) 2830 return -TARGET_EINVAL; 2831 lv = sizeof(lv); 2832 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); 2833 if (ret < 0) 2834 return ret; 2835 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { 2836 len = 1; 2837 if (put_user_u32(len, optlen) 2838 || put_user_u8(val, optval_addr)) 2839 return -TARGET_EFAULT; 2840 } else { 2841 if (len > sizeof(int)) 2842 len = sizeof(int); 2843 if (put_user_u32(len, optlen) 2844 || put_user_u32(val, optval_addr)) 2845 return -TARGET_EFAULT; 2846 } 2847 break; 2848 default: 2849 ret = -TARGET_ENOPROTOOPT; 2850 break; 2851 } 2852 break; 2853 case SOL_IPV6: 2854 switch (optname) { 2855 case IPV6_MTU_DISCOVER: 2856 case IPV6_MTU: 2857 case IPV6_V6ONLY: 2858 case IPV6_RECVPKTINFO: 2859 case IPV6_UNICAST_HOPS: 2860 case IPV6_MULTICAST_HOPS: 2861 case IPV6_MULTICAST_LOOP: 2862 case IPV6_RECVERR: 2863 case IPV6_RECVHOPLIMIT: 2864 case IPV6_2292HOPLIMIT: 2865 case IPV6_CHECKSUM: 2866 case IPV6_ADDRFORM: 2867 case IPV6_2292PKTINFO: 2868 case IPV6_RECVTCLASS: 2869 case IPV6_RECVRTHDR: 2870 case IPV6_2292RTHDR: 2871 case IPV6_RECVHOPOPTS: 2872 case IPV6_2292HOPOPTS: 2873 case IPV6_RECVDSTOPTS: 2874 case IPV6_2292DSTOPTS: 2875 case IPV6_TCLASS: 2876 case IPV6_ADDR_PREFERENCES: 2877 #ifdef IPV6_RECVPATHMTU 2878 case IPV6_RECVPATHMTU: 2879 #endif 2880 #ifdef IPV6_TRANSPARENT 2881 case IPV6_TRANSPARENT: 2882 #endif 2883 #ifdef IPV6_FREEBIND 2884 case IPV6_FREEBIND: 2885 #endif 2886 #ifdef IPV6_RECVORIGDSTADDR 2887 case IPV6_RECVORIGDSTADDR: 2888 #endif 2889 if (get_user_u32(len, optlen)) 2890 return -TARGET_EFAULT; 2891 if (len < 0) 2892 return -TARGET_EINVAL; 2893 lv = sizeof(lv); 2894 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); 2895 if (ret < 0) 2896 return ret; 2897 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) { 2898 len = 1; 2899 if (put_user_u32(len, optlen) 2900 || put_user_u8(val, optval_addr)) 2901 return -TARGET_EFAULT; 2902 } else { 2903 if (len > sizeof(int)) 2904 len = sizeof(int); 2905 if (put_user_u32(len, optlen) 2906 || put_user_u32(val, optval_addr)) 2907 return -TARGET_EFAULT; 2908 } 2909 break; 2910 default: 2911 ret = -TARGET_ENOPROTOOPT; 2912 break; 2913 } 2914 break; 2915 #ifdef SOL_NETLINK 2916 case SOL_NETLINK: 2917 switch (optname) { 2918 case NETLINK_PKTINFO: 2919 case NETLINK_BROADCAST_ERROR: 2920 case NETLINK_NO_ENOBUFS: 2921 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) 2922 case NETLINK_LISTEN_ALL_NSID: 2923 case NETLINK_CAP_ACK: 2924 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ 2925 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) 2926 case NETLINK_EXT_ACK: 2927 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ 2928 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) 2929 case NETLINK_GET_STRICT_CHK: 2930 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */ 2931 if (get_user_u32(len, optlen)) { 2932 return -TARGET_EFAULT; 2933 } 2934 if (len != sizeof(val)) { 2935 return -TARGET_EINVAL; 2936 } 2937 lv = len; 2938 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv)); 2939 if (ret < 0) { 2940 return ret; 2941 } 2942 if (put_user_u32(lv, optlen) 2943 || put_user_u32(val, optval_addr)) { 2944 return -TARGET_EFAULT; 2945 } 2946 break; 2947 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) 2948 case NETLINK_LIST_MEMBERSHIPS: 2949 { 2950 uint32_t *results; 2951 int i; 2952 if (get_user_u32(len, optlen)) { 2953 return -TARGET_EFAULT; 2954 } 2955 if (len < 0) { 2956 return -TARGET_EINVAL; 2957 } 2958 results = lock_user(VERIFY_WRITE, optval_addr, len, 1); 2959 if (!results && len > 0) { 2960 return -TARGET_EFAULT; 2961 } 2962 lv = len; 2963 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv)); 2964 if (ret < 0) { 2965 unlock_user(results, optval_addr, 0); 2966 return ret; 2967 } 2968 /* swap host endianess to target endianess. */ 2969 for (i = 0; i < (len / sizeof(uint32_t)); i++) { 2970 results[i] = tswap32(results[i]); 2971 } 2972 if (put_user_u32(lv, optlen)) { 2973 return -TARGET_EFAULT; 2974 } 2975 unlock_user(results, optval_addr, 0); 2976 break; 2977 } 2978 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */ 2979 default: 2980 goto unimplemented; 2981 } 2982 break; 2983 #endif /* SOL_NETLINK */ 2984 default: 2985 unimplemented: 2986 qemu_log_mask(LOG_UNIMP, 2987 "getsockopt level=%d optname=%d not yet supported\n", 2988 level, optname); 2989 ret = -TARGET_EOPNOTSUPP; 2990 break; 2991 } 2992 return ret; 2993 } 2994 2995 /* Convert target low/high pair representing file offset into the host 2996 * low/high pair. This function doesn't handle offsets bigger than 64 bits 2997 * as the kernel doesn't handle them either. 2998 */ 2999 static void target_to_host_low_high(abi_ulong tlow, 3000 abi_ulong thigh, 3001 unsigned long *hlow, 3002 unsigned long *hhigh) 3003 { 3004 uint64_t off = tlow | 3005 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) << 3006 TARGET_LONG_BITS / 2; 3007 3008 *hlow = off; 3009 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2; 3010 } 3011 3012 static struct iovec *lock_iovec(int type, abi_ulong target_addr, 3013 abi_ulong count, int copy) 3014 { 3015 struct target_iovec *target_vec; 3016 struct iovec *vec; 3017 abi_ulong total_len, max_len; 3018 int i; 3019 int err = 0; 3020 bool bad_address = false; 3021 3022 if (count == 0) { 3023 errno = 0; 3024 return NULL; 3025 } 3026 if (count > IOV_MAX) { 3027 errno = EINVAL; 3028 return NULL; 3029 } 3030 3031 vec = g_try_new0(struct iovec, count); 3032 if (vec == NULL) { 3033 errno = ENOMEM; 3034 return NULL; 3035 } 3036 3037 target_vec = lock_user(VERIFY_READ, target_addr, 3038 count * sizeof(struct target_iovec), 1); 3039 if (target_vec == NULL) { 3040 err = EFAULT; 3041 goto fail2; 3042 } 3043 3044 /* ??? If host page size > target page size, this will result in a 3045 value larger than what we can actually support. */ 3046 max_len = 0x7fffffff & TARGET_PAGE_MASK; 3047 total_len = 0; 3048 3049 for (i = 0; i < count; i++) { 3050 abi_ulong base = tswapal(target_vec[i].iov_base); 3051 abi_long len = tswapal(target_vec[i].iov_len); 3052 3053 if (len < 0) { 3054 err = EINVAL; 3055 goto fail; 3056 } else if (len == 0) { 3057 /* Zero length pointer is ignored. */ 3058 vec[i].iov_base = 0; 3059 } else { 3060 vec[i].iov_base = lock_user(type, base, len, copy); 3061 /* If the first buffer pointer is bad, this is a fault. But 3062 * subsequent bad buffers will result in a partial write; this 3063 * is realized by filling the vector with null pointers and 3064 * zero lengths. */ 3065 if (!vec[i].iov_base) { 3066 if (i == 0) { 3067 err = EFAULT; 3068 goto fail; 3069 } else { 3070 bad_address = true; 3071 } 3072 } 3073 if (bad_address) { 3074 len = 0; 3075 } 3076 if (len > max_len - total_len) { 3077 len = max_len - total_len; 3078 } 3079 } 3080 vec[i].iov_len = len; 3081 total_len += len; 3082 } 3083 3084 unlock_user(target_vec, target_addr, 0); 3085 return vec; 3086 3087 fail: 3088 while (--i >= 0) { 3089 if (tswapal(target_vec[i].iov_len) > 0) { 3090 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0); 3091 } 3092 } 3093 unlock_user(target_vec, target_addr, 0); 3094 fail2: 3095 g_free(vec); 3096 errno = err; 3097 return NULL; 3098 } 3099 3100 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr, 3101 abi_ulong count, int copy) 3102 { 3103 struct target_iovec *target_vec; 3104 int i; 3105 3106 target_vec = lock_user(VERIFY_READ, target_addr, 3107 count * sizeof(struct target_iovec), 1); 3108 if (target_vec) { 3109 for (i = 0; i < count; i++) { 3110 abi_ulong base = tswapal(target_vec[i].iov_base); 3111 abi_long len = tswapal(target_vec[i].iov_len); 3112 if (len < 0) { 3113 break; 3114 } 3115 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); 3116 } 3117 unlock_user(target_vec, target_addr, 0); 3118 } 3119 3120 g_free(vec); 3121 } 3122 3123 static inline int target_to_host_sock_type(int *type) 3124 { 3125 int host_type = 0; 3126 int target_type = *type; 3127 3128 switch (target_type & TARGET_SOCK_TYPE_MASK) { 3129 case TARGET_SOCK_DGRAM: 3130 host_type = SOCK_DGRAM; 3131 break; 3132 case TARGET_SOCK_STREAM: 3133 host_type = SOCK_STREAM; 3134 break; 3135 default: 3136 host_type = target_type & TARGET_SOCK_TYPE_MASK; 3137 break; 3138 } 3139 if (target_type & TARGET_SOCK_CLOEXEC) { 3140 #if defined(SOCK_CLOEXEC) 3141 host_type |= SOCK_CLOEXEC; 3142 #else 3143 return -TARGET_EINVAL; 3144 #endif 3145 } 3146 if (target_type & TARGET_SOCK_NONBLOCK) { 3147 #if defined(SOCK_NONBLOCK) 3148 host_type |= SOCK_NONBLOCK; 3149 #elif !defined(O_NONBLOCK) 3150 return -TARGET_EINVAL; 3151 #endif 3152 } 3153 *type = host_type; 3154 return 0; 3155 } 3156 3157 /* Try to emulate socket type flags after socket creation. */ 3158 static int sock_flags_fixup(int fd, int target_type) 3159 { 3160 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK) 3161 if (target_type & TARGET_SOCK_NONBLOCK) { 3162 int flags = fcntl(fd, F_GETFL); 3163 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) { 3164 close(fd); 3165 return -TARGET_EINVAL; 3166 } 3167 } 3168 #endif 3169 return fd; 3170 } 3171 3172 /* do_socket() Must return target values and target errnos. */ 3173 static abi_long do_socket(int domain, int type, int protocol) 3174 { 3175 int target_type = type; 3176 int ret; 3177 3178 ret = target_to_host_sock_type(&type); 3179 if (ret) { 3180 return ret; 3181 } 3182 3183 if (domain == PF_NETLINK && !( 3184 #ifdef CONFIG_RTNETLINK 3185 protocol == NETLINK_ROUTE || 3186 #endif 3187 protocol == NETLINK_KOBJECT_UEVENT || 3188 protocol == NETLINK_AUDIT)) { 3189 return -TARGET_EPROTONOSUPPORT; 3190 } 3191 3192 if (domain == AF_PACKET || 3193 (domain == AF_INET && type == SOCK_PACKET)) { 3194 protocol = tswap16(protocol); 3195 } 3196 3197 ret = get_errno(socket(domain, type, protocol)); 3198 if (ret >= 0) { 3199 ret = sock_flags_fixup(ret, target_type); 3200 if (type == SOCK_PACKET) { 3201 /* Manage an obsolete case : 3202 * if socket type is SOCK_PACKET, bind by name 3203 */ 3204 fd_trans_register(ret, &target_packet_trans); 3205 } else if (domain == PF_NETLINK) { 3206 switch (protocol) { 3207 #ifdef CONFIG_RTNETLINK 3208 case NETLINK_ROUTE: 3209 fd_trans_register(ret, &target_netlink_route_trans); 3210 break; 3211 #endif 3212 case NETLINK_KOBJECT_UEVENT: 3213 /* nothing to do: messages are strings */ 3214 break; 3215 case NETLINK_AUDIT: 3216 fd_trans_register(ret, &target_netlink_audit_trans); 3217 break; 3218 default: 3219 g_assert_not_reached(); 3220 } 3221 } 3222 } 3223 return ret; 3224 } 3225 3226 /* do_bind() Must return target values and target errnos. */ 3227 static abi_long do_bind(int sockfd, abi_ulong target_addr, 3228 socklen_t addrlen) 3229 { 3230 void *addr; 3231 abi_long ret; 3232 3233 if ((int)addrlen < 0) { 3234 return -TARGET_EINVAL; 3235 } 3236 3237 addr = alloca(addrlen+1); 3238 3239 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); 3240 if (ret) 3241 return ret; 3242 3243 return get_errno(bind(sockfd, addr, addrlen)); 3244 } 3245 3246 /* do_connect() Must return target values and target errnos. */ 3247 static abi_long do_connect(int sockfd, abi_ulong target_addr, 3248 socklen_t addrlen) 3249 { 3250 void *addr; 3251 abi_long ret; 3252 3253 if ((int)addrlen < 0) { 3254 return -TARGET_EINVAL; 3255 } 3256 3257 addr = alloca(addrlen+1); 3258 3259 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen); 3260 if (ret) 3261 return ret; 3262 3263 return get_errno(safe_connect(sockfd, addr, addrlen)); 3264 } 3265 3266 /* do_sendrecvmsg_locked() Must return target values and target errnos. */ 3267 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, 3268 int flags, int send) 3269 { 3270 abi_long ret, len; 3271 struct msghdr msg; 3272 abi_ulong count; 3273 struct iovec *vec; 3274 abi_ulong target_vec; 3275 3276 if (msgp->msg_name) { 3277 msg.msg_namelen = tswap32(msgp->msg_namelen); 3278 msg.msg_name = alloca(msg.msg_namelen+1); 3279 ret = target_to_host_sockaddr(fd, msg.msg_name, 3280 tswapal(msgp->msg_name), 3281 msg.msg_namelen); 3282 if (ret == -TARGET_EFAULT) { 3283 /* For connected sockets msg_name and msg_namelen must 3284 * be ignored, so returning EFAULT immediately is wrong. 3285 * Instead, pass a bad msg_name to the host kernel, and 3286 * let it decide whether to return EFAULT or not. 3287 */ 3288 msg.msg_name = (void *)-1; 3289 } else if (ret) { 3290 goto out2; 3291 } 3292 } else { 3293 msg.msg_name = NULL; 3294 msg.msg_namelen = 0; 3295 } 3296 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen); 3297 msg.msg_control = alloca(msg.msg_controllen); 3298 memset(msg.msg_control, 0, msg.msg_controllen); 3299 3300 msg.msg_flags = tswap32(msgp->msg_flags); 3301 3302 count = tswapal(msgp->msg_iovlen); 3303 target_vec = tswapal(msgp->msg_iov); 3304 3305 if (count > IOV_MAX) { 3306 /* sendrcvmsg returns a different errno for this condition than 3307 * readv/writev, so we must catch it here before lock_iovec() does. 3308 */ 3309 ret = -TARGET_EMSGSIZE; 3310 goto out2; 3311 } 3312 3313 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, 3314 target_vec, count, send); 3315 if (vec == NULL) { 3316 ret = -host_to_target_errno(errno); 3317 goto out2; 3318 } 3319 msg.msg_iovlen = count; 3320 msg.msg_iov = vec; 3321 3322 if (send) { 3323 if (fd_trans_target_to_host_data(fd)) { 3324 void *host_msg; 3325 3326 host_msg = g_malloc(msg.msg_iov->iov_len); 3327 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len); 3328 ret = fd_trans_target_to_host_data(fd)(host_msg, 3329 msg.msg_iov->iov_len); 3330 if (ret >= 0) { 3331 msg.msg_iov->iov_base = host_msg; 3332 ret = get_errno(safe_sendmsg(fd, &msg, flags)); 3333 } 3334 g_free(host_msg); 3335 } else { 3336 ret = target_to_host_cmsg(&msg, msgp); 3337 if (ret == 0) { 3338 ret = get_errno(safe_sendmsg(fd, &msg, flags)); 3339 } 3340 } 3341 } else { 3342 ret = get_errno(safe_recvmsg(fd, &msg, flags)); 3343 if (!is_error(ret)) { 3344 len = ret; 3345 if (fd_trans_host_to_target_data(fd)) { 3346 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base, 3347 MIN(msg.msg_iov->iov_len, len)); 3348 } else { 3349 ret = host_to_target_cmsg(msgp, &msg); 3350 } 3351 if (!is_error(ret)) { 3352 msgp->msg_namelen = tswap32(msg.msg_namelen); 3353 msgp->msg_flags = tswap32(msg.msg_flags); 3354 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) { 3355 ret = host_to_target_sockaddr(tswapal(msgp->msg_name), 3356 msg.msg_name, msg.msg_namelen); 3357 if (ret) { 3358 goto out; 3359 } 3360 } 3361 3362 ret = len; 3363 } 3364 } 3365 } 3366 3367 out: 3368 unlock_iovec(vec, target_vec, count, !send); 3369 out2: 3370 return ret; 3371 } 3372 3373 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg, 3374 int flags, int send) 3375 { 3376 abi_long ret; 3377 struct target_msghdr *msgp; 3378 3379 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE, 3380 msgp, 3381 target_msg, 3382 send ? 1 : 0)) { 3383 return -TARGET_EFAULT; 3384 } 3385 ret = do_sendrecvmsg_locked(fd, msgp, flags, send); 3386 unlock_user_struct(msgp, target_msg, send ? 0 : 1); 3387 return ret; 3388 } 3389 3390 /* We don't rely on the C library to have sendmmsg/recvmmsg support, 3391 * so it might not have this *mmsg-specific flag either. 3392 */ 3393 #ifndef MSG_WAITFORONE 3394 #define MSG_WAITFORONE 0x10000 3395 #endif 3396 3397 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec, 3398 unsigned int vlen, unsigned int flags, 3399 int send) 3400 { 3401 struct target_mmsghdr *mmsgp; 3402 abi_long ret = 0; 3403 int i; 3404 3405 if (vlen > UIO_MAXIOV) { 3406 vlen = UIO_MAXIOV; 3407 } 3408 3409 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1); 3410 if (!mmsgp) { 3411 return -TARGET_EFAULT; 3412 } 3413 3414 for (i = 0; i < vlen; i++) { 3415 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send); 3416 if (is_error(ret)) { 3417 break; 3418 } 3419 mmsgp[i].msg_len = tswap32(ret); 3420 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ 3421 if (flags & MSG_WAITFORONE) { 3422 flags |= MSG_DONTWAIT; 3423 } 3424 } 3425 3426 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i); 3427 3428 /* Return number of datagrams sent if we sent any at all; 3429 * otherwise return the error. 3430 */ 3431 if (i) { 3432 return i; 3433 } 3434 return ret; 3435 } 3436 3437 /* do_accept4() Must return target values and target errnos. */ 3438 static abi_long do_accept4(int fd, abi_ulong target_addr, 3439 abi_ulong target_addrlen_addr, int flags) 3440 { 3441 socklen_t addrlen, ret_addrlen; 3442 void *addr; 3443 abi_long ret; 3444 int host_flags; 3445 3446 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); 3447 3448 if (target_addr == 0) { 3449 return get_errno(safe_accept4(fd, NULL, NULL, host_flags)); 3450 } 3451 3452 /* linux returns EFAULT if addrlen pointer is invalid */ 3453 if (get_user_u32(addrlen, target_addrlen_addr)) 3454 return -TARGET_EFAULT; 3455 3456 if ((int)addrlen < 0) { 3457 return -TARGET_EINVAL; 3458 } 3459 3460 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) { 3461 return -TARGET_EFAULT; 3462 } 3463 3464 addr = alloca(addrlen); 3465 3466 ret_addrlen = addrlen; 3467 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags)); 3468 if (!is_error(ret)) { 3469 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); 3470 if (put_user_u32(ret_addrlen, target_addrlen_addr)) { 3471 ret = -TARGET_EFAULT; 3472 } 3473 } 3474 return ret; 3475 } 3476 3477 /* do_getpeername() Must return target values and target errnos. */ 3478 static abi_long do_getpeername(int fd, abi_ulong target_addr, 3479 abi_ulong target_addrlen_addr) 3480 { 3481 socklen_t addrlen, ret_addrlen; 3482 void *addr; 3483 abi_long ret; 3484 3485 if (get_user_u32(addrlen, target_addrlen_addr)) 3486 return -TARGET_EFAULT; 3487 3488 if ((int)addrlen < 0) { 3489 return -TARGET_EINVAL; 3490 } 3491 3492 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) { 3493 return -TARGET_EFAULT; 3494 } 3495 3496 addr = alloca(addrlen); 3497 3498 ret_addrlen = addrlen; 3499 ret = get_errno(getpeername(fd, addr, &ret_addrlen)); 3500 if (!is_error(ret)) { 3501 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); 3502 if (put_user_u32(ret_addrlen, target_addrlen_addr)) { 3503 ret = -TARGET_EFAULT; 3504 } 3505 } 3506 return ret; 3507 } 3508 3509 /* do_getsockname() Must return target values and target errnos. */ 3510 static abi_long do_getsockname(int fd, abi_ulong target_addr, 3511 abi_ulong target_addrlen_addr) 3512 { 3513 socklen_t addrlen, ret_addrlen; 3514 void *addr; 3515 abi_long ret; 3516 3517 if (get_user_u32(addrlen, target_addrlen_addr)) 3518 return -TARGET_EFAULT; 3519 3520 if ((int)addrlen < 0) { 3521 return -TARGET_EINVAL; 3522 } 3523 3524 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) { 3525 return -TARGET_EFAULT; 3526 } 3527 3528 addr = alloca(addrlen); 3529 3530 ret_addrlen = addrlen; 3531 ret = get_errno(getsockname(fd, addr, &ret_addrlen)); 3532 if (!is_error(ret)) { 3533 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen)); 3534 if (put_user_u32(ret_addrlen, target_addrlen_addr)) { 3535 ret = -TARGET_EFAULT; 3536 } 3537 } 3538 return ret; 3539 } 3540 3541 /* do_socketpair() Must return target values and target errnos. */ 3542 static abi_long do_socketpair(int domain, int type, int protocol, 3543 abi_ulong target_tab_addr) 3544 { 3545 int tab[2]; 3546 abi_long ret; 3547 3548 target_to_host_sock_type(&type); 3549 3550 ret = get_errno(socketpair(domain, type, protocol, tab)); 3551 if (!is_error(ret)) { 3552 if (put_user_s32(tab[0], target_tab_addr) 3553 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0]))) 3554 ret = -TARGET_EFAULT; 3555 } 3556 return ret; 3557 } 3558 3559 /* do_sendto() Must return target values and target errnos. */ 3560 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags, 3561 abi_ulong target_addr, socklen_t addrlen) 3562 { 3563 void *addr; 3564 void *host_msg; 3565 void *copy_msg = NULL; 3566 abi_long ret; 3567 3568 if ((int)addrlen < 0) { 3569 return -TARGET_EINVAL; 3570 } 3571 3572 host_msg = lock_user(VERIFY_READ, msg, len, 1); 3573 if (!host_msg) 3574 return -TARGET_EFAULT; 3575 if (fd_trans_target_to_host_data(fd)) { 3576 copy_msg = host_msg; 3577 host_msg = g_malloc(len); 3578 memcpy(host_msg, copy_msg, len); 3579 ret = fd_trans_target_to_host_data(fd)(host_msg, len); 3580 if (ret < 0) { 3581 goto fail; 3582 } 3583 } 3584 if (target_addr) { 3585 addr = alloca(addrlen+1); 3586 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen); 3587 if (ret) { 3588 goto fail; 3589 } 3590 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen)); 3591 } else { 3592 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0)); 3593 } 3594 fail: 3595 if (copy_msg) { 3596 g_free(host_msg); 3597 host_msg = copy_msg; 3598 } 3599 unlock_user(host_msg, msg, 0); 3600 return ret; 3601 } 3602 3603 /* do_recvfrom() Must return target values and target errnos. */ 3604 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags, 3605 abi_ulong target_addr, 3606 abi_ulong target_addrlen) 3607 { 3608 socklen_t addrlen, ret_addrlen; 3609 void *addr; 3610 void *host_msg; 3611 abi_long ret; 3612 3613 if (!msg) { 3614 host_msg = NULL; 3615 } else { 3616 host_msg = lock_user(VERIFY_WRITE, msg, len, 0); 3617 if (!host_msg) { 3618 return -TARGET_EFAULT; 3619 } 3620 } 3621 if (target_addr) { 3622 if (get_user_u32(addrlen, target_addrlen)) { 3623 ret = -TARGET_EFAULT; 3624 goto fail; 3625 } 3626 if ((int)addrlen < 0) { 3627 ret = -TARGET_EINVAL; 3628 goto fail; 3629 } 3630 addr = alloca(addrlen); 3631 ret_addrlen = addrlen; 3632 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, 3633 addr, &ret_addrlen)); 3634 } else { 3635 addr = NULL; /* To keep compiler quiet. */ 3636 addrlen = 0; /* To keep compiler quiet. */ 3637 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0)); 3638 } 3639 if (!is_error(ret)) { 3640 if (fd_trans_host_to_target_data(fd)) { 3641 abi_long trans; 3642 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len)); 3643 if (is_error(trans)) { 3644 ret = trans; 3645 goto fail; 3646 } 3647 } 3648 if (target_addr) { 3649 host_to_target_sockaddr(target_addr, addr, 3650 MIN(addrlen, ret_addrlen)); 3651 if (put_user_u32(ret_addrlen, target_addrlen)) { 3652 ret = -TARGET_EFAULT; 3653 goto fail; 3654 } 3655 } 3656 unlock_user(host_msg, msg, len); 3657 } else { 3658 fail: 3659 unlock_user(host_msg, msg, 0); 3660 } 3661 return ret; 3662 } 3663 3664 #ifdef TARGET_NR_socketcall 3665 /* do_socketcall() must return target values and target errnos. */ 3666 static abi_long do_socketcall(int num, abi_ulong vptr) 3667 { 3668 static const unsigned nargs[] = { /* number of arguments per operation */ 3669 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */ 3670 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */ 3671 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */ 3672 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */ 3673 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */ 3674 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */ 3675 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */ 3676 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */ 3677 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */ 3678 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */ 3679 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */ 3680 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */ 3681 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */ 3682 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */ 3683 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */ 3684 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */ 3685 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */ 3686 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */ 3687 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */ 3688 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */ 3689 }; 3690 abi_long a[6]; /* max 6 args */ 3691 unsigned i; 3692 3693 /* check the range of the first argument num */ 3694 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */ 3695 if (num < 1 || num > TARGET_SYS_SENDMMSG) { 3696 return -TARGET_EINVAL; 3697 } 3698 /* ensure we have space for args */ 3699 if (nargs[num] > ARRAY_SIZE(a)) { 3700 return -TARGET_EINVAL; 3701 } 3702 /* collect the arguments in a[] according to nargs[] */ 3703 for (i = 0; i < nargs[num]; ++i) { 3704 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) { 3705 return -TARGET_EFAULT; 3706 } 3707 } 3708 /* now when we have the args, invoke the appropriate underlying function */ 3709 switch (num) { 3710 case TARGET_SYS_SOCKET: /* domain, type, protocol */ 3711 return do_socket(a[0], a[1], a[2]); 3712 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */ 3713 return do_bind(a[0], a[1], a[2]); 3714 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */ 3715 return do_connect(a[0], a[1], a[2]); 3716 case TARGET_SYS_LISTEN: /* sockfd, backlog */ 3717 return get_errno(listen(a[0], a[1])); 3718 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */ 3719 return do_accept4(a[0], a[1], a[2], 0); 3720 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */ 3721 return do_getsockname(a[0], a[1], a[2]); 3722 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */ 3723 return do_getpeername(a[0], a[1], a[2]); 3724 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */ 3725 return do_socketpair(a[0], a[1], a[2], a[3]); 3726 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */ 3727 return do_sendto(a[0], a[1], a[2], a[3], 0, 0); 3728 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */ 3729 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0); 3730 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */ 3731 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]); 3732 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */ 3733 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]); 3734 case TARGET_SYS_SHUTDOWN: /* sockfd, how */ 3735 return get_errno(shutdown(a[0], a[1])); 3736 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */ 3737 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]); 3738 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */ 3739 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]); 3740 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */ 3741 return do_sendrecvmsg(a[0], a[1], a[2], 1); 3742 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */ 3743 return do_sendrecvmsg(a[0], a[1], a[2], 0); 3744 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */ 3745 return do_accept4(a[0], a[1], a[2], a[3]); 3746 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */ 3747 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0); 3748 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */ 3749 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1); 3750 default: 3751 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num); 3752 return -TARGET_EINVAL; 3753 } 3754 } 3755 #endif 3756 3757 #define N_SHM_REGIONS 32 3758 3759 static struct shm_region { 3760 abi_ulong start; 3761 abi_ulong size; 3762 bool in_use; 3763 } shm_regions[N_SHM_REGIONS]; 3764 3765 #ifndef TARGET_SEMID64_DS 3766 /* asm-generic version of this struct */ 3767 struct target_semid64_ds 3768 { 3769 struct target_ipc_perm sem_perm; 3770 abi_ulong sem_otime; 3771 #if TARGET_ABI_BITS == 32 3772 abi_ulong __unused1; 3773 #endif 3774 abi_ulong sem_ctime; 3775 #if TARGET_ABI_BITS == 32 3776 abi_ulong __unused2; 3777 #endif 3778 abi_ulong sem_nsems; 3779 abi_ulong __unused3; 3780 abi_ulong __unused4; 3781 }; 3782 #endif 3783 3784 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip, 3785 abi_ulong target_addr) 3786 { 3787 struct target_ipc_perm *target_ip; 3788 struct target_semid64_ds *target_sd; 3789 3790 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) 3791 return -TARGET_EFAULT; 3792 target_ip = &(target_sd->sem_perm); 3793 host_ip->__key = tswap32(target_ip->__key); 3794 host_ip->uid = tswap32(target_ip->uid); 3795 host_ip->gid = tswap32(target_ip->gid); 3796 host_ip->cuid = tswap32(target_ip->cuid); 3797 host_ip->cgid = tswap32(target_ip->cgid); 3798 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) 3799 host_ip->mode = tswap32(target_ip->mode); 3800 #else 3801 host_ip->mode = tswap16(target_ip->mode); 3802 #endif 3803 #if defined(TARGET_PPC) 3804 host_ip->__seq = tswap32(target_ip->__seq); 3805 #else 3806 host_ip->__seq = tswap16(target_ip->__seq); 3807 #endif 3808 unlock_user_struct(target_sd, target_addr, 0); 3809 return 0; 3810 } 3811 3812 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr, 3813 struct ipc_perm *host_ip) 3814 { 3815 struct target_ipc_perm *target_ip; 3816 struct target_semid64_ds *target_sd; 3817 3818 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) 3819 return -TARGET_EFAULT; 3820 target_ip = &(target_sd->sem_perm); 3821 target_ip->__key = tswap32(host_ip->__key); 3822 target_ip->uid = tswap32(host_ip->uid); 3823 target_ip->gid = tswap32(host_ip->gid); 3824 target_ip->cuid = tswap32(host_ip->cuid); 3825 target_ip->cgid = tswap32(host_ip->cgid); 3826 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC) 3827 target_ip->mode = tswap32(host_ip->mode); 3828 #else 3829 target_ip->mode = tswap16(host_ip->mode); 3830 #endif 3831 #if defined(TARGET_PPC) 3832 target_ip->__seq = tswap32(host_ip->__seq); 3833 #else 3834 target_ip->__seq = tswap16(host_ip->__seq); 3835 #endif 3836 unlock_user_struct(target_sd, target_addr, 1); 3837 return 0; 3838 } 3839 3840 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd, 3841 abi_ulong target_addr) 3842 { 3843 struct target_semid64_ds *target_sd; 3844 3845 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) 3846 return -TARGET_EFAULT; 3847 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr)) 3848 return -TARGET_EFAULT; 3849 host_sd->sem_nsems = tswapal(target_sd->sem_nsems); 3850 host_sd->sem_otime = tswapal(target_sd->sem_otime); 3851 host_sd->sem_ctime = tswapal(target_sd->sem_ctime); 3852 unlock_user_struct(target_sd, target_addr, 0); 3853 return 0; 3854 } 3855 3856 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr, 3857 struct semid_ds *host_sd) 3858 { 3859 struct target_semid64_ds *target_sd; 3860 3861 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) 3862 return -TARGET_EFAULT; 3863 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm))) 3864 return -TARGET_EFAULT; 3865 target_sd->sem_nsems = tswapal(host_sd->sem_nsems); 3866 target_sd->sem_otime = tswapal(host_sd->sem_otime); 3867 target_sd->sem_ctime = tswapal(host_sd->sem_ctime); 3868 unlock_user_struct(target_sd, target_addr, 1); 3869 return 0; 3870 } 3871 3872 struct target_seminfo { 3873 int semmap; 3874 int semmni; 3875 int semmns; 3876 int semmnu; 3877 int semmsl; 3878 int semopm; 3879 int semume; 3880 int semusz; 3881 int semvmx; 3882 int semaem; 3883 }; 3884 3885 static inline abi_long host_to_target_seminfo(abi_ulong target_addr, 3886 struct seminfo *host_seminfo) 3887 { 3888 struct target_seminfo *target_seminfo; 3889 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0)) 3890 return -TARGET_EFAULT; 3891 __put_user(host_seminfo->semmap, &target_seminfo->semmap); 3892 __put_user(host_seminfo->semmni, &target_seminfo->semmni); 3893 __put_user(host_seminfo->semmns, &target_seminfo->semmns); 3894 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu); 3895 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl); 3896 __put_user(host_seminfo->semopm, &target_seminfo->semopm); 3897 __put_user(host_seminfo->semume, &target_seminfo->semume); 3898 __put_user(host_seminfo->semusz, &target_seminfo->semusz); 3899 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx); 3900 __put_user(host_seminfo->semaem, &target_seminfo->semaem); 3901 unlock_user_struct(target_seminfo, target_addr, 1); 3902 return 0; 3903 } 3904 3905 union semun { 3906 int val; 3907 struct semid_ds *buf; 3908 unsigned short *array; 3909 struct seminfo *__buf; 3910 }; 3911 3912 union target_semun { 3913 int val; 3914 abi_ulong buf; 3915 abi_ulong array; 3916 abi_ulong __buf; 3917 }; 3918 3919 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array, 3920 abi_ulong target_addr) 3921 { 3922 int nsems; 3923 unsigned short *array; 3924 union semun semun; 3925 struct semid_ds semid_ds; 3926 int i, ret; 3927 3928 semun.buf = &semid_ds; 3929 3930 ret = semctl(semid, 0, IPC_STAT, semun); 3931 if (ret == -1) 3932 return get_errno(ret); 3933 3934 nsems = semid_ds.sem_nsems; 3935 3936 *host_array = g_try_new(unsigned short, nsems); 3937 if (!*host_array) { 3938 return -TARGET_ENOMEM; 3939 } 3940 array = lock_user(VERIFY_READ, target_addr, 3941 nsems*sizeof(unsigned short), 1); 3942 if (!array) { 3943 g_free(*host_array); 3944 return -TARGET_EFAULT; 3945 } 3946 3947 for(i=0; i<nsems; i++) { 3948 __get_user((*host_array)[i], &array[i]); 3949 } 3950 unlock_user(array, target_addr, 0); 3951 3952 return 0; 3953 } 3954 3955 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr, 3956 unsigned short **host_array) 3957 { 3958 int nsems; 3959 unsigned short *array; 3960 union semun semun; 3961 struct semid_ds semid_ds; 3962 int i, ret; 3963 3964 semun.buf = &semid_ds; 3965 3966 ret = semctl(semid, 0, IPC_STAT, semun); 3967 if (ret == -1) 3968 return get_errno(ret); 3969 3970 nsems = semid_ds.sem_nsems; 3971 3972 array = lock_user(VERIFY_WRITE, target_addr, 3973 nsems*sizeof(unsigned short), 0); 3974 if (!array) 3975 return -TARGET_EFAULT; 3976 3977 for(i=0; i<nsems; i++) { 3978 __put_user((*host_array)[i], &array[i]); 3979 } 3980 g_free(*host_array); 3981 unlock_user(array, target_addr, 1); 3982 3983 return 0; 3984 } 3985 3986 static inline abi_long do_semctl(int semid, int semnum, int cmd, 3987 abi_ulong target_arg) 3988 { 3989 union target_semun target_su = { .buf = target_arg }; 3990 union semun arg; 3991 struct semid_ds dsarg; 3992 unsigned short *array = NULL; 3993 struct seminfo seminfo; 3994 abi_long ret = -TARGET_EINVAL; 3995 abi_long err; 3996 cmd &= 0xff; 3997 3998 switch( cmd ) { 3999 case GETVAL: 4000 case SETVAL: 4001 /* In 64 bit cross-endian situations, we will erroneously pick up 4002 * the wrong half of the union for the "val" element. To rectify 4003 * this, the entire 8-byte structure is byteswapped, followed by 4004 * a swap of the 4 byte val field. In other cases, the data is 4005 * already in proper host byte order. */ 4006 if (sizeof(target_su.val) != (sizeof(target_su.buf))) { 4007 target_su.buf = tswapal(target_su.buf); 4008 arg.val = tswap32(target_su.val); 4009 } else { 4010 arg.val = target_su.val; 4011 } 4012 ret = get_errno(semctl(semid, semnum, cmd, arg)); 4013 break; 4014 case GETALL: 4015 case SETALL: 4016 err = target_to_host_semarray(semid, &array, target_su.array); 4017 if (err) 4018 return err; 4019 arg.array = array; 4020 ret = get_errno(semctl(semid, semnum, cmd, arg)); 4021 err = host_to_target_semarray(semid, target_su.array, &array); 4022 if (err) 4023 return err; 4024 break; 4025 case IPC_STAT: 4026 case IPC_SET: 4027 case SEM_STAT: 4028 err = target_to_host_semid_ds(&dsarg, target_su.buf); 4029 if (err) 4030 return err; 4031 arg.buf = &dsarg; 4032 ret = get_errno(semctl(semid, semnum, cmd, arg)); 4033 err = host_to_target_semid_ds(target_su.buf, &dsarg); 4034 if (err) 4035 return err; 4036 break; 4037 case IPC_INFO: 4038 case SEM_INFO: 4039 arg.__buf = &seminfo; 4040 ret = get_errno(semctl(semid, semnum, cmd, arg)); 4041 err = host_to_target_seminfo(target_su.__buf, &seminfo); 4042 if (err) 4043 return err; 4044 break; 4045 case IPC_RMID: 4046 case GETPID: 4047 case GETNCNT: 4048 case GETZCNT: 4049 ret = get_errno(semctl(semid, semnum, cmd, NULL)); 4050 break; 4051 } 4052 4053 return ret; 4054 } 4055 4056 struct target_sembuf { 4057 unsigned short sem_num; 4058 short sem_op; 4059 short sem_flg; 4060 }; 4061 4062 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, 4063 abi_ulong target_addr, 4064 unsigned nsops) 4065 { 4066 struct target_sembuf *target_sembuf; 4067 int i; 4068 4069 target_sembuf = lock_user(VERIFY_READ, target_addr, 4070 nsops*sizeof(struct target_sembuf), 1); 4071 if (!target_sembuf) 4072 return -TARGET_EFAULT; 4073 4074 for(i=0; i<nsops; i++) { 4075 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num); 4076 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op); 4077 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg); 4078 } 4079 4080 unlock_user(target_sembuf, target_addr, 0); 4081 4082 return 0; 4083 } 4084 4085 #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ 4086 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) 4087 4088 /* 4089 * This macro is required to handle the s390 variants, which passes the 4090 * arguments in a different order than default. 4091 */ 4092 #ifdef __s390x__ 4093 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \ 4094 (__nsops), (__timeout), (__sops) 4095 #else 4096 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \ 4097 (__nsops), 0, (__sops), (__timeout) 4098 #endif 4099 4100 static inline abi_long do_semtimedop(int semid, 4101 abi_long ptr, 4102 unsigned nsops, 4103 abi_long timeout, bool time64) 4104 { 4105 struct sembuf *sops; 4106 struct timespec ts, *pts = NULL; 4107 abi_long ret; 4108 4109 if (timeout) { 4110 pts = &ts; 4111 if (time64) { 4112 if (target_to_host_timespec64(pts, timeout)) { 4113 return -TARGET_EFAULT; 4114 } 4115 } else { 4116 if (target_to_host_timespec(pts, timeout)) { 4117 return -TARGET_EFAULT; 4118 } 4119 } 4120 } 4121 4122 if (nsops > TARGET_SEMOPM) { 4123 return -TARGET_E2BIG; 4124 } 4125 4126 sops = g_new(struct sembuf, nsops); 4127 4128 if (target_to_host_sembuf(sops, ptr, nsops)) { 4129 g_free(sops); 4130 return -TARGET_EFAULT; 4131 } 4132 4133 ret = -TARGET_ENOSYS; 4134 #ifdef __NR_semtimedop 4135 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts)); 4136 #endif 4137 #ifdef __NR_ipc 4138 if (ret == -TARGET_ENOSYS) { 4139 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, 4140 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts))); 4141 } 4142 #endif 4143 g_free(sops); 4144 return ret; 4145 } 4146 #endif 4147 4148 struct target_msqid_ds 4149 { 4150 struct target_ipc_perm msg_perm; 4151 abi_ulong msg_stime; 4152 #if TARGET_ABI_BITS == 32 4153 abi_ulong __unused1; 4154 #endif 4155 abi_ulong msg_rtime; 4156 #if TARGET_ABI_BITS == 32 4157 abi_ulong __unused2; 4158 #endif 4159 abi_ulong msg_ctime; 4160 #if TARGET_ABI_BITS == 32 4161 abi_ulong __unused3; 4162 #endif 4163 abi_ulong __msg_cbytes; 4164 abi_ulong msg_qnum; 4165 abi_ulong msg_qbytes; 4166 abi_ulong msg_lspid; 4167 abi_ulong msg_lrpid; 4168 abi_ulong __unused4; 4169 abi_ulong __unused5; 4170 }; 4171 4172 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md, 4173 abi_ulong target_addr) 4174 { 4175 struct target_msqid_ds *target_md; 4176 4177 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1)) 4178 return -TARGET_EFAULT; 4179 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr)) 4180 return -TARGET_EFAULT; 4181 host_md->msg_stime = tswapal(target_md->msg_stime); 4182 host_md->msg_rtime = tswapal(target_md->msg_rtime); 4183 host_md->msg_ctime = tswapal(target_md->msg_ctime); 4184 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes); 4185 host_md->msg_qnum = tswapal(target_md->msg_qnum); 4186 host_md->msg_qbytes = tswapal(target_md->msg_qbytes); 4187 host_md->msg_lspid = tswapal(target_md->msg_lspid); 4188 host_md->msg_lrpid = tswapal(target_md->msg_lrpid); 4189 unlock_user_struct(target_md, target_addr, 0); 4190 return 0; 4191 } 4192 4193 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr, 4194 struct msqid_ds *host_md) 4195 { 4196 struct target_msqid_ds *target_md; 4197 4198 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) 4199 return -TARGET_EFAULT; 4200 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm))) 4201 return -TARGET_EFAULT; 4202 target_md->msg_stime = tswapal(host_md->msg_stime); 4203 target_md->msg_rtime = tswapal(host_md->msg_rtime); 4204 target_md->msg_ctime = tswapal(host_md->msg_ctime); 4205 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes); 4206 target_md->msg_qnum = tswapal(host_md->msg_qnum); 4207 target_md->msg_qbytes = tswapal(host_md->msg_qbytes); 4208 target_md->msg_lspid = tswapal(host_md->msg_lspid); 4209 target_md->msg_lrpid = tswapal(host_md->msg_lrpid); 4210 unlock_user_struct(target_md, target_addr, 1); 4211 return 0; 4212 } 4213 4214 struct target_msginfo { 4215 int msgpool; 4216 int msgmap; 4217 int msgmax; 4218 int msgmnb; 4219 int msgmni; 4220 int msgssz; 4221 int msgtql; 4222 unsigned short int msgseg; 4223 }; 4224 4225 static inline abi_long host_to_target_msginfo(abi_ulong target_addr, 4226 struct msginfo *host_msginfo) 4227 { 4228 struct target_msginfo *target_msginfo; 4229 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0)) 4230 return -TARGET_EFAULT; 4231 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool); 4232 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap); 4233 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax); 4234 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb); 4235 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni); 4236 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz); 4237 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql); 4238 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg); 4239 unlock_user_struct(target_msginfo, target_addr, 1); 4240 return 0; 4241 } 4242 4243 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr) 4244 { 4245 struct msqid_ds dsarg; 4246 struct msginfo msginfo; 4247 abi_long ret = -TARGET_EINVAL; 4248 4249 cmd &= 0xff; 4250 4251 switch (cmd) { 4252 case IPC_STAT: 4253 case IPC_SET: 4254 case MSG_STAT: 4255 if (target_to_host_msqid_ds(&dsarg,ptr)) 4256 return -TARGET_EFAULT; 4257 ret = get_errno(msgctl(msgid, cmd, &dsarg)); 4258 if (host_to_target_msqid_ds(ptr,&dsarg)) 4259 return -TARGET_EFAULT; 4260 break; 4261 case IPC_RMID: 4262 ret = get_errno(msgctl(msgid, cmd, NULL)); 4263 break; 4264 case IPC_INFO: 4265 case MSG_INFO: 4266 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo)); 4267 if (host_to_target_msginfo(ptr, &msginfo)) 4268 return -TARGET_EFAULT; 4269 break; 4270 } 4271 4272 return ret; 4273 } 4274 4275 struct target_msgbuf { 4276 abi_long mtype; 4277 char mtext[1]; 4278 }; 4279 4280 static inline abi_long do_msgsnd(int msqid, abi_long msgp, 4281 ssize_t msgsz, int msgflg) 4282 { 4283 struct target_msgbuf *target_mb; 4284 struct msgbuf *host_mb; 4285 abi_long ret = 0; 4286 4287 if (msgsz < 0) { 4288 return -TARGET_EINVAL; 4289 } 4290 4291 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0)) 4292 return -TARGET_EFAULT; 4293 host_mb = g_try_malloc(msgsz + sizeof(long)); 4294 if (!host_mb) { 4295 unlock_user_struct(target_mb, msgp, 0); 4296 return -TARGET_ENOMEM; 4297 } 4298 host_mb->mtype = (abi_long) tswapal(target_mb->mtype); 4299 memcpy(host_mb->mtext, target_mb->mtext, msgsz); 4300 ret = -TARGET_ENOSYS; 4301 #ifdef __NR_msgsnd 4302 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg)); 4303 #endif 4304 #ifdef __NR_ipc 4305 if (ret == -TARGET_ENOSYS) { 4306 #ifdef __s390x__ 4307 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, 4308 host_mb)); 4309 #else 4310 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg, 4311 host_mb, 0)); 4312 #endif 4313 } 4314 #endif 4315 g_free(host_mb); 4316 unlock_user_struct(target_mb, msgp, 0); 4317 4318 return ret; 4319 } 4320 4321 #ifdef __NR_ipc 4322 #if defined(__sparc__) 4323 /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */ 4324 #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp 4325 #elif defined(__s390x__) 4326 /* The s390 sys_ipc variant has only five parameters. */ 4327 #define MSGRCV_ARGS(__msgp, __msgtyp) \ 4328 ((long int[]){(long int)__msgp, __msgtyp}) 4329 #else 4330 #define MSGRCV_ARGS(__msgp, __msgtyp) \ 4331 ((long int[]){(long int)__msgp, __msgtyp}), 0 4332 #endif 4333 #endif 4334 4335 static inline abi_long do_msgrcv(int msqid, abi_long msgp, 4336 ssize_t msgsz, abi_long msgtyp, 4337 int msgflg) 4338 { 4339 struct target_msgbuf *target_mb; 4340 char *target_mtext; 4341 struct msgbuf *host_mb; 4342 abi_long ret = 0; 4343 4344 if (msgsz < 0) { 4345 return -TARGET_EINVAL; 4346 } 4347 4348 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0)) 4349 return -TARGET_EFAULT; 4350 4351 host_mb = g_try_malloc(msgsz + sizeof(long)); 4352 if (!host_mb) { 4353 ret = -TARGET_ENOMEM; 4354 goto end; 4355 } 4356 ret = -TARGET_ENOSYS; 4357 #ifdef __NR_msgrcv 4358 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg)); 4359 #endif 4360 #ifdef __NR_ipc 4361 if (ret == -TARGET_ENOSYS) { 4362 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz, 4363 msgflg, MSGRCV_ARGS(host_mb, msgtyp))); 4364 } 4365 #endif 4366 4367 if (ret > 0) { 4368 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong); 4369 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0); 4370 if (!target_mtext) { 4371 ret = -TARGET_EFAULT; 4372 goto end; 4373 } 4374 memcpy(target_mb->mtext, host_mb->mtext, ret); 4375 unlock_user(target_mtext, target_mtext_addr, ret); 4376 } 4377 4378 target_mb->mtype = tswapal(host_mb->mtype); 4379 4380 end: 4381 if (target_mb) 4382 unlock_user_struct(target_mb, msgp, 1); 4383 g_free(host_mb); 4384 return ret; 4385 } 4386 4387 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd, 4388 abi_ulong target_addr) 4389 { 4390 struct target_shmid_ds *target_sd; 4391 4392 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) 4393 return -TARGET_EFAULT; 4394 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr)) 4395 return -TARGET_EFAULT; 4396 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz); 4397 __get_user(host_sd->shm_atime, &target_sd->shm_atime); 4398 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime); 4399 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime); 4400 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid); 4401 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid); 4402 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch); 4403 unlock_user_struct(target_sd, target_addr, 0); 4404 return 0; 4405 } 4406 4407 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr, 4408 struct shmid_ds *host_sd) 4409 { 4410 struct target_shmid_ds *target_sd; 4411 4412 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) 4413 return -TARGET_EFAULT; 4414 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm))) 4415 return -TARGET_EFAULT; 4416 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz); 4417 __put_user(host_sd->shm_atime, &target_sd->shm_atime); 4418 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime); 4419 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime); 4420 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid); 4421 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid); 4422 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch); 4423 unlock_user_struct(target_sd, target_addr, 1); 4424 return 0; 4425 } 4426 4427 struct target_shminfo { 4428 abi_ulong shmmax; 4429 abi_ulong shmmin; 4430 abi_ulong shmmni; 4431 abi_ulong shmseg; 4432 abi_ulong shmall; 4433 }; 4434 4435 static inline abi_long host_to_target_shminfo(abi_ulong target_addr, 4436 struct shminfo *host_shminfo) 4437 { 4438 struct target_shminfo *target_shminfo; 4439 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0)) 4440 return -TARGET_EFAULT; 4441 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax); 4442 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin); 4443 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni); 4444 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg); 4445 __put_user(host_shminfo->shmall, &target_shminfo->shmall); 4446 unlock_user_struct(target_shminfo, target_addr, 1); 4447 return 0; 4448 } 4449 4450 struct target_shm_info { 4451 int used_ids; 4452 abi_ulong shm_tot; 4453 abi_ulong shm_rss; 4454 abi_ulong shm_swp; 4455 abi_ulong swap_attempts; 4456 abi_ulong swap_successes; 4457 }; 4458 4459 static inline abi_long host_to_target_shm_info(abi_ulong target_addr, 4460 struct shm_info *host_shm_info) 4461 { 4462 struct target_shm_info *target_shm_info; 4463 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0)) 4464 return -TARGET_EFAULT; 4465 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids); 4466 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot); 4467 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss); 4468 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp); 4469 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts); 4470 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes); 4471 unlock_user_struct(target_shm_info, target_addr, 1); 4472 return 0; 4473 } 4474 4475 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf) 4476 { 4477 struct shmid_ds dsarg; 4478 struct shminfo shminfo; 4479 struct shm_info shm_info; 4480 abi_long ret = -TARGET_EINVAL; 4481 4482 cmd &= 0xff; 4483 4484 switch(cmd) { 4485 case IPC_STAT: 4486 case IPC_SET: 4487 case SHM_STAT: 4488 if (target_to_host_shmid_ds(&dsarg, buf)) 4489 return -TARGET_EFAULT; 4490 ret = get_errno(shmctl(shmid, cmd, &dsarg)); 4491 if (host_to_target_shmid_ds(buf, &dsarg)) 4492 return -TARGET_EFAULT; 4493 break; 4494 case IPC_INFO: 4495 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo)); 4496 if (host_to_target_shminfo(buf, &shminfo)) 4497 return -TARGET_EFAULT; 4498 break; 4499 case SHM_INFO: 4500 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info)); 4501 if (host_to_target_shm_info(buf, &shm_info)) 4502 return -TARGET_EFAULT; 4503 break; 4504 case IPC_RMID: 4505 case SHM_LOCK: 4506 case SHM_UNLOCK: 4507 ret = get_errno(shmctl(shmid, cmd, NULL)); 4508 break; 4509 } 4510 4511 return ret; 4512 } 4513 4514 #ifndef TARGET_FORCE_SHMLBA 4515 /* For most architectures, SHMLBA is the same as the page size; 4516 * some architectures have larger values, in which case they should 4517 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function. 4518 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA 4519 * and defining its own value for SHMLBA. 4520 * 4521 * The kernel also permits SHMLBA to be set by the architecture to a 4522 * value larger than the page size without setting __ARCH_FORCE_SHMLBA; 4523 * this means that addresses are rounded to the large size if 4524 * SHM_RND is set but addresses not aligned to that size are not rejected 4525 * as long as they are at least page-aligned. Since the only architecture 4526 * which uses this is ia64 this code doesn't provide for that oddity. 4527 */ 4528 static inline abi_ulong target_shmlba(CPUArchState *cpu_env) 4529 { 4530 return TARGET_PAGE_SIZE; 4531 } 4532 #endif 4533 4534 static inline abi_ulong do_shmat(CPUArchState *cpu_env, 4535 int shmid, abi_ulong shmaddr, int shmflg) 4536 { 4537 CPUState *cpu = env_cpu(cpu_env); 4538 abi_long raddr; 4539 void *host_raddr; 4540 struct shmid_ds shm_info; 4541 int i,ret; 4542 abi_ulong shmlba; 4543 4544 /* shmat pointers are always untagged */ 4545 4546 /* find out the length of the shared memory segment */ 4547 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); 4548 if (is_error(ret)) { 4549 /* can't get length, bail out */ 4550 return ret; 4551 } 4552 4553 shmlba = target_shmlba(cpu_env); 4554 4555 if (shmaddr & (shmlba - 1)) { 4556 if (shmflg & SHM_RND) { 4557 shmaddr &= ~(shmlba - 1); 4558 } else { 4559 return -TARGET_EINVAL; 4560 } 4561 } 4562 if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) { 4563 return -TARGET_EINVAL; 4564 } 4565 4566 mmap_lock(); 4567 4568 /* 4569 * We're mapping shared memory, so ensure we generate code for parallel 4570 * execution and flush old translations. This will work up to the level 4571 * supported by the host -- anything that requires EXCP_ATOMIC will not 4572 * be atomic with respect to an external process. 4573 */ 4574 if (!(cpu->tcg_cflags & CF_PARALLEL)) { 4575 cpu->tcg_cflags |= CF_PARALLEL; 4576 tb_flush(cpu); 4577 } 4578 4579 if (shmaddr) 4580 host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); 4581 else { 4582 abi_ulong mmap_start; 4583 4584 /* In order to use the host shmat, we need to honor host SHMLBA. */ 4585 mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba)); 4586 4587 if (mmap_start == -1) { 4588 errno = ENOMEM; 4589 host_raddr = (void *)-1; 4590 } else 4591 host_raddr = shmat(shmid, g2h_untagged(mmap_start), 4592 shmflg | SHM_REMAP); 4593 } 4594 4595 if (host_raddr == (void *)-1) { 4596 mmap_unlock(); 4597 return get_errno((long)host_raddr); 4598 } 4599 raddr=h2g((unsigned long)host_raddr); 4600 4601 page_set_flags(raddr, raddr + shm_info.shm_segsz, 4602 PAGE_VALID | PAGE_RESET | PAGE_READ | 4603 (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); 4604 4605 for (i = 0; i < N_SHM_REGIONS; i++) { 4606 if (!shm_regions[i].in_use) { 4607 shm_regions[i].in_use = true; 4608 shm_regions[i].start = raddr; 4609 shm_regions[i].size = shm_info.shm_segsz; 4610 break; 4611 } 4612 } 4613 4614 mmap_unlock(); 4615 return raddr; 4616 4617 } 4618 4619 static inline abi_long do_shmdt(abi_ulong shmaddr) 4620 { 4621 int i; 4622 abi_long rv; 4623 4624 /* shmdt pointers are always untagged */ 4625 4626 mmap_lock(); 4627 4628 for (i = 0; i < N_SHM_REGIONS; ++i) { 4629 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { 4630 shm_regions[i].in_use = false; 4631 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0); 4632 break; 4633 } 4634 } 4635 rv = get_errno(shmdt(g2h_untagged(shmaddr))); 4636 4637 mmap_unlock(); 4638 4639 return rv; 4640 } 4641 4642 #ifdef TARGET_NR_ipc 4643 /* ??? This only works with linear mappings. */ 4644 /* do_ipc() must return target values and target errnos. */ 4645 static abi_long do_ipc(CPUArchState *cpu_env, 4646 unsigned int call, abi_long first, 4647 abi_long second, abi_long third, 4648 abi_long ptr, abi_long fifth) 4649 { 4650 int version; 4651 abi_long ret = 0; 4652 4653 version = call >> 16; 4654 call &= 0xffff; 4655 4656 switch (call) { 4657 case IPCOP_semop: 4658 ret = do_semtimedop(first, ptr, second, 0, false); 4659 break; 4660 case IPCOP_semtimedop: 4661 /* 4662 * The s390 sys_ipc variant has only five parameters instead of six 4663 * (as for default variant) and the only difference is the handling of 4664 * SEMTIMEDOP where on s390 the third parameter is used as a pointer 4665 * to a struct timespec where the generic variant uses fifth parameter. 4666 */ 4667 #if defined(TARGET_S390X) 4668 ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); 4669 #else 4670 ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); 4671 #endif 4672 break; 4673 4674 case IPCOP_semget: 4675 ret = get_errno(semget(first, second, third)); 4676 break; 4677 4678 case IPCOP_semctl: { 4679 /* The semun argument to semctl is passed by value, so dereference the 4680 * ptr argument. */ 4681 abi_ulong atptr; 4682 get_user_ual(atptr, ptr); 4683 ret = do_semctl(first, second, third, atptr); 4684 break; 4685 } 4686 4687 case IPCOP_msgget: 4688 ret = get_errno(msgget(first, second)); 4689 break; 4690 4691 case IPCOP_msgsnd: 4692 ret = do_msgsnd(first, ptr, second, third); 4693 break; 4694 4695 case IPCOP_msgctl: 4696 ret = do_msgctl(first, second, ptr); 4697 break; 4698 4699 case IPCOP_msgrcv: 4700 switch (version) { 4701 case 0: 4702 { 4703 struct target_ipc_kludge { 4704 abi_long msgp; 4705 abi_long msgtyp; 4706 } *tmp; 4707 4708 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) { 4709 ret = -TARGET_EFAULT; 4710 break; 4711 } 4712 4713 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third); 4714 4715 unlock_user_struct(tmp, ptr, 0); 4716 break; 4717 } 4718 default: 4719 ret = do_msgrcv(first, ptr, second, fifth, third); 4720 } 4721 break; 4722 4723 case IPCOP_shmat: 4724 switch (version) { 4725 default: 4726 { 4727 abi_ulong raddr; 4728 raddr = do_shmat(cpu_env, first, ptr, second); 4729 if (is_error(raddr)) 4730 return get_errno(raddr); 4731 if (put_user_ual(raddr, third)) 4732 return -TARGET_EFAULT; 4733 break; 4734 } 4735 case 1: 4736 ret = -TARGET_EINVAL; 4737 break; 4738 } 4739 break; 4740 case IPCOP_shmdt: 4741 ret = do_shmdt(ptr); 4742 break; 4743 4744 case IPCOP_shmget: 4745 /* IPC_* flag values are the same on all linux platforms */ 4746 ret = get_errno(shmget(first, second, third)); 4747 break; 4748 4749 /* IPC_* and SHM_* command values are the same on all linux platforms */ 4750 case IPCOP_shmctl: 4751 ret = do_shmctl(first, second, ptr); 4752 break; 4753 default: 4754 qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n", 4755 call, version); 4756 ret = -TARGET_ENOSYS; 4757 break; 4758 } 4759 return ret; 4760 } 4761 #endif 4762 4763 /* kernel structure types definitions */ 4764 4765 #define STRUCT(name, ...) STRUCT_ ## name, 4766 #define STRUCT_SPECIAL(name) STRUCT_ ## name, 4767 enum { 4768 #include "syscall_types.h" 4769 STRUCT_MAX 4770 }; 4771 #undef STRUCT 4772 #undef STRUCT_SPECIAL 4773 4774 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL }; 4775 #define STRUCT_SPECIAL(name) 4776 #include "syscall_types.h" 4777 #undef STRUCT 4778 #undef STRUCT_SPECIAL 4779 4780 #define MAX_STRUCT_SIZE 4096 4781 4782 #ifdef CONFIG_FIEMAP 4783 /* So fiemap access checks don't overflow on 32 bit systems. 4784 * This is very slightly smaller than the limit imposed by 4785 * the underlying kernel. 4786 */ 4787 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \ 4788 / sizeof(struct fiemap_extent)) 4789 4790 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp, 4791 int fd, int cmd, abi_long arg) 4792 { 4793 /* The parameter for this ioctl is a struct fiemap followed 4794 * by an array of struct fiemap_extent whose size is set 4795 * in fiemap->fm_extent_count. The array is filled in by the 4796 * ioctl. 4797 */ 4798 int target_size_in, target_size_out; 4799 struct fiemap *fm; 4800 const argtype *arg_type = ie->arg_type; 4801 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) }; 4802 void *argptr, *p; 4803 abi_long ret; 4804 int i, extent_size = thunk_type_size(extent_arg_type, 0); 4805 uint32_t outbufsz; 4806 int free_fm = 0; 4807 4808 assert(arg_type[0] == TYPE_PTR); 4809 assert(ie->access == IOC_RW); 4810 arg_type++; 4811 target_size_in = thunk_type_size(arg_type, 0); 4812 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1); 4813 if (!argptr) { 4814 return -TARGET_EFAULT; 4815 } 4816 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); 4817 unlock_user(argptr, arg, 0); 4818 fm = (struct fiemap *)buf_temp; 4819 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) { 4820 return -TARGET_EINVAL; 4821 } 4822 4823 outbufsz = sizeof (*fm) + 4824 (sizeof(struct fiemap_extent) * fm->fm_extent_count); 4825 4826 if (outbufsz > MAX_STRUCT_SIZE) { 4827 /* We can't fit all the extents into the fixed size buffer. 4828 * Allocate one that is large enough and use it instead. 4829 */ 4830 fm = g_try_malloc(outbufsz); 4831 if (!fm) { 4832 return -TARGET_ENOMEM; 4833 } 4834 memcpy(fm, buf_temp, sizeof(struct fiemap)); 4835 free_fm = 1; 4836 } 4837 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm)); 4838 if (!is_error(ret)) { 4839 target_size_out = target_size_in; 4840 /* An extent_count of 0 means we were only counting the extents 4841 * so there are no structs to copy 4842 */ 4843 if (fm->fm_extent_count != 0) { 4844 target_size_out += fm->fm_mapped_extents * extent_size; 4845 } 4846 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0); 4847 if (!argptr) { 4848 ret = -TARGET_EFAULT; 4849 } else { 4850 /* Convert the struct fiemap */ 4851 thunk_convert(argptr, fm, arg_type, THUNK_TARGET); 4852 if (fm->fm_extent_count != 0) { 4853 p = argptr + target_size_in; 4854 /* ...and then all the struct fiemap_extents */ 4855 for (i = 0; i < fm->fm_mapped_extents; i++) { 4856 thunk_convert(p, &fm->fm_extents[i], extent_arg_type, 4857 THUNK_TARGET); 4858 p += extent_size; 4859 } 4860 } 4861 unlock_user(argptr, arg, target_size_out); 4862 } 4863 } 4864 if (free_fm) { 4865 g_free(fm); 4866 } 4867 return ret; 4868 } 4869 #endif 4870 4871 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, 4872 int fd, int cmd, abi_long arg) 4873 { 4874 const argtype *arg_type = ie->arg_type; 4875 int target_size; 4876 void *argptr; 4877 int ret; 4878 struct ifconf *host_ifconf; 4879 uint32_t outbufsz; 4880 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) }; 4881 const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) }; 4882 int target_ifreq_size; 4883 int nb_ifreq; 4884 int free_buf = 0; 4885 int i; 4886 int target_ifc_len; 4887 abi_long target_ifc_buf; 4888 int host_ifc_len; 4889 char *host_ifc_buf; 4890 4891 assert(arg_type[0] == TYPE_PTR); 4892 assert(ie->access == IOC_RW); 4893 4894 arg_type++; 4895 target_size = thunk_type_size(arg_type, 0); 4896 4897 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 4898 if (!argptr) 4899 return -TARGET_EFAULT; 4900 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); 4901 unlock_user(argptr, arg, 0); 4902 4903 host_ifconf = (struct ifconf *)(unsigned long)buf_temp; 4904 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf; 4905 target_ifreq_size = thunk_type_size(ifreq_max_type, 0); 4906 4907 if (target_ifc_buf != 0) { 4908 target_ifc_len = host_ifconf->ifc_len; 4909 nb_ifreq = target_ifc_len / target_ifreq_size; 4910 host_ifc_len = nb_ifreq * sizeof(struct ifreq); 4911 4912 outbufsz = sizeof(*host_ifconf) + host_ifc_len; 4913 if (outbufsz > MAX_STRUCT_SIZE) { 4914 /* 4915 * We can't fit all the extents into the fixed size buffer. 4916 * Allocate one that is large enough and use it instead. 4917 */ 4918 host_ifconf = g_try_malloc(outbufsz); 4919 if (!host_ifconf) { 4920 return -TARGET_ENOMEM; 4921 } 4922 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf)); 4923 free_buf = 1; 4924 } 4925 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf); 4926 4927 host_ifconf->ifc_len = host_ifc_len; 4928 } else { 4929 host_ifc_buf = NULL; 4930 } 4931 host_ifconf->ifc_buf = host_ifc_buf; 4932 4933 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf)); 4934 if (!is_error(ret)) { 4935 /* convert host ifc_len to target ifc_len */ 4936 4937 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq); 4938 target_ifc_len = nb_ifreq * target_ifreq_size; 4939 host_ifconf->ifc_len = target_ifc_len; 4940 4941 /* restore target ifc_buf */ 4942 4943 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf; 4944 4945 /* copy struct ifconf to target user */ 4946 4947 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); 4948 if (!argptr) 4949 return -TARGET_EFAULT; 4950 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET); 4951 unlock_user(argptr, arg, target_size); 4952 4953 if (target_ifc_buf != 0) { 4954 /* copy ifreq[] to target user */ 4955 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0); 4956 for (i = 0; i < nb_ifreq ; i++) { 4957 thunk_convert(argptr + i * target_ifreq_size, 4958 host_ifc_buf + i * sizeof(struct ifreq), 4959 ifreq_arg_type, THUNK_TARGET); 4960 } 4961 unlock_user(argptr, target_ifc_buf, target_ifc_len); 4962 } 4963 } 4964 4965 if (free_buf) { 4966 g_free(host_ifconf); 4967 } 4968 4969 return ret; 4970 } 4971 4972 #if defined(CONFIG_USBFS) 4973 #if HOST_LONG_BITS > 64 4974 #error USBDEVFS thunks do not support >64 bit hosts yet. 4975 #endif 4976 struct live_urb { 4977 uint64_t target_urb_adr; 4978 uint64_t target_buf_adr; 4979 char *target_buf_ptr; 4980 struct usbdevfs_urb host_urb; 4981 }; 4982 4983 static GHashTable *usbdevfs_urb_hashtable(void) 4984 { 4985 static GHashTable *urb_hashtable; 4986 4987 if (!urb_hashtable) { 4988 urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal); 4989 } 4990 return urb_hashtable; 4991 } 4992 4993 static void urb_hashtable_insert(struct live_urb *urb) 4994 { 4995 GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); 4996 g_hash_table_insert(urb_hashtable, urb, urb); 4997 } 4998 4999 static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr) 5000 { 5001 GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); 5002 return g_hash_table_lookup(urb_hashtable, &target_urb_adr); 5003 } 5004 5005 static void urb_hashtable_remove(struct live_urb *urb) 5006 { 5007 GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); 5008 g_hash_table_remove(urb_hashtable, urb); 5009 } 5010 5011 static abi_long 5012 do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp, 5013 int fd, int cmd, abi_long arg) 5014 { 5015 const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) }; 5016 const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 }; 5017 struct live_urb *lurb; 5018 void *argptr; 5019 uint64_t hurb; 5020 int target_size; 5021 uintptr_t target_urb_adr; 5022 abi_long ret; 5023 5024 target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET); 5025 5026 memset(buf_temp, 0, sizeof(uint64_t)); 5027 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); 5028 if (is_error(ret)) { 5029 return ret; 5030 } 5031 5032 memcpy(&hurb, buf_temp, sizeof(uint64_t)); 5033 lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb)); 5034 if (!lurb->target_urb_adr) { 5035 return -TARGET_EFAULT; 5036 } 5037 urb_hashtable_remove(lurb); 5038 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 5039 lurb->host_urb.buffer_length); 5040 lurb->target_buf_ptr = NULL; 5041 5042 /* restore the guest buffer pointer */ 5043 lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr; 5044 5045 /* update the guest urb struct */ 5046 argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0); 5047 if (!argptr) { 5048 g_free(lurb); 5049 return -TARGET_EFAULT; 5050 } 5051 thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET); 5052 unlock_user(argptr, lurb->target_urb_adr, target_size); 5053 5054 target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET); 5055 /* write back the urb handle */ 5056 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); 5057 if (!argptr) { 5058 g_free(lurb); 5059 return -TARGET_EFAULT; 5060 } 5061 5062 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */ 5063 target_urb_adr = lurb->target_urb_adr; 5064 thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET); 5065 unlock_user(argptr, arg, target_size); 5066 5067 g_free(lurb); 5068 return ret; 5069 } 5070 5071 static abi_long 5072 do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie, 5073 uint8_t *buf_temp __attribute__((unused)), 5074 int fd, int cmd, abi_long arg) 5075 { 5076 struct live_urb *lurb; 5077 5078 /* map target address back to host URB with metadata. */ 5079 lurb = urb_hashtable_lookup(arg); 5080 if (!lurb) { 5081 return -TARGET_EFAULT; 5082 } 5083 return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); 5084 } 5085 5086 static abi_long 5087 do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp, 5088 int fd, int cmd, abi_long arg) 5089 { 5090 const argtype *arg_type = ie->arg_type; 5091 int target_size; 5092 abi_long ret; 5093 void *argptr; 5094 int rw_dir; 5095 struct live_urb *lurb; 5096 5097 /* 5098 * each submitted URB needs to map to a unique ID for the 5099 * kernel, and that unique ID needs to be a pointer to 5100 * host memory. hence, we need to malloc for each URB. 5101 * isochronous transfers have a variable length struct. 5102 */ 5103 arg_type++; 5104 target_size = thunk_type_size(arg_type, THUNK_TARGET); 5105 5106 /* construct host copy of urb and metadata */ 5107 lurb = g_try_malloc0(sizeof(struct live_urb)); 5108 if (!lurb) { 5109 return -TARGET_ENOMEM; 5110 } 5111 5112 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5113 if (!argptr) { 5114 g_free(lurb); 5115 return -TARGET_EFAULT; 5116 } 5117 thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST); 5118 unlock_user(argptr, arg, 0); 5119 5120 lurb->target_urb_adr = arg; 5121 lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer; 5122 5123 /* buffer space used depends on endpoint type so lock the entire buffer */ 5124 /* control type urbs should check the buffer contents for true direction */ 5125 rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ; 5126 lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr, 5127 lurb->host_urb.buffer_length, 1); 5128 if (lurb->target_buf_ptr == NULL) { 5129 g_free(lurb); 5130 return -TARGET_EFAULT; 5131 } 5132 5133 /* update buffer pointer in host copy */ 5134 lurb->host_urb.buffer = lurb->target_buf_ptr; 5135 5136 ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); 5137 if (is_error(ret)) { 5138 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0); 5139 g_free(lurb); 5140 } else { 5141 urb_hashtable_insert(lurb); 5142 } 5143 5144 return ret; 5145 } 5146 #endif /* CONFIG_USBFS */ 5147 5148 static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd, 5149 int cmd, abi_long arg) 5150 { 5151 void *argptr; 5152 struct dm_ioctl *host_dm; 5153 abi_long guest_data; 5154 uint32_t guest_data_size; 5155 int target_size; 5156 const argtype *arg_type = ie->arg_type; 5157 abi_long ret; 5158 void *big_buf = NULL; 5159 char *host_data; 5160 5161 arg_type++; 5162 target_size = thunk_type_size(arg_type, 0); 5163 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5164 if (!argptr) { 5165 ret = -TARGET_EFAULT; 5166 goto out; 5167 } 5168 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); 5169 unlock_user(argptr, arg, 0); 5170 5171 /* buf_temp is too small, so fetch things into a bigger buffer */ 5172 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2); 5173 memcpy(big_buf, buf_temp, target_size); 5174 buf_temp = big_buf; 5175 host_dm = big_buf; 5176 5177 guest_data = arg + host_dm->data_start; 5178 if ((guest_data - arg) < 0) { 5179 ret = -TARGET_EINVAL; 5180 goto out; 5181 } 5182 guest_data_size = host_dm->data_size - host_dm->data_start; 5183 host_data = (char*)host_dm + host_dm->data_start; 5184 5185 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1); 5186 if (!argptr) { 5187 ret = -TARGET_EFAULT; 5188 goto out; 5189 } 5190 5191 switch (ie->host_cmd) { 5192 case DM_REMOVE_ALL: 5193 case DM_LIST_DEVICES: 5194 case DM_DEV_CREATE: 5195 case DM_DEV_REMOVE: 5196 case DM_DEV_SUSPEND: 5197 case DM_DEV_STATUS: 5198 case DM_DEV_WAIT: 5199 case DM_TABLE_STATUS: 5200 case DM_TABLE_CLEAR: 5201 case DM_TABLE_DEPS: 5202 case DM_LIST_VERSIONS: 5203 /* no input data */ 5204 break; 5205 case DM_DEV_RENAME: 5206 case DM_DEV_SET_GEOMETRY: 5207 /* data contains only strings */ 5208 memcpy(host_data, argptr, guest_data_size); 5209 break; 5210 case DM_TARGET_MSG: 5211 memcpy(host_data, argptr, guest_data_size); 5212 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr); 5213 break; 5214 case DM_TABLE_LOAD: 5215 { 5216 void *gspec = argptr; 5217 void *cur_data = host_data; 5218 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; 5219 int spec_size = thunk_type_size(arg_type, 0); 5220 int i; 5221 5222 for (i = 0; i < host_dm->target_count; i++) { 5223 struct dm_target_spec *spec = cur_data; 5224 uint32_t next; 5225 int slen; 5226 5227 thunk_convert(spec, gspec, arg_type, THUNK_HOST); 5228 slen = strlen((char*)gspec + spec_size) + 1; 5229 next = spec->next; 5230 spec->next = sizeof(*spec) + slen; 5231 strcpy((char*)&spec[1], gspec + spec_size); 5232 gspec += next; 5233 cur_data += spec->next; 5234 } 5235 break; 5236 } 5237 default: 5238 ret = -TARGET_EINVAL; 5239 unlock_user(argptr, guest_data, 0); 5240 goto out; 5241 } 5242 unlock_user(argptr, guest_data, 0); 5243 5244 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); 5245 if (!is_error(ret)) { 5246 guest_data = arg + host_dm->data_start; 5247 guest_data_size = host_dm->data_size - host_dm->data_start; 5248 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0); 5249 switch (ie->host_cmd) { 5250 case DM_REMOVE_ALL: 5251 case DM_DEV_CREATE: 5252 case DM_DEV_REMOVE: 5253 case DM_DEV_RENAME: 5254 case DM_DEV_SUSPEND: 5255 case DM_DEV_STATUS: 5256 case DM_TABLE_LOAD: 5257 case DM_TABLE_CLEAR: 5258 case DM_TARGET_MSG: 5259 case DM_DEV_SET_GEOMETRY: 5260 /* no return data */ 5261 break; 5262 case DM_LIST_DEVICES: 5263 { 5264 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start; 5265 uint32_t remaining_data = guest_data_size; 5266 void *cur_data = argptr; 5267 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) }; 5268 int nl_size = 12; /* can't use thunk_size due to alignment */ 5269 5270 while (1) { 5271 uint32_t next = nl->next; 5272 if (next) { 5273 nl->next = nl_size + (strlen(nl->name) + 1); 5274 } 5275 if (remaining_data < nl->next) { 5276 host_dm->flags |= DM_BUFFER_FULL_FLAG; 5277 break; 5278 } 5279 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET); 5280 strcpy(cur_data + nl_size, nl->name); 5281 cur_data += nl->next; 5282 remaining_data -= nl->next; 5283 if (!next) { 5284 break; 5285 } 5286 nl = (void*)nl + next; 5287 } 5288 break; 5289 } 5290 case DM_DEV_WAIT: 5291 case DM_TABLE_STATUS: 5292 { 5293 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start; 5294 void *cur_data = argptr; 5295 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) }; 5296 int spec_size = thunk_type_size(arg_type, 0); 5297 int i; 5298 5299 for (i = 0; i < host_dm->target_count; i++) { 5300 uint32_t next = spec->next; 5301 int slen = strlen((char*)&spec[1]) + 1; 5302 spec->next = (cur_data - argptr) + spec_size + slen; 5303 if (guest_data_size < spec->next) { 5304 host_dm->flags |= DM_BUFFER_FULL_FLAG; 5305 break; 5306 } 5307 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET); 5308 strcpy(cur_data + spec_size, (char*)&spec[1]); 5309 cur_data = argptr + spec->next; 5310 spec = (void*)host_dm + host_dm->data_start + next; 5311 } 5312 break; 5313 } 5314 case DM_TABLE_DEPS: 5315 { 5316 void *hdata = (void*)host_dm + host_dm->data_start; 5317 int count = *(uint32_t*)hdata; 5318 uint64_t *hdev = hdata + 8; 5319 uint64_t *gdev = argptr + 8; 5320 int i; 5321 5322 *(uint32_t*)argptr = tswap32(count); 5323 for (i = 0; i < count; i++) { 5324 *gdev = tswap64(*hdev); 5325 gdev++; 5326 hdev++; 5327 } 5328 break; 5329 } 5330 case DM_LIST_VERSIONS: 5331 { 5332 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start; 5333 uint32_t remaining_data = guest_data_size; 5334 void *cur_data = argptr; 5335 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) }; 5336 int vers_size = thunk_type_size(arg_type, 0); 5337 5338 while (1) { 5339 uint32_t next = vers->next; 5340 if (next) { 5341 vers->next = vers_size + (strlen(vers->name) + 1); 5342 } 5343 if (remaining_data < vers->next) { 5344 host_dm->flags |= DM_BUFFER_FULL_FLAG; 5345 break; 5346 } 5347 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET); 5348 strcpy(cur_data + vers_size, vers->name); 5349 cur_data += vers->next; 5350 remaining_data -= vers->next; 5351 if (!next) { 5352 break; 5353 } 5354 vers = (void*)vers + next; 5355 } 5356 break; 5357 } 5358 default: 5359 unlock_user(argptr, guest_data, 0); 5360 ret = -TARGET_EINVAL; 5361 goto out; 5362 } 5363 unlock_user(argptr, guest_data, guest_data_size); 5364 5365 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); 5366 if (!argptr) { 5367 ret = -TARGET_EFAULT; 5368 goto out; 5369 } 5370 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); 5371 unlock_user(argptr, arg, target_size); 5372 } 5373 out: 5374 g_free(big_buf); 5375 return ret; 5376 } 5377 5378 static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd, 5379 int cmd, abi_long arg) 5380 { 5381 void *argptr; 5382 int target_size; 5383 const argtype *arg_type = ie->arg_type; 5384 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) }; 5385 abi_long ret; 5386 5387 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp; 5388 struct blkpg_partition host_part; 5389 5390 /* Read and convert blkpg */ 5391 arg_type++; 5392 target_size = thunk_type_size(arg_type, 0); 5393 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5394 if (!argptr) { 5395 ret = -TARGET_EFAULT; 5396 goto out; 5397 } 5398 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); 5399 unlock_user(argptr, arg, 0); 5400 5401 switch (host_blkpg->op) { 5402 case BLKPG_ADD_PARTITION: 5403 case BLKPG_DEL_PARTITION: 5404 /* payload is struct blkpg_partition */ 5405 break; 5406 default: 5407 /* Unknown opcode */ 5408 ret = -TARGET_EINVAL; 5409 goto out; 5410 } 5411 5412 /* Read and convert blkpg->data */ 5413 arg = (abi_long)(uintptr_t)host_blkpg->data; 5414 target_size = thunk_type_size(part_arg_type, 0); 5415 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5416 if (!argptr) { 5417 ret = -TARGET_EFAULT; 5418 goto out; 5419 } 5420 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST); 5421 unlock_user(argptr, arg, 0); 5422 5423 /* Swizzle the data pointer to our local copy and call! */ 5424 host_blkpg->data = &host_part; 5425 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg)); 5426 5427 out: 5428 return ret; 5429 } 5430 5431 static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, 5432 int fd, int cmd, abi_long arg) 5433 { 5434 const argtype *arg_type = ie->arg_type; 5435 const StructEntry *se; 5436 const argtype *field_types; 5437 const int *dst_offsets, *src_offsets; 5438 int target_size; 5439 void *argptr; 5440 abi_ulong *target_rt_dev_ptr = NULL; 5441 unsigned long *host_rt_dev_ptr = NULL; 5442 abi_long ret; 5443 int i; 5444 5445 assert(ie->access == IOC_W); 5446 assert(*arg_type == TYPE_PTR); 5447 arg_type++; 5448 assert(*arg_type == TYPE_STRUCT); 5449 target_size = thunk_type_size(arg_type, 0); 5450 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5451 if (!argptr) { 5452 return -TARGET_EFAULT; 5453 } 5454 arg_type++; 5455 assert(*arg_type == (int)STRUCT_rtentry); 5456 se = struct_entries + *arg_type++; 5457 assert(se->convert[0] == NULL); 5458 /* convert struct here to be able to catch rt_dev string */ 5459 field_types = se->field_types; 5460 dst_offsets = se->field_offsets[THUNK_HOST]; 5461 src_offsets = se->field_offsets[THUNK_TARGET]; 5462 for (i = 0; i < se->nb_fields; i++) { 5463 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) { 5464 assert(*field_types == TYPE_PTRVOID); 5465 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]); 5466 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]); 5467 if (*target_rt_dev_ptr != 0) { 5468 *host_rt_dev_ptr = (unsigned long)lock_user_string( 5469 tswapal(*target_rt_dev_ptr)); 5470 if (!*host_rt_dev_ptr) { 5471 unlock_user(argptr, arg, 0); 5472 return -TARGET_EFAULT; 5473 } 5474 } else { 5475 *host_rt_dev_ptr = 0; 5476 } 5477 field_types++; 5478 continue; 5479 } 5480 field_types = thunk_convert(buf_temp + dst_offsets[i], 5481 argptr + src_offsets[i], 5482 field_types, THUNK_HOST); 5483 } 5484 unlock_user(argptr, arg, 0); 5485 5486 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); 5487 5488 assert(host_rt_dev_ptr != NULL); 5489 assert(target_rt_dev_ptr != NULL); 5490 if (*host_rt_dev_ptr != 0) { 5491 unlock_user((void *)*host_rt_dev_ptr, 5492 *target_rt_dev_ptr, 0); 5493 } 5494 return ret; 5495 } 5496 5497 static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp, 5498 int fd, int cmd, abi_long arg) 5499 { 5500 int sig = target_to_host_signal(arg); 5501 return get_errno(safe_ioctl(fd, ie->host_cmd, sig)); 5502 } 5503 5504 static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp, 5505 int fd, int cmd, abi_long arg) 5506 { 5507 struct timeval tv; 5508 abi_long ret; 5509 5510 ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv)); 5511 if (is_error(ret)) { 5512 return ret; 5513 } 5514 5515 if (cmd == (int)TARGET_SIOCGSTAMP_OLD) { 5516 if (copy_to_user_timeval(arg, &tv)) { 5517 return -TARGET_EFAULT; 5518 } 5519 } else { 5520 if (copy_to_user_timeval64(arg, &tv)) { 5521 return -TARGET_EFAULT; 5522 } 5523 } 5524 5525 return ret; 5526 } 5527 5528 static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp, 5529 int fd, int cmd, abi_long arg) 5530 { 5531 struct timespec ts; 5532 abi_long ret; 5533 5534 ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts)); 5535 if (is_error(ret)) { 5536 return ret; 5537 } 5538 5539 if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) { 5540 if (host_to_target_timespec(arg, &ts)) { 5541 return -TARGET_EFAULT; 5542 } 5543 } else{ 5544 if (host_to_target_timespec64(arg, &ts)) { 5545 return -TARGET_EFAULT; 5546 } 5547 } 5548 5549 return ret; 5550 } 5551 5552 #ifdef TIOCGPTPEER 5553 static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp, 5554 int fd, int cmd, abi_long arg) 5555 { 5556 int flags = target_to_host_bitmask(arg, fcntl_flags_tbl); 5557 return get_errno(safe_ioctl(fd, ie->host_cmd, flags)); 5558 } 5559 #endif 5560 5561 #ifdef HAVE_DRM_H 5562 5563 static void unlock_drm_version(struct drm_version *host_ver, 5564 struct target_drm_version *target_ver, 5565 bool copy) 5566 { 5567 unlock_user(host_ver->name, target_ver->name, 5568 copy ? host_ver->name_len : 0); 5569 unlock_user(host_ver->date, target_ver->date, 5570 copy ? host_ver->date_len : 0); 5571 unlock_user(host_ver->desc, target_ver->desc, 5572 copy ? host_ver->desc_len : 0); 5573 } 5574 5575 static inline abi_long target_to_host_drmversion(struct drm_version *host_ver, 5576 struct target_drm_version *target_ver) 5577 { 5578 memset(host_ver, 0, sizeof(*host_ver)); 5579 5580 __get_user(host_ver->name_len, &target_ver->name_len); 5581 if (host_ver->name_len) { 5582 host_ver->name = lock_user(VERIFY_WRITE, target_ver->name, 5583 target_ver->name_len, 0); 5584 if (!host_ver->name) { 5585 return -EFAULT; 5586 } 5587 } 5588 5589 __get_user(host_ver->date_len, &target_ver->date_len); 5590 if (host_ver->date_len) { 5591 host_ver->date = lock_user(VERIFY_WRITE, target_ver->date, 5592 target_ver->date_len, 0); 5593 if (!host_ver->date) { 5594 goto err; 5595 } 5596 } 5597 5598 __get_user(host_ver->desc_len, &target_ver->desc_len); 5599 if (host_ver->desc_len) { 5600 host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc, 5601 target_ver->desc_len, 0); 5602 if (!host_ver->desc) { 5603 goto err; 5604 } 5605 } 5606 5607 return 0; 5608 err: 5609 unlock_drm_version(host_ver, target_ver, false); 5610 return -EFAULT; 5611 } 5612 5613 static inline void host_to_target_drmversion( 5614 struct target_drm_version *target_ver, 5615 struct drm_version *host_ver) 5616 { 5617 __put_user(host_ver->version_major, &target_ver->version_major); 5618 __put_user(host_ver->version_minor, &target_ver->version_minor); 5619 __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel); 5620 __put_user(host_ver->name_len, &target_ver->name_len); 5621 __put_user(host_ver->date_len, &target_ver->date_len); 5622 __put_user(host_ver->desc_len, &target_ver->desc_len); 5623 unlock_drm_version(host_ver, target_ver, true); 5624 } 5625 5626 static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp, 5627 int fd, int cmd, abi_long arg) 5628 { 5629 struct drm_version *ver; 5630 struct target_drm_version *target_ver; 5631 abi_long ret; 5632 5633 switch (ie->host_cmd) { 5634 case DRM_IOCTL_VERSION: 5635 if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) { 5636 return -TARGET_EFAULT; 5637 } 5638 ver = (struct drm_version *)buf_temp; 5639 ret = target_to_host_drmversion(ver, target_ver); 5640 if (!is_error(ret)) { 5641 ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver)); 5642 if (is_error(ret)) { 5643 unlock_drm_version(ver, target_ver, false); 5644 } else { 5645 host_to_target_drmversion(target_ver, ver); 5646 } 5647 } 5648 unlock_user_struct(target_ver, arg, 0); 5649 return ret; 5650 } 5651 return -TARGET_ENOSYS; 5652 } 5653 5654 static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie, 5655 struct drm_i915_getparam *gparam, 5656 int fd, abi_long arg) 5657 { 5658 abi_long ret; 5659 int value; 5660 struct target_drm_i915_getparam *target_gparam; 5661 5662 if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) { 5663 return -TARGET_EFAULT; 5664 } 5665 5666 __get_user(gparam->param, &target_gparam->param); 5667 gparam->value = &value; 5668 ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam)); 5669 put_user_s32(value, target_gparam->value); 5670 5671 unlock_user_struct(target_gparam, arg, 0); 5672 return ret; 5673 } 5674 5675 static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp, 5676 int fd, int cmd, abi_long arg) 5677 { 5678 switch (ie->host_cmd) { 5679 case DRM_IOCTL_I915_GETPARAM: 5680 return do_ioctl_drm_i915_getparam(ie, 5681 (struct drm_i915_getparam *)buf_temp, 5682 fd, arg); 5683 default: 5684 return -TARGET_ENOSYS; 5685 } 5686 } 5687 5688 #endif 5689 5690 static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp, 5691 int fd, int cmd, abi_long arg) 5692 { 5693 struct tun_filter *filter = (struct tun_filter *)buf_temp; 5694 struct tun_filter *target_filter; 5695 char *target_addr; 5696 5697 assert(ie->access == IOC_W); 5698 5699 target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1); 5700 if (!target_filter) { 5701 return -TARGET_EFAULT; 5702 } 5703 filter->flags = tswap16(target_filter->flags); 5704 filter->count = tswap16(target_filter->count); 5705 unlock_user(target_filter, arg, 0); 5706 5707 if (filter->count) { 5708 if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN > 5709 MAX_STRUCT_SIZE) { 5710 return -TARGET_EFAULT; 5711 } 5712 5713 target_addr = lock_user(VERIFY_READ, 5714 arg + offsetof(struct tun_filter, addr), 5715 filter->count * ETH_ALEN, 1); 5716 if (!target_addr) { 5717 return -TARGET_EFAULT; 5718 } 5719 memcpy(filter->addr, target_addr, filter->count * ETH_ALEN); 5720 unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0); 5721 } 5722 5723 return get_errno(safe_ioctl(fd, ie->host_cmd, filter)); 5724 } 5725 5726 IOCTLEntry ioctl_entries[] = { 5727 #define IOCTL(cmd, access, ...) \ 5728 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } }, 5729 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \ 5730 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } }, 5731 #define IOCTL_IGNORE(cmd) \ 5732 { TARGET_ ## cmd, 0, #cmd }, 5733 #include "ioctls.h" 5734 { 0, 0, }, 5735 }; 5736 5737 /* ??? Implement proper locking for ioctls. */ 5738 /* do_ioctl() Must return target values and target errnos. */ 5739 static abi_long do_ioctl(int fd, int cmd, abi_long arg) 5740 { 5741 const IOCTLEntry *ie; 5742 const argtype *arg_type; 5743 abi_long ret; 5744 uint8_t buf_temp[MAX_STRUCT_SIZE]; 5745 int target_size; 5746 void *argptr; 5747 5748 ie = ioctl_entries; 5749 for(;;) { 5750 if (ie->target_cmd == 0) { 5751 qemu_log_mask( 5752 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd); 5753 return -TARGET_ENOSYS; 5754 } 5755 if (ie->target_cmd == cmd) 5756 break; 5757 ie++; 5758 } 5759 arg_type = ie->arg_type; 5760 if (ie->do_ioctl) { 5761 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg); 5762 } else if (!ie->host_cmd) { 5763 /* Some architectures define BSD ioctls in their headers 5764 that are not implemented in Linux. */ 5765 return -TARGET_ENOSYS; 5766 } 5767 5768 switch(arg_type[0]) { 5769 case TYPE_NULL: 5770 /* no argument */ 5771 ret = get_errno(safe_ioctl(fd, ie->host_cmd)); 5772 break; 5773 case TYPE_PTRVOID: 5774 case TYPE_INT: 5775 case TYPE_LONG: 5776 case TYPE_ULONG: 5777 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg)); 5778 break; 5779 case TYPE_PTR: 5780 arg_type++; 5781 target_size = thunk_type_size(arg_type, 0); 5782 switch(ie->access) { 5783 case IOC_R: 5784 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); 5785 if (!is_error(ret)) { 5786 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); 5787 if (!argptr) 5788 return -TARGET_EFAULT; 5789 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); 5790 unlock_user(argptr, arg, target_size); 5791 } 5792 break; 5793 case IOC_W: 5794 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5795 if (!argptr) 5796 return -TARGET_EFAULT; 5797 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); 5798 unlock_user(argptr, arg, 0); 5799 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); 5800 break; 5801 default: 5802 case IOC_RW: 5803 argptr = lock_user(VERIFY_READ, arg, target_size, 1); 5804 if (!argptr) 5805 return -TARGET_EFAULT; 5806 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST); 5807 unlock_user(argptr, arg, 0); 5808 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); 5809 if (!is_error(ret)) { 5810 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); 5811 if (!argptr) 5812 return -TARGET_EFAULT; 5813 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET); 5814 unlock_user(argptr, arg, target_size); 5815 } 5816 break; 5817 } 5818 break; 5819 default: 5820 qemu_log_mask(LOG_UNIMP, 5821 "Unsupported ioctl type: cmd=0x%04lx type=%d\n", 5822 (long)cmd, arg_type[0]); 5823 ret = -TARGET_ENOSYS; 5824 break; 5825 } 5826 return ret; 5827 } 5828 5829 static const bitmask_transtbl iflag_tbl[] = { 5830 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK }, 5831 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT }, 5832 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR }, 5833 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK }, 5834 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK }, 5835 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP }, 5836 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR }, 5837 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR }, 5838 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL }, 5839 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC }, 5840 { TARGET_IXON, TARGET_IXON, IXON, IXON }, 5841 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY }, 5842 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF }, 5843 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL }, 5844 { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8}, 5845 { 0, 0, 0, 0 } 5846 }; 5847 5848 static const bitmask_transtbl oflag_tbl[] = { 5849 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST }, 5850 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC }, 5851 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR }, 5852 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL }, 5853 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR }, 5854 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET }, 5855 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL }, 5856 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL }, 5857 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 }, 5858 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 }, 5859 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 }, 5860 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 }, 5861 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 }, 5862 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 }, 5863 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 }, 5864 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 }, 5865 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 }, 5866 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 }, 5867 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 }, 5868 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 }, 5869 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 }, 5870 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 }, 5871 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 }, 5872 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 }, 5873 { 0, 0, 0, 0 } 5874 }; 5875 5876 static const bitmask_transtbl cflag_tbl[] = { 5877 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 }, 5878 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 }, 5879 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 }, 5880 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 }, 5881 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 }, 5882 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 }, 5883 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 }, 5884 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 }, 5885 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 }, 5886 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 }, 5887 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 }, 5888 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 }, 5889 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 }, 5890 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 }, 5891 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 }, 5892 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 }, 5893 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 }, 5894 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 }, 5895 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 }, 5896 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 }, 5897 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 }, 5898 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 }, 5899 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 }, 5900 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 }, 5901 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB }, 5902 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD }, 5903 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB }, 5904 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD }, 5905 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL }, 5906 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL }, 5907 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS }, 5908 { 0, 0, 0, 0 } 5909 }; 5910 5911 static const bitmask_transtbl lflag_tbl[] = { 5912 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG }, 5913 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON }, 5914 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE }, 5915 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO }, 5916 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE }, 5917 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK }, 5918 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL }, 5919 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH }, 5920 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP }, 5921 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL }, 5922 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT }, 5923 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE }, 5924 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO }, 5925 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN }, 5926 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN }, 5927 { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC}, 5928 { 0, 0, 0, 0 } 5929 }; 5930 5931 static void target_to_host_termios (void *dst, const void *src) 5932 { 5933 struct host_termios *host = dst; 5934 const struct target_termios *target = src; 5935 5936 host->c_iflag = 5937 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl); 5938 host->c_oflag = 5939 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl); 5940 host->c_cflag = 5941 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl); 5942 host->c_lflag = 5943 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl); 5944 host->c_line = target->c_line; 5945 5946 memset(host->c_cc, 0, sizeof(host->c_cc)); 5947 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; 5948 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; 5949 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE]; 5950 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; 5951 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF]; 5952 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; 5953 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN]; 5954 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; 5955 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART]; 5956 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; 5957 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; 5958 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL]; 5959 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT]; 5960 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD]; 5961 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE]; 5962 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT]; 5963 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; 5964 } 5965 5966 static void host_to_target_termios (void *dst, const void *src) 5967 { 5968 struct target_termios *target = dst; 5969 const struct host_termios *host = src; 5970 5971 target->c_iflag = 5972 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl)); 5973 target->c_oflag = 5974 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl)); 5975 target->c_cflag = 5976 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl)); 5977 target->c_lflag = 5978 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl)); 5979 target->c_line = host->c_line; 5980 5981 memset(target->c_cc, 0, sizeof(target->c_cc)); 5982 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR]; 5983 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT]; 5984 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE]; 5985 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL]; 5986 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF]; 5987 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME]; 5988 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN]; 5989 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC]; 5990 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART]; 5991 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP]; 5992 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP]; 5993 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL]; 5994 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT]; 5995 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD]; 5996 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE]; 5997 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT]; 5998 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2]; 5999 } 6000 6001 static const StructEntry struct_termios_def = { 6002 .convert = { host_to_target_termios, target_to_host_termios }, 6003 .size = { sizeof(struct target_termios), sizeof(struct host_termios) }, 6004 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) }, 6005 .print = print_termios, 6006 }; 6007 6008 static const bitmask_transtbl mmap_flags_tbl[] = { 6009 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED }, 6010 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE }, 6011 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED }, 6012 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, 6013 MAP_ANONYMOUS, MAP_ANONYMOUS }, 6014 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, 6015 MAP_GROWSDOWN, MAP_GROWSDOWN }, 6016 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, 6017 MAP_DENYWRITE, MAP_DENYWRITE }, 6018 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, 6019 MAP_EXECUTABLE, MAP_EXECUTABLE }, 6020 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED }, 6021 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, 6022 MAP_NORESERVE, MAP_NORESERVE }, 6023 { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB }, 6024 /* MAP_STACK had been ignored by the kernel for quite some time. 6025 Recognize it for the target insofar as we do not want to pass 6026 it through to the host. */ 6027 { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 }, 6028 { 0, 0, 0, 0 } 6029 }; 6030 6031 /* 6032 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64) 6033 * TARGET_I386 is defined if TARGET_X86_64 is defined 6034 */ 6035 #if defined(TARGET_I386) 6036 6037 /* NOTE: there is really one LDT for all the threads */ 6038 static uint8_t *ldt_table; 6039 6040 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount) 6041 { 6042 int size; 6043 void *p; 6044 6045 if (!ldt_table) 6046 return 0; 6047 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE; 6048 if (size > bytecount) 6049 size = bytecount; 6050 p = lock_user(VERIFY_WRITE, ptr, size, 0); 6051 if (!p) 6052 return -TARGET_EFAULT; 6053 /* ??? Should this by byteswapped? */ 6054 memcpy(p, ldt_table, size); 6055 unlock_user(p, ptr, size); 6056 return size; 6057 } 6058 6059 /* XXX: add locking support */ 6060 static abi_long write_ldt(CPUX86State *env, 6061 abi_ulong ptr, unsigned long bytecount, int oldmode) 6062 { 6063 struct target_modify_ldt_ldt_s ldt_info; 6064 struct target_modify_ldt_ldt_s *target_ldt_info; 6065 int seg_32bit, contents, read_exec_only, limit_in_pages; 6066 int seg_not_present, useable, lm; 6067 uint32_t *lp, entry_1, entry_2; 6068 6069 if (bytecount != sizeof(ldt_info)) 6070 return -TARGET_EINVAL; 6071 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1)) 6072 return -TARGET_EFAULT; 6073 ldt_info.entry_number = tswap32(target_ldt_info->entry_number); 6074 ldt_info.base_addr = tswapal(target_ldt_info->base_addr); 6075 ldt_info.limit = tswap32(target_ldt_info->limit); 6076 ldt_info.flags = tswap32(target_ldt_info->flags); 6077 unlock_user_struct(target_ldt_info, ptr, 0); 6078 6079 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES) 6080 return -TARGET_EINVAL; 6081 seg_32bit = ldt_info.flags & 1; 6082 contents = (ldt_info.flags >> 1) & 3; 6083 read_exec_only = (ldt_info.flags >> 3) & 1; 6084 limit_in_pages = (ldt_info.flags >> 4) & 1; 6085 seg_not_present = (ldt_info.flags >> 5) & 1; 6086 useable = (ldt_info.flags >> 6) & 1; 6087 #ifdef TARGET_ABI32 6088 lm = 0; 6089 #else 6090 lm = (ldt_info.flags >> 7) & 1; 6091 #endif 6092 if (contents == 3) { 6093 if (oldmode) 6094 return -TARGET_EINVAL; 6095 if (seg_not_present == 0) 6096 return -TARGET_EINVAL; 6097 } 6098 /* allocate the LDT */ 6099 if (!ldt_table) { 6100 env->ldt.base = target_mmap(0, 6101 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE, 6102 PROT_READ|PROT_WRITE, 6103 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 6104 if (env->ldt.base == -1) 6105 return -TARGET_ENOMEM; 6106 memset(g2h_untagged(env->ldt.base), 0, 6107 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE); 6108 env->ldt.limit = 0xffff; 6109 ldt_table = g2h_untagged(env->ldt.base); 6110 } 6111 6112 /* NOTE: same code as Linux kernel */ 6113 /* Allow LDTs to be cleared by the user. */ 6114 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { 6115 if (oldmode || 6116 (contents == 0 && 6117 read_exec_only == 1 && 6118 seg_32bit == 0 && 6119 limit_in_pages == 0 && 6120 seg_not_present == 1 && 6121 useable == 0 )) { 6122 entry_1 = 0; 6123 entry_2 = 0; 6124 goto install; 6125 } 6126 } 6127 6128 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | 6129 (ldt_info.limit & 0x0ffff); 6130 entry_2 = (ldt_info.base_addr & 0xff000000) | 6131 ((ldt_info.base_addr & 0x00ff0000) >> 16) | 6132 (ldt_info.limit & 0xf0000) | 6133 ((read_exec_only ^ 1) << 9) | 6134 (contents << 10) | 6135 ((seg_not_present ^ 1) << 15) | 6136 (seg_32bit << 22) | 6137 (limit_in_pages << 23) | 6138 (lm << 21) | 6139 0x7000; 6140 if (!oldmode) 6141 entry_2 |= (useable << 20); 6142 6143 /* Install the new entry ... */ 6144 install: 6145 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3)); 6146 lp[0] = tswap32(entry_1); 6147 lp[1] = tswap32(entry_2); 6148 return 0; 6149 } 6150 6151 /* specific and weird i386 syscalls */ 6152 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, 6153 unsigned long bytecount) 6154 { 6155 abi_long ret; 6156 6157 switch (func) { 6158 case 0: 6159 ret = read_ldt(ptr, bytecount); 6160 break; 6161 case 1: 6162 ret = write_ldt(env, ptr, bytecount, 1); 6163 break; 6164 case 0x11: 6165 ret = write_ldt(env, ptr, bytecount, 0); 6166 break; 6167 default: 6168 ret = -TARGET_ENOSYS; 6169 break; 6170 } 6171 return ret; 6172 } 6173 6174 #if defined(TARGET_ABI32) 6175 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr) 6176 { 6177 uint64_t *gdt_table = g2h_untagged(env->gdt.base); 6178 struct target_modify_ldt_ldt_s ldt_info; 6179 struct target_modify_ldt_ldt_s *target_ldt_info; 6180 int seg_32bit, contents, read_exec_only, limit_in_pages; 6181 int seg_not_present, useable, lm; 6182 uint32_t *lp, entry_1, entry_2; 6183 int i; 6184 6185 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); 6186 if (!target_ldt_info) 6187 return -TARGET_EFAULT; 6188 ldt_info.entry_number = tswap32(target_ldt_info->entry_number); 6189 ldt_info.base_addr = tswapal(target_ldt_info->base_addr); 6190 ldt_info.limit = tswap32(target_ldt_info->limit); 6191 ldt_info.flags = tswap32(target_ldt_info->flags); 6192 if (ldt_info.entry_number == -1) { 6193 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) { 6194 if (gdt_table[i] == 0) { 6195 ldt_info.entry_number = i; 6196 target_ldt_info->entry_number = tswap32(i); 6197 break; 6198 } 6199 } 6200 } 6201 unlock_user_struct(target_ldt_info, ptr, 1); 6202 6203 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN || 6204 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX) 6205 return -TARGET_EINVAL; 6206 seg_32bit = ldt_info.flags & 1; 6207 contents = (ldt_info.flags >> 1) & 3; 6208 read_exec_only = (ldt_info.flags >> 3) & 1; 6209 limit_in_pages = (ldt_info.flags >> 4) & 1; 6210 seg_not_present = (ldt_info.flags >> 5) & 1; 6211 useable = (ldt_info.flags >> 6) & 1; 6212 #ifdef TARGET_ABI32 6213 lm = 0; 6214 #else 6215 lm = (ldt_info.flags >> 7) & 1; 6216 #endif 6217 6218 if (contents == 3) { 6219 if (seg_not_present == 0) 6220 return -TARGET_EINVAL; 6221 } 6222 6223 /* NOTE: same code as Linux kernel */ 6224 /* Allow LDTs to be cleared by the user. */ 6225 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { 6226 if ((contents == 0 && 6227 read_exec_only == 1 && 6228 seg_32bit == 0 && 6229 limit_in_pages == 0 && 6230 seg_not_present == 1 && 6231 useable == 0 )) { 6232 entry_1 = 0; 6233 entry_2 = 0; 6234 goto install; 6235 } 6236 } 6237 6238 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) | 6239 (ldt_info.limit & 0x0ffff); 6240 entry_2 = (ldt_info.base_addr & 0xff000000) | 6241 ((ldt_info.base_addr & 0x00ff0000) >> 16) | 6242 (ldt_info.limit & 0xf0000) | 6243 ((read_exec_only ^ 1) << 9) | 6244 (contents << 10) | 6245 ((seg_not_present ^ 1) << 15) | 6246 (seg_32bit << 22) | 6247 (limit_in_pages << 23) | 6248 (useable << 20) | 6249 (lm << 21) | 6250 0x7000; 6251 6252 /* Install the new entry ... */ 6253 install: 6254 lp = (uint32_t *)(gdt_table + ldt_info.entry_number); 6255 lp[0] = tswap32(entry_1); 6256 lp[1] = tswap32(entry_2); 6257 return 0; 6258 } 6259 6260 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr) 6261 { 6262 struct target_modify_ldt_ldt_s *target_ldt_info; 6263 uint64_t *gdt_table = g2h_untagged(env->gdt.base); 6264 uint32_t base_addr, limit, flags; 6265 int seg_32bit, contents, read_exec_only, limit_in_pages, idx; 6266 int seg_not_present, useable, lm; 6267 uint32_t *lp, entry_1, entry_2; 6268 6269 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1); 6270 if (!target_ldt_info) 6271 return -TARGET_EFAULT; 6272 idx = tswap32(target_ldt_info->entry_number); 6273 if (idx < TARGET_GDT_ENTRY_TLS_MIN || 6274 idx > TARGET_GDT_ENTRY_TLS_MAX) { 6275 unlock_user_struct(target_ldt_info, ptr, 1); 6276 return -TARGET_EINVAL; 6277 } 6278 lp = (uint32_t *)(gdt_table + idx); 6279 entry_1 = tswap32(lp[0]); 6280 entry_2 = tswap32(lp[1]); 6281 6282 read_exec_only = ((entry_2 >> 9) & 1) ^ 1; 6283 contents = (entry_2 >> 10) & 3; 6284 seg_not_present = ((entry_2 >> 15) & 1) ^ 1; 6285 seg_32bit = (entry_2 >> 22) & 1; 6286 limit_in_pages = (entry_2 >> 23) & 1; 6287 useable = (entry_2 >> 20) & 1; 6288 #ifdef TARGET_ABI32 6289 lm = 0; 6290 #else 6291 lm = (entry_2 >> 21) & 1; 6292 #endif 6293 flags = (seg_32bit << 0) | (contents << 1) | 6294 (read_exec_only << 3) | (limit_in_pages << 4) | 6295 (seg_not_present << 5) | (useable << 6) | (lm << 7); 6296 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000); 6297 base_addr = (entry_1 >> 16) | 6298 (entry_2 & 0xff000000) | 6299 ((entry_2 & 0xff) << 16); 6300 target_ldt_info->base_addr = tswapal(base_addr); 6301 target_ldt_info->limit = tswap32(limit); 6302 target_ldt_info->flags = tswap32(flags); 6303 unlock_user_struct(target_ldt_info, ptr, 1); 6304 return 0; 6305 } 6306 6307 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) 6308 { 6309 return -TARGET_ENOSYS; 6310 } 6311 #else 6312 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr) 6313 { 6314 abi_long ret = 0; 6315 abi_ulong val; 6316 int idx; 6317 6318 switch(code) { 6319 case TARGET_ARCH_SET_GS: 6320 case TARGET_ARCH_SET_FS: 6321 if (code == TARGET_ARCH_SET_GS) 6322 idx = R_GS; 6323 else 6324 idx = R_FS; 6325 cpu_x86_load_seg(env, idx, 0); 6326 env->segs[idx].base = addr; 6327 break; 6328 case TARGET_ARCH_GET_GS: 6329 case TARGET_ARCH_GET_FS: 6330 if (code == TARGET_ARCH_GET_GS) 6331 idx = R_GS; 6332 else 6333 idx = R_FS; 6334 val = env->segs[idx].base; 6335 if (put_user(val, addr, abi_ulong)) 6336 ret = -TARGET_EFAULT; 6337 break; 6338 default: 6339 ret = -TARGET_EINVAL; 6340 break; 6341 } 6342 return ret; 6343 } 6344 #endif /* defined(TARGET_ABI32 */ 6345 #endif /* defined(TARGET_I386) */ 6346 6347 /* 6348 * These constants are generic. Supply any that are missing from the host. 6349 */ 6350 #ifndef PR_SET_NAME 6351 # define PR_SET_NAME 15 6352 # define PR_GET_NAME 16 6353 #endif 6354 #ifndef PR_SET_FP_MODE 6355 # define PR_SET_FP_MODE 45 6356 # define PR_GET_FP_MODE 46 6357 # define PR_FP_MODE_FR (1 << 0) 6358 # define PR_FP_MODE_FRE (1 << 1) 6359 #endif 6360 #ifndef PR_SVE_SET_VL 6361 # define PR_SVE_SET_VL 50 6362 # define PR_SVE_GET_VL 51 6363 # define PR_SVE_VL_LEN_MASK 0xffff 6364 # define PR_SVE_VL_INHERIT (1 << 17) 6365 #endif 6366 #ifndef PR_PAC_RESET_KEYS 6367 # define PR_PAC_RESET_KEYS 54 6368 # define PR_PAC_APIAKEY (1 << 0) 6369 # define PR_PAC_APIBKEY (1 << 1) 6370 # define PR_PAC_APDAKEY (1 << 2) 6371 # define PR_PAC_APDBKEY (1 << 3) 6372 # define PR_PAC_APGAKEY (1 << 4) 6373 #endif 6374 #ifndef PR_SET_TAGGED_ADDR_CTRL 6375 # define PR_SET_TAGGED_ADDR_CTRL 55 6376 # define PR_GET_TAGGED_ADDR_CTRL 56 6377 # define PR_TAGGED_ADDR_ENABLE (1UL << 0) 6378 #endif 6379 #ifndef PR_MTE_TCF_SHIFT 6380 # define PR_MTE_TCF_SHIFT 1 6381 # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) 6382 # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) 6383 # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) 6384 # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT) 6385 # define PR_MTE_TAG_SHIFT 3 6386 # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) 6387 #endif 6388 #ifndef PR_SET_IO_FLUSHER 6389 # define PR_SET_IO_FLUSHER 57 6390 # define PR_GET_IO_FLUSHER 58 6391 #endif 6392 #ifndef PR_SET_SYSCALL_USER_DISPATCH 6393 # define PR_SET_SYSCALL_USER_DISPATCH 59 6394 #endif 6395 6396 #include "target_prctl.h" 6397 6398 static abi_long do_prctl_inval0(CPUArchState *env) 6399 { 6400 return -TARGET_EINVAL; 6401 } 6402 6403 static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2) 6404 { 6405 return -TARGET_EINVAL; 6406 } 6407 6408 #ifndef do_prctl_get_fp_mode 6409 #define do_prctl_get_fp_mode do_prctl_inval0 6410 #endif 6411 #ifndef do_prctl_set_fp_mode 6412 #define do_prctl_set_fp_mode do_prctl_inval1 6413 #endif 6414 #ifndef do_prctl_get_vl 6415 #define do_prctl_get_vl do_prctl_inval0 6416 #endif 6417 #ifndef do_prctl_set_vl 6418 #define do_prctl_set_vl do_prctl_inval1 6419 #endif 6420 #ifndef do_prctl_reset_keys 6421 #define do_prctl_reset_keys do_prctl_inval1 6422 #endif 6423 #ifndef do_prctl_set_tagged_addr_ctrl 6424 #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1 6425 #endif 6426 #ifndef do_prctl_get_tagged_addr_ctrl 6427 #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0 6428 #endif 6429 #ifndef do_prctl_get_unalign 6430 #define do_prctl_get_unalign do_prctl_inval1 6431 #endif 6432 #ifndef do_prctl_set_unalign 6433 #define do_prctl_set_unalign do_prctl_inval1 6434 #endif 6435 6436 static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, 6437 abi_long arg3, abi_long arg4, abi_long arg5) 6438 { 6439 abi_long ret; 6440 6441 switch (option) { 6442 case PR_GET_PDEATHSIG: 6443 { 6444 int deathsig; 6445 ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig, 6446 arg3, arg4, arg5)); 6447 if (!is_error(ret) && arg2 && put_user_s32(deathsig, arg2)) { 6448 return -TARGET_EFAULT; 6449 } 6450 return ret; 6451 } 6452 case PR_GET_NAME: 6453 { 6454 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1); 6455 if (!name) { 6456 return -TARGET_EFAULT; 6457 } 6458 ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name, 6459 arg3, arg4, arg5)); 6460 unlock_user(name, arg2, 16); 6461 return ret; 6462 } 6463 case PR_SET_NAME: 6464 { 6465 void *name = lock_user(VERIFY_READ, arg2, 16, 1); 6466 if (!name) { 6467 return -TARGET_EFAULT; 6468 } 6469 ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name, 6470 arg3, arg4, arg5)); 6471 unlock_user(name, arg2, 0); 6472 return ret; 6473 } 6474 case PR_GET_FP_MODE: 6475 return do_prctl_get_fp_mode(env); 6476 case PR_SET_FP_MODE: 6477 return do_prctl_set_fp_mode(env, arg2); 6478 case PR_SVE_GET_VL: 6479 return do_prctl_get_vl(env); 6480 case PR_SVE_SET_VL: 6481 return do_prctl_set_vl(env, arg2); 6482 case PR_PAC_RESET_KEYS: 6483 if (arg3 || arg4 || arg5) { 6484 return -TARGET_EINVAL; 6485 } 6486 return do_prctl_reset_keys(env, arg2); 6487 case PR_SET_TAGGED_ADDR_CTRL: 6488 if (arg3 || arg4 || arg5) { 6489 return -TARGET_EINVAL; 6490 } 6491 return do_prctl_set_tagged_addr_ctrl(env, arg2); 6492 case PR_GET_TAGGED_ADDR_CTRL: 6493 if (arg2 || arg3 || arg4 || arg5) { 6494 return -TARGET_EINVAL; 6495 } 6496 return do_prctl_get_tagged_addr_ctrl(env); 6497 6498 case PR_GET_UNALIGN: 6499 return do_prctl_get_unalign(env, arg2); 6500 case PR_SET_UNALIGN: 6501 return do_prctl_set_unalign(env, arg2); 6502 6503 case PR_GET_DUMPABLE: 6504 case PR_SET_DUMPABLE: 6505 case PR_GET_KEEPCAPS: 6506 case PR_SET_KEEPCAPS: 6507 case PR_GET_TIMING: 6508 case PR_SET_TIMING: 6509 case PR_GET_TIMERSLACK: 6510 case PR_SET_TIMERSLACK: 6511 case PR_MCE_KILL: 6512 case PR_MCE_KILL_GET: 6513 case PR_GET_NO_NEW_PRIVS: 6514 case PR_SET_NO_NEW_PRIVS: 6515 case PR_GET_IO_FLUSHER: 6516 case PR_SET_IO_FLUSHER: 6517 /* Some prctl options have no pointer arguments and we can pass on. */ 6518 return get_errno(prctl(option, arg2, arg3, arg4, arg5)); 6519 6520 case PR_GET_CHILD_SUBREAPER: 6521 case PR_SET_CHILD_SUBREAPER: 6522 case PR_GET_SPECULATION_CTRL: 6523 case PR_SET_SPECULATION_CTRL: 6524 case PR_GET_TID_ADDRESS: 6525 /* TODO */ 6526 return -TARGET_EINVAL; 6527 6528 case PR_GET_FPEXC: 6529 case PR_SET_FPEXC: 6530 /* Was used for SPE on PowerPC. */ 6531 return -TARGET_EINVAL; 6532 6533 case PR_GET_ENDIAN: 6534 case PR_SET_ENDIAN: 6535 case PR_GET_FPEMU: 6536 case PR_SET_FPEMU: 6537 case PR_SET_MM: 6538 case PR_GET_SECCOMP: 6539 case PR_SET_SECCOMP: 6540 case PR_SET_SYSCALL_USER_DISPATCH: 6541 case PR_GET_THP_DISABLE: 6542 case PR_SET_THP_DISABLE: 6543 case PR_GET_TSC: 6544 case PR_SET_TSC: 6545 /* Disable to prevent the target disabling stuff we need. */ 6546 return -TARGET_EINVAL; 6547 6548 default: 6549 qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n", 6550 option); 6551 return -TARGET_EINVAL; 6552 } 6553 } 6554 6555 #define NEW_STACK_SIZE 0x40000 6556 6557 6558 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER; 6559 typedef struct { 6560 CPUArchState *env; 6561 pthread_mutex_t mutex; 6562 pthread_cond_t cond; 6563 pthread_t thread; 6564 uint32_t tid; 6565 abi_ulong child_tidptr; 6566 abi_ulong parent_tidptr; 6567 sigset_t sigmask; 6568 } new_thread_info; 6569 6570 static void *clone_func(void *arg) 6571 { 6572 new_thread_info *info = arg; 6573 CPUArchState *env; 6574 CPUState *cpu; 6575 TaskState *ts; 6576 6577 rcu_register_thread(); 6578 tcg_register_thread(); 6579 env = info->env; 6580 cpu = env_cpu(env); 6581 thread_cpu = cpu; 6582 ts = (TaskState *)cpu->opaque; 6583 info->tid = sys_gettid(); 6584 task_settid(ts); 6585 if (info->child_tidptr) 6586 put_user_u32(info->tid, info->child_tidptr); 6587 if (info->parent_tidptr) 6588 put_user_u32(info->tid, info->parent_tidptr); 6589 qemu_guest_random_seed_thread_part2(cpu->random_seed); 6590 /* Enable signals. */ 6591 sigprocmask(SIG_SETMASK, &info->sigmask, NULL); 6592 /* Signal to the parent that we're ready. */ 6593 pthread_mutex_lock(&info->mutex); 6594 pthread_cond_broadcast(&info->cond); 6595 pthread_mutex_unlock(&info->mutex); 6596 /* Wait until the parent has finished initializing the tls state. */ 6597 pthread_mutex_lock(&clone_lock); 6598 pthread_mutex_unlock(&clone_lock); 6599 cpu_loop(env); 6600 /* never exits */ 6601 return NULL; 6602 } 6603 6604 /* do_fork() Must return host values and target errnos (unlike most 6605 do_*() functions). */ 6606 static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, 6607 abi_ulong parent_tidptr, target_ulong newtls, 6608 abi_ulong child_tidptr) 6609 { 6610 CPUState *cpu = env_cpu(env); 6611 int ret; 6612 TaskState *ts; 6613 CPUState *new_cpu; 6614 CPUArchState *new_env; 6615 sigset_t sigmask; 6616 6617 flags &= ~CLONE_IGNORED_FLAGS; 6618 6619 /* Emulate vfork() with fork() */ 6620 if (flags & CLONE_VFORK) 6621 flags &= ~(CLONE_VFORK | CLONE_VM); 6622 6623 if (flags & CLONE_VM) { 6624 TaskState *parent_ts = (TaskState *)cpu->opaque; 6625 new_thread_info info; 6626 pthread_attr_t attr; 6627 6628 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) || 6629 (flags & CLONE_INVALID_THREAD_FLAGS)) { 6630 return -TARGET_EINVAL; 6631 } 6632 6633 ts = g_new0(TaskState, 1); 6634 init_task_state(ts); 6635 6636 /* Grab a mutex so that thread setup appears atomic. */ 6637 pthread_mutex_lock(&clone_lock); 6638 6639 /* 6640 * If this is our first additional thread, we need to ensure we 6641 * generate code for parallel execution and flush old translations. 6642 * Do this now so that the copy gets CF_PARALLEL too. 6643 */ 6644 if (!(cpu->tcg_cflags & CF_PARALLEL)) { 6645 cpu->tcg_cflags |= CF_PARALLEL; 6646 tb_flush(cpu); 6647 } 6648 6649 /* we create a new CPU instance. */ 6650 new_env = cpu_copy(env); 6651 /* Init regs that differ from the parent. */ 6652 cpu_clone_regs_child(new_env, newsp, flags); 6653 cpu_clone_regs_parent(env, flags); 6654 new_cpu = env_cpu(new_env); 6655 new_cpu->opaque = ts; 6656 ts->bprm = parent_ts->bprm; 6657 ts->info = parent_ts->info; 6658 ts->signal_mask = parent_ts->signal_mask; 6659 6660 if (flags & CLONE_CHILD_CLEARTID) { 6661 ts->child_tidptr = child_tidptr; 6662 } 6663 6664 if (flags & CLONE_SETTLS) { 6665 cpu_set_tls (new_env, newtls); 6666 } 6667 6668 memset(&info, 0, sizeof(info)); 6669 pthread_mutex_init(&info.mutex, NULL); 6670 pthread_mutex_lock(&info.mutex); 6671 pthread_cond_init(&info.cond, NULL); 6672 info.env = new_env; 6673 if (flags & CLONE_CHILD_SETTID) { 6674 info.child_tidptr = child_tidptr; 6675 } 6676 if (flags & CLONE_PARENT_SETTID) { 6677 info.parent_tidptr = parent_tidptr; 6678 } 6679 6680 ret = pthread_attr_init(&attr); 6681 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE); 6682 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 6683 /* It is not safe to deliver signals until the child has finished 6684 initializing, so temporarily block all signals. */ 6685 sigfillset(&sigmask); 6686 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask); 6687 cpu->random_seed = qemu_guest_random_seed_thread_part1(); 6688 6689 ret = pthread_create(&info.thread, &attr, clone_func, &info); 6690 /* TODO: Free new CPU state if thread creation failed. */ 6691 6692 sigprocmask(SIG_SETMASK, &info.sigmask, NULL); 6693 pthread_attr_destroy(&attr); 6694 if (ret == 0) { 6695 /* Wait for the child to initialize. */ 6696 pthread_cond_wait(&info.cond, &info.mutex); 6697 ret = info.tid; 6698 } else { 6699 ret = -1; 6700 } 6701 pthread_mutex_unlock(&info.mutex); 6702 pthread_cond_destroy(&info.cond); 6703 pthread_mutex_destroy(&info.mutex); 6704 pthread_mutex_unlock(&clone_lock); 6705 } else { 6706 /* if no CLONE_VM, we consider it is a fork */ 6707 if (flags & CLONE_INVALID_FORK_FLAGS) { 6708 return -TARGET_EINVAL; 6709 } 6710 6711 /* We can't support custom termination signals */ 6712 if ((flags & CSIGNAL) != TARGET_SIGCHLD) { 6713 return -TARGET_EINVAL; 6714 } 6715 6716 if (block_signals()) { 6717 return -QEMU_ERESTARTSYS; 6718 } 6719 6720 fork_start(); 6721 ret = fork(); 6722 if (ret == 0) { 6723 /* Child Process. */ 6724 cpu_clone_regs_child(env, newsp, flags); 6725 fork_end(1); 6726 /* There is a race condition here. The parent process could 6727 theoretically read the TID in the child process before the child 6728 tid is set. This would require using either ptrace 6729 (not implemented) or having *_tidptr to point at a shared memory 6730 mapping. We can't repeat the spinlock hack used above because 6731 the child process gets its own copy of the lock. */ 6732 if (flags & CLONE_CHILD_SETTID) 6733 put_user_u32(sys_gettid(), child_tidptr); 6734 if (flags & CLONE_PARENT_SETTID) 6735 put_user_u32(sys_gettid(), parent_tidptr); 6736 ts = (TaskState *)cpu->opaque; 6737 if (flags & CLONE_SETTLS) 6738 cpu_set_tls (env, newtls); 6739 if (flags & CLONE_CHILD_CLEARTID) 6740 ts->child_tidptr = child_tidptr; 6741 } else { 6742 cpu_clone_regs_parent(env, flags); 6743 fork_end(0); 6744 } 6745 } 6746 return ret; 6747 } 6748 6749 /* warning : doesn't handle linux specific flags... */ 6750 static int target_to_host_fcntl_cmd(int cmd) 6751 { 6752 int ret; 6753 6754 switch(cmd) { 6755 case TARGET_F_DUPFD: 6756 case TARGET_F_GETFD: 6757 case TARGET_F_SETFD: 6758 case TARGET_F_GETFL: 6759 case TARGET_F_SETFL: 6760 case TARGET_F_OFD_GETLK: 6761 case TARGET_F_OFD_SETLK: 6762 case TARGET_F_OFD_SETLKW: 6763 ret = cmd; 6764 break; 6765 case TARGET_F_GETLK: 6766 ret = F_GETLK64; 6767 break; 6768 case TARGET_F_SETLK: 6769 ret = F_SETLK64; 6770 break; 6771 case TARGET_F_SETLKW: 6772 ret = F_SETLKW64; 6773 break; 6774 case TARGET_F_GETOWN: 6775 ret = F_GETOWN; 6776 break; 6777 case TARGET_F_SETOWN: 6778 ret = F_SETOWN; 6779 break; 6780 case TARGET_F_GETSIG: 6781 ret = F_GETSIG; 6782 break; 6783 case TARGET_F_SETSIG: 6784 ret = F_SETSIG; 6785 break; 6786 #if TARGET_ABI_BITS == 32 6787 case TARGET_F_GETLK64: 6788 ret = F_GETLK64; 6789 break; 6790 case TARGET_F_SETLK64: 6791 ret = F_SETLK64; 6792 break; 6793 case TARGET_F_SETLKW64: 6794 ret = F_SETLKW64; 6795 break; 6796 #endif 6797 case TARGET_F_SETLEASE: 6798 ret = F_SETLEASE; 6799 break; 6800 case TARGET_F_GETLEASE: 6801 ret = F_GETLEASE; 6802 break; 6803 #ifdef F_DUPFD_CLOEXEC 6804 case TARGET_F_DUPFD_CLOEXEC: 6805 ret = F_DUPFD_CLOEXEC; 6806 break; 6807 #endif 6808 case TARGET_F_NOTIFY: 6809 ret = F_NOTIFY; 6810 break; 6811 #ifdef F_GETOWN_EX 6812 case TARGET_F_GETOWN_EX: 6813 ret = F_GETOWN_EX; 6814 break; 6815 #endif 6816 #ifdef F_SETOWN_EX 6817 case TARGET_F_SETOWN_EX: 6818 ret = F_SETOWN_EX; 6819 break; 6820 #endif 6821 #ifdef F_SETPIPE_SZ 6822 case TARGET_F_SETPIPE_SZ: 6823 ret = F_SETPIPE_SZ; 6824 break; 6825 case TARGET_F_GETPIPE_SZ: 6826 ret = F_GETPIPE_SZ; 6827 break; 6828 #endif 6829 #ifdef F_ADD_SEALS 6830 case TARGET_F_ADD_SEALS: 6831 ret = F_ADD_SEALS; 6832 break; 6833 case TARGET_F_GET_SEALS: 6834 ret = F_GET_SEALS; 6835 break; 6836 #endif 6837 default: 6838 ret = -TARGET_EINVAL; 6839 break; 6840 } 6841 6842 #if defined(__powerpc64__) 6843 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and 6844 * is not supported by kernel. The glibc fcntl call actually adjusts 6845 * them to 5, 6 and 7 before making the syscall(). Since we make the 6846 * syscall directly, adjust to what is supported by the kernel. 6847 */ 6848 if (ret >= F_GETLK64 && ret <= F_SETLKW64) { 6849 ret -= F_GETLK64 - 5; 6850 } 6851 #endif 6852 6853 return ret; 6854 } 6855 6856 #define FLOCK_TRANSTBL \ 6857 switch (type) { \ 6858 TRANSTBL_CONVERT(F_RDLCK); \ 6859 TRANSTBL_CONVERT(F_WRLCK); \ 6860 TRANSTBL_CONVERT(F_UNLCK); \ 6861 } 6862 6863 static int target_to_host_flock(int type) 6864 { 6865 #define TRANSTBL_CONVERT(a) case TARGET_##a: return a 6866 FLOCK_TRANSTBL 6867 #undef TRANSTBL_CONVERT 6868 return -TARGET_EINVAL; 6869 } 6870 6871 static int host_to_target_flock(int type) 6872 { 6873 #define TRANSTBL_CONVERT(a) case a: return TARGET_##a 6874 FLOCK_TRANSTBL 6875 #undef TRANSTBL_CONVERT 6876 /* if we don't know how to convert the value coming 6877 * from the host we copy to the target field as-is 6878 */ 6879 return type; 6880 } 6881 6882 static inline abi_long copy_from_user_flock(struct flock64 *fl, 6883 abi_ulong target_flock_addr) 6884 { 6885 struct target_flock *target_fl; 6886 int l_type; 6887 6888 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { 6889 return -TARGET_EFAULT; 6890 } 6891 6892 __get_user(l_type, &target_fl->l_type); 6893 l_type = target_to_host_flock(l_type); 6894 if (l_type < 0) { 6895 return l_type; 6896 } 6897 fl->l_type = l_type; 6898 __get_user(fl->l_whence, &target_fl->l_whence); 6899 __get_user(fl->l_start, &target_fl->l_start); 6900 __get_user(fl->l_len, &target_fl->l_len); 6901 __get_user(fl->l_pid, &target_fl->l_pid); 6902 unlock_user_struct(target_fl, target_flock_addr, 0); 6903 return 0; 6904 } 6905 6906 static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr, 6907 const struct flock64 *fl) 6908 { 6909 struct target_flock *target_fl; 6910 short l_type; 6911 6912 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { 6913 return -TARGET_EFAULT; 6914 } 6915 6916 l_type = host_to_target_flock(fl->l_type); 6917 __put_user(l_type, &target_fl->l_type); 6918 __put_user(fl->l_whence, &target_fl->l_whence); 6919 __put_user(fl->l_start, &target_fl->l_start); 6920 __put_user(fl->l_len, &target_fl->l_len); 6921 __put_user(fl->l_pid, &target_fl->l_pid); 6922 unlock_user_struct(target_fl, target_flock_addr, 1); 6923 return 0; 6924 } 6925 6926 typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr); 6927 typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl); 6928 6929 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32 6930 struct target_oabi_flock64 { 6931 abi_short l_type; 6932 abi_short l_whence; 6933 abi_llong l_start; 6934 abi_llong l_len; 6935 abi_int l_pid; 6936 } QEMU_PACKED; 6937 6938 static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl, 6939 abi_ulong target_flock_addr) 6940 { 6941 struct target_oabi_flock64 *target_fl; 6942 int l_type; 6943 6944 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { 6945 return -TARGET_EFAULT; 6946 } 6947 6948 __get_user(l_type, &target_fl->l_type); 6949 l_type = target_to_host_flock(l_type); 6950 if (l_type < 0) { 6951 return l_type; 6952 } 6953 fl->l_type = l_type; 6954 __get_user(fl->l_whence, &target_fl->l_whence); 6955 __get_user(fl->l_start, &target_fl->l_start); 6956 __get_user(fl->l_len, &target_fl->l_len); 6957 __get_user(fl->l_pid, &target_fl->l_pid); 6958 unlock_user_struct(target_fl, target_flock_addr, 0); 6959 return 0; 6960 } 6961 6962 static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr, 6963 const struct flock64 *fl) 6964 { 6965 struct target_oabi_flock64 *target_fl; 6966 short l_type; 6967 6968 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { 6969 return -TARGET_EFAULT; 6970 } 6971 6972 l_type = host_to_target_flock(fl->l_type); 6973 __put_user(l_type, &target_fl->l_type); 6974 __put_user(fl->l_whence, &target_fl->l_whence); 6975 __put_user(fl->l_start, &target_fl->l_start); 6976 __put_user(fl->l_len, &target_fl->l_len); 6977 __put_user(fl->l_pid, &target_fl->l_pid); 6978 unlock_user_struct(target_fl, target_flock_addr, 1); 6979 return 0; 6980 } 6981 #endif 6982 6983 static inline abi_long copy_from_user_flock64(struct flock64 *fl, 6984 abi_ulong target_flock_addr) 6985 { 6986 struct target_flock64 *target_fl; 6987 int l_type; 6988 6989 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) { 6990 return -TARGET_EFAULT; 6991 } 6992 6993 __get_user(l_type, &target_fl->l_type); 6994 l_type = target_to_host_flock(l_type); 6995 if (l_type < 0) { 6996 return l_type; 6997 } 6998 fl->l_type = l_type; 6999 __get_user(fl->l_whence, &target_fl->l_whence); 7000 __get_user(fl->l_start, &target_fl->l_start); 7001 __get_user(fl->l_len, &target_fl->l_len); 7002 __get_user(fl->l_pid, &target_fl->l_pid); 7003 unlock_user_struct(target_fl, target_flock_addr, 0); 7004 return 0; 7005 } 7006 7007 static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr, 7008 const struct flock64 *fl) 7009 { 7010 struct target_flock64 *target_fl; 7011 short l_type; 7012 7013 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) { 7014 return -TARGET_EFAULT; 7015 } 7016 7017 l_type = host_to_target_flock(fl->l_type); 7018 __put_user(l_type, &target_fl->l_type); 7019 __put_user(fl->l_whence, &target_fl->l_whence); 7020 __put_user(fl->l_start, &target_fl->l_start); 7021 __put_user(fl->l_len, &target_fl->l_len); 7022 __put_user(fl->l_pid, &target_fl->l_pid); 7023 unlock_user_struct(target_fl, target_flock_addr, 1); 7024 return 0; 7025 } 7026 7027 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg) 7028 { 7029 struct flock64 fl64; 7030 #ifdef F_GETOWN_EX 7031 struct f_owner_ex fox; 7032 struct target_f_owner_ex *target_fox; 7033 #endif 7034 abi_long ret; 7035 int host_cmd = target_to_host_fcntl_cmd(cmd); 7036 7037 if (host_cmd == -TARGET_EINVAL) 7038 return host_cmd; 7039 7040 switch(cmd) { 7041 case TARGET_F_GETLK: 7042 ret = copy_from_user_flock(&fl64, arg); 7043 if (ret) { 7044 return ret; 7045 } 7046 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); 7047 if (ret == 0) { 7048 ret = copy_to_user_flock(arg, &fl64); 7049 } 7050 break; 7051 7052 case TARGET_F_SETLK: 7053 case TARGET_F_SETLKW: 7054 ret = copy_from_user_flock(&fl64, arg); 7055 if (ret) { 7056 return ret; 7057 } 7058 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); 7059 break; 7060 7061 case TARGET_F_GETLK64: 7062 case TARGET_F_OFD_GETLK: 7063 ret = copy_from_user_flock64(&fl64, arg); 7064 if (ret) { 7065 return ret; 7066 } 7067 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); 7068 if (ret == 0) { 7069 ret = copy_to_user_flock64(arg, &fl64); 7070 } 7071 break; 7072 case TARGET_F_SETLK64: 7073 case TARGET_F_SETLKW64: 7074 case TARGET_F_OFD_SETLK: 7075 case TARGET_F_OFD_SETLKW: 7076 ret = copy_from_user_flock64(&fl64, arg); 7077 if (ret) { 7078 return ret; 7079 } 7080 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64)); 7081 break; 7082 7083 case TARGET_F_GETFL: 7084 ret = get_errno(safe_fcntl(fd, host_cmd, arg)); 7085 if (ret >= 0) { 7086 ret = host_to_target_bitmask(ret, fcntl_flags_tbl); 7087 } 7088 break; 7089 7090 case TARGET_F_SETFL: 7091 ret = get_errno(safe_fcntl(fd, host_cmd, 7092 target_to_host_bitmask(arg, 7093 fcntl_flags_tbl))); 7094 break; 7095 7096 #ifdef F_GETOWN_EX 7097 case TARGET_F_GETOWN_EX: 7098 ret = get_errno(safe_fcntl(fd, host_cmd, &fox)); 7099 if (ret >= 0) { 7100 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0)) 7101 return -TARGET_EFAULT; 7102 target_fox->type = tswap32(fox.type); 7103 target_fox->pid = tswap32(fox.pid); 7104 unlock_user_struct(target_fox, arg, 1); 7105 } 7106 break; 7107 #endif 7108 7109 #ifdef F_SETOWN_EX 7110 case TARGET_F_SETOWN_EX: 7111 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1)) 7112 return -TARGET_EFAULT; 7113 fox.type = tswap32(target_fox->type); 7114 fox.pid = tswap32(target_fox->pid); 7115 unlock_user_struct(target_fox, arg, 0); 7116 ret = get_errno(safe_fcntl(fd, host_cmd, &fox)); 7117 break; 7118 #endif 7119 7120 case TARGET_F_SETSIG: 7121 ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg))); 7122 break; 7123 7124 case TARGET_F_GETSIG: 7125 ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg))); 7126 break; 7127 7128 case TARGET_F_SETOWN: 7129 case TARGET_F_GETOWN: 7130 case TARGET_F_SETLEASE: 7131 case TARGET_F_GETLEASE: 7132 case TARGET_F_SETPIPE_SZ: 7133 case TARGET_F_GETPIPE_SZ: 7134 case TARGET_F_ADD_SEALS: 7135 case TARGET_F_GET_SEALS: 7136 ret = get_errno(safe_fcntl(fd, host_cmd, arg)); 7137 break; 7138 7139 default: 7140 ret = get_errno(safe_fcntl(fd, cmd, arg)); 7141 break; 7142 } 7143 return ret; 7144 } 7145 7146 #ifdef USE_UID16 7147 7148 static inline int high2lowuid(int uid) 7149 { 7150 if (uid > 65535) 7151 return 65534; 7152 else 7153 return uid; 7154 } 7155 7156 static inline int high2lowgid(int gid) 7157 { 7158 if (gid > 65535) 7159 return 65534; 7160 else 7161 return gid; 7162 } 7163 7164 static inline int low2highuid(int uid) 7165 { 7166 if ((int16_t)uid == -1) 7167 return -1; 7168 else 7169 return uid; 7170 } 7171 7172 static inline int low2highgid(int gid) 7173 { 7174 if ((int16_t)gid == -1) 7175 return -1; 7176 else 7177 return gid; 7178 } 7179 static inline int tswapid(int id) 7180 { 7181 return tswap16(id); 7182 } 7183 7184 #define put_user_id(x, gaddr) put_user_u16(x, gaddr) 7185 7186 #else /* !USE_UID16 */ 7187 static inline int high2lowuid(int uid) 7188 { 7189 return uid; 7190 } 7191 static inline int high2lowgid(int gid) 7192 { 7193 return gid; 7194 } 7195 static inline int low2highuid(int uid) 7196 { 7197 return uid; 7198 } 7199 static inline int low2highgid(int gid) 7200 { 7201 return gid; 7202 } 7203 static inline int tswapid(int id) 7204 { 7205 return tswap32(id); 7206 } 7207 7208 #define put_user_id(x, gaddr) put_user_u32(x, gaddr) 7209 7210 #endif /* USE_UID16 */ 7211 7212 /* We must do direct syscalls for setting UID/GID, because we want to 7213 * implement the Linux system call semantics of "change only for this thread", 7214 * not the libc/POSIX semantics of "change for all threads in process". 7215 * (See http://ewontfix.com/17/ for more details.) 7216 * We use the 32-bit version of the syscalls if present; if it is not 7217 * then either the host architecture supports 32-bit UIDs natively with 7218 * the standard syscall, or the 16-bit UID is the best we can do. 7219 */ 7220 #ifdef __NR_setuid32 7221 #define __NR_sys_setuid __NR_setuid32 7222 #else 7223 #define __NR_sys_setuid __NR_setuid 7224 #endif 7225 #ifdef __NR_setgid32 7226 #define __NR_sys_setgid __NR_setgid32 7227 #else 7228 #define __NR_sys_setgid __NR_setgid 7229 #endif 7230 #ifdef __NR_setresuid32 7231 #define __NR_sys_setresuid __NR_setresuid32 7232 #else 7233 #define __NR_sys_setresuid __NR_setresuid 7234 #endif 7235 #ifdef __NR_setresgid32 7236 #define __NR_sys_setresgid __NR_setresgid32 7237 #else 7238 #define __NR_sys_setresgid __NR_setresgid 7239 #endif 7240 7241 _syscall1(int, sys_setuid, uid_t, uid) 7242 _syscall1(int, sys_setgid, gid_t, gid) 7243 _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid) 7244 _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid) 7245 7246 void syscall_init(void) 7247 { 7248 IOCTLEntry *ie; 7249 const argtype *arg_type; 7250 int size; 7251 7252 thunk_init(STRUCT_MAX); 7253 7254 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); 7255 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def); 7256 #include "syscall_types.h" 7257 #undef STRUCT 7258 #undef STRUCT_SPECIAL 7259 7260 /* we patch the ioctl size if necessary. We rely on the fact that 7261 no ioctl has all the bits at '1' in the size field */ 7262 ie = ioctl_entries; 7263 while (ie->target_cmd != 0) { 7264 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) == 7265 TARGET_IOC_SIZEMASK) { 7266 arg_type = ie->arg_type; 7267 if (arg_type[0] != TYPE_PTR) { 7268 fprintf(stderr, "cannot patch size for ioctl 0x%x\n", 7269 ie->target_cmd); 7270 exit(1); 7271 } 7272 arg_type++; 7273 size = thunk_type_size(arg_type, 0); 7274 ie->target_cmd = (ie->target_cmd & 7275 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) | 7276 (size << TARGET_IOC_SIZESHIFT); 7277 } 7278 7279 /* automatic consistency check if same arch */ 7280 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \ 7281 (defined(__x86_64__) && defined(TARGET_X86_64)) 7282 if (unlikely(ie->target_cmd != ie->host_cmd)) { 7283 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n", 7284 ie->name, ie->target_cmd, ie->host_cmd); 7285 } 7286 #endif 7287 ie++; 7288 } 7289 } 7290 7291 #ifdef TARGET_NR_truncate64 7292 static inline abi_long target_truncate64(void *cpu_env, const char *arg1, 7293 abi_long arg2, 7294 abi_long arg3, 7295 abi_long arg4) 7296 { 7297 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) { 7298 arg2 = arg3; 7299 arg3 = arg4; 7300 } 7301 return get_errno(truncate64(arg1, target_offset64(arg2, arg3))); 7302 } 7303 #endif 7304 7305 #ifdef TARGET_NR_ftruncate64 7306 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1, 7307 abi_long arg2, 7308 abi_long arg3, 7309 abi_long arg4) 7310 { 7311 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) { 7312 arg2 = arg3; 7313 arg3 = arg4; 7314 } 7315 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3))); 7316 } 7317 #endif 7318 7319 #if defined(TARGET_NR_timer_settime) || \ 7320 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)) 7321 static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its, 7322 abi_ulong target_addr) 7323 { 7324 if (target_to_host_timespec(&host_its->it_interval, target_addr + 7325 offsetof(struct target_itimerspec, 7326 it_interval)) || 7327 target_to_host_timespec(&host_its->it_value, target_addr + 7328 offsetof(struct target_itimerspec, 7329 it_value))) { 7330 return -TARGET_EFAULT; 7331 } 7332 7333 return 0; 7334 } 7335 #endif 7336 7337 #if defined(TARGET_NR_timer_settime64) || \ 7338 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) 7339 static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its, 7340 abi_ulong target_addr) 7341 { 7342 if (target_to_host_timespec64(&host_its->it_interval, target_addr + 7343 offsetof(struct target__kernel_itimerspec, 7344 it_interval)) || 7345 target_to_host_timespec64(&host_its->it_value, target_addr + 7346 offsetof(struct target__kernel_itimerspec, 7347 it_value))) { 7348 return -TARGET_EFAULT; 7349 } 7350 7351 return 0; 7352 } 7353 #endif 7354 7355 #if ((defined(TARGET_NR_timerfd_gettime) || \ 7356 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \ 7357 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime) 7358 static inline abi_long host_to_target_itimerspec(abi_ulong target_addr, 7359 struct itimerspec *host_its) 7360 { 7361 if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec, 7362 it_interval), 7363 &host_its->it_interval) || 7364 host_to_target_timespec(target_addr + offsetof(struct target_itimerspec, 7365 it_value), 7366 &host_its->it_value)) { 7367 return -TARGET_EFAULT; 7368 } 7369 return 0; 7370 } 7371 #endif 7372 7373 #if ((defined(TARGET_NR_timerfd_gettime64) || \ 7374 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \ 7375 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64) 7376 static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr, 7377 struct itimerspec *host_its) 7378 { 7379 if (host_to_target_timespec64(target_addr + 7380 offsetof(struct target__kernel_itimerspec, 7381 it_interval), 7382 &host_its->it_interval) || 7383 host_to_target_timespec64(target_addr + 7384 offsetof(struct target__kernel_itimerspec, 7385 it_value), 7386 &host_its->it_value)) { 7387 return -TARGET_EFAULT; 7388 } 7389 return 0; 7390 } 7391 #endif 7392 7393 #if defined(TARGET_NR_adjtimex) || \ 7394 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)) 7395 static inline abi_long target_to_host_timex(struct timex *host_tx, 7396 abi_long target_addr) 7397 { 7398 struct target_timex *target_tx; 7399 7400 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) { 7401 return -TARGET_EFAULT; 7402 } 7403 7404 __get_user(host_tx->modes, &target_tx->modes); 7405 __get_user(host_tx->offset, &target_tx->offset); 7406 __get_user(host_tx->freq, &target_tx->freq); 7407 __get_user(host_tx->maxerror, &target_tx->maxerror); 7408 __get_user(host_tx->esterror, &target_tx->esterror); 7409 __get_user(host_tx->status, &target_tx->status); 7410 __get_user(host_tx->constant, &target_tx->constant); 7411 __get_user(host_tx->precision, &target_tx->precision); 7412 __get_user(host_tx->tolerance, &target_tx->tolerance); 7413 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec); 7414 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec); 7415 __get_user(host_tx->tick, &target_tx->tick); 7416 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq); 7417 __get_user(host_tx->jitter, &target_tx->jitter); 7418 __get_user(host_tx->shift, &target_tx->shift); 7419 __get_user(host_tx->stabil, &target_tx->stabil); 7420 __get_user(host_tx->jitcnt, &target_tx->jitcnt); 7421 __get_user(host_tx->calcnt, &target_tx->calcnt); 7422 __get_user(host_tx->errcnt, &target_tx->errcnt); 7423 __get_user(host_tx->stbcnt, &target_tx->stbcnt); 7424 __get_user(host_tx->tai, &target_tx->tai); 7425 7426 unlock_user_struct(target_tx, target_addr, 0); 7427 return 0; 7428 } 7429 7430 static inline abi_long host_to_target_timex(abi_long target_addr, 7431 struct timex *host_tx) 7432 { 7433 struct target_timex *target_tx; 7434 7435 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) { 7436 return -TARGET_EFAULT; 7437 } 7438 7439 __put_user(host_tx->modes, &target_tx->modes); 7440 __put_user(host_tx->offset, &target_tx->offset); 7441 __put_user(host_tx->freq, &target_tx->freq); 7442 __put_user(host_tx->maxerror, &target_tx->maxerror); 7443 __put_user(host_tx->esterror, &target_tx->esterror); 7444 __put_user(host_tx->status, &target_tx->status); 7445 __put_user(host_tx->constant, &target_tx->constant); 7446 __put_user(host_tx->precision, &target_tx->precision); 7447 __put_user(host_tx->tolerance, &target_tx->tolerance); 7448 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec); 7449 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec); 7450 __put_user(host_tx->tick, &target_tx->tick); 7451 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq); 7452 __put_user(host_tx->jitter, &target_tx->jitter); 7453 __put_user(host_tx->shift, &target_tx->shift); 7454 __put_user(host_tx->stabil, &target_tx->stabil); 7455 __put_user(host_tx->jitcnt, &target_tx->jitcnt); 7456 __put_user(host_tx->calcnt, &target_tx->calcnt); 7457 __put_user(host_tx->errcnt, &target_tx->errcnt); 7458 __put_user(host_tx->stbcnt, &target_tx->stbcnt); 7459 __put_user(host_tx->tai, &target_tx->tai); 7460 7461 unlock_user_struct(target_tx, target_addr, 1); 7462 return 0; 7463 } 7464 #endif 7465 7466 7467 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME) 7468 static inline abi_long target_to_host_timex64(struct timex *host_tx, 7469 abi_long target_addr) 7470 { 7471 struct target__kernel_timex *target_tx; 7472 7473 if (copy_from_user_timeval64(&host_tx->time, target_addr + 7474 offsetof(struct target__kernel_timex, 7475 time))) { 7476 return -TARGET_EFAULT; 7477 } 7478 7479 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) { 7480 return -TARGET_EFAULT; 7481 } 7482 7483 __get_user(host_tx->modes, &target_tx->modes); 7484 __get_user(host_tx->offset, &target_tx->offset); 7485 __get_user(host_tx->freq, &target_tx->freq); 7486 __get_user(host_tx->maxerror, &target_tx->maxerror); 7487 __get_user(host_tx->esterror, &target_tx->esterror); 7488 __get_user(host_tx->status, &target_tx->status); 7489 __get_user(host_tx->constant, &target_tx->constant); 7490 __get_user(host_tx->precision, &target_tx->precision); 7491 __get_user(host_tx->tolerance, &target_tx->tolerance); 7492 __get_user(host_tx->tick, &target_tx->tick); 7493 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq); 7494 __get_user(host_tx->jitter, &target_tx->jitter); 7495 __get_user(host_tx->shift, &target_tx->shift); 7496 __get_user(host_tx->stabil, &target_tx->stabil); 7497 __get_user(host_tx->jitcnt, &target_tx->jitcnt); 7498 __get_user(host_tx->calcnt, &target_tx->calcnt); 7499 __get_user(host_tx->errcnt, &target_tx->errcnt); 7500 __get_user(host_tx->stbcnt, &target_tx->stbcnt); 7501 __get_user(host_tx->tai, &target_tx->tai); 7502 7503 unlock_user_struct(target_tx, target_addr, 0); 7504 return 0; 7505 } 7506 7507 static inline abi_long host_to_target_timex64(abi_long target_addr, 7508 struct timex *host_tx) 7509 { 7510 struct target__kernel_timex *target_tx; 7511 7512 if (copy_to_user_timeval64(target_addr + 7513 offsetof(struct target__kernel_timex, time), 7514 &host_tx->time)) { 7515 return -TARGET_EFAULT; 7516 } 7517 7518 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) { 7519 return -TARGET_EFAULT; 7520 } 7521 7522 __put_user(host_tx->modes, &target_tx->modes); 7523 __put_user(host_tx->offset, &target_tx->offset); 7524 __put_user(host_tx->freq, &target_tx->freq); 7525 __put_user(host_tx->maxerror, &target_tx->maxerror); 7526 __put_user(host_tx->esterror, &target_tx->esterror); 7527 __put_user(host_tx->status, &target_tx->status); 7528 __put_user(host_tx->constant, &target_tx->constant); 7529 __put_user(host_tx->precision, &target_tx->precision); 7530 __put_user(host_tx->tolerance, &target_tx->tolerance); 7531 __put_user(host_tx->tick, &target_tx->tick); 7532 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq); 7533 __put_user(host_tx->jitter, &target_tx->jitter); 7534 __put_user(host_tx->shift, &target_tx->shift); 7535 __put_user(host_tx->stabil, &target_tx->stabil); 7536 __put_user(host_tx->jitcnt, &target_tx->jitcnt); 7537 __put_user(host_tx->calcnt, &target_tx->calcnt); 7538 __put_user(host_tx->errcnt, &target_tx->errcnt); 7539 __put_user(host_tx->stbcnt, &target_tx->stbcnt); 7540 __put_user(host_tx->tai, &target_tx->tai); 7541 7542 unlock_user_struct(target_tx, target_addr, 1); 7543 return 0; 7544 } 7545 #endif 7546 7547 #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID 7548 #define sigev_notify_thread_id _sigev_un._tid 7549 #endif 7550 7551 static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp, 7552 abi_ulong target_addr) 7553 { 7554 struct target_sigevent *target_sevp; 7555 7556 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) { 7557 return -TARGET_EFAULT; 7558 } 7559 7560 /* This union is awkward on 64 bit systems because it has a 32 bit 7561 * integer and a pointer in it; we follow the conversion approach 7562 * used for handling sigval types in signal.c so the guest should get 7563 * the correct value back even if we did a 64 bit byteswap and it's 7564 * using the 32 bit integer. 7565 */ 7566 host_sevp->sigev_value.sival_ptr = 7567 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr); 7568 host_sevp->sigev_signo = 7569 target_to_host_signal(tswap32(target_sevp->sigev_signo)); 7570 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify); 7571 host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid); 7572 7573 unlock_user_struct(target_sevp, target_addr, 1); 7574 return 0; 7575 } 7576 7577 #if defined(TARGET_NR_mlockall) 7578 static inline int target_to_host_mlockall_arg(int arg) 7579 { 7580 int result = 0; 7581 7582 if (arg & TARGET_MCL_CURRENT) { 7583 result |= MCL_CURRENT; 7584 } 7585 if (arg & TARGET_MCL_FUTURE) { 7586 result |= MCL_FUTURE; 7587 } 7588 #ifdef MCL_ONFAULT 7589 if (arg & TARGET_MCL_ONFAULT) { 7590 result |= MCL_ONFAULT; 7591 } 7592 #endif 7593 7594 return result; 7595 } 7596 #endif 7597 7598 #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \ 7599 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \ 7600 defined(TARGET_NR_newfstatat)) 7601 static inline abi_long host_to_target_stat64(void *cpu_env, 7602 abi_ulong target_addr, 7603 struct stat *host_st) 7604 { 7605 #if defined(TARGET_ARM) && defined(TARGET_ABI32) 7606 if (((CPUARMState *)cpu_env)->eabi) { 7607 struct target_eabi_stat64 *target_st; 7608 7609 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) 7610 return -TARGET_EFAULT; 7611 memset(target_st, 0, sizeof(struct target_eabi_stat64)); 7612 __put_user(host_st->st_dev, &target_st->st_dev); 7613 __put_user(host_st->st_ino, &target_st->st_ino); 7614 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO 7615 __put_user(host_st->st_ino, &target_st->__st_ino); 7616 #endif 7617 __put_user(host_st->st_mode, &target_st->st_mode); 7618 __put_user(host_st->st_nlink, &target_st->st_nlink); 7619 __put_user(host_st->st_uid, &target_st->st_uid); 7620 __put_user(host_st->st_gid, &target_st->st_gid); 7621 __put_user(host_st->st_rdev, &target_st->st_rdev); 7622 __put_user(host_st->st_size, &target_st->st_size); 7623 __put_user(host_st->st_blksize, &target_st->st_blksize); 7624 __put_user(host_st->st_blocks, &target_st->st_blocks); 7625 __put_user(host_st->st_atime, &target_st->target_st_atime); 7626 __put_user(host_st->st_mtime, &target_st->target_st_mtime); 7627 __put_user(host_st->st_ctime, &target_st->target_st_ctime); 7628 #ifdef HAVE_STRUCT_STAT_ST_ATIM 7629 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec); 7630 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec); 7631 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec); 7632 #endif 7633 unlock_user_struct(target_st, target_addr, 1); 7634 } else 7635 #endif 7636 { 7637 #if defined(TARGET_HAS_STRUCT_STAT64) 7638 struct target_stat64 *target_st; 7639 #else 7640 struct target_stat *target_st; 7641 #endif 7642 7643 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) 7644 return -TARGET_EFAULT; 7645 memset(target_st, 0, sizeof(*target_st)); 7646 __put_user(host_st->st_dev, &target_st->st_dev); 7647 __put_user(host_st->st_ino, &target_st->st_ino); 7648 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO 7649 __put_user(host_st->st_ino, &target_st->__st_ino); 7650 #endif 7651 __put_user(host_st->st_mode, &target_st->st_mode); 7652 __put_user(host_st->st_nlink, &target_st->st_nlink); 7653 __put_user(host_st->st_uid, &target_st->st_uid); 7654 __put_user(host_st->st_gid, &target_st->st_gid); 7655 __put_user(host_st->st_rdev, &target_st->st_rdev); 7656 /* XXX: better use of kernel struct */ 7657 __put_user(host_st->st_size, &target_st->st_size); 7658 __put_user(host_st->st_blksize, &target_st->st_blksize); 7659 __put_user(host_st->st_blocks, &target_st->st_blocks); 7660 __put_user(host_st->st_atime, &target_st->target_st_atime); 7661 __put_user(host_st->st_mtime, &target_st->target_st_mtime); 7662 __put_user(host_st->st_ctime, &target_st->target_st_ctime); 7663 #ifdef HAVE_STRUCT_STAT_ST_ATIM 7664 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec); 7665 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec); 7666 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec); 7667 #endif 7668 unlock_user_struct(target_st, target_addr, 1); 7669 } 7670 7671 return 0; 7672 } 7673 #endif 7674 7675 #if defined(TARGET_NR_statx) && defined(__NR_statx) 7676 static inline abi_long host_to_target_statx(struct target_statx *host_stx, 7677 abi_ulong target_addr) 7678 { 7679 struct target_statx *target_stx; 7680 7681 if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) { 7682 return -TARGET_EFAULT; 7683 } 7684 memset(target_stx, 0, sizeof(*target_stx)); 7685 7686 __put_user(host_stx->stx_mask, &target_stx->stx_mask); 7687 __put_user(host_stx->stx_blksize, &target_stx->stx_blksize); 7688 __put_user(host_stx->stx_attributes, &target_stx->stx_attributes); 7689 __put_user(host_stx->stx_nlink, &target_stx->stx_nlink); 7690 __put_user(host_stx->stx_uid, &target_stx->stx_uid); 7691 __put_user(host_stx->stx_gid, &target_stx->stx_gid); 7692 __put_user(host_stx->stx_mode, &target_stx->stx_mode); 7693 __put_user(host_stx->stx_ino, &target_stx->stx_ino); 7694 __put_user(host_stx->stx_size, &target_stx->stx_size); 7695 __put_user(host_stx->stx_blocks, &target_stx->stx_blocks); 7696 __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask); 7697 __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec); 7698 __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec); 7699 __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec); 7700 __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec); 7701 __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec); 7702 __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec); 7703 __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec); 7704 __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec); 7705 __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major); 7706 __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor); 7707 __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major); 7708 __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor); 7709 7710 unlock_user_struct(target_stx, target_addr, 1); 7711 7712 return 0; 7713 } 7714 #endif 7715 7716 static int do_sys_futex(int *uaddr, int op, int val, 7717 const struct timespec *timeout, int *uaddr2, 7718 int val3) 7719 { 7720 #if HOST_LONG_BITS == 64 7721 #if defined(__NR_futex) 7722 /* always a 64-bit time_t, it doesn't define _time64 version */ 7723 return sys_futex(uaddr, op, val, timeout, uaddr2, val3); 7724 7725 #endif 7726 #else /* HOST_LONG_BITS == 64 */ 7727 #if defined(__NR_futex_time64) 7728 if (sizeof(timeout->tv_sec) == 8) { 7729 /* _time64 function on 32bit arch */ 7730 return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3); 7731 } 7732 #endif 7733 #if defined(__NR_futex) 7734 /* old function on 32bit arch */ 7735 return sys_futex(uaddr, op, val, timeout, uaddr2, val3); 7736 #endif 7737 #endif /* HOST_LONG_BITS == 64 */ 7738 g_assert_not_reached(); 7739 } 7740 7741 static int do_safe_futex(int *uaddr, int op, int val, 7742 const struct timespec *timeout, int *uaddr2, 7743 int val3) 7744 { 7745 #if HOST_LONG_BITS == 64 7746 #if defined(__NR_futex) 7747 /* always a 64-bit time_t, it doesn't define _time64 version */ 7748 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3)); 7749 #endif 7750 #else /* HOST_LONG_BITS == 64 */ 7751 #if defined(__NR_futex_time64) 7752 if (sizeof(timeout->tv_sec) == 8) { 7753 /* _time64 function on 32bit arch */ 7754 return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2, 7755 val3)); 7756 } 7757 #endif 7758 #if defined(__NR_futex) 7759 /* old function on 32bit arch */ 7760 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3)); 7761 #endif 7762 #endif /* HOST_LONG_BITS == 64 */ 7763 return -TARGET_ENOSYS; 7764 } 7765 7766 /* ??? Using host futex calls even when target atomic operations 7767 are not really atomic probably breaks things. However implementing 7768 futexes locally would make futexes shared between multiple processes 7769 tricky. However they're probably useless because guest atomic 7770 operations won't work either. */ 7771 #if defined(TARGET_NR_futex) 7772 static int do_futex(CPUState *cpu, target_ulong uaddr, int op, int val, 7773 target_ulong timeout, target_ulong uaddr2, int val3) 7774 { 7775 struct timespec ts, *pts; 7776 int base_op; 7777 7778 /* ??? We assume FUTEX_* constants are the same on both host 7779 and target. */ 7780 #ifdef FUTEX_CMD_MASK 7781 base_op = op & FUTEX_CMD_MASK; 7782 #else 7783 base_op = op; 7784 #endif 7785 switch (base_op) { 7786 case FUTEX_WAIT: 7787 case FUTEX_WAIT_BITSET: 7788 if (timeout) { 7789 pts = &ts; 7790 target_to_host_timespec(pts, timeout); 7791 } else { 7792 pts = NULL; 7793 } 7794 return do_safe_futex(g2h(cpu, uaddr), 7795 op, tswap32(val), pts, NULL, val3); 7796 case FUTEX_WAKE: 7797 return do_safe_futex(g2h(cpu, uaddr), 7798 op, val, NULL, NULL, 0); 7799 case FUTEX_FD: 7800 return do_safe_futex(g2h(cpu, uaddr), 7801 op, val, NULL, NULL, 0); 7802 case FUTEX_REQUEUE: 7803 case FUTEX_CMP_REQUEUE: 7804 case FUTEX_WAKE_OP: 7805 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the 7806 TIMEOUT parameter is interpreted as a uint32_t by the kernel. 7807 But the prototype takes a `struct timespec *'; insert casts 7808 to satisfy the compiler. We do not need to tswap TIMEOUT 7809 since it's not compared to guest memory. */ 7810 pts = (struct timespec *)(uintptr_t) timeout; 7811 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, g2h(cpu, uaddr2), 7812 (base_op == FUTEX_CMP_REQUEUE 7813 ? tswap32(val3) : val3)); 7814 default: 7815 return -TARGET_ENOSYS; 7816 } 7817 } 7818 #endif 7819 7820 #if defined(TARGET_NR_futex_time64) 7821 static int do_futex_time64(CPUState *cpu, target_ulong uaddr, int op, 7822 int val, target_ulong timeout, 7823 target_ulong uaddr2, int val3) 7824 { 7825 struct timespec ts, *pts; 7826 int base_op; 7827 7828 /* ??? We assume FUTEX_* constants are the same on both host 7829 and target. */ 7830 #ifdef FUTEX_CMD_MASK 7831 base_op = op & FUTEX_CMD_MASK; 7832 #else 7833 base_op = op; 7834 #endif 7835 switch (base_op) { 7836 case FUTEX_WAIT: 7837 case FUTEX_WAIT_BITSET: 7838 if (timeout) { 7839 pts = &ts; 7840 if (target_to_host_timespec64(pts, timeout)) { 7841 return -TARGET_EFAULT; 7842 } 7843 } else { 7844 pts = NULL; 7845 } 7846 return do_safe_futex(g2h(cpu, uaddr), op, 7847 tswap32(val), pts, NULL, val3); 7848 case FUTEX_WAKE: 7849 return do_safe_futex(g2h(cpu, uaddr), op, val, NULL, NULL, 0); 7850 case FUTEX_FD: 7851 return do_safe_futex(g2h(cpu, uaddr), op, val, NULL, NULL, 0); 7852 case FUTEX_REQUEUE: 7853 case FUTEX_CMP_REQUEUE: 7854 case FUTEX_WAKE_OP: 7855 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the 7856 TIMEOUT parameter is interpreted as a uint32_t by the kernel. 7857 But the prototype takes a `struct timespec *'; insert casts 7858 to satisfy the compiler. We do not need to tswap TIMEOUT 7859 since it's not compared to guest memory. */ 7860 pts = (struct timespec *)(uintptr_t) timeout; 7861 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, g2h(cpu, uaddr2), 7862 (base_op == FUTEX_CMP_REQUEUE 7863 ? tswap32(val3) : val3)); 7864 default: 7865 return -TARGET_ENOSYS; 7866 } 7867 } 7868 #endif 7869 7870 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) 7871 static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname, 7872 abi_long handle, abi_long mount_id, 7873 abi_long flags) 7874 { 7875 struct file_handle *target_fh; 7876 struct file_handle *fh; 7877 int mid = 0; 7878 abi_long ret; 7879 char *name; 7880 unsigned int size, total_size; 7881 7882 if (get_user_s32(size, handle)) { 7883 return -TARGET_EFAULT; 7884 } 7885 7886 name = lock_user_string(pathname); 7887 if (!name) { 7888 return -TARGET_EFAULT; 7889 } 7890 7891 total_size = sizeof(struct file_handle) + size; 7892 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0); 7893 if (!target_fh) { 7894 unlock_user(name, pathname, 0); 7895 return -TARGET_EFAULT; 7896 } 7897 7898 fh = g_malloc0(total_size); 7899 fh->handle_bytes = size; 7900 7901 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags)); 7902 unlock_user(name, pathname, 0); 7903 7904 /* man name_to_handle_at(2): 7905 * Other than the use of the handle_bytes field, the caller should treat 7906 * the file_handle structure as an opaque data type 7907 */ 7908 7909 memcpy(target_fh, fh, total_size); 7910 target_fh->handle_bytes = tswap32(fh->handle_bytes); 7911 target_fh->handle_type = tswap32(fh->handle_type); 7912 g_free(fh); 7913 unlock_user(target_fh, handle, total_size); 7914 7915 if (put_user_s32(mid, mount_id)) { 7916 return -TARGET_EFAULT; 7917 } 7918 7919 return ret; 7920 7921 } 7922 #endif 7923 7924 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) 7925 static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle, 7926 abi_long flags) 7927 { 7928 struct file_handle *target_fh; 7929 struct file_handle *fh; 7930 unsigned int size, total_size; 7931 abi_long ret; 7932 7933 if (get_user_s32(size, handle)) { 7934 return -TARGET_EFAULT; 7935 } 7936 7937 total_size = sizeof(struct file_handle) + size; 7938 target_fh = lock_user(VERIFY_READ, handle, total_size, 1); 7939 if (!target_fh) { 7940 return -TARGET_EFAULT; 7941 } 7942 7943 fh = g_memdup(target_fh, total_size); 7944 fh->handle_bytes = size; 7945 fh->handle_type = tswap32(target_fh->handle_type); 7946 7947 ret = get_errno(open_by_handle_at(mount_fd, fh, 7948 target_to_host_bitmask(flags, fcntl_flags_tbl))); 7949 7950 g_free(fh); 7951 7952 unlock_user(target_fh, handle, total_size); 7953 7954 return ret; 7955 } 7956 #endif 7957 7958 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4) 7959 7960 static abi_long do_signalfd4(int fd, abi_long mask, int flags) 7961 { 7962 int host_flags; 7963 target_sigset_t *target_mask; 7964 sigset_t host_mask; 7965 abi_long ret; 7966 7967 if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) { 7968 return -TARGET_EINVAL; 7969 } 7970 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) { 7971 return -TARGET_EFAULT; 7972 } 7973 7974 target_to_host_sigset(&host_mask, target_mask); 7975 7976 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl); 7977 7978 ret = get_errno(signalfd(fd, &host_mask, host_flags)); 7979 if (ret >= 0) { 7980 fd_trans_register(ret, &target_signalfd_trans); 7981 } 7982 7983 unlock_user_struct(target_mask, mask, 0); 7984 7985 return ret; 7986 } 7987 #endif 7988 7989 /* Map host to target signal numbers for the wait family of syscalls. 7990 Assume all other status bits are the same. */ 7991 int host_to_target_waitstatus(int status) 7992 { 7993 if (WIFSIGNALED(status)) { 7994 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f); 7995 } 7996 if (WIFSTOPPED(status)) { 7997 return (host_to_target_signal(WSTOPSIG(status)) << 8) 7998 | (status & 0xff); 7999 } 8000 return status; 8001 } 8002 8003 static int open_self_cmdline(void *cpu_env, int fd) 8004 { 8005 CPUState *cpu = env_cpu((CPUArchState *)cpu_env); 8006 struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm; 8007 int i; 8008 8009 for (i = 0; i < bprm->argc; i++) { 8010 size_t len = strlen(bprm->argv[i]) + 1; 8011 8012 if (write(fd, bprm->argv[i], len) != len) { 8013 return -1; 8014 } 8015 } 8016 8017 return 0; 8018 } 8019 8020 static int open_self_maps(void *cpu_env, int fd) 8021 { 8022 CPUState *cpu = env_cpu((CPUArchState *)cpu_env); 8023 TaskState *ts = cpu->opaque; 8024 GSList *map_info = read_self_maps(); 8025 GSList *s; 8026 int count; 8027 8028 for (s = map_info; s; s = g_slist_next(s)) { 8029 MapInfo *e = (MapInfo *) s->data; 8030 8031 if (h2g_valid(e->start)) { 8032 unsigned long min = e->start; 8033 unsigned long max = e->end; 8034 int flags = page_get_flags(h2g(min)); 8035 const char *path; 8036 8037 max = h2g_valid(max - 1) ? 8038 max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1; 8039 8040 if (page_check_range(h2g(min), max - min, flags) == -1) { 8041 continue; 8042 } 8043 8044 if (h2g(min) == ts->info->stack_limit) { 8045 path = "[stack]"; 8046 } else { 8047 path = e->path; 8048 } 8049 8050 count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr 8051 " %c%c%c%c %08" PRIx64 " %s %"PRId64, 8052 h2g(min), h2g(max - 1) + 1, 8053 (flags & PAGE_READ) ? 'r' : '-', 8054 (flags & PAGE_WRITE_ORG) ? 'w' : '-', 8055 (flags & PAGE_EXEC) ? 'x' : '-', 8056 e->is_priv ? 'p' : 's', 8057 (uint64_t) e->offset, e->dev, e->inode); 8058 if (path) { 8059 dprintf(fd, "%*s%s\n", 73 - count, "", path); 8060 } else { 8061 dprintf(fd, "\n"); 8062 } 8063 } 8064 } 8065 8066 free_self_maps(map_info); 8067 8068 #ifdef TARGET_VSYSCALL_PAGE 8069 /* 8070 * We only support execution from the vsyscall page. 8071 * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3. 8072 */ 8073 count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx 8074 " --xp 00000000 00:00 0", 8075 TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE); 8076 dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]"); 8077 #endif 8078 8079 return 0; 8080 } 8081 8082 static int open_self_stat(void *cpu_env, int fd) 8083 { 8084 CPUState *cpu = env_cpu((CPUArchState *)cpu_env); 8085 TaskState *ts = cpu->opaque; 8086 g_autoptr(GString) buf = g_string_new(NULL); 8087 int i; 8088 8089 for (i = 0; i < 44; i++) { 8090 if (i == 0) { 8091 /* pid */ 8092 g_string_printf(buf, FMT_pid " ", getpid()); 8093 } else if (i == 1) { 8094 /* app name */ 8095 gchar *bin = g_strrstr(ts->bprm->argv[0], "/"); 8096 bin = bin ? bin + 1 : ts->bprm->argv[0]; 8097 g_string_printf(buf, "(%.15s) ", bin); 8098 } else if (i == 3) { 8099 /* ppid */ 8100 g_string_printf(buf, FMT_pid " ", getppid()); 8101 } else if (i == 27) { 8102 /* stack bottom */ 8103 g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack); 8104 } else { 8105 /* for the rest, there is MasterCard */ 8106 g_string_printf(buf, "0%c", i == 43 ? '\n' : ' '); 8107 } 8108 8109 if (write(fd, buf->str, buf->len) != buf->len) { 8110 return -1; 8111 } 8112 } 8113 8114 return 0; 8115 } 8116 8117 static int open_self_auxv(void *cpu_env, int fd) 8118 { 8119 CPUState *cpu = env_cpu((CPUArchState *)cpu_env); 8120 TaskState *ts = cpu->opaque; 8121 abi_ulong auxv = ts->info->saved_auxv; 8122 abi_ulong len = ts->info->auxv_len; 8123 char *ptr; 8124 8125 /* 8126 * Auxiliary vector is stored in target process stack. 8127 * read in whole auxv vector and copy it to file 8128 */ 8129 ptr = lock_user(VERIFY_READ, auxv, len, 0); 8130 if (ptr != NULL) { 8131 while (len > 0) { 8132 ssize_t r; 8133 r = write(fd, ptr, len); 8134 if (r <= 0) { 8135 break; 8136 } 8137 len -= r; 8138 ptr += r; 8139 } 8140 lseek(fd, 0, SEEK_SET); 8141 unlock_user(ptr, auxv, len); 8142 } 8143 8144 return 0; 8145 } 8146 8147 static int is_proc_myself(const char *filename, const char *entry) 8148 { 8149 if (!strncmp(filename, "/proc/", strlen("/proc/"))) { 8150 filename += strlen("/proc/"); 8151 if (!strncmp(filename, "self/", strlen("self/"))) { 8152 filename += strlen("self/"); 8153 } else if (*filename >= '1' && *filename <= '9') { 8154 char myself[80]; 8155 snprintf(myself, sizeof(myself), "%d/", getpid()); 8156 if (!strncmp(filename, myself, strlen(myself))) { 8157 filename += strlen(myself); 8158 } else { 8159 return 0; 8160 } 8161 } else { 8162 return 0; 8163 } 8164 if (!strcmp(filename, entry)) { 8165 return 1; 8166 } 8167 } 8168 return 0; 8169 } 8170 8171 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \ 8172 defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA) 8173 static int is_proc(const char *filename, const char *entry) 8174 { 8175 return strcmp(filename, entry) == 0; 8176 } 8177 #endif 8178 8179 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) 8180 static int open_net_route(void *cpu_env, int fd) 8181 { 8182 FILE *fp; 8183 char *line = NULL; 8184 size_t len = 0; 8185 ssize_t read; 8186 8187 fp = fopen("/proc/net/route", "r"); 8188 if (fp == NULL) { 8189 return -1; 8190 } 8191 8192 /* read header */ 8193 8194 read = getline(&line, &len, fp); 8195 dprintf(fd, "%s", line); 8196 8197 /* read routes */ 8198 8199 while ((read = getline(&line, &len, fp)) != -1) { 8200 char iface[16]; 8201 uint32_t dest, gw, mask; 8202 unsigned int flags, refcnt, use, metric, mtu, window, irtt; 8203 int fields; 8204 8205 fields = sscanf(line, 8206 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", 8207 iface, &dest, &gw, &flags, &refcnt, &use, &metric, 8208 &mask, &mtu, &window, &irtt); 8209 if (fields != 11) { 8210 continue; 8211 } 8212 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n", 8213 iface, tswap32(dest), tswap32(gw), flags, refcnt, use, 8214 metric, tswap32(mask), mtu, window, irtt); 8215 } 8216 8217 free(line); 8218 fclose(fp); 8219 8220 return 0; 8221 } 8222 #endif 8223 8224 #if defined(TARGET_SPARC) 8225 static int open_cpuinfo(void *cpu_env, int fd) 8226 { 8227 dprintf(fd, "type\t\t: sun4u\n"); 8228 return 0; 8229 } 8230 #endif 8231 8232 #if defined(TARGET_HPPA) 8233 static int open_cpuinfo(void *cpu_env, int fd) 8234 { 8235 dprintf(fd, "cpu family\t: PA-RISC 1.1e\n"); 8236 dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n"); 8237 dprintf(fd, "capabilities\t: os32\n"); 8238 dprintf(fd, "model\t\t: 9000/778/B160L\n"); 8239 dprintf(fd, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n"); 8240 return 0; 8241 } 8242 #endif 8243 8244 #if defined(TARGET_M68K) 8245 static int open_hardware(void *cpu_env, int fd) 8246 { 8247 dprintf(fd, "Model:\t\tqemu-m68k\n"); 8248 return 0; 8249 } 8250 #endif 8251 8252 static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode) 8253 { 8254 struct fake_open { 8255 const char *filename; 8256 int (*fill)(void *cpu_env, int fd); 8257 int (*cmp)(const char *s1, const char *s2); 8258 }; 8259 const struct fake_open *fake_open; 8260 static const struct fake_open fakes[] = { 8261 { "maps", open_self_maps, is_proc_myself }, 8262 { "stat", open_self_stat, is_proc_myself }, 8263 { "auxv", open_self_auxv, is_proc_myself }, 8264 { "cmdline", open_self_cmdline, is_proc_myself }, 8265 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) 8266 { "/proc/net/route", open_net_route, is_proc }, 8267 #endif 8268 #if defined(TARGET_SPARC) || defined(TARGET_HPPA) 8269 { "/proc/cpuinfo", open_cpuinfo, is_proc }, 8270 #endif 8271 #if defined(TARGET_M68K) 8272 { "/proc/hardware", open_hardware, is_proc }, 8273 #endif 8274 { NULL, NULL, NULL } 8275 }; 8276 8277 if (is_proc_myself(pathname, "exe")) { 8278 int execfd = qemu_getauxval(AT_EXECFD); 8279 return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode); 8280 } 8281 8282 for (fake_open = fakes; fake_open->filename; fake_open++) { 8283 if (fake_open->cmp(pathname, fake_open->filename)) { 8284 break; 8285 } 8286 } 8287 8288 if (fake_open->filename) { 8289 const char *tmpdir; 8290 char filename[PATH_MAX]; 8291 int fd, r; 8292 8293 /* create temporary file to map stat to */ 8294 tmpdir = getenv("TMPDIR"); 8295 if (!tmpdir) 8296 tmpdir = "/tmp"; 8297 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); 8298 fd = mkstemp(filename); 8299 if (fd < 0) { 8300 return fd; 8301 } 8302 unlink(filename); 8303 8304 if ((r = fake_open->fill(cpu_env, fd))) { 8305 int e = errno; 8306 close(fd); 8307 errno = e; 8308 return r; 8309 } 8310 lseek(fd, 0, SEEK_SET); 8311 8312 return fd; 8313 } 8314 8315 return safe_openat(dirfd, path(pathname), flags, mode); 8316 } 8317 8318 #define TIMER_MAGIC 0x0caf0000 8319 #define TIMER_MAGIC_MASK 0xffff0000 8320 8321 /* Convert QEMU provided timer ID back to internal 16bit index format */ 8322 static target_timer_t get_timer_id(abi_long arg) 8323 { 8324 target_timer_t timerid = arg; 8325 8326 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) { 8327 return -TARGET_EINVAL; 8328 } 8329 8330 timerid &= 0xffff; 8331 8332 if (timerid >= ARRAY_SIZE(g_posix_timers)) { 8333 return -TARGET_EINVAL; 8334 } 8335 8336 return timerid; 8337 } 8338 8339 static int target_to_host_cpu_mask(unsigned long *host_mask, 8340 size_t host_size, 8341 abi_ulong target_addr, 8342 size_t target_size) 8343 { 8344 unsigned target_bits = sizeof(abi_ulong) * 8; 8345 unsigned host_bits = sizeof(*host_mask) * 8; 8346 abi_ulong *target_mask; 8347 unsigned i, j; 8348 8349 assert(host_size >= target_size); 8350 8351 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1); 8352 if (!target_mask) { 8353 return -TARGET_EFAULT; 8354 } 8355 memset(host_mask, 0, host_size); 8356 8357 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) { 8358 unsigned bit = i * target_bits; 8359 abi_ulong val; 8360 8361 __get_user(val, &target_mask[i]); 8362 for (j = 0; j < target_bits; j++, bit++) { 8363 if (val & (1UL << j)) { 8364 host_mask[bit / host_bits] |= 1UL << (bit % host_bits); 8365 } 8366 } 8367 } 8368 8369 unlock_user(target_mask, target_addr, 0); 8370 return 0; 8371 } 8372 8373 static int host_to_target_cpu_mask(const unsigned long *host_mask, 8374 size_t host_size, 8375 abi_ulong target_addr, 8376 size_t target_size) 8377 { 8378 unsigned target_bits = sizeof(abi_ulong) * 8; 8379 unsigned host_bits = sizeof(*host_mask) * 8; 8380 abi_ulong *target_mask; 8381 unsigned i, j; 8382 8383 assert(host_size >= target_size); 8384 8385 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0); 8386 if (!target_mask) { 8387 return -TARGET_EFAULT; 8388 } 8389 8390 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) { 8391 unsigned bit = i * target_bits; 8392 abi_ulong val = 0; 8393 8394 for (j = 0; j < target_bits; j++, bit++) { 8395 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) { 8396 val |= 1UL << j; 8397 } 8398 } 8399 __put_user(val, &target_mask[i]); 8400 } 8401 8402 unlock_user(target_mask, target_addr, target_size); 8403 return 0; 8404 } 8405 8406 #ifdef TARGET_NR_getdents 8407 static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count) 8408 { 8409 g_autofree void *hdirp = NULL; 8410 void *tdirp; 8411 int hlen, hoff, toff; 8412 int hreclen, treclen; 8413 off64_t prev_diroff = 0; 8414 8415 hdirp = g_try_malloc(count); 8416 if (!hdirp) { 8417 return -TARGET_ENOMEM; 8418 } 8419 8420 #ifdef EMULATE_GETDENTS_WITH_GETDENTS 8421 hlen = sys_getdents(dirfd, hdirp, count); 8422 #else 8423 hlen = sys_getdents64(dirfd, hdirp, count); 8424 #endif 8425 8426 hlen = get_errno(hlen); 8427 if (is_error(hlen)) { 8428 return hlen; 8429 } 8430 8431 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0); 8432 if (!tdirp) { 8433 return -TARGET_EFAULT; 8434 } 8435 8436 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) { 8437 #ifdef EMULATE_GETDENTS_WITH_GETDENTS 8438 struct linux_dirent *hde = hdirp + hoff; 8439 #else 8440 struct linux_dirent64 *hde = hdirp + hoff; 8441 #endif 8442 struct target_dirent *tde = tdirp + toff; 8443 int namelen; 8444 uint8_t type; 8445 8446 namelen = strlen(hde->d_name); 8447 hreclen = hde->d_reclen; 8448 treclen = offsetof(struct target_dirent, d_name) + namelen + 2; 8449 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent)); 8450 8451 if (toff + treclen > count) { 8452 /* 8453 * If the host struct is smaller than the target struct, or 8454 * requires less alignment and thus packs into less space, 8455 * then the host can return more entries than we can pass 8456 * on to the guest. 8457 */ 8458 if (toff == 0) { 8459 toff = -TARGET_EINVAL; /* result buffer is too small */ 8460 break; 8461 } 8462 /* 8463 * Return what we have, resetting the file pointer to the 8464 * location of the first record not returned. 8465 */ 8466 lseek64(dirfd, prev_diroff, SEEK_SET); 8467 break; 8468 } 8469 8470 prev_diroff = hde->d_off; 8471 tde->d_ino = tswapal(hde->d_ino); 8472 tde->d_off = tswapal(hde->d_off); 8473 tde->d_reclen = tswap16(treclen); 8474 memcpy(tde->d_name, hde->d_name, namelen + 1); 8475 8476 /* 8477 * The getdents type is in what was formerly a padding byte at the 8478 * end of the structure. 8479 */ 8480 #ifdef EMULATE_GETDENTS_WITH_GETDENTS 8481 type = *((uint8_t *)hde + hreclen - 1); 8482 #else 8483 type = hde->d_type; 8484 #endif 8485 *((uint8_t *)tde + treclen - 1) = type; 8486 } 8487 8488 unlock_user(tdirp, arg2, toff); 8489 return toff; 8490 } 8491 #endif /* TARGET_NR_getdents */ 8492 8493 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) 8494 static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count) 8495 { 8496 g_autofree void *hdirp = NULL; 8497 void *tdirp; 8498 int hlen, hoff, toff; 8499 int hreclen, treclen; 8500 off64_t prev_diroff = 0; 8501 8502 hdirp = g_try_malloc(count); 8503 if (!hdirp) { 8504 return -TARGET_ENOMEM; 8505 } 8506 8507 hlen = get_errno(sys_getdents64(dirfd, hdirp, count)); 8508 if (is_error(hlen)) { 8509 return hlen; 8510 } 8511 8512 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0); 8513 if (!tdirp) { 8514 return -TARGET_EFAULT; 8515 } 8516 8517 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) { 8518 struct linux_dirent64 *hde = hdirp + hoff; 8519 struct target_dirent64 *tde = tdirp + toff; 8520 int namelen; 8521 8522 namelen = strlen(hde->d_name) + 1; 8523 hreclen = hde->d_reclen; 8524 treclen = offsetof(struct target_dirent64, d_name) + namelen; 8525 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64)); 8526 8527 if (toff + treclen > count) { 8528 /* 8529 * If the host struct is smaller than the target struct, or 8530 * requires less alignment and thus packs into less space, 8531 * then the host can return more entries than we can pass 8532 * on to the guest. 8533 */ 8534 if (toff == 0) { 8535 toff = -TARGET_EINVAL; /* result buffer is too small */ 8536 break; 8537 } 8538 /* 8539 * Return what we have, resetting the file pointer to the 8540 * location of the first record not returned. 8541 */ 8542 lseek64(dirfd, prev_diroff, SEEK_SET); 8543 break; 8544 } 8545 8546 prev_diroff = hde->d_off; 8547 tde->d_ino = tswap64(hde->d_ino); 8548 tde->d_off = tswap64(hde->d_off); 8549 tde->d_reclen = tswap16(treclen); 8550 tde->d_type = hde->d_type; 8551 memcpy(tde->d_name, hde->d_name, namelen); 8552 } 8553 8554 unlock_user(tdirp, arg2, toff); 8555 return toff; 8556 } 8557 #endif /* TARGET_NR_getdents64 */ 8558 8559 #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root) 8560 _syscall2(int, pivot_root, const char *, new_root, const char *, put_old) 8561 #endif 8562 8563 /* This is an internal helper for do_syscall so that it is easier 8564 * to have a single return point, so that actions, such as logging 8565 * of syscall results, can be performed. 8566 * All errnos that do_syscall() returns must be -TARGET_<errcode>. 8567 */ 8568 static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, 8569 abi_long arg2, abi_long arg3, abi_long arg4, 8570 abi_long arg5, abi_long arg6, abi_long arg7, 8571 abi_long arg8) 8572 { 8573 CPUState *cpu = env_cpu(cpu_env); 8574 abi_long ret; 8575 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \ 8576 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \ 8577 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \ 8578 || defined(TARGET_NR_statx) 8579 struct stat st; 8580 #endif 8581 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \ 8582 || defined(TARGET_NR_fstatfs) 8583 struct statfs stfs; 8584 #endif 8585 void *p; 8586 8587 switch(num) { 8588 case TARGET_NR_exit: 8589 /* In old applications this may be used to implement _exit(2). 8590 However in threaded applications it is used for thread termination, 8591 and _exit_group is used for application termination. 8592 Do thread termination if we have more then one thread. */ 8593 8594 if (block_signals()) { 8595 return -QEMU_ERESTARTSYS; 8596 } 8597 8598 pthread_mutex_lock(&clone_lock); 8599 8600 if (CPU_NEXT(first_cpu)) { 8601 TaskState *ts = cpu->opaque; 8602 8603 object_property_set_bool(OBJECT(cpu), "realized", false, NULL); 8604 object_unref(OBJECT(cpu)); 8605 /* 8606 * At this point the CPU should be unrealized and removed 8607 * from cpu lists. We can clean-up the rest of the thread 8608 * data without the lock held. 8609 */ 8610 8611 pthread_mutex_unlock(&clone_lock); 8612 8613 if (ts->child_tidptr) { 8614 put_user_u32(0, ts->child_tidptr); 8615 do_sys_futex(g2h(cpu, ts->child_tidptr), 8616 FUTEX_WAKE, INT_MAX, NULL, NULL, 0); 8617 } 8618 thread_cpu = NULL; 8619 g_free(ts); 8620 rcu_unregister_thread(); 8621 pthread_exit(NULL); 8622 } 8623 8624 pthread_mutex_unlock(&clone_lock); 8625 preexit_cleanup(cpu_env, arg1); 8626 _exit(arg1); 8627 return 0; /* avoid warning */ 8628 case TARGET_NR_read: 8629 if (arg2 == 0 && arg3 == 0) { 8630 return get_errno(safe_read(arg1, 0, 0)); 8631 } else { 8632 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0))) 8633 return -TARGET_EFAULT; 8634 ret = get_errno(safe_read(arg1, p, arg3)); 8635 if (ret >= 0 && 8636 fd_trans_host_to_target_data(arg1)) { 8637 ret = fd_trans_host_to_target_data(arg1)(p, ret); 8638 } 8639 unlock_user(p, arg2, ret); 8640 } 8641 return ret; 8642 case TARGET_NR_write: 8643 if (arg2 == 0 && arg3 == 0) { 8644 return get_errno(safe_write(arg1, 0, 0)); 8645 } 8646 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1))) 8647 return -TARGET_EFAULT; 8648 if (fd_trans_target_to_host_data(arg1)) { 8649 void *copy = g_malloc(arg3); 8650 memcpy(copy, p, arg3); 8651 ret = fd_trans_target_to_host_data(arg1)(copy, arg3); 8652 if (ret >= 0) { 8653 ret = get_errno(safe_write(arg1, copy, ret)); 8654 } 8655 g_free(copy); 8656 } else { 8657 ret = get_errno(safe_write(arg1, p, arg3)); 8658 } 8659 unlock_user(p, arg2, 0); 8660 return ret; 8661 8662 #ifdef TARGET_NR_open 8663 case TARGET_NR_open: 8664 if (!(p = lock_user_string(arg1))) 8665 return -TARGET_EFAULT; 8666 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p, 8667 target_to_host_bitmask(arg2, fcntl_flags_tbl), 8668 arg3)); 8669 fd_trans_unregister(ret); 8670 unlock_user(p, arg1, 0); 8671 return ret; 8672 #endif 8673 case TARGET_NR_openat: 8674 if (!(p = lock_user_string(arg2))) 8675 return -TARGET_EFAULT; 8676 ret = get_errno(do_openat(cpu_env, arg1, p, 8677 target_to_host_bitmask(arg3, fcntl_flags_tbl), 8678 arg4)); 8679 fd_trans_unregister(ret); 8680 unlock_user(p, arg2, 0); 8681 return ret; 8682 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) 8683 case TARGET_NR_name_to_handle_at: 8684 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5); 8685 return ret; 8686 #endif 8687 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE) 8688 case TARGET_NR_open_by_handle_at: 8689 ret = do_open_by_handle_at(arg1, arg2, arg3); 8690 fd_trans_unregister(ret); 8691 return ret; 8692 #endif 8693 case TARGET_NR_close: 8694 fd_trans_unregister(arg1); 8695 return get_errno(close(arg1)); 8696 8697 case TARGET_NR_brk: 8698 return do_brk(arg1); 8699 #ifdef TARGET_NR_fork 8700 case TARGET_NR_fork: 8701 return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0)); 8702 #endif 8703 #ifdef TARGET_NR_waitpid 8704 case TARGET_NR_waitpid: 8705 { 8706 int status; 8707 ret = get_errno(safe_wait4(arg1, &status, arg3, 0)); 8708 if (!is_error(ret) && arg2 && ret 8709 && put_user_s32(host_to_target_waitstatus(status), arg2)) 8710 return -TARGET_EFAULT; 8711 } 8712 return ret; 8713 #endif 8714 #ifdef TARGET_NR_waitid 8715 case TARGET_NR_waitid: 8716 { 8717 siginfo_t info; 8718 info.si_pid = 0; 8719 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL)); 8720 if (!is_error(ret) && arg3 && info.si_pid != 0) { 8721 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0))) 8722 return -TARGET_EFAULT; 8723 host_to_target_siginfo(p, &info); 8724 unlock_user(p, arg3, sizeof(target_siginfo_t)); 8725 } 8726 } 8727 return ret; 8728 #endif 8729 #ifdef TARGET_NR_creat /* not on alpha */ 8730 case TARGET_NR_creat: 8731 if (!(p = lock_user_string(arg1))) 8732 return -TARGET_EFAULT; 8733 ret = get_errno(creat(p, arg2)); 8734 fd_trans_unregister(ret); 8735 unlock_user(p, arg1, 0); 8736 return ret; 8737 #endif 8738 #ifdef TARGET_NR_link 8739 case TARGET_NR_link: 8740 { 8741 void * p2; 8742 p = lock_user_string(arg1); 8743 p2 = lock_user_string(arg2); 8744 if (!p || !p2) 8745 ret = -TARGET_EFAULT; 8746 else 8747 ret = get_errno(link(p, p2)); 8748 unlock_user(p2, arg2, 0); 8749 unlock_user(p, arg1, 0); 8750 } 8751 return ret; 8752 #endif 8753 #if defined(TARGET_NR_linkat) 8754 case TARGET_NR_linkat: 8755 { 8756 void * p2 = NULL; 8757 if (!arg2 || !arg4) 8758 return -TARGET_EFAULT; 8759 p = lock_user_string(arg2); 8760 p2 = lock_user_string(arg4); 8761 if (!p || !p2) 8762 ret = -TARGET_EFAULT; 8763 else 8764 ret = get_errno(linkat(arg1, p, arg3, p2, arg5)); 8765 unlock_user(p, arg2, 0); 8766 unlock_user(p2, arg4, 0); 8767 } 8768 return ret; 8769 #endif 8770 #ifdef TARGET_NR_unlink 8771 case TARGET_NR_unlink: 8772 if (!(p = lock_user_string(arg1))) 8773 return -TARGET_EFAULT; 8774 ret = get_errno(unlink(p)); 8775 unlock_user(p, arg1, 0); 8776 return ret; 8777 #endif 8778 #if defined(TARGET_NR_unlinkat) 8779 case TARGET_NR_unlinkat: 8780 if (!(p = lock_user_string(arg2))) 8781 return -TARGET_EFAULT; 8782 ret = get_errno(unlinkat(arg1, p, arg3)); 8783 unlock_user(p, arg2, 0); 8784 return ret; 8785 #endif 8786 case TARGET_NR_execve: 8787 { 8788 char **argp, **envp; 8789 int argc, envc; 8790 abi_ulong gp; 8791 abi_ulong guest_argp; 8792 abi_ulong guest_envp; 8793 abi_ulong addr; 8794 char **q; 8795 8796 argc = 0; 8797 guest_argp = arg2; 8798 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) { 8799 if (get_user_ual(addr, gp)) 8800 return -TARGET_EFAULT; 8801 if (!addr) 8802 break; 8803 argc++; 8804 } 8805 envc = 0; 8806 guest_envp = arg3; 8807 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) { 8808 if (get_user_ual(addr, gp)) 8809 return -TARGET_EFAULT; 8810 if (!addr) 8811 break; 8812 envc++; 8813 } 8814 8815 argp = g_new0(char *, argc + 1); 8816 envp = g_new0(char *, envc + 1); 8817 8818 for (gp = guest_argp, q = argp; gp; 8819 gp += sizeof(abi_ulong), q++) { 8820 if (get_user_ual(addr, gp)) 8821 goto execve_efault; 8822 if (!addr) 8823 break; 8824 if (!(*q = lock_user_string(addr))) 8825 goto execve_efault; 8826 } 8827 *q = NULL; 8828 8829 for (gp = guest_envp, q = envp; gp; 8830 gp += sizeof(abi_ulong), q++) { 8831 if (get_user_ual(addr, gp)) 8832 goto execve_efault; 8833 if (!addr) 8834 break; 8835 if (!(*q = lock_user_string(addr))) 8836 goto execve_efault; 8837 } 8838 *q = NULL; 8839 8840 if (!(p = lock_user_string(arg1))) 8841 goto execve_efault; 8842 /* Although execve() is not an interruptible syscall it is 8843 * a special case where we must use the safe_syscall wrapper: 8844 * if we allow a signal to happen before we make the host 8845 * syscall then we will 'lose' it, because at the point of 8846 * execve the process leaves QEMU's control. So we use the 8847 * safe syscall wrapper to ensure that we either take the 8848 * signal as a guest signal, or else it does not happen 8849 * before the execve completes and makes it the other 8850 * program's problem. 8851 */ 8852 ret = get_errno(safe_execve(p, argp, envp)); 8853 unlock_user(p, arg1, 0); 8854 8855 goto execve_end; 8856 8857 execve_efault: 8858 ret = -TARGET_EFAULT; 8859 8860 execve_end: 8861 for (gp = guest_argp, q = argp; *q; 8862 gp += sizeof(abi_ulong), q++) { 8863 if (get_user_ual(addr, gp) 8864 || !addr) 8865 break; 8866 unlock_user(*q, addr, 0); 8867 } 8868 for (gp = guest_envp, q = envp; *q; 8869 gp += sizeof(abi_ulong), q++) { 8870 if (get_user_ual(addr, gp) 8871 || !addr) 8872 break; 8873 unlock_user(*q, addr, 0); 8874 } 8875 8876 g_free(argp); 8877 g_free(envp); 8878 } 8879 return ret; 8880 case TARGET_NR_chdir: 8881 if (!(p = lock_user_string(arg1))) 8882 return -TARGET_EFAULT; 8883 ret = get_errno(chdir(p)); 8884 unlock_user(p, arg1, 0); 8885 return ret; 8886 #ifdef TARGET_NR_time 8887 case TARGET_NR_time: 8888 { 8889 time_t host_time; 8890 ret = get_errno(time(&host_time)); 8891 if (!is_error(ret) 8892 && arg1 8893 && put_user_sal(host_time, arg1)) 8894 return -TARGET_EFAULT; 8895 } 8896 return ret; 8897 #endif 8898 #ifdef TARGET_NR_mknod 8899 case TARGET_NR_mknod: 8900 if (!(p = lock_user_string(arg1))) 8901 return -TARGET_EFAULT; 8902 ret = get_errno(mknod(p, arg2, arg3)); 8903 unlock_user(p, arg1, 0); 8904 return ret; 8905 #endif 8906 #if defined(TARGET_NR_mknodat) 8907 case TARGET_NR_mknodat: 8908 if (!(p = lock_user_string(arg2))) 8909 return -TARGET_EFAULT; 8910 ret = get_errno(mknodat(arg1, p, arg3, arg4)); 8911 unlock_user(p, arg2, 0); 8912 return ret; 8913 #endif 8914 #ifdef TARGET_NR_chmod 8915 case TARGET_NR_chmod: 8916 if (!(p = lock_user_string(arg1))) 8917 return -TARGET_EFAULT; 8918 ret = get_errno(chmod(p, arg2)); 8919 unlock_user(p, arg1, 0); 8920 return ret; 8921 #endif 8922 #ifdef TARGET_NR_lseek 8923 case TARGET_NR_lseek: 8924 return get_errno(lseek(arg1, arg2, arg3)); 8925 #endif 8926 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA) 8927 /* Alpha specific */ 8928 case TARGET_NR_getxpid: 8929 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid(); 8930 return get_errno(getpid()); 8931 #endif 8932 #ifdef TARGET_NR_getpid 8933 case TARGET_NR_getpid: 8934 return get_errno(getpid()); 8935 #endif 8936 case TARGET_NR_mount: 8937 { 8938 /* need to look at the data field */ 8939 void *p2, *p3; 8940 8941 if (arg1) { 8942 p = lock_user_string(arg1); 8943 if (!p) { 8944 return -TARGET_EFAULT; 8945 } 8946 } else { 8947 p = NULL; 8948 } 8949 8950 p2 = lock_user_string(arg2); 8951 if (!p2) { 8952 if (arg1) { 8953 unlock_user(p, arg1, 0); 8954 } 8955 return -TARGET_EFAULT; 8956 } 8957 8958 if (arg3) { 8959 p3 = lock_user_string(arg3); 8960 if (!p3) { 8961 if (arg1) { 8962 unlock_user(p, arg1, 0); 8963 } 8964 unlock_user(p2, arg2, 0); 8965 return -TARGET_EFAULT; 8966 } 8967 } else { 8968 p3 = NULL; 8969 } 8970 8971 /* FIXME - arg5 should be locked, but it isn't clear how to 8972 * do that since it's not guaranteed to be a NULL-terminated 8973 * string. 8974 */ 8975 if (!arg5) { 8976 ret = mount(p, p2, p3, (unsigned long)arg4, NULL); 8977 } else { 8978 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5)); 8979 } 8980 ret = get_errno(ret); 8981 8982 if (arg1) { 8983 unlock_user(p, arg1, 0); 8984 } 8985 unlock_user(p2, arg2, 0); 8986 if (arg3) { 8987 unlock_user(p3, arg3, 0); 8988 } 8989 } 8990 return ret; 8991 #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount) 8992 #if defined(TARGET_NR_umount) 8993 case TARGET_NR_umount: 8994 #endif 8995 #if defined(TARGET_NR_oldumount) 8996 case TARGET_NR_oldumount: 8997 #endif 8998 if (!(p = lock_user_string(arg1))) 8999 return -TARGET_EFAULT; 9000 ret = get_errno(umount(p)); 9001 unlock_user(p, arg1, 0); 9002 return ret; 9003 #endif 9004 #ifdef TARGET_NR_stime /* not on alpha */ 9005 case TARGET_NR_stime: 9006 { 9007 struct timespec ts; 9008 ts.tv_nsec = 0; 9009 if (get_user_sal(ts.tv_sec, arg1)) { 9010 return -TARGET_EFAULT; 9011 } 9012 return get_errno(clock_settime(CLOCK_REALTIME, &ts)); 9013 } 9014 #endif 9015 #ifdef TARGET_NR_alarm /* not on alpha */ 9016 case TARGET_NR_alarm: 9017 return alarm(arg1); 9018 #endif 9019 #ifdef TARGET_NR_pause /* not on alpha */ 9020 case TARGET_NR_pause: 9021 if (!block_signals()) { 9022 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask); 9023 } 9024 return -TARGET_EINTR; 9025 #endif 9026 #ifdef TARGET_NR_utime 9027 case TARGET_NR_utime: 9028 { 9029 struct utimbuf tbuf, *host_tbuf; 9030 struct target_utimbuf *target_tbuf; 9031 if (arg2) { 9032 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1)) 9033 return -TARGET_EFAULT; 9034 tbuf.actime = tswapal(target_tbuf->actime); 9035 tbuf.modtime = tswapal(target_tbuf->modtime); 9036 unlock_user_struct(target_tbuf, arg2, 0); 9037 host_tbuf = &tbuf; 9038 } else { 9039 host_tbuf = NULL; 9040 } 9041 if (!(p = lock_user_string(arg1))) 9042 return -TARGET_EFAULT; 9043 ret = get_errno(utime(p, host_tbuf)); 9044 unlock_user(p, arg1, 0); 9045 } 9046 return ret; 9047 #endif 9048 #ifdef TARGET_NR_utimes 9049 case TARGET_NR_utimes: 9050 { 9051 struct timeval *tvp, tv[2]; 9052 if (arg2) { 9053 if (copy_from_user_timeval(&tv[0], arg2) 9054 || copy_from_user_timeval(&tv[1], 9055 arg2 + sizeof(struct target_timeval))) 9056 return -TARGET_EFAULT; 9057 tvp = tv; 9058 } else { 9059 tvp = NULL; 9060 } 9061 if (!(p = lock_user_string(arg1))) 9062 return -TARGET_EFAULT; 9063 ret = get_errno(utimes(p, tvp)); 9064 unlock_user(p, arg1, 0); 9065 } 9066 return ret; 9067 #endif 9068 #if defined(TARGET_NR_futimesat) 9069 case TARGET_NR_futimesat: 9070 { 9071 struct timeval *tvp, tv[2]; 9072 if (arg3) { 9073 if (copy_from_user_timeval(&tv[0], arg3) 9074 || copy_from_user_timeval(&tv[1], 9075 arg3 + sizeof(struct target_timeval))) 9076 return -TARGET_EFAULT; 9077 tvp = tv; 9078 } else { 9079 tvp = NULL; 9080 } 9081 if (!(p = lock_user_string(arg2))) { 9082 return -TARGET_EFAULT; 9083 } 9084 ret = get_errno(futimesat(arg1, path(p), tvp)); 9085 unlock_user(p, arg2, 0); 9086 } 9087 return ret; 9088 #endif 9089 #ifdef TARGET_NR_access 9090 case TARGET_NR_access: 9091 if (!(p = lock_user_string(arg1))) { 9092 return -TARGET_EFAULT; 9093 } 9094 ret = get_errno(access(path(p), arg2)); 9095 unlock_user(p, arg1, 0); 9096 return ret; 9097 #endif 9098 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) 9099 case TARGET_NR_faccessat: 9100 if (!(p = lock_user_string(arg2))) { 9101 return -TARGET_EFAULT; 9102 } 9103 ret = get_errno(faccessat(arg1, p, arg3, 0)); 9104 unlock_user(p, arg2, 0); 9105 return ret; 9106 #endif 9107 #ifdef TARGET_NR_nice /* not on alpha */ 9108 case TARGET_NR_nice: 9109 return get_errno(nice(arg1)); 9110 #endif 9111 case TARGET_NR_sync: 9112 sync(); 9113 return 0; 9114 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS) 9115 case TARGET_NR_syncfs: 9116 return get_errno(syncfs(arg1)); 9117 #endif 9118 case TARGET_NR_kill: 9119 return get_errno(safe_kill(arg1, target_to_host_signal(arg2))); 9120 #ifdef TARGET_NR_rename 9121 case TARGET_NR_rename: 9122 { 9123 void *p2; 9124 p = lock_user_string(arg1); 9125 p2 = lock_user_string(arg2); 9126 if (!p || !p2) 9127 ret = -TARGET_EFAULT; 9128 else 9129 ret = get_errno(rename(p, p2)); 9130 unlock_user(p2, arg2, 0); 9131 unlock_user(p, arg1, 0); 9132 } 9133 return ret; 9134 #endif 9135 #if defined(TARGET_NR_renameat) 9136 case TARGET_NR_renameat: 9137 { 9138 void *p2; 9139 p = lock_user_string(arg2); 9140 p2 = lock_user_string(arg4); 9141 if (!p || !p2) 9142 ret = -TARGET_EFAULT; 9143 else 9144 ret = get_errno(renameat(arg1, p, arg3, p2)); 9145 unlock_user(p2, arg4, 0); 9146 unlock_user(p, arg2, 0); 9147 } 9148 return ret; 9149 #endif 9150 #if defined(TARGET_NR_renameat2) 9151 case TARGET_NR_renameat2: 9152 { 9153 void *p2; 9154 p = lock_user_string(arg2); 9155 p2 = lock_user_string(arg4); 9156 if (!p || !p2) { 9157 ret = -TARGET_EFAULT; 9158 } else { 9159 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5)); 9160 } 9161 unlock_user(p2, arg4, 0); 9162 unlock_user(p, arg2, 0); 9163 } 9164 return ret; 9165 #endif 9166 #ifdef TARGET_NR_mkdir 9167 case TARGET_NR_mkdir: 9168 if (!(p = lock_user_string(arg1))) 9169 return -TARGET_EFAULT; 9170 ret = get_errno(mkdir(p, arg2)); 9171 unlock_user(p, arg1, 0); 9172 return ret; 9173 #endif 9174 #if defined(TARGET_NR_mkdirat) 9175 case TARGET_NR_mkdirat: 9176 if (!(p = lock_user_string(arg2))) 9177 return -TARGET_EFAULT; 9178 ret = get_errno(mkdirat(arg1, p, arg3)); 9179 unlock_user(p, arg2, 0); 9180 return ret; 9181 #endif 9182 #ifdef TARGET_NR_rmdir 9183 case TARGET_NR_rmdir: 9184 if (!(p = lock_user_string(arg1))) 9185 return -TARGET_EFAULT; 9186 ret = get_errno(rmdir(p)); 9187 unlock_user(p, arg1, 0); 9188 return ret; 9189 #endif 9190 case TARGET_NR_dup: 9191 ret = get_errno(dup(arg1)); 9192 if (ret >= 0) { 9193 fd_trans_dup(arg1, ret); 9194 } 9195 return ret; 9196 #ifdef TARGET_NR_pipe 9197 case TARGET_NR_pipe: 9198 return do_pipe(cpu_env, arg1, 0, 0); 9199 #endif 9200 #ifdef TARGET_NR_pipe2 9201 case TARGET_NR_pipe2: 9202 return do_pipe(cpu_env, arg1, 9203 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1); 9204 #endif 9205 case TARGET_NR_times: 9206 { 9207 struct target_tms *tmsp; 9208 struct tms tms; 9209 ret = get_errno(times(&tms)); 9210 if (arg1) { 9211 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0); 9212 if (!tmsp) 9213 return -TARGET_EFAULT; 9214 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime)); 9215 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime)); 9216 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime)); 9217 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime)); 9218 } 9219 if (!is_error(ret)) 9220 ret = host_to_target_clock_t(ret); 9221 } 9222 return ret; 9223 case TARGET_NR_acct: 9224 if (arg1 == 0) { 9225 ret = get_errno(acct(NULL)); 9226 } else { 9227 if (!(p = lock_user_string(arg1))) { 9228 return -TARGET_EFAULT; 9229 } 9230 ret = get_errno(acct(path(p))); 9231 unlock_user(p, arg1, 0); 9232 } 9233 return ret; 9234 #ifdef TARGET_NR_umount2 9235 case TARGET_NR_umount2: 9236 if (!(p = lock_user_string(arg1))) 9237 return -TARGET_EFAULT; 9238 ret = get_errno(umount2(p, arg2)); 9239 unlock_user(p, arg1, 0); 9240 return ret; 9241 #endif 9242 case TARGET_NR_ioctl: 9243 return do_ioctl(arg1, arg2, arg3); 9244 #ifdef TARGET_NR_fcntl 9245 case TARGET_NR_fcntl: 9246 return do_fcntl(arg1, arg2, arg3); 9247 #endif 9248 case TARGET_NR_setpgid: 9249 return get_errno(setpgid(arg1, arg2)); 9250 case TARGET_NR_umask: 9251 return get_errno(umask(arg1)); 9252 case TARGET_NR_chroot: 9253 if (!(p = lock_user_string(arg1))) 9254 return -TARGET_EFAULT; 9255 ret = get_errno(chroot(p)); 9256 unlock_user(p, arg1, 0); 9257 return ret; 9258 #ifdef TARGET_NR_dup2 9259 case TARGET_NR_dup2: 9260 ret = get_errno(dup2(arg1, arg2)); 9261 if (ret >= 0) { 9262 fd_trans_dup(arg1, arg2); 9263 } 9264 return ret; 9265 #endif 9266 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3) 9267 case TARGET_NR_dup3: 9268 { 9269 int host_flags; 9270 9271 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) { 9272 return -EINVAL; 9273 } 9274 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl); 9275 ret = get_errno(dup3(arg1, arg2, host_flags)); 9276 if (ret >= 0) { 9277 fd_trans_dup(arg1, arg2); 9278 } 9279 return ret; 9280 } 9281 #endif 9282 #ifdef TARGET_NR_getppid /* not on alpha */ 9283 case TARGET_NR_getppid: 9284 return get_errno(getppid()); 9285 #endif 9286 #ifdef TARGET_NR_getpgrp 9287 case TARGET_NR_getpgrp: 9288 return get_errno(getpgrp()); 9289 #endif 9290 case TARGET_NR_setsid: 9291 return get_errno(setsid()); 9292 #ifdef TARGET_NR_sigaction 9293 case TARGET_NR_sigaction: 9294 { 9295 #if defined(TARGET_MIPS) 9296 struct target_sigaction act, oact, *pact, *old_act; 9297 9298 if (arg2) { 9299 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) 9300 return -TARGET_EFAULT; 9301 act._sa_handler = old_act->_sa_handler; 9302 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]); 9303 act.sa_flags = old_act->sa_flags; 9304 unlock_user_struct(old_act, arg2, 0); 9305 pact = &act; 9306 } else { 9307 pact = NULL; 9308 } 9309 9310 ret = get_errno(do_sigaction(arg1, pact, &oact, 0)); 9311 9312 if (!is_error(ret) && arg3) { 9313 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) 9314 return -TARGET_EFAULT; 9315 old_act->_sa_handler = oact._sa_handler; 9316 old_act->sa_flags = oact.sa_flags; 9317 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0]; 9318 old_act->sa_mask.sig[1] = 0; 9319 old_act->sa_mask.sig[2] = 0; 9320 old_act->sa_mask.sig[3] = 0; 9321 unlock_user_struct(old_act, arg3, 1); 9322 } 9323 #else 9324 struct target_old_sigaction *old_act; 9325 struct target_sigaction act, oact, *pact; 9326 if (arg2) { 9327 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) 9328 return -TARGET_EFAULT; 9329 act._sa_handler = old_act->_sa_handler; 9330 target_siginitset(&act.sa_mask, old_act->sa_mask); 9331 act.sa_flags = old_act->sa_flags; 9332 #ifdef TARGET_ARCH_HAS_SA_RESTORER 9333 act.sa_restorer = old_act->sa_restorer; 9334 #endif 9335 unlock_user_struct(old_act, arg2, 0); 9336 pact = &act; 9337 } else { 9338 pact = NULL; 9339 } 9340 ret = get_errno(do_sigaction(arg1, pact, &oact, 0)); 9341 if (!is_error(ret) && arg3) { 9342 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) 9343 return -TARGET_EFAULT; 9344 old_act->_sa_handler = oact._sa_handler; 9345 old_act->sa_mask = oact.sa_mask.sig[0]; 9346 old_act->sa_flags = oact.sa_flags; 9347 #ifdef TARGET_ARCH_HAS_SA_RESTORER 9348 old_act->sa_restorer = oact.sa_restorer; 9349 #endif 9350 unlock_user_struct(old_act, arg3, 1); 9351 } 9352 #endif 9353 } 9354 return ret; 9355 #endif 9356 case TARGET_NR_rt_sigaction: 9357 { 9358 /* 9359 * For Alpha and SPARC this is a 5 argument syscall, with 9360 * a 'restorer' parameter which must be copied into the 9361 * sa_restorer field of the sigaction struct. 9362 * For Alpha that 'restorer' is arg5; for SPARC it is arg4, 9363 * and arg5 is the sigsetsize. 9364 */ 9365 #if defined(TARGET_ALPHA) 9366 target_ulong sigsetsize = arg4; 9367 target_ulong restorer = arg5; 9368 #elif defined(TARGET_SPARC) 9369 target_ulong restorer = arg4; 9370 target_ulong sigsetsize = arg5; 9371 #else 9372 target_ulong sigsetsize = arg4; 9373 target_ulong restorer = 0; 9374 #endif 9375 struct target_sigaction *act = NULL; 9376 struct target_sigaction *oact = NULL; 9377 9378 if (sigsetsize != sizeof(target_sigset_t)) { 9379 return -TARGET_EINVAL; 9380 } 9381 if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) { 9382 return -TARGET_EFAULT; 9383 } 9384 if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) { 9385 ret = -TARGET_EFAULT; 9386 } else { 9387 ret = get_errno(do_sigaction(arg1, act, oact, restorer)); 9388 if (oact) { 9389 unlock_user_struct(oact, arg3, 1); 9390 } 9391 } 9392 if (act) { 9393 unlock_user_struct(act, arg2, 0); 9394 } 9395 } 9396 return ret; 9397 #ifdef TARGET_NR_sgetmask /* not on alpha */ 9398 case TARGET_NR_sgetmask: 9399 { 9400 sigset_t cur_set; 9401 abi_ulong target_set; 9402 ret = do_sigprocmask(0, NULL, &cur_set); 9403 if (!ret) { 9404 host_to_target_old_sigset(&target_set, &cur_set); 9405 ret = target_set; 9406 } 9407 } 9408 return ret; 9409 #endif 9410 #ifdef TARGET_NR_ssetmask /* not on alpha */ 9411 case TARGET_NR_ssetmask: 9412 { 9413 sigset_t set, oset; 9414 abi_ulong target_set = arg1; 9415 target_to_host_old_sigset(&set, &target_set); 9416 ret = do_sigprocmask(SIG_SETMASK, &set, &oset); 9417 if (!ret) { 9418 host_to_target_old_sigset(&target_set, &oset); 9419 ret = target_set; 9420 } 9421 } 9422 return ret; 9423 #endif 9424 #ifdef TARGET_NR_sigprocmask 9425 case TARGET_NR_sigprocmask: 9426 { 9427 #if defined(TARGET_ALPHA) 9428 sigset_t set, oldset; 9429 abi_ulong mask; 9430 int how; 9431 9432 switch (arg1) { 9433 case TARGET_SIG_BLOCK: 9434 how = SIG_BLOCK; 9435 break; 9436 case TARGET_SIG_UNBLOCK: 9437 how = SIG_UNBLOCK; 9438 break; 9439 case TARGET_SIG_SETMASK: 9440 how = SIG_SETMASK; 9441 break; 9442 default: 9443 return -TARGET_EINVAL; 9444 } 9445 mask = arg2; 9446 target_to_host_old_sigset(&set, &mask); 9447 9448 ret = do_sigprocmask(how, &set, &oldset); 9449 if (!is_error(ret)) { 9450 host_to_target_old_sigset(&mask, &oldset); 9451 ret = mask; 9452 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */ 9453 } 9454 #else 9455 sigset_t set, oldset, *set_ptr; 9456 int how; 9457 9458 if (arg2) { 9459 switch (arg1) { 9460 case TARGET_SIG_BLOCK: 9461 how = SIG_BLOCK; 9462 break; 9463 case TARGET_SIG_UNBLOCK: 9464 how = SIG_UNBLOCK; 9465 break; 9466 case TARGET_SIG_SETMASK: 9467 how = SIG_SETMASK; 9468 break; 9469 default: 9470 return -TARGET_EINVAL; 9471 } 9472 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) 9473 return -TARGET_EFAULT; 9474 target_to_host_old_sigset(&set, p); 9475 unlock_user(p, arg2, 0); 9476 set_ptr = &set; 9477 } else { 9478 how = 0; 9479 set_ptr = NULL; 9480 } 9481 ret = do_sigprocmask(how, set_ptr, &oldset); 9482 if (!is_error(ret) && arg3) { 9483 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) 9484 return -TARGET_EFAULT; 9485 host_to_target_old_sigset(p, &oldset); 9486 unlock_user(p, arg3, sizeof(target_sigset_t)); 9487 } 9488 #endif 9489 } 9490 return ret; 9491 #endif 9492 case TARGET_NR_rt_sigprocmask: 9493 { 9494 int how = arg1; 9495 sigset_t set, oldset, *set_ptr; 9496 9497 if (arg4 != sizeof(target_sigset_t)) { 9498 return -TARGET_EINVAL; 9499 } 9500 9501 if (arg2) { 9502 switch(how) { 9503 case TARGET_SIG_BLOCK: 9504 how = SIG_BLOCK; 9505 break; 9506 case TARGET_SIG_UNBLOCK: 9507 how = SIG_UNBLOCK; 9508 break; 9509 case TARGET_SIG_SETMASK: 9510 how = SIG_SETMASK; 9511 break; 9512 default: 9513 return -TARGET_EINVAL; 9514 } 9515 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1))) 9516 return -TARGET_EFAULT; 9517 target_to_host_sigset(&set, p); 9518 unlock_user(p, arg2, 0); 9519 set_ptr = &set; 9520 } else { 9521 how = 0; 9522 set_ptr = NULL; 9523 } 9524 ret = do_sigprocmask(how, set_ptr, &oldset); 9525 if (!is_error(ret) && arg3) { 9526 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0))) 9527 return -TARGET_EFAULT; 9528 host_to_target_sigset(p, &oldset); 9529 unlock_user(p, arg3, sizeof(target_sigset_t)); 9530 } 9531 } 9532 return ret; 9533 #ifdef TARGET_NR_sigpending 9534 case TARGET_NR_sigpending: 9535 { 9536 sigset_t set; 9537 ret = get_errno(sigpending(&set)); 9538 if (!is_error(ret)) { 9539 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) 9540 return -TARGET_EFAULT; 9541 host_to_target_old_sigset(p, &set); 9542 unlock_user(p, arg1, sizeof(target_sigset_t)); 9543 } 9544 } 9545 return ret; 9546 #endif 9547 case TARGET_NR_rt_sigpending: 9548 { 9549 sigset_t set; 9550 9551 /* Yes, this check is >, not != like most. We follow the kernel's 9552 * logic and it does it like this because it implements 9553 * NR_sigpending through the same code path, and in that case 9554 * the old_sigset_t is smaller in size. 9555 */ 9556 if (arg2 > sizeof(target_sigset_t)) { 9557 return -TARGET_EINVAL; 9558 } 9559 9560 ret = get_errno(sigpending(&set)); 9561 if (!is_error(ret)) { 9562 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0))) 9563 return -TARGET_EFAULT; 9564 host_to_target_sigset(p, &set); 9565 unlock_user(p, arg1, sizeof(target_sigset_t)); 9566 } 9567 } 9568 return ret; 9569 #ifdef TARGET_NR_sigsuspend 9570 case TARGET_NR_sigsuspend: 9571 { 9572 TaskState *ts = cpu->opaque; 9573 #if defined(TARGET_ALPHA) 9574 abi_ulong mask = arg1; 9575 target_to_host_old_sigset(&ts->sigsuspend_mask, &mask); 9576 #else 9577 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) 9578 return -TARGET_EFAULT; 9579 target_to_host_old_sigset(&ts->sigsuspend_mask, p); 9580 unlock_user(p, arg1, 0); 9581 #endif 9582 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask, 9583 SIGSET_T_SIZE)); 9584 if (ret != -QEMU_ERESTARTSYS) { 9585 ts->in_sigsuspend = 1; 9586 } 9587 } 9588 return ret; 9589 #endif 9590 case TARGET_NR_rt_sigsuspend: 9591 { 9592 TaskState *ts = cpu->opaque; 9593 9594 if (arg2 != sizeof(target_sigset_t)) { 9595 return -TARGET_EINVAL; 9596 } 9597 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) 9598 return -TARGET_EFAULT; 9599 target_to_host_sigset(&ts->sigsuspend_mask, p); 9600 unlock_user(p, arg1, 0); 9601 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask, 9602 SIGSET_T_SIZE)); 9603 if (ret != -QEMU_ERESTARTSYS) { 9604 ts->in_sigsuspend = 1; 9605 } 9606 } 9607 return ret; 9608 #ifdef TARGET_NR_rt_sigtimedwait 9609 case TARGET_NR_rt_sigtimedwait: 9610 { 9611 sigset_t set; 9612 struct timespec uts, *puts; 9613 siginfo_t uinfo; 9614 9615 if (arg4 != sizeof(target_sigset_t)) { 9616 return -TARGET_EINVAL; 9617 } 9618 9619 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1))) 9620 return -TARGET_EFAULT; 9621 target_to_host_sigset(&set, p); 9622 unlock_user(p, arg1, 0); 9623 if (arg3) { 9624 puts = &uts; 9625 if (target_to_host_timespec(puts, arg3)) { 9626 return -TARGET_EFAULT; 9627 } 9628 } else { 9629 puts = NULL; 9630 } 9631 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts, 9632 SIGSET_T_SIZE)); 9633 if (!is_error(ret)) { 9634 if (arg2) { 9635 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 9636 0); 9637 if (!p) { 9638 return -TARGET_EFAULT; 9639 } 9640 host_to_target_siginfo(p, &uinfo); 9641 unlock_user(p, arg2, sizeof(target_siginfo_t)); 9642 } 9643 ret = host_to_target_signal(ret); 9644 } 9645 } 9646 return ret; 9647 #endif 9648 #ifdef TARGET_NR_rt_sigtimedwait_time64 9649 case TARGET_NR_rt_sigtimedwait_time64: 9650 { 9651 sigset_t set; 9652 struct timespec uts, *puts; 9653 siginfo_t uinfo; 9654 9655 if (arg4 != sizeof(target_sigset_t)) { 9656 return -TARGET_EINVAL; 9657 } 9658 9659 p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1); 9660 if (!p) { 9661 return -TARGET_EFAULT; 9662 } 9663 target_to_host_sigset(&set, p); 9664 unlock_user(p, arg1, 0); 9665 if (arg3) { 9666 puts = &uts; 9667 if (target_to_host_timespec64(puts, arg3)) { 9668 return -TARGET_EFAULT; 9669 } 9670 } else { 9671 puts = NULL; 9672 } 9673 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts, 9674 SIGSET_T_SIZE)); 9675 if (!is_error(ret)) { 9676 if (arg2) { 9677 p = lock_user(VERIFY_WRITE, arg2, 9678 sizeof(target_siginfo_t), 0); 9679 if (!p) { 9680 return -TARGET_EFAULT; 9681 } 9682 host_to_target_siginfo(p, &uinfo); 9683 unlock_user(p, arg2, sizeof(target_siginfo_t)); 9684 } 9685 ret = host_to_target_signal(ret); 9686 } 9687 } 9688 return ret; 9689 #endif 9690 case TARGET_NR_rt_sigqueueinfo: 9691 { 9692 siginfo_t uinfo; 9693 9694 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1); 9695 if (!p) { 9696 return -TARGET_EFAULT; 9697 } 9698 target_to_host_siginfo(&uinfo, p); 9699 unlock_user(p, arg3, 0); 9700 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo)); 9701 } 9702 return ret; 9703 case TARGET_NR_rt_tgsigqueueinfo: 9704 { 9705 siginfo_t uinfo; 9706 9707 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1); 9708 if (!p) { 9709 return -TARGET_EFAULT; 9710 } 9711 target_to_host_siginfo(&uinfo, p); 9712 unlock_user(p, arg4, 0); 9713 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo)); 9714 } 9715 return ret; 9716 #ifdef TARGET_NR_sigreturn 9717 case TARGET_NR_sigreturn: 9718 if (block_signals()) { 9719 return -QEMU_ERESTARTSYS; 9720 } 9721 return do_sigreturn(cpu_env); 9722 #endif 9723 case TARGET_NR_rt_sigreturn: 9724 if (block_signals()) { 9725 return -QEMU_ERESTARTSYS; 9726 } 9727 return do_rt_sigreturn(cpu_env); 9728 case TARGET_NR_sethostname: 9729 if (!(p = lock_user_string(arg1))) 9730 return -TARGET_EFAULT; 9731 ret = get_errno(sethostname(p, arg2)); 9732 unlock_user(p, arg1, 0); 9733 return ret; 9734 #ifdef TARGET_NR_setrlimit 9735 case TARGET_NR_setrlimit: 9736 { 9737 int resource = target_to_host_resource(arg1); 9738 struct target_rlimit *target_rlim; 9739 struct rlimit rlim; 9740 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) 9741 return -TARGET_EFAULT; 9742 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur); 9743 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max); 9744 unlock_user_struct(target_rlim, arg2, 0); 9745 /* 9746 * If we just passed through resource limit settings for memory then 9747 * they would also apply to QEMU's own allocations, and QEMU will 9748 * crash or hang or die if its allocations fail. Ideally we would 9749 * track the guest allocations in QEMU and apply the limits ourselves. 9750 * For now, just tell the guest the call succeeded but don't actually 9751 * limit anything. 9752 */ 9753 if (resource != RLIMIT_AS && 9754 resource != RLIMIT_DATA && 9755 resource != RLIMIT_STACK) { 9756 return get_errno(setrlimit(resource, &rlim)); 9757 } else { 9758 return 0; 9759 } 9760 } 9761 #endif 9762 #ifdef TARGET_NR_getrlimit 9763 case TARGET_NR_getrlimit: 9764 { 9765 int resource = target_to_host_resource(arg1); 9766 struct target_rlimit *target_rlim; 9767 struct rlimit rlim; 9768 9769 ret = get_errno(getrlimit(resource, &rlim)); 9770 if (!is_error(ret)) { 9771 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) 9772 return -TARGET_EFAULT; 9773 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); 9774 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); 9775 unlock_user_struct(target_rlim, arg2, 1); 9776 } 9777 } 9778 return ret; 9779 #endif 9780 case TARGET_NR_getrusage: 9781 { 9782 struct rusage rusage; 9783 ret = get_errno(getrusage(arg1, &rusage)); 9784 if (!is_error(ret)) { 9785 ret = host_to_target_rusage(arg2, &rusage); 9786 } 9787 } 9788 return ret; 9789 #if defined(TARGET_NR_gettimeofday) 9790 case TARGET_NR_gettimeofday: 9791 { 9792 struct timeval tv; 9793 struct timezone tz; 9794 9795 ret = get_errno(gettimeofday(&tv, &tz)); 9796 if (!is_error(ret)) { 9797 if (arg1 && copy_to_user_timeval(arg1, &tv)) { 9798 return -TARGET_EFAULT; 9799 } 9800 if (arg2 && copy_to_user_timezone(arg2, &tz)) { 9801 return -TARGET_EFAULT; 9802 } 9803 } 9804 } 9805 return ret; 9806 #endif 9807 #if defined(TARGET_NR_settimeofday) 9808 case TARGET_NR_settimeofday: 9809 { 9810 struct timeval tv, *ptv = NULL; 9811 struct timezone tz, *ptz = NULL; 9812 9813 if (arg1) { 9814 if (copy_from_user_timeval(&tv, arg1)) { 9815 return -TARGET_EFAULT; 9816 } 9817 ptv = &tv; 9818 } 9819 9820 if (arg2) { 9821 if (copy_from_user_timezone(&tz, arg2)) { 9822 return -TARGET_EFAULT; 9823 } 9824 ptz = &tz; 9825 } 9826 9827 return get_errno(settimeofday(ptv, ptz)); 9828 } 9829 #endif 9830 #if defined(TARGET_NR_select) 9831 case TARGET_NR_select: 9832 #if defined(TARGET_WANT_NI_OLD_SELECT) 9833 /* some architectures used to have old_select here 9834 * but now ENOSYS it. 9835 */ 9836 ret = -TARGET_ENOSYS; 9837 #elif defined(TARGET_WANT_OLD_SYS_SELECT) 9838 ret = do_old_select(arg1); 9839 #else 9840 ret = do_select(arg1, arg2, arg3, arg4, arg5); 9841 #endif 9842 return ret; 9843 #endif 9844 #ifdef TARGET_NR_pselect6 9845 case TARGET_NR_pselect6: 9846 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); 9847 #endif 9848 #ifdef TARGET_NR_pselect6_time64 9849 case TARGET_NR_pselect6_time64: 9850 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); 9851 #endif 9852 #ifdef TARGET_NR_symlink 9853 case TARGET_NR_symlink: 9854 { 9855 void *p2; 9856 p = lock_user_string(arg1); 9857 p2 = lock_user_string(arg2); 9858 if (!p || !p2) 9859 ret = -TARGET_EFAULT; 9860 else 9861 ret = get_errno(symlink(p, p2)); 9862 unlock_user(p2, arg2, 0); 9863 unlock_user(p, arg1, 0); 9864 } 9865 return ret; 9866 #endif 9867 #if defined(TARGET_NR_symlinkat) 9868 case TARGET_NR_symlinkat: 9869 { 9870 void *p2; 9871 p = lock_user_string(arg1); 9872 p2 = lock_user_string(arg3); 9873 if (!p || !p2) 9874 ret = -TARGET_EFAULT; 9875 else 9876 ret = get_errno(symlinkat(p, arg2, p2)); 9877 unlock_user(p2, arg3, 0); 9878 unlock_user(p, arg1, 0); 9879 } 9880 return ret; 9881 #endif 9882 #ifdef TARGET_NR_readlink 9883 case TARGET_NR_readlink: 9884 { 9885 void *p2; 9886 p = lock_user_string(arg1); 9887 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0); 9888 if (!p || !p2) { 9889 ret = -TARGET_EFAULT; 9890 } else if (!arg3) { 9891 /* Short circuit this for the magic exe check. */ 9892 ret = -TARGET_EINVAL; 9893 } else if (is_proc_myself((const char *)p, "exe")) { 9894 char real[PATH_MAX], *temp; 9895 temp = realpath(exec_path, real); 9896 /* Return value is # of bytes that we wrote to the buffer. */ 9897 if (temp == NULL) { 9898 ret = get_errno(-1); 9899 } else { 9900 /* Don't worry about sign mismatch as earlier mapping 9901 * logic would have thrown a bad address error. */ 9902 ret = MIN(strlen(real), arg3); 9903 /* We cannot NUL terminate the string. */ 9904 memcpy(p2, real, ret); 9905 } 9906 } else { 9907 ret = get_errno(readlink(path(p), p2, arg3)); 9908 } 9909 unlock_user(p2, arg2, ret); 9910 unlock_user(p, arg1, 0); 9911 } 9912 return ret; 9913 #endif 9914 #if defined(TARGET_NR_readlinkat) 9915 case TARGET_NR_readlinkat: 9916 { 9917 void *p2; 9918 p = lock_user_string(arg2); 9919 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); 9920 if (!p || !p2) { 9921 ret = -TARGET_EFAULT; 9922 } else if (is_proc_myself((const char *)p, "exe")) { 9923 char real[PATH_MAX], *temp; 9924 temp = realpath(exec_path, real); 9925 ret = temp == NULL ? get_errno(-1) : strlen(real) ; 9926 snprintf((char *)p2, arg4, "%s", real); 9927 } else { 9928 ret = get_errno(readlinkat(arg1, path(p), p2, arg4)); 9929 } 9930 unlock_user(p2, arg3, ret); 9931 unlock_user(p, arg2, 0); 9932 } 9933 return ret; 9934 #endif 9935 #ifdef TARGET_NR_swapon 9936 case TARGET_NR_swapon: 9937 if (!(p = lock_user_string(arg1))) 9938 return -TARGET_EFAULT; 9939 ret = get_errno(swapon(p, arg2)); 9940 unlock_user(p, arg1, 0); 9941 return ret; 9942 #endif 9943 case TARGET_NR_reboot: 9944 if (arg3 == LINUX_REBOOT_CMD_RESTART2) { 9945 /* arg4 must be ignored in all other cases */ 9946 p = lock_user_string(arg4); 9947 if (!p) { 9948 return -TARGET_EFAULT; 9949 } 9950 ret = get_errno(reboot(arg1, arg2, arg3, p)); 9951 unlock_user(p, arg4, 0); 9952 } else { 9953 ret = get_errno(reboot(arg1, arg2, arg3, NULL)); 9954 } 9955 return ret; 9956 #ifdef TARGET_NR_mmap 9957 case TARGET_NR_mmap: 9958 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \ 9959 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \ 9960 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \ 9961 || defined(TARGET_S390X) 9962 { 9963 abi_ulong *v; 9964 abi_ulong v1, v2, v3, v4, v5, v6; 9965 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1))) 9966 return -TARGET_EFAULT; 9967 v1 = tswapal(v[0]); 9968 v2 = tswapal(v[1]); 9969 v3 = tswapal(v[2]); 9970 v4 = tswapal(v[3]); 9971 v5 = tswapal(v[4]); 9972 v6 = tswapal(v[5]); 9973 unlock_user(v, arg1, 0); 9974 ret = get_errno(target_mmap(v1, v2, v3, 9975 target_to_host_bitmask(v4, mmap_flags_tbl), 9976 v5, v6)); 9977 } 9978 #else 9979 /* mmap pointers are always untagged */ 9980 ret = get_errno(target_mmap(arg1, arg2, arg3, 9981 target_to_host_bitmask(arg4, mmap_flags_tbl), 9982 arg5, 9983 arg6)); 9984 #endif 9985 return ret; 9986 #endif 9987 #ifdef TARGET_NR_mmap2 9988 case TARGET_NR_mmap2: 9989 #ifndef MMAP_SHIFT 9990 #define MMAP_SHIFT 12 9991 #endif 9992 ret = target_mmap(arg1, arg2, arg3, 9993 target_to_host_bitmask(arg4, mmap_flags_tbl), 9994 arg5, arg6 << MMAP_SHIFT); 9995 return get_errno(ret); 9996 #endif 9997 case TARGET_NR_munmap: 9998 arg1 = cpu_untagged_addr(cpu, arg1); 9999 return get_errno(target_munmap(arg1, arg2)); 10000 case TARGET_NR_mprotect: 10001 arg1 = cpu_untagged_addr(cpu, arg1); 10002 { 10003 TaskState *ts = cpu->opaque; 10004 /* Special hack to detect libc making the stack executable. */ 10005 if ((arg3 & PROT_GROWSDOWN) 10006 && arg1 >= ts->info->stack_limit 10007 && arg1 <= ts->info->start_stack) { 10008 arg3 &= ~PROT_GROWSDOWN; 10009 arg2 = arg2 + arg1 - ts->info->stack_limit; 10010 arg1 = ts->info->stack_limit; 10011 } 10012 } 10013 return get_errno(target_mprotect(arg1, arg2, arg3)); 10014 #ifdef TARGET_NR_mremap 10015 case TARGET_NR_mremap: 10016 arg1 = cpu_untagged_addr(cpu, arg1); 10017 /* mremap new_addr (arg5) is always untagged */ 10018 return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5)); 10019 #endif 10020 /* ??? msync/mlock/munlock are broken for softmmu. */ 10021 #ifdef TARGET_NR_msync 10022 case TARGET_NR_msync: 10023 return get_errno(msync(g2h(cpu, arg1), arg2, arg3)); 10024 #endif 10025 #ifdef TARGET_NR_mlock 10026 case TARGET_NR_mlock: 10027 return get_errno(mlock(g2h(cpu, arg1), arg2)); 10028 #endif 10029 #ifdef TARGET_NR_munlock 10030 case TARGET_NR_munlock: 10031 return get_errno(munlock(g2h(cpu, arg1), arg2)); 10032 #endif 10033 #ifdef TARGET_NR_mlockall 10034 case TARGET_NR_mlockall: 10035 return get_errno(mlockall(target_to_host_mlockall_arg(arg1))); 10036 #endif 10037 #ifdef TARGET_NR_munlockall 10038 case TARGET_NR_munlockall: 10039 return get_errno(munlockall()); 10040 #endif 10041 #ifdef TARGET_NR_truncate 10042 case TARGET_NR_truncate: 10043 if (!(p = lock_user_string(arg1))) 10044 return -TARGET_EFAULT; 10045 ret = get_errno(truncate(p, arg2)); 10046 unlock_user(p, arg1, 0); 10047 return ret; 10048 #endif 10049 #ifdef TARGET_NR_ftruncate 10050 case TARGET_NR_ftruncate: 10051 return get_errno(ftruncate(arg1, arg2)); 10052 #endif 10053 case TARGET_NR_fchmod: 10054 return get_errno(fchmod(arg1, arg2)); 10055 #if defined(TARGET_NR_fchmodat) 10056 case TARGET_NR_fchmodat: 10057 if (!(p = lock_user_string(arg2))) 10058 return -TARGET_EFAULT; 10059 ret = get_errno(fchmodat(arg1, p, arg3, 0)); 10060 unlock_user(p, arg2, 0); 10061 return ret; 10062 #endif 10063 case TARGET_NR_getpriority: 10064 /* Note that negative values are valid for getpriority, so we must 10065 differentiate based on errno settings. */ 10066 errno = 0; 10067 ret = getpriority(arg1, arg2); 10068 if (ret == -1 && errno != 0) { 10069 return -host_to_target_errno(errno); 10070 } 10071 #ifdef TARGET_ALPHA 10072 /* Return value is the unbiased priority. Signal no error. */ 10073 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; 10074 #else 10075 /* Return value is a biased priority to avoid negative numbers. */ 10076 ret = 20 - ret; 10077 #endif 10078 return ret; 10079 case TARGET_NR_setpriority: 10080 return get_errno(setpriority(arg1, arg2, arg3)); 10081 #ifdef TARGET_NR_statfs 10082 case TARGET_NR_statfs: 10083 if (!(p = lock_user_string(arg1))) { 10084 return -TARGET_EFAULT; 10085 } 10086 ret = get_errno(statfs(path(p), &stfs)); 10087 unlock_user(p, arg1, 0); 10088 convert_statfs: 10089 if (!is_error(ret)) { 10090 struct target_statfs *target_stfs; 10091 10092 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0)) 10093 return -TARGET_EFAULT; 10094 __put_user(stfs.f_type, &target_stfs->f_type); 10095 __put_user(stfs.f_bsize, &target_stfs->f_bsize); 10096 __put_user(stfs.f_blocks, &target_stfs->f_blocks); 10097 __put_user(stfs.f_bfree, &target_stfs->f_bfree); 10098 __put_user(stfs.f_bavail, &target_stfs->f_bavail); 10099 __put_user(stfs.f_files, &target_stfs->f_files); 10100 __put_user(stfs.f_ffree, &target_stfs->f_ffree); 10101 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); 10102 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); 10103 __put_user(stfs.f_namelen, &target_stfs->f_namelen); 10104 __put_user(stfs.f_frsize, &target_stfs->f_frsize); 10105 #ifdef _STATFS_F_FLAGS 10106 __put_user(stfs.f_flags, &target_stfs->f_flags); 10107 #else 10108 __put_user(0, &target_stfs->f_flags); 10109 #endif 10110 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); 10111 unlock_user_struct(target_stfs, arg2, 1); 10112 } 10113 return ret; 10114 #endif 10115 #ifdef TARGET_NR_fstatfs 10116 case TARGET_NR_fstatfs: 10117 ret = get_errno(fstatfs(arg1, &stfs)); 10118 goto convert_statfs; 10119 #endif 10120 #ifdef TARGET_NR_statfs64 10121 case TARGET_NR_statfs64: 10122 if (!(p = lock_user_string(arg1))) { 10123 return -TARGET_EFAULT; 10124 } 10125 ret = get_errno(statfs(path(p), &stfs)); 10126 unlock_user(p, arg1, 0); 10127 convert_statfs64: 10128 if (!is_error(ret)) { 10129 struct target_statfs64 *target_stfs; 10130 10131 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0)) 10132 return -TARGET_EFAULT; 10133 __put_user(stfs.f_type, &target_stfs->f_type); 10134 __put_user(stfs.f_bsize, &target_stfs->f_bsize); 10135 __put_user(stfs.f_blocks, &target_stfs->f_blocks); 10136 __put_user(stfs.f_bfree, &target_stfs->f_bfree); 10137 __put_user(stfs.f_bavail, &target_stfs->f_bavail); 10138 __put_user(stfs.f_files, &target_stfs->f_files); 10139 __put_user(stfs.f_ffree, &target_stfs->f_ffree); 10140 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]); 10141 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]); 10142 __put_user(stfs.f_namelen, &target_stfs->f_namelen); 10143 __put_user(stfs.f_frsize, &target_stfs->f_frsize); 10144 #ifdef _STATFS_F_FLAGS 10145 __put_user(stfs.f_flags, &target_stfs->f_flags); 10146 #else 10147 __put_user(0, &target_stfs->f_flags); 10148 #endif 10149 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare)); 10150 unlock_user_struct(target_stfs, arg3, 1); 10151 } 10152 return ret; 10153 case TARGET_NR_fstatfs64: 10154 ret = get_errno(fstatfs(arg1, &stfs)); 10155 goto convert_statfs64; 10156 #endif 10157 #ifdef TARGET_NR_socketcall 10158 case TARGET_NR_socketcall: 10159 return do_socketcall(arg1, arg2); 10160 #endif 10161 #ifdef TARGET_NR_accept 10162 case TARGET_NR_accept: 10163 return do_accept4(arg1, arg2, arg3, 0); 10164 #endif 10165 #ifdef TARGET_NR_accept4 10166 case TARGET_NR_accept4: 10167 return do_accept4(arg1, arg2, arg3, arg4); 10168 #endif 10169 #ifdef TARGET_NR_bind 10170 case TARGET_NR_bind: 10171 return do_bind(arg1, arg2, arg3); 10172 #endif 10173 #ifdef TARGET_NR_connect 10174 case TARGET_NR_connect: 10175 return do_connect(arg1, arg2, arg3); 10176 #endif 10177 #ifdef TARGET_NR_getpeername 10178 case TARGET_NR_getpeername: 10179 return do_getpeername(arg1, arg2, arg3); 10180 #endif 10181 #ifdef TARGET_NR_getsockname 10182 case TARGET_NR_getsockname: 10183 return do_getsockname(arg1, arg2, arg3); 10184 #endif 10185 #ifdef TARGET_NR_getsockopt 10186 case TARGET_NR_getsockopt: 10187 return do_getsockopt(arg1, arg2, arg3, arg4, arg5); 10188 #endif 10189 #ifdef TARGET_NR_listen 10190 case TARGET_NR_listen: 10191 return get_errno(listen(arg1, arg2)); 10192 #endif 10193 #ifdef TARGET_NR_recv 10194 case TARGET_NR_recv: 10195 return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0); 10196 #endif 10197 #ifdef TARGET_NR_recvfrom 10198 case TARGET_NR_recvfrom: 10199 return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6); 10200 #endif 10201 #ifdef TARGET_NR_recvmsg 10202 case TARGET_NR_recvmsg: 10203 return do_sendrecvmsg(arg1, arg2, arg3, 0); 10204 #endif 10205 #ifdef TARGET_NR_send 10206 case TARGET_NR_send: 10207 return do_sendto(arg1, arg2, arg3, arg4, 0, 0); 10208 #endif 10209 #ifdef TARGET_NR_sendmsg 10210 case TARGET_NR_sendmsg: 10211 return do_sendrecvmsg(arg1, arg2, arg3, 1); 10212 #endif 10213 #ifdef TARGET_NR_sendmmsg 10214 case TARGET_NR_sendmmsg: 10215 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1); 10216 #endif 10217 #ifdef TARGET_NR_recvmmsg 10218 case TARGET_NR_recvmmsg: 10219 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0); 10220 #endif 10221 #ifdef TARGET_NR_sendto 10222 case TARGET_NR_sendto: 10223 return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6); 10224 #endif 10225 #ifdef TARGET_NR_shutdown 10226 case TARGET_NR_shutdown: 10227 return get_errno(shutdown(arg1, arg2)); 10228 #endif 10229 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom) 10230 case TARGET_NR_getrandom: 10231 p = lock_user(VERIFY_WRITE, arg1, arg2, 0); 10232 if (!p) { 10233 return -TARGET_EFAULT; 10234 } 10235 ret = get_errno(getrandom(p, arg2, arg3)); 10236 unlock_user(p, arg1, ret); 10237 return ret; 10238 #endif 10239 #ifdef TARGET_NR_socket 10240 case TARGET_NR_socket: 10241 return do_socket(arg1, arg2, arg3); 10242 #endif 10243 #ifdef TARGET_NR_socketpair 10244 case TARGET_NR_socketpair: 10245 return do_socketpair(arg1, arg2, arg3, arg4); 10246 #endif 10247 #ifdef TARGET_NR_setsockopt 10248 case TARGET_NR_setsockopt: 10249 return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5); 10250 #endif 10251 #if defined(TARGET_NR_syslog) 10252 case TARGET_NR_syslog: 10253 { 10254 int len = arg2; 10255 10256 switch (arg1) { 10257 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */ 10258 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */ 10259 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ 10260 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */ 10261 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */ 10262 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */ 10263 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */ 10264 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */ 10265 return get_errno(sys_syslog((int)arg1, NULL, (int)arg3)); 10266 case TARGET_SYSLOG_ACTION_READ: /* Read from log */ 10267 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */ 10268 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */ 10269 { 10270 if (len < 0) { 10271 return -TARGET_EINVAL; 10272 } 10273 if (len == 0) { 10274 return 0; 10275 } 10276 p = lock_user(VERIFY_WRITE, arg2, arg3, 0); 10277 if (!p) { 10278 return -TARGET_EFAULT; 10279 } 10280 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3)); 10281 unlock_user(p, arg2, arg3); 10282 } 10283 return ret; 10284 default: 10285 return -TARGET_EINVAL; 10286 } 10287 } 10288 break; 10289 #endif 10290 case TARGET_NR_setitimer: 10291 { 10292 struct itimerval value, ovalue, *pvalue; 10293 10294 if (arg2) { 10295 pvalue = &value; 10296 if (copy_from_user_timeval(&pvalue->it_interval, arg2) 10297 || copy_from_user_timeval(&pvalue->it_value, 10298 arg2 + sizeof(struct target_timeval))) 10299 return -TARGET_EFAULT; 10300 } else { 10301 pvalue = NULL; 10302 } 10303 ret = get_errno(setitimer(arg1, pvalue, &ovalue)); 10304 if (!is_error(ret) && arg3) { 10305 if (copy_to_user_timeval(arg3, 10306 &ovalue.it_interval) 10307 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval), 10308 &ovalue.it_value)) 10309 return -TARGET_EFAULT; 10310 } 10311 } 10312 return ret; 10313 case TARGET_NR_getitimer: 10314 { 10315 struct itimerval value; 10316 10317 ret = get_errno(getitimer(arg1, &value)); 10318 if (!is_error(ret) && arg2) { 10319 if (copy_to_user_timeval(arg2, 10320 &value.it_interval) 10321 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval), 10322 &value.it_value)) 10323 return -TARGET_EFAULT; 10324 } 10325 } 10326 return ret; 10327 #ifdef TARGET_NR_stat 10328 case TARGET_NR_stat: 10329 if (!(p = lock_user_string(arg1))) { 10330 return -TARGET_EFAULT; 10331 } 10332 ret = get_errno(stat(path(p), &st)); 10333 unlock_user(p, arg1, 0); 10334 goto do_stat; 10335 #endif 10336 #ifdef TARGET_NR_lstat 10337 case TARGET_NR_lstat: 10338 if (!(p = lock_user_string(arg1))) { 10339 return -TARGET_EFAULT; 10340 } 10341 ret = get_errno(lstat(path(p), &st)); 10342 unlock_user(p, arg1, 0); 10343 goto do_stat; 10344 #endif 10345 #ifdef TARGET_NR_fstat 10346 case TARGET_NR_fstat: 10347 { 10348 ret = get_errno(fstat(arg1, &st)); 10349 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat) 10350 do_stat: 10351 #endif 10352 if (!is_error(ret)) { 10353 struct target_stat *target_st; 10354 10355 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0)) 10356 return -TARGET_EFAULT; 10357 memset(target_st, 0, sizeof(*target_st)); 10358 __put_user(st.st_dev, &target_st->st_dev); 10359 __put_user(st.st_ino, &target_st->st_ino); 10360 __put_user(st.st_mode, &target_st->st_mode); 10361 __put_user(st.st_uid, &target_st->st_uid); 10362 __put_user(st.st_gid, &target_st->st_gid); 10363 __put_user(st.st_nlink, &target_st->st_nlink); 10364 __put_user(st.st_rdev, &target_st->st_rdev); 10365 __put_user(st.st_size, &target_st->st_size); 10366 __put_user(st.st_blksize, &target_st->st_blksize); 10367 __put_user(st.st_blocks, &target_st->st_blocks); 10368 __put_user(st.st_atime, &target_st->target_st_atime); 10369 __put_user(st.st_mtime, &target_st->target_st_mtime); 10370 __put_user(st.st_ctime, &target_st->target_st_ctime); 10371 #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC) 10372 __put_user(st.st_atim.tv_nsec, 10373 &target_st->target_st_atime_nsec); 10374 __put_user(st.st_mtim.tv_nsec, 10375 &target_st->target_st_mtime_nsec); 10376 __put_user(st.st_ctim.tv_nsec, 10377 &target_st->target_st_ctime_nsec); 10378 #endif 10379 unlock_user_struct(target_st, arg2, 1); 10380 } 10381 } 10382 return ret; 10383 #endif 10384 case TARGET_NR_vhangup: 10385 return get_errno(vhangup()); 10386 #ifdef TARGET_NR_syscall 10387 case TARGET_NR_syscall: 10388 return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5, 10389 arg6, arg7, arg8, 0); 10390 #endif 10391 #if defined(TARGET_NR_wait4) 10392 case TARGET_NR_wait4: 10393 { 10394 int status; 10395 abi_long status_ptr = arg2; 10396 struct rusage rusage, *rusage_ptr; 10397 abi_ulong target_rusage = arg4; 10398 abi_long rusage_err; 10399 if (target_rusage) 10400 rusage_ptr = &rusage; 10401 else 10402 rusage_ptr = NULL; 10403 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr)); 10404 if (!is_error(ret)) { 10405 if (status_ptr && ret) { 10406 status = host_to_target_waitstatus(status); 10407 if (put_user_s32(status, status_ptr)) 10408 return -TARGET_EFAULT; 10409 } 10410 if (target_rusage) { 10411 rusage_err = host_to_target_rusage(target_rusage, &rusage); 10412 if (rusage_err) { 10413 ret = rusage_err; 10414 } 10415 } 10416 } 10417 } 10418 return ret; 10419 #endif 10420 #ifdef TARGET_NR_swapoff 10421 case TARGET_NR_swapoff: 10422 if (!(p = lock_user_string(arg1))) 10423 return -TARGET_EFAULT; 10424 ret = get_errno(swapoff(p)); 10425 unlock_user(p, arg1, 0); 10426 return ret; 10427 #endif 10428 case TARGET_NR_sysinfo: 10429 { 10430 struct target_sysinfo *target_value; 10431 struct sysinfo value; 10432 ret = get_errno(sysinfo(&value)); 10433 if (!is_error(ret) && arg1) 10434 { 10435 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0)) 10436 return -TARGET_EFAULT; 10437 __put_user(value.uptime, &target_value->uptime); 10438 __put_user(value.loads[0], &target_value->loads[0]); 10439 __put_user(value.loads[1], &target_value->loads[1]); 10440 __put_user(value.loads[2], &target_value->loads[2]); 10441 __put_user(value.totalram, &target_value->totalram); 10442 __put_user(value.freeram, &target_value->freeram); 10443 __put_user(value.sharedram, &target_value->sharedram); 10444 __put_user(value.bufferram, &target_value->bufferram); 10445 __put_user(value.totalswap, &target_value->totalswap); 10446 __put_user(value.freeswap, &target_value->freeswap); 10447 __put_user(value.procs, &target_value->procs); 10448 __put_user(value.totalhigh, &target_value->totalhigh); 10449 __put_user(value.freehigh, &target_value->freehigh); 10450 __put_user(value.mem_unit, &target_value->mem_unit); 10451 unlock_user_struct(target_value, arg1, 1); 10452 } 10453 } 10454 return ret; 10455 #ifdef TARGET_NR_ipc 10456 case TARGET_NR_ipc: 10457 return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); 10458 #endif 10459 #ifdef TARGET_NR_semget 10460 case TARGET_NR_semget: 10461 return get_errno(semget(arg1, arg2, arg3)); 10462 #endif 10463 #ifdef TARGET_NR_semop 10464 case TARGET_NR_semop: 10465 return do_semtimedop(arg1, arg2, arg3, 0, false); 10466 #endif 10467 #ifdef TARGET_NR_semtimedop 10468 case TARGET_NR_semtimedop: 10469 return do_semtimedop(arg1, arg2, arg3, arg4, false); 10470 #endif 10471 #ifdef TARGET_NR_semtimedop_time64 10472 case TARGET_NR_semtimedop_time64: 10473 return do_semtimedop(arg1, arg2, arg3, arg4, true); 10474 #endif 10475 #ifdef TARGET_NR_semctl 10476 case TARGET_NR_semctl: 10477 return do_semctl(arg1, arg2, arg3, arg4); 10478 #endif 10479 #ifdef TARGET_NR_msgctl 10480 case TARGET_NR_msgctl: 10481 return do_msgctl(arg1, arg2, arg3); 10482 #endif 10483 #ifdef TARGET_NR_msgget 10484 case TARGET_NR_msgget: 10485 return get_errno(msgget(arg1, arg2)); 10486 #endif 10487 #ifdef TARGET_NR_msgrcv 10488 case TARGET_NR_msgrcv: 10489 return do_msgrcv(arg1, arg2, arg3, arg4, arg5); 10490 #endif 10491 #ifdef TARGET_NR_msgsnd 10492 case TARGET_NR_msgsnd: 10493 return do_msgsnd(arg1, arg2, arg3, arg4); 10494 #endif 10495 #ifdef TARGET_NR_shmget 10496 case TARGET_NR_shmget: 10497 return get_errno(shmget(arg1, arg2, arg3)); 10498 #endif 10499 #ifdef TARGET_NR_shmctl 10500 case TARGET_NR_shmctl: 10501 return do_shmctl(arg1, arg2, arg3); 10502 #endif 10503 #ifdef TARGET_NR_shmat 10504 case TARGET_NR_shmat: 10505 return do_shmat(cpu_env, arg1, arg2, arg3); 10506 #endif 10507 #ifdef TARGET_NR_shmdt 10508 case TARGET_NR_shmdt: 10509 return do_shmdt(arg1); 10510 #endif 10511 case TARGET_NR_fsync: 10512 return get_errno(fsync(arg1)); 10513 case TARGET_NR_clone: 10514 /* Linux manages to have three different orderings for its 10515 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines 10516 * match the kernel's CONFIG_CLONE_* settings. 10517 * Microblaze is further special in that it uses a sixth 10518 * implicit argument to clone for the TLS pointer. 10519 */ 10520 #if defined(TARGET_MICROBLAZE) 10521 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5)); 10522 #elif defined(TARGET_CLONE_BACKWARDS) 10523 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5)); 10524 #elif defined(TARGET_CLONE_BACKWARDS2) 10525 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4)); 10526 #else 10527 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4)); 10528 #endif 10529 return ret; 10530 #ifdef __NR_exit_group 10531 /* new thread calls */ 10532 case TARGET_NR_exit_group: 10533 preexit_cleanup(cpu_env, arg1); 10534 return get_errno(exit_group(arg1)); 10535 #endif 10536 case TARGET_NR_setdomainname: 10537 if (!(p = lock_user_string(arg1))) 10538 return -TARGET_EFAULT; 10539 ret = get_errno(setdomainname(p, arg2)); 10540 unlock_user(p, arg1, 0); 10541 return ret; 10542 case TARGET_NR_uname: 10543 /* no need to transcode because we use the linux syscall */ 10544 { 10545 struct new_utsname * buf; 10546 10547 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0)) 10548 return -TARGET_EFAULT; 10549 ret = get_errno(sys_uname(buf)); 10550 if (!is_error(ret)) { 10551 /* Overwrite the native machine name with whatever is being 10552 emulated. */ 10553 g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env), 10554 sizeof(buf->machine)); 10555 /* Allow the user to override the reported release. */ 10556 if (qemu_uname_release && *qemu_uname_release) { 10557 g_strlcpy(buf->release, qemu_uname_release, 10558 sizeof(buf->release)); 10559 } 10560 } 10561 unlock_user_struct(buf, arg1, 1); 10562 } 10563 return ret; 10564 #ifdef TARGET_I386 10565 case TARGET_NR_modify_ldt: 10566 return do_modify_ldt(cpu_env, arg1, arg2, arg3); 10567 #if !defined(TARGET_X86_64) 10568 case TARGET_NR_vm86: 10569 return do_vm86(cpu_env, arg1, arg2); 10570 #endif 10571 #endif 10572 #if defined(TARGET_NR_adjtimex) 10573 case TARGET_NR_adjtimex: 10574 { 10575 struct timex host_buf; 10576 10577 if (target_to_host_timex(&host_buf, arg1) != 0) { 10578 return -TARGET_EFAULT; 10579 } 10580 ret = get_errno(adjtimex(&host_buf)); 10581 if (!is_error(ret)) { 10582 if (host_to_target_timex(arg1, &host_buf) != 0) { 10583 return -TARGET_EFAULT; 10584 } 10585 } 10586 } 10587 return ret; 10588 #endif 10589 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME) 10590 case TARGET_NR_clock_adjtime: 10591 { 10592 struct timex htx, *phtx = &htx; 10593 10594 if (target_to_host_timex(phtx, arg2) != 0) { 10595 return -TARGET_EFAULT; 10596 } 10597 ret = get_errno(clock_adjtime(arg1, phtx)); 10598 if (!is_error(ret) && phtx) { 10599 if (host_to_target_timex(arg2, phtx) != 0) { 10600 return -TARGET_EFAULT; 10601 } 10602 } 10603 } 10604 return ret; 10605 #endif 10606 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME) 10607 case TARGET_NR_clock_adjtime64: 10608 { 10609 struct timex htx; 10610 10611 if (target_to_host_timex64(&htx, arg2) != 0) { 10612 return -TARGET_EFAULT; 10613 } 10614 ret = get_errno(clock_adjtime(arg1, &htx)); 10615 if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) { 10616 return -TARGET_EFAULT; 10617 } 10618 } 10619 return ret; 10620 #endif 10621 case TARGET_NR_getpgid: 10622 return get_errno(getpgid(arg1)); 10623 case TARGET_NR_fchdir: 10624 return get_errno(fchdir(arg1)); 10625 case TARGET_NR_personality: 10626 return get_errno(personality(arg1)); 10627 #ifdef TARGET_NR__llseek /* Not on alpha */ 10628 case TARGET_NR__llseek: 10629 { 10630 int64_t res; 10631 #if !defined(__NR_llseek) 10632 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5); 10633 if (res == -1) { 10634 ret = get_errno(res); 10635 } else { 10636 ret = 0; 10637 } 10638 #else 10639 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5)); 10640 #endif 10641 if ((ret == 0) && put_user_s64(res, arg4)) { 10642 return -TARGET_EFAULT; 10643 } 10644 } 10645 return ret; 10646 #endif 10647 #ifdef TARGET_NR_getdents 10648 case TARGET_NR_getdents: 10649 return do_getdents(arg1, arg2, arg3); 10650 #endif /* TARGET_NR_getdents */ 10651 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) 10652 case TARGET_NR_getdents64: 10653 return do_getdents64(arg1, arg2, arg3); 10654 #endif /* TARGET_NR_getdents64 */ 10655 #if defined(TARGET_NR__newselect) 10656 case TARGET_NR__newselect: 10657 return do_select(arg1, arg2, arg3, arg4, arg5); 10658 #endif 10659 #ifdef TARGET_NR_poll 10660 case TARGET_NR_poll: 10661 return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); 10662 #endif 10663 #ifdef TARGET_NR_ppoll 10664 case TARGET_NR_ppoll: 10665 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); 10666 #endif 10667 #ifdef TARGET_NR_ppoll_time64 10668 case TARGET_NR_ppoll_time64: 10669 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); 10670 #endif 10671 case TARGET_NR_flock: 10672 /* NOTE: the flock constant seems to be the same for every 10673 Linux platform */ 10674 return get_errno(safe_flock(arg1, arg2)); 10675 case TARGET_NR_readv: 10676 { 10677 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); 10678 if (vec != NULL) { 10679 ret = get_errno(safe_readv(arg1, vec, arg3)); 10680 unlock_iovec(vec, arg2, arg3, 1); 10681 } else { 10682 ret = -host_to_target_errno(errno); 10683 } 10684 } 10685 return ret; 10686 case TARGET_NR_writev: 10687 { 10688 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); 10689 if (vec != NULL) { 10690 ret = get_errno(safe_writev(arg1, vec, arg3)); 10691 unlock_iovec(vec, arg2, arg3, 0); 10692 } else { 10693 ret = -host_to_target_errno(errno); 10694 } 10695 } 10696 return ret; 10697 #if defined(TARGET_NR_preadv) 10698 case TARGET_NR_preadv: 10699 { 10700 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); 10701 if (vec != NULL) { 10702 unsigned long low, high; 10703 10704 target_to_host_low_high(arg4, arg5, &low, &high); 10705 ret = get_errno(safe_preadv(arg1, vec, arg3, low, high)); 10706 unlock_iovec(vec, arg2, arg3, 1); 10707 } else { 10708 ret = -host_to_target_errno(errno); 10709 } 10710 } 10711 return ret; 10712 #endif 10713 #if defined(TARGET_NR_pwritev) 10714 case TARGET_NR_pwritev: 10715 { 10716 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); 10717 if (vec != NULL) { 10718 unsigned long low, high; 10719 10720 target_to_host_low_high(arg4, arg5, &low, &high); 10721 ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high)); 10722 unlock_iovec(vec, arg2, arg3, 0); 10723 } else { 10724 ret = -host_to_target_errno(errno); 10725 } 10726 } 10727 return ret; 10728 #endif 10729 case TARGET_NR_getsid: 10730 return get_errno(getsid(arg1)); 10731 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */ 10732 case TARGET_NR_fdatasync: 10733 return get_errno(fdatasync(arg1)); 10734 #endif 10735 case TARGET_NR_sched_getaffinity: 10736 { 10737 unsigned int mask_size; 10738 unsigned long *mask; 10739 10740 /* 10741 * sched_getaffinity needs multiples of ulong, so need to take 10742 * care of mismatches between target ulong and host ulong sizes. 10743 */ 10744 if (arg2 & (sizeof(abi_ulong) - 1)) { 10745 return -TARGET_EINVAL; 10746 } 10747 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); 10748 10749 mask = alloca(mask_size); 10750 memset(mask, 0, mask_size); 10751 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask)); 10752 10753 if (!is_error(ret)) { 10754 if (ret > arg2) { 10755 /* More data returned than the caller's buffer will fit. 10756 * This only happens if sizeof(abi_long) < sizeof(long) 10757 * and the caller passed us a buffer holding an odd number 10758 * of abi_longs. If the host kernel is actually using the 10759 * extra 4 bytes then fail EINVAL; otherwise we can just 10760 * ignore them and only copy the interesting part. 10761 */ 10762 int numcpus = sysconf(_SC_NPROCESSORS_CONF); 10763 if (numcpus > arg2 * 8) { 10764 return -TARGET_EINVAL; 10765 } 10766 ret = arg2; 10767 } 10768 10769 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) { 10770 return -TARGET_EFAULT; 10771 } 10772 } 10773 } 10774 return ret; 10775 case TARGET_NR_sched_setaffinity: 10776 { 10777 unsigned int mask_size; 10778 unsigned long *mask; 10779 10780 /* 10781 * sched_setaffinity needs multiples of ulong, so need to take 10782 * care of mismatches between target ulong and host ulong sizes. 10783 */ 10784 if (arg2 & (sizeof(abi_ulong) - 1)) { 10785 return -TARGET_EINVAL; 10786 } 10787 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1); 10788 mask = alloca(mask_size); 10789 10790 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2); 10791 if (ret) { 10792 return ret; 10793 } 10794 10795 return get_errno(sys_sched_setaffinity(arg1, mask_size, mask)); 10796 } 10797 case TARGET_NR_getcpu: 10798 { 10799 unsigned cpu, node; 10800 ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL, 10801 arg2 ? &node : NULL, 10802 NULL)); 10803 if (is_error(ret)) { 10804 return ret; 10805 } 10806 if (arg1 && put_user_u32(cpu, arg1)) { 10807 return -TARGET_EFAULT; 10808 } 10809 if (arg2 && put_user_u32(node, arg2)) { 10810 return -TARGET_EFAULT; 10811 } 10812 } 10813 return ret; 10814 case TARGET_NR_sched_setparam: 10815 { 10816 struct target_sched_param *target_schp; 10817 struct sched_param schp; 10818 10819 if (arg2 == 0) { 10820 return -TARGET_EINVAL; 10821 } 10822 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) { 10823 return -TARGET_EFAULT; 10824 } 10825 schp.sched_priority = tswap32(target_schp->sched_priority); 10826 unlock_user_struct(target_schp, arg2, 0); 10827 return get_errno(sys_sched_setparam(arg1, &schp)); 10828 } 10829 case TARGET_NR_sched_getparam: 10830 { 10831 struct target_sched_param *target_schp; 10832 struct sched_param schp; 10833 10834 if (arg2 == 0) { 10835 return -TARGET_EINVAL; 10836 } 10837 ret = get_errno(sys_sched_getparam(arg1, &schp)); 10838 if (!is_error(ret)) { 10839 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) { 10840 return -TARGET_EFAULT; 10841 } 10842 target_schp->sched_priority = tswap32(schp.sched_priority); 10843 unlock_user_struct(target_schp, arg2, 1); 10844 } 10845 } 10846 return ret; 10847 case TARGET_NR_sched_setscheduler: 10848 { 10849 struct target_sched_param *target_schp; 10850 struct sched_param schp; 10851 if (arg3 == 0) { 10852 return -TARGET_EINVAL; 10853 } 10854 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) { 10855 return -TARGET_EFAULT; 10856 } 10857 schp.sched_priority = tswap32(target_schp->sched_priority); 10858 unlock_user_struct(target_schp, arg3, 0); 10859 return get_errno(sys_sched_setscheduler(arg1, arg2, &schp)); 10860 } 10861 case TARGET_NR_sched_getscheduler: 10862 return get_errno(sys_sched_getscheduler(arg1)); 10863 case TARGET_NR_sched_getattr: 10864 { 10865 struct target_sched_attr *target_scha; 10866 struct sched_attr scha; 10867 if (arg2 == 0) { 10868 return -TARGET_EINVAL; 10869 } 10870 if (arg3 > sizeof(scha)) { 10871 arg3 = sizeof(scha); 10872 } 10873 ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4)); 10874 if (!is_error(ret)) { 10875 target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0); 10876 if (!target_scha) { 10877 return -TARGET_EFAULT; 10878 } 10879 target_scha->size = tswap32(scha.size); 10880 target_scha->sched_policy = tswap32(scha.sched_policy); 10881 target_scha->sched_flags = tswap64(scha.sched_flags); 10882 target_scha->sched_nice = tswap32(scha.sched_nice); 10883 target_scha->sched_priority = tswap32(scha.sched_priority); 10884 target_scha->sched_runtime = tswap64(scha.sched_runtime); 10885 target_scha->sched_deadline = tswap64(scha.sched_deadline); 10886 target_scha->sched_period = tswap64(scha.sched_period); 10887 if (scha.size > offsetof(struct sched_attr, sched_util_min)) { 10888 target_scha->sched_util_min = tswap32(scha.sched_util_min); 10889 target_scha->sched_util_max = tswap32(scha.sched_util_max); 10890 } 10891 unlock_user(target_scha, arg2, arg3); 10892 } 10893 return ret; 10894 } 10895 case TARGET_NR_sched_setattr: 10896 { 10897 struct target_sched_attr *target_scha; 10898 struct sched_attr scha; 10899 uint32_t size; 10900 int zeroed; 10901 if (arg2 == 0) { 10902 return -TARGET_EINVAL; 10903 } 10904 if (get_user_u32(size, arg2)) { 10905 return -TARGET_EFAULT; 10906 } 10907 if (!size) { 10908 size = offsetof(struct target_sched_attr, sched_util_min); 10909 } 10910 if (size < offsetof(struct target_sched_attr, sched_util_min)) { 10911 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) { 10912 return -TARGET_EFAULT; 10913 } 10914 return -TARGET_E2BIG; 10915 } 10916 10917 zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size); 10918 if (zeroed < 0) { 10919 return zeroed; 10920 } else if (zeroed == 0) { 10921 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) { 10922 return -TARGET_EFAULT; 10923 } 10924 return -TARGET_E2BIG; 10925 } 10926 if (size > sizeof(struct target_sched_attr)) { 10927 size = sizeof(struct target_sched_attr); 10928 } 10929 10930 target_scha = lock_user(VERIFY_READ, arg2, size, 1); 10931 if (!target_scha) { 10932 return -TARGET_EFAULT; 10933 } 10934 scha.size = size; 10935 scha.sched_policy = tswap32(target_scha->sched_policy); 10936 scha.sched_flags = tswap64(target_scha->sched_flags); 10937 scha.sched_nice = tswap32(target_scha->sched_nice); 10938 scha.sched_priority = tswap32(target_scha->sched_priority); 10939 scha.sched_runtime = tswap64(target_scha->sched_runtime); 10940 scha.sched_deadline = tswap64(target_scha->sched_deadline); 10941 scha.sched_period = tswap64(target_scha->sched_period); 10942 if (size > offsetof(struct target_sched_attr, sched_util_min)) { 10943 scha.sched_util_min = tswap32(target_scha->sched_util_min); 10944 scha.sched_util_max = tswap32(target_scha->sched_util_max); 10945 } 10946 unlock_user(target_scha, arg2, 0); 10947 return get_errno(sys_sched_setattr(arg1, &scha, arg3)); 10948 } 10949 case TARGET_NR_sched_yield: 10950 return get_errno(sched_yield()); 10951 case TARGET_NR_sched_get_priority_max: 10952 return get_errno(sched_get_priority_max(arg1)); 10953 case TARGET_NR_sched_get_priority_min: 10954 return get_errno(sched_get_priority_min(arg1)); 10955 #ifdef TARGET_NR_sched_rr_get_interval 10956 case TARGET_NR_sched_rr_get_interval: 10957 { 10958 struct timespec ts; 10959 ret = get_errno(sched_rr_get_interval(arg1, &ts)); 10960 if (!is_error(ret)) { 10961 ret = host_to_target_timespec(arg2, &ts); 10962 } 10963 } 10964 return ret; 10965 #endif 10966 #ifdef TARGET_NR_sched_rr_get_interval_time64 10967 case TARGET_NR_sched_rr_get_interval_time64: 10968 { 10969 struct timespec ts; 10970 ret = get_errno(sched_rr_get_interval(arg1, &ts)); 10971 if (!is_error(ret)) { 10972 ret = host_to_target_timespec64(arg2, &ts); 10973 } 10974 } 10975 return ret; 10976 #endif 10977 #if defined(TARGET_NR_nanosleep) 10978 case TARGET_NR_nanosleep: 10979 { 10980 struct timespec req, rem; 10981 target_to_host_timespec(&req, arg1); 10982 ret = get_errno(safe_nanosleep(&req, &rem)); 10983 if (is_error(ret) && arg2) { 10984 host_to_target_timespec(arg2, &rem); 10985 } 10986 } 10987 return ret; 10988 #endif 10989 case TARGET_NR_prctl: 10990 return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5); 10991 break; 10992 #ifdef TARGET_NR_arch_prctl 10993 case TARGET_NR_arch_prctl: 10994 return do_arch_prctl(cpu_env, arg1, arg2); 10995 #endif 10996 #ifdef TARGET_NR_pread64 10997 case TARGET_NR_pread64: 10998 if (regpairs_aligned(cpu_env, num)) { 10999 arg4 = arg5; 11000 arg5 = arg6; 11001 } 11002 if (arg2 == 0 && arg3 == 0) { 11003 /* Special-case NULL buffer and zero length, which should succeed */ 11004 p = 0; 11005 } else { 11006 p = lock_user(VERIFY_WRITE, arg2, arg3, 0); 11007 if (!p) { 11008 return -TARGET_EFAULT; 11009 } 11010 } 11011 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5))); 11012 unlock_user(p, arg2, ret); 11013 return ret; 11014 case TARGET_NR_pwrite64: 11015 if (regpairs_aligned(cpu_env, num)) { 11016 arg4 = arg5; 11017 arg5 = arg6; 11018 } 11019 if (arg2 == 0 && arg3 == 0) { 11020 /* Special-case NULL buffer and zero length, which should succeed */ 11021 p = 0; 11022 } else { 11023 p = lock_user(VERIFY_READ, arg2, arg3, 1); 11024 if (!p) { 11025 return -TARGET_EFAULT; 11026 } 11027 } 11028 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5))); 11029 unlock_user(p, arg2, 0); 11030 return ret; 11031 #endif 11032 case TARGET_NR_getcwd: 11033 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0))) 11034 return -TARGET_EFAULT; 11035 ret = get_errno(sys_getcwd1(p, arg2)); 11036 unlock_user(p, arg1, ret); 11037 return ret; 11038 case TARGET_NR_capget: 11039 case TARGET_NR_capset: 11040 { 11041 struct target_user_cap_header *target_header; 11042 struct target_user_cap_data *target_data = NULL; 11043 struct __user_cap_header_struct header; 11044 struct __user_cap_data_struct data[2]; 11045 struct __user_cap_data_struct *dataptr = NULL; 11046 int i, target_datalen; 11047 int data_items = 1; 11048 11049 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) { 11050 return -TARGET_EFAULT; 11051 } 11052 header.version = tswap32(target_header->version); 11053 header.pid = tswap32(target_header->pid); 11054 11055 if (header.version != _LINUX_CAPABILITY_VERSION) { 11056 /* Version 2 and up takes pointer to two user_data structs */ 11057 data_items = 2; 11058 } 11059 11060 target_datalen = sizeof(*target_data) * data_items; 11061 11062 if (arg2) { 11063 if (num == TARGET_NR_capget) { 11064 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0); 11065 } else { 11066 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1); 11067 } 11068 if (!target_data) { 11069 unlock_user_struct(target_header, arg1, 0); 11070 return -TARGET_EFAULT; 11071 } 11072 11073 if (num == TARGET_NR_capset) { 11074 for (i = 0; i < data_items; i++) { 11075 data[i].effective = tswap32(target_data[i].effective); 11076 data[i].permitted = tswap32(target_data[i].permitted); 11077 data[i].inheritable = tswap32(target_data[i].inheritable); 11078 } 11079 } 11080 11081 dataptr = data; 11082 } 11083 11084 if (num == TARGET_NR_capget) { 11085 ret = get_errno(capget(&header, dataptr)); 11086 } else { 11087 ret = get_errno(capset(&header, dataptr)); 11088 } 11089 11090 /* The kernel always updates version for both capget and capset */ 11091 target_header->version = tswap32(header.version); 11092 unlock_user_struct(target_header, arg1, 1); 11093 11094 if (arg2) { 11095 if (num == TARGET_NR_capget) { 11096 for (i = 0; i < data_items; i++) { 11097 target_data[i].effective = tswap32(data[i].effective); 11098 target_data[i].permitted = tswap32(data[i].permitted); 11099 target_data[i].inheritable = tswap32(data[i].inheritable); 11100 } 11101 unlock_user(target_data, arg2, target_datalen); 11102 } else { 11103 unlock_user(target_data, arg2, 0); 11104 } 11105 } 11106 return ret; 11107 } 11108 case TARGET_NR_sigaltstack: 11109 return do_sigaltstack(arg1, arg2, cpu_env); 11110 11111 #ifdef CONFIG_SENDFILE 11112 #ifdef TARGET_NR_sendfile 11113 case TARGET_NR_sendfile: 11114 { 11115 off_t *offp = NULL; 11116 off_t off; 11117 if (arg3) { 11118 ret = get_user_sal(off, arg3); 11119 if (is_error(ret)) { 11120 return ret; 11121 } 11122 offp = &off; 11123 } 11124 ret = get_errno(sendfile(arg1, arg2, offp, arg4)); 11125 if (!is_error(ret) && arg3) { 11126 abi_long ret2 = put_user_sal(off, arg3); 11127 if (is_error(ret2)) { 11128 ret = ret2; 11129 } 11130 } 11131 return ret; 11132 } 11133 #endif 11134 #ifdef TARGET_NR_sendfile64 11135 case TARGET_NR_sendfile64: 11136 { 11137 off_t *offp = NULL; 11138 off_t off; 11139 if (arg3) { 11140 ret = get_user_s64(off, arg3); 11141 if (is_error(ret)) { 11142 return ret; 11143 } 11144 offp = &off; 11145 } 11146 ret = get_errno(sendfile(arg1, arg2, offp, arg4)); 11147 if (!is_error(ret) && arg3) { 11148 abi_long ret2 = put_user_s64(off, arg3); 11149 if (is_error(ret2)) { 11150 ret = ret2; 11151 } 11152 } 11153 return ret; 11154 } 11155 #endif 11156 #endif 11157 #ifdef TARGET_NR_vfork 11158 case TARGET_NR_vfork: 11159 return get_errno(do_fork(cpu_env, 11160 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD, 11161 0, 0, 0, 0)); 11162 #endif 11163 #ifdef TARGET_NR_ugetrlimit 11164 case TARGET_NR_ugetrlimit: 11165 { 11166 struct rlimit rlim; 11167 int resource = target_to_host_resource(arg1); 11168 ret = get_errno(getrlimit(resource, &rlim)); 11169 if (!is_error(ret)) { 11170 struct target_rlimit *target_rlim; 11171 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) 11172 return -TARGET_EFAULT; 11173 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); 11174 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); 11175 unlock_user_struct(target_rlim, arg2, 1); 11176 } 11177 return ret; 11178 } 11179 #endif 11180 #ifdef TARGET_NR_truncate64 11181 case TARGET_NR_truncate64: 11182 if (!(p = lock_user_string(arg1))) 11183 return -TARGET_EFAULT; 11184 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4); 11185 unlock_user(p, arg1, 0); 11186 return ret; 11187 #endif 11188 #ifdef TARGET_NR_ftruncate64 11189 case TARGET_NR_ftruncate64: 11190 return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4); 11191 #endif 11192 #ifdef TARGET_NR_stat64 11193 case TARGET_NR_stat64: 11194 if (!(p = lock_user_string(arg1))) { 11195 return -TARGET_EFAULT; 11196 } 11197 ret = get_errno(stat(path(p), &st)); 11198 unlock_user(p, arg1, 0); 11199 if (!is_error(ret)) 11200 ret = host_to_target_stat64(cpu_env, arg2, &st); 11201 return ret; 11202 #endif 11203 #ifdef TARGET_NR_lstat64 11204 case TARGET_NR_lstat64: 11205 if (!(p = lock_user_string(arg1))) { 11206 return -TARGET_EFAULT; 11207 } 11208 ret = get_errno(lstat(path(p), &st)); 11209 unlock_user(p, arg1, 0); 11210 if (!is_error(ret)) 11211 ret = host_to_target_stat64(cpu_env, arg2, &st); 11212 return ret; 11213 #endif 11214 #ifdef TARGET_NR_fstat64 11215 case TARGET_NR_fstat64: 11216 ret = get_errno(fstat(arg1, &st)); 11217 if (!is_error(ret)) 11218 ret = host_to_target_stat64(cpu_env, arg2, &st); 11219 return ret; 11220 #endif 11221 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) 11222 #ifdef TARGET_NR_fstatat64 11223 case TARGET_NR_fstatat64: 11224 #endif 11225 #ifdef TARGET_NR_newfstatat 11226 case TARGET_NR_newfstatat: 11227 #endif 11228 if (!(p = lock_user_string(arg2))) { 11229 return -TARGET_EFAULT; 11230 } 11231 ret = get_errno(fstatat(arg1, path(p), &st, arg4)); 11232 unlock_user(p, arg2, 0); 11233 if (!is_error(ret)) 11234 ret = host_to_target_stat64(cpu_env, arg3, &st); 11235 return ret; 11236 #endif 11237 #if defined(TARGET_NR_statx) 11238 case TARGET_NR_statx: 11239 { 11240 struct target_statx *target_stx; 11241 int dirfd = arg1; 11242 int flags = arg3; 11243 11244 p = lock_user_string(arg2); 11245 if (p == NULL) { 11246 return -TARGET_EFAULT; 11247 } 11248 #if defined(__NR_statx) 11249 { 11250 /* 11251 * It is assumed that struct statx is architecture independent. 11252 */ 11253 struct target_statx host_stx; 11254 int mask = arg4; 11255 11256 ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx)); 11257 if (!is_error(ret)) { 11258 if (host_to_target_statx(&host_stx, arg5) != 0) { 11259 unlock_user(p, arg2, 0); 11260 return -TARGET_EFAULT; 11261 } 11262 } 11263 11264 if (ret != -TARGET_ENOSYS) { 11265 unlock_user(p, arg2, 0); 11266 return ret; 11267 } 11268 } 11269 #endif 11270 ret = get_errno(fstatat(dirfd, path(p), &st, flags)); 11271 unlock_user(p, arg2, 0); 11272 11273 if (!is_error(ret)) { 11274 if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) { 11275 return -TARGET_EFAULT; 11276 } 11277 memset(target_stx, 0, sizeof(*target_stx)); 11278 __put_user(major(st.st_dev), &target_stx->stx_dev_major); 11279 __put_user(minor(st.st_dev), &target_stx->stx_dev_minor); 11280 __put_user(st.st_ino, &target_stx->stx_ino); 11281 __put_user(st.st_mode, &target_stx->stx_mode); 11282 __put_user(st.st_uid, &target_stx->stx_uid); 11283 __put_user(st.st_gid, &target_stx->stx_gid); 11284 __put_user(st.st_nlink, &target_stx->stx_nlink); 11285 __put_user(major(st.st_rdev), &target_stx->stx_rdev_major); 11286 __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor); 11287 __put_user(st.st_size, &target_stx->stx_size); 11288 __put_user(st.st_blksize, &target_stx->stx_blksize); 11289 __put_user(st.st_blocks, &target_stx->stx_blocks); 11290 __put_user(st.st_atime, &target_stx->stx_atime.tv_sec); 11291 __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec); 11292 __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec); 11293 unlock_user_struct(target_stx, arg5, 1); 11294 } 11295 } 11296 return ret; 11297 #endif 11298 #ifdef TARGET_NR_lchown 11299 case TARGET_NR_lchown: 11300 if (!(p = lock_user_string(arg1))) 11301 return -TARGET_EFAULT; 11302 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3))); 11303 unlock_user(p, arg1, 0); 11304 return ret; 11305 #endif 11306 #ifdef TARGET_NR_getuid 11307 case TARGET_NR_getuid: 11308 return get_errno(high2lowuid(getuid())); 11309 #endif 11310 #ifdef TARGET_NR_getgid 11311 case TARGET_NR_getgid: 11312 return get_errno(high2lowgid(getgid())); 11313 #endif 11314 #ifdef TARGET_NR_geteuid 11315 case TARGET_NR_geteuid: 11316 return get_errno(high2lowuid(geteuid())); 11317 #endif 11318 #ifdef TARGET_NR_getegid 11319 case TARGET_NR_getegid: 11320 return get_errno(high2lowgid(getegid())); 11321 #endif 11322 case TARGET_NR_setreuid: 11323 return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2))); 11324 case TARGET_NR_setregid: 11325 return get_errno(setregid(low2highgid(arg1), low2highgid(arg2))); 11326 case TARGET_NR_getgroups: 11327 { 11328 int gidsetsize = arg1; 11329 target_id *target_grouplist; 11330 gid_t *grouplist; 11331 int i; 11332 11333 grouplist = alloca(gidsetsize * sizeof(gid_t)); 11334 ret = get_errno(getgroups(gidsetsize, grouplist)); 11335 if (gidsetsize == 0) 11336 return ret; 11337 if (!is_error(ret)) { 11338 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0); 11339 if (!target_grouplist) 11340 return -TARGET_EFAULT; 11341 for(i = 0;i < ret; i++) 11342 target_grouplist[i] = tswapid(high2lowgid(grouplist[i])); 11343 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id)); 11344 } 11345 } 11346 return ret; 11347 case TARGET_NR_setgroups: 11348 { 11349 int gidsetsize = arg1; 11350 target_id *target_grouplist; 11351 gid_t *grouplist = NULL; 11352 int i; 11353 if (gidsetsize) { 11354 grouplist = alloca(gidsetsize * sizeof(gid_t)); 11355 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1); 11356 if (!target_grouplist) { 11357 return -TARGET_EFAULT; 11358 } 11359 for (i = 0; i < gidsetsize; i++) { 11360 grouplist[i] = low2highgid(tswapid(target_grouplist[i])); 11361 } 11362 unlock_user(target_grouplist, arg2, 0); 11363 } 11364 return get_errno(setgroups(gidsetsize, grouplist)); 11365 } 11366 case TARGET_NR_fchown: 11367 return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); 11368 #if defined(TARGET_NR_fchownat) 11369 case TARGET_NR_fchownat: 11370 if (!(p = lock_user_string(arg2))) 11371 return -TARGET_EFAULT; 11372 ret = get_errno(fchownat(arg1, p, low2highuid(arg3), 11373 low2highgid(arg4), arg5)); 11374 unlock_user(p, arg2, 0); 11375 return ret; 11376 #endif 11377 #ifdef TARGET_NR_setresuid 11378 case TARGET_NR_setresuid: 11379 return get_errno(sys_setresuid(low2highuid(arg1), 11380 low2highuid(arg2), 11381 low2highuid(arg3))); 11382 #endif 11383 #ifdef TARGET_NR_getresuid 11384 case TARGET_NR_getresuid: 11385 { 11386 uid_t ruid, euid, suid; 11387 ret = get_errno(getresuid(&ruid, &euid, &suid)); 11388 if (!is_error(ret)) { 11389 if (put_user_id(high2lowuid(ruid), arg1) 11390 || put_user_id(high2lowuid(euid), arg2) 11391 || put_user_id(high2lowuid(suid), arg3)) 11392 return -TARGET_EFAULT; 11393 } 11394 } 11395 return ret; 11396 #endif 11397 #ifdef TARGET_NR_getresgid 11398 case TARGET_NR_setresgid: 11399 return get_errno(sys_setresgid(low2highgid(arg1), 11400 low2highgid(arg2), 11401 low2highgid(arg3))); 11402 #endif 11403 #ifdef TARGET_NR_getresgid 11404 case TARGET_NR_getresgid: 11405 { 11406 gid_t rgid, egid, sgid; 11407 ret = get_errno(getresgid(&rgid, &egid, &sgid)); 11408 if (!is_error(ret)) { 11409 if (put_user_id(high2lowgid(rgid), arg1) 11410 || put_user_id(high2lowgid(egid), arg2) 11411 || put_user_id(high2lowgid(sgid), arg3)) 11412 return -TARGET_EFAULT; 11413 } 11414 } 11415 return ret; 11416 #endif 11417 #ifdef TARGET_NR_chown 11418 case TARGET_NR_chown: 11419 if (!(p = lock_user_string(arg1))) 11420 return -TARGET_EFAULT; 11421 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3))); 11422 unlock_user(p, arg1, 0); 11423 return ret; 11424 #endif 11425 case TARGET_NR_setuid: 11426 return get_errno(sys_setuid(low2highuid(arg1))); 11427 case TARGET_NR_setgid: 11428 return get_errno(sys_setgid(low2highgid(arg1))); 11429 case TARGET_NR_setfsuid: 11430 return get_errno(setfsuid(arg1)); 11431 case TARGET_NR_setfsgid: 11432 return get_errno(setfsgid(arg1)); 11433 11434 #ifdef TARGET_NR_lchown32 11435 case TARGET_NR_lchown32: 11436 if (!(p = lock_user_string(arg1))) 11437 return -TARGET_EFAULT; 11438 ret = get_errno(lchown(p, arg2, arg3)); 11439 unlock_user(p, arg1, 0); 11440 return ret; 11441 #endif 11442 #ifdef TARGET_NR_getuid32 11443 case TARGET_NR_getuid32: 11444 return get_errno(getuid()); 11445 #endif 11446 11447 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA) 11448 /* Alpha specific */ 11449 case TARGET_NR_getxuid: 11450 { 11451 uid_t euid; 11452 euid=geteuid(); 11453 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid; 11454 } 11455 return get_errno(getuid()); 11456 #endif 11457 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA) 11458 /* Alpha specific */ 11459 case TARGET_NR_getxgid: 11460 { 11461 uid_t egid; 11462 egid=getegid(); 11463 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid; 11464 } 11465 return get_errno(getgid()); 11466 #endif 11467 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA) 11468 /* Alpha specific */ 11469 case TARGET_NR_osf_getsysinfo: 11470 ret = -TARGET_EOPNOTSUPP; 11471 switch (arg1) { 11472 case TARGET_GSI_IEEE_FP_CONTROL: 11473 { 11474 uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env); 11475 uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr; 11476 11477 swcr &= ~SWCR_STATUS_MASK; 11478 swcr |= (fpcr >> 35) & SWCR_STATUS_MASK; 11479 11480 if (put_user_u64 (swcr, arg2)) 11481 return -TARGET_EFAULT; 11482 ret = 0; 11483 } 11484 break; 11485 11486 /* case GSI_IEEE_STATE_AT_SIGNAL: 11487 -- Not implemented in linux kernel. 11488 case GSI_UACPROC: 11489 -- Retrieves current unaligned access state; not much used. 11490 case GSI_PROC_TYPE: 11491 -- Retrieves implver information; surely not used. 11492 case GSI_GET_HWRPB: 11493 -- Grabs a copy of the HWRPB; surely not used. 11494 */ 11495 } 11496 return ret; 11497 #endif 11498 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA) 11499 /* Alpha specific */ 11500 case TARGET_NR_osf_setsysinfo: 11501 ret = -TARGET_EOPNOTSUPP; 11502 switch (arg1) { 11503 case TARGET_SSI_IEEE_FP_CONTROL: 11504 { 11505 uint64_t swcr, fpcr; 11506 11507 if (get_user_u64 (swcr, arg2)) { 11508 return -TARGET_EFAULT; 11509 } 11510 11511 /* 11512 * The kernel calls swcr_update_status to update the 11513 * status bits from the fpcr at every point that it 11514 * could be queried. Therefore, we store the status 11515 * bits only in FPCR. 11516 */ 11517 ((CPUAlphaState *)cpu_env)->swcr 11518 = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK); 11519 11520 fpcr = cpu_alpha_load_fpcr(cpu_env); 11521 fpcr &= ((uint64_t)FPCR_DYN_MASK << 32); 11522 fpcr |= alpha_ieee_swcr_to_fpcr(swcr); 11523 cpu_alpha_store_fpcr(cpu_env, fpcr); 11524 ret = 0; 11525 } 11526 break; 11527 11528 case TARGET_SSI_IEEE_RAISE_EXCEPTION: 11529 { 11530 uint64_t exc, fpcr, fex; 11531 11532 if (get_user_u64(exc, arg2)) { 11533 return -TARGET_EFAULT; 11534 } 11535 exc &= SWCR_STATUS_MASK; 11536 fpcr = cpu_alpha_load_fpcr(cpu_env); 11537 11538 /* Old exceptions are not signaled. */ 11539 fex = alpha_ieee_fpcr_to_swcr(fpcr); 11540 fex = exc & ~fex; 11541 fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT; 11542 fex &= ((CPUArchState *)cpu_env)->swcr; 11543 11544 /* Update the hardware fpcr. */ 11545 fpcr |= alpha_ieee_swcr_to_fpcr(exc); 11546 cpu_alpha_store_fpcr(cpu_env, fpcr); 11547 11548 if (fex) { 11549 int si_code = TARGET_FPE_FLTUNK; 11550 target_siginfo_t info; 11551 11552 if (fex & SWCR_TRAP_ENABLE_DNO) { 11553 si_code = TARGET_FPE_FLTUND; 11554 } 11555 if (fex & SWCR_TRAP_ENABLE_INE) { 11556 si_code = TARGET_FPE_FLTRES; 11557 } 11558 if (fex & SWCR_TRAP_ENABLE_UNF) { 11559 si_code = TARGET_FPE_FLTUND; 11560 } 11561 if (fex & SWCR_TRAP_ENABLE_OVF) { 11562 si_code = TARGET_FPE_FLTOVF; 11563 } 11564 if (fex & SWCR_TRAP_ENABLE_DZE) { 11565 si_code = TARGET_FPE_FLTDIV; 11566 } 11567 if (fex & SWCR_TRAP_ENABLE_INV) { 11568 si_code = TARGET_FPE_FLTINV; 11569 } 11570 11571 info.si_signo = SIGFPE; 11572 info.si_errno = 0; 11573 info.si_code = si_code; 11574 info._sifields._sigfault._addr 11575 = ((CPUArchState *)cpu_env)->pc; 11576 queue_signal((CPUArchState *)cpu_env, info.si_signo, 11577 QEMU_SI_FAULT, &info); 11578 } 11579 ret = 0; 11580 } 11581 break; 11582 11583 /* case SSI_NVPAIRS: 11584 -- Used with SSIN_UACPROC to enable unaligned accesses. 11585 case SSI_IEEE_STATE_AT_SIGNAL: 11586 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL: 11587 -- Not implemented in linux kernel 11588 */ 11589 } 11590 return ret; 11591 #endif 11592 #ifdef TARGET_NR_osf_sigprocmask 11593 /* Alpha specific. */ 11594 case TARGET_NR_osf_sigprocmask: 11595 { 11596 abi_ulong mask; 11597 int how; 11598 sigset_t set, oldset; 11599 11600 switch(arg1) { 11601 case TARGET_SIG_BLOCK: 11602 how = SIG_BLOCK; 11603 break; 11604 case TARGET_SIG_UNBLOCK: 11605 how = SIG_UNBLOCK; 11606 break; 11607 case TARGET_SIG_SETMASK: 11608 how = SIG_SETMASK; 11609 break; 11610 default: 11611 return -TARGET_EINVAL; 11612 } 11613 mask = arg2; 11614 target_to_host_old_sigset(&set, &mask); 11615 ret = do_sigprocmask(how, &set, &oldset); 11616 if (!ret) { 11617 host_to_target_old_sigset(&mask, &oldset); 11618 ret = mask; 11619 } 11620 } 11621 return ret; 11622 #endif 11623 11624 #ifdef TARGET_NR_getgid32 11625 case TARGET_NR_getgid32: 11626 return get_errno(getgid()); 11627 #endif 11628 #ifdef TARGET_NR_geteuid32 11629 case TARGET_NR_geteuid32: 11630 return get_errno(geteuid()); 11631 #endif 11632 #ifdef TARGET_NR_getegid32 11633 case TARGET_NR_getegid32: 11634 return get_errno(getegid()); 11635 #endif 11636 #ifdef TARGET_NR_setreuid32 11637 case TARGET_NR_setreuid32: 11638 return get_errno(setreuid(arg1, arg2)); 11639 #endif 11640 #ifdef TARGET_NR_setregid32 11641 case TARGET_NR_setregid32: 11642 return get_errno(setregid(arg1, arg2)); 11643 #endif 11644 #ifdef TARGET_NR_getgroups32 11645 case TARGET_NR_getgroups32: 11646 { 11647 int gidsetsize = arg1; 11648 uint32_t *target_grouplist; 11649 gid_t *grouplist; 11650 int i; 11651 11652 grouplist = alloca(gidsetsize * sizeof(gid_t)); 11653 ret = get_errno(getgroups(gidsetsize, grouplist)); 11654 if (gidsetsize == 0) 11655 return ret; 11656 if (!is_error(ret)) { 11657 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0); 11658 if (!target_grouplist) { 11659 return -TARGET_EFAULT; 11660 } 11661 for(i = 0;i < ret; i++) 11662 target_grouplist[i] = tswap32(grouplist[i]); 11663 unlock_user(target_grouplist, arg2, gidsetsize * 4); 11664 } 11665 } 11666 return ret; 11667 #endif 11668 #ifdef TARGET_NR_setgroups32 11669 case TARGET_NR_setgroups32: 11670 { 11671 int gidsetsize = arg1; 11672 uint32_t *target_grouplist; 11673 gid_t *grouplist; 11674 int i; 11675 11676 grouplist = alloca(gidsetsize * sizeof(gid_t)); 11677 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1); 11678 if (!target_grouplist) { 11679 return -TARGET_EFAULT; 11680 } 11681 for(i = 0;i < gidsetsize; i++) 11682 grouplist[i] = tswap32(target_grouplist[i]); 11683 unlock_user(target_grouplist, arg2, 0); 11684 return get_errno(setgroups(gidsetsize, grouplist)); 11685 } 11686 #endif 11687 #ifdef TARGET_NR_fchown32 11688 case TARGET_NR_fchown32: 11689 return get_errno(fchown(arg1, arg2, arg3)); 11690 #endif 11691 #ifdef TARGET_NR_setresuid32 11692 case TARGET_NR_setresuid32: 11693 return get_errno(sys_setresuid(arg1, arg2, arg3)); 11694 #endif 11695 #ifdef TARGET_NR_getresuid32 11696 case TARGET_NR_getresuid32: 11697 { 11698 uid_t ruid, euid, suid; 11699 ret = get_errno(getresuid(&ruid, &euid, &suid)); 11700 if (!is_error(ret)) { 11701 if (put_user_u32(ruid, arg1) 11702 || put_user_u32(euid, arg2) 11703 || put_user_u32(suid, arg3)) 11704 return -TARGET_EFAULT; 11705 } 11706 } 11707 return ret; 11708 #endif 11709 #ifdef TARGET_NR_setresgid32 11710 case TARGET_NR_setresgid32: 11711 return get_errno(sys_setresgid(arg1, arg2, arg3)); 11712 #endif 11713 #ifdef TARGET_NR_getresgid32 11714 case TARGET_NR_getresgid32: 11715 { 11716 gid_t rgid, egid, sgid; 11717 ret = get_errno(getresgid(&rgid, &egid, &sgid)); 11718 if (!is_error(ret)) { 11719 if (put_user_u32(rgid, arg1) 11720 || put_user_u32(egid, arg2) 11721 || put_user_u32(sgid, arg3)) 11722 return -TARGET_EFAULT; 11723 } 11724 } 11725 return ret; 11726 #endif 11727 #ifdef TARGET_NR_chown32 11728 case TARGET_NR_chown32: 11729 if (!(p = lock_user_string(arg1))) 11730 return -TARGET_EFAULT; 11731 ret = get_errno(chown(p, arg2, arg3)); 11732 unlock_user(p, arg1, 0); 11733 return ret; 11734 #endif 11735 #ifdef TARGET_NR_setuid32 11736 case TARGET_NR_setuid32: 11737 return get_errno(sys_setuid(arg1)); 11738 #endif 11739 #ifdef TARGET_NR_setgid32 11740 case TARGET_NR_setgid32: 11741 return get_errno(sys_setgid(arg1)); 11742 #endif 11743 #ifdef TARGET_NR_setfsuid32 11744 case TARGET_NR_setfsuid32: 11745 return get_errno(setfsuid(arg1)); 11746 #endif 11747 #ifdef TARGET_NR_setfsgid32 11748 case TARGET_NR_setfsgid32: 11749 return get_errno(setfsgid(arg1)); 11750 #endif 11751 #ifdef TARGET_NR_mincore 11752 case TARGET_NR_mincore: 11753 { 11754 void *a = lock_user(VERIFY_READ, arg1, arg2, 0); 11755 if (!a) { 11756 return -TARGET_ENOMEM; 11757 } 11758 p = lock_user_string(arg3); 11759 if (!p) { 11760 ret = -TARGET_EFAULT; 11761 } else { 11762 ret = get_errno(mincore(a, arg2, p)); 11763 unlock_user(p, arg3, ret); 11764 } 11765 unlock_user(a, arg1, 0); 11766 } 11767 return ret; 11768 #endif 11769 #ifdef TARGET_NR_arm_fadvise64_64 11770 case TARGET_NR_arm_fadvise64_64: 11771 /* arm_fadvise64_64 looks like fadvise64_64 but 11772 * with different argument order: fd, advice, offset, len 11773 * rather than the usual fd, offset, len, advice. 11774 * Note that offset and len are both 64-bit so appear as 11775 * pairs of 32-bit registers. 11776 */ 11777 ret = posix_fadvise(arg1, target_offset64(arg3, arg4), 11778 target_offset64(arg5, arg6), arg2); 11779 return -host_to_target_errno(ret); 11780 #endif 11781 11782 #if TARGET_ABI_BITS == 32 11783 11784 #ifdef TARGET_NR_fadvise64_64 11785 case TARGET_NR_fadvise64_64: 11786 #if defined(TARGET_PPC) || defined(TARGET_XTENSA) 11787 /* 6 args: fd, advice, offset (high, low), len (high, low) */ 11788 ret = arg2; 11789 arg2 = arg3; 11790 arg3 = arg4; 11791 arg4 = arg5; 11792 arg5 = arg6; 11793 arg6 = ret; 11794 #else 11795 /* 6 args: fd, offset (high, low), len (high, low), advice */ 11796 if (regpairs_aligned(cpu_env, num)) { 11797 /* offset is in (3,4), len in (5,6) and advice in 7 */ 11798 arg2 = arg3; 11799 arg3 = arg4; 11800 arg4 = arg5; 11801 arg5 = arg6; 11802 arg6 = arg7; 11803 } 11804 #endif 11805 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), 11806 target_offset64(arg4, arg5), arg6); 11807 return -host_to_target_errno(ret); 11808 #endif 11809 11810 #ifdef TARGET_NR_fadvise64 11811 case TARGET_NR_fadvise64: 11812 /* 5 args: fd, offset (high, low), len, advice */ 11813 if (regpairs_aligned(cpu_env, num)) { 11814 /* offset is in (3,4), len in 5 and advice in 6 */ 11815 arg2 = arg3; 11816 arg3 = arg4; 11817 arg4 = arg5; 11818 arg5 = arg6; 11819 } 11820 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5); 11821 return -host_to_target_errno(ret); 11822 #endif 11823 11824 #else /* not a 32-bit ABI */ 11825 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64) 11826 #ifdef TARGET_NR_fadvise64_64 11827 case TARGET_NR_fadvise64_64: 11828 #endif 11829 #ifdef TARGET_NR_fadvise64 11830 case TARGET_NR_fadvise64: 11831 #endif 11832 #ifdef TARGET_S390X 11833 switch (arg4) { 11834 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */ 11835 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */ 11836 case 6: arg4 = POSIX_FADV_DONTNEED; break; 11837 case 7: arg4 = POSIX_FADV_NOREUSE; break; 11838 default: break; 11839 } 11840 #endif 11841 return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4)); 11842 #endif 11843 #endif /* end of 64-bit ABI fadvise handling */ 11844 11845 #ifdef TARGET_NR_madvise 11846 case TARGET_NR_madvise: 11847 /* A straight passthrough may not be safe because qemu sometimes 11848 turns private file-backed mappings into anonymous mappings. 11849 This will break MADV_DONTNEED. 11850 This is a hint, so ignoring and returning success is ok. */ 11851 return 0; 11852 #endif 11853 #ifdef TARGET_NR_fcntl64 11854 case TARGET_NR_fcntl64: 11855 { 11856 int cmd; 11857 struct flock64 fl; 11858 from_flock64_fn *copyfrom = copy_from_user_flock64; 11859 to_flock64_fn *copyto = copy_to_user_flock64; 11860 11861 #ifdef TARGET_ARM 11862 if (!((CPUARMState *)cpu_env)->eabi) { 11863 copyfrom = copy_from_user_oabi_flock64; 11864 copyto = copy_to_user_oabi_flock64; 11865 } 11866 #endif 11867 11868 cmd = target_to_host_fcntl_cmd(arg2); 11869 if (cmd == -TARGET_EINVAL) { 11870 return cmd; 11871 } 11872 11873 switch(arg2) { 11874 case TARGET_F_GETLK64: 11875 ret = copyfrom(&fl, arg3); 11876 if (ret) { 11877 break; 11878 } 11879 ret = get_errno(safe_fcntl(arg1, cmd, &fl)); 11880 if (ret == 0) { 11881 ret = copyto(arg3, &fl); 11882 } 11883 break; 11884 11885 case TARGET_F_SETLK64: 11886 case TARGET_F_SETLKW64: 11887 ret = copyfrom(&fl, arg3); 11888 if (ret) { 11889 break; 11890 } 11891 ret = get_errno(safe_fcntl(arg1, cmd, &fl)); 11892 break; 11893 default: 11894 ret = do_fcntl(arg1, arg2, arg3); 11895 break; 11896 } 11897 return ret; 11898 } 11899 #endif 11900 #ifdef TARGET_NR_cacheflush 11901 case TARGET_NR_cacheflush: 11902 /* self-modifying code is handled automatically, so nothing needed */ 11903 return 0; 11904 #endif 11905 #ifdef TARGET_NR_getpagesize 11906 case TARGET_NR_getpagesize: 11907 return TARGET_PAGE_SIZE; 11908 #endif 11909 case TARGET_NR_gettid: 11910 return get_errno(sys_gettid()); 11911 #ifdef TARGET_NR_readahead 11912 case TARGET_NR_readahead: 11913 #if TARGET_ABI_BITS == 32 11914 if (regpairs_aligned(cpu_env, num)) { 11915 arg2 = arg3; 11916 arg3 = arg4; 11917 arg4 = arg5; 11918 } 11919 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4)); 11920 #else 11921 ret = get_errno(readahead(arg1, arg2, arg3)); 11922 #endif 11923 return ret; 11924 #endif 11925 #ifdef CONFIG_ATTR 11926 #ifdef TARGET_NR_setxattr 11927 case TARGET_NR_listxattr: 11928 case TARGET_NR_llistxattr: 11929 { 11930 void *p, *b = 0; 11931 if (arg2) { 11932 b = lock_user(VERIFY_WRITE, arg2, arg3, 0); 11933 if (!b) { 11934 return -TARGET_EFAULT; 11935 } 11936 } 11937 p = lock_user_string(arg1); 11938 if (p) { 11939 if (num == TARGET_NR_listxattr) { 11940 ret = get_errno(listxattr(p, b, arg3)); 11941 } else { 11942 ret = get_errno(llistxattr(p, b, arg3)); 11943 } 11944 } else { 11945 ret = -TARGET_EFAULT; 11946 } 11947 unlock_user(p, arg1, 0); 11948 unlock_user(b, arg2, arg3); 11949 return ret; 11950 } 11951 case TARGET_NR_flistxattr: 11952 { 11953 void *b = 0; 11954 if (arg2) { 11955 b = lock_user(VERIFY_WRITE, arg2, arg3, 0); 11956 if (!b) { 11957 return -TARGET_EFAULT; 11958 } 11959 } 11960 ret = get_errno(flistxattr(arg1, b, arg3)); 11961 unlock_user(b, arg2, arg3); 11962 return ret; 11963 } 11964 case TARGET_NR_setxattr: 11965 case TARGET_NR_lsetxattr: 11966 { 11967 void *p, *n, *v = 0; 11968 if (arg3) { 11969 v = lock_user(VERIFY_READ, arg3, arg4, 1); 11970 if (!v) { 11971 return -TARGET_EFAULT; 11972 } 11973 } 11974 p = lock_user_string(arg1); 11975 n = lock_user_string(arg2); 11976 if (p && n) { 11977 if (num == TARGET_NR_setxattr) { 11978 ret = get_errno(setxattr(p, n, v, arg4, arg5)); 11979 } else { 11980 ret = get_errno(lsetxattr(p, n, v, arg4, arg5)); 11981 } 11982 } else { 11983 ret = -TARGET_EFAULT; 11984 } 11985 unlock_user(p, arg1, 0); 11986 unlock_user(n, arg2, 0); 11987 unlock_user(v, arg3, 0); 11988 } 11989 return ret; 11990 case TARGET_NR_fsetxattr: 11991 { 11992 void *n, *v = 0; 11993 if (arg3) { 11994 v = lock_user(VERIFY_READ, arg3, arg4, 1); 11995 if (!v) { 11996 return -TARGET_EFAULT; 11997 } 11998 } 11999 n = lock_user_string(arg2); 12000 if (n) { 12001 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5)); 12002 } else { 12003 ret = -TARGET_EFAULT; 12004 } 12005 unlock_user(n, arg2, 0); 12006 unlock_user(v, arg3, 0); 12007 } 12008 return ret; 12009 case TARGET_NR_getxattr: 12010 case TARGET_NR_lgetxattr: 12011 { 12012 void *p, *n, *v = 0; 12013 if (arg3) { 12014 v = lock_user(VERIFY_WRITE, arg3, arg4, 0); 12015 if (!v) { 12016 return -TARGET_EFAULT; 12017 } 12018 } 12019 p = lock_user_string(arg1); 12020 n = lock_user_string(arg2); 12021 if (p && n) { 12022 if (num == TARGET_NR_getxattr) { 12023 ret = get_errno(getxattr(p, n, v, arg4)); 12024 } else { 12025 ret = get_errno(lgetxattr(p, n, v, arg4)); 12026 } 12027 } else { 12028 ret = -TARGET_EFAULT; 12029 } 12030 unlock_user(p, arg1, 0); 12031 unlock_user(n, arg2, 0); 12032 unlock_user(v, arg3, arg4); 12033 } 12034 return ret; 12035 case TARGET_NR_fgetxattr: 12036 { 12037 void *n, *v = 0; 12038 if (arg3) { 12039 v = lock_user(VERIFY_WRITE, arg3, arg4, 0); 12040 if (!v) { 12041 return -TARGET_EFAULT; 12042 } 12043 } 12044 n = lock_user_string(arg2); 12045 if (n) { 12046 ret = get_errno(fgetxattr(arg1, n, v, arg4)); 12047 } else { 12048 ret = -TARGET_EFAULT; 12049 } 12050 unlock_user(n, arg2, 0); 12051 unlock_user(v, arg3, arg4); 12052 } 12053 return ret; 12054 case TARGET_NR_removexattr: 12055 case TARGET_NR_lremovexattr: 12056 { 12057 void *p, *n; 12058 p = lock_user_string(arg1); 12059 n = lock_user_string(arg2); 12060 if (p && n) { 12061 if (num == TARGET_NR_removexattr) { 12062 ret = get_errno(removexattr(p, n)); 12063 } else { 12064 ret = get_errno(lremovexattr(p, n)); 12065 } 12066 } else { 12067 ret = -TARGET_EFAULT; 12068 } 12069 unlock_user(p, arg1, 0); 12070 unlock_user(n, arg2, 0); 12071 } 12072 return ret; 12073 case TARGET_NR_fremovexattr: 12074 { 12075 void *n; 12076 n = lock_user_string(arg2); 12077 if (n) { 12078 ret = get_errno(fremovexattr(arg1, n)); 12079 } else { 12080 ret = -TARGET_EFAULT; 12081 } 12082 unlock_user(n, arg2, 0); 12083 } 12084 return ret; 12085 #endif 12086 #endif /* CONFIG_ATTR */ 12087 #ifdef TARGET_NR_set_thread_area 12088 case TARGET_NR_set_thread_area: 12089 #if defined(TARGET_MIPS) 12090 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1; 12091 return 0; 12092 #elif defined(TARGET_CRIS) 12093 if (arg1 & 0xff) 12094 ret = -TARGET_EINVAL; 12095 else { 12096 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1; 12097 ret = 0; 12098 } 12099 return ret; 12100 #elif defined(TARGET_I386) && defined(TARGET_ABI32) 12101 return do_set_thread_area(cpu_env, arg1); 12102 #elif defined(TARGET_M68K) 12103 { 12104 TaskState *ts = cpu->opaque; 12105 ts->tp_value = arg1; 12106 return 0; 12107 } 12108 #else 12109 return -TARGET_ENOSYS; 12110 #endif 12111 #endif 12112 #ifdef TARGET_NR_get_thread_area 12113 case TARGET_NR_get_thread_area: 12114 #if defined(TARGET_I386) && defined(TARGET_ABI32) 12115 return do_get_thread_area(cpu_env, arg1); 12116 #elif defined(TARGET_M68K) 12117 { 12118 TaskState *ts = cpu->opaque; 12119 return ts->tp_value; 12120 } 12121 #else 12122 return -TARGET_ENOSYS; 12123 #endif 12124 #endif 12125 #ifdef TARGET_NR_getdomainname 12126 case TARGET_NR_getdomainname: 12127 return -TARGET_ENOSYS; 12128 #endif 12129 12130 #ifdef TARGET_NR_clock_settime 12131 case TARGET_NR_clock_settime: 12132 { 12133 struct timespec ts; 12134 12135 ret = target_to_host_timespec(&ts, arg2); 12136 if (!is_error(ret)) { 12137 ret = get_errno(clock_settime(arg1, &ts)); 12138 } 12139 return ret; 12140 } 12141 #endif 12142 #ifdef TARGET_NR_clock_settime64 12143 case TARGET_NR_clock_settime64: 12144 { 12145 struct timespec ts; 12146 12147 ret = target_to_host_timespec64(&ts, arg2); 12148 if (!is_error(ret)) { 12149 ret = get_errno(clock_settime(arg1, &ts)); 12150 } 12151 return ret; 12152 } 12153 #endif 12154 #ifdef TARGET_NR_clock_gettime 12155 case TARGET_NR_clock_gettime: 12156 { 12157 struct timespec ts; 12158 ret = get_errno(clock_gettime(arg1, &ts)); 12159 if (!is_error(ret)) { 12160 ret = host_to_target_timespec(arg2, &ts); 12161 } 12162 return ret; 12163 } 12164 #endif 12165 #ifdef TARGET_NR_clock_gettime64 12166 case TARGET_NR_clock_gettime64: 12167 { 12168 struct timespec ts; 12169 ret = get_errno(clock_gettime(arg1, &ts)); 12170 if (!is_error(ret)) { 12171 ret = host_to_target_timespec64(arg2, &ts); 12172 } 12173 return ret; 12174 } 12175 #endif 12176 #ifdef TARGET_NR_clock_getres 12177 case TARGET_NR_clock_getres: 12178 { 12179 struct timespec ts; 12180 ret = get_errno(clock_getres(arg1, &ts)); 12181 if (!is_error(ret)) { 12182 host_to_target_timespec(arg2, &ts); 12183 } 12184 return ret; 12185 } 12186 #endif 12187 #ifdef TARGET_NR_clock_getres_time64 12188 case TARGET_NR_clock_getres_time64: 12189 { 12190 struct timespec ts; 12191 ret = get_errno(clock_getres(arg1, &ts)); 12192 if (!is_error(ret)) { 12193 host_to_target_timespec64(arg2, &ts); 12194 } 12195 return ret; 12196 } 12197 #endif 12198 #ifdef TARGET_NR_clock_nanosleep 12199 case TARGET_NR_clock_nanosleep: 12200 { 12201 struct timespec ts; 12202 if (target_to_host_timespec(&ts, arg3)) { 12203 return -TARGET_EFAULT; 12204 } 12205 ret = get_errno(safe_clock_nanosleep(arg1, arg2, 12206 &ts, arg4 ? &ts : NULL)); 12207 /* 12208 * if the call is interrupted by a signal handler, it fails 12209 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not 12210 * TIMER_ABSTIME, it returns the remaining unslept time in arg4. 12211 */ 12212 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME && 12213 host_to_target_timespec(arg4, &ts)) { 12214 return -TARGET_EFAULT; 12215 } 12216 12217 return ret; 12218 } 12219 #endif 12220 #ifdef TARGET_NR_clock_nanosleep_time64 12221 case TARGET_NR_clock_nanosleep_time64: 12222 { 12223 struct timespec ts; 12224 12225 if (target_to_host_timespec64(&ts, arg3)) { 12226 return -TARGET_EFAULT; 12227 } 12228 12229 ret = get_errno(safe_clock_nanosleep(arg1, arg2, 12230 &ts, arg4 ? &ts : NULL)); 12231 12232 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME && 12233 host_to_target_timespec64(arg4, &ts)) { 12234 return -TARGET_EFAULT; 12235 } 12236 return ret; 12237 } 12238 #endif 12239 12240 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address) 12241 case TARGET_NR_set_tid_address: 12242 return get_errno(set_tid_address((int *)g2h(cpu, arg1))); 12243 #endif 12244 12245 case TARGET_NR_tkill: 12246 return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2))); 12247 12248 case TARGET_NR_tgkill: 12249 return get_errno(safe_tgkill((int)arg1, (int)arg2, 12250 target_to_host_signal(arg3))); 12251 12252 #ifdef TARGET_NR_set_robust_list 12253 case TARGET_NR_set_robust_list: 12254 case TARGET_NR_get_robust_list: 12255 /* The ABI for supporting robust futexes has userspace pass 12256 * the kernel a pointer to a linked list which is updated by 12257 * userspace after the syscall; the list is walked by the kernel 12258 * when the thread exits. Since the linked list in QEMU guest 12259 * memory isn't a valid linked list for the host and we have 12260 * no way to reliably intercept the thread-death event, we can't 12261 * support these. Silently return ENOSYS so that guest userspace 12262 * falls back to a non-robust futex implementation (which should 12263 * be OK except in the corner case of the guest crashing while 12264 * holding a mutex that is shared with another process via 12265 * shared memory). 12266 */ 12267 return -TARGET_ENOSYS; 12268 #endif 12269 12270 #if defined(TARGET_NR_utimensat) 12271 case TARGET_NR_utimensat: 12272 { 12273 struct timespec *tsp, ts[2]; 12274 if (!arg3) { 12275 tsp = NULL; 12276 } else { 12277 if (target_to_host_timespec(ts, arg3)) { 12278 return -TARGET_EFAULT; 12279 } 12280 if (target_to_host_timespec(ts + 1, arg3 + 12281 sizeof(struct target_timespec))) { 12282 return -TARGET_EFAULT; 12283 } 12284 tsp = ts; 12285 } 12286 if (!arg2) 12287 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); 12288 else { 12289 if (!(p = lock_user_string(arg2))) { 12290 return -TARGET_EFAULT; 12291 } 12292 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); 12293 unlock_user(p, arg2, 0); 12294 } 12295 } 12296 return ret; 12297 #endif 12298 #ifdef TARGET_NR_utimensat_time64 12299 case TARGET_NR_utimensat_time64: 12300 { 12301 struct timespec *tsp, ts[2]; 12302 if (!arg3) { 12303 tsp = NULL; 12304 } else { 12305 if (target_to_host_timespec64(ts, arg3)) { 12306 return -TARGET_EFAULT; 12307 } 12308 if (target_to_host_timespec64(ts + 1, arg3 + 12309 sizeof(struct target__kernel_timespec))) { 12310 return -TARGET_EFAULT; 12311 } 12312 tsp = ts; 12313 } 12314 if (!arg2) 12315 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); 12316 else { 12317 p = lock_user_string(arg2); 12318 if (!p) { 12319 return -TARGET_EFAULT; 12320 } 12321 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); 12322 unlock_user(p, arg2, 0); 12323 } 12324 } 12325 return ret; 12326 #endif 12327 #ifdef TARGET_NR_futex 12328 case TARGET_NR_futex: 12329 return do_futex(cpu, arg1, arg2, arg3, arg4, arg5, arg6); 12330 #endif 12331 #ifdef TARGET_NR_futex_time64 12332 case TARGET_NR_futex_time64: 12333 return do_futex_time64(cpu, arg1, arg2, arg3, arg4, arg5, arg6); 12334 #endif 12335 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init) 12336 case TARGET_NR_inotify_init: 12337 ret = get_errno(sys_inotify_init()); 12338 if (ret >= 0) { 12339 fd_trans_register(ret, &target_inotify_trans); 12340 } 12341 return ret; 12342 #endif 12343 #ifdef CONFIG_INOTIFY1 12344 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1) 12345 case TARGET_NR_inotify_init1: 12346 ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1, 12347 fcntl_flags_tbl))); 12348 if (ret >= 0) { 12349 fd_trans_register(ret, &target_inotify_trans); 12350 } 12351 return ret; 12352 #endif 12353 #endif 12354 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) 12355 case TARGET_NR_inotify_add_watch: 12356 p = lock_user_string(arg2); 12357 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3)); 12358 unlock_user(p, arg2, 0); 12359 return ret; 12360 #endif 12361 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch) 12362 case TARGET_NR_inotify_rm_watch: 12363 return get_errno(sys_inotify_rm_watch(arg1, arg2)); 12364 #endif 12365 12366 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open) 12367 case TARGET_NR_mq_open: 12368 { 12369 struct mq_attr posix_mq_attr; 12370 struct mq_attr *pposix_mq_attr; 12371 int host_flags; 12372 12373 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl); 12374 pposix_mq_attr = NULL; 12375 if (arg4) { 12376 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) { 12377 return -TARGET_EFAULT; 12378 } 12379 pposix_mq_attr = &posix_mq_attr; 12380 } 12381 p = lock_user_string(arg1 - 1); 12382 if (!p) { 12383 return -TARGET_EFAULT; 12384 } 12385 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr)); 12386 unlock_user (p, arg1, 0); 12387 } 12388 return ret; 12389 12390 case TARGET_NR_mq_unlink: 12391 p = lock_user_string(arg1 - 1); 12392 if (!p) { 12393 return -TARGET_EFAULT; 12394 } 12395 ret = get_errno(mq_unlink(p)); 12396 unlock_user (p, arg1, 0); 12397 return ret; 12398 12399 #ifdef TARGET_NR_mq_timedsend 12400 case TARGET_NR_mq_timedsend: 12401 { 12402 struct timespec ts; 12403 12404 p = lock_user (VERIFY_READ, arg2, arg3, 1); 12405 if (arg5 != 0) { 12406 if (target_to_host_timespec(&ts, arg5)) { 12407 return -TARGET_EFAULT; 12408 } 12409 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts)); 12410 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) { 12411 return -TARGET_EFAULT; 12412 } 12413 } else { 12414 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL)); 12415 } 12416 unlock_user (p, arg2, arg3); 12417 } 12418 return ret; 12419 #endif 12420 #ifdef TARGET_NR_mq_timedsend_time64 12421 case TARGET_NR_mq_timedsend_time64: 12422 { 12423 struct timespec ts; 12424 12425 p = lock_user(VERIFY_READ, arg2, arg3, 1); 12426 if (arg5 != 0) { 12427 if (target_to_host_timespec64(&ts, arg5)) { 12428 return -TARGET_EFAULT; 12429 } 12430 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts)); 12431 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) { 12432 return -TARGET_EFAULT; 12433 } 12434 } else { 12435 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL)); 12436 } 12437 unlock_user(p, arg2, arg3); 12438 } 12439 return ret; 12440 #endif 12441 12442 #ifdef TARGET_NR_mq_timedreceive 12443 case TARGET_NR_mq_timedreceive: 12444 { 12445 struct timespec ts; 12446 unsigned int prio; 12447 12448 p = lock_user (VERIFY_READ, arg2, arg3, 1); 12449 if (arg5 != 0) { 12450 if (target_to_host_timespec(&ts, arg5)) { 12451 return -TARGET_EFAULT; 12452 } 12453 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, 12454 &prio, &ts)); 12455 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) { 12456 return -TARGET_EFAULT; 12457 } 12458 } else { 12459 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, 12460 &prio, NULL)); 12461 } 12462 unlock_user (p, arg2, arg3); 12463 if (arg4 != 0) 12464 put_user_u32(prio, arg4); 12465 } 12466 return ret; 12467 #endif 12468 #ifdef TARGET_NR_mq_timedreceive_time64 12469 case TARGET_NR_mq_timedreceive_time64: 12470 { 12471 struct timespec ts; 12472 unsigned int prio; 12473 12474 p = lock_user(VERIFY_READ, arg2, arg3, 1); 12475 if (arg5 != 0) { 12476 if (target_to_host_timespec64(&ts, arg5)) { 12477 return -TARGET_EFAULT; 12478 } 12479 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, 12480 &prio, &ts)); 12481 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) { 12482 return -TARGET_EFAULT; 12483 } 12484 } else { 12485 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3, 12486 &prio, NULL)); 12487 } 12488 unlock_user(p, arg2, arg3); 12489 if (arg4 != 0) { 12490 put_user_u32(prio, arg4); 12491 } 12492 } 12493 return ret; 12494 #endif 12495 12496 /* Not implemented for now... */ 12497 /* case TARGET_NR_mq_notify: */ 12498 /* break; */ 12499 12500 case TARGET_NR_mq_getsetattr: 12501 { 12502 struct mq_attr posix_mq_attr_in, posix_mq_attr_out; 12503 ret = 0; 12504 if (arg2 != 0) { 12505 copy_from_user_mq_attr(&posix_mq_attr_in, arg2); 12506 ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in, 12507 &posix_mq_attr_out)); 12508 } else if (arg3 != 0) { 12509 ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out)); 12510 } 12511 if (ret == 0 && arg3 != 0) { 12512 copy_to_user_mq_attr(arg3, &posix_mq_attr_out); 12513 } 12514 } 12515 return ret; 12516 #endif 12517 12518 #ifdef CONFIG_SPLICE 12519 #ifdef TARGET_NR_tee 12520 case TARGET_NR_tee: 12521 { 12522 ret = get_errno(tee(arg1,arg2,arg3,arg4)); 12523 } 12524 return ret; 12525 #endif 12526 #ifdef TARGET_NR_splice 12527 case TARGET_NR_splice: 12528 { 12529 loff_t loff_in, loff_out; 12530 loff_t *ploff_in = NULL, *ploff_out = NULL; 12531 if (arg2) { 12532 if (get_user_u64(loff_in, arg2)) { 12533 return -TARGET_EFAULT; 12534 } 12535 ploff_in = &loff_in; 12536 } 12537 if (arg4) { 12538 if (get_user_u64(loff_out, arg4)) { 12539 return -TARGET_EFAULT; 12540 } 12541 ploff_out = &loff_out; 12542 } 12543 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6)); 12544 if (arg2) { 12545 if (put_user_u64(loff_in, arg2)) { 12546 return -TARGET_EFAULT; 12547 } 12548 } 12549 if (arg4) { 12550 if (put_user_u64(loff_out, arg4)) { 12551 return -TARGET_EFAULT; 12552 } 12553 } 12554 } 12555 return ret; 12556 #endif 12557 #ifdef TARGET_NR_vmsplice 12558 case TARGET_NR_vmsplice: 12559 { 12560 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); 12561 if (vec != NULL) { 12562 ret = get_errno(vmsplice(arg1, vec, arg3, arg4)); 12563 unlock_iovec(vec, arg2, arg3, 0); 12564 } else { 12565 ret = -host_to_target_errno(errno); 12566 } 12567 } 12568 return ret; 12569 #endif 12570 #endif /* CONFIG_SPLICE */ 12571 #ifdef CONFIG_EVENTFD 12572 #if defined(TARGET_NR_eventfd) 12573 case TARGET_NR_eventfd: 12574 ret = get_errno(eventfd(arg1, 0)); 12575 if (ret >= 0) { 12576 fd_trans_register(ret, &target_eventfd_trans); 12577 } 12578 return ret; 12579 #endif 12580 #if defined(TARGET_NR_eventfd2) 12581 case TARGET_NR_eventfd2: 12582 { 12583 int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)); 12584 if (arg2 & TARGET_O_NONBLOCK) { 12585 host_flags |= O_NONBLOCK; 12586 } 12587 if (arg2 & TARGET_O_CLOEXEC) { 12588 host_flags |= O_CLOEXEC; 12589 } 12590 ret = get_errno(eventfd(arg1, host_flags)); 12591 if (ret >= 0) { 12592 fd_trans_register(ret, &target_eventfd_trans); 12593 } 12594 return ret; 12595 } 12596 #endif 12597 #endif /* CONFIG_EVENTFD */ 12598 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate) 12599 case TARGET_NR_fallocate: 12600 #if TARGET_ABI_BITS == 32 12601 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4), 12602 target_offset64(arg5, arg6))); 12603 #else 12604 ret = get_errno(fallocate(arg1, arg2, arg3, arg4)); 12605 #endif 12606 return ret; 12607 #endif 12608 #if defined(CONFIG_SYNC_FILE_RANGE) 12609 #if defined(TARGET_NR_sync_file_range) 12610 case TARGET_NR_sync_file_range: 12611 #if TARGET_ABI_BITS == 32 12612 #if defined(TARGET_MIPS) 12613 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), 12614 target_offset64(arg5, arg6), arg7)); 12615 #else 12616 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3), 12617 target_offset64(arg4, arg5), arg6)); 12618 #endif /* !TARGET_MIPS */ 12619 #else 12620 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4)); 12621 #endif 12622 return ret; 12623 #endif 12624 #if defined(TARGET_NR_sync_file_range2) || \ 12625 defined(TARGET_NR_arm_sync_file_range) 12626 #if defined(TARGET_NR_sync_file_range2) 12627 case TARGET_NR_sync_file_range2: 12628 #endif 12629 #if defined(TARGET_NR_arm_sync_file_range) 12630 case TARGET_NR_arm_sync_file_range: 12631 #endif 12632 /* This is like sync_file_range but the arguments are reordered */ 12633 #if TARGET_ABI_BITS == 32 12634 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4), 12635 target_offset64(arg5, arg6), arg2)); 12636 #else 12637 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2)); 12638 #endif 12639 return ret; 12640 #endif 12641 #endif 12642 #if defined(TARGET_NR_signalfd4) 12643 case TARGET_NR_signalfd4: 12644 return do_signalfd4(arg1, arg2, arg4); 12645 #endif 12646 #if defined(TARGET_NR_signalfd) 12647 case TARGET_NR_signalfd: 12648 return do_signalfd4(arg1, arg2, 0); 12649 #endif 12650 #if defined(CONFIG_EPOLL) 12651 #if defined(TARGET_NR_epoll_create) 12652 case TARGET_NR_epoll_create: 12653 return get_errno(epoll_create(arg1)); 12654 #endif 12655 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1) 12656 case TARGET_NR_epoll_create1: 12657 return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl))); 12658 #endif 12659 #if defined(TARGET_NR_epoll_ctl) 12660 case TARGET_NR_epoll_ctl: 12661 { 12662 struct epoll_event ep; 12663 struct epoll_event *epp = 0; 12664 if (arg4) { 12665 if (arg2 != EPOLL_CTL_DEL) { 12666 struct target_epoll_event *target_ep; 12667 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) { 12668 return -TARGET_EFAULT; 12669 } 12670 ep.events = tswap32(target_ep->events); 12671 /* 12672 * The epoll_data_t union is just opaque data to the kernel, 12673 * so we transfer all 64 bits across and need not worry what 12674 * actual data type it is. 12675 */ 12676 ep.data.u64 = tswap64(target_ep->data.u64); 12677 unlock_user_struct(target_ep, arg4, 0); 12678 } 12679 /* 12680 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a 12681 * non-null pointer, even though this argument is ignored. 12682 * 12683 */ 12684 epp = &ep; 12685 } 12686 return get_errno(epoll_ctl(arg1, arg2, arg3, epp)); 12687 } 12688 #endif 12689 12690 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait) 12691 #if defined(TARGET_NR_epoll_wait) 12692 case TARGET_NR_epoll_wait: 12693 #endif 12694 #if defined(TARGET_NR_epoll_pwait) 12695 case TARGET_NR_epoll_pwait: 12696 #endif 12697 { 12698 struct target_epoll_event *target_ep; 12699 struct epoll_event *ep; 12700 int epfd = arg1; 12701 int maxevents = arg3; 12702 int timeout = arg4; 12703 12704 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) { 12705 return -TARGET_EINVAL; 12706 } 12707 12708 target_ep = lock_user(VERIFY_WRITE, arg2, 12709 maxevents * sizeof(struct target_epoll_event), 1); 12710 if (!target_ep) { 12711 return -TARGET_EFAULT; 12712 } 12713 12714 ep = g_try_new(struct epoll_event, maxevents); 12715 if (!ep) { 12716 unlock_user(target_ep, arg2, 0); 12717 return -TARGET_ENOMEM; 12718 } 12719 12720 switch (num) { 12721 #if defined(TARGET_NR_epoll_pwait) 12722 case TARGET_NR_epoll_pwait: 12723 { 12724 target_sigset_t *target_set; 12725 sigset_t _set, *set = &_set; 12726 12727 if (arg5) { 12728 if (arg6 != sizeof(target_sigset_t)) { 12729 ret = -TARGET_EINVAL; 12730 break; 12731 } 12732 12733 target_set = lock_user(VERIFY_READ, arg5, 12734 sizeof(target_sigset_t), 1); 12735 if (!target_set) { 12736 ret = -TARGET_EFAULT; 12737 break; 12738 } 12739 target_to_host_sigset(set, target_set); 12740 unlock_user(target_set, arg5, 0); 12741 } else { 12742 set = NULL; 12743 } 12744 12745 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, 12746 set, SIGSET_T_SIZE)); 12747 break; 12748 } 12749 #endif 12750 #if defined(TARGET_NR_epoll_wait) 12751 case TARGET_NR_epoll_wait: 12752 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout, 12753 NULL, 0)); 12754 break; 12755 #endif 12756 default: 12757 ret = -TARGET_ENOSYS; 12758 } 12759 if (!is_error(ret)) { 12760 int i; 12761 for (i = 0; i < ret; i++) { 12762 target_ep[i].events = tswap32(ep[i].events); 12763 target_ep[i].data.u64 = tswap64(ep[i].data.u64); 12764 } 12765 unlock_user(target_ep, arg2, 12766 ret * sizeof(struct target_epoll_event)); 12767 } else { 12768 unlock_user(target_ep, arg2, 0); 12769 } 12770 g_free(ep); 12771 return ret; 12772 } 12773 #endif 12774 #endif 12775 #ifdef TARGET_NR_prlimit64 12776 case TARGET_NR_prlimit64: 12777 { 12778 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */ 12779 struct target_rlimit64 *target_rnew, *target_rold; 12780 struct host_rlimit64 rnew, rold, *rnewp = 0; 12781 int resource = target_to_host_resource(arg2); 12782 12783 if (arg3 && (resource != RLIMIT_AS && 12784 resource != RLIMIT_DATA && 12785 resource != RLIMIT_STACK)) { 12786 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) { 12787 return -TARGET_EFAULT; 12788 } 12789 rnew.rlim_cur = tswap64(target_rnew->rlim_cur); 12790 rnew.rlim_max = tswap64(target_rnew->rlim_max); 12791 unlock_user_struct(target_rnew, arg3, 0); 12792 rnewp = &rnew; 12793 } 12794 12795 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0)); 12796 if (!is_error(ret) && arg4) { 12797 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) { 12798 return -TARGET_EFAULT; 12799 } 12800 target_rold->rlim_cur = tswap64(rold.rlim_cur); 12801 target_rold->rlim_max = tswap64(rold.rlim_max); 12802 unlock_user_struct(target_rold, arg4, 1); 12803 } 12804 return ret; 12805 } 12806 #endif 12807 #ifdef TARGET_NR_gethostname 12808 case TARGET_NR_gethostname: 12809 { 12810 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0); 12811 if (name) { 12812 ret = get_errno(gethostname(name, arg2)); 12813 unlock_user(name, arg1, arg2); 12814 } else { 12815 ret = -TARGET_EFAULT; 12816 } 12817 return ret; 12818 } 12819 #endif 12820 #ifdef TARGET_NR_atomic_cmpxchg_32 12821 case TARGET_NR_atomic_cmpxchg_32: 12822 { 12823 /* should use start_exclusive from main.c */ 12824 abi_ulong mem_value; 12825 if (get_user_u32(mem_value, arg6)) { 12826 target_siginfo_t info; 12827 info.si_signo = SIGSEGV; 12828 info.si_errno = 0; 12829 info.si_code = TARGET_SEGV_MAPERR; 12830 info._sifields._sigfault._addr = arg6; 12831 queue_signal((CPUArchState *)cpu_env, info.si_signo, 12832 QEMU_SI_FAULT, &info); 12833 ret = 0xdeadbeef; 12834 12835 } 12836 if (mem_value == arg2) 12837 put_user_u32(arg1, arg6); 12838 return mem_value; 12839 } 12840 #endif 12841 #ifdef TARGET_NR_atomic_barrier 12842 case TARGET_NR_atomic_barrier: 12843 /* Like the kernel implementation and the 12844 qemu arm barrier, no-op this? */ 12845 return 0; 12846 #endif 12847 12848 #ifdef TARGET_NR_timer_create 12849 case TARGET_NR_timer_create: 12850 { 12851 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */ 12852 12853 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL; 12854 12855 int clkid = arg1; 12856 int timer_index = next_free_host_timer(); 12857 12858 if (timer_index < 0) { 12859 ret = -TARGET_EAGAIN; 12860 } else { 12861 timer_t *phtimer = g_posix_timers + timer_index; 12862 12863 if (arg2) { 12864 phost_sevp = &host_sevp; 12865 ret = target_to_host_sigevent(phost_sevp, arg2); 12866 if (ret != 0) { 12867 return ret; 12868 } 12869 } 12870 12871 ret = get_errno(timer_create(clkid, phost_sevp, phtimer)); 12872 if (ret) { 12873 phtimer = NULL; 12874 } else { 12875 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) { 12876 return -TARGET_EFAULT; 12877 } 12878 } 12879 } 12880 return ret; 12881 } 12882 #endif 12883 12884 #ifdef TARGET_NR_timer_settime 12885 case TARGET_NR_timer_settime: 12886 { 12887 /* args: timer_t timerid, int flags, const struct itimerspec *new_value, 12888 * struct itimerspec * old_value */ 12889 target_timer_t timerid = get_timer_id(arg1); 12890 12891 if (timerid < 0) { 12892 ret = timerid; 12893 } else if (arg3 == 0) { 12894 ret = -TARGET_EINVAL; 12895 } else { 12896 timer_t htimer = g_posix_timers[timerid]; 12897 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; 12898 12899 if (target_to_host_itimerspec(&hspec_new, arg3)) { 12900 return -TARGET_EFAULT; 12901 } 12902 ret = get_errno( 12903 timer_settime(htimer, arg2, &hspec_new, &hspec_old)); 12904 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) { 12905 return -TARGET_EFAULT; 12906 } 12907 } 12908 return ret; 12909 } 12910 #endif 12911 12912 #ifdef TARGET_NR_timer_settime64 12913 case TARGET_NR_timer_settime64: 12914 { 12915 target_timer_t timerid = get_timer_id(arg1); 12916 12917 if (timerid < 0) { 12918 ret = timerid; 12919 } else if (arg3 == 0) { 12920 ret = -TARGET_EINVAL; 12921 } else { 12922 timer_t htimer = g_posix_timers[timerid]; 12923 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; 12924 12925 if (target_to_host_itimerspec64(&hspec_new, arg3)) { 12926 return -TARGET_EFAULT; 12927 } 12928 ret = get_errno( 12929 timer_settime(htimer, arg2, &hspec_new, &hspec_old)); 12930 if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) { 12931 return -TARGET_EFAULT; 12932 } 12933 } 12934 return ret; 12935 } 12936 #endif 12937 12938 #ifdef TARGET_NR_timer_gettime 12939 case TARGET_NR_timer_gettime: 12940 { 12941 /* args: timer_t timerid, struct itimerspec *curr_value */ 12942 target_timer_t timerid = get_timer_id(arg1); 12943 12944 if (timerid < 0) { 12945 ret = timerid; 12946 } else if (!arg2) { 12947 ret = -TARGET_EFAULT; 12948 } else { 12949 timer_t htimer = g_posix_timers[timerid]; 12950 struct itimerspec hspec; 12951 ret = get_errno(timer_gettime(htimer, &hspec)); 12952 12953 if (host_to_target_itimerspec(arg2, &hspec)) { 12954 ret = -TARGET_EFAULT; 12955 } 12956 } 12957 return ret; 12958 } 12959 #endif 12960 12961 #ifdef TARGET_NR_timer_gettime64 12962 case TARGET_NR_timer_gettime64: 12963 { 12964 /* args: timer_t timerid, struct itimerspec64 *curr_value */ 12965 target_timer_t timerid = get_timer_id(arg1); 12966 12967 if (timerid < 0) { 12968 ret = timerid; 12969 } else if (!arg2) { 12970 ret = -TARGET_EFAULT; 12971 } else { 12972 timer_t htimer = g_posix_timers[timerid]; 12973 struct itimerspec hspec; 12974 ret = get_errno(timer_gettime(htimer, &hspec)); 12975 12976 if (host_to_target_itimerspec64(arg2, &hspec)) { 12977 ret = -TARGET_EFAULT; 12978 } 12979 } 12980 return ret; 12981 } 12982 #endif 12983 12984 #ifdef TARGET_NR_timer_getoverrun 12985 case TARGET_NR_timer_getoverrun: 12986 { 12987 /* args: timer_t timerid */ 12988 target_timer_t timerid = get_timer_id(arg1); 12989 12990 if (timerid < 0) { 12991 ret = timerid; 12992 } else { 12993 timer_t htimer = g_posix_timers[timerid]; 12994 ret = get_errno(timer_getoverrun(htimer)); 12995 } 12996 return ret; 12997 } 12998 #endif 12999 13000 #ifdef TARGET_NR_timer_delete 13001 case TARGET_NR_timer_delete: 13002 { 13003 /* args: timer_t timerid */ 13004 target_timer_t timerid = get_timer_id(arg1); 13005 13006 if (timerid < 0) { 13007 ret = timerid; 13008 } else { 13009 timer_t htimer = g_posix_timers[timerid]; 13010 ret = get_errno(timer_delete(htimer)); 13011 g_posix_timers[timerid] = 0; 13012 } 13013 return ret; 13014 } 13015 #endif 13016 13017 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD) 13018 case TARGET_NR_timerfd_create: 13019 return get_errno(timerfd_create(arg1, 13020 target_to_host_bitmask(arg2, fcntl_flags_tbl))); 13021 #endif 13022 13023 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD) 13024 case TARGET_NR_timerfd_gettime: 13025 { 13026 struct itimerspec its_curr; 13027 13028 ret = get_errno(timerfd_gettime(arg1, &its_curr)); 13029 13030 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) { 13031 return -TARGET_EFAULT; 13032 } 13033 } 13034 return ret; 13035 #endif 13036 13037 #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD) 13038 case TARGET_NR_timerfd_gettime64: 13039 { 13040 struct itimerspec its_curr; 13041 13042 ret = get_errno(timerfd_gettime(arg1, &its_curr)); 13043 13044 if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) { 13045 return -TARGET_EFAULT; 13046 } 13047 } 13048 return ret; 13049 #endif 13050 13051 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD) 13052 case TARGET_NR_timerfd_settime: 13053 { 13054 struct itimerspec its_new, its_old, *p_new; 13055 13056 if (arg3) { 13057 if (target_to_host_itimerspec(&its_new, arg3)) { 13058 return -TARGET_EFAULT; 13059 } 13060 p_new = &its_new; 13061 } else { 13062 p_new = NULL; 13063 } 13064 13065 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old)); 13066 13067 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) { 13068 return -TARGET_EFAULT; 13069 } 13070 } 13071 return ret; 13072 #endif 13073 13074 #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD) 13075 case TARGET_NR_timerfd_settime64: 13076 { 13077 struct itimerspec its_new, its_old, *p_new; 13078 13079 if (arg3) { 13080 if (target_to_host_itimerspec64(&its_new, arg3)) { 13081 return -TARGET_EFAULT; 13082 } 13083 p_new = &its_new; 13084 } else { 13085 p_new = NULL; 13086 } 13087 13088 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old)); 13089 13090 if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) { 13091 return -TARGET_EFAULT; 13092 } 13093 } 13094 return ret; 13095 #endif 13096 13097 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get) 13098 case TARGET_NR_ioprio_get: 13099 return get_errno(ioprio_get(arg1, arg2)); 13100 #endif 13101 13102 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set) 13103 case TARGET_NR_ioprio_set: 13104 return get_errno(ioprio_set(arg1, arg2, arg3)); 13105 #endif 13106 13107 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS) 13108 case TARGET_NR_setns: 13109 return get_errno(setns(arg1, arg2)); 13110 #endif 13111 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS) 13112 case TARGET_NR_unshare: 13113 return get_errno(unshare(arg1)); 13114 #endif 13115 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp) 13116 case TARGET_NR_kcmp: 13117 return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5)); 13118 #endif 13119 #ifdef TARGET_NR_swapcontext 13120 case TARGET_NR_swapcontext: 13121 /* PowerPC specific. */ 13122 return do_swapcontext(cpu_env, arg1, arg2, arg3); 13123 #endif 13124 #ifdef TARGET_NR_memfd_create 13125 case TARGET_NR_memfd_create: 13126 p = lock_user_string(arg1); 13127 if (!p) { 13128 return -TARGET_EFAULT; 13129 } 13130 ret = get_errno(memfd_create(p, arg2)); 13131 fd_trans_unregister(ret); 13132 unlock_user(p, arg1, 0); 13133 return ret; 13134 #endif 13135 #if defined TARGET_NR_membarrier && defined __NR_membarrier 13136 case TARGET_NR_membarrier: 13137 return get_errno(membarrier(arg1, arg2)); 13138 #endif 13139 13140 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range) 13141 case TARGET_NR_copy_file_range: 13142 { 13143 loff_t inoff, outoff; 13144 loff_t *pinoff = NULL, *poutoff = NULL; 13145 13146 if (arg2) { 13147 if (get_user_u64(inoff, arg2)) { 13148 return -TARGET_EFAULT; 13149 } 13150 pinoff = &inoff; 13151 } 13152 if (arg4) { 13153 if (get_user_u64(outoff, arg4)) { 13154 return -TARGET_EFAULT; 13155 } 13156 poutoff = &outoff; 13157 } 13158 /* Do not sign-extend the count parameter. */ 13159 ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff, 13160 (abi_ulong)arg5, arg6)); 13161 if (!is_error(ret) && ret > 0) { 13162 if (arg2) { 13163 if (put_user_u64(inoff, arg2)) { 13164 return -TARGET_EFAULT; 13165 } 13166 } 13167 if (arg4) { 13168 if (put_user_u64(outoff, arg4)) { 13169 return -TARGET_EFAULT; 13170 } 13171 } 13172 } 13173 } 13174 return ret; 13175 #endif 13176 13177 #if defined(TARGET_NR_pivot_root) 13178 case TARGET_NR_pivot_root: 13179 { 13180 void *p2; 13181 p = lock_user_string(arg1); /* new_root */ 13182 p2 = lock_user_string(arg2); /* put_old */ 13183 if (!p || !p2) { 13184 ret = -TARGET_EFAULT; 13185 } else { 13186 ret = get_errno(pivot_root(p, p2)); 13187 } 13188 unlock_user(p2, arg2, 0); 13189 unlock_user(p, arg1, 0); 13190 } 13191 return ret; 13192 #endif 13193 13194 default: 13195 qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); 13196 return -TARGET_ENOSYS; 13197 } 13198 return ret; 13199 } 13200 13201 abi_long do_syscall(void *cpu_env, int num, abi_long arg1, 13202 abi_long arg2, abi_long arg3, abi_long arg4, 13203 abi_long arg5, abi_long arg6, abi_long arg7, 13204 abi_long arg8) 13205 { 13206 CPUState *cpu = env_cpu(cpu_env); 13207 abi_long ret; 13208 13209 #ifdef DEBUG_ERESTARTSYS 13210 /* Debug-only code for exercising the syscall-restart code paths 13211 * in the per-architecture cpu main loops: restart every syscall 13212 * the guest makes once before letting it through. 13213 */ 13214 { 13215 static bool flag; 13216 flag = !flag; 13217 if (flag) { 13218 return -QEMU_ERESTARTSYS; 13219 } 13220 } 13221 #endif 13222 13223 record_syscall_start(cpu, num, arg1, 13224 arg2, arg3, arg4, arg5, arg6, arg7, arg8); 13225 13226 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { 13227 print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6); 13228 } 13229 13230 ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4, 13231 arg5, arg6, arg7, arg8); 13232 13233 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { 13234 print_syscall_ret(cpu_env, num, ret, arg1, arg2, 13235 arg3, arg4, arg5, arg6); 13236 } 13237 13238 record_syscall_return(cpu, num, ret); 13239 return ret; 13240 } 13241