cpu: Added interface for vector reg file
[gem5.git] / src / sim / syscall_emul.cc
index 876f39e9917575f5ac1da5653d06b96fc3f01472..8736176edbd12c8d0b4b082c8b2dd5d6566104cf 100644 (file)
  *          Ali Saidi
  */
 
+#include "sim/syscall_emul.hh"
+
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <string>
+#include <csignal>
 #include <iostream>
+#include <string>
 
-#include "sim/syscall_emul.hh"
+#include "arch/utility.hh"
 #include "base/chunk_generator.hh"
 #include "base/trace.hh"
+#include "config/the_isa.hh"
 #include "cpu/thread_context.hh"
-#include "cpu/base.hh"
 #include "mem/page_table.hh"
 #include "sim/process.hh"
-
 #include "sim/sim_exit.hh"
+#include "sim/syscall_debug_macros.hh"
+#include "sim/syscall_desc.hh"
+#include "sim/system.hh"
 
 using namespace std;
 using namespace TheISA;
 
-void
-SyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
-{
-    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
-             curTick,tc->getCpuPtr()->name(), name,
-             tc->getSyscallArg(0),tc->getSyscallArg(1),
-             tc->getSyscallArg(2),tc->getSyscallArg(3));
-
-    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
-
-    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
-             curTick,tc->getCpuPtr()->name(), name, retval.value());
-
-    if (!(flags & SyscallDesc::SuppressReturnValue))
-        tc->setSyscallReturn(retval);
-}
-
-
 SyscallReturn
-unimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
+unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
                   ThreadContext *tc)
 {
-    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
+    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
 
     return 1;
 }
 
 
 SyscallReturn
-ignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
+ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
            ThreadContext *tc)
 {
-    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
-         tc->getSyscallArg(0), tc->getSyscallArg(1));
+    if (desc->needWarning()) {
+        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
+             "\n      (further warnings will be suppressed)" : "");
+    }
 
     return 0;
 }
 
+static void
+exitFutexWake(ThreadContext *tc, Addr addr, uint64_t tgid)
+{
+    // Clear value at address pointed to by thread's childClearTID field.
+    BufferArg ctidBuf(addr, sizeof(long));
+    long *ctid = (long *)ctidBuf.bufferPtr();
+    *ctid = 0;
+    ctidBuf.copyOut(tc->getMemProxy());
+
+    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
+    // Wake one of the waiting threads.
+    futex_map.wakeup(addr, tgid, 1);
+}
 
-SyscallReturn
-exitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-         ThreadContext *tc)
+static SyscallReturn
+exitImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
+         bool group)
 {
-    if (tc->exit()) {
-        exitSimLoop("target called exit()", tc->getSyscallArg(0) & 0xff);
+    int index = 0;
+    int status = p->getSyscallArg(tc, index);
+
+    System *sys = tc->getSystemPtr();
+
+    int activeContexts = 0;
+    for (auto &system: sys->systemList)
+        activeContexts += system->numRunningContexts();
+    if (activeContexts == 1) {
+        exitSimLoop("exiting with last active thread context", status & 0xff);
+        return status;
     }
 
-    return 1;
-}
+    if (group)
+        *p->exitGroup = true;
+
+    if (p->childClearTID)
+        exitFutexWake(tc, p->childClearTID, p->tgid());
+
+    bool last_thread = true;
+    Process *parent = nullptr, *tg_lead = nullptr;
+    for (int i = 0; last_thread && i < sys->numContexts(); i++) {
+        Process *walk;
+        if (!(walk = sys->threadContexts[i]->getProcessPtr()))
+            continue;
+
+        /**
+         * Threads in a thread group require special handing. For instance,
+         * we send the SIGCHLD signal so that it appears that it came from
+         * the head of the group. We also only delete file descriptors if
+         * we are the last thread in the thread group.
+         */
+        if (walk->pid() == p->tgid())
+            tg_lead = walk;
+
+        if ((sys->threadContexts[i]->status() != ThreadContext::Halted)
+            && (walk != p)) {
+            /**
+             * Check if we share thread group with the pointer; this denotes
+             * that we are not the last thread active in the thread group.
+             * Note that setting this to false also prevents further
+             * iterations of the loop.
+             */
+            if (walk->tgid() == p->tgid())
+                last_thread = false;
+
+            /**
+             * A corner case exists which involves execve(). After execve(),
+             * the execve will enable SIGCHLD in the process. The problem
+             * occurs when the exiting process is the root process in the
+             * system; there is no parent to receive the signal. We obviate
+             * this problem by setting the root process' ppid to zero in the
+             * Python configuration files. We really should handle the
+             * root/execve specific case more gracefully.
+             */
+            if (*p->sigchld && (p->ppid() != 0) && (walk->pid() == p->ppid()))
+                parent = walk;
+        }
+    }
 
+    if (last_thread) {
+        if (parent) {
+            assert(tg_lead);
+            sys->signalList.push_back(BasicSignal(tg_lead, parent, SIGCHLD));
+        }
+
+        /**
+         * Run though FD array of the exiting process and close all file
+         * descriptors except for the standard file descriptors.
+         * (The standard file descriptors are shared with gem5.)
+         */
+        for (int i = 0; i < p->fds->getSize(); i++) {
+            if ((*p->fds)[i])
+                p->fds->closeFDEntry(i);
+        }
+    }
+
+    tc->halt();
+    return status;
+}
 
 SyscallReturn
-getpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+exitFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
 {
-    return (int)VMPageSize;
+    return exitImpl(desc, callnum, p, tc, false);
 }
 
+SyscallReturn
+exitGroupFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
+{
+    return exitImpl(desc, callnum, p, tc, true);
+}
 
 SyscallReturn
-obreakFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    Addr junk;
+    return (int)PageBytes;
+}
+
 
+SyscallReturn
+brkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
     // change brk addr to first arg
-    Addr new_brk = tc->getSyscallArg(0);
-    if (new_brk != 0) {
-        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
-                                VMPageSize); !gen.done(); gen.next()) {
-            if (!p->pTable->translate(gen.addr(), junk))
-                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
-                                    VMPageSize);
+    int index = 0;
+    Addr new_brk = p->getSyscallArg(tc, index);
+
+    std::shared_ptr<MemState> mem_state = p->memState;
+    Addr brk_point = mem_state->getBrkPoint();
+
+    // in Linux at least, brk(0) returns the current break value
+    // (note that the syscall and the glibc function have different behavior)
+    if (new_brk == 0)
+        return brk_point;
+
+    if (new_brk > brk_point) {
+        // might need to allocate some new pages
+        for (ChunkGenerator gen(brk_point,
+                                new_brk - brk_point,
+                                PageBytes); !gen.done(); gen.next()) {
+            if (!p->pTable->translate(gen.addr()))
+                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
+
+            // if the address is already there, zero it out
+            else {
+                uint8_t zero = 0;
+                SETranslatingPortProxy &tp = tc->getMemProxy();
+
+                // split non-page aligned accesses
+                Addr next_page = roundUp(gen.addr(), PageBytes);
+                uint32_t size_needed = next_page - gen.addr();
+                tp.memsetBlob(gen.addr(), zero, size_needed);
+                if (gen.addr() + PageBytes > next_page &&
+                    next_page < new_brk &&
+                    p->pTable->translate(next_page)) {
+                    size_needed = PageBytes - size_needed;
+                    tp.memsetBlob(next_page, zero, size_needed);
+                }
+            }
         }
-        p->brk_point = new_brk;
     }
-    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
-    return p->brk_point;
+
+    mem_state->setBrkPoint(new_brk);
+    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
+                    mem_state->getBrkPoint());
+    return mem_state->getBrkPoint();
 }
 
+SyscallReturn
+setTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
+                  ThreadContext *tc)
+{
+    int index = 0;
+    uint64_t tidPtr = process->getSyscallArg(tc, index);
+
+    process->childClearTID = tidPtr;
+    return process->pid();
+}
 
 SyscallReturn
-closeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+closeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int target_fd = tc->getSyscallArg(0);
-    int status = close(p->sim_fd(target_fd));
-    if (status >= 0)
-        p->free_fd(target_fd);
-    return status;
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+
+    return p->fds->closeFDEntry(tgt_fd);
 }
 
 
 SyscallReturn
-readFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = p->sim_fd(tc->getSyscallArg(0));
-    int nbytes = tc->getSyscallArg(2);
-    BufferArg bufArg(tc->getSyscallArg(1), nbytes);
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    Addr buf_ptr = p->getSyscallArg(tc, index);
+    int nbytes = p->getSyscallArg(tc, index);
 
-    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
+    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
+    if (!hbfdp)
+        return -EBADF;
+    int sim_fd = hbfdp->getSimFD();
 
-    if (bytes_read != -1)
-        bufArg.copyOut(tc->getMemPort());
+    BufferArg bufArg(buf_ptr, nbytes);
+    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
+
+    if (bytes_read > 0)
+        bufArg.copyOut(tc->getMemProxy());
 
     return bytes_read;
 }
 
 SyscallReturn
-writeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = p->sim_fd(tc->getSyscallArg(0));
-    int nbytes = tc->getSyscallArg(2);
-    BufferArg bufArg(tc->getSyscallArg(1), nbytes);
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    Addr buf_ptr = p->getSyscallArg(tc, index);
+    int nbytes = p->getSyscallArg(tc, index);
+
+    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
+    if (!hbfdp)
+        return -EBADF;
+    int sim_fd = hbfdp->getSimFD();
 
-    bufArg.copyIn(tc->getMemPort());
+    BufferArg bufArg(buf_ptr, nbytes);
+    bufArg.copyIn(tc->getMemProxy());
 
-    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
+    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
 
-    fsync(fd);
+    fsync(sim_fd);
 
     return bytes_written;
 }
 
 
 SyscallReturn
-lseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+lseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = p->sim_fd(tc->getSyscallArg(0));
-    uint64_t offs = tc->getSyscallArg(1);
-    int whence = tc->getSyscallArg(2);
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    uint64_t offs = p->getSyscallArg(tc, index);
+    int whence = p->getSyscallArg(tc, index);
 
-    off_t result = lseek(fd, offs, whence);
+    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
+    if (!ffdp)
+        return -EBADF;
+    int sim_fd = ffdp->getSimFD();
+
+    off_t result = lseek(sim_fd, offs, whence);
 
     return (result == (off_t)-1) ? -errno : result;
 }
 
 
 SyscallReturn
-_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = p->sim_fd(tc->getSyscallArg(0));
-    uint64_t offset_high = tc->getSyscallArg(1);
-    uint32_t offset_low = tc->getSyscallArg(2);
-    Addr result_ptr = tc->getSyscallArg(3);
-    int whence = tc->getSyscallArg(4);
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    uint64_t offset_high = p->getSyscallArg(tc, index);
+    uint32_t offset_low = p->getSyscallArg(tc, index);
+    Addr result_ptr = p->getSyscallArg(tc, index);
+    int whence = p->getSyscallArg(tc, index);
+
+    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
+    if (!ffdp)
+        return -EBADF;
+    int sim_fd = ffdp->getSimFD();
 
     uint64_t offset = (offset_high << 32) | offset_low;
 
-    uint64_t result = lseek(fd, offset, whence);
+    uint64_t result = lseek(sim_fd, offset, whence);
     result = TheISA::htog(result);
 
-    if (result == (off_t)-1) {
-        //The seek failed.
+    if (result == (off_t)-1)
         return -errno;
-    } else {
-        //The seek succeeded.
-        //Copy "result" to "result_ptr"
-        //XXX We'll assume that the size of loff_t is 64 bits on the
-        //target platform
-        BufferArg result_buf(result_ptr, sizeof(result));
-        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
-        result_buf.copyOut(tc->getMemPort());
-        return 0;
-    }
-
-
-    return (result == (off_t)-1) ? -errno : result;
+    // Assuming that the size of loff_t is 64 bits on the target platform
+    BufferArg result_buf(result_ptr, sizeof(result));
+    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
+    result_buf.copyOut(tc->getMemProxy());
+    return 0;
 }
 
 
 SyscallReturn
-munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+munmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    // given that we don't really implement mmap, munmap is really easy
+    // With mmap more fully implemented, it might be worthwhile to bite
+    // the bullet and implement munmap. Should allow us to reuse simulated
+    // memory.
     return 0;
 }
 
