syscall_emul.hh revision 5513
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 "sim/host.hh" // for Addr 54#include "base/chunk_generator.hh" 55#include "base/intmath.hh" // for RoundUp 56#include "base/misc.hh" 57#include "base/trace.hh" 58#include "cpu/base.hh" 59#include "cpu/thread_context.hh" 60#include "mem/translating_port.hh" 61#include "mem/page_table.hh" 62#include "sim/process.hh" 63 64/// 65/// System call descriptor. 66/// 67class SyscallDesc { 68 69 public: 70 71 /// Typedef for target syscall handler functions. 72 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 73 LiveProcess *, ThreadContext *); 74 75 const char *name; //!< Syscall name (e.g., "open"). 76 FuncPtr funcPtr; //!< Pointer to emulation function. 77 int flags; //!< Flags (see Flags enum). 78 79 /// Flag values for controlling syscall behavior. 80 enum Flags { 81 /// Don't set return regs according to funcPtr return value. 82 /// Used for syscalls with non-standard return conventions 83 /// that explicitly set the ThreadContext regs (e.g., 84 /// sigreturn). 85 SuppressReturnValue = 1 86 }; 87 88 /// Constructor. 89 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 90 : name(_name), funcPtr(_funcPtr), flags(_flags) 91 { 92 } 93 94 /// Emulate the syscall. Public interface for calling through funcPtr. 95 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 96}; 97 98 99class BaseBufferArg { 100 101 public: 102 103 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 104 { 105 bufPtr = new uint8_t[size]; 106 // clear out buffer: in case we only partially populate this, 107 // and then do a copyOut(), we want to make sure we don't 108 // introduce any random junk into the simulated address space 109 memset(bufPtr, 0, size); 110 } 111 112 virtual ~BaseBufferArg() { delete [] bufPtr; } 113 114 // 115 // copy data into simulator space (read from target memory) 116 // 117 virtual bool copyIn(TranslatingPort *memport) 118 { 119 memport->readBlob(addr, bufPtr, size); 120 return true; // no EFAULT detection for now 121 } 122 123 // 124 // copy data out of simulator space (write to target memory) 125 // 126 virtual bool copyOut(TranslatingPort *memport) 127 { 128 memport->writeBlob(addr, bufPtr, size); 129 return true; // no EFAULT detection for now 130 } 131 132 protected: 133 Addr addr; 134 int size; 135 uint8_t *bufPtr; 136}; 137 138 139class BufferArg : public BaseBufferArg 140{ 141 public: 142 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 143 void *bufferPtr() { return bufPtr; } 144}; 145 146template <class T> 147class TypedBufferArg : public BaseBufferArg 148{ 149 public: 150 // user can optionally specify a specific number of bytes to 151 // allocate to deal with those structs that have variable-size 152 // arrays at the end 153 TypedBufferArg(Addr _addr, int _size = sizeof(T)) 154 : BaseBufferArg(_addr, _size) 155 { } 156 157 // type case 158 operator T*() { return (T *)bufPtr; } 159 160 // dereference operators 161 T &operator*() { return *((T *)bufPtr); } 162 T* operator->() { return (T *)bufPtr; } 163 T &operator[](int i) { return ((T *)bufPtr)[i]; } 164}; 165 166////////////////////////////////////////////////////////////////////// 167// 168// The following emulation functions are generic enough that they 169// don't need to be recompiled for different emulated OS's. They are 170// defined in sim/syscall_emul.cc. 171// 172////////////////////////////////////////////////////////////////////// 173 174 175/// Handler for unimplemented syscalls that we haven't thought about. 176SyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 177 LiveProcess *p, ThreadContext *tc); 178 179/// Handler for unimplemented syscalls that we never intend to 180/// implement (signal handling, etc.) and should not affect the correct 181/// behavior of the program. Print a warning only if the appropriate 182/// trace flag is enabled. Return success to the target program. 183SyscallReturn ignoreFunc(SyscallDesc *desc, int num, 184 LiveProcess *p, ThreadContext *tc); 185 186/// Target exit() handler: terminate simulation. 187SyscallReturn exitFunc(SyscallDesc *desc, int num, 188 LiveProcess *p, ThreadContext *tc); 189 190/// Target getpagesize() handler. 191SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 192 LiveProcess *p, ThreadContext *tc); 193 194/// Target obreak() handler: set brk address. 195SyscallReturn obreakFunc(SyscallDesc *desc, int num, 196 LiveProcess *p, ThreadContext *tc); 197 198/// Target close() handler. 199SyscallReturn closeFunc(SyscallDesc *desc, int num, 200 LiveProcess *p, ThreadContext *tc); 201 202/// Target read() handler. 203SyscallReturn readFunc(SyscallDesc *desc, int num, 204 LiveProcess *p, ThreadContext *tc); 205 206/// Target write() handler. 207SyscallReturn writeFunc(SyscallDesc *desc, int num, 208 LiveProcess *p, ThreadContext *tc); 209 210/// Target lseek() handler. 211SyscallReturn lseekFunc(SyscallDesc *desc, int num, 212 LiveProcess *p, ThreadContext *tc); 213 214/// Target _llseek() handler. 215SyscallReturn _llseekFunc(SyscallDesc *desc, int num, 216 LiveProcess *p, ThreadContext *tc); 217 218/// Target munmap() handler. 219SyscallReturn munmapFunc(SyscallDesc *desc, int num, 220 LiveProcess *p, ThreadContext *tc); 221 222/// Target gethostname() handler. 223SyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 224 LiveProcess *p, ThreadContext *tc); 225 226/// Target getcwd() handler. 227SyscallReturn getcwdFunc(SyscallDesc *desc, int num, 228 LiveProcess *p, ThreadContext *tc); 229 230/// Target unlink() handler. 231SyscallReturn readlinkFunc(SyscallDesc *desc, int num, 232 LiveProcess *p, ThreadContext *tc); 233 234/// Target unlink() handler. 235SyscallReturn unlinkFunc(SyscallDesc *desc, int num, 236 LiveProcess *p, ThreadContext *tc); 237 238/// Target mkdir() handler. 239SyscallReturn mkdirFunc(SyscallDesc *desc, int num, 240 LiveProcess *p, ThreadContext *tc); 241 242/// Target rename() handler. 243SyscallReturn renameFunc(SyscallDesc *desc, int num, 244 LiveProcess *p, ThreadContext *tc); 245 246 247/// Target truncate() handler. 248SyscallReturn truncateFunc(SyscallDesc *desc, int num, 249 LiveProcess *p, ThreadContext *tc); 250 251 252/// Target ftruncate() handler. 253SyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 254 LiveProcess *p, ThreadContext *tc); 255 256 257/// Target umask() handler. 258SyscallReturn umaskFunc(SyscallDesc *desc, int num, 259 LiveProcess *p, ThreadContext *tc); 260 261 262/// Target chown() handler. 263SyscallReturn chownFunc(SyscallDesc *desc, int num, 264 LiveProcess *p, ThreadContext *tc); 265 266 267/// Target fchown() handler. 268SyscallReturn fchownFunc(SyscallDesc *desc, int num, 269 LiveProcess *p, ThreadContext *tc); 270 271/// Target dup() handler. 272SyscallReturn dupFunc(SyscallDesc *desc, int num, 273 LiveProcess *process, ThreadContext *tc); 274 275/// Target fnctl() handler. 276SyscallReturn fcntlFunc(SyscallDesc *desc, int num, 277 LiveProcess *process, ThreadContext *tc); 278 279/// Target fcntl64() handler. 280SyscallReturn fcntl64Func(SyscallDesc *desc, int num, 281 LiveProcess *process, ThreadContext *tc); 282 283/// Target setuid() handler. 284SyscallReturn setuidFunc(SyscallDesc *desc, int num, 285 LiveProcess *p, ThreadContext *tc); 286 287/// Target getpid() handler. 288SyscallReturn getpidFunc(SyscallDesc *desc, int num, 289 LiveProcess *p, ThreadContext *tc); 290 291/// Target getuid() handler. 292SyscallReturn getuidFunc(SyscallDesc *desc, int num, 293 LiveProcess *p, ThreadContext *tc); 294 295/// Target getgid() handler. 296SyscallReturn getgidFunc(SyscallDesc *desc, int num, 297 LiveProcess *p, ThreadContext *tc); 298 299/// Target getppid() handler. 300SyscallReturn getppidFunc(SyscallDesc *desc, int num, 301 LiveProcess *p, ThreadContext *tc); 302 303/// Target geteuid() handler. 304SyscallReturn geteuidFunc(SyscallDesc *desc, int num, 305 LiveProcess *p, ThreadContext *tc); 306 307/// Target getegid() handler. 308SyscallReturn getegidFunc(SyscallDesc *desc, int num, 309 LiveProcess *p, ThreadContext *tc); 310 311 312 313/// Pseudo Funcs - These functions use a different return convension, 314/// returning a second value in a register other than the normal return register 315SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 316 LiveProcess *process, ThreadContext *tc); 317 318/// Target getpidPseudo() handler. 319SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 320 LiveProcess *p, ThreadContext *tc); 321 322/// Target getuidPseudo() handler. 323SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 324 LiveProcess *p, ThreadContext *tc); 325 326/// Target getgidPseudo() handler. 327SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 328 LiveProcess *p, ThreadContext *tc); 329 330 331/// A readable name for 1,000,000, for converting microseconds to seconds. 332const int one_million = 1000000; 333 334/// Approximate seconds since the epoch (1/1/1970). About a billion, 335/// by my reckoning. We want to keep this a constant (not use the 336/// real-world time) to keep simulations repeatable. 337const unsigned seconds_since_epoch = 1000000000; 338 339/// Helper function to convert current elapsed time to seconds and 340/// microseconds. 341template <class T1, class T2> 342void 343getElapsedTime(T1 &sec, T2 &usec) 344{ 345 int elapsed_usecs = curTick / Clock::Int::us; 346 sec = elapsed_usecs / one_million; 347 usec = elapsed_usecs % one_million; 348} 349 350////////////////////////////////////////////////////////////////////// 351// 352// The following emulation functions are generic, but need to be 353// templated to account for differences in types, constants, etc. 354// 355////////////////////////////////////////////////////////////////////// 356 357#if NO_STAT64 358 typedef struct stat hst_stat; 359 typedef struct stat hst_stat64; 360#else 361 typedef struct stat hst_stat; 362 typedef struct stat64 hst_stat64; 363#endif 364 365//// Helper function to convert a host stat buffer to a target stat 366//// buffer. Also copies the target buffer out to the simulated 367//// memory space. Used by stat(), fstat(), and lstat(). 368 369template <typename target_stat, typename host_stat> 370static void 371convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 372{ 373 using namespace TheISA; 374 375 if (fakeTTY) 376 tgt->st_dev = 0xA; 377 else 378 tgt->st_dev = host->st_dev; 379 tgt->st_dev = htog(tgt->st_dev); 380 tgt->st_ino = host->st_ino; 381 tgt->st_ino = htog(tgt->st_ino); 382 tgt->st_mode = host->st_mode; 383 tgt->st_mode = htog(tgt->st_mode); 384 tgt->st_nlink = host->st_nlink; 385 tgt->st_nlink = htog(tgt->st_nlink); 386 tgt->st_uid = host->st_uid; 387 tgt->st_uid = htog(tgt->st_uid); 388 tgt->st_gid = host->st_gid; 389 tgt->st_gid = htog(tgt->st_gid); 390 if (fakeTTY) 391 tgt->st_rdev = 0x880d; 392 else 393 tgt->st_rdev = host->st_rdev; 394 tgt->st_rdev = htog(tgt->st_rdev); 395 tgt->st_size = host->st_size; 396 tgt->st_size = htog(tgt->st_size); 397 tgt->st_atimeX = host->st_atime; 398 tgt->st_atimeX = htog(tgt->st_atimeX); 399 tgt->st_mtimeX = host->st_mtime; 400 tgt->st_mtimeX = htog(tgt->st_mtimeX); 401 tgt->st_ctimeX = host->st_ctime; 402 tgt->st_ctimeX = htog(tgt->st_ctimeX); 403 // Force the block size to be 8k. This helps to ensure buffered io works 404 // consistently across different hosts. 405 tgt->st_blksize = 0x2000; 406 tgt->st_blksize = htog(tgt->st_blksize); 407 tgt->st_blocks = host->st_blocks; 408 tgt->st_blocks = htog(tgt->st_blocks); 409} 410 411// Same for stat64 412 413template <typename target_stat, typename host_stat64> 414static void 415convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 416{ 417 using namespace TheISA; 418 419 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 420#if defined(STAT_HAVE_NSEC) 421 tgt->st_atime_nsec = host->st_atime_nsec; 422 tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 423 tgt->st_mtime_nsec = host->st_mtime_nsec; 424 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 425 tgt->st_ctime_nsec = host->st_ctime_nsec; 426 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 427#else 428 tgt->st_atime_nsec = 0; 429 tgt->st_mtime_nsec = 0; 430 tgt->st_ctime_nsec = 0; 431#endif 432} 433 434//Here are a couple convenience functions 435template<class OS> 436static void 437copyOutStatBuf(TranslatingPort * mem, Addr addr, 438 hst_stat *host, bool fakeTTY = false) 439{ 440 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 441 tgt_stat_buf tgt(addr); 442 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 443 tgt.copyOut(mem); 444} 445 446template<class OS> 447static void 448copyOutStat64Buf(TranslatingPort * mem, Addr addr, 449 hst_stat64 *host, bool fakeTTY = false) 450{ 451 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 452 tgt_stat_buf tgt(addr); 453 convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 454 tgt.copyOut(mem); 455} 456 457/// Target ioctl() handler. For the most part, programs call ioctl() 458/// only to find out if their stdout is a tty, to determine whether to 459/// do line or block buffering. 460template <class OS> 461SyscallReturn 462ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 463 ThreadContext *tc) 464{ 465 int fd = tc->getSyscallArg(0); 466 unsigned req = tc->getSyscallArg(1); 467 468 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 469 470 if (fd < 0 || process->sim_fd(fd) < 0) { 471 // doesn't map to any simulator fd: not a valid target fd 472 return -EBADF; 473 } 474 475 switch (req) { 476 case OS::TIOCISATTY_: 477 case OS::TIOCGETP_: 478 case OS::TIOCSETP_: 479 case OS::TIOCSETN_: 480 case OS::TIOCSETC_: 481 case OS::TIOCGETC_: 482 case OS::TIOCGETS_: 483 case OS::TIOCGETA_: 484 return -ENOTTY; 485 486 default: 487 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 488 fd, req, tc->readPC()); 489 } 490} 491 492/// Target open() handler. 493template <class OS> 494SyscallReturn 495openFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 496 ThreadContext *tc) 497{ 498 std::string path; 499 500 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 501 return -EFAULT; 502 503 if (path == "/dev/sysdev0") { 504 // This is a memory-mapped high-resolution timer device on Alpha. 505 // We don't support it, so just punt. 506 warn("Ignoring open(%s, ...)\n", path); 507 return -ENOENT; 508 } 509 510 int tgtFlags = tc->getSyscallArg(1); 511 int mode = tc->getSyscallArg(2); 512 int hostFlags = 0; 513 514 // translate open flags 515 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 516 if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 517 tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 518 hostFlags |= OS::openFlagTable[i].hostFlag; 519 } 520 } 521 522 // any target flags left? 523 if (tgtFlags != 0) 524 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 525 526#ifdef __CYGWIN32__ 527 hostFlags |= O_BINARY; 528#endif 529 530 // Adjust path for current working directory 531 path = process->fullPath(path); 532 533 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 534 535 // open the file 536 int fd = open(path.c_str(), hostFlags, mode); 537 538 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 539} 540 541 542/// Target chmod() handler. 543template <class OS> 544SyscallReturn 545chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 546 ThreadContext *tc) 547{ 548 std::string path; 549 550 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 551 return -EFAULT; 552 553 uint32_t mode = tc->getSyscallArg(1); 554 mode_t hostMode = 0; 555 556 // XXX translate mode flags via OS::something??? 557 hostMode = mode; 558 559 // Adjust path for current working directory 560 path = process->fullPath(path); 561 562 // do the chmod 563 int result = chmod(path.c_str(), hostMode); 564 if (result < 0) 565 return -errno; 566 567 return 0; 568} 569 570 571/// Target fchmod() handler. 572template <class OS> 573SyscallReturn 574fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 575 ThreadContext *tc) 576{ 577 int fd = tc->getSyscallArg(0); 578 if (fd < 0 || process->sim_fd(fd) < 0) { 579 // doesn't map to any simulator fd: not a valid target fd 580 return -EBADF; 581 } 582 583 uint32_t mode = tc->getSyscallArg(1); 584 mode_t hostMode = 0; 585 586 // XXX translate mode flags via OS::someting??? 587 hostMode = mode; 588 589 // do the fchmod 590 int result = fchmod(process->sim_fd(fd), hostMode); 591 if (result < 0) 592 return -errno; 593 594 return 0; 595} 596 597 598/// Target stat() handler. 599template <class OS> 600SyscallReturn 601statFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 602 ThreadContext *tc) 603{ 604 std::string path; 605 606 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 607 return -EFAULT; 608 609 // Adjust path for current working directory 610 path = process->fullPath(path); 611 612 struct stat hostBuf; 613 int result = stat(path.c_str(), &hostBuf); 614 615 if (result < 0) 616 return -errno; 617 618 copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 619 620 return 0; 621} 622 623 624/// Target stat64() handler. 625template <class OS> 626SyscallReturn 627stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 628 ThreadContext *tc) 629{ 630 std::string path; 631 632 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 633 return -EFAULT; 634 635 // Adjust path for current working directory 636 path = process->fullPath(path); 637 638#if NO_STAT64 639 struct stat hostBuf; 640 int result = stat(path.c_str(), &hostBuf); 641#else 642 struct stat64 hostBuf; 643 int result = stat64(path.c_str(), &hostBuf); 644#endif 645 646 if (result < 0) 647 return -errno; 648 649 copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 650 651 return 0; 652} 653 654 655/// Target fstat64() handler. 656template <class OS> 657SyscallReturn 658fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 659 ThreadContext *tc) 660{ 661 int fd = tc->getSyscallArg(0); 662 if (fd < 0 || process->sim_fd(fd) < 0) { 663 // doesn't map to any simulator fd: not a valid target fd 664 return -EBADF; 665 } 666 667#if NO_STAT64 668 struct stat hostBuf; 669 int result = fstat(process->sim_fd(fd), &hostBuf); 670#else 671 struct stat64 hostBuf; 672 int result = fstat64(process->sim_fd(fd), &hostBuf); 673#endif 674 675 if (result < 0) 676 return -errno; 677 678 copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), 679 &hostBuf, (fd == 1)); 680 681 return 0; 682} 683 684 685/// Target lstat() handler. 686template <class OS> 687SyscallReturn 688lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 689 ThreadContext *tc) 690{ 691 std::string path; 692 693 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 694 return -EFAULT; 695 696 // Adjust path for current working directory 697 path = process->fullPath(path); 698 699 struct stat hostBuf; 700 int result = lstat(path.c_str(), &hostBuf); 701 702 if (result < 0) 703 return -errno; 704 705 copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 706 707 return 0; 708} 709 710/// Target lstat64() handler. 711template <class OS> 712SyscallReturn 713lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 714 ThreadContext *tc) 715{ 716 std::string path; 717 718 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 719 return -EFAULT; 720 721 // Adjust path for current working directory 722 path = process->fullPath(path); 723 724#if NO_STAT64 725 struct stat hostBuf; 726 int result = lstat(path.c_str(), &hostBuf); 727#else 728 struct stat64 hostBuf; 729 int result = lstat64(path.c_str(), &hostBuf); 730#endif 731 732 if (result < 0) 733 return -errno; 734 735 copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 736 737 return 0; 738} 739 740/// Target fstat() handler. 741template <class OS> 742SyscallReturn 743fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 744 ThreadContext *tc) 745{ 746 int fd = process->sim_fd(tc->getSyscallArg(0)); 747 748 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 749 750 if (fd < 0) 751 return -EBADF; 752 753 struct stat hostBuf; 754 int result = fstat(fd, &hostBuf); 755 756 if (result < 0) 757 return -errno; 758 759 copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), 760 &hostBuf, (fd == 1)); 761 762 return 0; 763} 764 765 766/// Target statfs() handler. 767template <class OS> 768SyscallReturn 769statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 770 ThreadContext *tc) 771{ 772 std::string path; 773 774 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 775 return -EFAULT; 776 777 // Adjust path for current working directory 778 path = process->fullPath(path); 779 780 struct statfs hostBuf; 781 int result = statfs(path.c_str(), &hostBuf); 782 783 if (result < 0) 784 return -errno; 785 786 OS::copyOutStatfsBuf(tc->getMemPort(), 787 (Addr)(tc->getSyscallArg(1)), &hostBuf); 788 789 return 0; 790} 791 792 793/// Target fstatfs() handler. 794template <class OS> 795SyscallReturn 796fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 797 ThreadContext *tc) 798{ 799 int fd = process->sim_fd(tc->getSyscallArg(0)); 800 801 if (fd < 0) 802 return -EBADF; 803 804 struct statfs hostBuf; 805 int result = fstatfs(fd, &hostBuf); 806 807 if (result < 0) 808 return -errno; 809 810 OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), 811 &hostBuf); 812 813 return 0; 814} 815 816 817/// Target writev() handler. 818template <class OS> 819SyscallReturn 820writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 821 ThreadContext *tc) 822{ 823 int fd = tc->getSyscallArg(0); 824 if (fd < 0 || process->sim_fd(fd) < 0) { 825 // doesn't map to any simulator fd: not a valid target fd 826 return -EBADF; 827 } 828 829 TranslatingPort *p = tc->getMemPort(); 830 uint64_t tiov_base = tc->getSyscallArg(1); 831 size_t count = tc->getSyscallArg(2); 832 struct iovec hiov[count]; 833 for (int i = 0; i < count; ++i) 834 { 835 typename OS::tgt_iovec tiov; 836 837 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 838 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 839 hiov[i].iov_len = gtoh(tiov.iov_len); 840 hiov[i].iov_base = new char [hiov[i].iov_len]; 841 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 842 hiov[i].iov_len); 843 } 844 845 int result = writev(process->sim_fd(fd), hiov, count); 846 847 for (int i = 0; i < count; ++i) 848 { 849 delete [] (char *)hiov[i].iov_base; 850 } 851 852 if (result < 0) 853 return -errno; 854 855 return 0; 856} 857 858 859/// Target mmap() handler. 860/// 861/// We don't really handle mmap(). If the target is mmaping an 862/// anonymous region or /dev/zero, we can get away with doing basically 863/// nothing (since memory is initialized to zero and the simulator 864/// doesn't really check addresses anyway). Always print a warning, 865/// since this could be seriously broken if we're not mapping 866/// /dev/zero. 867// 868/// Someday we should explicitly check for /dev/zero in open, flag the 869/// file descriptor, and fail (or implement!) a non-anonymous mmap to 870/// anything else. 871template <class OS> 872SyscallReturn 873mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 874{ 875 Addr start = tc->getSyscallArg(0); 876 uint64_t length = tc->getSyscallArg(1); 877 // int prot = tc->getSyscallArg(2); 878 int flags = tc->getSyscallArg(3); 879 // int fd = p->sim_fd(tc->getSyscallArg(4)); 880 // int offset = tc->getSyscallArg(5); 881 882 if ((start % TheISA::VMPageSize) != 0 || 883 (length % TheISA::VMPageSize) != 0) { 884 warn("mmap failing: arguments not page-aligned: " 885 "start 0x%x length 0x%x", 886 start, length); 887 return -EINVAL; 888 } 889 890 if (start != 0) { 891 warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 892 start, p->mmap_end); 893 } 894 895 // pick next address from our "mmap region" 896 start = p->mmap_end; 897 p->pTable->allocate(start, length); 898 p->mmap_end += length; 899 900 if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 901 warn("allowing mmap of file @ fd %d. " 902 "This will break if not /dev/zero.", tc->getSyscallArg(4)); 903 } 904 905 return start; 906} 907 908/// Target getrlimit() handler. 909template <class OS> 910SyscallReturn 911getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 912 ThreadContext *tc) 913{ 914 unsigned resource = tc->getSyscallArg(0); 915 TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1)); 916 917 switch (resource) { 918 case OS::TGT_RLIMIT_STACK: 919 // max stack size in bytes: make up a number (2MB for now) 920 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 921 rlp->rlim_cur = htog(rlp->rlim_cur); 922 rlp->rlim_max = htog(rlp->rlim_max); 923 break; 924 925 default: 926 std::cerr << "getrlimitFunc: unimplemented resource " << resource 927 << std::endl; 928 abort(); 929 break; 930 } 931 932 rlp.copyOut(tc->getMemPort()); 933 return 0; 934} 935 936/// Target gettimeofday() handler. 937template <class OS> 938SyscallReturn 939gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 940 ThreadContext *tc) 941{ 942 TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0)); 943 944 getElapsedTime(tp->tv_sec, tp->tv_usec); 945 tp->tv_sec += seconds_since_epoch; 946 tp->tv_sec = htog(tp->tv_sec); 947 tp->tv_usec = htog(tp->tv_usec); 948 949 tp.copyOut(tc->getMemPort()); 950 951 return 0; 952} 953 954 955/// Target utimes() handler. 956template <class OS> 957SyscallReturn 958utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 959 ThreadContext *tc) 960{ 961 std::string path; 962 963 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 964 return -EFAULT; 965 966 TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1)); 967 tp.copyIn(tc->getMemPort()); 968 969 struct timeval hostTimeval[2]; 970 for (int i = 0; i < 2; ++i) 971 { 972 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 973 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 974 } 975 976 // Adjust path for current working directory 977 path = process->fullPath(path); 978 979 int result = utimes(path.c_str(), hostTimeval); 980 981 if (result < 0) 982 return -errno; 983 984 return 0; 985} 986/// Target getrusage() function. 987template <class OS> 988SyscallReturn 989getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 990 ThreadContext *tc) 991{ 992 int who = tc->getSyscallArg(0); // THREAD, SELF, or CHILDREN 993 TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1)); 994 995 rup->ru_utime.tv_sec = 0; 996 rup->ru_utime.tv_usec = 0; 997 rup->ru_stime.tv_sec = 0; 998 rup->ru_stime.tv_usec = 0; 999 rup->ru_maxrss = 0; 1000 rup->ru_ixrss = 0; 1001 rup->ru_idrss = 0; 1002 rup->ru_isrss = 0; 1003 rup->ru_minflt = 0; 1004 rup->ru_majflt = 0; 1005 rup->ru_nswap = 0; 1006 rup->ru_inblock = 0; 1007 rup->ru_oublock = 0; 1008 rup->ru_msgsnd = 0; 1009 rup->ru_msgrcv = 0; 1010 rup->ru_nsignals = 0; 1011 rup->ru_nvcsw = 0; 1012 rup->ru_nivcsw = 0; 1013 1014 switch (who) { 1015 case OS::TGT_RUSAGE_SELF: 1016 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1017 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 1018 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 1019 break; 1020 1021 case OS::TGT_RUSAGE_CHILDREN: 1022 // do nothing. We have no child processes, so they take no time. 1023 break; 1024 1025 default: 1026 // don't really handle THREAD or CHILDREN, but just warn and 1027 // plow ahead 1028 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1029 who); 1030 } 1031 1032 rup.copyOut(tc->getMemPort()); 1033 1034 return 0; 1035} 1036 1037 1038 1039 1040#endif // __SIM_SYSCALL_EMUL_HH__ 1041