syscall_emul.hh revision 511
1/* 2 * Copyright (c) 2003 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 29#ifndef __SYSCALL_EMUL_HH__ 30#define __SYSCALL_EMUL_HH__ 31 32/// 33/// @file syscall_emul.hh 34/// 35/// This file defines objects used to emulate syscalls from the target 36/// application on the host machine. 37 38#include <string> 39 40#include "base/intmath.hh" // for RoundUp 41#include "targetarch/isa_traits.hh" // for Addr 42#include "mem/functional_mem/functional_memory.hh" 43 44class Process; 45class ExecContext; 46 47/// 48/// System call descriptor. 49/// 50class SyscallDesc { 51 52 public: 53 54 /// Typedef for target syscall handler functions. 55 typedef int (*FuncPtr)(SyscallDesc *, int num, 56 Process *, ExecContext *); 57 58 const char *name; //!< Syscall name (e.g., "open"). 59 FuncPtr funcPtr; //!< Pointer to emulation function. 60 int flags; //!< Flags (see Flags enum). 61 62 /// Flag values for controlling syscall behavior. 63 enum Flags { 64 /// Don't set return regs according to funcPtr return value. 65 /// Used for syscalls with non-standard return conventions 66 /// that explicitly set the ExecContext regs (e.g., 67 /// sigreturn). 68 SuppressReturnValue = 1 69 }; 70 71 /// Constructor. 72 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 73 : name(_name), funcPtr(_funcPtr), flags(_flags) 74 { 75 } 76 77 /// Emulate the syscall. Public interface for calling through funcPtr. 78 void doSyscall(int callnum, Process *proc, ExecContext *xc); 79}; 80 81 82class BaseBufferArg { 83 84 public: 85 86 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 87 { 88 bufPtr = new uint8_t[size]; 89 // clear out buffer: in case we only partially populate this, 90 // and then do a copyOut(), we want to make sure we don't 91 // introduce any random junk into the simulated address space 92 memset(bufPtr, 0, size); 93 } 94 95 virtual ~BaseBufferArg() { delete [] bufPtr; } 96 97 // 98 // copy data into simulator space (read from target memory) 99 // 100 virtual bool copyIn(FunctionalMemory *mem) 101 { 102 mem->access(Read, addr, bufPtr, size); 103 return true; // no EFAULT detection for now 104 } 105 106 // 107 // copy data out of simulator space (write to target memory) 108 // 109 virtual bool copyOut(FunctionalMemory *mem) 110 { 111 mem->access(Write, addr, bufPtr, size); 112 return true; // no EFAULT detection for now 113 } 114 115 protected: 116 Addr addr; 117 int size; 118 uint8_t *bufPtr; 119}; 120 121 122class BufferArg : public BaseBufferArg 123{ 124 public: 125 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 126 void *bufferPtr() { return bufPtr; } 127}; 128 129template <class T> 130class TypedBufferArg : public BaseBufferArg 131{ 132 public: 133 // user can optionally specify a specific number of bytes to 134 // allocate to deal with those structs that have variable-size 135 // arrays at the end 136 TypedBufferArg(Addr _addr, int _size = sizeof(T)) 137 : BaseBufferArg(_addr, _size) 138 { } 139 140 // type case 141 operator T*() { return (T *)bufPtr; } 142 143 // dereference operators 144 T &operator*() { return *((T *)bufPtr); } 145 T* operator->() { return (T *)bufPtr; } 146 T &operator[](int i) { return ((T *)bufPtr)[i]; } 147}; 148 149////////////////////////////////////////////////////////////////////// 150// 151// The following emulation functions are generic enough that they 152// don't need to be recompiled for different emulated OS's. They are 153// defined in sim/syscall_emul.cc. 154// 155////////////////////////////////////////////////////////////////////// 156 157 158/// Handler for unimplemented syscalls that we haven't thought about. 159int unimplementedFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 160 161/// Handler for unimplemented syscalls that we never intend to 162/// implement (signal handling, etc.) and should not affect the correct 163/// behavior of the program. Print a warning only if the appropriate 164/// trace flag is enabled. Return success to the target program. 165int ignoreFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 166 167/// Target exit() handler: terminate simulation. 168int exitFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 169 170/// Target getpagesize() handler. 171int getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 172 173/// Target obreak() handler: set brk address. 174int obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 175 176/// Target close() handler. 177int closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 178 179/// Target read() handler. 180int readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 181 182/// Target write() handler. 183int writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 184 185/// Target lseek() handler. 186int lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 187 188/// Target munmap() handler. 189int munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 190 191/// Target gethostname() handler. 192int gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 193 194/// Target unlink() handler. 195int unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 196 197/// Target rename() handler. 198int renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc); 199 200////////////////////////////////////////////////////////////////////// 201// 202// The following emulation functions are generic, but need to be 203// templated to account for differences in types, constants, etc. 204// 205////////////////////////////////////////////////////////////////////// 206 207/// Target ioctl() handler. For the most part, programs call ioctl() 208/// only to find out if their stdout is a tty, to determine whether to 209/// do line or block buffering. 210template <class OS> 211int 212ioctlFunc(SyscallDesc *desc, int callnum, Process *process, 213 ExecContext *xc) 214{ 215 int fd = xc->getSyscallArg(0); 216 unsigned req = xc->getSyscallArg(1); 217 218 // DPRINTFR(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 219 220 if (fd < 0 || process->sim_fd(fd) < 0) { 221 // doesn't map to any simulator fd: not a valid target fd 222 return -EBADF; 223 } 224 225 switch (req) { 226 case OS::TIOCISATTY: 227 case OS::TIOCGETP: 228 case OS::TIOCSETP: 229 case OS::TIOCSETN: 230 case OS::TIOCSETC: 231 case OS::TIOCGETC: 232 case OS::TIOCGETS: 233 case OS::TIOCGETA: 234 return -ENOTTY; 235 236 default: 237 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", fd, req, xc->readPC()); 238 } 239} 240 241/// This struct is used to build an target-OS-dependent table that 242/// maps the target's open() flags to the host open() flags. 243struct OpenFlagTransTable { 244 int tgtFlag; //!< Target system flag value. 245 int hostFlag; //!< Corresponding host system flag value. 246}; 247 248 249/// Target open() handler. 250template <class OS> 251int 252openFunc(SyscallDesc *desc, int callnum, Process *process, 253 ExecContext *xc) 254{ 255 std::string path; 256 257 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 258 return -EFAULT; 259 260 if (path == "/dev/sysdev0") { 261 // This is a memory-mapped high-resolution timer device on Alpha. 262 // We don't support it, so just punt. 263 DCOUT(SyscallWarnings) << "Ignoring open(" << path << ", ...)" << endl; 264 return -ENOENT; 265 } 266 267 int tgtFlags = xc->getSyscallArg(1); 268 int mode = xc->getSyscallArg(2); 269 int hostFlags = 0; 270 271 // translate open flags 272 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 273 if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 274 tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 275 hostFlags |= OS::openFlagTable[i].hostFlag; 276 } 277 } 278 279 // any target flags left? 280 if (tgtFlags != 0) 281 cerr << "Syscall: open: cannot decode flags: " << tgtFlags << endl; 282 283#ifdef __CYGWIN32__ 284 hostFlags |= O_BINARY; 285#endif 286 287 // open the file 288 int fd = open(path.c_str(), hostFlags, mode); 289 290 return (fd == -1) ? -errno : process->open_fd(fd); 291} 292 293 294/// Target stat() handler. 295template <class OS> 296int 297statFunc(SyscallDesc *desc, int callnum, Process *process, 298 ExecContext *xc) 299{ 300 std::string path; 301 302 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 303 return -EFAULT; 304 305 struct stat hostBuf; 306 int result = stat(path.c_str(), &hostBuf); 307 308 if (result < 0) 309 return -errno; 310 311 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 312 313 return 0; 314} 315 316 317/// Target lstat() handler. 318template <class OS> 319int 320lstatFunc(SyscallDesc *desc, int callnum, Process *process, 321 ExecContext *xc) 322{ 323 std::string path; 324 325 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 326 return -EFAULT; 327 328 struct stat hostBuf; 329 int result = lstat(path.c_str(), &hostBuf); 330 331 if (result < 0) 332 return -errno; 333 334 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 335 336 return 0; 337} 338 339/// Target fstat() handler. 340template <class OS> 341int 342fstatFunc(SyscallDesc *desc, int callnum, Process *process, 343 ExecContext *xc) 344{ 345 int fd = process->sim_fd(xc->getSyscallArg(0)); 346 347 // DPRINTFR(SyscallVerbose, "fstat(%d, ...)\n", fd); 348 349 if (fd < 0) 350 return -EBADF; 351 352 struct stat hostBuf; 353 int result = fstat(fd, &hostBuf); 354 355 if (result < 0) 356 return -errno; 357 358 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 359 360 return 0; 361} 362 363 364/// Target mmap() handler. 365/// 366/// We don't really handle mmap(). If the target is mmaping an 367/// anonymous region or /dev/zero, we can get away with doing basically 368/// nothing (since memory is initialized to zero and the simulator 369/// doesn't really check addresses anyway). Always print a warning, 370/// since this could be seriously broken if we're not mapping 371/// /dev/zero. 372// 373/// Someday we should explicitly check for /dev/zero in open, flag the 374/// file descriptor, and fail (or implement!) a non-anonymous mmap to 375/// anything else. 376template <class OS> 377int 378mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc) 379{ 380 Addr start = xc->getSyscallArg(0); 381 uint64_t length = xc->getSyscallArg(1); 382 // int prot = xc->getSyscallArg(2); 383 int flags = xc->getSyscallArg(3); 384 // int fd = p->sim_fd(xc->getSyscallArg(4)); 385 // int offset = xc->getSyscallArg(5); 386 387 if (start == 0) { 388 // user didn't give an address... pick one from our "mmap region" 389 start = p->mmap_base; 390 p->mmap_base += RoundUp<Addr>(length, VMPageSize); 391 } 392 393 if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 394 DPRINTF(SyscallWarnings, "Warning: allowing mmap of file @ fd %d. " 395 "This will break if not /dev/zero.", xc->getSyscallArg(4)); 396 } 397 398 return start; 399} 400 401/// Target getrlimit() handler. 402template <class OS> 403int 404getrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 405 ExecContext *xc) 406{ 407 unsigned resource = xc->getSyscallArg(0); 408 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1)); 409 410 switch (resource) { 411 case OS::RLIMIT_STACK: 412 // max stack size in bytes: make up a number (2MB for now) 413 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 414 break; 415 416 default: 417 cerr << "getrlimitFunc: unimplemented resource " << resource << endl; 418 abort(); 419 break; 420 } 421 422 rlp.copyOut(xc->mem); 423 return 0; 424} 425 426/// A readable name for 1,000,000, for converting microseconds to seconds. 427const int one_million = 1000000; 428 429/// Approximate seconds since the epoch (1/1/1970). About a billion, 430/// by my reckoning. We want to keep this a constant (not use the 431/// real-world time) to keep simulations repeatable. 432const unsigned seconds_since_epoch = 1000000000; 433 434/// Helper function to convert current elapsed time to seconds and 435/// microseconds. 436template <class T1, class T2> 437void 438getElapsedTime(T1 &sec, T2 &usec) 439{ 440 int cycles_per_usec = ticksPerSecond / one_million; 441 442 int elapsed_usecs = curTick / cycles_per_usec; 443 sec = elapsed_usecs / one_million; 444 usec = elapsed_usecs % one_million; 445} 446 447 448/// Target gettimeofday() handler. 449template <class OS> 450int 451gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 452 ExecContext *xc) 453{ 454 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0)); 455 456 getElapsedTime(tp->tv_sec, tp->tv_usec); 457 tp->tv_sec += seconds_since_epoch; 458 459 tp.copyOut(xc->mem); 460 461 return 0; 462} 463 464 465/// Target getrusage() function. 466template <class OS> 467int 468getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 469 ExecContext *xc) 470{ 471 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN 472 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1)); 473 474 if (who != OS::RUSAGE_SELF) { 475 // don't really handle THREAD or CHILDREN, but just warn and 476 // plow ahead 477 DCOUT(SyscallWarnings) 478 << "Warning: getrusage() only supports RUSAGE_SELF." 479 << " Parameter " << who << " ignored." << endl; 480 } 481 482 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 483 rup->ru_stime.tv_sec = 0; 484 rup->ru_stime.tv_usec = 0; 485 rup->ru_maxrss = 0; 486 rup->ru_ixrss = 0; 487 rup->ru_idrss = 0; 488 rup->ru_isrss = 0; 489 rup->ru_minflt = 0; 490 rup->ru_majflt = 0; 491 rup->ru_nswap = 0; 492 rup->ru_inblock = 0; 493 rup->ru_oublock = 0; 494 rup->ru_msgsnd = 0; 495 rup->ru_msgrcv = 0; 496 rup->ru_nsignals = 0; 497 rup->ru_nvcsw = 0; 498 rup->ru_nivcsw = 0; 499 500 rup.copyOut(xc->mem); 501 502 return 0; 503} 504 505 506 507#endif // __SYSCALL_EMUL_HH__ 508