@@ -227,25 +370,132 @@ munmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
 const char *hostname = "m5.eecs.umich.edu";
 
 SyscallReturn
-gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+gethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int name_len = tc->getSyscallArg(1);
-    BufferArg name(tc->getSyscallArg(0), name_len);
+    int index = 0;
+    Addr buf_ptr = p->getSyscallArg(tc, index);
+    int name_len = p->getSyscallArg(tc, index);
+    BufferArg name(buf_ptr, name_len);
 
     strncpy((char *)name.bufferPtr(), hostname, name_len);
 
-    name.copyOut(tc->getMemPort());
+    name.copyOut(tc->getMemProxy());
 
     return 0;
 }
 
 SyscallReturn
-unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+getcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
+    int result = 0;
+    int index = 0;
+    Addr buf_ptr = p->getSyscallArg(tc, index);
+    unsigned long size = p->getSyscallArg(tc, index);
+    BufferArg buf(buf_ptr, size);
+
+    // Is current working directory defined?
+    string cwd = p->getcwd();
+    if (!cwd.empty()) {
+        if (cwd.length() >= size) {
+            // Buffer too small
+            return -ERANGE;
+        }
+        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
+        result = cwd.length();
+    } else {
+        if (getcwd((char *)buf.bufferPtr(), size)) {
+            result = strlen((char *)buf.bufferPtr());
+        } else {
+            result = -1;
+        }
+    }
+
+    buf.copyOut(tc->getMemProxy());
+
+    return (result == -1) ? -errno : result;
+}
+
+SyscallReturn
+readlinkFunc(SyscallDesc *desc, int callnum, Process *process,
+             ThreadContext *tc)
+{
+    return readlinkFunc(desc, callnum, process, tc, 0);
+}
+
+SyscallReturn
+readlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
+             int index)
+{
+    string path;
+
+    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
+        return -EFAULT;
+
+    // Adjust path for current working directory
+    path = p->fullPath(path);
+
+    Addr buf_ptr = p->getSyscallArg(tc, index);
+    size_t bufsiz = p->getSyscallArg(tc, index);
+
+    BufferArg buf(buf_ptr, bufsiz);
+
+    int result = -1;
+    if (path != "/proc/self/exe") {
+        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
+    } else {
+        // Emulate readlink() called on '/proc/self/exe' should return the
+        // absolute path of the binary running in the simulated system (the
+        // Process' executable). It is possible that using this path in
+        // the simulated system will result in unexpected behavior if:
+        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
+        //     called binary calls readlink().
+        //  2) The host's full path to the running benchmark changes from one
+        //     simulation to another. This can result in different simulated
+        //     performance since the simulated system will process the binary
+        //     path differently, even if the binary itself does not change.
+
+        // Get the absolute canonical path to the running application
+        char real_path[PATH_MAX];
+        char *check_real_path = realpath(p->progName(), real_path);
+        if (!check_real_path) {
+            fatal("readlink('/proc/self/exe') unable to resolve path to "
+                  "executable: %s", p->progName());
+        }
+        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
+        size_t real_path_len = strlen(real_path);
+        if (real_path_len > bufsiz) {
+            // readlink will truncate the contents of the
+            // path to ensure it is no more than bufsiz
+            result = bufsiz;
+        } else {
+            result = real_path_len;
+        }
+
+        // Issue a warning about potential unexpected results
+        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
+                  "results in various settings.\n      Returning '%s'\n",
+                  (char*)buf.bufferPtr());
+    }
+
+    buf.copyOut(tc->getMemProxy());
+
+    return (result == -1) ? -errno : result;
+}
+
+SyscallReturn
+unlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
+    return unlinkHelper(desc, num, p, tc, 0);
+}
+
+SyscallReturn
+unlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
+             int index)
 {
     string path;
 
-    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
-        return (TheISA::IntReg)-EFAULT;
+    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
+        return -EFAULT;
 
     // Adjust path for current working directory
     path = p->fullPath(path);
@@ -254,17 +504,37 @@ unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
     return (result == -1) ? -errno : result;
 }
 
+
 SyscallReturn
-renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+mkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
+    string path;
+
+    int index = 0;
+    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
+        return -EFAULT;
+
+    // Adjust path for current working directory
+    path = p->fullPath(path);
+
+    mode_t mode = p->getSyscallArg(tc, index);
+
+    int result = mkdir(path.c_str(), mode);
+    return (result == -1) ? -errno : result;
+}
+
+SyscallReturn
+renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
     string old_name;
 
-    if (!tc->getMemPort()->tryReadString(old_name, tc->getSyscallArg(0)))
+    int index = 0;
+    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
         return -EFAULT;
 
     string new_name;
 
-    if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1)))
+    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
         return -EFAULT;
 
     // Adjust path for current working directory
@@ -276,14 +546,15 @@ renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
 }
 
 SyscallReturn
-truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
     string path;
 
-    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
+    int index = 0;
+    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
         return -EFAULT;
 
-    off_t length = tc->getSyscallArg(1);
+    off_t length = p->getSyscallArg(tc, index);
 
     // Adjust path for current working directory
     path = p->fullPath(path);
@@ -293,31 +564,88 @@ truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
 }
 
 SyscallReturn
-ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
+ftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = process->sim_fd(tc->getSyscallArg(0));
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    off_t length = p->getSyscallArg(tc, index);
 
-    if (fd < 0)
+    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
+    if (!ffdp)
         return -EBADF;
+    int sim_fd = ffdp->getSimFD();
+
+    int result = ftruncate(sim_fd, length);
+    return (result == -1) ? -errno : result;
+}
+
+SyscallReturn
+truncate64Func(SyscallDesc *desc, int num,
+               Process *process, ThreadContext *tc)
+{
+    int index = 0;
+    string path;
+
+    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
+        return -EFAULT;
+
+    int64_t length = process->getSyscallArg(tc, index, 64);
+
+    // Adjust path for current working directory
+    path = process->fullPath(path);
 
-    off_t length = tc->getSyscallArg(1);
+#if NO_STAT64
+    int result = truncate(path.c_str(), length);
+#else
+    int result = truncate64(path.c_str(), length);
+#endif
+    return (result == -1) ? -errno : result;
+}
+
+SyscallReturn
+ftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    int64_t length = p->getSyscallArg(tc, index, 64);
+
+    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
+    if (!ffdp)
+        return -EBADF;
+    int sim_fd = ffdp->getSimFD();
 
-    int result = ftruncate(fd, length);
+#if NO_STAT64
+    int result = ftruncate(sim_fd, length);
+#else
+    int result = ftruncate64(sim_fd, length);
+#endif
     return (result == -1) ? -errno : result;
 }
 
 SyscallReturn
-chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
+umaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
+{
+    // Letting the simulated program change the simulator's umask seems like
+    // a bad idea.  Compromise by just returning the current umask but not
+    // changing anything.
+    mode_t oldMask = umask(0);
+    umask(oldMask);
+    return (int)oldMask;
+}
+
+SyscallReturn
+chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
     string path;
 
-    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
+    int index = 0;
+    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
         return -EFAULT;
 
     /* XXX endianess */
-    uint32_t owner = tc->getSyscallArg(1);
+    uint32_t owner = p->getSyscallArg(tc, index);
     uid_t hostOwner = owner;
-    uint32_t group = tc->getSyscallArg(2);
+    uint32_t group = p->getSyscallArg(tc, index);
     gid_t hostGroup = group;
 
     // Adjust path for current working directory
@@ -328,133 +656,262 @@ chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
 }
 
 SyscallReturn
-fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
+fchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = process->sim_fd(tc->getSyscallArg(0));
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
 
-    if (fd < 0)
+    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
+    if (!ffdp)
         return -EBADF;
+    int sim_fd = ffdp->getSimFD();
 
     /* XXX endianess */
-    uint32_t owner = tc->getSyscallArg(1);
+    uint32_t owner = p->getSyscallArg(tc, index);
     uid_t hostOwner = owner;
-    uint32_t group = tc->getSyscallArg(2);
+    uint32_t group = p->getSyscallArg(tc, index);
     gid_t hostGroup = group;
 
