syscall_emul.cc revision 11886
1360SN/A/*
21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
3360SN/A * All rights reserved.
4360SN/A *
5360SN/A * Redistribution and use in source and binary forms, with or without
6360SN/A * modification, are permitted provided that the following conditions are
7360SN/A * met: redistributions of source code must retain the above copyright
8360SN/A * notice, this list of conditions and the following disclaimer;
9360SN/A * redistributions in binary form must reproduce the above copyright
10360SN/A * notice, this list of conditions and the following disclaimer in the
11360SN/A * documentation and/or other materials provided with the distribution;
12360SN/A * neither the name of the copyright holders nor the names of its
13360SN/A * contributors may be used to endorse or promote products derived from
14360SN/A * this software without specific prior written permission.
15360SN/A *
16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
30360SN/A */
31360SN/A
3211793Sbrandon.potter@amd.com#include "sim/syscall_emul.hh"
3311793Sbrandon.potter@amd.com
342093SN/A#include <fcntl.h>
35360SN/A#include <unistd.h>
36360SN/A
376712Snate@binkert.org#include <iostream>
38360SN/A#include <string>
39360SN/A
407680Sgblack@eecs.umich.edu#include "arch/utility.hh"
412474SN/A#include "base/chunk_generator.hh"
42360SN/A#include "base/trace.hh"
436658Snate@binkert.org#include "config/the_isa.hh"
442680Sktlim@umich.edu#include "cpu/thread_context.hh"
452474SN/A#include "mem/page_table.hh"
46360SN/A#include "sim/process.hh"
478229Snate@binkert.org#include "sim/sim_exit.hh"
4811794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
4911794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
506029Ssteve.reinhardt@amd.com#include "sim/system.hh"
51360SN/A
52360SN/Ausing namespace std;
532107SN/Ausing namespace TheISA;
54360SN/A
551450SN/ASyscallReturn
5611851Sbrandon.potter@amd.comunimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
572680Sktlim@umich.edu                  ThreadContext *tc)
58360SN/A{
5911794Sbrandon.potter@amd.com    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
602484SN/A
612484SN/A    return 1;
62360SN/A}
63360SN/A
64360SN/A
651450SN/ASyscallReturn
6611851Sbrandon.potter@amd.comignoreFunc(SyscallDesc *desc, int callnum, Process *process,
672680Sktlim@umich.edu           ThreadContext *tc)
68360SN/A{
6911794Sbrandon.potter@amd.com    if (desc->needWarning()) {
7011794Sbrandon.potter@amd.com        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
7111794Sbrandon.potter@amd.com             "\n      (further warnings will be suppressed)" : "");
7210831Ssteve.reinhardt@amd.com    }
73360SN/A
748149SChris.Emmons@ARM.com    return 0;
758149SChris.Emmons@ARM.com}
768149SChris.Emmons@ARM.com
7711886Sbrandon.potter@amd.comstatic void
7811886Sbrandon.potter@amd.comexitFutexWake(ThreadContext *tc, uint64_t uaddr)
7911886Sbrandon.potter@amd.com{
8011886Sbrandon.potter@amd.com    std::map<uint64_t, std::list<ThreadContext *> * >
8111886Sbrandon.potter@amd.com        &futex_map = tc->getSystemPtr()->futexMap;
8211886Sbrandon.potter@amd.com
8311886Sbrandon.potter@amd.com    int wokenUp = 0;
8411886Sbrandon.potter@amd.com    std::list<ThreadContext *> * tcWaitList;
8511886Sbrandon.potter@amd.com    if (futex_map.count(uaddr)) {
8611886Sbrandon.potter@amd.com        tcWaitList = futex_map.find(uaddr)->second;
8711886Sbrandon.potter@amd.com        if (tcWaitList->size() > 0) {
8811886Sbrandon.potter@amd.com            tcWaitList->front()->activate();
8911886Sbrandon.potter@amd.com            tcWaitList->pop_front();
9011886Sbrandon.potter@amd.com            wokenUp++;
9111886Sbrandon.potter@amd.com        }
9211886Sbrandon.potter@amd.com        if (tcWaitList->empty()) {
9311886Sbrandon.potter@amd.com            futex_map.erase(uaddr);
9411886Sbrandon.potter@amd.com            delete tcWaitList;
9511886Sbrandon.potter@amd.com        }
9611886Sbrandon.potter@amd.com    }
9711886Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "exit: FUTEX_WAKE, activated %d waiting "
9811886Sbrandon.potter@amd.com                            "thread contexts\n", wokenUp);
9911886Sbrandon.potter@amd.com}
1008149SChris.Emmons@ARM.com
1018149SChris.Emmons@ARM.comSyscallReturn
10211886Sbrandon.potter@amd.comexitFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
103360SN/A{
10411886Sbrandon.potter@amd.com    if (p->system->numRunningContexts() == 1 && !p->childClearTID) {
10511886Sbrandon.potter@amd.com        // Last running free-parent context; exit simulator.
1066701Sgblack@eecs.umich.edu        int index = 0;
1075958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
10811886Sbrandon.potter@amd.com                    p->getSyscallArg(tc, index) & 0xff);
1096029Ssteve.reinhardt@amd.com    } else {
11011886Sbrandon.potter@amd.com        if (p->childClearTID)
11111886Sbrandon.potter@amd.com            exitFutexWake(tc, p->childClearTID);
1126029Ssteve.reinhardt@amd.com        tc->halt();
1132834Sksewell@umich.edu    }
114360SN/A
1151458SN/A    return 1;
116360SN/A}
117360SN/A
118360SN/A
1191450SN/ASyscallReturn
12011851Sbrandon.potter@amd.comexitGroupFunc(SyscallDesc *desc, int callnum, Process *process,
1216109Ssanchezd@stanford.edu              ThreadContext *tc)
1226109Ssanchezd@stanford.edu{
12310483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
12410483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
12510483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
12610483Swiseveri@student.ethz.ch    }
12710483Swiseveri@student.ethz.ch
12810483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
12910483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
13010483Swiseveri@student.ethz.ch        int index = 0;
13110483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
13210483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
13310483Swiseveri@student.ethz.ch    }
1346109Ssanchezd@stanford.edu
1356109Ssanchezd@stanford.edu    return 1;
1366109Ssanchezd@stanford.edu}
1376109Ssanchezd@stanford.edu
1386109Ssanchezd@stanford.edu
1396109Ssanchezd@stanford.eduSyscallReturn
14011851Sbrandon.potter@amd.comgetpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
141360SN/A{
14210318Sandreas.hansson@arm.com    return (int)PageBytes;
143360SN/A}
144360SN/A
145360SN/A
1461450SN/ASyscallReturn
14711851Sbrandon.potter@amd.combrkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
148360SN/A{
149360SN/A    // change brk addr to first arg
1506701Sgblack@eecs.umich.edu    int index = 0;
1516701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1525748SSteve.Reinhardt@amd.com
1535748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1545748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1555748SSteve.Reinhardt@amd.com    if (new_brk == 0)
15611886Sbrandon.potter@amd.com        return p->memState->brkPoint;
1575748SSteve.Reinhardt@amd.com
15811886Sbrandon.potter@amd.com    if (new_brk > p->memState->brkPoint) {
1595748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
16011886Sbrandon.potter@amd.com        for (ChunkGenerator gen(p->memState->brkPoint,
16111886Sbrandon.potter@amd.com                                new_brk - p->memState->brkPoint,
16210318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1635748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
16410318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1656687Stjones1@inf.ed.ac.uk
1666687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1676687Stjones1@inf.ed.ac.uk            else {
1686687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1698852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1706687Stjones1@inf.ed.ac.uk
1716687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
17210318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1736687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1748852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
17510318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1766687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1776687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1786687Stjones1@inf.ed.ac.uk                {
17910318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1808852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1816687Stjones1@inf.ed.ac.uk                }
1826687Stjones1@inf.ed.ac.uk            }
1832474SN/A        }
1841450SN/A    }
1855748SSteve.Reinhardt@amd.com
18611886Sbrandon.potter@amd.com    p->memState->brkPoint = new_brk;
18711380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
18811886Sbrandon.potter@amd.com                    p->memState->brkPoint);
18911886Sbrandon.potter@amd.com    return p->memState->brkPoint;
190360SN/A}
191360SN/A
19211886Sbrandon.potter@amd.comSyscallReturn
19311886Sbrandon.potter@amd.comsetTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
19411886Sbrandon.potter@amd.com                  ThreadContext *tc)
19511886Sbrandon.potter@amd.com{
19611886Sbrandon.potter@amd.com    int index = 0;
19711886Sbrandon.potter@amd.com    uint64_t tidPtr = process->getSyscallArg(tc, index);
19811886Sbrandon.potter@amd.com
19911886Sbrandon.potter@amd.com    process->childClearTID = tidPtr;
20011886Sbrandon.potter@amd.com    return process->pid();
20111886Sbrandon.potter@amd.com}
202360SN/A
2031450SN/ASyscallReturn
20411851Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
205360SN/A{
2066701Sgblack@eecs.umich.edu    int index = 0;
20710931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
20810931Sbrandon.potter@amd.com
20911856Sbrandon.potter@amd.com    return p->fds->closeFDEntry(tgt_fd);
210360SN/A}
211360SN/A
212360SN/A
2131450SN/ASyscallReturn
21411851Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
215360SN/A{
2166701Sgblack@eecs.umich.edu    int index = 0;
21710931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2186701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2196701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
22011856Sbrandon.potter@amd.com
22111856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
22211856Sbrandon.potter@amd.com    if (!hbfdp)
22311856Sbrandon.potter@amd.com        return -EBADF;
22411856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
22511856Sbrandon.potter@amd.com
2266701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
22710931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
228360SN/A
22911684Snderumigny@gmail.com    if (bytes_read > 0)
2308706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
231360SN/A
2321458SN/A    return bytes_read;
233360SN/A}
234360SN/A
2351450SN/ASyscallReturn
23611851Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
237360SN/A{
2386701Sgblack@eecs.umich.edu    int index = 0;
23910931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2406701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2416701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
24211856Sbrandon.potter@amd.com
24311856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
24411856Sbrandon.potter@amd.com    if (!hbfdp)
24511856Sbrandon.potter@amd.com        return -EBADF;
24611856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
24711856Sbrandon.potter@amd.com
2486701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
2498706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
250360SN/A
25110931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
252360SN/A
25310931Sbrandon.potter@amd.com    fsync(sim_fd);
254360SN/A
2551458SN/A    return bytes_written;
256360SN/A}
257360SN/A
258360SN/A
2591450SN/ASyscallReturn
26011851Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
261360SN/A{
2626701Sgblack@eecs.umich.edu    int index = 0;
26310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2646701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2656701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
266360SN/A
26711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
26811856Sbrandon.potter@amd.com    if (!ffdp)
26910931Sbrandon.potter@amd.com        return -EBADF;
27011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
27110931Sbrandon.potter@amd.com
27210931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
273360SN/A
2741458SN/A    return (result == (off_t)-1) ? -errno : result;
275360SN/A}
276360SN/A
277360SN/A
2781450SN/ASyscallReturn
27911851Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2804118Sgblack@eecs.umich.edu{
2816701Sgblack@eecs.umich.edu    int index = 0;
28210931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2836701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2846701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2856701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2866701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2874118Sgblack@eecs.umich.edu
28811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
28911856Sbrandon.potter@amd.com    if (!ffdp)
29010931Sbrandon.potter@amd.com        return -EBADF;
29111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
29210931Sbrandon.potter@amd.com
2934118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2944118Sgblack@eecs.umich.edu
29510931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
2964118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2974118Sgblack@eecs.umich.edu
29811379Sbrandon.potter@amd.com    if (result == (off_t)-1)
2994118Sgblack@eecs.umich.edu        return -errno;
30011379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
30111379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
30211379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
30311379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
30411379Sbrandon.potter@amd.com    return 0;
3054118Sgblack@eecs.umich.edu}
3064118Sgblack@eecs.umich.edu
3074118Sgblack@eecs.umich.edu
3084118Sgblack@eecs.umich.eduSyscallReturn
30911851Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
310360SN/A{
31111383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
31211383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
31311383Sbrandon.potter@amd.com    // memory.
3141458SN/A    return 0;
315360SN/A}
316360SN/A
317360SN/A
318360SN/Aconst char *hostname = "m5.eecs.umich.edu";
319360SN/A
3201450SN/ASyscallReturn
32111851Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
322360SN/A{
3236701Sgblack@eecs.umich.edu    int index = 0;
3246701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3256701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3266701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
327360SN/A
328360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
329360SN/A
3308706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
331360SN/A
3321458SN/A    return 0;
333360SN/A}
334360SN/A
3351450SN/ASyscallReturn
33611851Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3375513SMichael.Adler@intel.com{
3385513SMichael.Adler@intel.com    int result = 0;
3396731Svince@csl.cornell.edu    int index = 0;
3406701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3416701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3426701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3435513SMichael.Adler@intel.com
3445513SMichael.Adler@intel.com    // Is current working directory defined?
3455513SMichael.Adler@intel.com    string cwd = p->getcwd();
3465513SMichael.Adler@intel.com    if (!cwd.empty()) {
3475513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3485513SMichael.Adler@intel.com            // Buffer too small
3495513SMichael.Adler@intel.com            return -ERANGE;
3505513SMichael.Adler@intel.com        }
3515513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3525513SMichael.Adler@intel.com        result = cwd.length();
35310955Sdavid.hashe@amd.com    } else {
35411856Sbrandon.potter@amd.com        if (getcwd((char *)buf.bufferPtr(), size)) {
3555513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
35610955Sdavid.hashe@amd.com        } else {
3575513SMichael.Adler@intel.com            result = -1;
3585513SMichael.Adler@intel.com        }
3595513SMichael.Adler@intel.com    }
3605513SMichael.Adler@intel.com
3618706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3625513SMichael.Adler@intel.com
3635513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3645513SMichael.Adler@intel.com}
3655513SMichael.Adler@intel.com
36610203SAli.Saidi@ARM.com/// Target open() handler.
36710203SAli.Saidi@ARM.comSyscallReturn
36811851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
36911851Sbrandon.potter@amd.com             ThreadContext *tc)
37010203SAli.Saidi@ARM.com{
37110203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
37210203SAli.Saidi@ARM.com}
3735513SMichael.Adler@intel.com
3745513SMichael.Adler@intel.comSyscallReturn
37511851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
37611851Sbrandon.potter@amd.com             int index)
3775513SMichael.Adler@intel.com{
3785513SMichael.Adler@intel.com    string path;
3795513SMichael.Adler@intel.com
3808852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
38110223Ssteve.reinhardt@amd.com        return -EFAULT;
3825513SMichael.Adler@intel.com
3835513SMichael.Adler@intel.com    // Adjust path for current working directory
3845513SMichael.Adler@intel.com    path = p->fullPath(path);
3855513SMichael.Adler@intel.com
3866701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3876701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3886701Sgblack@eecs.umich.edu
3896701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3905513SMichael.Adler@intel.com
39110955Sdavid.hashe@amd.com    int result = -1;
39210955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
39310955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
39410955Sdavid.hashe@amd.com    } else {
39511140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
39611140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
39711851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
39811140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
39911140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
40011140Sjthestness@gmail.com        //     called binary calls readlink().
40111140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
40211140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
40311140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
40411140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
40511140Sjthestness@gmail.com
40611140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
40711140Sjthestness@gmail.com        char real_path[PATH_MAX];
40811140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
40911140Sjthestness@gmail.com        if (!check_real_path) {
41011140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
41111140Sjthestness@gmail.com                  "executable: %s", p->progName());
41211140Sjthestness@gmail.com        }
41311140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
41411140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
41511140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
41610955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
41710955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
41810955Sdavid.hashe@amd.com            result = bufsiz;
41910955Sdavid.hashe@amd.com        } else {
42011140Sjthestness@gmail.com            result = real_path_len;
42110955Sdavid.hashe@amd.com        }
42211140Sjthestness@gmail.com
42311140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
42411140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
42511140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
42611140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
42710955Sdavid.hashe@amd.com    }
4285513SMichael.Adler@intel.com
4298706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4305513SMichael.Adler@intel.com
4315513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4325513SMichael.Adler@intel.com}
4335513SMichael.Adler@intel.com
4345513SMichael.Adler@intel.comSyscallReturn
43511851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
436511SN/A{
43710633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
43810633Smichaelupton@gmail.com}
43910633Smichaelupton@gmail.com
44010633Smichaelupton@gmail.comSyscallReturn
44111851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
44211851Sbrandon.potter@amd.com             int index)
44310633Smichaelupton@gmail.com{
4441706SN/A    string path;
445360SN/A
4468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
44710223Ssteve.reinhardt@amd.com        return -EFAULT;
448511SN/A
4493669Sbinkertn@umich.edu    // Adjust path for current working directory
4503669Sbinkertn@umich.edu    path = p->fullPath(path);
4513669Sbinkertn@umich.edu
452511SN/A    int result = unlink(path.c_str());
4531458SN/A    return (result == -1) ? -errno : result;
454511SN/A}
455511SN/A
4565513SMichael.Adler@intel.com
4575513SMichael.Adler@intel.comSyscallReturn
45811851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4595513SMichael.Adler@intel.com{
4605513SMichael.Adler@intel.com    string path;
4615513SMichael.Adler@intel.com
4626701Sgblack@eecs.umich.edu    int index = 0;
4638852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46410223Ssteve.reinhardt@amd.com        return -EFAULT;
4655513SMichael.Adler@intel.com
4665513SMichael.Adler@intel.com    // Adjust path for current working directory
4675513SMichael.Adler@intel.com    path = p->fullPath(path);
4685513SMichael.Adler@intel.com
4696701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4705513SMichael.Adler@intel.com
4715513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4725513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4735513SMichael.Adler@intel.com}
4745513SMichael.Adler@intel.com
4751450SN/ASyscallReturn
47611851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
477511SN/A{
4781706SN/A    string old_name;
479511SN/A
4806701Sgblack@eecs.umich.edu    int index = 0;
4818852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4821458SN/A        return -EFAULT;
483511SN/A
4841706SN/A    string new_name;
485511SN/A
4868852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4871458SN/A        return -EFAULT;
488511SN/A
4893669Sbinkertn@umich.edu    // Adjust path for current working directory
4903669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4913669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4923669Sbinkertn@umich.edu
4931706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4941458SN/A    return (result == -1) ? -errno : result;
495511SN/A}
496511SN/A
4971706SN/ASyscallReturn
49811851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4991706SN/A{
5001706SN/A    string path;
5011706SN/A
5026701Sgblack@eecs.umich.edu    int index = 0;
5038852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5041706SN/A        return -EFAULT;
5051706SN/A
5066701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5071706SN/A
5083669Sbinkertn@umich.edu    // Adjust path for current working directory
5093669Sbinkertn@umich.edu    path = p->fullPath(path);
5103669Sbinkertn@umich.edu
5111706SN/A    int result = truncate(path.c_str(), length);
5121706SN/A    return (result == -1) ? -errno : result;
5131706SN/A}
5141706SN/A
5151706SN/ASyscallReturn
51611856Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5171706SN/A{
5186701Sgblack@eecs.umich.edu    int index = 0;
51911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
52011856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
5211706SN/A
52211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
52311856Sbrandon.potter@amd.com    if (!ffdp)
5241706SN/A        return -EBADF;
52511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5261706SN/A
52710931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5281706SN/A    return (result == -1) ? -errno : result;
5291706SN/A}
5301999SN/A
5311999SN/ASyscallReturn
5326703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
53311851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
5346703Svince@csl.cornell.edu{
5356703Svince@csl.cornell.edu    int index = 0;
5366703Svince@csl.cornell.edu    string path;
5376703Svince@csl.cornell.edu
5388852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5396703Svince@csl.cornell.edu       return -EFAULT;
5406703Svince@csl.cornell.edu
5416744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5426703Svince@csl.cornell.edu
5436703Svince@csl.cornell.edu    // Adjust path for current working directory
5446703Svince@csl.cornell.edu    path = process->fullPath(path);
5456703Svince@csl.cornell.edu
5466744SAli.Saidi@arm.com#if NO_STAT64
5476744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5486744SAli.Saidi@arm.com#else
5496703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5506744SAli.Saidi@arm.com#endif
5516703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5526703Svince@csl.cornell.edu}
5536703Svince@csl.cornell.edu
5546703Svince@csl.cornell.eduSyscallReturn
55511856Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5566685Stjones1@inf.ed.ac.uk{
5576701Sgblack@eecs.umich.edu    int index = 0;
55811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
55911856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
5606685Stjones1@inf.ed.ac.uk
56111856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
56211856Sbrandon.potter@amd.com    if (!ffdp)
5636685Stjones1@inf.ed.ac.uk        return -EBADF;
56411856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5656685Stjones1@inf.ed.ac.uk
5666744SAli.Saidi@arm.com#if NO_STAT64
56710931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5686744SAli.Saidi@arm.com#else
56910931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5706744SAli.Saidi@arm.com#endif
5716685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5726685Stjones1@inf.ed.ac.uk}
5736685Stjones1@inf.ed.ac.uk
5746685Stjones1@inf.ed.ac.ukSyscallReturn
57511851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5765513SMichael.Adler@intel.com{
5775513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5785513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5795513SMichael.Adler@intel.com    // changing anything.
5805513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5815513SMichael.Adler@intel.com    umask(oldMask);
5825521Snate@binkert.org    return (int)oldMask;
5835513SMichael.Adler@intel.com}
5845513SMichael.Adler@intel.com
5855513SMichael.Adler@intel.comSyscallReturn
58611851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5871999SN/A{
5881999SN/A    string path;
5891999SN/A
5906701Sgblack@eecs.umich.edu    int index = 0;
5918852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5921999SN/A        return -EFAULT;
5931999SN/A
5941999SN/A    /* XXX endianess */
5956701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5961999SN/A    uid_t hostOwner = owner;
5976701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5981999SN/A    gid_t hostGroup = group;
5991999SN/A
6003669Sbinkertn@umich.edu    // Adjust path for current working directory
6013669Sbinkertn@umich.edu    path = p->fullPath(path);
6023669Sbinkertn@umich.edu
6031999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6041999SN/A    return (result == -1) ? -errno : result;
6051999SN/A}
6061999SN/A
6071999SN/ASyscallReturn
60811856Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6091999SN/A{
6106701Sgblack@eecs.umich.edu    int index = 0;
61111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6121999SN/A
61311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
61411856Sbrandon.potter@amd.com    if (!ffdp)
6151999SN/A        return -EBADF;
61611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6171999SN/A
6181999SN/A    /* XXX endianess */
61911856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
6201999SN/A    uid_t hostOwner = owner;
62111856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
6221999SN/A    gid_t hostGroup = group;
6231999SN/A
62410931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6251999SN/A    return (result == -1) ? -errno : result;
6261999SN/A}
6272093SN/A
6282093SN/A
62911856Sbrandon.potter@amd.com/**
63011856Sbrandon.potter@amd.com * TODO: there's a bit more involved here since file descriptors created with
63111856Sbrandon.potter@amd.com * dup are supposed to share a file description. So, there is a problem with
63211856Sbrandon.potter@amd.com * maintaining fields like file offset or flags since an update to such a
63311856Sbrandon.potter@amd.com * field won't be reflected in the metadata for the fd entries that we
63411856Sbrandon.potter@amd.com * maintain to hold metadata for checkpoint restoration.
63511856Sbrandon.potter@amd.com */
6362093SN/ASyscallReturn
63711856Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6383079Sstever@eecs.umich.edu{
6396701Sgblack@eecs.umich.edu    int index = 0;
64011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
64110931Sbrandon.potter@amd.com
64211856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
64311856Sbrandon.potter@amd.com    if (!old_hbfdp)
6443079Sstever@eecs.umich.edu        return -EBADF;
64511856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
6465282Srstrong@cs.ucsd.edu
64710781Snilay@cs.wisc.edu    int result = dup(sim_fd);
64811856Sbrandon.potter@amd.com    int local_errno = errno;
64911856Sbrandon.potter@amd.com
65011856Sbrandon.potter@amd.com    std::shared_ptr<FDEntry> new_fdep = old_hbfdp->clone();
65111856Sbrandon.potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(new_fdep);
65211856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
65311856Sbrandon.potter@amd.com
65411856Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : p->fds->allocFD(new_fdep);
6553079Sstever@eecs.umich.edu}
6563079Sstever@eecs.umich.edu
6573079Sstever@eecs.umich.eduSyscallReturn
65811856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6592093SN/A{
66011875Sbrandon.potter@amd.com    int arg;
6616701Sgblack@eecs.umich.edu    int index = 0;
66211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
66311875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6642093SN/A
66511856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
66611856Sbrandon.potter@amd.com    if (!hbfdp)
6672093SN/A        return -EBADF;
66811856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6692093SN/A
67011875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
67111875Sbrandon.potter@amd.com
6722093SN/A    switch (cmd) {
67311875Sbrandon.potter@amd.com      case F_GETFD:
67411875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
6752093SN/A
67611875Sbrandon.potter@amd.com      case F_SETFD: {
67711875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
67811875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
6792093SN/A        return 0;
68011875Sbrandon.potter@amd.com      }
6812093SN/A
68211875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
68311875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
68411875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
68511875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
68611875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
68711875Sbrandon.potter@amd.com      // subsequent fcntls.
68811875Sbrandon.potter@amd.com      case F_GETFL:
68911875Sbrandon.potter@amd.com      case F_SETFL: {
69011875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
69111875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
69211875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
69311875Sbrandon.potter@amd.com      }
6942093SN/A
6952093SN/A      default:
69611875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
6972093SN/A        return 0;
6982093SN/A    }
6992093SN/A}
7002093SN/A
7012238SN/ASyscallReturn
70211856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
7032687Sksewell@umich.edu{
7046701Sgblack@eecs.umich.edu    int index = 0;
70511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
7062687Sksewell@umich.edu
70711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
70811856Sbrandon.potter@amd.com    if (!hbfdp)
7092687Sksewell@umich.edu        return -EBADF;
71011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
7112687Sksewell@umich.edu
71211856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
7132687Sksewell@umich.edu    switch (cmd) {
7142687Sksewell@umich.edu      case 33: //F_GETLK64
71510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7162687Sksewell@umich.edu        return -EMFILE;
7172687Sksewell@umich.edu
7182687Sksewell@umich.edu      case 34: // F_SETLK64
7192687Sksewell@umich.edu      case 35: // F_SETLKW64
72010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
72110931Sbrandon.potter@amd.com             tgt_fd);
7222687Sksewell@umich.edu        return -EMFILE;
7232687Sksewell@umich.edu
7242687Sksewell@umich.edu      default:
7252687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7262687Sksewell@umich.edu        // to the underlying OS
72710931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
72810931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7292687Sksewell@umich.edu        // return 0;
7302687Sksewell@umich.edu    }
7312687Sksewell@umich.edu}
7322687Sksewell@umich.edu
7332687Sksewell@umich.eduSyscallReturn
73411851Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
73511851Sbrandon.potter@amd.com               ThreadContext *tc)
7362238SN/A{
73711856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
7382093SN/A
73911856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
74011856Sbrandon.potter@amd.com    if (pipe_retval < 0)
7412238SN/A        return pipe_retval;
7422238SN/A
74311856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
74411856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
7452238SN/A
74611856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
74711856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
74811856Sbrandon.potter@amd.com
74911856Sbrandon.potter@amd.com    tgt_fds[0] = process->fds->allocFD(rpfd);
75011856Sbrandon.potter@amd.com    tgt_fds[1] = process->fds->allocFD(wpfd);
75111856Sbrandon.potter@amd.com
75211856Sbrandon.potter@amd.com    /**
75311856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
75411856Sbrandon.potter@amd.com     * as the write end of the pipe.
75511856Sbrandon.potter@amd.com     */
75611856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
75711856Sbrandon.potter@amd.com
75811856Sbrandon.potter@amd.com    /**
75911856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
76011856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
76111856Sbrandon.potter@amd.com     */
76211856Sbrandon.potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
7632238SN/A    return sim_fds[0];
7642238SN/A}
7652238SN/A
76611885Sbrandon.potter@amd.comSyscallReturn
76711885Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, Process *process,
76811885Sbrandon.potter@amd.com            ThreadContext *tc)
76911885Sbrandon.potter@amd.com{
77011885Sbrandon.potter@amd.com    int index = 0;
77111885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
77211885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
77311885Sbrandon.potter@amd.com
77411885Sbrandon.potter@amd.com    if (pgid < 0)
77511885Sbrandon.potter@amd.com        return -EINVAL;
77611885Sbrandon.potter@amd.com
77711885Sbrandon.potter@amd.com    if (pid == 0) {
77811885Sbrandon.potter@amd.com        process->setpgid(process->pid());
77911885Sbrandon.potter@amd.com        return 0;
78011885Sbrandon.potter@amd.com    }
78111885Sbrandon.potter@amd.com
78211885Sbrandon.potter@amd.com    Process *matched_ph = NULL;
78311885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
78411885Sbrandon.potter@amd.com
78511885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
78611885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
78711885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
78811885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
78911885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
79011885Sbrandon.potter@amd.com
79111885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
79211885Sbrandon.potter@amd.com                matched_ph = walk_ph;
79311885Sbrandon.potter@amd.com        }
79411885Sbrandon.potter@amd.com    }
79511885Sbrandon.potter@amd.com
79611885Sbrandon.potter@amd.com    assert(matched_ph != NULL);
79711885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
79811885Sbrandon.potter@amd.com
79911885Sbrandon.potter@amd.com    return 0;
80011885Sbrandon.potter@amd.com}
8012238SN/A
8022238SN/ASyscallReturn
80311851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
80411851Sbrandon.potter@amd.com                 ThreadContext *tc)
8052238SN/A{
8062238SN/A    // Make up a PID.  There's no interprocess communication in
8072238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8082238SN/A    // not getting a unique value.
8092238SN/A
8103114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
8113114Sgblack@eecs.umich.edu    return process->pid();
8122238SN/A}
8132238SN/A
8142238SN/A
8152238SN/ASyscallReturn
81611851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
81711851Sbrandon.potter@amd.com                 ThreadContext *tc)
8182238SN/A{
8192238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
8202238SN/A    // simulation to be deterministic.
8212238SN/A
8222238SN/A    // EUID goes in r20.
8233114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
8245543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8252238SN/A}
8262238SN/A
8272238SN/A
8282238SN/ASyscallReturn
82911851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
83011851Sbrandon.potter@amd.com                 ThreadContext *tc)
8312238SN/A{
8322238SN/A    // Get current group ID.  EGID goes in r20.
8333114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
8343114Sgblack@eecs.umich.edu    return process->gid();
8352238SN/A}
8362238SN/A
8372238SN/A
8382238SN/ASyscallReturn
83911851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8402680Sktlim@umich.edu           ThreadContext *tc)
8412238SN/A{
8422238SN/A    // can't fathom why a benchmark would call this.
8436701Sgblack@eecs.umich.edu    int index = 0;
8446701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8452238SN/A    return 0;
8462238SN/A}
8472238SN/A
8482238SN/ASyscallReturn
84911851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
8502680Sktlim@umich.edu           ThreadContext *tc)
8512238SN/A{
85211885Sbrandon.potter@amd.com    return process->tgid();
85311885Sbrandon.potter@amd.com}
8542238SN/A
85511885Sbrandon.potter@amd.comSyscallReturn
85611885Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, Process *process,
85711885Sbrandon.potter@amd.com           ThreadContext *tc)
85811885Sbrandon.potter@amd.com{
8593114Sgblack@eecs.umich.edu    return process->pid();
8602238SN/A}
8612238SN/A
8622238SN/ASyscallReturn
86311851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
86411851Sbrandon.potter@amd.com            ThreadContext *tc)
8652238SN/A{
8663114Sgblack@eecs.umich.edu    return process->ppid();
8672238SN/A}
8682238SN/A
8692238SN/ASyscallReturn
87011851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8712680Sktlim@umich.edu           ThreadContext *tc)
8722238SN/A{
8735543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8742238SN/A}
8752238SN/A
8762238SN/ASyscallReturn
87711851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
87811851Sbrandon.potter@amd.com            ThreadContext *tc)
8792238SN/A{
8805543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8812238SN/A}
8822238SN/A
8832238SN/ASyscallReturn
88411851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
8852680Sktlim@umich.edu           ThreadContext *tc)
8862238SN/A{
8873114Sgblack@eecs.umich.edu    return process->gid();
8882238SN/A}
8892238SN/A
8902238SN/ASyscallReturn
89111851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
89211851Sbrandon.potter@amd.com            ThreadContext *tc)
8932238SN/A{
8943114Sgblack@eecs.umich.edu    return process->egid();
8952238SN/A}
8962238SN/A
8979455Smitch.hayenga+gem5@gmail.comSyscallReturn
89811856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
89911760Sbrandon.potter@amd.com{
90011799Sbrandon.potter@amd.com#if NO_FALLOCATE
90111799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
90211799Sbrandon.potter@amd.com#else
90311760Sbrandon.potter@amd.com    int index = 0;
90411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
90511856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
90611856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
90711856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
90811760Sbrandon.potter@amd.com
90911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
91011856Sbrandon.potter@amd.com    if (!ffdp)
91111760Sbrandon.potter@amd.com        return -EBADF;
91211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
91311760Sbrandon.potter@amd.com
91411760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
91511760Sbrandon.potter@amd.com    if (result < 0)
91611760Sbrandon.potter@amd.com        return -errno;
91711799Sbrandon.potter@amd.com#endif
91811760Sbrandon.potter@amd.com    return 0;
91911760Sbrandon.potter@amd.com}
92011760Sbrandon.potter@amd.com
92111760Sbrandon.potter@amd.comSyscallReturn
92211851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
92311851Sbrandon.potter@amd.com           int index)
9249455Smitch.hayenga+gem5@gmail.com{
9259455Smitch.hayenga+gem5@gmail.com    string path;
9269455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
92710223Ssteve.reinhardt@amd.com        return -EFAULT;
9289455Smitch.hayenga+gem5@gmail.com
9299455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9309455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9319455Smitch.hayenga+gem5@gmail.com
9329455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9339455Smitch.hayenga+gem5@gmail.com
9349455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9359455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9369455Smitch.hayenga+gem5@gmail.com}
93710203SAli.Saidi@ARM.com
93810203SAli.Saidi@ARM.comSyscallReturn
93911851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
94010203SAli.Saidi@ARM.com{
94110203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
94210203SAli.Saidi@ARM.com}
94310203SAli.Saidi@ARM.com
944