syscall_emul.hh revision 6683
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 umask() handler. 264SyscallReturn umaskFunc(SyscallDesc *desc, int num, 265 LiveProcess *p, ThreadContext *tc); 266 267 268/// Target chown() handler. 269SyscallReturn chownFunc(SyscallDesc *desc, int num, 270 LiveProcess *p, ThreadContext *tc); 271 272 273/// Target fchown() handler. 274SyscallReturn fchownFunc(SyscallDesc *desc, int num, 275 LiveProcess *p, ThreadContext *tc); 276 277/// Target dup() handler. 278SyscallReturn dupFunc(SyscallDesc *desc, int num, 279 LiveProcess *process, ThreadContext *tc); 280 281/// Target fnctl() handler. 282SyscallReturn fcntlFunc(SyscallDesc *desc, int num, 283 LiveProcess *process, ThreadContext *tc); 284 285/// Target fcntl64() handler. 286SyscallReturn fcntl64Func(SyscallDesc *desc, int num, 287 LiveProcess *process, ThreadContext *tc); 288 289/// Target setuid() handler. 290SyscallReturn setuidFunc(SyscallDesc *desc, int num, 291 LiveProcess *p, ThreadContext *tc); 292 293/// Target getpid() handler. 294SyscallReturn getpidFunc(SyscallDesc *desc, int num, 295 LiveProcess *p, ThreadContext *tc); 296 297/// Target getuid() handler. 298SyscallReturn getuidFunc(SyscallDesc *desc, int num, 299 LiveProcess *p, ThreadContext *tc); 300 301/// Target getgid() handler. 302SyscallReturn getgidFunc(SyscallDesc *desc, int num, 303 LiveProcess *p, ThreadContext *tc); 304 305/// Target getppid() handler. 306SyscallReturn getppidFunc(SyscallDesc *desc, int num, 307 LiveProcess *p, ThreadContext *tc); 308 309/// Target geteuid() handler. 310SyscallReturn geteuidFunc(SyscallDesc *desc, int num, 311 LiveProcess *p, ThreadContext *tc); 312 313/// Target getegid() handler. 314SyscallReturn getegidFunc(SyscallDesc *desc, int num, 315 LiveProcess *p, ThreadContext *tc); 316 317/// Target clone() handler. 318SyscallReturn cloneFunc(SyscallDesc *desc, int num, 319 LiveProcess *p, ThreadContext *tc); 320 321 322/// Pseudo Funcs - These functions use a different return convension, 323/// returning a second value in a register other than the normal return register 324SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 325 LiveProcess *process, ThreadContext *tc); 326 327/// Target getpidPseudo() handler. 328SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 329 LiveProcess *p, ThreadContext *tc); 330 331/// Target getuidPseudo() handler. 332SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 333 LiveProcess *p, ThreadContext *tc); 334 335/// Target getgidPseudo() handler. 336SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 337 LiveProcess *p, ThreadContext *tc); 338 339 340/// A readable name for 1,000,000, for converting microseconds to seconds. 341const int one_million = 1000000; 342 343/// Approximate seconds since the epoch (1/1/1970). About a billion, 344/// by my reckoning. We want to keep this a constant (not use the 345/// real-world time) to keep simulations repeatable. 346const unsigned seconds_since_epoch = 1000000000; 347 348/// Helper function to convert current elapsed time to seconds and 349/// microseconds. 350template <class T1, class T2> 351void 352getElapsedTime(T1 &sec, T2 &usec) 353{ 354 int elapsed_usecs = curTick / Clock::Int::us; 355 sec = elapsed_usecs / one_million; 356 usec = elapsed_usecs % one_million; 357} 358 359////////////////////////////////////////////////////////////////////// 360// 361// The following emulation functions are generic, but need to be 362// templated to account for differences in types, constants, etc. 363// 364////////////////////////////////////////////////////////////////////// 365 366#if NO_STAT64 367 typedef struct stat hst_stat; 368 typedef struct stat hst_stat64; 369#else 370 typedef struct stat hst_stat; 371 typedef struct stat64 hst_stat64; 372#endif 373 374//// Helper function to convert a host stat buffer to a target stat 375//// buffer. Also copies the target buffer out to the simulated 376//// memory space. Used by stat(), fstat(), and lstat(). 377 378template <typename target_stat, typename host_stat> 379static void 380convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 381{ 382 using namespace TheISA; 383 384 if (fakeTTY) 385 tgt->st_dev = 0xA; 386 else 387 tgt->st_dev = host->st_dev; 388 tgt->st_dev = htog(tgt->st_dev); 389 tgt->st_ino = host->st_ino; 390 tgt->st_ino = htog(tgt->st_ino); 391 tgt->st_mode = host->st_mode; 392 if (fakeTTY) { 393 // Claim to be a character device 394 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 395 tgt->st_mode |= S_IFCHR; // Set S_IFCHR 396 } 397 tgt->st_mode = htog(tgt->st_mode); 398 tgt->st_nlink = host->st_nlink; 399 tgt->st_nlink = htog(tgt->st_nlink); 400 tgt->st_uid = host->st_uid; 401 tgt->st_uid = htog(tgt->st_uid); 402 tgt->st_gid = host->st_gid; 403 tgt->st_gid = htog(tgt->st_gid); 404 if (fakeTTY) 405 tgt->st_rdev = 0x880d; 406 else 407 tgt->st_rdev = host->st_rdev; 408 tgt->st_rdev = htog(tgt->st_rdev); 409 tgt->st_size = host->st_size; 410 tgt->st_size = htog(tgt->st_size); 411 tgt->st_atimeX = host->st_atime; 412 tgt->st_atimeX = htog(tgt->st_atimeX); 413 tgt->st_mtimeX = host->st_mtime; 414 tgt->st_mtimeX = htog(tgt->st_mtimeX); 415 tgt->st_ctimeX = host->st_ctime; 416 tgt->st_ctimeX = htog(tgt->st_ctimeX); 417 // Force the block size to be 8k. This helps to ensure buffered io works 418 // consistently across different hosts. 419 tgt->st_blksize = 0x2000; 420 tgt->st_blksize = htog(tgt->st_blksize); 421 tgt->st_blocks = host->st_blocks; 422 tgt->st_blocks = htog(tgt->st_blocks); 423} 424 425// Same for stat64 426 427template <typename target_stat, typename host_stat64> 428static void 429convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 430{ 431 using namespace TheISA; 432 433 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 434#if defined(STAT_HAVE_NSEC) 435 tgt->st_atime_nsec = host->st_atime_nsec; 436 tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 437 tgt->st_mtime_nsec = host->st_mtime_nsec; 438 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 439 tgt->st_ctime_nsec = host->st_ctime_nsec; 440 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 441#else 442 tgt->st_atime_nsec = 0; 443 tgt->st_mtime_nsec = 0; 444 tgt->st_ctime_nsec = 0; 445#endif 446} 447 448//Here are a couple convenience functions 449template<class OS> 450static void 451copyOutStatBuf(TranslatingPort * mem, Addr addr, 452 hst_stat *host, bool fakeTTY = false) 453{ 454 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 455 tgt_stat_buf tgt(addr); 456 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 457 tgt.copyOut(mem); 458} 459 460template<class OS> 461static void 462copyOutStat64Buf(TranslatingPort * mem, Addr addr, 463 hst_stat64 *host, bool fakeTTY = false) 464{ 465 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 466 tgt_stat_buf tgt(addr); 467 convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 468 tgt.copyOut(mem); 469} 470 471/// Target ioctl() handler. For the most part, programs call ioctl() 472/// only to find out if their stdout is a tty, to determine whether to 473/// do line or block buffering. 474template <class OS> 475SyscallReturn 476ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 477 ThreadContext *tc) 478{ 479 int fd = process->getSyscallArg(tc, 0); 480 unsigned req = process->getSyscallArg(tc, 1); 481 482 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 483 484 if (fd < 0 || process->sim_fd(fd) < 0) { 485 // doesn't map to any simulator fd: not a valid target fd 486 return -EBADF; 487 } 488 489 switch (req) { 490 case OS::TIOCISATTY_: 491 case OS::TIOCGETP_: 492 case OS::TIOCSETP_: 493 case OS::TIOCSETN_: 494 case OS::TIOCSETC_: 495 case OS::TIOCGETC_: 496 case OS::TIOCGETS_: 497 case OS::TIOCGETA_: 498 return -ENOTTY; 499 500 default: 501 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 502 fd, req, tc->readPC()); 503 } 504} 505 506/// Target open() handler. 507template <class OS> 508SyscallReturn 509openFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 510 ThreadContext *tc) 511{ 512 std::string path; 513 514 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 515 return -EFAULT; 516 517 if (path == "/dev/sysdev0") { 518 // This is a memory-mapped high-resolution timer device on Alpha. 519 // We don't support it, so just punt. 520 warn("Ignoring open(%s, ...)\n", path); 521 return -ENOENT; 522 } 523 524 int tgtFlags = process->getSyscallArg(tc, 1); 525 int mode = process->getSyscallArg(tc, 2); 526 int hostFlags = 0; 527 528 // translate open flags 529 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 530 if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 531 tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 532 hostFlags |= OS::openFlagTable[i].hostFlag; 533 } 534 } 535 536 // any target flags left? 537 if (tgtFlags != 0) 538 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 539 540#ifdef __CYGWIN32__ 541 hostFlags |= O_BINARY; 542#endif 543 544 // Adjust path for current working directory 545 path = process->fullPath(path); 546 547 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 548 549 int fd; 550 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 551 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 552 // It's a proc/sys entery and requires special handling 553 fd = OS::openSpecialFile(path, process, tc); 554 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 555 } else { 556 // open the file 557 fd = open(path.c_str(), hostFlags, mode); 558 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 559 } 560 561} 562 563/// Target sysinfo() handler. 564template <class OS> 565SyscallReturn 566sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 567 ThreadContext *tc) 568{ 569 570 TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0)); 571 572 sysinfo->uptime=seconds_since_epoch; 573 sysinfo->totalram=process->system->memSize(); 574 575 sysinfo.copyOut(tc->getMemPort()); 576 577 return 0; 578} 579 580/// Target chmod() handler. 581template <class OS> 582SyscallReturn 583chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 584 ThreadContext *tc) 585{ 586 std::string path; 587 588 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 589 return -EFAULT; 590 591 uint32_t mode = process->getSyscallArg(tc, 1); 592 mode_t hostMode = 0; 593 594 // XXX translate mode flags via OS::something??? 595 hostMode = mode; 596 597 // Adjust path for current working directory 598 path = process->fullPath(path); 599 600 // do the chmod 601 int result = chmod(path.c_str(), hostMode); 602 if (result < 0) 603 return -errno; 604 605 return 0; 606} 607 608 609/// Target fchmod() handler. 610template <class OS> 611SyscallReturn 612fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 613 ThreadContext *tc) 614{ 615 int fd = process->getSyscallArg(tc, 0); 616 if (fd < 0 || process->sim_fd(fd) < 0) { 617 // doesn't map to any simulator fd: not a valid target fd 618 return -EBADF; 619 } 620 621 uint32_t mode = process->getSyscallArg(tc, 1); 622 mode_t hostMode = 0; 623 624 // XXX translate mode flags via OS::someting??? 625 hostMode = mode; 626 627 // do the fchmod 628 int result = fchmod(process->sim_fd(fd), hostMode); 629 if (result < 0) 630 return -errno; 631 632 return 0; 633} 634 635/// Target mremap() handler. 636template <class OS> 637SyscallReturn 638mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 639{ 640 Addr start = process->getSyscallArg(tc, 0); 641 uint64_t old_length = process->getSyscallArg(tc, 1); 642 uint64_t new_length = process->getSyscallArg(tc, 2); 643 uint64_t flags = process->getSyscallArg(tc, 3); 644 645 if ((start % TheISA::VMPageSize != 0) || 646 (new_length % TheISA::VMPageSize != 0)) { 647 warn("mremap failing: arguments not page aligned"); 648 return -EINVAL; 649 } 650 651 if (new_length > old_length) { 652 if ((start + old_length) == process->mmap_end) { 653 uint64_t diff = new_length - old_length; 654 process->pTable->allocate(process->mmap_end, diff); 655 process->mmap_end += diff; 656 return start; 657 } else { 658 // sys/mman.h defined MREMAP_MAYMOVE 659 if (!(flags & 1)) { 660 warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 661 return -ENOMEM; 662 } else { 663 process->pTable->remap(start, old_length, process->mmap_end); 664 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 665 process->mmap_end, process->mmap_end + new_length, new_length); 666 start = process->mmap_end; 667 // add on the remaining unallocated pages 668 process->pTable->allocate(start + old_length, new_length - old_length); 669 process->mmap_end += new_length; 670 warn("returning %08p as start\n", start); 671 return start; 672 } 673 } 674 } else { 675 process->pTable->deallocate(start + new_length, old_length - 676 new_length); 677 return start; 678 } 679} 680 681/// Target stat() handler. 682template <class OS> 683SyscallReturn 684statFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 685 ThreadContext *tc) 686{ 687 std::string path; 688 689 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 690 return -EFAULT; 691 692 // Adjust path for current working directory 693 path = process->fullPath(path); 694 695 struct stat hostBuf; 696 int result = stat(path.c_str(), &hostBuf); 697 698 if (result < 0) 699 return -errno; 700 701 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 702 &hostBuf); 703 704 return 0; 705} 706 707 708/// Target stat64() handler. 709template <class OS> 710SyscallReturn 711stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 712 ThreadContext *tc) 713{ 714 std::string path; 715 716 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 717 return -EFAULT; 718 719 // Adjust path for current working directory 720 path = process->fullPath(path); 721 722#if NO_STAT64 723 struct stat hostBuf; 724 int result = stat(path.c_str(), &hostBuf); 725#else 726 struct stat64 hostBuf; 727 int result = stat64(path.c_str(), &hostBuf); 728#endif 729 730 if (result < 0) 731 return -errno; 732 733 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 734 &hostBuf); 735 736 return 0; 737} 738 739 740/// Target fstat64() handler. 741template <class OS> 742SyscallReturn 743fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 744 ThreadContext *tc) 745{ 746 int fd = process->getSyscallArg(tc, 0); 747 if (fd < 0 || process->sim_fd(fd) < 0) { 748 // doesn't map to any simulator fd: not a valid target fd 749 return -EBADF; 750 } 751 752#if NO_STAT64 753 struct stat hostBuf; 754 int result = fstat(process->sim_fd(fd), &hostBuf); 755#else 756 struct stat64 hostBuf; 757 int result = fstat64(process->sim_fd(fd), &hostBuf); 758#endif 759 760 if (result < 0) 761 return -errno; 762 763 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 764 &hostBuf, (fd == 1)); 765 766 return 0; 767} 768 769 770/// Target lstat() handler. 771template <class OS> 772SyscallReturn 773lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 774 ThreadContext *tc) 775{ 776 std::string path; 777 778 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 779 return -EFAULT; 780 781 // Adjust path for current working directory 782 path = process->fullPath(path); 783 784 struct stat hostBuf; 785 int result = lstat(path.c_str(), &hostBuf); 786 787 if (result < 0) 788 return -errno; 789 790 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 791 &hostBuf); 792 793 return 0; 794} 795 796/// Target lstat64() handler. 797template <class OS> 798SyscallReturn 799lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 800 ThreadContext *tc) 801{ 802 std::string path; 803 804 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 805 return -EFAULT; 806 807 // Adjust path for current working directory 808 path = process->fullPath(path); 809 810#if NO_STAT64 811 struct stat hostBuf; 812 int result = lstat(path.c_str(), &hostBuf); 813#else 814 struct stat64 hostBuf; 815 int result = lstat64(path.c_str(), &hostBuf); 816#endif 817 818 if (result < 0) 819 return -errno; 820 821 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 822 &hostBuf); 823 824 return 0; 825} 826 827/// Target fstat() handler. 828template <class OS> 829SyscallReturn 830fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 831 ThreadContext *tc) 832{ 833 int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 834 835 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 836 837 if (fd < 0) 838 return -EBADF; 839 840 struct stat hostBuf; 841 int result = fstat(fd, &hostBuf); 842 843 if (result < 0) 844 return -errno; 845 846 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 847 &hostBuf, (fd == 1)); 848 849 return 0; 850} 851 852 853/// Target statfs() handler. 854template <class OS> 855SyscallReturn 856statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 857 ThreadContext *tc) 858{ 859 std::string path; 860 861 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 862 return -EFAULT; 863 864 // Adjust path for current working directory 865 path = process->fullPath(path); 866 867 struct statfs hostBuf; 868 int result = statfs(path.c_str(), &hostBuf); 869 870 if (result < 0) 871 return -errno; 872 873 OS::copyOutStatfsBuf(tc->getMemPort(), 874 (Addr)(process->getSyscallArg(tc, 1)), &hostBuf); 875 876 return 0; 877} 878 879 880/// Target fstatfs() handler. 881template <class OS> 882SyscallReturn 883fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 884 ThreadContext *tc) 885{ 886 int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 887 888 if (fd < 0) 889 return -EBADF; 890 891 struct statfs hostBuf; 892 int result = fstatfs(fd, &hostBuf); 893 894 if (result < 0) 895 return -errno; 896 897 OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1), 898 &hostBuf); 899 900 return 0; 901} 902 903 904/// Target writev() handler. 905template <class OS> 906SyscallReturn 907writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 908 ThreadContext *tc) 909{ 910 int fd = process->getSyscallArg(tc, 0); 911 if (fd < 0 || process->sim_fd(fd) < 0) { 912 // doesn't map to any simulator fd: not a valid target fd 913 return -EBADF; 914 } 915 916 TranslatingPort *p = tc->getMemPort(); 917 uint64_t tiov_base = process->getSyscallArg(tc, 1); 918 size_t count = process->getSyscallArg(tc, 2); 919 struct iovec hiov[count]; 920 for (size_t i = 0; i < count; ++i) { 921 typename OS::tgt_iovec tiov; 922 923 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 924 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 925 hiov[i].iov_len = gtoh(tiov.iov_len); 926 hiov[i].iov_base = new char [hiov[i].iov_len]; 927 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 928 hiov[i].iov_len); 929 } 930 931 int result = writev(process->sim_fd(fd), hiov, count); 932 933 for (size_t i = 0; i < count; ++i) 934 delete [] (char *)hiov[i].iov_base; 935 936 if (result < 0) 937 return -errno; 938 939 return 0; 940} 941 942 943/// Target mmap() handler. 944/// 945/// We don't really handle mmap(). If the target is mmaping an 946/// anonymous region or /dev/zero, we can get away with doing basically 947/// nothing (since memory is initialized to zero and the simulator 948/// doesn't really check addresses anyway). Always print a warning, 949/// since this could be seriously broken if we're not mapping 950/// /dev/zero. 951// 952/// Someday we should explicitly check for /dev/zero in open, flag the 953/// file descriptor, and fail (or implement!) a non-anonymous mmap to 954/// anything else. 955template <class OS> 956SyscallReturn 957mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 958{ 959 Addr start = p->getSyscallArg(tc, 0); 960 uint64_t length = p->getSyscallArg(tc, 1); 961 // int prot = p->getSyscallArg(tc, 2); 962 int flags = p->getSyscallArg(tc, 3); 963 // int fd = p->sim_fd(p->getSyscallArg(tc, 4)); 964 // int offset = p->getSyscallArg(tc, 5); 965 966 967 if ((start % TheISA::VMPageSize) != 0 || 968 (length % TheISA::VMPageSize) != 0) { 969 warn("mmap failing: arguments not page-aligned: " 970 "start 0x%x length 0x%x", 971 start, length); 972 return -EINVAL; 973 } 974 975 if (start != 0) { 976 warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 977 start, p->mmap_end); 978 } 979 980 // pick next address from our "mmap region" 981 if (OS::mmapGrowsDown()) { 982 start = p->mmap_end - length; 983 p->mmap_end = start; 984 } else { 985 start = p->mmap_end; 986 p->mmap_end += length; 987 } 988 p->pTable->allocate(start, length); 989 990 if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 991 warn("allowing mmap of file @ fd %d. " 992 "This will break if not /dev/zero.", p->getSyscallArg(tc, 4)); 993 } 994 995 return start; 996} 997 998/// Target getrlimit() handler. 999template <class OS> 1000SyscallReturn 1001getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1002 ThreadContext *tc) 1003{ 1004 unsigned resource = process->getSyscallArg(tc, 0); 1005 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1)); 1006 1007 switch (resource) { 1008 case OS::TGT_RLIMIT_STACK: 1009 // max stack size in bytes: make up a number (8MB for now) 1010 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1011 rlp->rlim_cur = htog(rlp->rlim_cur); 1012 rlp->rlim_max = htog(rlp->rlim_max); 1013 break; 1014 1015 case OS::TGT_RLIMIT_DATA: 1016 // max data segment size in bytes: make up a number 1017 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1018 rlp->rlim_cur = htog(rlp->rlim_cur); 1019 rlp->rlim_max = htog(rlp->rlim_max); 1020 break; 1021 1022 default: 1023 std::cerr << "getrlimitFunc: unimplemented resource " << resource 1024 << std::endl; 1025 abort(); 1026 break; 1027 } 1028 1029 rlp.copyOut(tc->getMemPort()); 1030 return 0; 1031} 1032 1033/// Target gettimeofday() handler. 1034template <class OS> 1035SyscallReturn 1036gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1037 ThreadContext *tc) 1038{ 1039 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0)); 1040 1041 getElapsedTime(tp->tv_sec, tp->tv_usec); 1042 tp->tv_sec += seconds_since_epoch; 1043 tp->tv_sec = TheISA::htog(tp->tv_sec); 1044 tp->tv_usec = TheISA::htog(tp->tv_usec); 1045 1046 tp.copyOut(tc->getMemPort()); 1047 1048 return 0; 1049} 1050 1051 1052/// Target utimes() handler. 1053template <class OS> 1054SyscallReturn 1055utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1056 ThreadContext *tc) 1057{ 1058 std::string path; 1059 1060 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 1061 return -EFAULT; 1062 1063 TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1)); 1064 tp.copyIn(tc->getMemPort()); 1065 1066 struct timeval hostTimeval[2]; 1067 for (int i = 0; i < 2; ++i) 1068 { 1069 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 1070 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 1071 } 1072 1073 // Adjust path for current working directory 1074 path = process->fullPath(path); 1075 1076 int result = utimes(path.c_str(), hostTimeval); 1077 1078 if (result < 0) 1079 return -errno; 1080 1081 return 0; 1082} 1083/// Target getrusage() function. 1084template <class OS> 1085SyscallReturn 1086getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1087 ThreadContext *tc) 1088{ 1089 int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN 1090 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1)); 1091 1092 rup->ru_utime.tv_sec = 0; 1093 rup->ru_utime.tv_usec = 0; 1094 rup->ru_stime.tv_sec = 0; 1095 rup->ru_stime.tv_usec = 0; 1096 rup->ru_maxrss = 0; 1097 rup->ru_ixrss = 0; 1098 rup->ru_idrss = 0; 1099 rup->ru_isrss = 0; 1100 rup->ru_minflt = 0; 1101 rup->ru_majflt = 0; 1102 rup->ru_nswap = 0; 1103 rup->ru_inblock = 0; 1104 rup->ru_oublock = 0; 1105 rup->ru_msgsnd = 0; 1106 rup->ru_msgrcv = 0; 1107 rup->ru_nsignals = 0; 1108 rup->ru_nvcsw = 0; 1109 rup->ru_nivcsw = 0; 1110 1111 switch (who) { 1112 case OS::TGT_RUSAGE_SELF: 1113 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1114 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 1115 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 1116 break; 1117 1118 case OS::TGT_RUSAGE_CHILDREN: 1119 // do nothing. We have no child processes, so they take no time. 1120 break; 1121 1122 default: 1123 // don't really handle THREAD or CHILDREN, but just warn and 1124 // plow ahead 1125 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1126 who); 1127 } 1128 1129 rup.copyOut(tc->getMemPort()); 1130 1131 return 0; 1132} 1133 1134/// Target times() function. 1135template <class OS> 1136SyscallReturn 1137timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1138 ThreadContext *tc) 1139{ 1140 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, 0)); 1141 1142 // Fill in the time structure (in clocks) 1143 int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s; 1144 bufp->tms_utime = clocks; 1145 bufp->tms_stime = 0; 1146 bufp->tms_cutime = 0; 1147 bufp->tms_cstime = 0; 1148 1149 // Convert to host endianness 1150 bufp->tms_utime = htog(bufp->tms_utime); 1151 1152 // Write back 1153 bufp.copyOut(tc->getMemPort()); 1154 1155 // Return clock ticks since system boot 1156 return clocks; 1157} 1158 1159 1160 1161#endif // __SIM_SYSCALL_EMUL_HH__ 1162