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