syscall_emul.hh revision 9143
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#ifdef __CYGWIN32__
45#include <sys/fcntl.h>  // for O_BINARY
46#endif
47#include <sys/stat.h>
48#include <sys/time.h>
49#include <sys/uio.h>
50#include <fcntl.h>
51
52#include <cerrno>
53#include <string>
54
55#include "base/chunk_generator.hh"
56#include "base/intmath.hh"      // for RoundUp
57#include "base/misc.hh"
58#include "base/trace.hh"
59#include "base/types.hh"
60#include "config/the_isa.hh"
61#include "cpu/base.hh"
62#include "cpu/thread_context.hh"
63#include "debug/SyscallVerbose.hh"
64#include "mem/page_table.hh"
65#include "mem/se_translating_port_proxy.hh"
66#include "sim/byteswap.hh"
67#include "sim/process.hh"
68#include "sim/syscallreturn.hh"
69#include "sim/system.hh"
70
71///
72/// System call descriptor.
73///
74class SyscallDesc {
75
76  public:
77
78    /// Typedef for target syscall handler functions.
79    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
80                           LiveProcess *, ThreadContext *);
81
82    const char *name;   //!< Syscall name (e.g., "open").
83    FuncPtr funcPtr;    //!< Pointer to emulation function.
84    int flags;          //!< Flags (see Flags enum).
85
86    /// Flag values for controlling syscall behavior.
87    enum Flags {
88        /// Don't set return regs according to funcPtr return value.
89        /// Used for syscalls with non-standard return conventions
90        /// that explicitly set the ThreadContext regs (e.g.,
91        /// sigreturn).
92        SuppressReturnValue = 1
93    };
94
95    /// Constructor.
96    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
97        : name(_name), funcPtr(_funcPtr), flags(_flags)
98    {
99    }
100
101    /// Emulate the syscall.  Public interface for calling through funcPtr.
102    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
103};
104
105
106class BaseBufferArg {
107
108  public:
109
110    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
111    {
112        bufPtr = new uint8_t[size];
113        // clear out buffer: in case we only partially populate this,
114        // and then do a copyOut(), we want to make sure we don't
115        // introduce any random junk into the simulated address space
116        memset(bufPtr, 0, size);
117    }
118
119    virtual ~BaseBufferArg() { delete [] bufPtr; }
120
121    //
122    // copy data into simulator space (read from target memory)
123    //
124    virtual bool copyIn(SETranslatingPortProxy &memproxy)
125    {
126        memproxy.readBlob(addr, bufPtr, size);
127        return true;    // no EFAULT detection for now
128    }
129
130    //
131    // copy data out of simulator space (write to target memory)
132    //
133    virtual bool copyOut(SETranslatingPortProxy &memproxy)
134    {
135        memproxy.writeBlob(addr, bufPtr, size);
136        return true;    // no EFAULT detection for now
137    }
138
139  protected:
140    Addr addr;
141    int size;
142    uint8_t *bufPtr;
143};
144
145
146class BufferArg : public BaseBufferArg
147{
148  public:
149    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
150    void *bufferPtr()   { return bufPtr; }
151};
152
153template <class T>
154class TypedBufferArg : public BaseBufferArg
155{
156  public:
157    // user can optionally specify a specific number of bytes to
158    // allocate to deal with those structs that have variable-size
159    // arrays at the end
160    TypedBufferArg(Addr _addr, int _size = sizeof(T))
161        : BaseBufferArg(_addr, _size)
162    { }
163
164    // type case
165    operator T*() { return (T *)bufPtr; }
166
167    // dereference operators
168    T &operator*()       { return *((T *)bufPtr); }
169    T* operator->()      { return (T *)bufPtr; }
170    T &operator[](int i) { return ((T *)bufPtr)[i]; }
171};
172
173//////////////////////////////////////////////////////////////////////
174//
175// The following emulation functions are generic enough that they
176// don't need to be recompiled for different emulated OS's.  They are
177// defined in sim/syscall_emul.cc.
178//
179//////////////////////////////////////////////////////////////////////
180
181
182/// Handler for unimplemented syscalls that we haven't thought about.
183SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
184                                LiveProcess *p, ThreadContext *tc);
185
186/// Handler for unimplemented syscalls that we never intend to
187/// implement (signal handling, etc.) and should not affect the correct
188/// behavior of the program.  Print a warning only if the appropriate
189/// trace flag is enabled.  Return success to the target program.
190SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
191                         LiveProcess *p, ThreadContext *tc);
192SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
193                         LiveProcess *p, ThreadContext *tc);
194
195/// Target exit() handler: terminate current context.
196SyscallReturn exitFunc(SyscallDesc *desc, int num,
197                       LiveProcess *p, ThreadContext *tc);
198
199/// Target exit_group() handler: terminate simulation. (exit all threads)
200SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
201                       LiveProcess *p, ThreadContext *tc);
202
203/// Target getpagesize() handler.
204SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
205                              LiveProcess *p, ThreadContext *tc);
206
207/// Target brk() handler: set brk address.
208SyscallReturn brkFunc(SyscallDesc *desc, int num,
209                      LiveProcess *p, ThreadContext *tc);
210
211/// Target close() handler.
212SyscallReturn closeFunc(SyscallDesc *desc, int num,
213                        LiveProcess *p, ThreadContext *tc);
214
215/// Target read() handler.
216SyscallReturn readFunc(SyscallDesc *desc, int num,
217                       LiveProcess *p, ThreadContext *tc);
218
219/// Target write() handler.
220SyscallReturn writeFunc(SyscallDesc *desc, int num,
221                        LiveProcess *p, ThreadContext *tc);
222
223/// Target lseek() handler.
224SyscallReturn lseekFunc(SyscallDesc *desc, int num,
225                        LiveProcess *p, ThreadContext *tc);
226
227/// Target _llseek() handler.
228SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
229                        LiveProcess *p, ThreadContext *tc);
230
231/// Target munmap() handler.
232SyscallReturn munmapFunc(SyscallDesc *desc, int num,
233                         LiveProcess *p, ThreadContext *tc);
234
235/// Target gethostname() handler.
236SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
237                              LiveProcess *p, ThreadContext *tc);
238
239/// Target getcwd() handler.
240SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
241                         LiveProcess *p, ThreadContext *tc);
242
243/// Target unlink() handler.
244SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
245                           LiveProcess *p, ThreadContext *tc);
246
247/// Target unlink() handler.
248SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
249                         LiveProcess *p, ThreadContext *tc);
250
251/// Target mkdir() handler.
252SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
253                        LiveProcess *p, ThreadContext *tc);
254
255/// Target rename() handler.
256SyscallReturn renameFunc(SyscallDesc *desc, int num,
257                         LiveProcess *p, ThreadContext *tc);
258
259
260/// Target truncate() handler.
261SyscallReturn truncateFunc(SyscallDesc *desc, int num,
262                           LiveProcess *p, ThreadContext *tc);
263
264
265/// Target ftruncate() handler.
266SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
267                            LiveProcess *p, ThreadContext *tc);
268
269
270/// Target truncate64() handler.
271SyscallReturn truncate64Func(SyscallDesc *desc, int num,
272                             LiveProcess *p, ThreadContext *tc);
273
274/// Target ftruncate64() handler.
275SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
276                              LiveProcess *p, ThreadContext *tc);
277
278
279/// Target umask() handler.
280SyscallReturn umaskFunc(SyscallDesc *desc, int num,
281                        LiveProcess *p, ThreadContext *tc);
282
283
284/// Target chown() handler.
285SyscallReturn chownFunc(SyscallDesc *desc, int num,
286                        LiveProcess *p, ThreadContext *tc);
287
288
289/// Target fchown() handler.
290SyscallReturn fchownFunc(SyscallDesc *desc, int num,
291                         LiveProcess *p, ThreadContext *tc);
292
293/// Target dup() handler.
294SyscallReturn dupFunc(SyscallDesc *desc, int num,
295                      LiveProcess *process, ThreadContext *tc);
296
297/// Target fnctl() handler.
298SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
299                        LiveProcess *process, ThreadContext *tc);
300
301/// Target fcntl64() handler.
302SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
303                        LiveProcess *process, ThreadContext *tc);
304
305/// Target setuid() handler.
306SyscallReturn setuidFunc(SyscallDesc *desc, int num,
307                               LiveProcess *p, ThreadContext *tc);
308
309/// Target getpid() handler.
310SyscallReturn getpidFunc(SyscallDesc *desc, int num,
311                               LiveProcess *p, ThreadContext *tc);
312
313/// Target getuid() handler.
314SyscallReturn getuidFunc(SyscallDesc *desc, int num,
315                               LiveProcess *p, ThreadContext *tc);
316
317/// Target getgid() handler.
318SyscallReturn getgidFunc(SyscallDesc *desc, int num,
319                               LiveProcess *p, ThreadContext *tc);
320
321/// Target getppid() handler.
322SyscallReturn getppidFunc(SyscallDesc *desc, int num,
323                               LiveProcess *p, ThreadContext *tc);
324
325/// Target geteuid() handler.
326SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
327                               LiveProcess *p, ThreadContext *tc);
328
329/// Target getegid() handler.
330SyscallReturn getegidFunc(SyscallDesc *desc, int num,
331                               LiveProcess *p, ThreadContext *tc);
332
333/// Target clone() handler.
334SyscallReturn cloneFunc(SyscallDesc *desc, int num,
335                               LiveProcess *p, ThreadContext *tc);
336
337/// Futex system call
338///  Implemented by Daniel Sanchez
339///  Used by printf's in multi-threaded apps
340template <class OS>
341SyscallReturn
342futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
343          ThreadContext *tc)
344{
345    int index_uaddr = 0;
346    int index_op = 1;
347    int index_val = 2;
348    int index_timeout = 3;
349
350    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
351    int op = process->getSyscallArg(tc, index_op);
352    int val = process->getSyscallArg(tc, index_val);
353    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
354
355    std::map<uint64_t, std::list<ThreadContext *> * >
356        &futex_map = tc->getSystemPtr()->futexMap;
357
358    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
359            uaddr, op, val);
360
361
362    if (op == OS::TGT_FUTEX_WAIT) {
363        if (timeout != 0) {
364            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
365                 "we'll wait indefinitely");
366        }
367
368        uint8_t *buf = new uint8_t[sizeof(int)];
369        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
370        int mem_val = *((int *)buf);
371        delete buf;
372
373        if(val != mem_val) {
374            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
375                                    "expected: %d\n", mem_val, val);
376            return -OS::TGT_EWOULDBLOCK;
377        }
378
379        // Queue the thread context
380        std::list<ThreadContext *> * tcWaitList;
381        if (futex_map.count(uaddr)) {
382            tcWaitList = futex_map.find(uaddr)->second;
383        } else {
384            tcWaitList = new std::list<ThreadContext *>();
385            futex_map.insert(std::pair< uint64_t,
386                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
387        }
388        tcWaitList->push_back(tc);
389        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
390                                "thread context\n");
391        tc->suspend();
392        return 0;
393    } else if (op == OS::TGT_FUTEX_WAKE){
394        int wokenUp = 0;
395        std::list<ThreadContext *> * tcWaitList;
396        if (futex_map.count(uaddr)) {
397            tcWaitList = futex_map.find(uaddr)->second;
398            while (tcWaitList->size() > 0 && wokenUp < val) {
399                tcWaitList->front()->activate();
400                tcWaitList->pop_front();
401                wokenUp++;
402            }
403            if(tcWaitList->empty()) {
404                futex_map.erase(uaddr);
405                delete tcWaitList;
406            }
407        }
408        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
409                                "thread contexts\n", wokenUp);
410        return wokenUp;
411    } else {
412        warn("sys_futex: op %d is not implemented, just returning...");
413        return 0;
414    }
415
416}
417
418
419/// Pseudo Funcs  - These functions use a different return convension,
420/// returning a second value in a register other than the normal return register
421SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
422                             LiveProcess *process, ThreadContext *tc);
423
424/// Target getpidPseudo() handler.
425SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
426                               LiveProcess *p, ThreadContext *tc);
427
428/// Target getuidPseudo() handler.
429SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
430                               LiveProcess *p, ThreadContext *tc);
431
432/// Target getgidPseudo() handler.
433SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
434                               LiveProcess *p, ThreadContext *tc);
435
436
437/// A readable name for 1,000,000, for converting microseconds to seconds.
438const int one_million = 1000000;
439
440/// Approximate seconds since the epoch (1/1/1970).  About a billion,
441/// by my reckoning.  We want to keep this a constant (not use the
442/// real-world time) to keep simulations repeatable.
443const unsigned seconds_since_epoch = 1000000000;
444
445/// Helper function to convert current elapsed time to seconds and
446/// microseconds.
447template <class T1, class T2>
448void
449getElapsedTime(T1 &sec, T2 &usec)
450{
451    int elapsed_usecs = curTick() / SimClock::Int::us;
452    sec = elapsed_usecs / one_million;
453    usec = elapsed_usecs % one_million;
454}
455
456//////////////////////////////////////////////////////////////////////
457//
458// The following emulation functions are generic, but need to be
459// templated to account for differences in types, constants, etc.
460//
461//////////////////////////////////////////////////////////////////////
462
463#if NO_STAT64
464    typedef struct stat hst_stat;
465    typedef struct stat hst_stat64;
466#else
467    typedef struct stat hst_stat;
468    typedef struct stat64 hst_stat64;
469#endif
470
471//// Helper function to convert a host stat buffer to a target stat
472//// buffer.  Also copies the target buffer out to the simulated
473//// memory space.  Used by stat(), fstat(), and lstat().
474
475template <typename target_stat, typename host_stat>
476static void
477convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
478{
479    using namespace TheISA;
480
481    if (fakeTTY)
482        tgt->st_dev = 0xA;
483    else
484        tgt->st_dev = host->st_dev;
485    tgt->st_dev = TheISA::htog(tgt->st_dev);
486    tgt->st_ino = host->st_ino;
487    tgt->st_ino = TheISA::htog(tgt->st_ino);
488    tgt->st_mode = host->st_mode;
489    if (fakeTTY) {
490        // Claim to be a character device
491        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
492        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
493    }
494    tgt->st_mode = TheISA::htog(tgt->st_mode);
495    tgt->st_nlink = host->st_nlink;
496    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
497    tgt->st_uid = host->st_uid;
498    tgt->st_uid = TheISA::htog(tgt->st_uid);
499    tgt->st_gid = host->st_gid;
500    tgt->st_gid = TheISA::htog(tgt->st_gid);
501    if (fakeTTY)
502        tgt->st_rdev = 0x880d;
503    else
504        tgt->st_rdev = host->st_rdev;
505    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
506    tgt->st_size = host->st_size;
507    tgt->st_size = TheISA::htog(tgt->st_size);
508    tgt->st_atimeX = host->st_atime;
509    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
510    tgt->st_mtimeX = host->st_mtime;
511    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
512    tgt->st_ctimeX = host->st_ctime;
513    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
514    // Force the block size to be 8k. This helps to ensure buffered io works
515    // consistently across different hosts.
516    tgt->st_blksize = 0x2000;
517    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
518    tgt->st_blocks = host->st_blocks;
519    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
520}
521
522// Same for stat64
523
524template <typename target_stat, typename host_stat64>
525static void
526convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
527{
528    using namespace TheISA;
529
530    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
531#if defined(STAT_HAVE_NSEC)
532    tgt->st_atime_nsec = host->st_atime_nsec;
533    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
534    tgt->st_mtime_nsec = host->st_mtime_nsec;
535    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
536    tgt->st_ctime_nsec = host->st_ctime_nsec;
537    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
538#else
539    tgt->st_atime_nsec = 0;
540    tgt->st_mtime_nsec = 0;
541    tgt->st_ctime_nsec = 0;
542#endif
543}
544
545//Here are a couple convenience functions
546template<class OS>
547static void
548copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
549        hst_stat *host, bool fakeTTY = false)
550{
551    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
552    tgt_stat_buf tgt(addr);
553    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
554    tgt.copyOut(mem);
555}
556
557template<class OS>
558static void
559copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
560        hst_stat64 *host, bool fakeTTY = false)
561{
562    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
563    tgt_stat_buf tgt(addr);
564    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
565    tgt.copyOut(mem);
566}
567
568/// Target ioctl() handler.  For the most part, programs call ioctl()
569/// only to find out if their stdout is a tty, to determine whether to
570/// do line or block buffering.  We always claim that output fds are
571/// not TTYs to provide repeatable results.
572template <class OS>
573SyscallReturn
574ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
575          ThreadContext *tc)
576{
577    int index = 0;
578    int fd = process->getSyscallArg(tc, index);
579    unsigned req = process->getSyscallArg(tc, index);
580
581    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
582
583    if (fd < 0 || process->sim_fd(fd) < 0) {
584        // doesn't map to any simulator fd: not a valid target fd
585        return -EBADF;
586    }
587
588    if (OS::isTtyReq(req)) {
589        return -ENOTTY;
590    }
591
592    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
593         fd, req, tc->pcState());
594    return -ENOTTY;
595}
596
597/// Target open() handler.
598template <class OS>
599SyscallReturn
600openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
601         ThreadContext *tc)
602{
603    std::string path;
604
605    int index = 0;
606    if (!tc->getMemProxy().tryReadString(path,
607                process->getSyscallArg(tc, index)))
608        return -EFAULT;
609
610    if (path == "/dev/sysdev0") {
611        // This is a memory-mapped high-resolution timer device on Alpha.
612        // We don't support it, so just punt.
613        warn("Ignoring open(%s, ...)\n", path);
614        return -ENOENT;
615    }
616
617    int tgtFlags = process->getSyscallArg(tc, index);
618    int mode = process->getSyscallArg(tc, index);
619    int hostFlags = 0;
620
621    // translate open flags
622    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
623        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
624            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
625            hostFlags |= OS::openFlagTable[i].hostFlag;
626        }
627    }
628
629    // any target flags left?
630    if (tgtFlags != 0)
631        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
632
633#ifdef __CYGWIN32__
634    hostFlags |= O_BINARY;
635#endif
636
637    // Adjust path for current working directory
638    path = process->fullPath(path);
639
640    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
641
642    int fd;
643    int local_errno;
644    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
645        startswith(path, "/platform/") || startswith(path, "/sys/")) {
646        // It's a proc/sys entry and requires special handling
647        fd = OS::openSpecialFile(path, process, tc);
648        local_errno = ENOENT;
649     } else {
650        // open the file
651        fd = open(path.c_str(), hostFlags, mode);
652        local_errno = errno;
653     }
654
655    if (fd == -1)
656        return -local_errno;
657
658    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
659}
660
661/// Target sysinfo() handler.
662template <class OS>
663SyscallReturn
664sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
665         ThreadContext *tc)
666{
667
668    int index = 0;
669    TypedBufferArg<typename OS::tgt_sysinfo>
670        sysinfo(process->getSyscallArg(tc, index));
671
672    sysinfo->uptime=seconds_since_epoch;
673    sysinfo->totalram=process->system->memSize();
674
675    sysinfo.copyOut(tc->getMemProxy());
676
677    return 0;
678}
679
680/// Target chmod() handler.
681template <class OS>
682SyscallReturn
683chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
684          ThreadContext *tc)
685{
686    std::string path;
687
688    int index = 0;
689    if (!tc->getMemProxy().tryReadString(path,
690                process->getSyscallArg(tc, index))) {
691        return -EFAULT;
692    }
693
694    uint32_t mode = process->getSyscallArg(tc, index);
695    mode_t hostMode = 0;
696
697    // XXX translate mode flags via OS::something???
698    hostMode = mode;
699
700    // Adjust path for current working directory
701    path = process->fullPath(path);
702
703    // do the chmod
704    int result = chmod(path.c_str(), hostMode);
705    if (result < 0)
706        return -errno;
707
708    return 0;
709}
710
711
712/// Target fchmod() handler.
713template <class OS>
714SyscallReturn
715fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
716           ThreadContext *tc)
717{
718    int index = 0;
719    int fd = process->getSyscallArg(tc, index);
720    if (fd < 0 || process->sim_fd(fd) < 0) {
721        // doesn't map to any simulator fd: not a valid target fd
722        return -EBADF;
723    }
724
725    uint32_t mode = process->getSyscallArg(tc, index);
726    mode_t hostMode = 0;
727
728    // XXX translate mode flags via OS::someting???
729    hostMode = mode;
730
731    // do the fchmod
732    int result = fchmod(process->sim_fd(fd), hostMode);
733    if (result < 0)
734        return -errno;
735
736    return 0;
737}
738
739/// Target mremap() handler.
740template <class OS>
741SyscallReturn
742mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
743{
744    int index = 0;
745    Addr start = process->getSyscallArg(tc, index);
746    uint64_t old_length = process->getSyscallArg(tc, index);
747    uint64_t new_length = process->getSyscallArg(tc, index);
748    uint64_t flags = process->getSyscallArg(tc, index);
749
750    if ((start % TheISA::VMPageSize != 0) ||
751            (new_length % TheISA::VMPageSize != 0)) {
752        warn("mremap failing: arguments not page aligned");
753        return -EINVAL;
754    }
755
756    if (new_length > old_length) {
757        if ((start + old_length) == process->mmap_end) {
758            uint64_t diff = new_length - old_length;
759            process->allocateMem(process->mmap_end, diff);
760            process->mmap_end += diff;
761            return start;
762        } else {
763            // sys/mman.h defined MREMAP_MAYMOVE
764            if (!(flags & 1)) {
765                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
766                return -ENOMEM;
767            } else {
768                process->pTable->remap(start, old_length, process->mmap_end);
769                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
770                        process->mmap_end, process->mmap_end + new_length, new_length);
771                start = process->mmap_end;
772                // add on the remaining unallocated pages
773                process->allocateMem(start + old_length,
774                                     new_length - old_length);
775                process->mmap_end += new_length;
776                warn("returning %08p as start\n", start);
777                return start;
778            }
779        }
780    } else {
781        process->pTable->unmap(start + new_length, old_length - new_length);
782        return start;
783    }
784}
785
786/// Target stat() handler.
787template <class OS>
788SyscallReturn
789statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
790         ThreadContext *tc)
791{
792    std::string path;
793
794    int index = 0;
795    if (!tc->getMemProxy().tryReadString(path,
796                process->getSyscallArg(tc, index))) {
797        return -EFAULT;
798    }
799    Addr bufPtr = process->getSyscallArg(tc, index);
800
801    // Adjust path for current working directory
802    path = process->fullPath(path);
803
804    struct stat hostBuf;
805    int result = stat(path.c_str(), &hostBuf);
806
807    if (result < 0)
808        return -errno;
809
810    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
811
812    return 0;
813}
814
815
816/// Target stat64() handler.
817template <class OS>
818SyscallReturn
819stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
820           ThreadContext *tc)
821{
822    std::string path;
823
824    int index = 0;
825    if (!tc->getMemProxy().tryReadString(path,
826                process->getSyscallArg(tc, index)))
827        return -EFAULT;
828    Addr bufPtr = process->getSyscallArg(tc, index);
829
830    // Adjust path for current working directory
831    path = process->fullPath(path);
832
833#if NO_STAT64
834    struct stat  hostBuf;
835    int result = stat(path.c_str(), &hostBuf);
836#else
837    struct stat64 hostBuf;
838    int result = stat64(path.c_str(), &hostBuf);
839#endif
840
841    if (result < 0)
842        return -errno;
843
844    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
845
846    return 0;
847}
848
849
850/// Target fstat64() handler.
851template <class OS>
852SyscallReturn
853fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
854            ThreadContext *tc)
855{
856    int index = 0;
857    int fd = process->getSyscallArg(tc, index);
858    Addr bufPtr = process->getSyscallArg(tc, index);
859    if (fd < 0 || process->sim_fd(fd) < 0) {
860        // doesn't map to any simulator fd: not a valid target fd
861        return -EBADF;
862    }
863
864#if NO_STAT64
865    struct stat  hostBuf;
866    int result = fstat(process->sim_fd(fd), &hostBuf);
867#else
868    struct stat64  hostBuf;
869    int result = fstat64(process->sim_fd(fd), &hostBuf);
870#endif
871
872    if (result < 0)
873        return -errno;
874
875    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
876
877    return 0;
878}
879
880
881/// Target lstat() handler.
882template <class OS>
883SyscallReturn
884lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
885          ThreadContext *tc)
886{
887    std::string path;
888
889    int index = 0;
890    if (!tc->getMemProxy().tryReadString(path,
891                process->getSyscallArg(tc, index))) {
892        return -EFAULT;
893    }
894    Addr bufPtr = process->getSyscallArg(tc, index);
895
896    // Adjust path for current working directory
897    path = process->fullPath(path);
898
899    struct stat hostBuf;
900    int result = lstat(path.c_str(), &hostBuf);
901
902    if (result < 0)
903        return -errno;
904
905    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
906
907    return 0;
908}
909
910/// Target lstat64() handler.
911template <class OS>
912SyscallReturn
913lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
914            ThreadContext *tc)
915{
916    std::string path;
917
918    int index = 0;
919    if (!tc->getMemProxy().tryReadString(path,
920                process->getSyscallArg(tc, index))) {
921        return -EFAULT;
922    }
923    Addr bufPtr = process->getSyscallArg(tc, index);
924
925    // Adjust path for current working directory
926    path = process->fullPath(path);
927
928#if NO_STAT64
929    struct stat hostBuf;
930    int result = lstat(path.c_str(), &hostBuf);
931#else
932    struct stat64 hostBuf;
933    int result = lstat64(path.c_str(), &hostBuf);
934#endif
935
936    if (result < 0)
937        return -errno;
938
939    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
940
941    return 0;
942}
943
944/// Target fstat() handler.
945template <class OS>
946SyscallReturn
947fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
948          ThreadContext *tc)
949{
950    int index = 0;
951    int fd = process->sim_fd(process->getSyscallArg(tc, index));
952    Addr bufPtr = process->getSyscallArg(tc, index);
953
954    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
955
956    if (fd < 0)
957        return -EBADF;
958
959    struct stat hostBuf;
960    int result = fstat(fd, &hostBuf);
961
962    if (result < 0)
963        return -errno;
964
965    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
966
967    return 0;
968}
969
970
971/// Target statfs() handler.
972template <class OS>
973SyscallReturn
974statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
975           ThreadContext *tc)
976{
977    std::string path;
978
979    int index = 0;
980    if (!tc->getMemProxy().tryReadString(path,
981                process->getSyscallArg(tc, index))) {
982        return -EFAULT;
983    }
984    Addr bufPtr = process->getSyscallArg(tc, index);
985
986    // Adjust path for current working directory
987    path = process->fullPath(path);
988
989    struct statfs hostBuf;
990    int result = statfs(path.c_str(), &hostBuf);
991
992    if (result < 0)
993        return -errno;
994
995    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
996
997    return 0;
998}
999
1000
1001/// Target fstatfs() handler.
1002template <class OS>
1003SyscallReturn
1004fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1005            ThreadContext *tc)
1006{
1007    int index = 0;
1008    int fd = process->sim_fd(process->getSyscallArg(tc, index));
1009    Addr bufPtr = process->getSyscallArg(tc, index);
1010
1011    if (fd < 0)
1012        return -EBADF;
1013
1014    struct statfs hostBuf;
1015    int result = fstatfs(fd, &hostBuf);
1016
1017    if (result < 0)
1018        return -errno;
1019
1020    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1021
1022    return 0;
1023}
1024
1025
1026/// Target writev() handler.
1027template <class OS>
1028SyscallReturn
1029writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1030           ThreadContext *tc)
1031{
1032    int index = 0;
1033    int fd = process->getSyscallArg(tc, index);
1034    if (fd < 0 || process->sim_fd(fd) < 0) {
1035        // doesn't map to any simulator fd: not a valid target fd
1036        return -EBADF;
1037    }
1038
1039    SETranslatingPortProxy &p = tc->getMemProxy();
1040    uint64_t tiov_base = process->getSyscallArg(tc, index);
1041    size_t count = process->getSyscallArg(tc, index);
1042    struct iovec hiov[count];
1043    for (size_t i = 0; i < count; ++i) {
1044        typename OS::tgt_iovec tiov;
1045
1046        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1047                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1048        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1049        hiov[i].iov_base = new char [hiov[i].iov_len];
1050        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1051                   hiov[i].iov_len);
1052    }
1053
1054    int result = writev(process->sim_fd(fd), hiov, count);
1055
1056    for (size_t i = 0; i < count; ++i)
1057        delete [] (char *)hiov[i].iov_base;
1058
1059    if (result < 0)
1060        return -errno;
1061
1062    return 0;
1063}
1064
1065
1066/// Target mmap() handler.
1067///
1068/// We don't really handle mmap().  If the target is mmaping an
1069/// anonymous region or /dev/zero, we can get away with doing basically
1070/// nothing (since memory is initialized to zero and the simulator
1071/// doesn't really check addresses anyway).
1072///
1073template <class OS>
1074SyscallReturn
1075mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1076{
1077    int index = 0;
1078    Addr start = p->getSyscallArg(tc, index);
1079    uint64_t length = p->getSyscallArg(tc, index);
1080    index++; // int prot = p->getSyscallArg(tc, index);
1081    int flags = p->getSyscallArg(tc, index);
1082    int tgt_fd = p->getSyscallArg(tc, index);
1083    // int offset = p->getSyscallArg(tc, index);
1084
1085    if (length > 0x100000000ULL)
1086        warn("mmap length argument %#x is unreasonably large.\n", length);
1087
1088    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1089        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1090        if (!fd_map || fd_map->fd < 0) {
1091            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1092            return -EBADF;
1093        }
1094
1095        if (fd_map->filename != "/dev/zero") {
1096            // This is very likely broken, but leave a warning here
1097            // (rather than panic) in case /dev/zero is known by
1098            // another name on some platform
1099            warn("allowing mmap of file %s; mmap not supported on files"
1100                 " other than /dev/zero\n", fd_map->filename);
1101        }
1102    }
1103
1104    if ((start  % TheISA::VMPageSize) != 0 ||
1105        (length % TheISA::VMPageSize) != 0) {
1106        warn("mmap failing: arguments not page-aligned: "
1107             "start 0x%x length 0x%x",
1108             start, length);
1109        return -EINVAL;
1110    }
1111
1112    // are we ok with clobbering existing mappings?  only set this to
1113    // true if the user has been warned.
1114    bool clobber = false;
1115
1116    // try to use the caller-provided address if there is one
1117    bool use_provided_address = (start != 0);
1118
1119    if (use_provided_address) {
1120        // check to see if the desired address is already in use
1121        if (!p->pTable->isUnmapped(start, length)) {
1122            // there are existing mappings in the desired range
1123            // whether we clobber them or not depends on whether the caller
1124            // specified MAP_FIXED
1125            if (flags & OS::TGT_MAP_FIXED) {
1126                // MAP_FIXED specified: clobber existing mappings
1127                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
1128                     start);
1129                clobber = true;
1130            } else {
1131                // MAP_FIXED not specified: ignore suggested start address
1132                warn("mmap: ignoring suggested map address 0x%x\n", start);
1133                use_provided_address = false;
1134            }
1135        }
1136    }
1137
1138    if (!use_provided_address) {
1139        // no address provided, or provided address unusable:
1140        // pick next address from our "mmap region"
1141        if (OS::mmapGrowsDown()) {
1142            start = p->mmap_end - length;
1143            p->mmap_end = start;
1144        } else {
1145            start = p->mmap_end;
1146            p->mmap_end += length;
1147        }
1148    }
1149
1150    p->allocateMem(start, length, clobber);
1151
1152    return start;
1153}
1154
1155/// Target getrlimit() handler.
1156template <class OS>
1157SyscallReturn
1158getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1159        ThreadContext *tc)
1160{
1161    int index = 0;
1162    unsigned resource = process->getSyscallArg(tc, index);
1163    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1164
1165    switch (resource) {
1166        case OS::TGT_RLIMIT_STACK:
1167            // max stack size in bytes: make up a number (8MB for now)
1168            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1169            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1170            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1171            break;
1172
1173        case OS::TGT_RLIMIT_DATA:
1174            // max data segment size in bytes: make up a number
1175            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1176            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1177            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1178            break;
1179
1180        default:
1181            std::cerr << "getrlimitFunc: unimplemented resource " << resource
1182                << std::endl;
1183            abort();
1184            break;
1185    }
1186
1187    rlp.copyOut(tc->getMemProxy());
1188    return 0;
1189}
1190
1191/// Target gettimeofday() handler.
1192template <class OS>
1193SyscallReturn
1194gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1195        ThreadContext *tc)
1196{
1197    int index = 0;
1198    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1199
1200    getElapsedTime(tp->tv_sec, tp->tv_usec);
1201    tp->tv_sec += seconds_since_epoch;
1202    tp->tv_sec = TheISA::htog(tp->tv_sec);
1203    tp->tv_usec = TheISA::htog(tp->tv_usec);
1204
1205    tp.copyOut(tc->getMemProxy());
1206
1207    return 0;
1208}
1209
1210
1211/// Target utimes() handler.
1212template <class OS>
1213SyscallReturn
1214utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1215           ThreadContext *tc)
1216{
1217    std::string path;
1218
1219    int index = 0;
1220    if (!tc->getMemProxy().tryReadString(path,
1221                process->getSyscallArg(tc, index))) {
1222        return -EFAULT;
1223    }
1224
1225    TypedBufferArg<typename OS::timeval [2]>
1226        tp(process->getSyscallArg(tc, index));
1227    tp.copyIn(tc->getMemProxy());
1228
1229    struct timeval hostTimeval[2];
1230    for (int i = 0; i < 2; ++i)
1231    {
1232        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1233        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1234    }
1235
1236    // Adjust path for current working directory
1237    path = process->fullPath(path);
1238
1239    int result = utimes(path.c_str(), hostTimeval);
1240
1241    if (result < 0)
1242        return -errno;
1243
1244    return 0;
1245}
1246/// Target getrusage() function.
1247template <class OS>
1248SyscallReturn
1249getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1250              ThreadContext *tc)
1251{
1252    int index = 0;
1253    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1254    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1255
1256    rup->ru_utime.tv_sec = 0;
1257    rup->ru_utime.tv_usec = 0;
1258    rup->ru_stime.tv_sec = 0;
1259    rup->ru_stime.tv_usec = 0;
1260    rup->ru_maxrss = 0;
1261    rup->ru_ixrss = 0;
1262    rup->ru_idrss = 0;
1263    rup->ru_isrss = 0;
1264    rup->ru_minflt = 0;
1265    rup->ru_majflt = 0;
1266    rup->ru_nswap = 0;
1267    rup->ru_inblock = 0;
1268    rup->ru_oublock = 0;
1269    rup->ru_msgsnd = 0;
1270    rup->ru_msgrcv = 0;
1271    rup->ru_nsignals = 0;
1272    rup->ru_nvcsw = 0;
1273    rup->ru_nivcsw = 0;
1274
1275    switch (who) {
1276      case OS::TGT_RUSAGE_SELF:
1277        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1278        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1279        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1280        break;
1281
1282      case OS::TGT_RUSAGE_CHILDREN:
1283        // do nothing.  We have no child processes, so they take no time.
1284        break;
1285
1286      default:
1287        // don't really handle THREAD or CHILDREN, but just warn and
1288        // plow ahead
1289        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
1290             who);
1291    }
1292
1293    rup.copyOut(tc->getMemProxy());
1294
1295    return 0;
1296}
1297
1298/// Target times() function.
1299template <class OS>
1300SyscallReturn
1301timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1302           ThreadContext *tc)
1303{
1304    int index = 0;
1305    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1306
1307    // Fill in the time structure (in clocks)
1308    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1309    bufp->tms_utime = clocks;
1310    bufp->tms_stime = 0;
1311    bufp->tms_cutime = 0;
1312    bufp->tms_cstime = 0;
1313
1314    // Convert to host endianness
1315    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1316
1317    // Write back
1318    bufp.copyOut(tc->getMemProxy());
1319
1320    // Return clock ticks since system boot
1321    return clocks;
1322}
1323
1324/// Target time() function.
1325template <class OS>
1326SyscallReturn
1327timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1328           ThreadContext *tc)
1329{
1330    typename OS::time_t sec, usec;
1331    getElapsedTime(sec, usec);
1332    sec += seconds_since_epoch;
1333
1334    int index = 0;
1335    Addr taddr = (Addr)process->getSyscallArg(tc, index);
1336    if(taddr != 0) {
1337        typename OS::time_t t = sec;
1338        t = TheISA::htog(t);
1339        SETranslatingPortProxy &p = tc->getMemProxy();
1340        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1341    }
1342    return sec;
1343}
1344
1345
1346#endif // __SIM_SYSCALL_EMUL_HH__
1347