-    int result = fchown(fd, hostOwner, hostGroup);
+    int result = fchown(sim_fd, hostOwner, hostGroup);
     return (result == -1) ? -errno : result;
 }
 
-
+/**
+ * FIXME: The file description is not shared among file descriptors created
+ * with dup. Really, it's difficult to maintain fields like file offset or
+ * flags since an update to such a field won't be reflected in the metadata
+ * for the fd entries that we maintain for checkpoint restoration.
+ */
 SyscallReturn
-dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
+dupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = process->sim_fd(tc->getSyscallArg(0));
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
 
-    if (fd < 0)
+    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
+    if (!old_hbfdp)
         return -EBADF;
+    int sim_fd = old_hbfdp->getSimFD();
 
-    int result = dup(fd);
-    return (result == -1) ? -errno : process->alloc_fd(result);
+    int result = dup(sim_fd);
+    if (result == -1)
+        return -errno;
+
+    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone());
+    new_hbfdp->setSimFD(result);
+    new_hbfdp->setCOE(false);
+    return p->fds->allocFD(new_hbfdp);
 }
 
+SyscallReturn
+dup2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
+    int index = 0;
+
+    int old_tgt_fd = p->getSyscallArg(tc, index);
+    auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]);
+    if (!old_hbp)
+        return -EBADF;
+    int old_sim_fd = old_hbp->getSimFD();
+
+    /**
+     * We need a valid host file descriptor number to be able to pass into
+     * the second parameter for dup2 (newfd), but we don't know what the
+     * viable numbers are; we execute the open call to retrieve one.
+     */
+    int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY));
+    if (res_fd == -1)
+        return -errno;
+
+    int new_tgt_fd = p->getSyscallArg(tc, index);
+    auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]);
+    if (new_hbp)
+        p->fds->closeFDEntry(new_tgt_fd);
+    new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone());
+    new_hbp->setSimFD(res_fd);
+    new_hbp->setCOE(false);
+
+    return p->fds->allocFD(new_hbp);
+}
 
 SyscallReturn
-fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
-          ThreadContext *tc)
+fcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = tc->getSyscallArg(0);
+    int arg;
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    int cmd = p->getSyscallArg(tc, index);
 
-    if (fd < 0 || process->sim_fd(fd) < 0)
+    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
+    if (!hbfdp)
         return -EBADF;
+    int sim_fd = hbfdp->getSimFD();
 
-    int cmd = tc->getSyscallArg(1);
-    switch (cmd) {
-      case 0: // F_DUPFD
-        // if we really wanted to support this, we'd need to do it
-        // in the target fd space.
-        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
-        return -EMFILE;
+    int coe = hbfdp->getCOE();
 
-      case 1: // F_GETFD (get close-on-exec flag)
-      case 2: // F_SETFD (set close-on-exec flag)
-        return 0;
+    switch (cmd) {
+      case F_GETFD:
+        return coe & FD_CLOEXEC;
 
-      case 3: // F_GETFL (get file flags)
-      case 4: // F_SETFL (set file flags)
-        // not sure if this is totally valid, but we'll pass it through
-        // to the underlying OS
-        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
-        return fcntl(process->sim_fd(fd), cmd);
-        // return 0;
-
-      case 7: // F_GETLK  (get lock)
-      case 8: // F_SETLK  (set lock)
-      case 9: // F_SETLKW (set lock and wait)
-        // don't mess with file locking... just act like it's OK
-        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
+      case F_SETFD: {
+        arg = p->getSyscallArg(tc, index);
+        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
         return 0;
+      }
+
+      // Rely on the host to maintain the file status flags for this file
+      // description rather than maintain it ourselves. Admittedly, this
+      // is suboptimal (and possibly error prone), but it is difficult to
+      // maintain the flags by tracking them across the different descriptors
+      // (that refer to this file description) caused by clone, dup, and
+      // subsequent fcntls.
+      case F_GETFL:
+      case F_SETFL: {
+        arg = p->getSyscallArg(tc, index);
+        int rv = fcntl(sim_fd, cmd, arg);
+        return (rv == -1) ? -errno : rv;
+      }
 
       default:
-        warn("Unknown fcntl command %d\n", cmd);
+        warn("fcntl: unsupported command %d\n", cmd);
         return 0;
     }
 }
 
 SyscallReturn
-fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
-            ThreadContext *tc)
+fcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
 {
-    int fd = tc->getSyscallArg(0);
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
 
-    if (fd < 0 || process->sim_fd(fd) < 0)
+    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
+    if (!hbfdp)
         return -EBADF;
+    int sim_fd = hbfdp->getSimFD();
 
-    int cmd = tc->getSyscallArg(1);
+    int cmd = p->getSyscallArg(tc, index);
     switch (cmd) {
       case 33: //F_GETLK64
-        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
+        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
         return -EMFILE;
 
       case 34: // F_SETLK64
       case 35: // F_SETLKW64
-        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
+        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
+             tgt_fd);
         return -EMFILE;
 
       default:
         // not sure if this is totally valid, but we'll pass it through
         // to the underlying OS
-        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
-        return fcntl(process->sim_fd(fd), cmd);
-        // return 0;
+        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
+        return fcntl(sim_fd, cmd);
     }
 }
 
 SyscallReturn
-pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-         ThreadContext *tc)
+pipeImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
+         bool pseudoPipe)
 {
-    int fds[2], sim_fds[2];
-    int pipe_retval = pipe(fds);
+    Addr tgt_addr = 0;
+    if (!pseudoPipe) {
+        int index = 0;
+        tgt_addr = p->getSyscallArg(tc, index);
+    }
+
+    int sim_fds[2], tgt_fds[2];
+
+    int pipe_retval = pipe(sim_fds);
+    if (pipe_retval == -1)
+        return -errno;
 
-    if (pipe_retval < 0) {
-        // error
-        return pipe_retval;
+    auto rend = PipeFDEntry::EndType::read;
+    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
+    tgt_fds[0] = p->fds->allocFD(rpfd);
+
+    auto wend = PipeFDEntry::EndType::write;
+    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
+    tgt_fds[1] = p->fds->allocFD(wpfd);
+
+    /**
+     * Now patch the read object to record the target file descriptor chosen
+     * as the write end of the pipe.
+     */
+    rpfd->setPipeReadSource(tgt_fds[1]);
+
+    /**
+     * Alpha Linux convention for pipe() is that fd[0] is returned as
+     * the return value of the function, and fd[1] is returned in r20.
+     */
+    if (pseudoPipe) {
+        tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
+        return tgt_fds[0];
     }
 
-    sim_fds[0] = process->alloc_fd(fds[0]);
-    sim_fds[1] = process->alloc_fd(fds[1]);
+    /**
+     * Copy the target file descriptors into buffer space and then copy
+     * the buffer space back into the target address space.
+     */
+    BufferArg tgt_handle(tgt_addr, sizeof(int[2]));
+    int *buf_ptr = (int*)tgt_handle.bufferPtr();
+    buf_ptr[0] = tgt_fds[0];
+    buf_ptr[1] = tgt_fds[1];
+    tgt_handle.copyOut(tc->getMemProxy());
+    return 0;
+}
 
-    // Alpha Linux convention for pipe() is that fd[0] is returned as
-    // the return value of the function, and fd[1] is returned in r20.
-    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
-    return sim_fds[0];
+SyscallReturn
+pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
+               ThreadContext *tc)
+{
+    return pipeImpl(desc, callnum, process, tc, true);
+}
+
+SyscallReturn
+pipeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
+{
+    return pipeImpl(desc, callnum, process, tc, false);
 }
 
+SyscallReturn
+setpgidFunc(SyscallDesc *desc, int callnum, Process *process,
+            ThreadContext *tc)
+{
+    int index = 0;
+    int pid = process->getSyscallArg(tc, index);
+    int pgid = process->getSyscallArg(tc, index);
+
+    if (pgid < 0)
+        return -EINVAL;
+
+    if (pid == 0) {
+        process->setpgid(process->pid());
+        return 0;
+    }
+
+    Process *matched_ph = nullptr;
+    System *sysh = tc->getSystemPtr();
+
+    // Retrieves process pointer from active/suspended thread contexts.
+    for (int i = 0; i < sysh->numContexts(); i++) {
+        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
+            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
+            Process *walk_ph = (Process*)temp_h;
+
+            if (walk_ph && walk_ph->pid() == process->pid())
+                matched_ph = walk_ph;
+        }
+    }
+
+    assert(matched_ph);
+    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
+
+    return 0;
+}
 
 SyscallReturn
-getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-           ThreadContext *tc)
+getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
+                 ThreadContext *tc)
 {
     // Make up a PID.  There's no interprocess communication in
     // fake_syscall mode, so there's no way for a process to know it's
@@ -466,82 +923,131 @@ getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
 
 
 SyscallReturn
-getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-           ThreadContext *tc)
+getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
+                 ThreadContext *tc)
 {
     // Make up a UID and EUID... it shouldn't matter, and we want the
     // simulation to be deterministic.
 
     // EUID goes in r20.
-    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
-    return process->uid();             // UID
+    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID
+    return process->uid(); // UID
 }
 
 
 SyscallReturn
-getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-           ThreadContext *tc)
+getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
+                 ThreadContext *tc)
 {
     // Get current group ID.  EGID goes in r20.
-    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
+    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID
     return process->gid();
 }
 
 
 SyscallReturn
-setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
+setuidFunc(SyscallDesc *desc, int callnum, Process *process,
            ThreadContext *tc)
 {
     // can't fathom why a benchmark would call this.
-    warn("Ignoring call to setuid(%d)\n", tc->getSyscallArg(0));
+    int index = 0;
+    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
     return 0;
 }
 
 SyscallReturn
-getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
+getpidFunc(SyscallDesc *desc, int callnum, Process *process,
            ThreadContext *tc)
 {
-    // Make up a PID.  There's no interprocess communication in
-    // fake_syscall mode, so there's no way for a process to know it's
-    // not getting a unique value.
+    return process->tgid();
+}
 
-    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
+SyscallReturn
+gettidFunc(SyscallDesc *desc, int callnum, Process *process,
+           ThreadContext *tc)
+{
     return process->pid();
 }
 
 SyscallReturn
-getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-           ThreadContext *tc)
+getppidFunc(SyscallDesc *desc, int callnum, Process *process,
+            ThreadContext *tc)
 {
     return process->ppid();
 }
 
 SyscallReturn
-getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
+getuidFunc(SyscallDesc *desc, int callnum, Process *process,
            ThreadContext *tc)
 {
-    return process->uid();             // UID
+    return process->uid();              // UID
 }
 
 SyscallReturn
-geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-           ThreadContext *tc)
+geteuidFunc(SyscallDesc *desc, int callnum, Process *process,
+            ThreadContext *tc)
 {
-    return process->euid();            // UID
+    return process->euid();             // UID
 }
 
 SyscallReturn
-getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
+getgidFunc(SyscallDesc *desc, int callnum, Process *process,
            ThreadContext *tc)
 {
     return process->gid();
 }
 
 SyscallReturn
-getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
-           ThreadContext *tc)
+getegidFunc(SyscallDesc *desc, int callnum, Process *process,
+            ThreadContext *tc)
 {
     return process->egid();
 }
 
+SyscallReturn
+fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
+{
+#if NO_FALLOCATE
+    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
+#else
+    int index = 0;
+    int tgt_fd = p->getSyscallArg(tc, index);
+    int mode = p->getSyscallArg(tc, index);
+    off_t offset = p->getSyscallArg(tc, index);
+    off_t len = p->getSyscallArg(tc, index);
+
+    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
+    if (!ffdp)
+        return -EBADF;
+    int sim_fd = ffdp->getSimFD();
+
+    int result = fallocate(sim_fd, mode, offset, len);
+    if (result < 0)
+        return -errno;
+#endif
+    return 0;
+}
+
+SyscallReturn
+accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
+           int index)
+{
+    string path;
+    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
+        return -EFAULT;
+
+    // Adjust path for current working directory
+    path = p->fullPath(path);
+
+    mode_t mode = p->getSyscallArg(tc, index);
+
+    int result = access(path.c_str(), mode);
+    return (result == -1) ? -errno : result;
+}
+
+SyscallReturn
+accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
+{
+    return accessFunc(desc, callnum, p, tc, 0);
+}