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