syscall_emul.cc revision 11906
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
15311905SBrandon.Potter@amd.com    std::shared_ptr<MemState> mem_state = p->memState;
15411905SBrandon.Potter@amd.com    Addr brk_point = mem_state->getBrkPoint();
15511905SBrandon.Potter@amd.com
1565748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1575748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1585748SSteve.Reinhardt@amd.com    if (new_brk == 0)
15911905SBrandon.Potter@amd.com        return brk_point;
1605748SSteve.Reinhardt@amd.com
16111905SBrandon.Potter@amd.com    if (new_brk > brk_point) {
1625748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
16311905SBrandon.Potter@amd.com        for (ChunkGenerator gen(brk_point,
16411905SBrandon.Potter@amd.com                                new_brk - brk_point,
16510318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1665748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
16710318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1686687Stjones1@inf.ed.ac.uk
1696687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1706687Stjones1@inf.ed.ac.uk            else {
17111905SBrandon.Potter@amd.com                uint8_t zero = 0;
1728852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1736687Stjones1@inf.ed.ac.uk
1746687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
17510318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1766687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1778852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
17810318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1796687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
18011906SBrandon.Potter@amd.com                    p->pTable->translate(next_page)) {
18110318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1828852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1836687Stjones1@inf.ed.ac.uk                }
1846687Stjones1@inf.ed.ac.uk            }
1852474SN/A        }
1861450SN/A    }
1875748SSteve.Reinhardt@amd.com
18811905SBrandon.Potter@amd.com    mem_state->setBrkPoint(new_brk);
18911380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
19011905SBrandon.Potter@amd.com                    mem_state->getBrkPoint());
19111905SBrandon.Potter@amd.com    return mem_state->getBrkPoint();
192360SN/A}
193360SN/A
19411886Sbrandon.potter@amd.comSyscallReturn
19511886Sbrandon.potter@amd.comsetTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
19611886Sbrandon.potter@amd.com                  ThreadContext *tc)
19711886Sbrandon.potter@amd.com{
19811886Sbrandon.potter@amd.com    int index = 0;
19911886Sbrandon.potter@amd.com    uint64_t tidPtr = process->getSyscallArg(tc, index);
20011886Sbrandon.potter@amd.com
20111886Sbrandon.potter@amd.com    process->childClearTID = tidPtr;
20211886Sbrandon.potter@amd.com    return process->pid();
20311886Sbrandon.potter@amd.com}
204360SN/A
2051450SN/ASyscallReturn
20611851Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
207360SN/A{
2086701Sgblack@eecs.umich.edu    int index = 0;
20910931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
21010931Sbrandon.potter@amd.com
21111856Sbrandon.potter@amd.com    return p->fds->closeFDEntry(tgt_fd);
212360SN/A}
213360SN/A
214360SN/A
2151450SN/ASyscallReturn
21611851Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
217360SN/A{
2186701Sgblack@eecs.umich.edu    int index = 0;
21910931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
22011906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
2216701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
22211856Sbrandon.potter@amd.com
22311856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
22411856Sbrandon.potter@amd.com    if (!hbfdp)
22511856Sbrandon.potter@amd.com        return -EBADF;
22611856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
22711856Sbrandon.potter@amd.com
22811906SBrandon.Potter@amd.com    BufferArg bufArg(buf_ptr, nbytes);
22910931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
230360SN/A
23111684Snderumigny@gmail.com    if (bytes_read > 0)
2328706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
233360SN/A
2341458SN/A    return bytes_read;
235360SN/A}
236360SN/A
2371450SN/ASyscallReturn
23811851Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
239360SN/A{
2406701Sgblack@eecs.umich.edu    int index = 0;
24110931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
24211906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
2436701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
24411856Sbrandon.potter@amd.com
24511856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
24611856Sbrandon.potter@amd.com    if (!hbfdp)
24711856Sbrandon.potter@amd.com        return -EBADF;
24811856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
24911856Sbrandon.potter@amd.com
25011906SBrandon.Potter@amd.com    BufferArg bufArg(buf_ptr, nbytes);
2518706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
252360SN/A
25310931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
254360SN/A
25510931Sbrandon.potter@amd.com    fsync(sim_fd);
256360SN/A
2571458SN/A    return bytes_written;
258360SN/A}
259360SN/A
260360SN/A
2611450SN/ASyscallReturn
26211851Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
263360SN/A{
2646701Sgblack@eecs.umich.edu    int index = 0;
26510931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2666701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2676701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
268360SN/A
26911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
27011856Sbrandon.potter@amd.com    if (!ffdp)
27110931Sbrandon.potter@amd.com        return -EBADF;
27211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
27310931Sbrandon.potter@amd.com
27410931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
275360SN/A
2761458SN/A    return (result == (off_t)-1) ? -errno : result;
277360SN/A}
278360SN/A
279360SN/A
2801450SN/ASyscallReturn
28111851Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2824118Sgblack@eecs.umich.edu{
2836701Sgblack@eecs.umich.edu    int index = 0;
28410931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2856701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2866701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2876701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2886701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2894118Sgblack@eecs.umich.edu
29011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
29111856Sbrandon.potter@amd.com    if (!ffdp)
29210931Sbrandon.potter@amd.com        return -EBADF;
29311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
29410931Sbrandon.potter@amd.com
2954118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2964118Sgblack@eecs.umich.edu
29710931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
2984118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2994118Sgblack@eecs.umich.edu
30011379Sbrandon.potter@amd.com    if (result == (off_t)-1)
3014118Sgblack@eecs.umich.edu        return -errno;
30211379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
30311379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
30411379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
30511379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
30611379Sbrandon.potter@amd.com    return 0;
3074118Sgblack@eecs.umich.edu}
3084118Sgblack@eecs.umich.edu
3094118Sgblack@eecs.umich.edu
3104118Sgblack@eecs.umich.eduSyscallReturn
31111851Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
312360SN/A{
31311383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
31411383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
31511383Sbrandon.potter@amd.com    // memory.
3161458SN/A    return 0;
317360SN/A}
318360SN/A
319360SN/A
320360SN/Aconst char *hostname = "m5.eecs.umich.edu";
321360SN/A
3221450SN/ASyscallReturn
32311851Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
324360SN/A{
3256701Sgblack@eecs.umich.edu    int index = 0;
32611906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3276701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
32811906SBrandon.Potter@amd.com    BufferArg name(buf_ptr, name_len);
329360SN/A
330360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
331360SN/A
3328706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
333360SN/A
3341458SN/A    return 0;
335360SN/A}
336360SN/A
3371450SN/ASyscallReturn
33811851Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3395513SMichael.Adler@intel.com{
3405513SMichael.Adler@intel.com    int result = 0;
3416731Svince@csl.cornell.edu    int index = 0;
34211906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3436701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
34411906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, size);
3455513SMichael.Adler@intel.com
3465513SMichael.Adler@intel.com    // Is current working directory defined?
3475513SMichael.Adler@intel.com    string cwd = p->getcwd();
3485513SMichael.Adler@intel.com    if (!cwd.empty()) {
3495513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3505513SMichael.Adler@intel.com            // Buffer too small
3515513SMichael.Adler@intel.com            return -ERANGE;
3525513SMichael.Adler@intel.com        }
3535513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3545513SMichael.Adler@intel.com        result = cwd.length();
35510955Sdavid.hashe@amd.com    } else {
35611856Sbrandon.potter@amd.com        if (getcwd((char *)buf.bufferPtr(), size)) {
3575513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
35810955Sdavid.hashe@amd.com        } else {
3595513SMichael.Adler@intel.com            result = -1;
3605513SMichael.Adler@intel.com        }
3615513SMichael.Adler@intel.com    }
3625513SMichael.Adler@intel.com
3638706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3645513SMichael.Adler@intel.com
3655513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3665513SMichael.Adler@intel.com}
3675513SMichael.Adler@intel.com
36810203SAli.Saidi@ARM.com/// Target open() handler.
36910203SAli.Saidi@ARM.comSyscallReturn
37011851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
37111851Sbrandon.potter@amd.com             ThreadContext *tc)
37210203SAli.Saidi@ARM.com{
37310203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
37410203SAli.Saidi@ARM.com}
3755513SMichael.Adler@intel.com
3765513SMichael.Adler@intel.comSyscallReturn
37711851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
37811851Sbrandon.potter@amd.com             int index)
3795513SMichael.Adler@intel.com{
3805513SMichael.Adler@intel.com    string path;
3815513SMichael.Adler@intel.com
3828852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
38310223Ssteve.reinhardt@amd.com        return -EFAULT;
3845513SMichael.Adler@intel.com
3855513SMichael.Adler@intel.com    // Adjust path for current working directory
3865513SMichael.Adler@intel.com    path = p->fullPath(path);
3875513SMichael.Adler@intel.com
38811906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3896701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3906701Sgblack@eecs.umich.edu
39111906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, bufsiz);
3925513SMichael.Adler@intel.com
39310955Sdavid.hashe@amd.com    int result = -1;
39410955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
39510955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
39610955Sdavid.hashe@amd.com    } else {
39711140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
39811140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
39911851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
40011140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
40111140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
40211140Sjthestness@gmail.com        //     called binary calls readlink().
40311140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
40411140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
40511140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
40611140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
40711140Sjthestness@gmail.com
40811140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
40911140Sjthestness@gmail.com        char real_path[PATH_MAX];
41011140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
41111140Sjthestness@gmail.com        if (!check_real_path) {
41211140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
41311140Sjthestness@gmail.com                  "executable: %s", p->progName());
41411140Sjthestness@gmail.com        }
41511140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
41611140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
41711140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
41810955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
41910955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
42010955Sdavid.hashe@amd.com            result = bufsiz;
42110955Sdavid.hashe@amd.com        } else {
42211140Sjthestness@gmail.com            result = real_path_len;
42310955Sdavid.hashe@amd.com        }
42411140Sjthestness@gmail.com
42511140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
42611140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
42711140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
42811140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
42910955Sdavid.hashe@amd.com    }
4305513SMichael.Adler@intel.com
4318706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4325513SMichael.Adler@intel.com
4335513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4345513SMichael.Adler@intel.com}
4355513SMichael.Adler@intel.com
4365513SMichael.Adler@intel.comSyscallReturn
43711851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
438511SN/A{
43910633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
44010633Smichaelupton@gmail.com}
44110633Smichaelupton@gmail.com
44210633Smichaelupton@gmail.comSyscallReturn
44311851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
44411851Sbrandon.potter@amd.com             int index)
44510633Smichaelupton@gmail.com{
4461706SN/A    string path;
447360SN/A
4488852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
44910223Ssteve.reinhardt@amd.com        return -EFAULT;
450511SN/A
4513669Sbinkertn@umich.edu    // Adjust path for current working directory
4523669Sbinkertn@umich.edu    path = p->fullPath(path);
4533669Sbinkertn@umich.edu
454511SN/A    int result = unlink(path.c_str());
4551458SN/A    return (result == -1) ? -errno : result;
456511SN/A}
457511SN/A
4585513SMichael.Adler@intel.com
4595513SMichael.Adler@intel.comSyscallReturn
46011851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4615513SMichael.Adler@intel.com{
4625513SMichael.Adler@intel.com    string path;
4635513SMichael.Adler@intel.com
4646701Sgblack@eecs.umich.edu    int index = 0;
4658852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46610223Ssteve.reinhardt@amd.com        return -EFAULT;
4675513SMichael.Adler@intel.com
4685513SMichael.Adler@intel.com    // Adjust path for current working directory
4695513SMichael.Adler@intel.com    path = p->fullPath(path);
4705513SMichael.Adler@intel.com
4716701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4725513SMichael.Adler@intel.com
4735513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4745513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4755513SMichael.Adler@intel.com}
4765513SMichael.Adler@intel.com
4771450SN/ASyscallReturn
47811851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
479511SN/A{
4801706SN/A    string old_name;
481511SN/A
4826701Sgblack@eecs.umich.edu    int index = 0;
4838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4841458SN/A        return -EFAULT;
485511SN/A
4861706SN/A    string new_name;
487511SN/A
4888852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4891458SN/A        return -EFAULT;
490511SN/A
4913669Sbinkertn@umich.edu    // Adjust path for current working directory
4923669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4933669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4943669Sbinkertn@umich.edu
4951706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4961458SN/A    return (result == -1) ? -errno : result;
497511SN/A}
498511SN/A
4991706SN/ASyscallReturn
50011851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5011706SN/A{
5021706SN/A    string path;
5031706SN/A
5046701Sgblack@eecs.umich.edu    int index = 0;
5058852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5061706SN/A        return -EFAULT;
5071706SN/A
5086701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5091706SN/A
5103669Sbinkertn@umich.edu    // Adjust path for current working directory
5113669Sbinkertn@umich.edu    path = p->fullPath(path);
5123669Sbinkertn@umich.edu
5131706SN/A    int result = truncate(path.c_str(), length);
5141706SN/A    return (result == -1) ? -errno : result;
5151706SN/A}
5161706SN/A
5171706SN/ASyscallReturn
51811856Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5191706SN/A{
5206701Sgblack@eecs.umich.edu    int index = 0;
52111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
52211856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
5231706SN/A
52411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
52511856Sbrandon.potter@amd.com    if (!ffdp)
5261706SN/A        return -EBADF;
52711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5281706SN/A
52910931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5301706SN/A    return (result == -1) ? -errno : result;
5311706SN/A}
5321999SN/A
5331999SN/ASyscallReturn
5346703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
53511851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
5366703Svince@csl.cornell.edu{
5376703Svince@csl.cornell.edu    int index = 0;
5386703Svince@csl.cornell.edu    string path;
5396703Svince@csl.cornell.edu
5408852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
54111906SBrandon.Potter@amd.com        return -EFAULT;
5426703Svince@csl.cornell.edu
5436744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5446703Svince@csl.cornell.edu
5456703Svince@csl.cornell.edu    // Adjust path for current working directory
5466703Svince@csl.cornell.edu    path = process->fullPath(path);
5476703Svince@csl.cornell.edu
5486744SAli.Saidi@arm.com#if NO_STAT64
5496744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5506744SAli.Saidi@arm.com#else
5516703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5526744SAli.Saidi@arm.com#endif
5536703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5546703Svince@csl.cornell.edu}
5556703Svince@csl.cornell.edu
5566703Svince@csl.cornell.eduSyscallReturn
55711856Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5586685Stjones1@inf.ed.ac.uk{
5596701Sgblack@eecs.umich.edu    int index = 0;
56011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
56111856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
5626685Stjones1@inf.ed.ac.uk
56311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
56411856Sbrandon.potter@amd.com    if (!ffdp)
5656685Stjones1@inf.ed.ac.uk        return -EBADF;
56611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5676685Stjones1@inf.ed.ac.uk
5686744SAli.Saidi@arm.com#if NO_STAT64
56910931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5706744SAli.Saidi@arm.com#else
57110931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5726744SAli.Saidi@arm.com#endif
5736685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5746685Stjones1@inf.ed.ac.uk}
5756685Stjones1@inf.ed.ac.uk
5766685Stjones1@inf.ed.ac.ukSyscallReturn
57711851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5785513SMichael.Adler@intel.com{
5795513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5805513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5815513SMichael.Adler@intel.com    // changing anything.
5825513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5835513SMichael.Adler@intel.com    umask(oldMask);
5845521Snate@binkert.org    return (int)oldMask;
5855513SMichael.Adler@intel.com}
5865513SMichael.Adler@intel.com
5875513SMichael.Adler@intel.comSyscallReturn
58811851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5891999SN/A{
5901999SN/A    string path;
5911999SN/A
5926701Sgblack@eecs.umich.edu    int index = 0;
5938852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5941999SN/A        return -EFAULT;
5951999SN/A
5961999SN/A    /* XXX endianess */
5976701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5981999SN/A    uid_t hostOwner = owner;
5996701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6001999SN/A    gid_t hostGroup = group;
6011999SN/A
6023669Sbinkertn@umich.edu    // Adjust path for current working directory
6033669Sbinkertn@umich.edu    path = p->fullPath(path);
6043669Sbinkertn@umich.edu
6051999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6061999SN/A    return (result == -1) ? -errno : result;
6071999SN/A}
6081999SN/A
6091999SN/ASyscallReturn
61011856Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6111999SN/A{
6126701Sgblack@eecs.umich.edu    int index = 0;
61311856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6141999SN/A
61511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
61611856Sbrandon.potter@amd.com    if (!ffdp)
6171999SN/A        return -EBADF;
61811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6191999SN/A
6201999SN/A    /* XXX endianess */
62111856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
6221999SN/A    uid_t hostOwner = owner;
62311856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
6241999SN/A    gid_t hostGroup = group;
6251999SN/A
62610931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6271999SN/A    return (result == -1) ? -errno : result;
6281999SN/A}
6292093SN/A
6302093SN/A
63111856Sbrandon.potter@amd.com/**
63211856Sbrandon.potter@amd.com * TODO: there's a bit more involved here since file descriptors created with
63311856Sbrandon.potter@amd.com * dup are supposed to share a file description. So, there is a problem with
63411856Sbrandon.potter@amd.com * maintaining fields like file offset or flags since an update to such a
63511856Sbrandon.potter@amd.com * field won't be reflected in the metadata for the fd entries that we
63611856Sbrandon.potter@amd.com * maintain to hold metadata for checkpoint restoration.
63711856Sbrandon.potter@amd.com */
6382093SN/ASyscallReturn
63911856Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6403079Sstever@eecs.umich.edu{
6416701Sgblack@eecs.umich.edu    int index = 0;
64211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
64310931Sbrandon.potter@amd.com
64411856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
64511856Sbrandon.potter@amd.com    if (!old_hbfdp)
6463079Sstever@eecs.umich.edu        return -EBADF;
64711856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
6485282Srstrong@cs.ucsd.edu
64910781Snilay@cs.wisc.edu    int result = dup(sim_fd);
65011856Sbrandon.potter@amd.com    int local_errno = errno;
65111856Sbrandon.potter@amd.com
65211856Sbrandon.potter@amd.com    std::shared_ptr<FDEntry> new_fdep = old_hbfdp->clone();
65311856Sbrandon.potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(new_fdep);
65411856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
65511856Sbrandon.potter@amd.com
65611856Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : p->fds->allocFD(new_fdep);
6573079Sstever@eecs.umich.edu}
6583079Sstever@eecs.umich.edu
6593079Sstever@eecs.umich.eduSyscallReturn
66011856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6612093SN/A{
66211875Sbrandon.potter@amd.com    int arg;
6636701Sgblack@eecs.umich.edu    int index = 0;
66411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
66511875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6662093SN/A
66711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
66811856Sbrandon.potter@amd.com    if (!hbfdp)
6692093SN/A        return -EBADF;
67011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6712093SN/A
67211875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
67311875Sbrandon.potter@amd.com
6742093SN/A    switch (cmd) {
67511875Sbrandon.potter@amd.com      case F_GETFD:
67611875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
6772093SN/A
67811875Sbrandon.potter@amd.com      case F_SETFD: {
67911875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
68011875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
6812093SN/A        return 0;
68211875Sbrandon.potter@amd.com      }
6832093SN/A
68411875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
68511875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
68611875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
68711875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
68811875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
68911875Sbrandon.potter@amd.com      // subsequent fcntls.
69011875Sbrandon.potter@amd.com      case F_GETFL:
69111875Sbrandon.potter@amd.com      case F_SETFL: {
69211875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
69311875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
69411875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
69511875Sbrandon.potter@amd.com      }
6962093SN/A
6972093SN/A      default:
69811875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
6992093SN/A        return 0;
7002093SN/A    }
7012093SN/A}
7022093SN/A
7032238SN/ASyscallReturn
70411856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
7052687Sksewell@umich.edu{
7066701Sgblack@eecs.umich.edu    int index = 0;
70711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
7082687Sksewell@umich.edu
70911856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
71011856Sbrandon.potter@amd.com    if (!hbfdp)
7112687Sksewell@umich.edu        return -EBADF;
71211856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
7132687Sksewell@umich.edu
71411856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
7152687Sksewell@umich.edu    switch (cmd) {
7162687Sksewell@umich.edu      case 33: //F_GETLK64
71710931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7182687Sksewell@umich.edu        return -EMFILE;
7192687Sksewell@umich.edu
7202687Sksewell@umich.edu      case 34: // F_SETLK64
7212687Sksewell@umich.edu      case 35: // F_SETLKW64
72210931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
72310931Sbrandon.potter@amd.com             tgt_fd);
7242687Sksewell@umich.edu        return -EMFILE;
7252687Sksewell@umich.edu
7262687Sksewell@umich.edu      default:
7272687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7282687Sksewell@umich.edu        // to the underlying OS
72910931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
73010931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7312687Sksewell@umich.edu    }
7322687Sksewell@umich.edu}
7332687Sksewell@umich.edu
7342687Sksewell@umich.eduSyscallReturn
73511851Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
73611851Sbrandon.potter@amd.com               ThreadContext *tc)
7372238SN/A{
73811856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
7392093SN/A
74011856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
74111856Sbrandon.potter@amd.com    if (pipe_retval < 0)
7422238SN/A        return pipe_retval;
7432238SN/A
74411856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
74511856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
7462238SN/A
74711856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
74811856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
74911856Sbrandon.potter@amd.com
75011856Sbrandon.potter@amd.com    tgt_fds[0] = process->fds->allocFD(rpfd);
75111856Sbrandon.potter@amd.com    tgt_fds[1] = process->fds->allocFD(wpfd);
75211856Sbrandon.potter@amd.com
75311856Sbrandon.potter@amd.com    /**
75411856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
75511856Sbrandon.potter@amd.com     * as the write end of the pipe.
75611856Sbrandon.potter@amd.com     */
75711856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
75811856Sbrandon.potter@amd.com
75911856Sbrandon.potter@amd.com    /**
76011856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
76111856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
76211856Sbrandon.potter@amd.com     */
76311856Sbrandon.potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
7642238SN/A    return sim_fds[0];
7652238SN/A}
7662238SN/A
76711885Sbrandon.potter@amd.comSyscallReturn
76811885Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, Process *process,
76911885Sbrandon.potter@amd.com            ThreadContext *tc)
77011885Sbrandon.potter@amd.com{
77111885Sbrandon.potter@amd.com    int index = 0;
77211885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
77311885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
77411885Sbrandon.potter@amd.com
77511885Sbrandon.potter@amd.com    if (pgid < 0)
77611885Sbrandon.potter@amd.com        return -EINVAL;
77711885Sbrandon.potter@amd.com
77811885Sbrandon.potter@amd.com    if (pid == 0) {
77911885Sbrandon.potter@amd.com        process->setpgid(process->pid());
78011885Sbrandon.potter@amd.com        return 0;
78111885Sbrandon.potter@amd.com    }
78211885Sbrandon.potter@amd.com
78311885Sbrandon.potter@amd.com    Process *matched_ph = NULL;
78411885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
78511885Sbrandon.potter@amd.com
78611885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
78711885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
78811885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
78911885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
79011885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
79111885Sbrandon.potter@amd.com
79211885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
79311885Sbrandon.potter@amd.com                matched_ph = walk_ph;
79411885Sbrandon.potter@amd.com        }
79511885Sbrandon.potter@amd.com    }
79611885Sbrandon.potter@amd.com
79711885Sbrandon.potter@amd.com    assert(matched_ph != NULL);
79811885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
79911885Sbrandon.potter@amd.com
80011885Sbrandon.potter@amd.com    return 0;
80111885Sbrandon.potter@amd.com}
8022238SN/A
8032238SN/ASyscallReturn
80411851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
80511851Sbrandon.potter@amd.com                 ThreadContext *tc)
8062238SN/A{
8072238SN/A    // Make up a PID.  There's no interprocess communication in
8082238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8092238SN/A    // not getting a unique value.
8102238SN/A
8113114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
8123114Sgblack@eecs.umich.edu    return process->pid();
8132238SN/A}
8142238SN/A
8152238SN/A
8162238SN/ASyscallReturn
81711851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
81811851Sbrandon.potter@amd.com                 ThreadContext *tc)
8192238SN/A{
8202238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
8212238SN/A    // simulation to be deterministic.
8222238SN/A
8232238SN/A    // EUID goes in r20.
82411906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID
82511906SBrandon.Potter@amd.com    return process->uid(); // UID
8262238SN/A}
8272238SN/A
8282238SN/A
8292238SN/ASyscallReturn
83011851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
83111851Sbrandon.potter@amd.com                 ThreadContext *tc)
8322238SN/A{
8332238SN/A    // Get current group ID.  EGID goes in r20.
83411906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID
8353114Sgblack@eecs.umich.edu    return process->gid();
8362238SN/A}
8372238SN/A
8382238SN/A
8392238SN/ASyscallReturn
84011851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8412680Sktlim@umich.edu           ThreadContext *tc)
8422238SN/A{
8432238SN/A    // can't fathom why a benchmark would call this.
8446701Sgblack@eecs.umich.edu    int index = 0;
8456701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8462238SN/A    return 0;
8472238SN/A}
8482238SN/A
8492238SN/ASyscallReturn
85011851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
8512680Sktlim@umich.edu           ThreadContext *tc)
8522238SN/A{
85311885Sbrandon.potter@amd.com    return process->tgid();
85411885Sbrandon.potter@amd.com}
8552238SN/A
85611885Sbrandon.potter@amd.comSyscallReturn
85711885Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, Process *process,
85811885Sbrandon.potter@amd.com           ThreadContext *tc)
85911885Sbrandon.potter@amd.com{
8603114Sgblack@eecs.umich.edu    return process->pid();
8612238SN/A}
8622238SN/A
8632238SN/ASyscallReturn
86411851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
86511851Sbrandon.potter@amd.com            ThreadContext *tc)
8662238SN/A{
8673114Sgblack@eecs.umich.edu    return process->ppid();
8682238SN/A}
8692238SN/A
8702238SN/ASyscallReturn
87111851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8722680Sktlim@umich.edu           ThreadContext *tc)
8732238SN/A{
8745543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8752238SN/A}
8762238SN/A
8772238SN/ASyscallReturn
87811851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
87911851Sbrandon.potter@amd.com            ThreadContext *tc)
8802238SN/A{
8815543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8822238SN/A}
8832238SN/A
8842238SN/ASyscallReturn
88511851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
8862680Sktlim@umich.edu           ThreadContext *tc)
8872238SN/A{
8883114Sgblack@eecs.umich.edu    return process->gid();
8892238SN/A}
8902238SN/A
8912238SN/ASyscallReturn
89211851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
89311851Sbrandon.potter@amd.com            ThreadContext *tc)
8942238SN/A{
8953114Sgblack@eecs.umich.edu    return process->egid();
8962238SN/A}
8972238SN/A
8989455Smitch.hayenga+gem5@gmail.comSyscallReturn
89911856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
90011760Sbrandon.potter@amd.com{
90111799Sbrandon.potter@amd.com#if NO_FALLOCATE
90211799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
90311799Sbrandon.potter@amd.com#else
90411760Sbrandon.potter@amd.com    int index = 0;
90511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
90611856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
90711856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
90811856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
90911760Sbrandon.potter@amd.com
91011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
91111856Sbrandon.potter@amd.com    if (!ffdp)
91211760Sbrandon.potter@amd.com        return -EBADF;
91311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
91411760Sbrandon.potter@amd.com
91511760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
91611760Sbrandon.potter@amd.com    if (result < 0)
91711760Sbrandon.potter@amd.com        return -errno;
91811799Sbrandon.potter@amd.com#endif
91911760Sbrandon.potter@amd.com    return 0;
92011760Sbrandon.potter@amd.com}
92111760Sbrandon.potter@amd.com
92211760Sbrandon.potter@amd.comSyscallReturn
92311851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
92411851Sbrandon.potter@amd.com           int index)
9259455Smitch.hayenga+gem5@gmail.com{
9269455Smitch.hayenga+gem5@gmail.com    string path;
9279455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
92810223Ssteve.reinhardt@amd.com        return -EFAULT;
9299455Smitch.hayenga+gem5@gmail.com
9309455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9319455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9329455Smitch.hayenga+gem5@gmail.com
9339455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9349455Smitch.hayenga+gem5@gmail.com
9359455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9369455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9379455Smitch.hayenga+gem5@gmail.com}
93810203SAli.Saidi@ARM.com
93910203SAli.Saidi@ARM.comSyscallReturn
94011851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
94110203SAli.Saidi@ARM.com{
94210203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
94310203SAli.Saidi@ARM.com}
94410203SAli.Saidi@ARM.com
945