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