syscall_emul.cc revision 11886:43b882cada33
16019Shines@cs.fsu.edu/*
26019Shines@cs.fsu.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
37178Sgblack@eecs.umich.edu * All rights reserved.
47178Sgblack@eecs.umich.edu *
57178Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67178Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77178Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87178Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97178Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107178Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117178Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127178Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137178Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
147178Sgblack@eecs.umich.edu * this software without specific prior written permission.
156019Shines@cs.fsu.edu *
166019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276019Shines@cs.fsu.edu *
286019Shines@cs.fsu.edu * Authors: Steve Reinhardt
296019Shines@cs.fsu.edu *          Ali Saidi
306019Shines@cs.fsu.edu */
316019Shines@cs.fsu.edu
326019Shines@cs.fsu.edu#include "sim/syscall_emul.hh"
336019Shines@cs.fsu.edu
346019Shines@cs.fsu.edu#include <fcntl.h>
356019Shines@cs.fsu.edu#include <unistd.h>
366019Shines@cs.fsu.edu
376019Shines@cs.fsu.edu#include <iostream>
386019Shines@cs.fsu.edu#include <string>
396019Shines@cs.fsu.edu
406019Shines@cs.fsu.edu#include "arch/utility.hh"
416019Shines@cs.fsu.edu#include "base/chunk_generator.hh"
426019Shines@cs.fsu.edu#include "base/trace.hh"
436019Shines@cs.fsu.edu#include "config/the_isa.hh"
446019Shines@cs.fsu.edu#include "cpu/thread_context.hh"
456019Shines@cs.fsu.edu#include "mem/page_table.hh"
466019Shines@cs.fsu.edu#include "sim/process.hh"
476019Shines@cs.fsu.edu#include "sim/sim_exit.hh"
486019Shines@cs.fsu.edu#include "sim/syscall_debug_macros.hh"
496019Shines@cs.fsu.edu#include "sim/syscall_desc.hh"
506019Shines@cs.fsu.edu#include "sim/system.hh"
516019Shines@cs.fsu.edu
526019Shines@cs.fsu.eduusing namespace std;
536019Shines@cs.fsu.eduusing namespace TheISA;
546019Shines@cs.fsu.edu
556019Shines@cs.fsu.eduSyscallReturn
566019Shines@cs.fsu.eduunimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
576019Shines@cs.fsu.edu                  ThreadContext *tc)
586243Sgblack@eecs.umich.edu{
596243Sgblack@eecs.umich.edu    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
606243Sgblack@eecs.umich.edu
616243Sgblack@eecs.umich.edu    return 1;
626243Sgblack@eecs.umich.edu}
636019Shines@cs.fsu.edu
646019Shines@cs.fsu.edu
656019Shines@cs.fsu.eduSyscallReturn
666019Shines@cs.fsu.eduignoreFunc(SyscallDesc *desc, int callnum, Process *process,
676019Shines@cs.fsu.edu           ThreadContext *tc)
686019Shines@cs.fsu.edu{
696019Shines@cs.fsu.edu    if (desc->needWarning()) {
706019Shines@cs.fsu.edu        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
716019Shines@cs.fsu.edu             "\n      (further warnings will be suppressed)" : "");
726019Shines@cs.fsu.edu    }
736019Shines@cs.fsu.edu
746019Shines@cs.fsu.edu    return 0;
756019Shines@cs.fsu.edu}
766019Shines@cs.fsu.edu
776019Shines@cs.fsu.edustatic void
786019Shines@cs.fsu.eduexitFutexWake(ThreadContext *tc, uint64_t uaddr)
796019Shines@cs.fsu.edu{
806019Shines@cs.fsu.edu    std::map<uint64_t, std::list<ThreadContext *> * >
816019Shines@cs.fsu.edu        &futex_map = tc->getSystemPtr()->futexMap;
826019Shines@cs.fsu.edu
836019Shines@cs.fsu.edu    int wokenUp = 0;
846019Shines@cs.fsu.edu    std::list<ThreadContext *> * tcWaitList;
856019Shines@cs.fsu.edu    if (futex_map.count(uaddr)) {
866019Shines@cs.fsu.edu        tcWaitList = futex_map.find(uaddr)->second;
876019Shines@cs.fsu.edu        if (tcWaitList->size() > 0) {
886019Shines@cs.fsu.edu            tcWaitList->front()->activate();
896019Shines@cs.fsu.edu            tcWaitList->pop_front();
906019Shines@cs.fsu.edu            wokenUp++;
916019Shines@cs.fsu.edu        }
926019Shines@cs.fsu.edu        if (tcWaitList->empty()) {
936019Shines@cs.fsu.edu            futex_map.erase(uaddr);
946252Sgblack@eecs.umich.edu            delete tcWaitList;
956243Sgblack@eecs.umich.edu        }
966243Sgblack@eecs.umich.edu    }
976243Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, "exit: FUTEX_WAKE, activated %d waiting "
986019Shines@cs.fsu.edu                            "thread contexts\n", wokenUp);
996019Shines@cs.fsu.edu}
1006019Shines@cs.fsu.edu
1016019Shines@cs.fsu.eduSyscallReturn
1026019Shines@cs.fsu.eduexitFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1036252Sgblack@eecs.umich.edu{
1046243Sgblack@eecs.umich.edu    if (p->system->numRunningContexts() == 1 && !p->childClearTID) {
1056243Sgblack@eecs.umich.edu        // Last running free-parent context; exit simulator.
1066243Sgblack@eecs.umich.edu        int index = 0;
1076019Shines@cs.fsu.edu        exitSimLoop("target called exit()",
1086019Shines@cs.fsu.edu                    p->getSyscallArg(tc, index) & 0xff);
1096019Shines@cs.fsu.edu    } else {
1106019Shines@cs.fsu.edu        if (p->childClearTID)
1116019Shines@cs.fsu.edu            exitFutexWake(tc, p->childClearTID);
1126019Shines@cs.fsu.edu        tc->halt();
1136019Shines@cs.fsu.edu    }
1146252Sgblack@eecs.umich.edu
1156243Sgblack@eecs.umich.edu    return 1;
1166243Sgblack@eecs.umich.edu}
1176243Sgblack@eecs.umich.edu
1186019Shines@cs.fsu.edu
1196019Shines@cs.fsu.eduSyscallReturn
1206019Shines@cs.fsu.eduexitGroupFunc(SyscallDesc *desc, int callnum, Process *process,
1216019Shines@cs.fsu.edu              ThreadContext *tc)
1226019Shines@cs.fsu.edu{
1236019Shines@cs.fsu.edu    // halt all threads belonging to this process
1246019Shines@cs.fsu.edu    for (auto i: process->contextIds) {
1256019Shines@cs.fsu.edu        process->system->getThreadContext(i)->halt();
1266019Shines@cs.fsu.edu    }
1276019Shines@cs.fsu.edu
1286019Shines@cs.fsu.edu    if (!process->system->numRunningContexts()) {
1296019Shines@cs.fsu.edu        // all threads belonged to this process... exit simulator
1306019Shines@cs.fsu.edu        int index = 0;
1316019Shines@cs.fsu.edu        exitSimLoop("target called exit()",
1326019Shines@cs.fsu.edu                    process->getSyscallArg(tc, index) & 0xff);
1336019Shines@cs.fsu.edu    }
1346724Sgblack@eecs.umich.edu
1356724Sgblack@eecs.umich.edu    return 1;
1366019Shines@cs.fsu.edu}
1376019Shines@cs.fsu.edu
1386019Shines@cs.fsu.edu
1396019Shines@cs.fsu.eduSyscallReturn
1406019Shines@cs.fsu.edugetpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1416252Sgblack@eecs.umich.edu{
1426243Sgblack@eecs.umich.edu    return (int)PageBytes;
1436243Sgblack@eecs.umich.edu}
1446243Sgblack@eecs.umich.edu
1456019Shines@cs.fsu.edu
1466019Shines@cs.fsu.eduSyscallReturn
1476019Shines@cs.fsu.edubrkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
1486019Shines@cs.fsu.edu{
1496019Shines@cs.fsu.edu    // change brk addr to first arg
1506019Shines@cs.fsu.edu    int index = 0;
1517178Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1527178Sgblack@eecs.umich.edu
1537178Sgblack@eecs.umich.edu    // in Linux at least, brk(0) returns the current break value
1547178Sgblack@eecs.umich.edu    // (note that the syscall and the glibc function have different behavior)
1557178Sgblack@eecs.umich.edu    if (new_brk == 0)
1567178Sgblack@eecs.umich.edu        return p->memState->brkPoint;
1577178Sgblack@eecs.umich.edu
1587178Sgblack@eecs.umich.edu    if (new_brk > p->memState->brkPoint) {
1597178Sgblack@eecs.umich.edu        // might need to allocate some new pages
1607178Sgblack@eecs.umich.edu        for (ChunkGenerator gen(p->memState->brkPoint,
1617178Sgblack@eecs.umich.edu                                new_brk - p->memState->brkPoint,
1627178Sgblack@eecs.umich.edu                                PageBytes); !gen.done(); gen.next()) {
1637178Sgblack@eecs.umich.edu            if (!p->pTable->translate(gen.addr()))
1647178Sgblack@eecs.umich.edu                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1657178Sgblack@eecs.umich.edu
1667178Sgblack@eecs.umich.edu            // if the address is already there, zero it out
1677178Sgblack@eecs.umich.edu            else {
1687335Sgblack@eecs.umich.edu                uint8_t zero  = 0;
1697335Sgblack@eecs.umich.edu                SETranslatingPortProxy &tp = tc->getMemProxy();
1707335Sgblack@eecs.umich.edu
1717335Sgblack@eecs.umich.edu                // split non-page aligned accesses
1727335Sgblack@eecs.umich.edu                Addr next_page = roundUp(gen.addr(), PageBytes);
1737335Sgblack@eecs.umich.edu                uint32_t size_needed = next_page - gen.addr();
1747335Sgblack@eecs.umich.edu                tp.memsetBlob(gen.addr(), zero, size_needed);
1757335Sgblack@eecs.umich.edu                if (gen.addr() + PageBytes > next_page &&
1767335Sgblack@eecs.umich.edu                    next_page < new_brk &&
1777335Sgblack@eecs.umich.edu                    p->pTable->translate(next_page))
1787335Sgblack@eecs.umich.edu                {
1797335Sgblack@eecs.umich.edu                    size_needed = PageBytes - size_needed;
1807335Sgblack@eecs.umich.edu                    tp.memsetBlob(next_page, zero, size_needed);
1817335Sgblack@eecs.umich.edu                }
1827335Sgblack@eecs.umich.edu            }
1837335Sgblack@eecs.umich.edu        }
1847335Sgblack@eecs.umich.edu    }
1857335Sgblack@eecs.umich.edu
1867335Sgblack@eecs.umich.edu    p->memState->brkPoint = new_brk;
1877335Sgblack@eecs.umich.edu    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
1887335Sgblack@eecs.umich.edu                    p->memState->brkPoint);
1897335Sgblack@eecs.umich.edu    return p->memState->brkPoint;
1907335Sgblack@eecs.umich.edu}
1917335Sgblack@eecs.umich.edu
1927335Sgblack@eecs.umich.eduSyscallReturn
1937178Sgblack@eecs.umich.edusetTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
1947178Sgblack@eecs.umich.edu                  ThreadContext *tc)
1957178Sgblack@eecs.umich.edu{
1967178Sgblack@eecs.umich.edu    int index = 0;
1977178Sgblack@eecs.umich.edu    uint64_t tidPtr = process->getSyscallArg(tc, index);
1987178Sgblack@eecs.umich.edu
1997178Sgblack@eecs.umich.edu    process->childClearTID = tidPtr;
2007178Sgblack@eecs.umich.edu    return process->pid();
2017178Sgblack@eecs.umich.edu}
2027178Sgblack@eecs.umich.edu
2037178Sgblack@eecs.umich.eduSyscallReturn
2047178Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2057178Sgblack@eecs.umich.edu{
2067178Sgblack@eecs.umich.edu    int index = 0;
2077178Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
2087178Sgblack@eecs.umich.edu
2097178Sgblack@eecs.umich.edu    return p->fds->closeFDEntry(tgt_fd);
2107178Sgblack@eecs.umich.edu}
2117178Sgblack@eecs.umich.edu
2127178Sgblack@eecs.umich.edu
2137178Sgblack@eecs.umich.eduSyscallReturn
2147178Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2157178Sgblack@eecs.umich.edu{
2167178Sgblack@eecs.umich.edu    int index = 0;
2177178Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
2187178Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2197178Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2207178Sgblack@eecs.umich.edu
2217178Sgblack@eecs.umich.edu    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
2227178Sgblack@eecs.umich.edu    if (!hbfdp)
2237178Sgblack@eecs.umich.edu        return -EBADF;
2247178Sgblack@eecs.umich.edu    int sim_fd = hbfdp->getSimFD();
2257178Sgblack@eecs.umich.edu
2267178Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
2277178Sgblack@eecs.umich.edu    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
2287178Sgblack@eecs.umich.edu
2297178Sgblack@eecs.umich.edu    if (bytes_read > 0)
2307178Sgblack@eecs.umich.edu        bufArg.copyOut(tc->getMemProxy());
2317178Sgblack@eecs.umich.edu
2327321Sgblack@eecs.umich.edu    return bytes_read;
2337321Sgblack@eecs.umich.edu}
2347321Sgblack@eecs.umich.edu
2357321Sgblack@eecs.umich.eduSyscallReturn
2367321Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2377321Sgblack@eecs.umich.edu{
2387321Sgblack@eecs.umich.edu    int index = 0;
2397321Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
2407321Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2417321Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2427321Sgblack@eecs.umich.edu
2437321Sgblack@eecs.umich.edu    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
2447321Sgblack@eecs.umich.edu    if (!hbfdp)
2457321Sgblack@eecs.umich.edu        return -EBADF;
2467335Sgblack@eecs.umich.edu    int sim_fd = hbfdp->getSimFD();
2477335Sgblack@eecs.umich.edu
2487335Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
2497335Sgblack@eecs.umich.edu    bufArg.copyIn(tc->getMemProxy());
2507335Sgblack@eecs.umich.edu
2517335Sgblack@eecs.umich.edu    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
2527335Sgblack@eecs.umich.edu
2537335Sgblack@eecs.umich.edu    fsync(sim_fd);
2547335Sgblack@eecs.umich.edu
2557321Sgblack@eecs.umich.edu    return bytes_written;
2567323Sgblack@eecs.umich.edu}
2577323Sgblack@eecs.umich.edu
2587323Sgblack@eecs.umich.edu
2597323Sgblack@eecs.umich.eduSyscallReturn
2607323Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2617323Sgblack@eecs.umich.edu{
2627323Sgblack@eecs.umich.edu    int index = 0;
2637323Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
2647323Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2657323Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2667323Sgblack@eecs.umich.edu
2677323Sgblack@eecs.umich.edu    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
2687323Sgblack@eecs.umich.edu    if (!ffdp)
2697323Sgblack@eecs.umich.edu        return -EBADF;
2707323Sgblack@eecs.umich.edu    int sim_fd = ffdp->getSimFD();
2717323Sgblack@eecs.umich.edu
2727323Sgblack@eecs.umich.edu    off_t result = lseek(sim_fd, offs, whence);
2737321Sgblack@eecs.umich.edu
2747321Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2757321Sgblack@eecs.umich.edu}
2767335Sgblack@eecs.umich.edu
2777335Sgblack@eecs.umich.edu
2787335Sgblack@eecs.umich.eduSyscallReturn
2797335Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2807335Sgblack@eecs.umich.edu{
2817335Sgblack@eecs.umich.edu    int index = 0;
2827335Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
2837335Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2847335Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2857335Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2867335Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2877335Sgblack@eecs.umich.edu
2887335Sgblack@eecs.umich.edu    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
2897335Sgblack@eecs.umich.edu    if (!ffdp)
2907335Sgblack@eecs.umich.edu        return -EBADF;
2917335Sgblack@eecs.umich.edu    int sim_fd = ffdp->getSimFD();
2927335Sgblack@eecs.umich.edu
2937335Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2947335Sgblack@eecs.umich.edu
2957335Sgblack@eecs.umich.edu    uint64_t result = lseek(sim_fd, offset, whence);
2967335Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2977335Sgblack@eecs.umich.edu
2987335Sgblack@eecs.umich.edu    if (result == (off_t)-1)
2997335Sgblack@eecs.umich.edu        return -errno;
3007335Sgblack@eecs.umich.edu    // Assuming that the size of loff_t is 64 bits on the target platform
3017335Sgblack@eecs.umich.edu    BufferArg result_buf(result_ptr, sizeof(result));
3027335Sgblack@eecs.umich.edu    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
3037335Sgblack@eecs.umich.edu    result_buf.copyOut(tc->getMemProxy());
3047335Sgblack@eecs.umich.edu    return 0;
3057335Sgblack@eecs.umich.edu}
3067335Sgblack@eecs.umich.edu
3077335Sgblack@eecs.umich.edu
3087335Sgblack@eecs.umich.eduSyscallReturn
3097321Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3107321Sgblack@eecs.umich.edu{
3117321Sgblack@eecs.umich.edu    // With mmap more fully implemented, it might be worthwhile to bite
3127321Sgblack@eecs.umich.edu    // the bullet and implement munmap. Should allow us to reuse simulated
3137321Sgblack@eecs.umich.edu    // memory.
3147321Sgblack@eecs.umich.edu    return 0;
3157335Sgblack@eecs.umich.edu}
3167335Sgblack@eecs.umich.edu
3177335Sgblack@eecs.umich.edu
3187335Sgblack@eecs.umich.educonst char *hostname = "m5.eecs.umich.edu";
3197335Sgblack@eecs.umich.edu
3207335Sgblack@eecs.umich.eduSyscallReturn
3217335Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3227335Sgblack@eecs.umich.edu{
3237335Sgblack@eecs.umich.edu    int index = 0;
3247321Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3257326Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3267326Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
3277326Sgblack@eecs.umich.edu
3287326Sgblack@eecs.umich.edu    strncpy((char *)name.bufferPtr(), hostname, name_len);
3297326Sgblack@eecs.umich.edu
3307326Sgblack@eecs.umich.edu    name.copyOut(tc->getMemProxy());
3317326Sgblack@eecs.umich.edu
3327326Sgblack@eecs.umich.edu    return 0;
3337326Sgblack@eecs.umich.edu}
3347326Sgblack@eecs.umich.edu
3357326Sgblack@eecs.umich.eduSyscallReturn
3367326Sgblack@eecs.umich.edugetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3377326Sgblack@eecs.umich.edu{
3387326Sgblack@eecs.umich.edu    int result = 0;
3397326Sgblack@eecs.umich.edu    int index = 0;
3407326Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3417326Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3427326Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3437326Sgblack@eecs.umich.edu
3447326Sgblack@eecs.umich.edu    // Is current working directory defined?
3457326Sgblack@eecs.umich.edu    string cwd = p->getcwd();
3467326Sgblack@eecs.umich.edu    if (!cwd.empty()) {
3477326Sgblack@eecs.umich.edu        if (cwd.length() >= size) {
3487321Sgblack@eecs.umich.edu            // Buffer too small
3497321Sgblack@eecs.umich.edu            return -ERANGE;
3507335Sgblack@eecs.umich.edu        }
3517335Sgblack@eecs.umich.edu        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3527335Sgblack@eecs.umich.edu        result = cwd.length();
3537335Sgblack@eecs.umich.edu    } else {
3547335Sgblack@eecs.umich.edu        if (getcwd((char *)buf.bufferPtr(), size)) {
3557335Sgblack@eecs.umich.edu            result = strlen((char *)buf.bufferPtr());
3567335Sgblack@eecs.umich.edu        } else {
3577335Sgblack@eecs.umich.edu            result = -1;
3587335Sgblack@eecs.umich.edu        }
3597335Sgblack@eecs.umich.edu    }
3607335Sgblack@eecs.umich.edu
3617335Sgblack@eecs.umich.edu    buf.copyOut(tc->getMemProxy());
3627335Sgblack@eecs.umich.edu
3637335Sgblack@eecs.umich.edu    return (result == -1) ? -errno : result;
3647335Sgblack@eecs.umich.edu}
3657335Sgblack@eecs.umich.edu
3667335Sgblack@eecs.umich.edu/// Target open() handler.
3677335Sgblack@eecs.umich.eduSyscallReturn
3687335Sgblack@eecs.umich.edureadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
3697335Sgblack@eecs.umich.edu             ThreadContext *tc)
3707335Sgblack@eecs.umich.edu{
3717335Sgblack@eecs.umich.edu    return readlinkFunc(desc, callnum, process, tc, 0);
3727335Sgblack@eecs.umich.edu}
3737335Sgblack@eecs.umich.edu
3747335Sgblack@eecs.umich.eduSyscallReturn
3757335Sgblack@eecs.umich.edureadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
3767335Sgblack@eecs.umich.edu             int index)
3777335Sgblack@eecs.umich.edu{
3787335Sgblack@eecs.umich.edu    string path;
3797335Sgblack@eecs.umich.edu
3807335Sgblack@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
3817335Sgblack@eecs.umich.edu        return -EFAULT;
3827335Sgblack@eecs.umich.edu
3837335Sgblack@eecs.umich.edu    // Adjust path for current working directory
3847335Sgblack@eecs.umich.edu    path = p->fullPath(path);
3857335Sgblack@eecs.umich.edu
3867335Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3877335Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3887335Sgblack@eecs.umich.edu
3897335Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3907335Sgblack@eecs.umich.edu
3917335Sgblack@eecs.umich.edu    int result = -1;
3927335Sgblack@eecs.umich.edu    if (path != "/proc/self/exe") {
3937335Sgblack@eecs.umich.edu        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3947321Sgblack@eecs.umich.edu    } else {
3957321Sgblack@eecs.umich.edu        // Emulate readlink() called on '/proc/self/exe' should return the
3967321Sgblack@eecs.umich.edu        // absolute path of the binary running in the simulated system (the
3977321Sgblack@eecs.umich.edu        // Process' executable). It is possible that using this path in
3987321Sgblack@eecs.umich.edu        // the simulated system will result in unexpected behavior if:
399        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
400        //     called binary calls readlink().
401        //  2) The host's full path to the running benchmark changes from one
402        //     simulation to another. This can result in different simulated
403        //     performance since the simulated system will process the binary
404        //     path differently, even if the binary itself does not change.
405
406        // Get the absolute canonical path to the running application
407        char real_path[PATH_MAX];
408        char *check_real_path = realpath(p->progName(), real_path);
409        if (!check_real_path) {
410            fatal("readlink('/proc/self/exe') unable to resolve path to "
411                  "executable: %s", p->progName());
412        }
413        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
414        size_t real_path_len = strlen(real_path);
415        if (real_path_len > bufsiz) {
416            // readlink will truncate the contents of the
417            // path to ensure it is no more than bufsiz
418            result = bufsiz;
419        } else {
420            result = real_path_len;
421        }
422
423        // Issue a warning about potential unexpected results
424        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
425                  "results in various settings.\n      Returning '%s'\n",
426                  (char*)buf.bufferPtr());
427    }
428
429    buf.copyOut(tc->getMemProxy());
430
431    return (result == -1) ? -errno : result;
432}
433
434SyscallReturn
435unlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
436{
437    return unlinkHelper(desc, num, p, tc, 0);
438}
439
440SyscallReturn
441unlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
442             int index)
443{
444    string path;
445
446    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
447        return -EFAULT;
448
449    // Adjust path for current working directory
450    path = p->fullPath(path);
451
452    int result = unlink(path.c_str());
453    return (result == -1) ? -errno : result;
454}
455
456
457SyscallReturn
458mkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
459{
460    string path;
461
462    int index = 0;
463    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
464        return -EFAULT;
465
466    // Adjust path for current working directory
467    path = p->fullPath(path);
468
469    mode_t mode = p->getSyscallArg(tc, index);
470
471    int result = mkdir(path.c_str(), mode);
472    return (result == -1) ? -errno : result;
473}
474
475SyscallReturn
476renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
477{
478    string old_name;
479
480    int index = 0;
481    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
482        return -EFAULT;
483
484    string new_name;
485
486    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
487        return -EFAULT;
488
489    // Adjust path for current working directory
490    old_name = p->fullPath(old_name);
491    new_name = p->fullPath(new_name);
492
493    int64_t result = rename(old_name.c_str(), new_name.c_str());
494    return (result == -1) ? -errno : result;
495}
496
497SyscallReturn
498truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
499{
500    string path;
501
502    int index = 0;
503    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
504        return -EFAULT;
505
506    off_t length = p->getSyscallArg(tc, index);
507
508    // Adjust path for current working directory
509    path = p->fullPath(path);
510
511    int result = truncate(path.c_str(), length);
512    return (result == -1) ? -errno : result;
513}
514
515SyscallReturn
516ftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
517{
518    int index = 0;
519    int tgt_fd = p->getSyscallArg(tc, index);
520    off_t length = p->getSyscallArg(tc, index);
521
522    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
523    if (!ffdp)
524        return -EBADF;
525    int sim_fd = ffdp->getSimFD();
526
527    int result = ftruncate(sim_fd, length);
528    return (result == -1) ? -errno : result;
529}
530
531SyscallReturn
532truncate64Func(SyscallDesc *desc, int num,
533               Process *process, ThreadContext *tc)
534{
535    int index = 0;
536    string path;
537
538    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
539       return -EFAULT;
540
541    int64_t length = process->getSyscallArg(tc, index, 64);
542
543    // Adjust path for current working directory
544    path = process->fullPath(path);
545
546#if NO_STAT64
547    int result = truncate(path.c_str(), length);
548#else
549    int result = truncate64(path.c_str(), length);
550#endif
551    return (result == -1) ? -errno : result;
552}
553
554SyscallReturn
555ftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
556{
557    int index = 0;
558    int tgt_fd = p->getSyscallArg(tc, index);
559    int64_t length = p->getSyscallArg(tc, index, 64);
560
561    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
562    if (!ffdp)
563        return -EBADF;
564    int sim_fd = ffdp->getSimFD();
565
566#if NO_STAT64
567    int result = ftruncate(sim_fd, length);
568#else
569    int result = ftruncate64(sim_fd, length);
570#endif
571    return (result == -1) ? -errno : result;
572}
573
574SyscallReturn
575umaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
576{
577    // Letting the simulated program change the simulator's umask seems like
578    // a bad idea.  Compromise by just returning the current umask but not
579    // changing anything.
580    mode_t oldMask = umask(0);
581    umask(oldMask);
582    return (int)oldMask;
583}
584
585SyscallReturn
586chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
587{
588    string path;
589
590    int index = 0;
591    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
592        return -EFAULT;
593
594    /* XXX endianess */
595    uint32_t owner = p->getSyscallArg(tc, index);
596    uid_t hostOwner = owner;
597    uint32_t group = p->getSyscallArg(tc, index);
598    gid_t hostGroup = group;
599
600    // Adjust path for current working directory
601    path = p->fullPath(path);
602
603    int result = chown(path.c_str(), hostOwner, hostGroup);
604    return (result == -1) ? -errno : result;
605}
606
607SyscallReturn
608fchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
609{
610    int index = 0;
611    int tgt_fd = p->getSyscallArg(tc, index);
612
613    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
614    if (!ffdp)
615        return -EBADF;
616    int sim_fd = ffdp->getSimFD();
617
618    /* XXX endianess */
619    uint32_t owner = p->getSyscallArg(tc, index);
620    uid_t hostOwner = owner;
621    uint32_t group = p->getSyscallArg(tc, index);
622    gid_t hostGroup = group;
623
624    int result = fchown(sim_fd, hostOwner, hostGroup);
625    return (result == -1) ? -errno : result;
626}
627
628
629/**
630 * TODO: there's a bit more involved here since file descriptors created with
631 * dup are supposed to share a file description. So, there is a problem with
632 * maintaining fields like file offset or flags since an update to such a
633 * field won't be reflected in the metadata for the fd entries that we
634 * maintain to hold metadata for checkpoint restoration.
635 */
636SyscallReturn
637dupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
638{
639    int index = 0;
640    int tgt_fd = p->getSyscallArg(tc, index);
641
642    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
643    if (!old_hbfdp)
644        return -EBADF;
645    int sim_fd = old_hbfdp->getSimFD();
646
647    int result = dup(sim_fd);
648    int local_errno = errno;
649
650    std::shared_ptr<FDEntry> new_fdep = old_hbfdp->clone();
651    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(new_fdep);
652    new_hbfdp->setSimFD(result);
653
654    return (result == -1) ? -local_errno : p->fds->allocFD(new_fdep);
655}
656
657SyscallReturn
658fcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
659{
660    int arg;
661    int index = 0;
662    int tgt_fd = p->getSyscallArg(tc, index);
663    int cmd = p->getSyscallArg(tc, index);
664
665    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
666    if (!hbfdp)
667        return -EBADF;
668    int sim_fd = hbfdp->getSimFD();
669
670    int coe = hbfdp->getCOE();
671
672    switch (cmd) {
673      case F_GETFD:
674        return coe & FD_CLOEXEC;
675
676      case F_SETFD: {
677        arg = p->getSyscallArg(tc, index);
678        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
679        return 0;
680      }
681
682      // Rely on the host to maintain the file status flags for this file
683      // description rather than maintain it ourselves. Admittedly, this
684      // is suboptimal (and possibly error prone), but it is difficult to
685      // maintain the flags by tracking them across the different descriptors
686      // (that refer to this file description) caused by clone, dup, and
687      // subsequent fcntls.
688      case F_GETFL:
689      case F_SETFL: {
690        arg = p->getSyscallArg(tc, index);
691        int rv = fcntl(sim_fd, cmd, arg);
692        return (rv == -1) ? -errno : rv;
693      }
694
695      default:
696        warn("fcntl: unsupported command %d\n", cmd);
697        return 0;
698    }
699}
700
701SyscallReturn
702fcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
703{
704    int index = 0;
705    int tgt_fd = p->getSyscallArg(tc, index);
706
707    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
708    if (!hbfdp)
709        return -EBADF;
710    int sim_fd = hbfdp->getSimFD();
711
712    int cmd = p->getSyscallArg(tc, index);
713    switch (cmd) {
714      case 33: //F_GETLK64
715        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
716        return -EMFILE;
717
718      case 34: // F_SETLK64
719      case 35: // F_SETLKW64
720        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
721             tgt_fd);
722        return -EMFILE;
723
724      default:
725        // not sure if this is totally valid, but we'll pass it through
726        // to the underlying OS
727        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
728        return fcntl(sim_fd, cmd);
729        // return 0;
730    }
731}
732
733SyscallReturn
734pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
735               ThreadContext *tc)
736{
737    int sim_fds[2], tgt_fds[2];
738
739    int pipe_retval = pipe(sim_fds);
740    if (pipe_retval < 0)
741        return pipe_retval;
742
743    auto rend = PipeFDEntry::EndType::read;
744    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
745
746    auto wend = PipeFDEntry::EndType::write;
747    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
748
749    tgt_fds[0] = process->fds->allocFD(rpfd);
750    tgt_fds[1] = process->fds->allocFD(wpfd);
751
752    /**
753     * Now patch the read object to record the target file descriptor chosen
754     * as the write end of the pipe.
755     */
756    rpfd->setPipeReadSource(tgt_fds[1]);
757
758    /**
759     * Alpha Linux convention for pipe() is that fd[0] is returned as
760     * the return value of the function, and fd[1] is returned in r20.
761     */
762    tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
763    return sim_fds[0];
764}
765
766SyscallReturn
767setpgidFunc(SyscallDesc *desc, int callnum, Process *process,
768            ThreadContext *tc)
769{
770    int index = 0;
771    int pid = process->getSyscallArg(tc, index);
772    int pgid = process->getSyscallArg(tc, index);
773
774    if (pgid < 0)
775        return -EINVAL;
776
777    if (pid == 0) {
778        process->setpgid(process->pid());
779        return 0;
780    }
781
782    Process *matched_ph = NULL;
783    System *sysh = tc->getSystemPtr();
784
785    // Retrieves process pointer from active/suspended thread contexts.
786    for (int i = 0; i < sysh->numContexts(); i++) {
787        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
788            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
789            Process *walk_ph = (Process*)temp_h;
790
791            if (walk_ph && walk_ph->pid() == process->pid())
792                matched_ph = walk_ph;
793        }
794    }
795
796    assert(matched_ph != NULL);
797    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
798
799    return 0;
800}
801
802SyscallReturn
803getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
804                 ThreadContext *tc)
805{
806    // Make up a PID.  There's no interprocess communication in
807    // fake_syscall mode, so there's no way for a process to know it's
808    // not getting a unique value.
809
810    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
811    return process->pid();
812}
813
814
815SyscallReturn
816getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
817                 ThreadContext *tc)
818{
819    // Make up a UID and EUID... it shouldn't matter, and we want the
820    // simulation to be deterministic.
821
822    // EUID goes in r20.
823    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
824    return process->uid();              // UID
825}
826
827
828SyscallReturn
829getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
830                 ThreadContext *tc)
831{
832    // Get current group ID.  EGID goes in r20.
833    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
834    return process->gid();
835}
836
837
838SyscallReturn
839setuidFunc(SyscallDesc *desc, int callnum, Process *process,
840           ThreadContext *tc)
841{
842    // can't fathom why a benchmark would call this.
843    int index = 0;
844    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
845    return 0;
846}
847
848SyscallReturn
849getpidFunc(SyscallDesc *desc, int callnum, Process *process,
850           ThreadContext *tc)
851{
852    return process->tgid();
853}
854
855SyscallReturn
856gettidFunc(SyscallDesc *desc, int callnum, Process *process,
857           ThreadContext *tc)
858{
859    return process->pid();
860}
861
862SyscallReturn
863getppidFunc(SyscallDesc *desc, int callnum, Process *process,
864            ThreadContext *tc)
865{
866    return process->ppid();
867}
868
869SyscallReturn
870getuidFunc(SyscallDesc *desc, int callnum, Process *process,
871           ThreadContext *tc)
872{
873    return process->uid();              // UID
874}
875
876SyscallReturn
877geteuidFunc(SyscallDesc *desc, int callnum, Process *process,
878            ThreadContext *tc)
879{
880    return process->euid();             // UID
881}
882
883SyscallReturn
884getgidFunc(SyscallDesc *desc, int callnum, Process *process,
885           ThreadContext *tc)
886{
887    return process->gid();
888}
889
890SyscallReturn
891getegidFunc(SyscallDesc *desc, int callnum, Process *process,
892            ThreadContext *tc)
893{
894    return process->egid();
895}
896
897SyscallReturn
898fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
899{
900#if NO_FALLOCATE
901    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
902#else
903    int index = 0;
904    int tgt_fd = p->getSyscallArg(tc, index);
905    int mode = p->getSyscallArg(tc, index);
906    off_t offset = p->getSyscallArg(tc, index);
907    off_t len = p->getSyscallArg(tc, index);
908
909    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
910    if (!ffdp)
911        return -EBADF;
912    int sim_fd = ffdp->getSimFD();
913
914    int result = fallocate(sim_fd, mode, offset, len);
915    if (result < 0)
916        return -errno;
917#endif
918    return 0;
919}
920
921SyscallReturn
922accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
923           int index)
924{
925    string path;
926    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
927        return -EFAULT;
928
929    // Adjust path for current working directory
930    path = p->fullPath(path);
931
932    mode_t mode = p->getSyscallArg(tc, index);
933
934    int result = access(path.c_str(), mode);
935    return (result == -1) ? -errno : result;
936}
937
938SyscallReturn
939accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
940{
941    return accessFunc(desc, callnum, p, tc, 0);
942}
943
944