syscall_emul.hh revision 12796
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#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 57 defined(__FreeBSD__) || defined(__NetBSD__)) 58#define NO_STATFS 1 59#else 60#define NO_STATFS 0 61#endif 62 63#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 64 defined(__FreeBSD__) || defined(__NetBSD__)) 65#define NO_FALLOCATE 1 66#else 67#define NO_FALLOCATE 0 68#endif 69 70/// 71/// @file syscall_emul.hh 72/// 73/// This file defines objects used to emulate syscalls from the target 74/// application on the host machine. 75 76#ifdef __CYGWIN32__ 77#include <sys/fcntl.h> 78 79#endif 80#include <fcntl.h> 81#include <sys/mman.h> 82#include <sys/stat.h> 83#if (NO_STATFS == 0) 84#include <sys/statfs.h> 85#else 86#include <sys/mount.h> 87#endif 88#include <sys/time.h> 89#include <sys/uio.h> 90#include <unistd.h> 91 92#include <cerrno> 93#include <memory> 94#include <string> 95 96#include "arch/generic/tlb.hh" 97#include "arch/utility.hh" 98#include "base/intmath.hh" 99#include "base/loader/object_file.hh" 100#include "base/logging.hh" 101#include "base/trace.hh" 102#include "base/types.hh" 103#include "config/the_isa.hh" 104#include "cpu/base.hh" 105#include "cpu/thread_context.hh" 106#include "mem/page_table.hh" 107#include "params/Process.hh" 108#include "sim/emul_driver.hh" 109#include "sim/futex_map.hh" 110#include "sim/process.hh" 111#include "sim/syscall_debug_macros.hh" 112#include "sim/syscall_desc.hh" 113#include "sim/syscall_emul_buf.hh" 114#include "sim/syscall_return.hh" 115 116////////////////////////////////////////////////////////////////////// 117// 118// The following emulation functions are generic enough that they 119// don't need to be recompiled for different emulated OS's. They are 120// defined in sim/syscall_emul.cc. 121// 122////////////////////////////////////////////////////////////////////// 123 124 125/// Handler for unimplemented syscalls that we haven't thought about. 126SyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 127 Process *p, ThreadContext *tc); 128 129/// Handler for unimplemented syscalls that we never intend to 130/// implement (signal handling, etc.) and should not affect the correct 131/// behavior of the program. Print a warning only if the appropriate 132/// trace flag is enabled. Return success to the target program. 133SyscallReturn ignoreFunc(SyscallDesc *desc, int num, 134 Process *p, ThreadContext *tc); 135 136// Target fallocateFunc() handler. 137SyscallReturn fallocateFunc(SyscallDesc *desc, int num, 138 Process *p, ThreadContext *tc); 139 140/// Target exit() handler: terminate current context. 141SyscallReturn exitFunc(SyscallDesc *desc, int num, 142 Process *p, ThreadContext *tc); 143 144/// Target exit_group() handler: terminate simulation. (exit all threads) 145SyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 146 Process *p, ThreadContext *tc); 147 148/// Target set_tid_address() handler. 149SyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 150 Process *p, ThreadContext *tc); 151 152/// Target getpagesize() handler. 153SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 154 Process *p, ThreadContext *tc); 155 156/// Target brk() handler: set brk address. 157SyscallReturn brkFunc(SyscallDesc *desc, int num, 158 Process *p, ThreadContext *tc); 159 160/// Target close() handler. 161SyscallReturn closeFunc(SyscallDesc *desc, int num, 162 Process *p, ThreadContext *tc); 163 164// Target read() handler. 165SyscallReturn readFunc(SyscallDesc *desc, int num, 166 Process *p, ThreadContext *tc); 167 168/// Target write() handler. 169SyscallReturn writeFunc(SyscallDesc *desc, int num, 170 Process *p, ThreadContext *tc); 171 172/// Target lseek() handler. 173SyscallReturn lseekFunc(SyscallDesc *desc, int num, 174 Process *p, ThreadContext *tc); 175 176/// Target _llseek() handler. 177SyscallReturn _llseekFunc(SyscallDesc *desc, int num, 178 Process *p, ThreadContext *tc); 179 180/// Target munmap() handler. 181SyscallReturn munmapFunc(SyscallDesc *desc, int num, 182 Process *p, ThreadContext *tc); 183 184/// Target gethostname() handler. 185SyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 186 Process *p, ThreadContext *tc); 187 188/// Target getcwd() handler. 189SyscallReturn getcwdFunc(SyscallDesc *desc, int num, 190 Process *p, ThreadContext *tc); 191 192/// Target readlink() handler. 193SyscallReturn readlinkFunc(SyscallDesc *desc, int num, 194 Process *p, ThreadContext *tc, 195 int index = 0); 196SyscallReturn readlinkFunc(SyscallDesc *desc, int num, 197 Process *p, ThreadContext *tc); 198 199/// Target unlink() handler. 200SyscallReturn unlinkHelper(SyscallDesc *desc, int num, 201 Process *p, ThreadContext *tc, 202 int index); 203SyscallReturn unlinkFunc(SyscallDesc *desc, int num, 204 Process *p, ThreadContext *tc); 205 206/// Target link() handler 207SyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p, 208 ThreadContext *tc); 209 210/// Target symlink() handler. 211SyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p, 212 ThreadContext *tc); 213 214/// Target mkdir() handler. 215SyscallReturn mkdirFunc(SyscallDesc *desc, int num, 216 Process *p, ThreadContext *tc); 217 218/// Target rename() handler. 219SyscallReturn renameFunc(SyscallDesc *desc, int num, 220 Process *p, ThreadContext *tc); 221 222 223/// Target truncate() handler. 224SyscallReturn truncateFunc(SyscallDesc *desc, int num, 225 Process *p, ThreadContext *tc); 226 227 228/// Target ftruncate() handler. 229SyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 230 Process *p, ThreadContext *tc); 231 232 233/// Target truncate64() handler. 234SyscallReturn truncate64Func(SyscallDesc *desc, int num, 235 Process *p, ThreadContext *tc); 236 237/// Target ftruncate64() handler. 238SyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 239 Process *p, ThreadContext *tc); 240 241 242/// Target umask() handler. 243SyscallReturn umaskFunc(SyscallDesc *desc, int num, 244 Process *p, ThreadContext *tc); 245 246/// Target gettid() handler. 247SyscallReturn gettidFunc(SyscallDesc *desc, int num, 248 Process *p, ThreadContext *tc); 249 250/// Target chown() handler. 251SyscallReturn chownFunc(SyscallDesc *desc, int num, 252 Process *p, ThreadContext *tc); 253 254/// Target setpgid() handler. 255SyscallReturn setpgidFunc(SyscallDesc *desc, int num, 256 Process *p, ThreadContext *tc); 257 258/// Target fchown() handler. 259SyscallReturn fchownFunc(SyscallDesc *desc, int num, 260 Process *p, ThreadContext *tc); 261 262/// Target dup() handler. 263SyscallReturn dupFunc(SyscallDesc *desc, int num, 264 Process *process, ThreadContext *tc); 265 266/// Target dup2() handler. 267SyscallReturn dup2Func(SyscallDesc *desc, int num, 268 Process *process, ThreadContext *tc); 269 270/// Target fcntl() handler. 271SyscallReturn fcntlFunc(SyscallDesc *desc, int num, 272 Process *process, ThreadContext *tc); 273 274/// Target fcntl64() handler. 275SyscallReturn fcntl64Func(SyscallDesc *desc, int num, 276 Process *process, ThreadContext *tc); 277 278/// Target setuid() handler. 279SyscallReturn setuidFunc(SyscallDesc *desc, int num, 280 Process *p, ThreadContext *tc); 281 282/// Target pipe() handler. 283SyscallReturn pipeFunc(SyscallDesc *desc, int num, 284 Process *p, ThreadContext *tc); 285 286/// Internal pipe() handler. 287SyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 288 ThreadContext *tc, bool pseudoPipe); 289 290/// Target getpid() handler. 291SyscallReturn getpidFunc(SyscallDesc *desc, int num, 292 Process *p, ThreadContext *tc); 293 294/// Target getuid() handler. 295SyscallReturn getuidFunc(SyscallDesc *desc, int num, 296 Process *p, ThreadContext *tc); 297 298/// Target getgid() handler. 299SyscallReturn getgidFunc(SyscallDesc *desc, int num, 300 Process *p, ThreadContext *tc); 301 302/// Target getppid() handler. 303SyscallReturn getppidFunc(SyscallDesc *desc, int num, 304 Process *p, ThreadContext *tc); 305 306/// Target geteuid() handler. 307SyscallReturn geteuidFunc(SyscallDesc *desc, int num, 308 Process *p, ThreadContext *tc); 309 310/// Target getegid() handler. 311SyscallReturn getegidFunc(SyscallDesc *desc, int num, 312 Process *p, ThreadContext *tc); 313 314/// Target access() handler 315SyscallReturn accessFunc(SyscallDesc *desc, int num, 316 Process *p, ThreadContext *tc); 317SyscallReturn accessFunc(SyscallDesc *desc, int num, 318 Process *p, ThreadContext *tc, 319 int index); 320 321/// Futex system call 322/// Implemented by Daniel Sanchez 323/// Used by printf's in multi-threaded apps 324template <class OS> 325SyscallReturn 326futexFunc(SyscallDesc *desc, int callnum, Process *process, 327 ThreadContext *tc) 328{ 329 using namespace std; 330 331 int index = 0; 332 Addr uaddr = process->getSyscallArg(tc, index); 333 int op = process->getSyscallArg(tc, index); 334 int val = process->getSyscallArg(tc, index); 335 336 /* 337 * Unsupported option that does not affect the correctness of the 338 * application. This is a performance optimization utilized by Linux. 339 */ 340 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 341 342 FutexMap &futex_map = tc->getSystemPtr()->futexMap; 343 344 if (OS::TGT_FUTEX_WAIT == op) { 345 // Ensure futex system call accessed atomically. 346 BufferArg buf(uaddr, sizeof(int)); 347 buf.copyIn(tc->getMemProxy()); 348 int mem_val = *(int*)buf.bufferPtr(); 349 350 /* 351 * The value in memory at uaddr is not equal with the expected val 352 * (a different thread must have changed it before the system call was 353 * invoked). In this case, we need to throw an error. 354 */ 355 if (val != mem_val) 356 return -OS::TGT_EWOULDBLOCK; 357 358 futex_map.suspend(uaddr, process->tgid(), tc); 359 360 return 0; 361 } else if (OS::TGT_FUTEX_WAKE == op) { 362 return futex_map.wakeup(uaddr, process->tgid(), val); 363 } 364 365 warn("futex: op %d not implemented; ignoring.", op); 366 return -ENOSYS; 367} 368 369 370/// Pseudo Funcs - These functions use a different return convension, 371/// returning a second value in a register other than the normal return register 372SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 373 Process *process, ThreadContext *tc); 374 375/// Target getpidPseudo() handler. 376SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 377 Process *p, ThreadContext *tc); 378 379/// Target getuidPseudo() handler. 380SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 381 Process *p, ThreadContext *tc); 382 383/// Target getgidPseudo() handler. 384SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 385 Process *p, ThreadContext *tc); 386 387 388/// A readable name for 1,000,000, for converting microseconds to seconds. 389const int one_million = 1000000; 390/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 391const int one_billion = 1000000000; 392 393/// Approximate seconds since the epoch (1/1/1970). About a billion, 394/// by my reckoning. We want to keep this a constant (not use the 395/// real-world time) to keep simulations repeatable. 396const unsigned seconds_since_epoch = 1000000000; 397 398/// Helper function to convert current elapsed time to seconds and 399/// microseconds. 400template <class T1, class T2> 401void 402getElapsedTimeMicro(T1 &sec, T2 &usec) 403{ 404 uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 405 sec = elapsed_usecs / one_million; 406 usec = elapsed_usecs % one_million; 407} 408 409/// Helper function to convert current elapsed time to seconds and 410/// nanoseconds. 411template <class T1, class T2> 412void 413getElapsedTimeNano(T1 &sec, T2 &nsec) 414{ 415 uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 416 sec = elapsed_nsecs / one_billion; 417 nsec = elapsed_nsecs % one_billion; 418} 419 420////////////////////////////////////////////////////////////////////// 421// 422// The following emulation functions are generic, but need to be 423// templated to account for differences in types, constants, etc. 424// 425////////////////////////////////////////////////////////////////////// 426 427 typedef struct statfs hst_statfs; 428#if NO_STAT64 429 typedef struct stat hst_stat; 430 typedef struct stat hst_stat64; 431#else 432 typedef struct stat hst_stat; 433 typedef struct stat64 hst_stat64; 434#endif 435 436//// Helper function to convert a host stat buffer to a target stat 437//// buffer. Also copies the target buffer out to the simulated 438//// memory space. Used by stat(), fstat(), and lstat(). 439 440template <typename target_stat, typename host_stat> 441void 442convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 443{ 444 using namespace TheISA; 445 446 if (fakeTTY) 447 tgt->st_dev = 0xA; 448 else 449 tgt->st_dev = host->st_dev; 450 tgt->st_dev = TheISA::htog(tgt->st_dev); 451 tgt->st_ino = host->st_ino; 452 tgt->st_ino = TheISA::htog(tgt->st_ino); 453 tgt->st_mode = host->st_mode; 454 if (fakeTTY) { 455 // Claim to be a character device 456 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 457 tgt->st_mode |= S_IFCHR; // Set S_IFCHR 458 } 459 tgt->st_mode = TheISA::htog(tgt->st_mode); 460 tgt->st_nlink = host->st_nlink; 461 tgt->st_nlink = TheISA::htog(tgt->st_nlink); 462 tgt->st_uid = host->st_uid; 463 tgt->st_uid = TheISA::htog(tgt->st_uid); 464 tgt->st_gid = host->st_gid; 465 tgt->st_gid = TheISA::htog(tgt->st_gid); 466 if (fakeTTY) 467 tgt->st_rdev = 0x880d; 468 else 469 tgt->st_rdev = host->st_rdev; 470 tgt->st_rdev = TheISA::htog(tgt->st_rdev); 471 tgt->st_size = host->st_size; 472 tgt->st_size = TheISA::htog(tgt->st_size); 473 tgt->st_atimeX = host->st_atime; 474 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 475 tgt->st_mtimeX = host->st_mtime; 476 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 477 tgt->st_ctimeX = host->st_ctime; 478 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 479 // Force the block size to be 8KB. This helps to ensure buffered io works 480 // consistently across different hosts. 481 tgt->st_blksize = 0x2000; 482 tgt->st_blksize = TheISA::htog(tgt->st_blksize); 483 tgt->st_blocks = host->st_blocks; 484 tgt->st_blocks = TheISA::htog(tgt->st_blocks); 485} 486 487// Same for stat64 488 489template <typename target_stat, typename host_stat64> 490void 491convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 492{ 493 using namespace TheISA; 494 495 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 496#if defined(STAT_HAVE_NSEC) 497 tgt->st_atime_nsec = host->st_atime_nsec; 498 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 499 tgt->st_mtime_nsec = host->st_mtime_nsec; 500 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 501 tgt->st_ctime_nsec = host->st_ctime_nsec; 502 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 503#else 504 tgt->st_atime_nsec = 0; 505 tgt->st_mtime_nsec = 0; 506 tgt->st_ctime_nsec = 0; 507#endif 508} 509 510// Here are a couple of convenience functions 511template<class OS> 512void 513copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 514 hst_stat *host, bool fakeTTY = false) 515{ 516 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 517 tgt_stat_buf tgt(addr); 518 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 519 tgt.copyOut(mem); 520} 521 522template<class OS> 523void 524copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 525 hst_stat64 *host, bool fakeTTY = false) 526{ 527 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 528 tgt_stat_buf tgt(addr); 529 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 530 tgt.copyOut(mem); 531} 532 533template <class OS> 534void 535copyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 536 hst_statfs *host) 537{ 538 TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 539 540 tgt->f_type = TheISA::htog(host->f_type); 541#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 542 tgt->f_bsize = TheISA::htog(host->f_iosize); 543#else 544 tgt->f_bsize = TheISA::htog(host->f_bsize); 545#endif 546 tgt->f_blocks = TheISA::htog(host->f_blocks); 547 tgt->f_bfree = TheISA::htog(host->f_bfree); 548 tgt->f_bavail = TheISA::htog(host->f_bavail); 549 tgt->f_files = TheISA::htog(host->f_files); 550 tgt->f_ffree = TheISA::htog(host->f_ffree); 551 memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 552#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 553 tgt->f_namelen = TheISA::htog(host->f_namemax); 554 tgt->f_frsize = TheISA::htog(host->f_bsize); 555#elif defined(__APPLE__) 556 tgt->f_namelen = 0; 557 tgt->f_frsize = 0; 558#else 559 tgt->f_namelen = TheISA::htog(host->f_namelen); 560 tgt->f_frsize = TheISA::htog(host->f_frsize); 561#endif 562#if defined(__linux__) 563 memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 564#else 565 /* 566 * The fields are different sizes per OS. Don't bother with 567 * f_spare or f_reserved on non-Linux for now. 568 */ 569 memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 570#endif 571 572 tgt.copyOut(mem); 573} 574 575/// Target ioctl() handler. For the most part, programs call ioctl() 576/// only to find out if their stdout is a tty, to determine whether to 577/// do line or block buffering. We always claim that output fds are 578/// not TTYs to provide repeatable results. 579template <class OS> 580SyscallReturn 581ioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 582{ 583 int index = 0; 584 int tgt_fd = p->getSyscallArg(tc, index); 585 unsigned req = p->getSyscallArg(tc, index); 586 587 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 588 589 if (OS::isTtyReq(req)) 590 return -ENOTTY; 591 592 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 593 if (!dfdp) 594 return -EBADF; 595 596 /** 597 * If the driver is valid, issue the ioctl through it. Otherwise, 598 * there's an implicit assumption that the device is a TTY type and we 599 * return that we do not have a valid TTY. 600 */ 601 EmulatedDriver *emul_driver = dfdp->getDriver(); 602 if (emul_driver) 603 return emul_driver->ioctl(p, tc, req); 604 605 /** 606 * For lack of a better return code, return ENOTTY. Ideally, we should 607 * return something better here, but at least we issue the warning. 608 */ 609 warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 610 tgt_fd, req, tc->pcState()); 611 return -ENOTTY; 612} 613 614template <class OS> 615SyscallReturn 616openImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 617 bool isopenat) 618{ 619 int index = 0; 620 int tgt_dirfd = -1; 621 622 /** 623 * If using the openat variant, read in the target directory file 624 * descriptor from the simulated process. 625 */ 626 if (isopenat) 627 tgt_dirfd = p->getSyscallArg(tc, index); 628 629 /** 630 * Retrieve the simulated process' memory proxy and then read in the path 631 * string from that memory space into the host's working memory space. 632 */ 633 std::string path; 634 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 635 return -EFAULT; 636 637#ifdef __CYGWIN32__ 638 int host_flags = O_BINARY; 639#else 640 int host_flags = 0; 641#endif 642 /** 643 * Translate target flags into host flags. Flags exist which are not 644 * ported between architectures which can cause check failures. 645 */ 646 int tgt_flags = p->getSyscallArg(tc, index); 647 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 648 if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 649 tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 650 host_flags |= OS::openFlagTable[i].hostFlag; 651 } 652 } 653 if (tgt_flags) { 654 warn("open%s: cannot decode flags 0x%x", 655 isopenat ? "at" : "", tgt_flags); 656 } 657#ifdef __CYGWIN32__ 658 host_flags |= O_BINARY; 659#endif 660 661 int mode = p->getSyscallArg(tc, index); 662 663 /** 664 * If the simulated process called open or openat with AT_FDCWD specified, 665 * take the current working directory value which was passed into the 666 * process class as a Python parameter and append the current path to 667 * create a full path. 668 * Otherwise, openat with a valid target directory file descriptor has 669 * been called. If the path option, which was passed in as a parameter, 670 * is not absolute, retrieve the directory file descriptor's path and 671 * prepend it to the path passed in as a parameter. 672 * In every case, we should have a full path (which is relevant to the 673 * host) to work with after this block has been passed. 674 */ 675 if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 676 path = p->fullPath(path); 677 } else if (!startswith(path, "/")) { 678 std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 679 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 680 if (!ffdp) 681 return -EBADF; 682 path.insert(0, ffdp->getFileName()); 683 } 684 685 /** 686 * Since this is an emulated environment, we create pseudo file 687 * descriptors for device requests that have been registered with 688 * the process class through Python; this allows us to create a file 689 * descriptor for subsequent ioctl or mmap calls. 690 */ 691 if (startswith(path, "/dev/")) { 692 std::string filename = path.substr(strlen("/dev/")); 693 EmulatedDriver *drv = p->findDriver(filename); 694 if (drv) { 695 DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 696 "driver open with path[%s]\n", 697 isopenat ? "at" : "", path.c_str()); 698 return drv->open(p, tc, mode, host_flags); 699 } 700 /** 701 * Fall through here for pass through to host devices, such 702 * as /dev/zero 703 */ 704 } 705 706 /** 707 * Some special paths and files cannot be called on the host and need 708 * to be handled as special cases inside the simulator. 709 * If the full path that was created above does not match any of the 710 * special cases, pass it through to the open call on the host to let 711 * the host open the file on our behalf. 712 * If the host cannot open the file, return the host's error code back 713 * through the system call to the simulated process. 714 */ 715 int sim_fd = -1; 716 std::vector<std::string> special_paths = 717 { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 718 for (auto entry : special_paths) { 719 if (startswith(path, entry)) 720 sim_fd = OS::openSpecialFile(path, p, tc); 721 } 722 if (sim_fd == -1) { 723 sim_fd = open(path.c_str(), host_flags, mode); 724 } 725 if (sim_fd == -1) { 726 int local = -errno; 727 DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 728 isopenat ? "at" : "", path.c_str()); 729 return local; 730 } 731 732 /** 733 * The file was opened successfully and needs to be recorded in the 734 * process' file descriptor array so that it can be retrieved later. 735 * The target file descriptor that is chosen will be the lowest unused 736 * file descriptor. 737 * Return the indirect target file descriptor back to the simulated 738 * process to act as a handle for the opened file. 739 */ 740 auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 741 int tgt_fd = p->fds->allocFD(ffdp); 742 DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 743 isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 744 return tgt_fd; 745} 746 747/// Target open() handler. 748template <class OS> 749SyscallReturn 750openFunc(SyscallDesc *desc, int callnum, Process *process, 751 ThreadContext *tc) 752{ 753 return openImpl<OS>(desc, callnum, process, tc, false); 754} 755 756/// Target openat() handler. 757template <class OS> 758SyscallReturn 759openatFunc(SyscallDesc *desc, int callnum, Process *process, 760 ThreadContext *tc) 761{ 762 return openImpl<OS>(desc, callnum, process, tc, true); 763} 764 765/// Target unlinkat() handler. 766template <class OS> 767SyscallReturn 768unlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 769 ThreadContext *tc) 770{ 771 int index = 0; 772 int dirfd = process->getSyscallArg(tc, index); 773 if (dirfd != OS::TGT_AT_FDCWD) 774 warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 775 776 return unlinkHelper(desc, callnum, process, tc, 1); 777} 778 779/// Target facessat() handler 780template <class OS> 781SyscallReturn 782faccessatFunc(SyscallDesc *desc, int callnum, Process *process, 783 ThreadContext *tc) 784{ 785 int index = 0; 786 int dirfd = process->getSyscallArg(tc, index); 787 if (dirfd != OS::TGT_AT_FDCWD) 788 warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 789 return accessFunc(desc, callnum, process, tc, 1); 790} 791 792/// Target readlinkat() handler 793template <class OS> 794SyscallReturn 795readlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 796 ThreadContext *tc) 797{ 798 int index = 0; 799 int dirfd = process->getSyscallArg(tc, index); 800 if (dirfd != OS::TGT_AT_FDCWD) 801 warn("openat: first argument not AT_FDCWD; unlikely to work"); 802 return readlinkFunc(desc, callnum, process, tc, 1); 803} 804 805/// Target renameat() handler. 806template <class OS> 807SyscallReturn 808renameatFunc(SyscallDesc *desc, int callnum, Process *process, 809 ThreadContext *tc) 810{ 811 int index = 0; 812 813 int olddirfd = process->getSyscallArg(tc, index); 814 if (olddirfd != OS::TGT_AT_FDCWD) 815 warn("renameat: first argument not AT_FDCWD; unlikely to work"); 816 817 std::string old_name; 818 819 if (!tc->getMemProxy().tryReadString(old_name, 820 process->getSyscallArg(tc, index))) 821 return -EFAULT; 822 823 int newdirfd = process->getSyscallArg(tc, index); 824 if (newdirfd != OS::TGT_AT_FDCWD) 825 warn("renameat: third argument not AT_FDCWD; unlikely to work"); 826 827 std::string new_name; 828 829 if (!tc->getMemProxy().tryReadString(new_name, 830 process->getSyscallArg(tc, index))) 831 return -EFAULT; 832 833 // Adjust path for current working directory 834 old_name = process->fullPath(old_name); 835 new_name = process->fullPath(new_name); 836 837 int result = rename(old_name.c_str(), new_name.c_str()); 838 return (result == -1) ? -errno : result; 839} 840 841/// Target sysinfo() handler. 842template <class OS> 843SyscallReturn 844sysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 845 ThreadContext *tc) 846{ 847 848 int index = 0; 849 TypedBufferArg<typename OS::tgt_sysinfo> 850 sysinfo(process->getSyscallArg(tc, index)); 851 852 sysinfo->uptime = seconds_since_epoch; 853 sysinfo->totalram = process->system->memSize(); 854 sysinfo->mem_unit = 1; 855 856 sysinfo.copyOut(tc->getMemProxy()); 857 858 return 0; 859} 860 861/// Target chmod() handler. 862template <class OS> 863SyscallReturn 864chmodFunc(SyscallDesc *desc, int callnum, Process *process, 865 ThreadContext *tc) 866{ 867 std::string path; 868 869 int index = 0; 870 if (!tc->getMemProxy().tryReadString(path, 871 process->getSyscallArg(tc, index))) { 872 return -EFAULT; 873 } 874 875 uint32_t mode = process->getSyscallArg(tc, index); 876 mode_t hostMode = 0; 877 878 // XXX translate mode flags via OS::something??? 879 hostMode = mode; 880 881 // Adjust path for current working directory 882 path = process->fullPath(path); 883 884 // do the chmod 885 int result = chmod(path.c_str(), hostMode); 886 if (result < 0) 887 return -errno; 888 889 return 0; 890} 891 892 893/// Target fchmod() handler. 894template <class OS> 895SyscallReturn 896fchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 897{ 898 int index = 0; 899 int tgt_fd = p->getSyscallArg(tc, index); 900 uint32_t mode = p->getSyscallArg(tc, index); 901 902 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 903 if (!ffdp) 904 return -EBADF; 905 int sim_fd = ffdp->getSimFD(); 906 907 mode_t hostMode = mode; 908 909 int result = fchmod(sim_fd, hostMode); 910 911 return (result < 0) ? -errno : 0; 912} 913 914/// Target mremap() handler. 915template <class OS> 916SyscallReturn 917mremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 918{ 919 int index = 0; 920 Addr start = process->getSyscallArg(tc, index); 921 uint64_t old_length = process->getSyscallArg(tc, index); 922 uint64_t new_length = process->getSyscallArg(tc, index); 923 uint64_t flags = process->getSyscallArg(tc, index); 924 uint64_t provided_address = 0; 925 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 926 927 if (use_provided_address) 928 provided_address = process->getSyscallArg(tc, index); 929 930 if ((start % TheISA::PageBytes != 0) || 931 (provided_address % TheISA::PageBytes != 0)) { 932 warn("mremap failing: arguments not page aligned"); 933 return -EINVAL; 934 } 935 936 new_length = roundUp(new_length, TheISA::PageBytes); 937 938 if (new_length > old_length) { 939 std::shared_ptr<MemState> mem_state = process->memState; 940 Addr mmap_end = mem_state->getMmapEnd(); 941 942 if ((start + old_length) == mmap_end && 943 (!use_provided_address || provided_address == start)) { 944 // This case cannot occur when growing downward, as 945 // start is greater than or equal to mmap_end. 946 uint64_t diff = new_length - old_length; 947 process->allocateMem(mmap_end, diff); 948 mem_state->setMmapEnd(mmap_end + diff); 949 return start; 950 } else { 951 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 952 warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 953 return -ENOMEM; 954 } else { 955 uint64_t new_start = provided_address; 956 if (!use_provided_address) { 957 new_start = process->mmapGrowsDown() ? 958 mmap_end - new_length : mmap_end; 959 mmap_end = process->mmapGrowsDown() ? 960 new_start : mmap_end + new_length; 961 mem_state->setMmapEnd(mmap_end); 962 } 963 964 process->pTable->remap(start, old_length, new_start); 965 warn("mremapping to new vaddr %08p-%08p, adding %d\n", 966 new_start, new_start + new_length, 967 new_length - old_length); 968 // add on the remaining unallocated pages 969 process->allocateMem(new_start + old_length, 970 new_length - old_length, 971 use_provided_address /* clobber */); 972 if (use_provided_address && 973 ((new_start + new_length > mem_state->getMmapEnd() && 974 !process->mmapGrowsDown()) || 975 (new_start < mem_state->getMmapEnd() && 976 process->mmapGrowsDown()))) { 977 // something fishy going on here, at least notify the user 978 // @todo: increase mmap_end? 979 warn("mmap region limit exceeded with MREMAP_FIXED\n"); 980 } 981 warn("returning %08p as start\n", new_start); 982 return new_start; 983 } 984 } 985 } else { 986 if (use_provided_address && provided_address != start) 987 process->pTable->remap(start, new_length, provided_address); 988 process->pTable->unmap(start + new_length, old_length - new_length); 989 return use_provided_address ? provided_address : start; 990 } 991} 992 993/// Target stat() handler. 994template <class OS> 995SyscallReturn 996statFunc(SyscallDesc *desc, int callnum, Process *process, 997 ThreadContext *tc) 998{ 999 std::string path; 1000 1001 int index = 0; 1002 if (!tc->getMemProxy().tryReadString(path, 1003 process->getSyscallArg(tc, index))) { 1004 return -EFAULT; 1005 } 1006 Addr bufPtr = process->getSyscallArg(tc, index); 1007 1008 // Adjust path for current working directory 1009 path = process->fullPath(path); 1010 1011 struct stat hostBuf; 1012 int result = stat(path.c_str(), &hostBuf); 1013 1014 if (result < 0) 1015 return -errno; 1016 1017 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1018 1019 return 0; 1020} 1021 1022 1023/// Target stat64() handler. 1024template <class OS> 1025SyscallReturn 1026stat64Func(SyscallDesc *desc, int callnum, Process *process, 1027 ThreadContext *tc) 1028{ 1029 std::string path; 1030 1031 int index = 0; 1032 if (!tc->getMemProxy().tryReadString(path, 1033 process->getSyscallArg(tc, index))) 1034 return -EFAULT; 1035 Addr bufPtr = process->getSyscallArg(tc, index); 1036 1037 // Adjust path for current working directory 1038 path = process->fullPath(path); 1039 1040#if NO_STAT64 1041 struct stat hostBuf; 1042 int result = stat(path.c_str(), &hostBuf); 1043#else 1044 struct stat64 hostBuf; 1045 int result = stat64(path.c_str(), &hostBuf); 1046#endif 1047 1048 if (result < 0) 1049 return -errno; 1050 1051 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1052 1053 return 0; 1054} 1055 1056 1057/// Target fstatat64() handler. 1058template <class OS> 1059SyscallReturn 1060fstatat64Func(SyscallDesc *desc, int callnum, Process *process, 1061 ThreadContext *tc) 1062{ 1063 int index = 0; 1064 int dirfd = process->getSyscallArg(tc, index); 1065 if (dirfd != OS::TGT_AT_FDCWD) 1066 warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 1067 1068 std::string path; 1069 if (!tc->getMemProxy().tryReadString(path, 1070 process->getSyscallArg(tc, index))) 1071 return -EFAULT; 1072 Addr bufPtr = process->getSyscallArg(tc, index); 1073 1074 // Adjust path for current working directory 1075 path = process->fullPath(path); 1076 1077#if NO_STAT64 1078 struct stat hostBuf; 1079 int result = stat(path.c_str(), &hostBuf); 1080#else 1081 struct stat64 hostBuf; 1082 int result = stat64(path.c_str(), &hostBuf); 1083#endif 1084 1085 if (result < 0) 1086 return -errno; 1087 1088 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1089 1090 return 0; 1091} 1092 1093 1094/// Target fstat64() handler. 1095template <class OS> 1096SyscallReturn 1097fstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1098{ 1099 int index = 0; 1100 int tgt_fd = p->getSyscallArg(tc, index); 1101 Addr bufPtr = p->getSyscallArg(tc, index); 1102 1103 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1104 if (!ffdp) 1105 return -EBADF; 1106 int sim_fd = ffdp->getSimFD(); 1107 1108#if NO_STAT64 1109 struct stat hostBuf; 1110 int result = fstat(sim_fd, &hostBuf); 1111#else 1112 struct stat64 hostBuf; 1113 int result = fstat64(sim_fd, &hostBuf); 1114#endif 1115 1116 if (result < 0) 1117 return -errno; 1118 1119 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1120 1121 return 0; 1122} 1123 1124 1125/// Target lstat() handler. 1126template <class OS> 1127SyscallReturn 1128lstatFunc(SyscallDesc *desc, int callnum, Process *process, 1129 ThreadContext *tc) 1130{ 1131 std::string path; 1132 1133 int index = 0; 1134 if (!tc->getMemProxy().tryReadString(path, 1135 process->getSyscallArg(tc, index))) { 1136 return -EFAULT; 1137 } 1138 Addr bufPtr = process->getSyscallArg(tc, index); 1139 1140 // Adjust path for current working directory 1141 path = process->fullPath(path); 1142 1143 struct stat hostBuf; 1144 int result = lstat(path.c_str(), &hostBuf); 1145 1146 if (result < 0) 1147 return -errno; 1148 1149 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1150 1151 return 0; 1152} 1153 1154/// Target lstat64() handler. 1155template <class OS> 1156SyscallReturn 1157lstat64Func(SyscallDesc *desc, int callnum, Process *process, 1158 ThreadContext *tc) 1159{ 1160 std::string path; 1161 1162 int index = 0; 1163 if (!tc->getMemProxy().tryReadString(path, 1164 process->getSyscallArg(tc, index))) { 1165 return -EFAULT; 1166 } 1167 Addr bufPtr = process->getSyscallArg(tc, index); 1168 1169 // Adjust path for current working directory 1170 path = process->fullPath(path); 1171 1172#if NO_STAT64 1173 struct stat hostBuf; 1174 int result = lstat(path.c_str(), &hostBuf); 1175#else 1176 struct stat64 hostBuf; 1177 int result = lstat64(path.c_str(), &hostBuf); 1178#endif 1179 1180 if (result < 0) 1181 return -errno; 1182 1183 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1184 1185 return 0; 1186} 1187 1188/// Target fstat() handler. 1189template <class OS> 1190SyscallReturn 1191fstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1192{ 1193 int index = 0; 1194 int tgt_fd = p->getSyscallArg(tc, index); 1195 Addr bufPtr = p->getSyscallArg(tc, index); 1196 1197 DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1198 1199 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1200 if (!ffdp) 1201 return -EBADF; 1202 int sim_fd = ffdp->getSimFD(); 1203 1204 struct stat hostBuf; 1205 int result = fstat(sim_fd, &hostBuf); 1206 1207 if (result < 0) 1208 return -errno; 1209 1210 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1211 1212 return 0; 1213} 1214 1215 1216/// Target statfs() handler. 1217template <class OS> 1218SyscallReturn 1219statfsFunc(SyscallDesc *desc, int callnum, Process *process, 1220 ThreadContext *tc) 1221{ 1222#if NO_STATFS 1223 warn("Host OS cannot support calls to statfs. Ignoring syscall"); 1224#else 1225 std::string path; 1226 1227 int index = 0; 1228 if (!tc->getMemProxy().tryReadString(path, 1229 process->getSyscallArg(tc, index))) { 1230 return -EFAULT; 1231 } 1232 Addr bufPtr = process->getSyscallArg(tc, index); 1233 1234 // Adjust path for current working directory 1235 path = process->fullPath(path); 1236 1237 struct statfs hostBuf; 1238 int result = statfs(path.c_str(), &hostBuf); 1239 1240 if (result < 0) 1241 return -errno; 1242 1243 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1244#endif 1245 return 0; 1246} 1247 1248template <class OS> 1249SyscallReturn 1250cloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1251{ 1252 int index = 0; 1253 1254 TheISA::IntReg flags = p->getSyscallArg(tc, index); 1255 TheISA::IntReg newStack = p->getSyscallArg(tc, index); 1256 Addr ptidPtr = p->getSyscallArg(tc, index); 1257 1258#if THE_ISA == RISCV_ISA 1259 /** 1260 * Linux kernel 4.15 sets CLONE_BACKWARDS flag for RISC-V. 1261 * The flag defines the list of clone() arguments in the following 1262 * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 1263 */ 1264 Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 1265 Addr ctidPtr = p->getSyscallArg(tc, index); 1266#else 1267 Addr ctidPtr = p->getSyscallArg(tc, index); 1268 Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 1269#endif 1270 1271 if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 1272 ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 1273 ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 1274 ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 1275 ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 1276 ((flags & OS::TGT_CLONE_VM) && !(newStack))) 1277 return -EINVAL; 1278 1279 ThreadContext *ctc; 1280 if (!(ctc = p->findFreeContext())) 1281 fatal("clone: no spare thread context in system"); 1282 1283 /** 1284 * Note that ProcessParams is generated by swig and there are no other 1285 * examples of how to create anything but this default constructor. The 1286 * fields are manually initialized instead of passing parameters to the 1287 * constructor. 1288 */ 1289 ProcessParams *pp = new ProcessParams(); 1290 pp->executable.assign(*(new std::string(p->progName()))); 1291 pp->cmd.push_back(*(new std::string(p->progName()))); 1292 pp->system = p->system; 1293 pp->cwd.assign(p->getcwd()); 1294 pp->input.assign("stdin"); 1295 pp->output.assign("stdout"); 1296 pp->errout.assign("stderr"); 1297 pp->uid = p->uid(); 1298 pp->euid = p->euid(); 1299 pp->gid = p->gid(); 1300 pp->egid = p->egid(); 1301 1302 /* Find the first free PID that's less than the maximum */ 1303 std::set<int> const& pids = p->system->PIDs; 1304 int temp_pid = *pids.begin(); 1305 do { 1306 temp_pid++; 1307 } while (pids.find(temp_pid) != pids.end()); 1308 if (temp_pid >= System::maxPID) 1309 fatal("temp_pid is too large: %d", temp_pid); 1310 1311 pp->pid = temp_pid; 1312 pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 1313 Process *cp = pp->create(); 1314 delete pp; 1315 1316 Process *owner = ctc->getProcessPtr(); 1317 ctc->setProcessPtr(cp); 1318 cp->assignThreadContext(ctc->contextId()); 1319 owner->revokeThreadContext(ctc->contextId()); 1320 1321 if (flags & OS::TGT_CLONE_PARENT_SETTID) { 1322 BufferArg ptidBuf(ptidPtr, sizeof(long)); 1323 long *ptid = (long *)ptidBuf.bufferPtr(); 1324 *ptid = cp->pid(); 1325 ptidBuf.copyOut(tc->getMemProxy()); 1326 } 1327 1328 cp->initState(); 1329 p->clone(tc, ctc, cp, flags); 1330 1331 if (flags & OS::TGT_CLONE_THREAD) { 1332 delete cp->sigchld; 1333 cp->sigchld = p->sigchld; 1334 } else if (flags & OS::TGT_SIGCHLD) { 1335 *cp->sigchld = true; 1336 } 1337 1338 if (flags & OS::TGT_CLONE_CHILD_SETTID) { 1339 BufferArg ctidBuf(ctidPtr, sizeof(long)); 1340 long *ctid = (long *)ctidBuf.bufferPtr(); 1341 *ctid = cp->pid(); 1342 ctidBuf.copyOut(ctc->getMemProxy()); 1343 } 1344 1345 if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 1346 cp->childClearTID = (uint64_t)ctidPtr; 1347 1348 ctc->clearArchRegs(); 1349 1350#if THE_ISA == ALPHA_ISA 1351 TheISA::copyMiscRegs(tc, ctc); 1352#elif THE_ISA == SPARC_ISA 1353 TheISA::copyRegs(tc, ctc); 1354 ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 1355 ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 1356 ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 1357 ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 1358 ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 1359 ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 1360 ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 1361 ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 1362 for (int y = 8; y < 32; y++) 1363 ctc->setIntReg(y, tc->readIntReg(y)); 1364#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA or THE_ISA == RISCV_ISA 1365 TheISA::copyRegs(tc, ctc); 1366#endif 1367 1368#if THE_ISA == X86_ISA 1369 if (flags & OS::TGT_CLONE_SETTLS) { 1370 ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 1371 ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 1372 } 1373#endif 1374 1375 if (newStack) 1376 ctc->setIntReg(TheISA::StackPointerReg, newStack); 1377 1378 cp->setSyscallReturn(ctc, 0); 1379 1380#if THE_ISA == ALPHA_ISA 1381 ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 1382#elif THE_ISA == SPARC_ISA 1383 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1384 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 1385#endif 1386 1387 ctc->pcState(tc->nextInstAddr()); 1388 ctc->activate(); 1389 1390 return cp->pid(); 1391} 1392 1393/// Target fstatfs() handler. 1394template <class OS> 1395SyscallReturn 1396fstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1397{ 1398 int index = 0; 1399 int tgt_fd = p->getSyscallArg(tc, index); 1400 Addr bufPtr = p->getSyscallArg(tc, index); 1401 1402 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1403 if (!ffdp) 1404 return -EBADF; 1405 int sim_fd = ffdp->getSimFD(); 1406 1407 struct statfs hostBuf; 1408 int result = fstatfs(sim_fd, &hostBuf); 1409 1410 if (result < 0) 1411 return -errno; 1412 1413 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1414 1415 return 0; 1416} 1417 1418 1419/// Target writev() handler. 1420template <class OS> 1421SyscallReturn 1422writevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1423{ 1424 int index = 0; 1425 int tgt_fd = p->getSyscallArg(tc, index); 1426 1427 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 1428 if (!hbfdp) 1429 return -EBADF; 1430 int sim_fd = hbfdp->getSimFD(); 1431 1432 SETranslatingPortProxy &prox = tc->getMemProxy(); 1433 uint64_t tiov_base = p->getSyscallArg(tc, index); 1434 size_t count = p->getSyscallArg(tc, index); 1435 struct iovec hiov[count]; 1436 for (size_t i = 0; i < count; ++i) { 1437 typename OS::tgt_iovec tiov; 1438 1439 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 1440 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 1441 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1442 hiov[i].iov_base = new char [hiov[i].iov_len]; 1443 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 1444 hiov[i].iov_len); 1445 } 1446 1447 int result = writev(sim_fd, hiov, count); 1448 1449 for (size_t i = 0; i < count; ++i) 1450 delete [] (char *)hiov[i].iov_base; 1451 1452 if (result < 0) 1453 return -errno; 1454 1455 return result; 1456} 1457 1458/// Real mmap handler. 1459template <class OS> 1460SyscallReturn 1461mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1462 bool is_mmap2) 1463{ 1464 int index = 0; 1465 Addr start = p->getSyscallArg(tc, index); 1466 uint64_t length = p->getSyscallArg(tc, index); 1467 int prot = p->getSyscallArg(tc, index); 1468 int tgt_flags = p->getSyscallArg(tc, index); 1469 int tgt_fd = p->getSyscallArg(tc, index); 1470 int offset = p->getSyscallArg(tc, index); 1471 1472 if (is_mmap2) 1473 offset *= TheISA::PageBytes; 1474 1475 if (start & (TheISA::PageBytes - 1) || 1476 offset & (TheISA::PageBytes - 1) || 1477 (tgt_flags & OS::TGT_MAP_PRIVATE && 1478 tgt_flags & OS::TGT_MAP_SHARED) || 1479 (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 1480 !(tgt_flags & OS::TGT_MAP_SHARED)) || 1481 !length) { 1482 return -EINVAL; 1483 } 1484 1485 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1486 // With shared mmaps, there are two cases to consider: 1487 // 1) anonymous: writes should modify the mapping and this should be 1488 // visible to observers who share the mapping. Currently, it's 1489 // difficult to update the shared mapping because there's no 1490 // structure which maintains information about the which virtual 1491 // memory areas are shared. If that structure existed, it would be 1492 // possible to make the translations point to the same frames. 1493 // 2) file-backed: writes should modify the mapping and the file 1494 // which is backed by the mapping. The shared mapping problem is the 1495 // same as what was mentioned about the anonymous mappings. For 1496 // file-backed mappings, the writes to the file are difficult 1497 // because it requires syncing what the mapping holds with the file 1498 // that resides on the host system. So, any write on a real system 1499 // would cause the change to be propagated to the file mapping at 1500 // some point in the future (the inode is tracked along with the 1501 // mapping). This isn't guaranteed to always happen, but it usually 1502 // works well enough. The guarantee is provided by the msync system 1503 // call. We could force the change through with shared mappings with 1504 // a call to msync, but that again would require more information 1505 // than we currently maintain. 1506 warn("mmap: writing to shared mmap region is currently " 1507 "unsupported. The write succeeds on the target, but it " 1508 "will not be propagated to the host or shared mappings"); 1509 } 1510 1511 length = roundUp(length, TheISA::PageBytes); 1512 1513 int sim_fd = -1; 1514 uint8_t *pmap = nullptr; 1515 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1516 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1517 1518 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1519 if (dfdp) { 1520 EmulatedDriver *emul_driver = dfdp->getDriver(); 1521 return emul_driver->mmap(p, tc, start, length, prot, 1522 tgt_flags, tgt_fd, offset); 1523 } 1524 1525 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1526 if (!ffdp) 1527 return -EBADF; 1528 sim_fd = ffdp->getSimFD(); 1529 1530 pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 1531 sim_fd, offset); 1532 1533 if (pmap == (decltype(pmap))-1) { 1534 warn("mmap: failed to map file into host address space"); 1535 return -errno; 1536 } 1537 } 1538 1539 // Extend global mmap region if necessary. Note that we ignore the 1540 // start address unless MAP_FIXED is specified. 1541 if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 1542 std::shared_ptr<MemState> mem_state = p->memState; 1543 Addr mmap_end = mem_state->getMmapEnd(); 1544 1545 start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1546 mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 1547 1548 mem_state->setMmapEnd(mmap_end); 1549 } 1550 1551 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1552 start, start + length - 1); 1553 1554 // We only allow mappings to overwrite existing mappings if 1555 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 1556 // because we ignore the start hint if TGT_MAP_FIXED is not set. 1557 int clobber = tgt_flags & OS::TGT_MAP_FIXED; 1558 if (clobber) { 1559 for (auto tc : p->system->threadContexts) { 1560 // If we might be overwriting old mappings, we need to 1561 // invalidate potentially stale mappings out of the TLBs. 1562 tc->getDTBPtr()->flushAll(); 1563 tc->getITBPtr()->flushAll(); 1564 } 1565 } 1566 1567 // Allocate physical memory and map it in. If the page table is already 1568 // mapped and clobber is not set, the simulator will issue throw a 1569 // fatal and bail out of the simulation. 1570 p->allocateMem(start, length, clobber); 1571 1572 // Transfer content into target address space. 1573 SETranslatingPortProxy &tp = tc->getMemProxy(); 1574 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1575 // In general, we should zero the mapped area for anonymous mappings, 1576 // with something like: 1577 // tp.memsetBlob(start, 0, length); 1578 // However, given that we don't support sparse mappings, and 1579 // some applications can map a couple of gigabytes of space 1580 // (intending sparse usage), that can get painfully expensive. 1581 // Fortunately, since we don't properly implement munmap either, 1582 // there's no danger of remapping used memory, so for now all 1583 // newly mapped memory should already be zeroed so we can skip it. 1584 } else { 1585 // It is possible to mmap an area larger than a file, however 1586 // accessing unmapped portions the system triggers a "Bus error" 1587 // on the host. We must know when to stop copying the file from 1588 // the host into the target address space. 1589 struct stat file_stat; 1590 if (fstat(sim_fd, &file_stat) > 0) 1591 fatal("mmap: cannot stat file"); 1592 1593 // Copy the portion of the file that is resident. This requires 1594 // checking both the mmap size and the filesize that we are 1595 // trying to mmap into this space; the mmap size also depends 1596 // on the specified offset into the file. 1597 uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1598 length); 1599 tp.writeBlob(start, pmap, size); 1600 1601 // Cleanup the mmap region before exiting this function. 1602 munmap(pmap, length); 1603 1604 // Maintain the symbol table for dynamic executables. 1605 // The loader will call mmap to map the images into its address 1606 // space and we intercept that here. We can verify that we are 1607 // executing inside the loader by checking the program counter value. 1608 // XXX: with multiprogrammed workloads or multi-node configurations, 1609 // this will not work since there is a single global symbol table. 1610 ObjectFile *interpreter = p->getInterpreter(); 1611 if (interpreter) { 1612 Addr text_start = interpreter->textBase(); 1613 Addr text_end = text_start + interpreter->textSize(); 1614 1615 Addr pc = tc->pcState().pc(); 1616 1617 if (pc >= text_start && pc < text_end) { 1618 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1619 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1620 ObjectFile *lib = createObjectFile(ffdp->getFileName()); 1621 1622 if (lib) { 1623 lib->loadAllSymbols(debugSymbolTable, 1624 lib->textBase(), start); 1625 } 1626 } 1627 } 1628 1629 // Note that we do not zero out the remainder of the mapping. This 1630 // is done by a real system, but it probably will not affect 1631 // execution (hopefully). 1632 } 1633 1634 return start; 1635} 1636 1637template <class OS> 1638SyscallReturn 1639pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1640{ 1641 int index = 0; 1642 int tgt_fd = p->getSyscallArg(tc, index); 1643 Addr bufPtr = p->getSyscallArg(tc, index); 1644 int nbytes = p->getSyscallArg(tc, index); 1645 int offset = p->getSyscallArg(tc, index); 1646 1647 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1648 if (!ffdp) 1649 return -EBADF; 1650 int sim_fd = ffdp->getSimFD(); 1651 1652 BufferArg bufArg(bufPtr, nbytes); 1653 bufArg.copyIn(tc->getMemProxy()); 1654 1655 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1656 1657 return (bytes_written == -1) ? -errno : bytes_written; 1658} 1659 1660/// Target mmap() handler. 1661template <class OS> 1662SyscallReturn 1663mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1664{ 1665 return mmapImpl<OS>(desc, num, p, tc, false); 1666} 1667 1668/// Target mmap2() handler. 1669template <class OS> 1670SyscallReturn 1671mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1672{ 1673 return mmapImpl<OS>(desc, num, p, tc, true); 1674} 1675 1676/// Target getrlimit() handler. 1677template <class OS> 1678SyscallReturn 1679getrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1680 ThreadContext *tc) 1681{ 1682 int index = 0; 1683 unsigned resource = process->getSyscallArg(tc, index); 1684 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1685 1686 switch (resource) { 1687 case OS::TGT_RLIMIT_STACK: 1688 // max stack size in bytes: make up a number (8MB for now) 1689 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1690 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1691 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1692 break; 1693 1694 case OS::TGT_RLIMIT_DATA: 1695 // max data segment size in bytes: make up a number 1696 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1697 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1698 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1699 break; 1700 1701 default: 1702 warn("getrlimit: unimplemented resource %d", resource); 1703 return -EINVAL; 1704 break; 1705 } 1706 1707 rlp.copyOut(tc->getMemProxy()); 1708 return 0; 1709} 1710 1711template <class OS> 1712SyscallReturn 1713prlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1714 ThreadContext *tc) 1715{ 1716 int index = 0; 1717 if (process->getSyscallArg(tc, index) != 0) 1718 { 1719 warn("prlimit: ignoring rlimits for nonzero pid"); 1720 return -EPERM; 1721 } 1722 int resource = process->getSyscallArg(tc, index); 1723 Addr n = process->getSyscallArg(tc, index); 1724 if (n != 0) 1725 warn("prlimit: ignoring new rlimit"); 1726 Addr o = process->getSyscallArg(tc, index); 1727 if (o != 0) 1728 { 1729 TypedBufferArg<typename OS::rlimit> rlp(o); 1730 switch (resource) { 1731 case OS::TGT_RLIMIT_STACK: 1732 // max stack size in bytes: make up a number (8MB for now) 1733 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1734 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1735 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1736 break; 1737 case OS::TGT_RLIMIT_DATA: 1738 // max data segment size in bytes: make up a number 1739 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 1740 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1741 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1742 break; 1743 default: 1744 warn("prlimit: unimplemented resource %d", resource); 1745 return -EINVAL; 1746 break; 1747 } 1748 rlp.copyOut(tc->getMemProxy()); 1749 } 1750 return 0; 1751} 1752 1753/// Target clock_gettime() function. 1754template <class OS> 1755SyscallReturn 1756clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1757{ 1758 int index = 1; 1759 //int clk_id = p->getSyscallArg(tc, index); 1760 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1761 1762 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 1763 tp->tv_sec += seconds_since_epoch; 1764 tp->tv_sec = TheISA::htog(tp->tv_sec); 1765 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 1766 1767 tp.copyOut(tc->getMemProxy()); 1768 1769 return 0; 1770} 1771 1772/// Target clock_getres() function. 1773template <class OS> 1774SyscallReturn 1775clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1776{ 1777 int index = 1; 1778 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1779 1780 // Set resolution at ns, which is what clock_gettime() returns 1781 tp->tv_sec = 0; 1782 tp->tv_nsec = 1; 1783 1784 tp.copyOut(tc->getMemProxy()); 1785 1786 return 0; 1787} 1788 1789/// Target gettimeofday() handler. 1790template <class OS> 1791SyscallReturn 1792gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 1793 ThreadContext *tc) 1794{ 1795 int index = 0; 1796 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1797 1798 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1799 tp->tv_sec += seconds_since_epoch; 1800 tp->tv_sec = TheISA::htog(tp->tv_sec); 1801 tp->tv_usec = TheISA::htog(tp->tv_usec); 1802 1803 tp.copyOut(tc->getMemProxy()); 1804 1805 return 0; 1806} 1807 1808 1809/// Target utimes() handler. 1810template <class OS> 1811SyscallReturn 1812utimesFunc(SyscallDesc *desc, int callnum, Process *process, 1813 ThreadContext *tc) 1814{ 1815 std::string path; 1816 1817 int index = 0; 1818 if (!tc->getMemProxy().tryReadString(path, 1819 process->getSyscallArg(tc, index))) { 1820 return -EFAULT; 1821 } 1822 1823 TypedBufferArg<typename OS::timeval [2]> 1824 tp(process->getSyscallArg(tc, index)); 1825 tp.copyIn(tc->getMemProxy()); 1826 1827 struct timeval hostTimeval[2]; 1828 for (int i = 0; i < 2; ++i) { 1829 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 1830 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 1831 } 1832 1833 // Adjust path for current working directory 1834 path = process->fullPath(path); 1835 1836 int result = utimes(path.c_str(), hostTimeval); 1837 1838 if (result < 0) 1839 return -errno; 1840 1841 return 0; 1842} 1843 1844template <class OS> 1845SyscallReturn 1846execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1847{ 1848 desc->setFlags(0); 1849 1850 int index = 0; 1851 std::string path; 1852 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 1853 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 1854 return -EFAULT; 1855 1856 if (access(path.c_str(), F_OK) == -1) 1857 return -EACCES; 1858 1859 auto read_in = [](std::vector<std::string> & vect, 1860 SETranslatingPortProxy & mem_proxy, 1861 Addr mem_loc) 1862 { 1863 for (int inc = 0; ; inc++) { 1864 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 1865 b.copyIn(mem_proxy); 1866 1867 if (!*(Addr*)b.bufferPtr()) 1868 break; 1869 1870 vect.push_back(std::string()); 1871 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 1872 } 1873 }; 1874 1875 /** 1876 * Note that ProcessParams is generated by swig and there are no other 1877 * examples of how to create anything but this default constructor. The 1878 * fields are manually initialized instead of passing parameters to the 1879 * constructor. 1880 */ 1881 ProcessParams *pp = new ProcessParams(); 1882 pp->executable = path; 1883 Addr argv_mem_loc = p->getSyscallArg(tc, index); 1884 read_in(pp->cmd, mem_proxy, argv_mem_loc); 1885 Addr envp_mem_loc = p->getSyscallArg(tc, index); 1886 read_in(pp->env, mem_proxy, envp_mem_loc); 1887 pp->uid = p->uid(); 1888 pp->egid = p->egid(); 1889 pp->euid = p->euid(); 1890 pp->gid = p->gid(); 1891 pp->ppid = p->ppid(); 1892 pp->pid = p->pid(); 1893 pp->input.assign("cin"); 1894 pp->output.assign("cout"); 1895 pp->errout.assign("cerr"); 1896 pp->cwd.assign(p->getcwd()); 1897 pp->system = p->system; 1898 /** 1899 * Prevent process object creation with identical PIDs (which will trip 1900 * a fatal check in Process constructor). The execve call is supposed to 1901 * take over the currently executing process' identity but replace 1902 * whatever it is doing with a new process image. Instead of hijacking 1903 * the process object in the simulator, we create a new process object 1904 * and bind to the previous process' thread below (hijacking the thread). 1905 */ 1906 p->system->PIDs.erase(p->pid()); 1907 Process *new_p = pp->create(); 1908 delete pp; 1909 1910 /** 1911 * Work through the file descriptor array and close any files marked 1912 * close-on-exec. 1913 */ 1914 new_p->fds = p->fds; 1915 for (int i = 0; i < new_p->fds->getSize(); i++) { 1916 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 1917 if (fdep && fdep->getCOE()) 1918 new_p->fds->closeFDEntry(i); 1919 } 1920 1921 *new_p->sigchld = true; 1922 1923 delete p; 1924 tc->clearArchRegs(); 1925 tc->setProcessPtr(new_p); 1926 new_p->assignThreadContext(tc->contextId()); 1927 new_p->initState(); 1928 tc->activate(); 1929 TheISA::PCState pcState = tc->pcState(); 1930 tc->setNPC(pcState.instAddr()); 1931 1932 desc->setFlags(SyscallDesc::SuppressReturnValue); 1933 return 0; 1934} 1935 1936/// Target getrusage() function. 1937template <class OS> 1938SyscallReturn 1939getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 1940 ThreadContext *tc) 1941{ 1942 int index = 0; 1943 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 1944 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1945 1946 rup->ru_utime.tv_sec = 0; 1947 rup->ru_utime.tv_usec = 0; 1948 rup->ru_stime.tv_sec = 0; 1949 rup->ru_stime.tv_usec = 0; 1950 rup->ru_maxrss = 0; 1951 rup->ru_ixrss = 0; 1952 rup->ru_idrss = 0; 1953 rup->ru_isrss = 0; 1954 rup->ru_minflt = 0; 1955 rup->ru_majflt = 0; 1956 rup->ru_nswap = 0; 1957 rup->ru_inblock = 0; 1958 rup->ru_oublock = 0; 1959 rup->ru_msgsnd = 0; 1960 rup->ru_msgrcv = 0; 1961 rup->ru_nsignals = 0; 1962 rup->ru_nvcsw = 0; 1963 rup->ru_nivcsw = 0; 1964 1965 switch (who) { 1966 case OS::TGT_RUSAGE_SELF: 1967 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1968 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 1969 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 1970 break; 1971 1972 case OS::TGT_RUSAGE_CHILDREN: 1973 // do nothing. We have no child processes, so they take no time. 1974 break; 1975 1976 default: 1977 // don't really handle THREAD or CHILDREN, but just warn and 1978 // plow ahead 1979 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1980 who); 1981 } 1982 1983 rup.copyOut(tc->getMemProxy()); 1984 1985 return 0; 1986} 1987 1988/// Target times() function. 1989template <class OS> 1990SyscallReturn 1991timesFunc(SyscallDesc *desc, int callnum, Process *process, 1992 ThreadContext *tc) 1993{ 1994 int index = 0; 1995 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 1996 1997 // Fill in the time structure (in clocks) 1998 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 1999 bufp->tms_utime = clocks; 2000 bufp->tms_stime = 0; 2001 bufp->tms_cutime = 0; 2002 bufp->tms_cstime = 0; 2003 2004 // Convert to host endianness 2005 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 2006 2007 // Write back 2008 bufp.copyOut(tc->getMemProxy()); 2009 2010 // Return clock ticks since system boot 2011 return clocks; 2012} 2013 2014/// Target time() function. 2015template <class OS> 2016SyscallReturn 2017timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 2018{ 2019 typename OS::time_t sec, usec; 2020 getElapsedTimeMicro(sec, usec); 2021 sec += seconds_since_epoch; 2022 2023 int index = 0; 2024 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2025 if (taddr != 0) { 2026 typename OS::time_t t = sec; 2027 t = TheISA::htog(t); 2028 SETranslatingPortProxy &p = tc->getMemProxy(); 2029 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2030 } 2031 return sec; 2032} 2033 2034template <class OS> 2035SyscallReturn 2036tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 2037{ 2038 int index = 0; 2039 int tgid = process->getSyscallArg(tc, index); 2040 int tid = process->getSyscallArg(tc, index); 2041 int sig = process->getSyscallArg(tc, index); 2042 2043 /** 2044 * This system call is intended to allow killing a specific thread 2045 * within an arbitrary thread group if sanctioned with permission checks. 2046 * It's usually true that threads share the termination signal as pointed 2047 * out by the pthread_kill man page and this seems to be the intended 2048 * usage. Due to this being an emulated environment, assume the following: 2049 * Threads are allowed to call tgkill because the EUID for all threads 2050 * should be the same. There is no signal handling mechanism for kernel 2051 * registration of signal handlers since signals are poorly supported in 2052 * emulation mode. Since signal handlers cannot be registered, all 2053 * threads within in a thread group must share the termination signal. 2054 * We never exhaust PIDs so there's no chance of finding the wrong one 2055 * due to PID rollover. 2056 */ 2057 2058 System *sys = tc->getSystemPtr(); 2059 Process *tgt_proc = nullptr; 2060 for (int i = 0; i < sys->numContexts(); i++) { 2061 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2062 if (temp->pid() == tid) { 2063 tgt_proc = temp; 2064 break; 2065 } 2066 } 2067 2068 if (sig != 0 || sig != OS::TGT_SIGABRT) 2069 return -EINVAL; 2070 2071 if (tgt_proc == nullptr) 2072 return -ESRCH; 2073 2074 if (tgid != -1 && tgt_proc->tgid() != tgid) 2075 return -ESRCH; 2076 2077 if (sig == OS::TGT_SIGABRT) 2078 exitGroupFunc(desc, 252, process, tc); 2079 2080 return 0; 2081} 2082 2083 2084#endif // __SIM_SYSCALL_EMUL_HH__ 2085