syscall_emul.hh revision 13994
112150Sgabor.dozsa@arm.com/* 212150Sgabor.dozsa@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 312150Sgabor.dozsa@arm.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 412150Sgabor.dozsa@arm.com * All rights reserved 512150Sgabor.dozsa@arm.com * 612150Sgabor.dozsa@arm.com * The license below extends only to copyright in the software and shall 712150Sgabor.dozsa@arm.com * not be construed as granting a license to any other intellectual 812150Sgabor.dozsa@arm.com * property including but not limited to intellectual property relating 912150Sgabor.dozsa@arm.com * to a hardware implementation of the functionality of the software 1012150Sgabor.dozsa@arm.com * licensed hereunder. You may use the software subject to the license 1112150Sgabor.dozsa@arm.com * terms below provided that you ensure that this notice is replicated 1212150Sgabor.dozsa@arm.com * unmodified and in its entirety in all distributions of the software, 1312150Sgabor.dozsa@arm.com * modified or unmodified, in source code or in binary form. 1412150Sgabor.dozsa@arm.com * 1512150Sgabor.dozsa@arm.com * Copyright (c) 2003-2005 The Regents of The University of Michigan 1612150Sgabor.dozsa@arm.com * All rights reserved. 1712150Sgabor.dozsa@arm.com * 1812150Sgabor.dozsa@arm.com * Redistribution and use in source and binary forms, with or without 1912150Sgabor.dozsa@arm.com * modification, are permitted provided that the following conditions are 2012150Sgabor.dozsa@arm.com * met: redistributions of source code must retain the above copyright 2112150Sgabor.dozsa@arm.com * notice, this list of conditions and the following disclaimer; 2212150Sgabor.dozsa@arm.com * redistributions in binary form must reproduce the above copyright 2312150Sgabor.dozsa@arm.com * notice, this list of conditions and the following disclaimer in the 2412150Sgabor.dozsa@arm.com * documentation and/or other materials provided with the distribution; 2512150Sgabor.dozsa@arm.com * neither the name of the copyright holders nor the names of its 2612150Sgabor.dozsa@arm.com * contributors may be used to endorse or promote products derived from 2712150Sgabor.dozsa@arm.com * this software without specific prior written permission. 2812150Sgabor.dozsa@arm.com * 2912150Sgabor.dozsa@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3012150Sgabor.dozsa@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3112150Sgabor.dozsa@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3212150Sgabor.dozsa@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3312150Sgabor.dozsa@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3412150Sgabor.dozsa@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3512150Sgabor.dozsa@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3612150Sgabor.dozsa@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3712150Sgabor.dozsa@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3812150Sgabor.dozsa@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3912150Sgabor.dozsa@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 4012150Sgabor.dozsa@arm.com * 4112150Sgabor.dozsa@arm.com * Authors: Steve Reinhardt 4212150Sgabor.dozsa@arm.com * Kevin Lim 4312150Sgabor.dozsa@arm.com */ 4412150Sgabor.dozsa@arm.com 4512150Sgabor.dozsa@arm.com#ifndef __SIM_SYSCALL_EMUL_HH__ 4612564Sgabeblack@google.com#define __SIM_SYSCALL_EMUL_HH__ 4712564Sgabeblack@google.com 4812150Sgabor.dozsa@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 4912150Sgabor.dozsa@arm.com defined(__FreeBSD__) || defined(__CYGWIN__) || \ 5012150Sgabor.dozsa@arm.com defined(__NetBSD__)) 5112150Sgabor.dozsa@arm.com#define NO_STAT64 1 5212150Sgabor.dozsa@arm.com#else 5312150Sgabor.dozsa@arm.com#define NO_STAT64 0 5412150Sgabor.dozsa@arm.com#endif 5512150Sgabor.dozsa@arm.com 5612150Sgabor.dozsa@arm.com/// 5712150Sgabor.dozsa@arm.com/// @file syscall_emul.hh 5812150Sgabor.dozsa@arm.com/// 5912150Sgabor.dozsa@arm.com/// This file defines objects used to emulate syscalls from the target 6012150Sgabor.dozsa@arm.com/// application on the host machine. 6112150Sgabor.dozsa@arm.com 6212150Sgabor.dozsa@arm.com#if defined(__linux__) 6312150Sgabor.dozsa@arm.com#include <sys/eventfd.h> 6412150Sgabor.dozsa@arm.com#include <sys/statfs.h> 6512150Sgabor.dozsa@arm.com 6612150Sgabor.dozsa@arm.com#else 6712150Sgabor.dozsa@arm.com#include <sys/mount.h> 6812150Sgabor.dozsa@arm.com 6912150Sgabor.dozsa@arm.com#endif 7012150Sgabor.dozsa@arm.com 7112150Sgabor.dozsa@arm.com#ifdef __CYGWIN32__ 7212150Sgabor.dozsa@arm.com#include <sys/fcntl.h> 7312150Sgabor.dozsa@arm.com 7412150Sgabor.dozsa@arm.com#endif 7512150Sgabor.dozsa@arm.com#include <fcntl.h> 7612150Sgabor.dozsa@arm.com#include <net/if.h> 7712150Sgabor.dozsa@arm.com#include <poll.h> 7812150Sgabor.dozsa@arm.com#include <sys/ioctl.h> 7912150Sgabor.dozsa@arm.com#include <sys/mman.h> 8012150Sgabor.dozsa@arm.com#include <sys/socket.h> 8112150Sgabor.dozsa@arm.com#include <sys/stat.h> 8212150Sgabor.dozsa@arm.com#include <sys/time.h> 8312150Sgabor.dozsa@arm.com#include <sys/types.h> 8412150Sgabor.dozsa@arm.com#include <sys/uio.h> 8512150Sgabor.dozsa@arm.com#include <unistd.h> 8612150Sgabor.dozsa@arm.com 8712150Sgabor.dozsa@arm.com#include <cerrno> 8812150Sgabor.dozsa@arm.com#include <memory> 8912150Sgabor.dozsa@arm.com#include <string> 9012150Sgabor.dozsa@arm.com 9112150Sgabor.dozsa@arm.com#include "arch/generic/tlb.hh" 9212150Sgabor.dozsa@arm.com#include "arch/utility.hh" 9312150Sgabor.dozsa@arm.com#include "base/intmath.hh" 9412150Sgabor.dozsa@arm.com#include "base/loader/object_file.hh" 9512150Sgabor.dozsa@arm.com#include "base/logging.hh" 9612150Sgabor.dozsa@arm.com#include "base/trace.hh" 9712150Sgabor.dozsa@arm.com#include "base/types.hh" 9812150Sgabor.dozsa@arm.com#include "config/the_isa.hh" 9912150Sgabor.dozsa@arm.com#include "cpu/base.hh" 10012150Sgabor.dozsa@arm.com#include "cpu/thread_context.hh" 10112150Sgabor.dozsa@arm.com#include "mem/page_table.hh" 10212564Sgabeblack@google.com#include "params/Process.hh" 10312150Sgabor.dozsa@arm.com#include "sim/emul_driver.hh" 10412150Sgabor.dozsa@arm.com#include "sim/futex_map.hh" 10512150Sgabor.dozsa@arm.com#include "sim/process.hh" 10612150Sgabor.dozsa@arm.com#include "sim/syscall_debug_macros.hh" 10712150Sgabor.dozsa@arm.com#include "sim/syscall_desc.hh" 10812150Sgabor.dozsa@arm.com#include "sim/syscall_emul_buf.hh" 10912150Sgabor.dozsa@arm.com#include "sim/syscall_return.hh" 11012150Sgabor.dozsa@arm.com 11112150Sgabor.dozsa@arm.com#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN) 11212150Sgabor.dozsa@arm.com#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) 11312150Sgabor.dozsa@arm.com#endif 11412150Sgabor.dozsa@arm.com 11512153Sandreas.sandberg@arm.com////////////////////////////////////////////////////////////////////// 11612150Sgabor.dozsa@arm.com// 11712150Sgabor.dozsa@arm.com// The following emulation functions are generic enough that they 11812150Sgabor.dozsa@arm.com// don't need to be recompiled for different emulated OS's. They are 11912150Sgabor.dozsa@arm.com// defined in sim/syscall_emul.cc. 12012150Sgabor.dozsa@arm.com// 12112150Sgabor.dozsa@arm.com////////////////////////////////////////////////////////////////////// 12212150Sgabor.dozsa@arm.com 12312150Sgabor.dozsa@arm.comvoid warnUnsupportedOS(std::string syscall_name); 12412150Sgabor.dozsa@arm.com 12512150Sgabor.dozsa@arm.com/// Handler for unimplemented syscalls that we haven't thought about. 12612150Sgabor.dozsa@arm.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 12712150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 12812150Sgabor.dozsa@arm.com 12912150Sgabor.dozsa@arm.com/// Handler for unimplemented syscalls that we never intend to 13012150Sgabor.dozsa@arm.com/// implement (signal handling, etc.) and should not affect the correct 13112150Sgabor.dozsa@arm.com/// behavior of the program. Print a warning only if the appropriate 13212150Sgabor.dozsa@arm.com/// trace flag is enabled. Return success to the target program. 13312150Sgabor.dozsa@arm.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num, 13412150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 13512150Sgabor.dozsa@arm.com 13612150Sgabor.dozsa@arm.com// Target fallocateFunc() handler. 13712150Sgabor.dozsa@arm.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 13812150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 13912150Sgabor.dozsa@arm.com 14012150Sgabor.dozsa@arm.com/// Target exit() handler: terminate current context. 14112150Sgabor.dozsa@arm.comSyscallReturn exitFunc(SyscallDesc *desc, int num, 14212150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 14312150Sgabor.dozsa@arm.com 14412150Sgabor.dozsa@arm.com/// Target exit_group() handler: terminate simulation. (exit all threads) 14512150Sgabor.dozsa@arm.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 14612150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 14712150Sgabor.dozsa@arm.com 14812150Sgabor.dozsa@arm.com/// Target set_tid_address() handler. 14912150Sgabor.dozsa@arm.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 15012150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 15112150Sgabor.dozsa@arm.com 15212150Sgabor.dozsa@arm.com/// Target getpagesize() handler. 15312150Sgabor.dozsa@arm.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 15412150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 15512150Sgabor.dozsa@arm.com 15612150Sgabor.dozsa@arm.com/// Target brk() handler: set brk address. 15712150Sgabor.dozsa@arm.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 15812150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 15912150Sgabor.dozsa@arm.com 16012150Sgabor.dozsa@arm.com/// Target close() handler. 16112150Sgabor.dozsa@arm.comSyscallReturn closeFunc(SyscallDesc *desc, int num, 16212150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 16312150Sgabor.dozsa@arm.com 16412150Sgabor.dozsa@arm.com/// Target lseek() handler. 16512150Sgabor.dozsa@arm.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 16612150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 16712150Sgabor.dozsa@arm.com 16812150Sgabor.dozsa@arm.com/// Target _llseek() handler. 16912150Sgabor.dozsa@arm.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 17012150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 17112150Sgabor.dozsa@arm.com 17212150Sgabor.dozsa@arm.com/// Target munmap() handler. 17312150Sgabor.dozsa@arm.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 17412150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 17512150Sgabor.dozsa@arm.com 17612150Sgabor.dozsa@arm.com/// Target shutdown() handler. 17712150Sgabor.dozsa@arm.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num, 17812150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 17912150Sgabor.dozsa@arm.com 18012564Sgabeblack@google.com/// Target gethostname() handler. 18112150Sgabor.dozsa@arm.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 18212150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 18312150Sgabor.dozsa@arm.com 18412150Sgabor.dozsa@arm.com/// Target getcwd() handler. 18512150Sgabor.dozsa@arm.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 18612564Sgabeblack@google.com Process *p, ThreadContext *tc); 18712150Sgabor.dozsa@arm.com 18812150Sgabor.dozsa@arm.com/// Target readlink() handler. 18912564Sgabeblack@google.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19012150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc, 19112564Sgabeblack@google.com int index = 0); 19212150Sgabor.dozsa@arm.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19312150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 19412150Sgabor.dozsa@arm.com 19512150Sgabor.dozsa@arm.com/// Target unlink() handler. 19612150Sgabor.dozsa@arm.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 19712150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc, 19812150Sgabor.dozsa@arm.com int index); 19912150Sgabor.dozsa@arm.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 20012150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 20112150Sgabor.dozsa@arm.com 20212150Sgabor.dozsa@arm.com/// Target link() handler 20312150Sgabor.dozsa@arm.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p, 20412150Sgabor.dozsa@arm.com ThreadContext *tc); 20512150Sgabor.dozsa@arm.com 20612150Sgabor.dozsa@arm.com/// Target symlink() handler. 20712150Sgabor.dozsa@arm.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p, 20812150Sgabor.dozsa@arm.com ThreadContext *tc); 20912150Sgabor.dozsa@arm.com 21012150Sgabor.dozsa@arm.com/// Target mkdir() handler. 21112150Sgabor.dozsa@arm.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 21212150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 21312150Sgabor.dozsa@arm.com 21412150Sgabor.dozsa@arm.com/// Target mknod() handler. 21512150Sgabor.dozsa@arm.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, 21612150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 21712150Sgabor.dozsa@arm.com 21812150Sgabor.dozsa@arm.com/// Target chdir() handler. 21912150Sgabor.dozsa@arm.comSyscallReturn chdirFunc(SyscallDesc *desc, int num, 22012150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 22112150Sgabor.dozsa@arm.com 22212150Sgabor.dozsa@arm.com// Target rmdir() handler. 22312150Sgabor.dozsa@arm.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num, 22412150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 22512150Sgabor.dozsa@arm.com 22612150Sgabor.dozsa@arm.com/// Target rename() handler. 22712150Sgabor.dozsa@arm.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 22812150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 22912150Sgabor.dozsa@arm.com 23012150Sgabor.dozsa@arm.com 23112150Sgabor.dozsa@arm.com/// Target truncate() handler. 23212150Sgabor.dozsa@arm.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 23312150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 23412150Sgabor.dozsa@arm.com 23512150Sgabor.dozsa@arm.com 23612150Sgabor.dozsa@arm.com/// Target ftruncate() handler. 23712150Sgabor.dozsa@arm.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 23812150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 23912150Sgabor.dozsa@arm.com 24012150Sgabor.dozsa@arm.com 24112150Sgabor.dozsa@arm.com/// Target truncate64() handler. 24212150Sgabor.dozsa@arm.comSyscallReturn truncate64Func(SyscallDesc *desc, int num, 24312150Sgabor.dozsa@arm.com Process *p, ThreadContext *tc); 244 245/// Target ftruncate64() handler. 246SyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 247 Process *p, ThreadContext *tc); 248 249 250/// Target umask() handler. 251SyscallReturn umaskFunc(SyscallDesc *desc, int num, 252 Process *p, ThreadContext *tc); 253 254/// Target gettid() handler. 255SyscallReturn gettidFunc(SyscallDesc *desc, int num, 256 Process *p, ThreadContext *tc); 257 258/// Target chown() handler. 259SyscallReturn chownFunc(SyscallDesc *desc, int num, 260 Process *p, ThreadContext *tc); 261 262/// Target setpgid() handler. 263SyscallReturn setpgidFunc(SyscallDesc *desc, int num, 264 Process *p, ThreadContext *tc); 265 266/// Target fchown() handler. 267SyscallReturn fchownFunc(SyscallDesc *desc, int num, 268 Process *p, ThreadContext *tc); 269 270/// Target dup() handler. 271SyscallReturn dupFunc(SyscallDesc *desc, int num, 272 Process *process, ThreadContext *tc); 273 274/// Target dup2() handler. 275SyscallReturn dup2Func(SyscallDesc *desc, int num, 276 Process *process, ThreadContext *tc); 277 278/// Target fcntl() handler. 279SyscallReturn fcntlFunc(SyscallDesc *desc, int num, 280 Process *process, ThreadContext *tc); 281 282/// Target fcntl64() handler. 283SyscallReturn fcntl64Func(SyscallDesc *desc, int num, 284 Process *process, ThreadContext *tc); 285 286/// Target setuid() handler. 287SyscallReturn setuidFunc(SyscallDesc *desc, int num, 288 Process *p, ThreadContext *tc); 289 290/// Target pipe() handler. 291SyscallReturn pipeFunc(SyscallDesc *desc, int num, 292 Process *p, ThreadContext *tc); 293 294/// Internal pipe() handler. 295SyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 296 ThreadContext *tc, bool pseudoPipe); 297 298/// Target getpid() handler. 299SyscallReturn getpidFunc(SyscallDesc *desc, int num, 300 Process *p, ThreadContext *tc); 301 302// Target getpeername() handler. 303SyscallReturn getpeernameFunc(SyscallDesc *desc, int num, 304 Process *p, ThreadContext *tc); 305 306// Target bind() handler. 307SyscallReturn bindFunc(SyscallDesc *desc, int num, 308 Process *p, ThreadContext *tc); 309 310// Target listen() handler. 311SyscallReturn listenFunc(SyscallDesc *desc, int num, 312 Process *p, ThreadContext *tc); 313 314// Target connect() handler. 315SyscallReturn connectFunc(SyscallDesc *desc, int num, 316 Process *p, ThreadContext *tc); 317 318#if defined(SYS_getdents) 319// Target getdents() handler. 320SyscallReturn getdentsFunc(SyscallDesc *desc, int num, 321 Process *p, ThreadContext *tc); 322#endif 323 324#if defined(SYS_getdents64) 325// Target getdents() handler. 326SyscallReturn getdents64Func(SyscallDesc *desc, int num, 327 Process *p, ThreadContext *tc); 328#endif 329 330// Target sendto() handler. 331SyscallReturn sendtoFunc(SyscallDesc *desc, int num, 332 Process *p, ThreadContext *tc); 333 334// Target recvfrom() handler. 335SyscallReturn recvfromFunc(SyscallDesc *desc, int num, 336 Process *p, ThreadContext *tc); 337 338// Target recvmsg() handler. 339SyscallReturn recvmsgFunc(SyscallDesc *desc, int num, 340 Process *p, ThreadContext *tc); 341 342// Target sendmsg() handler. 343SyscallReturn sendmsgFunc(SyscallDesc *desc, int num, 344 Process *p, ThreadContext *tc); 345 346// Target getuid() handler. 347SyscallReturn getuidFunc(SyscallDesc *desc, int num, 348 Process *p, ThreadContext *tc); 349 350/// Target getgid() handler. 351SyscallReturn getgidFunc(SyscallDesc *desc, int num, 352 Process *p, ThreadContext *tc); 353 354/// Target getppid() handler. 355SyscallReturn getppidFunc(SyscallDesc *desc, int num, 356 Process *p, ThreadContext *tc); 357 358/// Target geteuid() handler. 359SyscallReturn geteuidFunc(SyscallDesc *desc, int num, 360 Process *p, ThreadContext *tc); 361 362/// Target getegid() handler. 363SyscallReturn getegidFunc(SyscallDesc *desc, int num, 364 Process *p, ThreadContext *tc); 365 366/// Target access() handler 367SyscallReturn accessFunc(SyscallDesc *desc, int num, 368 Process *p, ThreadContext *tc); 369SyscallReturn accessFunc(SyscallDesc *desc, int num, 370 Process *p, ThreadContext *tc, 371 int index); 372 373// Target getsockopt() handler. 374SyscallReturn getsockoptFunc(SyscallDesc *desc, int num, 375 Process *p, ThreadContext *tc); 376 377// Target setsockopt() handler. 378SyscallReturn setsockoptFunc(SyscallDesc *desc, int num, 379 Process *p, ThreadContext *tc); 380 381// Target getsockname() handler. 382SyscallReturn getsocknameFunc(SyscallDesc *desc, int num, 383 Process *p, ThreadContext *tc); 384 385/// Futex system call 386/// Implemented by Daniel Sanchez 387/// Used by printf's in multi-threaded apps 388template <class OS> 389SyscallReturn 390futexFunc(SyscallDesc *desc, int callnum, Process *process, 391 ThreadContext *tc) 392{ 393 using namespace std; 394 395 int index = 0; 396 Addr uaddr = process->getSyscallArg(tc, index); 397 int op = process->getSyscallArg(tc, index); 398 int val = process->getSyscallArg(tc, index); 399 int timeout M5_VAR_USED = process->getSyscallArg(tc, index); 400 Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index); 401 int val3 = process->getSyscallArg(tc, index); 402 403 /* 404 * Unsupported option that does not affect the correctness of the 405 * application. This is a performance optimization utilized by Linux. 406 */ 407 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 408 op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG; 409 410 FutexMap &futex_map = tc->getSystemPtr()->futexMap; 411 412 if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) { 413 // Ensure futex system call accessed atomically. 414 BufferArg buf(uaddr, sizeof(int)); 415 buf.copyIn(tc->getMemProxy()); 416 int mem_val = *(int*)buf.bufferPtr(); 417 418 /* 419 * The value in memory at uaddr is not equal with the expected val 420 * (a different thread must have changed it before the system call was 421 * invoked). In this case, we need to throw an error. 422 */ 423 if (val != mem_val) 424 return -OS::TGT_EWOULDBLOCK; 425 426 if (OS::TGT_FUTEX_WAIT) { 427 futex_map.suspend(uaddr, process->tgid(), tc); 428 } else { 429 futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3); 430 } 431 432 return 0; 433 } else if (OS::TGT_FUTEX_WAKE == op) { 434 return futex_map.wakeup(uaddr, process->tgid(), val); 435 } else if (OS::TGT_FUTEX_WAKE_BITSET == op) { 436 return futex_map.wakeup_bitset(uaddr, process->tgid(), val3); 437 } else if (OS::TGT_FUTEX_REQUEUE == op || 438 OS::TGT_FUTEX_CMP_REQUEUE == op) { 439 440 // Ensure futex system call accessed atomically. 441 BufferArg buf(uaddr, sizeof(int)); 442 buf.copyIn(tc->getMemProxy()); 443 int mem_val = *(int*)buf.bufferPtr(); 444 /* 445 * For CMP_REQUEUE, the whole operation is only started only if 446 * val3 is still the value of the futex pointed to by uaddr. 447 */ 448 if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val) 449 return -OS::TGT_EWOULDBLOCK; 450 return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2); 451 } else if (OS::TGT_FUTEX_WAKE_OP == op) { 452 /* 453 * The FUTEX_WAKE_OP operation is equivalent to executing the 454 * following code atomically and totally ordered with respect to 455 * other futex operations on any of the two supplied futex words: 456 * 457 * int oldval = *(int *) addr2; 458 * *(int *) addr2 = oldval op oparg; 459 * futex(addr1, FUTEX_WAKE, val, 0, 0, 0); 460 * if (oldval cmp cmparg) 461 * futex(addr2, FUTEX_WAKE, val2, 0, 0, 0); 462 * 463 * (op, oparg, cmp, cmparg are encoded in val3) 464 * 465 * +---+---+-----------+-----------+ 466 * |op |cmp| oparg | cmparg | 467 * +---+---+-----------+-----------+ 468 * 4 4 12 12 <== # of bits 469 * 470 * reference: http://man7.org/linux/man-pages/man2/futex.2.html 471 * 472 */ 473 // get value from simulated-space 474 BufferArg buf(uaddr2, sizeof(int)); 475 buf.copyIn(tc->getMemProxy()); 476 int oldval = *(int*)buf.bufferPtr(); 477 int newval = oldval; 478 // extract op, oparg, cmp, cmparg from val3 479 int wake_cmparg = val3 & 0xfff; 480 int wake_oparg = (val3 & 0xfff000) >> 12; 481 int wake_cmp = (val3 & 0xf000000) >> 24; 482 int wake_op = (val3 & 0xf0000000) >> 28; 483 if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1) 484 wake_oparg = (1 << wake_oparg); 485 wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT; 486 // perform operation on the value of the second futex 487 if (wake_op == OS::TGT_FUTEX_OP_SET) 488 newval = wake_oparg; 489 else if (wake_op == OS::TGT_FUTEX_OP_ADD) 490 newval += wake_oparg; 491 else if (wake_op == OS::TGT_FUTEX_OP_OR) 492 newval |= wake_oparg; 493 else if (wake_op == OS::TGT_FUTEX_OP_ANDN) 494 newval &= ~wake_oparg; 495 else if (wake_op == OS::TGT_FUTEX_OP_XOR) 496 newval ^= wake_oparg; 497 // copy updated value back to simulated-space 498 *(int*)buf.bufferPtr() = newval; 499 buf.copyOut(tc->getMemProxy()); 500 // perform the first wake-up 501 int woken1 = futex_map.wakeup(uaddr, process->tgid(), val); 502 int woken2 = 0; 503 // calculate the condition of the second wake-up 504 bool is_wake2 = false; 505 if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ) 506 is_wake2 = oldval == wake_cmparg; 507 else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE) 508 is_wake2 = oldval != wake_cmparg; 509 else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT) 510 is_wake2 = oldval < wake_cmparg; 511 else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE) 512 is_wake2 = oldval <= wake_cmparg; 513 else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT) 514 is_wake2 = oldval > wake_cmparg; 515 else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE) 516 is_wake2 = oldval >= wake_cmparg; 517 // perform the second wake-up 518 if (is_wake2) 519 woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout); 520 521 return woken1 + woken2; 522 } 523 warn("futex: op %d not implemented; ignoring.", op); 524 return -ENOSYS; 525} 526 527 528/// Pseudo Funcs - These functions use a different return convension, 529/// returning a second value in a register other than the normal return register 530SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 531 Process *process, ThreadContext *tc); 532 533/// Target getpidPseudo() handler. 534SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 535 Process *p, ThreadContext *tc); 536 537/// Target getuidPseudo() handler. 538SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 539 Process *p, ThreadContext *tc); 540 541/// Target getgidPseudo() handler. 542SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 543 Process *p, ThreadContext *tc); 544 545 546/// A readable name for 1,000,000, for converting microseconds to seconds. 547const int one_million = 1000000; 548/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 549const int one_billion = 1000000000; 550 551/// Approximate seconds since the epoch (1/1/1970). About a billion, 552/// by my reckoning. We want to keep this a constant (not use the 553/// real-world time) to keep simulations repeatable. 554const unsigned seconds_since_epoch = 1000000000; 555 556/// Helper function to convert current elapsed time to seconds and 557/// microseconds. 558template <class T1, class T2> 559void 560getElapsedTimeMicro(T1 &sec, T2 &usec) 561{ 562 uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 563 sec = elapsed_usecs / one_million; 564 usec = elapsed_usecs % one_million; 565} 566 567/// Helper function to convert current elapsed time to seconds and 568/// nanoseconds. 569template <class T1, class T2> 570void 571getElapsedTimeNano(T1 &sec, T2 &nsec) 572{ 573 uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 574 sec = elapsed_nsecs / one_billion; 575 nsec = elapsed_nsecs % one_billion; 576} 577 578////////////////////////////////////////////////////////////////////// 579// 580// The following emulation functions are generic, but need to be 581// templated to account for differences in types, constants, etc. 582// 583////////////////////////////////////////////////////////////////////// 584 585 typedef struct statfs hst_statfs; 586#if NO_STAT64 587 typedef struct stat hst_stat; 588 typedef struct stat hst_stat64; 589#else 590 typedef struct stat hst_stat; 591 typedef struct stat64 hst_stat64; 592#endif 593 594//// Helper function to convert a host stat buffer to a target stat 595//// buffer. Also copies the target buffer out to the simulated 596//// memory space. Used by stat(), fstat(), and lstat(). 597 598template <typename target_stat, typename host_stat> 599void 600convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 601{ 602 using namespace TheISA; 603 604 if (fakeTTY) 605 tgt->st_dev = 0xA; 606 else 607 tgt->st_dev = host->st_dev; 608 tgt->st_dev = TheISA::htog(tgt->st_dev); 609 tgt->st_ino = host->st_ino; 610 tgt->st_ino = TheISA::htog(tgt->st_ino); 611 tgt->st_mode = host->st_mode; 612 if (fakeTTY) { 613 // Claim to be a character device 614 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 615 tgt->st_mode |= S_IFCHR; // Set S_IFCHR 616 } 617 tgt->st_mode = TheISA::htog(tgt->st_mode); 618 tgt->st_nlink = host->st_nlink; 619 tgt->st_nlink = TheISA::htog(tgt->st_nlink); 620 tgt->st_uid = host->st_uid; 621 tgt->st_uid = TheISA::htog(tgt->st_uid); 622 tgt->st_gid = host->st_gid; 623 tgt->st_gid = TheISA::htog(tgt->st_gid); 624 if (fakeTTY) 625 tgt->st_rdev = 0x880d; 626 else 627 tgt->st_rdev = host->st_rdev; 628 tgt->st_rdev = TheISA::htog(tgt->st_rdev); 629 tgt->st_size = host->st_size; 630 tgt->st_size = TheISA::htog(tgt->st_size); 631 tgt->st_atimeX = host->st_atime; 632 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 633 tgt->st_mtimeX = host->st_mtime; 634 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 635 tgt->st_ctimeX = host->st_ctime; 636 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 637 // Force the block size to be 8KB. This helps to ensure buffered io works 638 // consistently across different hosts. 639 tgt->st_blksize = 0x2000; 640 tgt->st_blksize = TheISA::htog(tgt->st_blksize); 641 tgt->st_blocks = host->st_blocks; 642 tgt->st_blocks = TheISA::htog(tgt->st_blocks); 643} 644 645// Same for stat64 646 647template <typename target_stat, typename host_stat64> 648void 649convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 650{ 651 using namespace TheISA; 652 653 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 654#if defined(STAT_HAVE_NSEC) 655 tgt->st_atime_nsec = host->st_atime_nsec; 656 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 657 tgt->st_mtime_nsec = host->st_mtime_nsec; 658 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 659 tgt->st_ctime_nsec = host->st_ctime_nsec; 660 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 661#else 662 tgt->st_atime_nsec = 0; 663 tgt->st_mtime_nsec = 0; 664 tgt->st_ctime_nsec = 0; 665#endif 666} 667 668// Here are a couple of convenience functions 669template<class OS> 670void 671copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 672 hst_stat *host, bool fakeTTY = false) 673{ 674 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 675 tgt_stat_buf tgt(addr); 676 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 677 tgt.copyOut(mem); 678} 679 680template<class OS> 681void 682copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 683 hst_stat64 *host, bool fakeTTY = false) 684{ 685 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 686 tgt_stat_buf tgt(addr); 687 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 688 tgt.copyOut(mem); 689} 690 691template <class OS> 692void 693copyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 694 hst_statfs *host) 695{ 696 TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 697 698 tgt->f_type = TheISA::htog(host->f_type); 699#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 700 tgt->f_bsize = TheISA::htog(host->f_iosize); 701#else 702 tgt->f_bsize = TheISA::htog(host->f_bsize); 703#endif 704 tgt->f_blocks = TheISA::htog(host->f_blocks); 705 tgt->f_bfree = TheISA::htog(host->f_bfree); 706 tgt->f_bavail = TheISA::htog(host->f_bavail); 707 tgt->f_files = TheISA::htog(host->f_files); 708 tgt->f_ffree = TheISA::htog(host->f_ffree); 709 memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 710#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 711 tgt->f_namelen = TheISA::htog(host->f_namemax); 712 tgt->f_frsize = TheISA::htog(host->f_bsize); 713#elif defined(__APPLE__) 714 tgt->f_namelen = 0; 715 tgt->f_frsize = 0; 716#else 717 tgt->f_namelen = TheISA::htog(host->f_namelen); 718 tgt->f_frsize = TheISA::htog(host->f_frsize); 719#endif 720#if defined(__linux__) 721 memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 722#else 723 /* 724 * The fields are different sizes per OS. Don't bother with 725 * f_spare or f_reserved on non-Linux for now. 726 */ 727 memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 728#endif 729 730 tgt.copyOut(mem); 731} 732 733/// Target ioctl() handler. For the most part, programs call ioctl() 734/// only to find out if their stdout is a tty, to determine whether to 735/// do line or block buffering. We always claim that output fds are 736/// not TTYs to provide repeatable results. 737template <class OS> 738SyscallReturn 739ioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 740{ 741 int index = 0; 742 int tgt_fd = p->getSyscallArg(tc, index); 743 unsigned req = p->getSyscallArg(tc, index); 744 745 DPRINTF_SYSCALL(Verbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 746 747 if (OS::isTtyReq(req)) 748 return -ENOTTY; 749 750 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 751 if (dfdp) { 752 EmulatedDriver *emul_driver = dfdp->getDriver(); 753 if (emul_driver) 754 return emul_driver->ioctl(p, tc, req); 755 } 756 757 auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 758 if (sfdp) { 759 int status; 760 761 switch (req) { 762 case SIOCGIFCONF: { 763 Addr conf_addr = p->getSyscallArg(tc, index); 764 BufferArg conf_arg(conf_addr, sizeof(ifconf)); 765 conf_arg.copyIn(tc->getMemProxy()); 766 767 ifconf *conf = (ifconf*)conf_arg.bufferPtr(); 768 Addr ifc_buf_addr = (Addr)conf->ifc_buf; 769 BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len); 770 ifc_buf_arg.copyIn(tc->getMemProxy()); 771 772 conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr(); 773 774 status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr()); 775 if (status != -1) { 776 conf->ifc_buf = (char*)ifc_buf_addr; 777 ifc_buf_arg.copyOut(tc->getMemProxy()); 778 conf_arg.copyOut(tc->getMemProxy()); 779 } 780 781 return status; 782 } 783 case SIOCGIFFLAGS: 784#if defined(__linux__) 785 case SIOCGIFINDEX: 786#endif 787 case SIOCGIFNETMASK: 788 case SIOCGIFADDR: 789#if defined(__linux__) 790 case SIOCGIFHWADDR: 791#endif 792 case SIOCGIFMTU: { 793 Addr req_addr = p->getSyscallArg(tc, index); 794 BufferArg req_arg(req_addr, sizeof(ifreq)); 795 req_arg.copyIn(tc->getMemProxy()); 796 797 status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr()); 798 if (status != -1) 799 req_arg.copyOut(tc->getMemProxy()); 800 return status; 801 } 802 } 803 } 804 805 /** 806 * For lack of a better return code, return ENOTTY. Ideally, we should 807 * return something better here, but at least we issue the warning. 808 */ 809 warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 810 tgt_fd, req, tc->pcState()); 811 return -ENOTTY; 812} 813 814template <class OS> 815SyscallReturn 816openImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 817 bool isopenat) 818{ 819 int index = 0; 820 int tgt_dirfd = -1; 821 822 /** 823 * If using the openat variant, read in the target directory file 824 * descriptor from the simulated process. 825 */ 826 if (isopenat) 827 tgt_dirfd = p->getSyscallArg(tc, index); 828 829 /** 830 * Retrieve the simulated process' memory proxy and then read in the path 831 * string from that memory space into the host's working memory space. 832 */ 833 std::string path; 834 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 835 return -EFAULT; 836 837#ifdef __CYGWIN32__ 838 int host_flags = O_BINARY; 839#else 840 int host_flags = 0; 841#endif 842 /** 843 * Translate target flags into host flags. Flags exist which are not 844 * ported between architectures which can cause check failures. 845 */ 846 int tgt_flags = p->getSyscallArg(tc, index); 847 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 848 if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 849 tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 850 host_flags |= OS::openFlagTable[i].hostFlag; 851 } 852 } 853 if (tgt_flags) { 854 warn("open%s: cannot decode flags 0x%x", 855 isopenat ? "at" : "", tgt_flags); 856 } 857#ifdef __CYGWIN32__ 858 host_flags |= O_BINARY; 859#endif 860 861 int mode = p->getSyscallArg(tc, index); 862 863 /** 864 * If the simulated process called open or openat with AT_FDCWD specified, 865 * take the current working directory value which was passed into the 866 * process class as a Python parameter and append the current path to 867 * create a full path. 868 * Otherwise, openat with a valid target directory file descriptor has 869 * been called. If the path option, which was passed in as a parameter, 870 * is not absolute, retrieve the directory file descriptor's path and 871 * prepend it to the path passed in as a parameter. 872 * In every case, we should have a full path (which is relevant to the 873 * host) to work with after this block has been passed. 874 */ 875 std::string redir_path = path; 876 std::string abs_path = path; 877 if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) { 878 abs_path = p->absolutePath(path, true); 879 redir_path = p->checkPathRedirect(path); 880 } else if (!startswith(path, "/")) { 881 std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 882 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 883 if (!ffdp) 884 return -EBADF; 885 abs_path = ffdp->getFileName() + path; 886 redir_path = p->checkPathRedirect(abs_path); 887 } 888 889 /** 890 * Since this is an emulated environment, we create pseudo file 891 * descriptors for device requests that have been registered with 892 * the process class through Python; this allows us to create a file 893 * descriptor for subsequent ioctl or mmap calls. 894 */ 895 if (startswith(abs_path, "/dev/")) { 896 std::string filename = abs_path.substr(strlen("/dev/")); 897 EmulatedDriver *drv = p->findDriver(filename); 898 if (drv) { 899 DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 900 "driver open with path[%s]\n", 901 isopenat ? "at" : "", abs_path.c_str()); 902 return drv->open(p, tc, mode, host_flags); 903 } 904 /** 905 * Fall through here for pass through to host devices, such 906 * as /dev/zero 907 */ 908 } 909 910 /** 911 * We make several attempts resolve a call to open. 912 * 913 * 1) Resolve any path redirection before hand. This will set the path 914 * up with variable 'redir_path' which may contain a modified path or 915 * the original path value. This should already be done in prior code. 916 * 2) Try to handle the access using 'special_paths'. Some special_paths 917 * and files cannot be called on the host and need to be handled as 918 * special cases inside the simulator. These special_paths are handled by 919 * C++ routines to provide output back to userspace. 920 * 3) If the full path that was created above does not match any of the 921 * special cases, pass it through to the open call on the __HOST__ to let 922 * the host open the file on our behalf. Again, the openImpl tries to 923 * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the 924 * faux-filesystem files). The faux-filesystem is dynamically created 925 * during simulator configuration using Python functions. 926 * 4) If the host cannot open the file, the open attempt failed in "3)". 927 * Return the host's error code back through the system call to the 928 * simulated process. If running a debug trace, also notify the user that 929 * the open call failed. 930 * 931 * Any success will set sim_fd to something other than -1 and skip the 932 * next conditions effectively bypassing them. 933 */ 934 int sim_fd = -1; 935 std::string used_path; 936 std::vector<std::string> special_paths = 937 { "/proc/meminfo/", "/system/", "/platform/", "/etc/passwd" }; 938 for (auto entry : special_paths) { 939 if (startswith(path, entry)) { 940 sim_fd = OS::openSpecialFile(abs_path, p, tc); 941 used_path = abs_path; 942 } 943 } 944 if (sim_fd == -1) { 945 sim_fd = open(redir_path.c_str(), host_flags, mode); 946 used_path = redir_path; 947 } 948 if (sim_fd == -1) { 949 int local = -errno; 950 DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s " 951 "(inferred from:%s)\n", isopenat ? "at" : "", 952 used_path.c_str(), path.c_str()); 953 return local; 954 } 955 956 /** 957 * The file was opened successfully and needs to be recorded in the 958 * process' file descriptor array so that it can be retrieved later. 959 * The target file descriptor that is chosen will be the lowest unused 960 * file descriptor. 961 * Return the indirect target file descriptor back to the simulated 962 * process to act as a handle for the opened file. 963 */ 964 auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 965 int tgt_fd = p->fds->allocFD(ffdp); 966 DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n" 967 "(inferred from:%s)\n", isopenat ? "at" : "", 968 sim_fd, tgt_fd, used_path.c_str(), path.c_str()); 969 return tgt_fd; 970} 971 972/// Target open() handler. 973template <class OS> 974SyscallReturn 975openFunc(SyscallDesc *desc, int callnum, Process *process, 976 ThreadContext *tc) 977{ 978 return openImpl<OS>(desc, callnum, process, tc, false); 979} 980 981/// Target openat() handler. 982template <class OS> 983SyscallReturn 984openatFunc(SyscallDesc *desc, int callnum, Process *process, 985 ThreadContext *tc) 986{ 987 return openImpl<OS>(desc, callnum, process, tc, true); 988} 989 990/// Target unlinkat() handler. 991template <class OS> 992SyscallReturn 993unlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 994 ThreadContext *tc) 995{ 996 int index = 0; 997 int dirfd = process->getSyscallArg(tc, index); 998 if (dirfd != OS::TGT_AT_FDCWD) 999 warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 1000 1001 return unlinkHelper(desc, callnum, process, tc, 1); 1002} 1003 1004/// Target facessat() handler 1005template <class OS> 1006SyscallReturn 1007faccessatFunc(SyscallDesc *desc, int callnum, Process *process, 1008 ThreadContext *tc) 1009{ 1010 int index = 0; 1011 int dirfd = process->getSyscallArg(tc, index); 1012 if (dirfd != OS::TGT_AT_FDCWD) 1013 warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 1014 return accessFunc(desc, callnum, process, tc, 1); 1015} 1016 1017/// Target readlinkat() handler 1018template <class OS> 1019SyscallReturn 1020readlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 1021 ThreadContext *tc) 1022{ 1023 int index = 0; 1024 int dirfd = process->getSyscallArg(tc, index); 1025 if (dirfd != OS::TGT_AT_FDCWD) 1026 warn("openat: first argument not AT_FDCWD; unlikely to work"); 1027 return readlinkFunc(desc, callnum, process, tc, 1); 1028} 1029 1030/// Target renameat() handler. 1031template <class OS> 1032SyscallReturn 1033renameatFunc(SyscallDesc *desc, int callnum, Process *process, 1034 ThreadContext *tc) 1035{ 1036 int index = 0; 1037 1038 int olddirfd = process->getSyscallArg(tc, index); 1039 if (olddirfd != OS::TGT_AT_FDCWD) 1040 warn("renameat: first argument not AT_FDCWD; unlikely to work"); 1041 1042 std::string old_name; 1043 1044 if (!tc->getMemProxy().tryReadString(old_name, 1045 process->getSyscallArg(tc, index))) 1046 return -EFAULT; 1047 1048 int newdirfd = process->getSyscallArg(tc, index); 1049 if (newdirfd != OS::TGT_AT_FDCWD) 1050 warn("renameat: third argument not AT_FDCWD; unlikely to work"); 1051 1052 std::string new_name; 1053 1054 if (!tc->getMemProxy().tryReadString(new_name, 1055 process->getSyscallArg(tc, index))) 1056 return -EFAULT; 1057 1058 // Adjust path for cwd and redirection 1059 old_name = process->checkPathRedirect(old_name); 1060 new_name = process->checkPathRedirect(new_name); 1061 1062 int result = rename(old_name.c_str(), new_name.c_str()); 1063 return (result == -1) ? -errno : result; 1064} 1065 1066/// Target sysinfo() handler. 1067template <class OS> 1068SyscallReturn 1069sysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 1070 ThreadContext *tc) 1071{ 1072 1073 int index = 0; 1074 TypedBufferArg<typename OS::tgt_sysinfo> 1075 sysinfo(process->getSyscallArg(tc, index)); 1076 1077 sysinfo->uptime = seconds_since_epoch; 1078 sysinfo->totalram = process->system->memSize(); 1079 sysinfo->mem_unit = 1; 1080 1081 sysinfo.copyOut(tc->getMemProxy()); 1082 1083 return 0; 1084} 1085 1086/// Target chmod() handler. 1087template <class OS> 1088SyscallReturn 1089chmodFunc(SyscallDesc *desc, int callnum, Process *process, 1090 ThreadContext *tc) 1091{ 1092 std::string path; 1093 1094 int index = 0; 1095 if (!tc->getMemProxy().tryReadString(path, 1096 process->getSyscallArg(tc, index))) { 1097 return -EFAULT; 1098 } 1099 1100 uint32_t mode = process->getSyscallArg(tc, index); 1101 mode_t hostMode = 0; 1102 1103 // XXX translate mode flags via OS::something??? 1104 hostMode = mode; 1105 1106 // Adjust path for cwd and redirection 1107 path = process->checkPathRedirect(path); 1108 1109 // do the chmod 1110 int result = chmod(path.c_str(), hostMode); 1111 if (result < 0) 1112 return -errno; 1113 1114 return 0; 1115} 1116 1117template <class OS> 1118SyscallReturn 1119pollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1120{ 1121 int index = 0; 1122 Addr fdsPtr = p->getSyscallArg(tc, index); 1123 int nfds = p->getSyscallArg(tc, index); 1124 int tmout = p->getSyscallArg(tc, index); 1125 1126 BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 1127 fdsBuf.copyIn(tc->getMemProxy()); 1128 1129 /** 1130 * Record the target file descriptors in a local variable. We need to 1131 * replace them with host file descriptors but we need a temporary copy 1132 * for later. Afterwards, replace each target file descriptor in the 1133 * poll_fd array with its host_fd. 1134 */ 1135 int temp_tgt_fds[nfds]; 1136 for (index = 0; index < nfds; index++) { 1137 temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 1138 auto tgt_fd = temp_tgt_fds[index]; 1139 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 1140 if (!hbfdp) 1141 return -EBADF; 1142 auto host_fd = hbfdp->getSimFD(); 1143 ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 1144 } 1145 1146 /** 1147 * We cannot allow an infinite poll to occur or it will inevitably cause 1148 * a deadlock in the gem5 simulator with clone. We must pass in tmout with 1149 * a non-negative value, however it also makes no sense to poll on the 1150 * underlying host for any other time than tmout a zero timeout. 1151 */ 1152 int status; 1153 if (tmout < 0) { 1154 status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 1155 if (status == 0) { 1156 /** 1157 * If blocking indefinitely, check the signal list to see if a 1158 * signal would break the poll out of the retry cycle and try 1159 * to return the signal interrupt instead. 1160 */ 1161 System *sysh = tc->getSystemPtr(); 1162 std::list<BasicSignal>::iterator it; 1163 for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 1164 if (it->receiver == p) 1165 return -EINTR; 1166 return SyscallReturn::retry(); 1167 } 1168 } else 1169 status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 1170 1171 if (status == -1) 1172 return -errno; 1173 1174 /** 1175 * Replace each host_fd in the returned poll_fd array with its original 1176 * target file descriptor. 1177 */ 1178 for (index = 0; index < nfds; index++) { 1179 auto tgt_fd = temp_tgt_fds[index]; 1180 ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 1181 } 1182 1183 /** 1184 * Copy out the pollfd struct because the host may have updated fields 1185 * in the structure. 1186 */ 1187 fdsBuf.copyOut(tc->getMemProxy()); 1188 1189 return status; 1190} 1191 1192/// Target fchmod() handler. 1193template <class OS> 1194SyscallReturn 1195fchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1196{ 1197 int index = 0; 1198 int tgt_fd = p->getSyscallArg(tc, index); 1199 uint32_t mode = p->getSyscallArg(tc, index); 1200 1201 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1202 if (!ffdp) 1203 return -EBADF; 1204 int sim_fd = ffdp->getSimFD(); 1205 1206 mode_t hostMode = mode; 1207 1208 int result = fchmod(sim_fd, hostMode); 1209 1210 return (result < 0) ? -errno : 0; 1211} 1212 1213/// Target mremap() handler. 1214template <class OS> 1215SyscallReturn 1216mremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 1217{ 1218 int index = 0; 1219 Addr start = process->getSyscallArg(tc, index); 1220 uint64_t old_length = process->getSyscallArg(tc, index); 1221 uint64_t new_length = process->getSyscallArg(tc, index); 1222 uint64_t flags = process->getSyscallArg(tc, index); 1223 uint64_t provided_address = 0; 1224 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 1225 1226 if (use_provided_address) 1227 provided_address = process->getSyscallArg(tc, index); 1228 1229 if ((start % TheISA::PageBytes != 0) || 1230 (provided_address % TheISA::PageBytes != 0)) { 1231 warn("mremap failing: arguments not page aligned"); 1232 return -EINVAL; 1233 } 1234 1235 new_length = roundUp(new_length, TheISA::PageBytes); 1236 1237 if (new_length > old_length) { 1238 std::shared_ptr<MemState> mem_state = process->memState; 1239 Addr mmap_end = mem_state->getMmapEnd(); 1240 1241 if ((start + old_length) == mmap_end && 1242 (!use_provided_address || provided_address == start)) { 1243 // This case cannot occur when growing downward, as 1244 // start is greater than or equal to mmap_end. 1245 uint64_t diff = new_length - old_length; 1246 process->allocateMem(mmap_end, diff); 1247 mem_state->setMmapEnd(mmap_end + diff); 1248 return start; 1249 } else { 1250 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 1251 warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 1252 return -ENOMEM; 1253 } else { 1254 uint64_t new_start = provided_address; 1255 if (!use_provided_address) { 1256 new_start = process->mmapGrowsDown() ? 1257 mmap_end - new_length : mmap_end; 1258 mmap_end = process->mmapGrowsDown() ? 1259 new_start : mmap_end + new_length; 1260 mem_state->setMmapEnd(mmap_end); 1261 } 1262 1263 process->pTable->remap(start, old_length, new_start); 1264 warn("mremapping to new vaddr %08p-%08p, adding %d\n", 1265 new_start, new_start + new_length, 1266 new_length - old_length); 1267 // add on the remaining unallocated pages 1268 process->allocateMem(new_start + old_length, 1269 new_length - old_length, 1270 use_provided_address /* clobber */); 1271 if (use_provided_address && 1272 ((new_start + new_length > mem_state->getMmapEnd() && 1273 !process->mmapGrowsDown()) || 1274 (new_start < mem_state->getMmapEnd() && 1275 process->mmapGrowsDown()))) { 1276 // something fishy going on here, at least notify the user 1277 // @todo: increase mmap_end? 1278 warn("mmap region limit exceeded with MREMAP_FIXED\n"); 1279 } 1280 warn("returning %08p as start\n", new_start); 1281 return new_start; 1282 } 1283 } 1284 } else { 1285 if (use_provided_address && provided_address != start) 1286 process->pTable->remap(start, new_length, provided_address); 1287 process->pTable->unmap(start + new_length, old_length - new_length); 1288 return use_provided_address ? provided_address : start; 1289 } 1290} 1291 1292/// Target stat() handler. 1293template <class OS> 1294SyscallReturn 1295statFunc(SyscallDesc *desc, int callnum, Process *process, 1296 ThreadContext *tc) 1297{ 1298 std::string path; 1299 1300 int index = 0; 1301 if (!tc->getMemProxy().tryReadString(path, 1302 process->getSyscallArg(tc, index))) { 1303 return -EFAULT; 1304 } 1305 Addr bufPtr = process->getSyscallArg(tc, index); 1306 1307 // Adjust path for cwd and redirection 1308 path = process->checkPathRedirect(path); 1309 1310 struct stat hostBuf; 1311 int result = stat(path.c_str(), &hostBuf); 1312 1313 if (result < 0) 1314 return -errno; 1315 1316 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1317 1318 return 0; 1319} 1320 1321 1322/// Target stat64() handler. 1323template <class OS> 1324SyscallReturn 1325stat64Func(SyscallDesc *desc, int callnum, Process *process, 1326 ThreadContext *tc) 1327{ 1328 std::string path; 1329 1330 int index = 0; 1331 if (!tc->getMemProxy().tryReadString(path, 1332 process->getSyscallArg(tc, index))) 1333 return -EFAULT; 1334 Addr bufPtr = process->getSyscallArg(tc, index); 1335 1336 // Adjust path for cwd and redirection 1337 path = process->checkPathRedirect(path); 1338 1339#if NO_STAT64 1340 struct stat hostBuf; 1341 int result = stat(path.c_str(), &hostBuf); 1342#else 1343 struct stat64 hostBuf; 1344 int result = stat64(path.c_str(), &hostBuf); 1345#endif 1346 1347 if (result < 0) 1348 return -errno; 1349 1350 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1351 1352 return 0; 1353} 1354 1355 1356/// Target fstatat64() handler. 1357template <class OS> 1358SyscallReturn 1359fstatat64Func(SyscallDesc *desc, int callnum, Process *process, 1360 ThreadContext *tc) 1361{ 1362 int index = 0; 1363 int dirfd = process->getSyscallArg(tc, index); 1364 if (dirfd != OS::TGT_AT_FDCWD) 1365 warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 1366 1367 std::string path; 1368 if (!tc->getMemProxy().tryReadString(path, 1369 process->getSyscallArg(tc, index))) 1370 return -EFAULT; 1371 Addr bufPtr = process->getSyscallArg(tc, index); 1372 1373 // Adjust path for cwd and redirection 1374 path = process->checkPathRedirect(path); 1375 1376#if NO_STAT64 1377 struct stat hostBuf; 1378 int result = stat(path.c_str(), &hostBuf); 1379#else 1380 struct stat64 hostBuf; 1381 int result = stat64(path.c_str(), &hostBuf); 1382#endif 1383 1384 if (result < 0) 1385 return -errno; 1386 1387 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1388 1389 return 0; 1390} 1391 1392 1393/// Target fstat64() handler. 1394template <class OS> 1395SyscallReturn 1396fstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1397{ 1398 int index = 0; 1399 int tgt_fd = p->getSyscallArg(tc, index); 1400 Addr bufPtr = p->getSyscallArg(tc, index); 1401 1402 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1403 if (!ffdp) 1404 return -EBADF; 1405 int sim_fd = ffdp->getSimFD(); 1406 1407#if NO_STAT64 1408 struct stat hostBuf; 1409 int result = fstat(sim_fd, &hostBuf); 1410#else 1411 struct stat64 hostBuf; 1412 int result = fstat64(sim_fd, &hostBuf); 1413#endif 1414 1415 if (result < 0) 1416 return -errno; 1417 1418 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1419 1420 return 0; 1421} 1422 1423 1424/// Target lstat() handler. 1425template <class OS> 1426SyscallReturn 1427lstatFunc(SyscallDesc *desc, int callnum, Process *process, 1428 ThreadContext *tc) 1429{ 1430 std::string path; 1431 1432 int index = 0; 1433 if (!tc->getMemProxy().tryReadString(path, 1434 process->getSyscallArg(tc, index))) { 1435 return -EFAULT; 1436 } 1437 Addr bufPtr = process->getSyscallArg(tc, index); 1438 1439 // Adjust path for cwd and redirection 1440 path = process->checkPathRedirect(path); 1441 1442 struct stat hostBuf; 1443 int result = lstat(path.c_str(), &hostBuf); 1444 1445 if (result < 0) 1446 return -errno; 1447 1448 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1449 1450 return 0; 1451} 1452 1453/// Target lstat64() handler. 1454template <class OS> 1455SyscallReturn 1456lstat64Func(SyscallDesc *desc, int callnum, Process *process, 1457 ThreadContext *tc) 1458{ 1459 std::string path; 1460 1461 int index = 0; 1462 if (!tc->getMemProxy().tryReadString(path, 1463 process->getSyscallArg(tc, index))) { 1464 return -EFAULT; 1465 } 1466 Addr bufPtr = process->getSyscallArg(tc, index); 1467 1468 // Adjust path for cwd and redirection 1469 path = process->checkPathRedirect(path); 1470 1471#if NO_STAT64 1472 struct stat hostBuf; 1473 int result = lstat(path.c_str(), &hostBuf); 1474#else 1475 struct stat64 hostBuf; 1476 int result = lstat64(path.c_str(), &hostBuf); 1477#endif 1478 1479 if (result < 0) 1480 return -errno; 1481 1482 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1483 1484 return 0; 1485} 1486 1487/// Target fstat() handler. 1488template <class OS> 1489SyscallReturn 1490fstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1491{ 1492 int index = 0; 1493 int tgt_fd = p->getSyscallArg(tc, index); 1494 Addr bufPtr = p->getSyscallArg(tc, index); 1495 1496 DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1497 1498 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1499 if (!ffdp) 1500 return -EBADF; 1501 int sim_fd = ffdp->getSimFD(); 1502 1503 struct stat hostBuf; 1504 int result = fstat(sim_fd, &hostBuf); 1505 1506 if (result < 0) 1507 return -errno; 1508 1509 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1510 1511 return 0; 1512} 1513 1514/// Target statfs() handler. 1515template <class OS> 1516SyscallReturn 1517statfsFunc(SyscallDesc *desc, int callnum, Process *process, 1518 ThreadContext *tc) 1519{ 1520#if defined(__linux__) 1521 std::string path; 1522 1523 int index = 0; 1524 if (!tc->getMemProxy().tryReadString(path, 1525 process->getSyscallArg(tc, index))) { 1526 return -EFAULT; 1527 } 1528 Addr bufPtr = process->getSyscallArg(tc, index); 1529 1530 // Adjust path for cwd and redirection 1531 path = process->checkPathRedirect(path); 1532 1533 struct statfs hostBuf; 1534 int result = statfs(path.c_str(), &hostBuf); 1535 1536 if (result < 0) 1537 return -errno; 1538 1539 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1540 return 0; 1541#else 1542 warnUnsupportedOS("statfs"); 1543 return -1; 1544#endif 1545} 1546 1547template <class OS> 1548SyscallReturn 1549cloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1550{ 1551 int index = 0; 1552 1553 RegVal flags = p->getSyscallArg(tc, index); 1554 RegVal newStack = p->getSyscallArg(tc, index); 1555 Addr ptidPtr = p->getSyscallArg(tc, index); 1556 1557#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 1558 /** 1559 * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 1560 * The flag defines the list of clone() arguments in the following 1561 * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 1562 */ 1563 Addr tlsPtr = p->getSyscallArg(tc, index); 1564 Addr ctidPtr = p->getSyscallArg(tc, index); 1565#else 1566 Addr ctidPtr = p->getSyscallArg(tc, index); 1567 Addr tlsPtr = p->getSyscallArg(tc, index); 1568#endif 1569 1570 if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 1571 ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 1572 ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 1573 ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 1574 ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 1575 ((flags & OS::TGT_CLONE_VM) && !(newStack))) 1576 return -EINVAL; 1577 1578 ThreadContext *ctc; 1579 if (!(ctc = p->findFreeContext())) { 1580 DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system" 1581 "[cpu %d, thread %d]", tc->cpuId(), tc->threadId()); 1582 return -EAGAIN; 1583 } 1584 1585 /** 1586 * Note that ProcessParams is generated by swig and there are no other 1587 * examples of how to create anything but this default constructor. The 1588 * fields are manually initialized instead of passing parameters to the 1589 * constructor. 1590 */ 1591 ProcessParams *pp = new ProcessParams(); 1592 pp->executable.assign(*(new std::string(p->progName()))); 1593 pp->cmd.push_back(*(new std::string(p->progName()))); 1594 pp->system = p->system; 1595 pp->cwd.assign(p->tgtCwd); 1596 pp->input.assign("stdin"); 1597 pp->output.assign("stdout"); 1598 pp->errout.assign("stderr"); 1599 pp->uid = p->uid(); 1600 pp->euid = p->euid(); 1601 pp->gid = p->gid(); 1602 pp->egid = p->egid(); 1603 1604 /* Find the first free PID that's less than the maximum */ 1605 std::set<int> const& pids = p->system->PIDs; 1606 int temp_pid = *pids.begin(); 1607 do { 1608 temp_pid++; 1609 } while (pids.find(temp_pid) != pids.end()); 1610 if (temp_pid >= System::maxPID) 1611 fatal("temp_pid is too large: %d", temp_pid); 1612 1613 pp->pid = temp_pid; 1614 pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 1615 pp->useArchPT = p->useArchPT; 1616 pp->kvmInSE = p->kvmInSE; 1617 Process *cp = pp->create(); 1618 delete pp; 1619 1620 Process *owner = ctc->getProcessPtr(); 1621 ctc->setProcessPtr(cp); 1622 cp->assignThreadContext(ctc->contextId()); 1623 owner->revokeThreadContext(ctc->contextId()); 1624 1625 if (flags & OS::TGT_CLONE_PARENT_SETTID) { 1626 BufferArg ptidBuf(ptidPtr, sizeof(long)); 1627 long *ptid = (long *)ptidBuf.bufferPtr(); 1628 *ptid = cp->pid(); 1629 ptidBuf.copyOut(tc->getMemProxy()); 1630 } 1631 1632 if (flags & OS::TGT_CLONE_THREAD) { 1633 cp->pTable->shared = true; 1634 cp->useForClone = true; 1635 } 1636 cp->initState(); 1637 p->clone(tc, ctc, cp, flags); 1638 1639 if (flags & OS::TGT_CLONE_THREAD) { 1640 delete cp->sigchld; 1641 cp->sigchld = p->sigchld; 1642 } else if (flags & OS::TGT_SIGCHLD) { 1643 *cp->sigchld = true; 1644 } 1645 1646 if (flags & OS::TGT_CLONE_CHILD_SETTID) { 1647 BufferArg ctidBuf(ctidPtr, sizeof(long)); 1648 long *ctid = (long *)ctidBuf.bufferPtr(); 1649 *ctid = cp->pid(); 1650 ctidBuf.copyOut(ctc->getMemProxy()); 1651 } 1652 1653 if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 1654 cp->childClearTID = (uint64_t)ctidPtr; 1655 1656 ctc->clearArchRegs(); 1657 1658 OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 1659 1660 cp->setSyscallReturn(ctc, 0); 1661 1662#if THE_ISA == ALPHA_ISA 1663 ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 1664#elif THE_ISA == SPARC_ISA 1665 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1666 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 1667#endif 1668 1669 if (p->kvmInSE) { 1670#if THE_ISA == X86_ISA 1671 ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX)); 1672#else 1673 panic("KVM CPU model is not supported for this ISA"); 1674#endif 1675 } else { 1676 TheISA::PCState cpc = tc->pcState(); 1677 cpc.advance(); 1678 ctc->pcState(cpc); 1679 } 1680 ctc->activate(); 1681 1682 return cp->pid(); 1683} 1684 1685/// Target fstatfs() handler. 1686template <class OS> 1687SyscallReturn 1688fstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1689{ 1690 int index = 0; 1691 int tgt_fd = p->getSyscallArg(tc, index); 1692 Addr bufPtr = p->getSyscallArg(tc, index); 1693 1694 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1695 if (!ffdp) 1696 return -EBADF; 1697 int sim_fd = ffdp->getSimFD(); 1698 1699 struct statfs hostBuf; 1700 int result = fstatfs(sim_fd, &hostBuf); 1701 1702 if (result < 0) 1703 return -errno; 1704 1705 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1706 1707 return 0; 1708} 1709 1710/// Target readv() handler. 1711template <class OS> 1712SyscallReturn 1713readvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1714{ 1715 int index = 0; 1716 int tgt_fd = p->getSyscallArg(tc, index); 1717 1718 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1719 if (!ffdp) 1720 return -EBADF; 1721 int sim_fd = ffdp->getSimFD(); 1722 1723 SETranslatingPortProxy &prox = tc->getMemProxy(); 1724 uint64_t tiov_base = p->getSyscallArg(tc, index); 1725 size_t count = p->getSyscallArg(tc, index); 1726 typename OS::tgt_iovec tiov[count]; 1727 struct iovec hiov[count]; 1728 for (size_t i = 0; i < count; ++i) { 1729 prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)), 1730 (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec)); 1731 hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len); 1732 hiov[i].iov_base = new char [hiov[i].iov_len]; 1733 } 1734 1735 int result = readv(sim_fd, hiov, count); 1736 int local_errno = errno; 1737 1738 for (size_t i = 0; i < count; ++i) { 1739 if (result != -1) { 1740 prox.writeBlob(TheISA::htog(tiov[i].iov_base), 1741 (uint8_t*)hiov[i].iov_base, hiov[i].iov_len); 1742 } 1743 delete [] (char *)hiov[i].iov_base; 1744 } 1745 1746 return (result == -1) ? -local_errno : result; 1747} 1748 1749/// Target writev() handler. 1750template <class OS> 1751SyscallReturn 1752writevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1753{ 1754 int index = 0; 1755 int tgt_fd = p->getSyscallArg(tc, index); 1756 1757 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 1758 if (!hbfdp) 1759 return -EBADF; 1760 int sim_fd = hbfdp->getSimFD(); 1761 1762 SETranslatingPortProxy &prox = tc->getMemProxy(); 1763 uint64_t tiov_base = p->getSyscallArg(tc, index); 1764 size_t count = p->getSyscallArg(tc, index); 1765 struct iovec hiov[count]; 1766 for (size_t i = 0; i < count; ++i) { 1767 typename OS::tgt_iovec tiov; 1768 1769 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 1770 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 1771 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1772 hiov[i].iov_base = new char [hiov[i].iov_len]; 1773 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 1774 hiov[i].iov_len); 1775 } 1776 1777 int result = writev(sim_fd, hiov, count); 1778 1779 for (size_t i = 0; i < count; ++i) 1780 delete [] (char *)hiov[i].iov_base; 1781 1782 return (result == -1) ? -errno : result; 1783} 1784 1785/// Real mmap handler. 1786template <class OS> 1787SyscallReturn 1788mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1789 bool is_mmap2) 1790{ 1791 int index = 0; 1792 Addr start = p->getSyscallArg(tc, index); 1793 uint64_t length = p->getSyscallArg(tc, index); 1794 int prot = p->getSyscallArg(tc, index); 1795 int tgt_flags = p->getSyscallArg(tc, index); 1796 int tgt_fd = p->getSyscallArg(tc, index); 1797 int offset = p->getSyscallArg(tc, index); 1798 1799 if (is_mmap2) 1800 offset *= TheISA::PageBytes; 1801 1802 if (start & (TheISA::PageBytes - 1) || 1803 offset & (TheISA::PageBytes - 1) || 1804 (tgt_flags & OS::TGT_MAP_PRIVATE && 1805 tgt_flags & OS::TGT_MAP_SHARED) || 1806 (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 1807 !(tgt_flags & OS::TGT_MAP_SHARED)) || 1808 !length) { 1809 return -EINVAL; 1810 } 1811 1812 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1813 // With shared mmaps, there are two cases to consider: 1814 // 1) anonymous: writes should modify the mapping and this should be 1815 // visible to observers who share the mapping. Currently, it's 1816 // difficult to update the shared mapping because there's no 1817 // structure which maintains information about the which virtual 1818 // memory areas are shared. If that structure existed, it would be 1819 // possible to make the translations point to the same frames. 1820 // 2) file-backed: writes should modify the mapping and the file 1821 // which is backed by the mapping. The shared mapping problem is the 1822 // same as what was mentioned about the anonymous mappings. For 1823 // file-backed mappings, the writes to the file are difficult 1824 // because it requires syncing what the mapping holds with the file 1825 // that resides on the host system. So, any write on a real system 1826 // would cause the change to be propagated to the file mapping at 1827 // some point in the future (the inode is tracked along with the 1828 // mapping). This isn't guaranteed to always happen, but it usually 1829 // works well enough. The guarantee is provided by the msync system 1830 // call. We could force the change through with shared mappings with 1831 // a call to msync, but that again would require more information 1832 // than we currently maintain. 1833 warn("mmap: writing to shared mmap region is currently " 1834 "unsupported. The write succeeds on the target, but it " 1835 "will not be propagated to the host or shared mappings"); 1836 } 1837 1838 length = roundUp(length, TheISA::PageBytes); 1839 1840 int sim_fd = -1; 1841 uint8_t *pmap = nullptr; 1842 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1843 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1844 1845 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1846 if (dfdp) { 1847 EmulatedDriver *emul_driver = dfdp->getDriver(); 1848 return emul_driver->mmap(p, tc, start, length, prot, 1849 tgt_flags, tgt_fd, offset); 1850 } 1851 1852 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1853 if (!ffdp) 1854 return -EBADF; 1855 sim_fd = ffdp->getSimFD(); 1856 1857 pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 1858 sim_fd, offset); 1859 1860 if (pmap == (decltype(pmap))-1) { 1861 warn("mmap: failed to map file into host address space"); 1862 return -errno; 1863 } 1864 } 1865 1866 // Extend global mmap region if necessary. Note that we ignore the 1867 // start address unless MAP_FIXED is specified. 1868 if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 1869 std::shared_ptr<MemState> mem_state = p->memState; 1870 Addr mmap_end = mem_state->getMmapEnd(); 1871 1872 start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1873 mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 1874 1875 mem_state->setMmapEnd(mmap_end); 1876 } 1877 1878 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1879 start, start + length - 1); 1880 1881 // We only allow mappings to overwrite existing mappings if 1882 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 1883 // because we ignore the start hint if TGT_MAP_FIXED is not set. 1884 int clobber = tgt_flags & OS::TGT_MAP_FIXED; 1885 if (clobber) { 1886 for (auto tc : p->system->threadContexts) { 1887 // If we might be overwriting old mappings, we need to 1888 // invalidate potentially stale mappings out of the TLBs. 1889 tc->getDTBPtr()->flushAll(); 1890 tc->getITBPtr()->flushAll(); 1891 } 1892 } 1893 1894 // Allocate physical memory and map it in. If the page table is already 1895 // mapped and clobber is not set, the simulator will issue throw a 1896 // fatal and bail out of the simulation. 1897 p->allocateMem(start, length, clobber); 1898 1899 // Transfer content into target address space. 1900 SETranslatingPortProxy &tp = tc->getMemProxy(); 1901 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1902 // In general, we should zero the mapped area for anonymous mappings, 1903 // with something like: 1904 // tp.memsetBlob(start, 0, length); 1905 // However, given that we don't support sparse mappings, and 1906 // some applications can map a couple of gigabytes of space 1907 // (intending sparse usage), that can get painfully expensive. 1908 // Fortunately, since we don't properly implement munmap either, 1909 // there's no danger of remapping used memory, so for now all 1910 // newly mapped memory should already be zeroed so we can skip it. 1911 } else { 1912 // It is possible to mmap an area larger than a file, however 1913 // accessing unmapped portions the system triggers a "Bus error" 1914 // on the host. We must know when to stop copying the file from 1915 // the host into the target address space. 1916 struct stat file_stat; 1917 if (fstat(sim_fd, &file_stat) > 0) 1918 fatal("mmap: cannot stat file"); 1919 1920 // Copy the portion of the file that is resident. This requires 1921 // checking both the mmap size and the filesize that we are 1922 // trying to mmap into this space; the mmap size also depends 1923 // on the specified offset into the file. 1924 uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1925 length); 1926 tp.writeBlob(start, pmap, size); 1927 1928 // Cleanup the mmap region before exiting this function. 1929 munmap(pmap, length); 1930 1931 // Maintain the symbol table for dynamic executables. 1932 // The loader will call mmap to map the images into its address 1933 // space and we intercept that here. We can verify that we are 1934 // executing inside the loader by checking the program counter value. 1935 // XXX: with multiprogrammed workloads or multi-node configurations, 1936 // this will not work since there is a single global symbol table. 1937 ObjectFile *interpreter = p->getInterpreter(); 1938 if (interpreter) { 1939 Addr text_start = interpreter->textBase(); 1940 Addr text_end = text_start + interpreter->textSize(); 1941 1942 Addr pc = tc->pcState().pc(); 1943 1944 if (pc >= text_start && pc < text_end) { 1945 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1946 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1947 ObjectFile *lib = createObjectFile(ffdp->getFileName()); 1948 1949 if (lib) { 1950 lib->loadAllSymbols(debugSymbolTable, 1951 lib->textBase(), start); 1952 } 1953 } 1954 } 1955 1956 // Note that we do not zero out the remainder of the mapping. This 1957 // is done by a real system, but it probably will not affect 1958 // execution (hopefully). 1959 } 1960 1961 return start; 1962} 1963 1964template <class OS> 1965SyscallReturn 1966pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1967{ 1968 int index = 0; 1969 int tgt_fd = p->getSyscallArg(tc, index); 1970 Addr bufPtr = p->getSyscallArg(tc, index); 1971 int nbytes = p->getSyscallArg(tc, index); 1972 int offset = p->getSyscallArg(tc, index); 1973 1974 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1975 if (!ffdp) 1976 return -EBADF; 1977 int sim_fd = ffdp->getSimFD(); 1978 1979 BufferArg bufArg(bufPtr, nbytes); 1980 bufArg.copyIn(tc->getMemProxy()); 1981 1982 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1983 1984 return (bytes_written == -1) ? -errno : bytes_written; 1985} 1986 1987/// Target mmap() handler. 1988template <class OS> 1989SyscallReturn 1990mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1991{ 1992 return mmapImpl<OS>(desc, num, p, tc, false); 1993} 1994 1995/// Target mmap2() handler. 1996template <class OS> 1997SyscallReturn 1998mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1999{ 2000 return mmapImpl<OS>(desc, num, p, tc, true); 2001} 2002 2003/// Target getrlimit() handler. 2004template <class OS> 2005SyscallReturn 2006getrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 2007 ThreadContext *tc) 2008{ 2009 int index = 0; 2010 unsigned resource = process->getSyscallArg(tc, index); 2011 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 2012 2013 switch (resource) { 2014 case OS::TGT_RLIMIT_STACK: 2015 // max stack size in bytes: make up a number (8MB for now) 2016 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 2017 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2018 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2019 break; 2020 2021 case OS::TGT_RLIMIT_DATA: 2022 // max data segment size in bytes: make up a number 2023 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 2024 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2025 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2026 break; 2027 2028 default: 2029 warn("getrlimit: unimplemented resource %d", resource); 2030 return -EINVAL; 2031 break; 2032 } 2033 2034 rlp.copyOut(tc->getMemProxy()); 2035 return 0; 2036} 2037 2038template <class OS> 2039SyscallReturn 2040prlimitFunc(SyscallDesc *desc, int callnum, Process *process, 2041 ThreadContext *tc) 2042{ 2043 int index = 0; 2044 if (process->getSyscallArg(tc, index) != 0) 2045 { 2046 warn("prlimit: ignoring rlimits for nonzero pid"); 2047 return -EPERM; 2048 } 2049 int resource = process->getSyscallArg(tc, index); 2050 Addr n = process->getSyscallArg(tc, index); 2051 if (n != 0) 2052 warn("prlimit: ignoring new rlimit"); 2053 Addr o = process->getSyscallArg(tc, index); 2054 if (o != 0) 2055 { 2056 TypedBufferArg<typename OS::rlimit> rlp(o); 2057 switch (resource) { 2058 case OS::TGT_RLIMIT_STACK: 2059 // max stack size in bytes: make up a number (8MB for now) 2060 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 2061 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2062 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2063 break; 2064 case OS::TGT_RLIMIT_DATA: 2065 // max data segment size in bytes: make up a number 2066 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 2067 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2068 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2069 break; 2070 default: 2071 warn("prlimit: unimplemented resource %d", resource); 2072 return -EINVAL; 2073 break; 2074 } 2075 rlp.copyOut(tc->getMemProxy()); 2076 } 2077 return 0; 2078} 2079 2080/// Target clock_gettime() function. 2081template <class OS> 2082SyscallReturn 2083clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2084{ 2085 int index = 1; 2086 //int clk_id = p->getSyscallArg(tc, index); 2087 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 2088 2089 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 2090 tp->tv_sec += seconds_since_epoch; 2091 tp->tv_sec = TheISA::htog(tp->tv_sec); 2092 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 2093 2094 tp.copyOut(tc->getMemProxy()); 2095 2096 return 0; 2097} 2098 2099/// Target clock_getres() function. 2100template <class OS> 2101SyscallReturn 2102clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2103{ 2104 int index = 1; 2105 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 2106 2107 // Set resolution at ns, which is what clock_gettime() returns 2108 tp->tv_sec = 0; 2109 tp->tv_nsec = 1; 2110 2111 tp.copyOut(tc->getMemProxy()); 2112 2113 return 0; 2114} 2115 2116/// Target gettimeofday() handler. 2117template <class OS> 2118SyscallReturn 2119gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 2120 ThreadContext *tc) 2121{ 2122 int index = 0; 2123 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 2124 2125 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 2126 tp->tv_sec += seconds_since_epoch; 2127 tp->tv_sec = TheISA::htog(tp->tv_sec); 2128 tp->tv_usec = TheISA::htog(tp->tv_usec); 2129 2130 tp.copyOut(tc->getMemProxy()); 2131 2132 return 0; 2133} 2134 2135 2136/// Target utimes() handler. 2137template <class OS> 2138SyscallReturn 2139utimesFunc(SyscallDesc *desc, int callnum, Process *process, 2140 ThreadContext *tc) 2141{ 2142 std::string path; 2143 2144 int index = 0; 2145 if (!tc->getMemProxy().tryReadString(path, 2146 process->getSyscallArg(tc, index))) { 2147 return -EFAULT; 2148 } 2149 2150 TypedBufferArg<typename OS::timeval [2]> 2151 tp(process->getSyscallArg(tc, index)); 2152 tp.copyIn(tc->getMemProxy()); 2153 2154 struct timeval hostTimeval[2]; 2155 for (int i = 0; i < 2; ++i) { 2156 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 2157 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 2158 } 2159 2160 // Adjust path for cwd and redirection 2161 path = process->checkPathRedirect(path); 2162 2163 int result = utimes(path.c_str(), hostTimeval); 2164 2165 if (result < 0) 2166 return -errno; 2167 2168 return 0; 2169} 2170 2171template <class OS> 2172SyscallReturn 2173execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 2174{ 2175 desc->setFlags(0); 2176 2177 int index = 0; 2178 std::string path; 2179 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 2180 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 2181 return -EFAULT; 2182 2183 if (access(path.c_str(), F_OK) == -1) 2184 return -EACCES; 2185 2186 auto read_in = [](std::vector<std::string> & vect, 2187 SETranslatingPortProxy & mem_proxy, 2188 Addr mem_loc) 2189 { 2190 for (int inc = 0; ; inc++) { 2191 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 2192 b.copyIn(mem_proxy); 2193 2194 if (!*(Addr*)b.bufferPtr()) 2195 break; 2196 2197 vect.push_back(std::string()); 2198 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 2199 } 2200 }; 2201 2202 /** 2203 * Note that ProcessParams is generated by swig and there are no other 2204 * examples of how to create anything but this default constructor. The 2205 * fields are manually initialized instead of passing parameters to the 2206 * constructor. 2207 */ 2208 ProcessParams *pp = new ProcessParams(); 2209 pp->executable = path; 2210 Addr argv_mem_loc = p->getSyscallArg(tc, index); 2211 read_in(pp->cmd, mem_proxy, argv_mem_loc); 2212 Addr envp_mem_loc = p->getSyscallArg(tc, index); 2213 read_in(pp->env, mem_proxy, envp_mem_loc); 2214 pp->uid = p->uid(); 2215 pp->egid = p->egid(); 2216 pp->euid = p->euid(); 2217 pp->gid = p->gid(); 2218 pp->ppid = p->ppid(); 2219 pp->pid = p->pid(); 2220 pp->input.assign("cin"); 2221 pp->output.assign("cout"); 2222 pp->errout.assign("cerr"); 2223 pp->cwd.assign(p->tgtCwd); 2224 pp->system = p->system; 2225 /** 2226 * Prevent process object creation with identical PIDs (which will trip 2227 * a fatal check in Process constructor). The execve call is supposed to 2228 * take over the currently executing process' identity but replace 2229 * whatever it is doing with a new process image. Instead of hijacking 2230 * the process object in the simulator, we create a new process object 2231 * and bind to the previous process' thread below (hijacking the thread). 2232 */ 2233 p->system->PIDs.erase(p->pid()); 2234 Process *new_p = pp->create(); 2235 delete pp; 2236 2237 /** 2238 * Work through the file descriptor array and close any files marked 2239 * close-on-exec. 2240 */ 2241 new_p->fds = p->fds; 2242 for (int i = 0; i < new_p->fds->getSize(); i++) { 2243 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 2244 if (fdep && fdep->getCOE()) 2245 new_p->fds->closeFDEntry(i); 2246 } 2247 2248 *new_p->sigchld = true; 2249 2250 delete p; 2251 tc->clearArchRegs(); 2252 tc->setProcessPtr(new_p); 2253 new_p->assignThreadContext(tc->contextId()); 2254 new_p->initState(); 2255 tc->activate(); 2256 TheISA::PCState pcState = tc->pcState(); 2257 tc->setNPC(pcState.instAddr()); 2258 2259 desc->setFlags(SyscallDesc::SuppressReturnValue); 2260 return 0; 2261} 2262 2263/// Target getrusage() function. 2264template <class OS> 2265SyscallReturn 2266getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 2267 ThreadContext *tc) 2268{ 2269 int index = 0; 2270 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 2271 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2272 2273 rup->ru_utime.tv_sec = 0; 2274 rup->ru_utime.tv_usec = 0; 2275 rup->ru_stime.tv_sec = 0; 2276 rup->ru_stime.tv_usec = 0; 2277 rup->ru_maxrss = 0; 2278 rup->ru_ixrss = 0; 2279 rup->ru_idrss = 0; 2280 rup->ru_isrss = 0; 2281 rup->ru_minflt = 0; 2282 rup->ru_majflt = 0; 2283 rup->ru_nswap = 0; 2284 rup->ru_inblock = 0; 2285 rup->ru_oublock = 0; 2286 rup->ru_msgsnd = 0; 2287 rup->ru_msgrcv = 0; 2288 rup->ru_nsignals = 0; 2289 rup->ru_nvcsw = 0; 2290 rup->ru_nivcsw = 0; 2291 2292 switch (who) { 2293 case OS::TGT_RUSAGE_SELF: 2294 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 2295 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 2296 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 2297 break; 2298 2299 case OS::TGT_RUSAGE_CHILDREN: 2300 // do nothing. We have no child processes, so they take no time. 2301 break; 2302 2303 default: 2304 // don't really handle THREAD or CHILDREN, but just warn and 2305 // plow ahead 2306 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 2307 who); 2308 } 2309 2310 rup.copyOut(tc->getMemProxy()); 2311 2312 return 0; 2313} 2314 2315/// Target times() function. 2316template <class OS> 2317SyscallReturn 2318timesFunc(SyscallDesc *desc, int callnum, Process *process, 2319 ThreadContext *tc) 2320{ 2321 int index = 0; 2322 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 2323 2324 // Fill in the time structure (in clocks) 2325 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 2326 bufp->tms_utime = clocks; 2327 bufp->tms_stime = 0; 2328 bufp->tms_cutime = 0; 2329 bufp->tms_cstime = 0; 2330 2331 // Convert to host endianness 2332 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 2333 2334 // Write back 2335 bufp.copyOut(tc->getMemProxy()); 2336 2337 // Return clock ticks since system boot 2338 return clocks; 2339} 2340 2341/// Target time() function. 2342template <class OS> 2343SyscallReturn 2344timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 2345{ 2346 typename OS::time_t sec, usec; 2347 getElapsedTimeMicro(sec, usec); 2348 sec += seconds_since_epoch; 2349 2350 int index = 0; 2351 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2352 if (taddr != 0) { 2353 typename OS::time_t t = sec; 2354 t = TheISA::htog(t); 2355 SETranslatingPortProxy &p = tc->getMemProxy(); 2356 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2357 } 2358 return sec; 2359} 2360 2361template <class OS> 2362SyscallReturn 2363tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 2364{ 2365 int index = 0; 2366 int tgid = process->getSyscallArg(tc, index); 2367 int tid = process->getSyscallArg(tc, index); 2368 int sig = process->getSyscallArg(tc, index); 2369 2370 /** 2371 * This system call is intended to allow killing a specific thread 2372 * within an arbitrary thread group if sanctioned with permission checks. 2373 * It's usually true that threads share the termination signal as pointed 2374 * out by the pthread_kill man page and this seems to be the intended 2375 * usage. Due to this being an emulated environment, assume the following: 2376 * Threads are allowed to call tgkill because the EUID for all threads 2377 * should be the same. There is no signal handling mechanism for kernel 2378 * registration of signal handlers since signals are poorly supported in 2379 * emulation mode. Since signal handlers cannot be registered, all 2380 * threads within in a thread group must share the termination signal. 2381 * We never exhaust PIDs so there's no chance of finding the wrong one 2382 * due to PID rollover. 2383 */ 2384 2385 System *sys = tc->getSystemPtr(); 2386 Process *tgt_proc = nullptr; 2387 for (int i = 0; i < sys->numContexts(); i++) { 2388 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2389 if (temp->pid() == tid) { 2390 tgt_proc = temp; 2391 break; 2392 } 2393 } 2394 2395 if (sig != 0 || sig != OS::TGT_SIGABRT) 2396 return -EINVAL; 2397 2398 if (tgt_proc == nullptr) 2399 return -ESRCH; 2400 2401 if (tgid != -1 && tgt_proc->tgid() != tgid) 2402 return -ESRCH; 2403 2404 if (sig == OS::TGT_SIGABRT) 2405 exitGroupFunc(desc, 252, process, tc); 2406 2407 return 0; 2408} 2409 2410template <class OS> 2411SyscallReturn 2412socketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2413{ 2414 int index = 0; 2415 int domain = p->getSyscallArg(tc, index); 2416 int type = p->getSyscallArg(tc, index); 2417 int prot = p->getSyscallArg(tc, index); 2418 2419 int sim_fd = socket(domain, type, prot); 2420 if (sim_fd == -1) 2421 return -errno; 2422 2423 auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 2424 int tgt_fd = p->fds->allocFD(sfdp); 2425 2426 return tgt_fd; 2427} 2428 2429template <class OS> 2430SyscallReturn 2431socketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2432{ 2433 int index = 0; 2434 int domain = p->getSyscallArg(tc, index); 2435 int type = p->getSyscallArg(tc, index); 2436 int prot = p->getSyscallArg(tc, index); 2437 Addr svPtr = p->getSyscallArg(tc, index); 2438 2439 BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 2440 int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 2441 if (status == -1) 2442 return -errno; 2443 2444 int *fds = (int *)svBuf.bufferPtr(); 2445 2446 auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 2447 fds[0] = p->fds->allocFD(sfdp1); 2448 auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 2449 fds[1] = p->fds->allocFD(sfdp2); 2450 svBuf.copyOut(tc->getMemProxy()); 2451 2452 return status; 2453} 2454 2455template <class OS> 2456SyscallReturn 2457selectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 2458{ 2459 int retval; 2460 2461 int index = 0; 2462 int nfds_t = p->getSyscallArg(tc, index); 2463 Addr fds_read_ptr = p->getSyscallArg(tc, index); 2464 Addr fds_writ_ptr = p->getSyscallArg(tc, index); 2465 Addr fds_excp_ptr = p->getSyscallArg(tc, index); 2466 Addr time_val_ptr = p->getSyscallArg(tc, index); 2467 2468 TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 2469 TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 2470 TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 2471 TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 2472 2473 /** 2474 * Host fields. Notice that these use the definitions from the system 2475 * headers instead of the gem5 headers and libraries. If the host and 2476 * target have different header file definitions, this will not work. 2477 */ 2478 fd_set rd_h; 2479 FD_ZERO(&rd_h); 2480 fd_set wr_h; 2481 FD_ZERO(&wr_h); 2482 fd_set ex_h; 2483 FD_ZERO(&ex_h); 2484 2485 /** 2486 * Copy in the fd_set from the target. 2487 */ 2488 if (fds_read_ptr) 2489 rd_t.copyIn(tc->getMemProxy()); 2490 if (fds_writ_ptr) 2491 wr_t.copyIn(tc->getMemProxy()); 2492 if (fds_excp_ptr) 2493 ex_t.copyIn(tc->getMemProxy()); 2494 2495 /** 2496 * We need to translate the target file descriptor set into a host file 2497 * descriptor set. This involves both our internal process fd array 2498 * and the fd_set defined in Linux header files. The nfds field also 2499 * needs to be updated as it will be only target specific after 2500 * retrieving it from the target; the nfds value is expected to be the 2501 * highest file descriptor that needs to be checked, so we need to extend 2502 * it out for nfds_h when we do the update. 2503 */ 2504 int nfds_h = 0; 2505 std::map<int, int> trans_map; 2506 auto try_add_host_set = [&](fd_set *tgt_set_entry, 2507 fd_set *hst_set_entry, 2508 int iter) -> bool 2509 { 2510 /** 2511 * By this point, we know that we are looking at a valid file 2512 * descriptor set on the target. We need to check if the target file 2513 * descriptor value passed in as iter is part of the set. 2514 */ 2515 if (FD_ISSET(iter, tgt_set_entry)) { 2516 /** 2517 * We know that the target file descriptor belongs to the set, 2518 * but we do not yet know if the file descriptor is valid or 2519 * that we have a host mapping. Check that now. 2520 */ 2521 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 2522 if (!hbfdp) 2523 return true; 2524 auto sim_fd = hbfdp->getSimFD(); 2525 2526 /** 2527 * Add the sim_fd to tgt_fd translation into trans_map for use 2528 * later when we need to zero the target fd_set structures and 2529 * then update them with hits returned from the host select call. 2530 */ 2531 trans_map[sim_fd] = iter; 2532 2533 /** 2534 * We know that the host file descriptor exists so now we check 2535 * if we need to update the max count for nfds_h before passing 2536 * the duplicated structure into the host. 2537 */ 2538 nfds_h = std::max(nfds_h - 1, sim_fd + 1); 2539 2540 /** 2541 * Add the host file descriptor to the set that we are going to 2542 * pass into the host. 2543 */ 2544 FD_SET(sim_fd, hst_set_entry); 2545 } 2546 return false; 2547 }; 2548 2549 for (int i = 0; i < nfds_t; i++) { 2550 if (fds_read_ptr) { 2551 bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 2552 if (ebadf) return -EBADF; 2553 } 2554 if (fds_writ_ptr) { 2555 bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 2556 if (ebadf) return -EBADF; 2557 } 2558 if (fds_excp_ptr) { 2559 bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 2560 if (ebadf) return -EBADF; 2561 } 2562 } 2563 2564 if (time_val_ptr) { 2565 /** 2566 * It might be possible to decrement the timeval based on some 2567 * derivation of wall clock determined from elapsed simulator ticks 2568 * but that seems like overkill. Rather, we just set the timeval with 2569 * zero timeout. (There is no reason to block during the simulation 2570 * as it only decreases simulator performance.) 2571 */ 2572 tp->tv_sec = 0; 2573 tp->tv_usec = 0; 2574 2575 retval = select(nfds_h, 2576 fds_read_ptr ? &rd_h : nullptr, 2577 fds_writ_ptr ? &wr_h : nullptr, 2578 fds_excp_ptr ? &ex_h : nullptr, 2579 (timeval*)&*tp); 2580 } else { 2581 /** 2582 * If the timeval pointer is null, setup a new timeval structure to 2583 * pass into the host select call. Unfortunately, we will need to 2584 * manually check the return value and throw a retry fault if the 2585 * return value is zero. Allowing the system call to block will 2586 * likely deadlock the event queue. 2587 */ 2588 struct timeval tv = { 0, 0 }; 2589 2590 retval = select(nfds_h, 2591 fds_read_ptr ? &rd_h : nullptr, 2592 fds_writ_ptr ? &wr_h : nullptr, 2593 fds_excp_ptr ? &ex_h : nullptr, 2594 &tv); 2595 2596 if (retval == 0) { 2597 /** 2598 * If blocking indefinitely, check the signal list to see if a 2599 * signal would break the poll out of the retry cycle and try to 2600 * return the signal interrupt instead. 2601 */ 2602 for (auto sig : tc->getSystemPtr()->signalList) 2603 if (sig.receiver == p) 2604 return -EINTR; 2605 return SyscallReturn::retry(); 2606 } 2607 } 2608 2609 if (retval == -1) 2610 return -errno; 2611 2612 FD_ZERO((fd_set*)&*rd_t); 2613 FD_ZERO((fd_set*)&*wr_t); 2614 FD_ZERO((fd_set*)&*ex_t); 2615 2616 /** 2617 * We need to translate the host file descriptor set into a target file 2618 * descriptor set. This involves both our internal process fd array 2619 * and the fd_set defined in header files. 2620 */ 2621 for (int i = 0; i < nfds_h; i++) { 2622 if (fds_read_ptr) { 2623 if (FD_ISSET(i, &rd_h)) 2624 FD_SET(trans_map[i], (fd_set*)&*rd_t); 2625 } 2626 2627 if (fds_writ_ptr) { 2628 if (FD_ISSET(i, &wr_h)) 2629 FD_SET(trans_map[i], (fd_set*)&*wr_t); 2630 } 2631 2632 if (fds_excp_ptr) { 2633 if (FD_ISSET(i, &ex_h)) 2634 FD_SET(trans_map[i], (fd_set*)&*ex_t); 2635 } 2636 } 2637 2638 if (fds_read_ptr) 2639 rd_t.copyOut(tc->getMemProxy()); 2640 if (fds_writ_ptr) 2641 wr_t.copyOut(tc->getMemProxy()); 2642 if (fds_excp_ptr) 2643 ex_t.copyOut(tc->getMemProxy()); 2644 if (time_val_ptr) 2645 tp.copyOut(tc->getMemProxy()); 2646 2647 return retval; 2648} 2649 2650template <class OS> 2651SyscallReturn 2652readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2653{ 2654 int index = 0; 2655 int tgt_fd = p->getSyscallArg(tc, index); 2656 Addr buf_ptr = p->getSyscallArg(tc, index); 2657 int nbytes = p->getSyscallArg(tc, index); 2658 2659 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2660 if (!hbfdp) 2661 return -EBADF; 2662 int sim_fd = hbfdp->getSimFD(); 2663 2664 struct pollfd pfd; 2665 pfd.fd = sim_fd; 2666 pfd.events = POLLIN | POLLPRI; 2667 if ((poll(&pfd, 1, 0) == 0) 2668 && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2669 return SyscallReturn::retry(); 2670 2671 BufferArg buf_arg(buf_ptr, nbytes); 2672 int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 2673 2674 if (bytes_read > 0) 2675 buf_arg.copyOut(tc->getMemProxy()); 2676 2677 return (bytes_read == -1) ? -errno : bytes_read; 2678} 2679 2680template <class OS> 2681SyscallReturn 2682writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2683{ 2684 int index = 0; 2685 int tgt_fd = p->getSyscallArg(tc, index); 2686 Addr buf_ptr = p->getSyscallArg(tc, index); 2687 int nbytes = p->getSyscallArg(tc, index); 2688 2689 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2690 if (!hbfdp) 2691 return -EBADF; 2692 int sim_fd = hbfdp->getSimFD(); 2693 2694 BufferArg buf_arg(buf_ptr, nbytes); 2695 buf_arg.copyIn(tc->getMemProxy()); 2696 2697 struct pollfd pfd; 2698 pfd.fd = sim_fd; 2699 pfd.events = POLLOUT; 2700 2701 /** 2702 * We don't want to poll on /dev/random. The kernel will not enable the 2703 * file descriptor for writing unless the entropy in the system falls 2704 * below write_wakeup_threshold. This is not guaranteed to happen 2705 * depending on host settings. 2706 */ 2707 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 2708 if (ffdp && (ffdp->getFileName() != "/dev/random")) { 2709 if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 2710 return SyscallReturn::retry(); 2711 } 2712 2713 int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 2714 2715 if (bytes_written != -1) 2716 fsync(sim_fd); 2717 2718 return (bytes_written == -1) ? -errno : bytes_written; 2719} 2720 2721template <class OS> 2722SyscallReturn 2723wait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2724{ 2725 int index = 0; 2726 pid_t pid = p->getSyscallArg(tc, index); 2727 Addr statPtr = p->getSyscallArg(tc, index); 2728 int options = p->getSyscallArg(tc, index); 2729 Addr rusagePtr = p->getSyscallArg(tc, index); 2730 2731 if (rusagePtr) 2732 DPRINTF_SYSCALL(Verbose, "wait4: rusage pointer provided %lx, however " 2733 "functionality not supported. Ignoring rusage pointer.\n", 2734 rusagePtr); 2735 2736 /** 2737 * Currently, wait4 is only implemented so that it will wait for children 2738 * exit conditions which are denoted by a SIGCHLD signals posted into the 2739 * system signal list. We return no additional information via any of the 2740 * parameters supplied to wait4. If nothing is found in the system signal 2741 * list, we will wait indefinitely for SIGCHLD to post by retrying the 2742 * call. 2743 */ 2744 System *sysh = tc->getSystemPtr(); 2745 std::list<BasicSignal>::iterator iter; 2746 for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 2747 if (iter->receiver == p) { 2748 if (pid < -1) { 2749 if ((iter->sender->pgid() == -pid) 2750 && (iter->signalValue == OS::TGT_SIGCHLD)) 2751 goto success; 2752 } else if (pid == -1) { 2753 if (iter->signalValue == OS::TGT_SIGCHLD) 2754 goto success; 2755 } else if (pid == 0) { 2756 if ((iter->sender->pgid() == p->pgid()) 2757 && (iter->signalValue == OS::TGT_SIGCHLD)) 2758 goto success; 2759 } else { 2760 if ((iter->sender->pid() == pid) 2761 && (iter->signalValue == OS::TGT_SIGCHLD)) 2762 goto success; 2763 } 2764 } 2765 } 2766 2767 return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 2768 2769success: 2770 // Set status to EXITED for WIFEXITED evaluations. 2771 const int EXITED = 0; 2772 BufferArg statusBuf(statPtr, sizeof(int)); 2773 *(int *)statusBuf.bufferPtr() = EXITED; 2774 statusBuf.copyOut(tc->getMemProxy()); 2775 2776 // Return the child PID. 2777 pid_t retval = iter->sender->pid(); 2778 sysh->signalList.erase(iter); 2779 return retval; 2780} 2781 2782template <class OS> 2783SyscallReturn 2784acceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2785{ 2786 struct sockaddr sa; 2787 socklen_t addrLen; 2788 int host_fd; 2789 int index = 0; 2790 int tgt_fd = p->getSyscallArg(tc, index); 2791 Addr addrPtr = p->getSyscallArg(tc, index); 2792 Addr lenPtr = p->getSyscallArg(tc, index); 2793 2794 BufferArg *lenBufPtr = nullptr; 2795 BufferArg *addrBufPtr = nullptr; 2796 2797 auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 2798 if (!sfdp) 2799 return -EBADF; 2800 int sim_fd = sfdp->getSimFD(); 2801 2802 /** 2803 * We poll the socket file descriptor first to guarantee that we do not 2804 * block on our accept call. The socket can be opened without the 2805 * non-blocking flag (it blocks). This will cause deadlocks between 2806 * communicating processes. 2807 */ 2808 struct pollfd pfd; 2809 pfd.fd = sim_fd; 2810 pfd.events = POLLIN | POLLPRI; 2811 if ((poll(&pfd, 1, 0) == 0) 2812 && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2813 return SyscallReturn::retry(); 2814 2815 if (lenPtr) { 2816 lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 2817 lenBufPtr->copyIn(tc->getMemProxy()); 2818 memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 2819 sizeof(socklen_t)); 2820 } 2821 2822 if (addrPtr) { 2823 addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 2824 addrBufPtr->copyIn(tc->getMemProxy()); 2825 memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 2826 sizeof(struct sockaddr)); 2827 } 2828 2829 host_fd = accept(sim_fd, &sa, &addrLen); 2830 2831 if (host_fd == -1) 2832 return -errno; 2833 2834 if (addrPtr) { 2835 memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 2836 addrBufPtr->copyOut(tc->getMemProxy()); 2837 delete(addrBufPtr); 2838 } 2839 2840 if (lenPtr) { 2841 *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 2842 lenBufPtr->copyOut(tc->getMemProxy()); 2843 delete(lenBufPtr); 2844 } 2845 2846 auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 2847 sfdp->_type, sfdp->_protocol); 2848 return p->fds->allocFD(afdp); 2849} 2850 2851/// Target eventfd() function. 2852template <class OS> 2853SyscallReturn 2854eventfdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2855{ 2856#if defined(__linux__) 2857 int index = 0; 2858 unsigned initval = p->getSyscallArg(tc, index); 2859 int in_flags = p->getSyscallArg(tc, index); 2860 2861 int sim_fd = eventfd(initval, in_flags); 2862 if (sim_fd == -1) 2863 return -errno; 2864 2865 bool cloexec = in_flags & OS::TGT_O_CLOEXEC; 2866 2867 int flags = cloexec ? OS::TGT_O_CLOEXEC : 0; 2868 flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0; 2869 2870 auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec); 2871 int tgt_fd = p->fds->allocFD(hbfdp); 2872 return tgt_fd; 2873#else 2874 warnUnsupportedOS("eventfd"); 2875 return -1; 2876#endif 2877} 2878 2879#endif // __SIM_SYSCALL_EMUL_HH__ 2880