syscall_emul.hh revision 6689
1/* 2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Steve Reinhardt 29 * Kevin Lim 30 */ 31 32#ifndef __SIM_SYSCALL_EMUL_HH__ 33#define __SIM_SYSCALL_EMUL_HH__ 34 35#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \ 36 defined(__FreeBSD__) || defined(__CYGWIN__)) 37 38/// 39/// @file syscall_emul.hh 40/// 41/// This file defines objects used to emulate syscalls from the target 42/// application on the host machine. 43 44#include <errno.h> 45#include <string> 46#ifdef __CYGWIN32__ 47#include <sys/fcntl.h> // for O_BINARY 48#endif 49#include <sys/stat.h> 50#include <fcntl.h> 51#include <sys/uio.h> 52 53#include "base/chunk_generator.hh" 54#include "base/intmath.hh" // for RoundUp 55#include "base/misc.hh" 56#include "base/trace.hh" 57#include "base/types.hh" 58#include "config/the_isa.hh" 59#include "cpu/base.hh" 60#include "cpu/thread_context.hh" 61#include "mem/translating_port.hh" 62#include "mem/page_table.hh" 63#include "sim/system.hh" 64#include "sim/process.hh" 65 66/// 67/// System call descriptor. 68/// 69class SyscallDesc { 70 71 public: 72 73 /// Typedef for target syscall handler functions. 74 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 75 LiveProcess *, ThreadContext *); 76 77 const char *name; //!< Syscall name (e.g., "open"). 78 FuncPtr funcPtr; //!< Pointer to emulation function. 79 int flags; //!< Flags (see Flags enum). 80 81 /// Flag values for controlling syscall behavior. 82 enum Flags { 83 /// Don't set return regs according to funcPtr return value. 84 /// Used for syscalls with non-standard return conventions 85 /// that explicitly set the ThreadContext regs (e.g., 86 /// sigreturn). 87 SuppressReturnValue = 1 88 }; 89 90 /// Constructor. 91 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 92 : name(_name), funcPtr(_funcPtr), flags(_flags) 93 { 94 } 95 96 /// Emulate the syscall. Public interface for calling through funcPtr. 97 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 98}; 99 100 101class BaseBufferArg { 102 103 public: 104 105 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 106 { 107 bufPtr = new uint8_t[size]; 108 // clear out buffer: in case we only partially populate this, 109 // and then do a copyOut(), we want to make sure we don't 110 // introduce any random junk into the simulated address space 111 memset(bufPtr, 0, size); 112 } 113 114 virtual ~BaseBufferArg() { delete [] bufPtr; } 115 116 // 117 // copy data into simulator space (read from target memory) 118 // 119 virtual bool copyIn(TranslatingPort *memport) 120 { 121 memport->readBlob(addr, bufPtr, size); 122 return true; // no EFAULT detection for now 123 } 124 125 // 126 // copy data out of simulator space (write to target memory) 127 // 128 virtual bool copyOut(TranslatingPort *memport) 129 { 130 memport->writeBlob(addr, bufPtr, size); 131 return true; // no EFAULT detection for now 132 } 133 134 protected: 135 Addr addr; 136 int size; 137 uint8_t *bufPtr; 138}; 139 140 141class BufferArg : public BaseBufferArg 142{ 143 public: 144 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 145 void *bufferPtr() { return bufPtr; } 146}; 147 148template <class T> 149class TypedBufferArg : public BaseBufferArg 150{ 151 public: 152 // user can optionally specify a specific number of bytes to 153 // allocate to deal with those structs that have variable-size 154 // arrays at the end 155 TypedBufferArg(Addr _addr, int _size = sizeof(T)) 156 : BaseBufferArg(_addr, _size) 157 { } 158 159 // type case 160 operator T*() { return (T *)bufPtr; } 161 162 // dereference operators 163 T &operator*() { return *((T *)bufPtr); } 164 T* operator->() { return (T *)bufPtr; } 165 T &operator[](int i) { return ((T *)bufPtr)[i]; } 166}; 167 168////////////////////////////////////////////////////////////////////// 169// 170// The following emulation functions are generic enough that they 171// don't need to be recompiled for different emulated OS's. They are 172// defined in sim/syscall_emul.cc. 173// 174////////////////////////////////////////////////////////////////////// 175 176 177/// Handler for unimplemented syscalls that we haven't thought about. 178SyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 179 LiveProcess *p, ThreadContext *tc); 180 181/// Handler for unimplemented syscalls that we never intend to 182/// implement (signal handling, etc.) and should not affect the correct 183/// behavior of the program. Print a warning only if the appropriate 184/// trace flag is enabled. Return success to the target program. 185SyscallReturn ignoreFunc(SyscallDesc *desc, int num, 186 LiveProcess *p, ThreadContext *tc); 187 188/// Target exit() handler: terminate current context. 189SyscallReturn exitFunc(SyscallDesc *desc, int num, 190 LiveProcess *p, ThreadContext *tc); 191 192/// Target exit_group() handler: terminate simulation. (exit all threads) 193SyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 194 LiveProcess *p, ThreadContext *tc); 195 196/// Target getpagesize() handler. 197SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 198 LiveProcess *p, ThreadContext *tc); 199 200/// Target brk() handler: set brk address. 201SyscallReturn brkFunc(SyscallDesc *desc, int num, 202 LiveProcess *p, ThreadContext *tc); 203 204/// Target close() handler. 205SyscallReturn closeFunc(SyscallDesc *desc, int num, 206 LiveProcess *p, ThreadContext *tc); 207 208/// Target read() handler. 209SyscallReturn readFunc(SyscallDesc *desc, int num, 210 LiveProcess *p, ThreadContext *tc); 211 212/// Target write() handler. 213SyscallReturn writeFunc(SyscallDesc *desc, int num, 214 LiveProcess *p, ThreadContext *tc); 215 216/// Target lseek() handler. 217SyscallReturn lseekFunc(SyscallDesc *desc, int num, 218 LiveProcess *p, ThreadContext *tc); 219 220/// Target _llseek() handler. 221SyscallReturn _llseekFunc(SyscallDesc *desc, int num, 222 LiveProcess *p, ThreadContext *tc); 223 224/// Target munmap() handler. 225SyscallReturn munmapFunc(SyscallDesc *desc, int num, 226 LiveProcess *p, ThreadContext *tc); 227 228/// Target gethostname() handler. 229SyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 230 LiveProcess *p, ThreadContext *tc); 231 232/// Target getcwd() handler. 233SyscallReturn getcwdFunc(SyscallDesc *desc, int num, 234 LiveProcess *p, ThreadContext *tc); 235 236/// Target unlink() handler. 237SyscallReturn readlinkFunc(SyscallDesc *desc, int num, 238 LiveProcess *p, ThreadContext *tc); 239 240/// Target unlink() handler. 241SyscallReturn unlinkFunc(SyscallDesc *desc, int num, 242 LiveProcess *p, ThreadContext *tc); 243 244/// Target mkdir() handler. 245SyscallReturn mkdirFunc(SyscallDesc *desc, int num, 246 LiveProcess *p, ThreadContext *tc); 247 248/// Target rename() handler. 249SyscallReturn renameFunc(SyscallDesc *desc, int num, 250 LiveProcess *p, ThreadContext *tc); 251 252 253/// Target truncate() handler. 254SyscallReturn truncateFunc(SyscallDesc *desc, int num, 255 LiveProcess *p, ThreadContext *tc); 256 257 258/// Target ftruncate() handler. 259SyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 260 LiveProcess *p, ThreadContext *tc); 261 262 263/// Target ftruncate64() handler. 264SyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 265 LiveProcess *p, ThreadContext *tc); 266 267 268/// Target umask() handler. 269SyscallReturn umaskFunc(SyscallDesc *desc, int num, 270 LiveProcess *p, ThreadContext *tc); 271 272 273/// Target chown() handler. 274SyscallReturn chownFunc(SyscallDesc *desc, int num, 275 LiveProcess *p, ThreadContext *tc); 276 277 278/// Target fchown() handler. 279SyscallReturn fchownFunc(SyscallDesc *desc, int num, 280 LiveProcess *p, ThreadContext *tc); 281 282/// Target dup() handler. 283SyscallReturn dupFunc(SyscallDesc *desc, int num, 284 LiveProcess *process, ThreadContext *tc); 285 286/// Target fnctl() handler. 287SyscallReturn fcntlFunc(SyscallDesc *desc, int num, 288 LiveProcess *process, ThreadContext *tc); 289 290/// Target fcntl64() handler. 291SyscallReturn fcntl64Func(SyscallDesc *desc, int num, 292 LiveProcess *process, ThreadContext *tc); 293 294/// Target setuid() handler. 295SyscallReturn setuidFunc(SyscallDesc *desc, int num, 296 LiveProcess *p, ThreadContext *tc); 297 298/// Target getpid() handler. 299SyscallReturn getpidFunc(SyscallDesc *desc, int num, 300 LiveProcess *p, ThreadContext *tc); 301 302/// Target getuid() handler. 303SyscallReturn getuidFunc(SyscallDesc *desc, int num, 304 LiveProcess *p, ThreadContext *tc); 305 306/// Target getgid() handler. 307SyscallReturn getgidFunc(SyscallDesc *desc, int num, 308 LiveProcess *p, ThreadContext *tc); 309 310/// Target getppid() handler. 311SyscallReturn getppidFunc(SyscallDesc *desc, int num, 312 LiveProcess *p, ThreadContext *tc); 313 314/// Target geteuid() handler. 315SyscallReturn geteuidFunc(SyscallDesc *desc, int num, 316 LiveProcess *p, ThreadContext *tc); 317 318/// Target getegid() handler. 319SyscallReturn getegidFunc(SyscallDesc *desc, int num, 320 LiveProcess *p, ThreadContext *tc); 321 322/// Target clone() handler. 323SyscallReturn cloneFunc(SyscallDesc *desc, int num, 324 LiveProcess *p, ThreadContext *tc); 325 326 327/// Pseudo Funcs - These functions use a different return convension, 328/// returning a second value in a register other than the normal return register 329SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 330 LiveProcess *process, ThreadContext *tc); 331 332/// Target getpidPseudo() handler. 333SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 334 LiveProcess *p, ThreadContext *tc); 335 336/// Target getuidPseudo() handler. 337SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 338 LiveProcess *p, ThreadContext *tc); 339 340/// Target getgidPseudo() handler. 341SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 342 LiveProcess *p, ThreadContext *tc); 343 344 345/// A readable name for 1,000,000, for converting microseconds to seconds. 346const int one_million = 1000000; 347 348/// Approximate seconds since the epoch (1/1/1970). About a billion, 349/// by my reckoning. We want to keep this a constant (not use the 350/// real-world time) to keep simulations repeatable. 351const unsigned seconds_since_epoch = 1000000000; 352 353/// Helper function to convert current elapsed time to seconds and 354/// microseconds. 355template <class T1, class T2> 356void 357getElapsedTime(T1 &sec, T2 &usec) 358{ 359 int elapsed_usecs = curTick / Clock::Int::us; 360 sec = elapsed_usecs / one_million; 361 usec = elapsed_usecs % one_million; 362} 363 364////////////////////////////////////////////////////////////////////// 365// 366// The following emulation functions are generic, but need to be 367// templated to account for differences in types, constants, etc. 368// 369////////////////////////////////////////////////////////////////////// 370 371#if NO_STAT64 372 typedef struct stat hst_stat; 373 typedef struct stat hst_stat64; 374#else 375 typedef struct stat hst_stat; 376 typedef struct stat64 hst_stat64; 377#endif 378 379//// Helper function to convert a host stat buffer to a target stat 380//// buffer. Also copies the target buffer out to the simulated 381//// memory space. Used by stat(), fstat(), and lstat(). 382 383template <typename target_stat, typename host_stat> 384static void 385convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 386{ 387 using namespace TheISA; 388 389 if (fakeTTY) 390 tgt->st_dev = 0xA; 391 else 392 tgt->st_dev = host->st_dev; 393 tgt->st_dev = htog(tgt->st_dev); 394 tgt->st_ino = host->st_ino; 395 tgt->st_ino = htog(tgt->st_ino); 396 tgt->st_mode = host->st_mode; 397 if (fakeTTY) { 398 // Claim to be a character device 399 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 400 tgt->st_mode |= S_IFCHR; // Set S_IFCHR 401 } 402 tgt->st_mode = htog(tgt->st_mode); 403 tgt->st_nlink = host->st_nlink; 404 tgt->st_nlink = htog(tgt->st_nlink); 405 tgt->st_uid = host->st_uid; 406 tgt->st_uid = htog(tgt->st_uid); 407 tgt->st_gid = host->st_gid; 408 tgt->st_gid = htog(tgt->st_gid); 409 if (fakeTTY) 410 tgt->st_rdev = 0x880d; 411 else 412 tgt->st_rdev = host->st_rdev; 413 tgt->st_rdev = htog(tgt->st_rdev); 414 tgt->st_size = host->st_size; 415 tgt->st_size = htog(tgt->st_size); 416 tgt->st_atimeX = host->st_atime; 417 tgt->st_atimeX = htog(tgt->st_atimeX); 418 tgt->st_mtimeX = host->st_mtime; 419 tgt->st_mtimeX = htog(tgt->st_mtimeX); 420 tgt->st_ctimeX = host->st_ctime; 421 tgt->st_ctimeX = htog(tgt->st_ctimeX); 422 // Force the block size to be 8k. This helps to ensure buffered io works 423 // consistently across different hosts. 424 tgt->st_blksize = 0x2000; 425 tgt->st_blksize = htog(tgt->st_blksize); 426 tgt->st_blocks = host->st_blocks; 427 tgt->st_blocks = htog(tgt->st_blocks); 428} 429 430// Same for stat64 431 432template <typename target_stat, typename host_stat64> 433static void 434convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 435{ 436 using namespace TheISA; 437 438 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 439#if defined(STAT_HAVE_NSEC) 440 tgt->st_atime_nsec = host->st_atime_nsec; 441 tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 442 tgt->st_mtime_nsec = host->st_mtime_nsec; 443 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 444 tgt->st_ctime_nsec = host->st_ctime_nsec; 445 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 446#else 447 tgt->st_atime_nsec = 0; 448 tgt->st_mtime_nsec = 0; 449 tgt->st_ctime_nsec = 0; 450#endif 451} 452 453//Here are a couple convenience functions 454template<class OS> 455static void 456copyOutStatBuf(TranslatingPort * mem, Addr addr, 457 hst_stat *host, bool fakeTTY = false) 458{ 459 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 460 tgt_stat_buf tgt(addr); 461 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 462 tgt.copyOut(mem); 463} 464 465template<class OS> 466static void 467copyOutStat64Buf(TranslatingPort * mem, Addr addr, 468 hst_stat64 *host, bool fakeTTY = false) 469{ 470 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 471 tgt_stat_buf tgt(addr); 472 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 473 tgt.copyOut(mem); 474} 475 476/// Target ioctl() handler. For the most part, programs call ioctl() 477/// only to find out if their stdout is a tty, to determine whether to 478/// do line or block buffering. 479template <class OS> 480SyscallReturn 481ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 482 ThreadContext *tc) 483{ 484 int fd = process->getSyscallArg(tc, 0); 485 unsigned req = process->getSyscallArg(tc, 1); 486 487 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 488 489 if (fd < 0 || process->sim_fd(fd) < 0) { 490 // doesn't map to any simulator fd: not a valid target fd 491 return -EBADF; 492 } 493 494 switch (req) { 495 case OS::TIOCISATTY_: 496 case OS::TIOCGETP_: 497 case OS::TIOCSETP_: 498 case OS::TIOCSETN_: 499 case OS::TIOCSETC_: 500 case OS::TIOCGETC_: 501 case OS::TIOCGETS_: 502 case OS::TIOCGETA_: 503 case OS::TCSETAW_: 504 return -ENOTTY; 505 506 default: 507 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 508 fd, req, tc->readPC()); 509 } 510} 511 512/// Target open() handler. 513template <class OS> 514SyscallReturn 515openFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 516 ThreadContext *tc) 517{ 518 std::string path; 519 520 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 521 return -EFAULT; 522 523 if (path == "/dev/sysdev0") { 524 // This is a memory-mapped high-resolution timer device on Alpha. 525 // We don't support it, so just punt. 526 warn("Ignoring open(%s, ...)\n", path); 527 return -ENOENT; 528 } 529 530 int tgtFlags = process->getSyscallArg(tc, 1); 531 int mode = process->getSyscallArg(tc, 2); 532 int hostFlags = 0; 533 534 // translate open flags 535 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 536 if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 537 tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 538 hostFlags |= OS::openFlagTable[i].hostFlag; 539 } 540 } 541 542 // any target flags left? 543 if (tgtFlags != 0) 544 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 545 546#ifdef __CYGWIN32__ 547 hostFlags |= O_BINARY; 548#endif 549 550 // Adjust path for current working directory 551 path = process->fullPath(path); 552 553 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 554 555 int fd; 556 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 557 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 558 // It's a proc/sys entery and requires special handling 559 fd = OS::openSpecialFile(path, process, tc); 560 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 561 } else { 562 // open the file 563 fd = open(path.c_str(), hostFlags, mode); 564 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 565 } 566 567} 568 569/// Target sysinfo() handler. 570template <class OS> 571SyscallReturn 572sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 573 ThreadContext *tc) 574{ 575 576 TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0)); 577 578 sysinfo->uptime=seconds_since_epoch; 579 sysinfo->totalram=process->system->memSize(); 580 581 sysinfo.copyOut(tc->getMemPort()); 582 583 return 0; 584} 585 586/// Target chmod() handler. 587template <class OS> 588SyscallReturn 589chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 590 ThreadContext *tc) 591{ 592 std::string path; 593 594 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 595 return -EFAULT; 596 597 uint32_t mode = process->getSyscallArg(tc, 1); 598 mode_t hostMode = 0; 599 600 // XXX translate mode flags via OS::something??? 601 hostMode = mode; 602 603 // Adjust path for current working directory 604 path = process->fullPath(path); 605 606 // do the chmod 607 int result = chmod(path.c_str(), hostMode); 608 if (result < 0) 609 return -errno; 610 611 return 0; 612} 613 614 615/// Target fchmod() handler. 616template <class OS> 617SyscallReturn 618fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 619 ThreadContext *tc) 620{ 621 int fd = process->getSyscallArg(tc, 0); 622 if (fd < 0 || process->sim_fd(fd) < 0) { 623 // doesn't map to any simulator fd: not a valid target fd 624 return -EBADF; 625 } 626 627 uint32_t mode = process->getSyscallArg(tc, 1); 628 mode_t hostMode = 0; 629 630 // XXX translate mode flags via OS::someting??? 631 hostMode = mode; 632 633 // do the fchmod 634 int result = fchmod(process->sim_fd(fd), hostMode); 635 if (result < 0) 636 return -errno; 637 638 return 0; 639} 640 641/// Target mremap() handler. 642template <class OS> 643SyscallReturn 644mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 645{ 646 Addr start = process->getSyscallArg(tc, 0); 647 uint64_t old_length = process->getSyscallArg(tc, 1); 648 uint64_t new_length = process->getSyscallArg(tc, 2); 649 uint64_t flags = process->getSyscallArg(tc, 3); 650 651 if ((start % TheISA::VMPageSize != 0) || 652 (new_length % TheISA::VMPageSize != 0)) { 653 warn("mremap failing: arguments not page aligned"); 654 return -EINVAL; 655 } 656 657 if (new_length > old_length) { 658 if ((start + old_length) == process->mmap_end) { 659 uint64_t diff = new_length - old_length; 660 process->pTable->allocate(process->mmap_end, diff); 661 process->mmap_end += diff; 662 return start; 663 } else { 664 // sys/mman.h defined MREMAP_MAYMOVE 665 if (!(flags & 1)) { 666 warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 667 return -ENOMEM; 668 } else { 669 process->pTable->remap(start, old_length, process->mmap_end); 670 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 671 process->mmap_end, process->mmap_end + new_length, new_length); 672 start = process->mmap_end; 673 // add on the remaining unallocated pages 674 process->pTable->allocate(start + old_length, new_length - old_length); 675 process->mmap_end += new_length; 676 warn("returning %08p as start\n", start); 677 return start; 678 } 679 } 680 } else { 681 process->pTable->deallocate(start + new_length, old_length - 682 new_length); 683 return start; 684 } 685} 686 687/// Target stat() handler. 688template <class OS> 689SyscallReturn 690statFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 691 ThreadContext *tc) 692{ 693 std::string path; 694 695 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 696 return -EFAULT; 697 698 // Adjust path for current working directory 699 path = process->fullPath(path); 700 701 struct stat hostBuf; 702 int result = stat(path.c_str(), &hostBuf); 703 704 if (result < 0) 705 return -errno; 706 707 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 708 &hostBuf); 709 710 return 0; 711} 712 713 714/// Target stat64() handler. 715template <class OS> 716SyscallReturn 717stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 718 ThreadContext *tc) 719{ 720 std::string path; 721 722 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 723 return -EFAULT; 724 725 // Adjust path for current working directory 726 path = process->fullPath(path); 727 728#if NO_STAT64 729 struct stat hostBuf; 730 int result = stat(path.c_str(), &hostBuf); 731#else 732 struct stat64 hostBuf; 733 int result = stat64(path.c_str(), &hostBuf); 734#endif 735 736 if (result < 0) 737 return -errno; 738 739 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 740 &hostBuf); 741 742 return 0; 743} 744 745 746/// Target fstat64() handler. 747template <class OS> 748SyscallReturn 749fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 750 ThreadContext *tc) 751{ 752 int fd = process->getSyscallArg(tc, 0); 753 if (fd < 0 || process->sim_fd(fd) < 0) { 754 // doesn't map to any simulator fd: not a valid target fd 755 return -EBADF; 756 } 757 758#if NO_STAT64 759 struct stat hostBuf; 760 int result = fstat(process->sim_fd(fd), &hostBuf); 761#else 762 struct stat64 hostBuf; 763 int result = fstat64(process->sim_fd(fd), &hostBuf); 764#endif 765 766 if (result < 0) 767 return -errno; 768 769 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 770 &hostBuf, (fd == 1)); 771 772 return 0; 773} 774 775 776/// Target lstat() handler. 777template <class OS> 778SyscallReturn 779lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 780 ThreadContext *tc) 781{ 782 std::string path; 783 784 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 785 return -EFAULT; 786 787 // Adjust path for current working directory 788 path = process->fullPath(path); 789 790 struct stat hostBuf; 791 int result = lstat(path.c_str(), &hostBuf); 792 793 if (result < 0) 794 return -errno; 795 796 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 797 &hostBuf); 798 799 return 0; 800} 801 802/// Target lstat64() handler. 803template <class OS> 804SyscallReturn 805lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 806 ThreadContext *tc) 807{ 808 std::string path; 809 810 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 811 return -EFAULT; 812 813 // Adjust path for current working directory 814 path = process->fullPath(path); 815 816#if NO_STAT64 817 struct stat hostBuf; 818 int result = lstat(path.c_str(), &hostBuf); 819#else 820 struct stat64 hostBuf; 821 int result = lstat64(path.c_str(), &hostBuf); 822#endif 823 824 if (result < 0) 825 return -errno; 826 827 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 828 &hostBuf); 829 830 return 0; 831} 832 833/// Target fstat() handler. 834template <class OS> 835SyscallReturn 836fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 837 ThreadContext *tc) 838{ 839 int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 840 841 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 842 843 if (fd < 0) 844 return -EBADF; 845 846 struct stat hostBuf; 847 int result = fstat(fd, &hostBuf); 848 849 if (result < 0) 850 return -errno; 851 852 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 853 &hostBuf, (fd == 1)); 854 855 return 0; 856} 857 858 859/// Target statfs() handler. 860template <class OS> 861SyscallReturn 862statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 863 ThreadContext *tc) 864{ 865 std::string path; 866 867 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 868 return -EFAULT; 869 870 // Adjust path for current working directory 871 path = process->fullPath(path); 872 873 struct statfs hostBuf; 874 int result = statfs(path.c_str(), &hostBuf); 875 876 if (result < 0) 877 return -errno; 878 879 OS::copyOutStatfsBuf(tc->getMemPort(), 880 (Addr)(process->getSyscallArg(tc, 1)), &hostBuf); 881 882 return 0; 883} 884 885 886/// Target fstatfs() handler. 887template <class OS> 888SyscallReturn 889fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 890 ThreadContext *tc) 891{ 892 int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 893 894 if (fd < 0) 895 return -EBADF; 896 897 struct statfs hostBuf; 898 int result = fstatfs(fd, &hostBuf); 899 900 if (result < 0) 901 return -errno; 902 903 OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1), 904 &hostBuf); 905 906 return 0; 907} 908 909 910/// Target writev() handler. 911template <class OS> 912SyscallReturn 913writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 914 ThreadContext *tc) 915{ 916 int fd = process->getSyscallArg(tc, 0); 917 if (fd < 0 || process->sim_fd(fd) < 0) { 918 // doesn't map to any simulator fd: not a valid target fd 919 return -EBADF; 920 } 921 922 TranslatingPort *p = tc->getMemPort(); 923 uint64_t tiov_base = process->getSyscallArg(tc, 1); 924 size_t count = process->getSyscallArg(tc, 2); 925 struct iovec hiov[count]; 926 for (size_t i = 0; i < count; ++i) { 927 typename OS::tgt_iovec tiov; 928 929 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 930 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 931 hiov[i].iov_len = gtoh(tiov.iov_len); 932 hiov[i].iov_base = new char [hiov[i].iov_len]; 933 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 934 hiov[i].iov_len); 935 } 936 937 int result = writev(process->sim_fd(fd), hiov, count); 938 939 for (size_t i = 0; i < count; ++i) 940 delete [] (char *)hiov[i].iov_base; 941 942 if (result < 0) 943 return -errno; 944 945 return 0; 946} 947 948 949/// Target mmap() handler. 950/// 951/// We don't really handle mmap(). If the target is mmaping an 952/// anonymous region or /dev/zero, we can get away with doing basically 953/// nothing (since memory is initialized to zero and the simulator 954/// doesn't really check addresses anyway). Always print a warning, 955/// since this could be seriously broken if we're not mapping 956/// /dev/zero. 957// 958/// Someday we should explicitly check for /dev/zero in open, flag the 959/// file descriptor, and fail (or implement!) a non-anonymous mmap to 960/// anything else. 961template <class OS> 962SyscallReturn 963mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 964{ 965 Addr start = p->getSyscallArg(tc, 0); 966 uint64_t length = p->getSyscallArg(tc, 1); 967 // int prot = p->getSyscallArg(tc, 2); 968 int flags = p->getSyscallArg(tc, 3); 969 // int fd = p->sim_fd(p->getSyscallArg(tc, 4)); 970 // int offset = p->getSyscallArg(tc, 5); 971 972 973 if ((start % TheISA::VMPageSize) != 0 || 974 (length % TheISA::VMPageSize) != 0) { 975 warn("mmap failing: arguments not page-aligned: " 976 "start 0x%x length 0x%x", 977 start, length); 978 return -EINVAL; 979 } 980 981 if (start != 0) { 982 warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 983 start, p->mmap_end); 984 } 985 986 // pick next address from our "mmap region" 987 if (OS::mmapGrowsDown()) { 988 start = p->mmap_end - length; 989 p->mmap_end = start; 990 } else { 991 start = p->mmap_end; 992 p->mmap_end += length; 993 } 994 p->pTable->allocate(start, length); 995 996 if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 997 warn("allowing mmap of file @ fd %d. " 998 "This will break if not /dev/zero.", p->getSyscallArg(tc, 4)); 999 } 1000 1001 return start; 1002} 1003 1004/// Target getrlimit() handler. 1005template <class OS> 1006SyscallReturn 1007getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1008 ThreadContext *tc) 1009{ 1010 unsigned resource = process->getSyscallArg(tc, 0); 1011 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1)); 1012 1013 switch (resource) { 1014 case OS::TGT_RLIMIT_STACK: 1015 // max stack size in bytes: make up a number (8MB for now) 1016 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1017 rlp->rlim_cur = htog(rlp->rlim_cur); 1018 rlp->rlim_max = htog(rlp->rlim_max); 1019 break; 1020 1021 case OS::TGT_RLIMIT_DATA: 1022 // max data segment size in bytes: make up a number 1023 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1024 rlp->rlim_cur = htog(rlp->rlim_cur); 1025 rlp->rlim_max = htog(rlp->rlim_max); 1026 break; 1027 1028 default: 1029 std::cerr << "getrlimitFunc: unimplemented resource " << resource 1030 << std::endl; 1031 abort(); 1032 break; 1033 } 1034 1035 rlp.copyOut(tc->getMemPort()); 1036 return 0; 1037} 1038 1039/// Target gettimeofday() handler. 1040template <class OS> 1041SyscallReturn 1042gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1043 ThreadContext *tc) 1044{ 1045 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0)); 1046 1047 getElapsedTime(tp->tv_sec, tp->tv_usec); 1048 tp->tv_sec += seconds_since_epoch; 1049 tp->tv_sec = TheISA::htog(tp->tv_sec); 1050 tp->tv_usec = TheISA::htog(tp->tv_usec); 1051 1052 tp.copyOut(tc->getMemPort()); 1053 1054 return 0; 1055} 1056 1057 1058/// Target utimes() handler. 1059template <class OS> 1060SyscallReturn 1061utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1062 ThreadContext *tc) 1063{ 1064 std::string path; 1065 1066 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 1067 return -EFAULT; 1068 1069 TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1)); 1070 tp.copyIn(tc->getMemPort()); 1071 1072 struct timeval hostTimeval[2]; 1073 for (int i = 0; i < 2; ++i) 1074 { 1075 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 1076 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 1077 } 1078 1079 // Adjust path for current working directory 1080 path = process->fullPath(path); 1081 1082 int result = utimes(path.c_str(), hostTimeval); 1083 1084 if (result < 0) 1085 return -errno; 1086 1087 return 0; 1088} 1089/// Target getrusage() function. 1090template <class OS> 1091SyscallReturn 1092getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1093 ThreadContext *tc) 1094{ 1095 int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN 1096 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1)); 1097 1098 rup->ru_utime.tv_sec = 0; 1099 rup->ru_utime.tv_usec = 0; 1100 rup->ru_stime.tv_sec = 0; 1101 rup->ru_stime.tv_usec = 0; 1102 rup->ru_maxrss = 0; 1103 rup->ru_ixrss = 0; 1104 rup->ru_idrss = 0; 1105 rup->ru_isrss = 0; 1106 rup->ru_minflt = 0; 1107 rup->ru_majflt = 0; 1108 rup->ru_nswap = 0; 1109 rup->ru_inblock = 0; 1110 rup->ru_oublock = 0; 1111 rup->ru_msgsnd = 0; 1112 rup->ru_msgrcv = 0; 1113 rup->ru_nsignals = 0; 1114 rup->ru_nvcsw = 0; 1115 rup->ru_nivcsw = 0; 1116 1117 switch (who) { 1118 case OS::TGT_RUSAGE_SELF: 1119 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1120 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 1121 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 1122 break; 1123 1124 case OS::TGT_RUSAGE_CHILDREN: 1125 // do nothing. We have no child processes, so they take no time. 1126 break; 1127 1128 default: 1129 // don't really handle THREAD or CHILDREN, but just warn and 1130 // plow ahead 1131 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1132 who); 1133 } 1134 1135 rup.copyOut(tc->getMemPort()); 1136 1137 return 0; 1138} 1139 1140/// Target times() function. 1141template <class OS> 1142SyscallReturn 1143timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1144 ThreadContext *tc) 1145{ 1146 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, 0)); 1147 1148 // Fill in the time structure (in clocks) 1149 int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s; 1150 bufp->tms_utime = clocks; 1151 bufp->tms_stime = 0; 1152 bufp->tms_cutime = 0; 1153 bufp->tms_cstime = 0; 1154 1155 // Convert to host endianness 1156 bufp->tms_utime = htog(bufp->tms_utime); 1157 1158 // Write back 1159 bufp.copyOut(tc->getMemPort()); 1160 1161 // Return clock ticks since system boot 1162 return clocks; 1163} 1164 1165/// Target time() function. 1166template <class OS> 1167SyscallReturn 1168timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1169 ThreadContext *tc) 1170{ 1171 typename OS::time_t sec, usec; 1172 getElapsedTime(sec, usec); 1173 sec += seconds_since_epoch; 1174 1175 Addr taddr = (Addr)process->getSyscallArg(tc, 0); 1176 if(taddr != 0) { 1177 typename OS::time_t t = sec; 1178 t = htog(t); 1179 TranslatingPort *p = tc->getMemPort(); 1180 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 1181 } 1182 return sec; 1183} 1184 1185 1186#endif // __SIM_SYSCALL_EMUL_HH__ 1187