syscall_emul.cc revision 11908
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.comSyscallReturn
36911851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
37011851Sbrandon.potter@amd.com             ThreadContext *tc)
37110203SAli.Saidi@ARM.com{
37210203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
37310203SAli.Saidi@ARM.com}
3745513SMichael.Adler@intel.com
3755513SMichael.Adler@intel.comSyscallReturn
37611851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
37711851Sbrandon.potter@amd.com             int index)
3785513SMichael.Adler@intel.com{
3795513SMichael.Adler@intel.com    string path;
3805513SMichael.Adler@intel.com
3818852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
38210223Ssteve.reinhardt@amd.com        return -EFAULT;
3835513SMichael.Adler@intel.com
3845513SMichael.Adler@intel.com    // Adjust path for current working directory
3855513SMichael.Adler@intel.com    path = p->fullPath(path);
3865513SMichael.Adler@intel.com
38711906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3886701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3896701Sgblack@eecs.umich.edu
39011906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, bufsiz);
3915513SMichael.Adler@intel.com
39210955Sdavid.hashe@amd.com    int result = -1;
39310955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
39410955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
39510955Sdavid.hashe@amd.com    } else {
39611140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
39711140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
39811851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
39911140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
40011140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
40111140Sjthestness@gmail.com        //     called binary calls readlink().
40211140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
40311140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
40411140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
40511140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
40611140Sjthestness@gmail.com
40711140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
40811140Sjthestness@gmail.com        char real_path[PATH_MAX];
40911140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
41011140Sjthestness@gmail.com        if (!check_real_path) {
41111140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
41211140Sjthestness@gmail.com                  "executable: %s", p->progName());
41311140Sjthestness@gmail.com        }
41411140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
41511140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
41611140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
41710955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
41810955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
41910955Sdavid.hashe@amd.com            result = bufsiz;
42010955Sdavid.hashe@amd.com        } else {
42111140Sjthestness@gmail.com            result = real_path_len;
42210955Sdavid.hashe@amd.com        }
42311140Sjthestness@gmail.com
42411140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
42511140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
42611140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
42711140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
42810955Sdavid.hashe@amd.com    }
4295513SMichael.Adler@intel.com
4308706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4315513SMichael.Adler@intel.com
4325513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4335513SMichael.Adler@intel.com}
4345513SMichael.Adler@intel.com
4355513SMichael.Adler@intel.comSyscallReturn
43611851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
437511SN/A{
43810633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
43910633Smichaelupton@gmail.com}
44010633Smichaelupton@gmail.com
44110633Smichaelupton@gmail.comSyscallReturn
44211851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
44311851Sbrandon.potter@amd.com             int index)
44410633Smichaelupton@gmail.com{
4451706SN/A    string path;
446360SN/A
4478852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
44810223Ssteve.reinhardt@amd.com        return -EFAULT;
449511SN/A
4503669Sbinkertn@umich.edu    // Adjust path for current working directory
4513669Sbinkertn@umich.edu    path = p->fullPath(path);
4523669Sbinkertn@umich.edu
453511SN/A    int result = unlink(path.c_str());
4541458SN/A    return (result == -1) ? -errno : result;
455511SN/A}
456511SN/A
4575513SMichael.Adler@intel.com
4585513SMichael.Adler@intel.comSyscallReturn
45911851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4605513SMichael.Adler@intel.com{
4615513SMichael.Adler@intel.com    string path;
4625513SMichael.Adler@intel.com
4636701Sgblack@eecs.umich.edu    int index = 0;
4648852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46510223Ssteve.reinhardt@amd.com        return -EFAULT;
4665513SMichael.Adler@intel.com
4675513SMichael.Adler@intel.com    // Adjust path for current working directory
4685513SMichael.Adler@intel.com    path = p->fullPath(path);
4695513SMichael.Adler@intel.com
4706701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4715513SMichael.Adler@intel.com
4725513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4735513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4745513SMichael.Adler@intel.com}
4755513SMichael.Adler@intel.com
4761450SN/ASyscallReturn
47711851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
478511SN/A{
4791706SN/A    string old_name;
480511SN/A
4816701Sgblack@eecs.umich.edu    int index = 0;
4828852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4831458SN/A        return -EFAULT;
484511SN/A
4851706SN/A    string new_name;
486511SN/A
4878852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4881458SN/A        return -EFAULT;
489511SN/A
4903669Sbinkertn@umich.edu    // Adjust path for current working directory
4913669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4923669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4933669Sbinkertn@umich.edu
4941706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4951458SN/A    return (result == -1) ? -errno : result;
496511SN/A}
497511SN/A
4981706SN/ASyscallReturn
49911851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5001706SN/A{
5011706SN/A    string path;
5021706SN/A
5036701Sgblack@eecs.umich.edu    int index = 0;
5048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5051706SN/A        return -EFAULT;
5061706SN/A
5076701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5081706SN/A
5093669Sbinkertn@umich.edu    // Adjust path for current working directory
5103669Sbinkertn@umich.edu    path = p->fullPath(path);
5113669Sbinkertn@umich.edu
5121706SN/A    int result = truncate(path.c_str(), length);
5131706SN/A    return (result == -1) ? -errno : result;
5141706SN/A}
5151706SN/A
5161706SN/ASyscallReturn
51711856Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5181706SN/A{
5196701Sgblack@eecs.umich.edu    int index = 0;
52011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
52111856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
5221706SN/A
52311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
52411856Sbrandon.potter@amd.com    if (!ffdp)
5251706SN/A        return -EBADF;
52611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5271706SN/A
52810931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5291706SN/A    return (result == -1) ? -errno : result;
5301706SN/A}
5311999SN/A
5321999SN/ASyscallReturn
5336703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
53411851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
5356703Svince@csl.cornell.edu{
5366703Svince@csl.cornell.edu    int index = 0;
5376703Svince@csl.cornell.edu    string path;
5386703Svince@csl.cornell.edu
5398852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
54011906SBrandon.Potter@amd.com        return -EFAULT;
5416703Svince@csl.cornell.edu
5426744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5436703Svince@csl.cornell.edu
5446703Svince@csl.cornell.edu    // Adjust path for current working directory
5456703Svince@csl.cornell.edu    path = process->fullPath(path);
5466703Svince@csl.cornell.edu
5476744SAli.Saidi@arm.com#if NO_STAT64
5486744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5496744SAli.Saidi@arm.com#else
5506703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5516744SAli.Saidi@arm.com#endif
5526703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5536703Svince@csl.cornell.edu}
5546703Svince@csl.cornell.edu
5556703Svince@csl.cornell.eduSyscallReturn
55611856Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5576685Stjones1@inf.ed.ac.uk{
5586701Sgblack@eecs.umich.edu    int index = 0;
55911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
56011856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
5616685Stjones1@inf.ed.ac.uk
56211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
56311856Sbrandon.potter@amd.com    if (!ffdp)
5646685Stjones1@inf.ed.ac.uk        return -EBADF;
56511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5666685Stjones1@inf.ed.ac.uk
5676744SAli.Saidi@arm.com#if NO_STAT64
56810931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5696744SAli.Saidi@arm.com#else
57010931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5716744SAli.Saidi@arm.com#endif
5726685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5736685Stjones1@inf.ed.ac.uk}
5746685Stjones1@inf.ed.ac.uk
5756685Stjones1@inf.ed.ac.ukSyscallReturn
57611851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5775513SMichael.Adler@intel.com{
5785513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5795513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5805513SMichael.Adler@intel.com    // changing anything.
5815513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5825513SMichael.Adler@intel.com    umask(oldMask);
5835521Snate@binkert.org    return (int)oldMask;
5845513SMichael.Adler@intel.com}
5855513SMichael.Adler@intel.com
5865513SMichael.Adler@intel.comSyscallReturn
58711851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5881999SN/A{
5891999SN/A    string path;
5901999SN/A
5916701Sgblack@eecs.umich.edu    int index = 0;
5928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5931999SN/A        return -EFAULT;
5941999SN/A
5951999SN/A    /* XXX endianess */
5966701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5971999SN/A    uid_t hostOwner = owner;
5986701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5991999SN/A    gid_t hostGroup = group;
6001999SN/A
6013669Sbinkertn@umich.edu    // Adjust path for current working directory
6023669Sbinkertn@umich.edu    path = p->fullPath(path);
6033669Sbinkertn@umich.edu
6041999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6051999SN/A    return (result == -1) ? -errno : result;
6061999SN/A}
6071999SN/A
6081999SN/ASyscallReturn
60911856Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6101999SN/A{
6116701Sgblack@eecs.umich.edu    int index = 0;
61211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6131999SN/A
61411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
61511856Sbrandon.potter@amd.com    if (!ffdp)
6161999SN/A        return -EBADF;
61711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6181999SN/A
6191999SN/A    /* XXX endianess */
62011856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
6211999SN/A    uid_t hostOwner = owner;
62211856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
6231999SN/A    gid_t hostGroup = group;
6241999SN/A
62510931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6261999SN/A    return (result == -1) ? -errno : result;
6271999SN/A}
6282093SN/A
62911856Sbrandon.potter@amd.com/**
63011908SBrandon.Potter@amd.com * FIXME: The file description is not shared among file descriptors created
63111908SBrandon.Potter@amd.com * with dup. Really, it's difficult to maintain fields like file offset or
63211908SBrandon.Potter@amd.com * flags since an update to such a field won't be reflected in the metadata
63311908SBrandon.Potter@amd.com * for the fd entries that we maintain for checkpoint restoration.
63411856Sbrandon.potter@amd.com */
6352093SN/ASyscallReturn
63611856Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6373079Sstever@eecs.umich.edu{
6386701Sgblack@eecs.umich.edu    int index = 0;
63911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
64010931Sbrandon.potter@amd.com
64111856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
64211856Sbrandon.potter@amd.com    if (!old_hbfdp)
6433079Sstever@eecs.umich.edu        return -EBADF;
64411856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
6455282Srstrong@cs.ucsd.edu
64610781Snilay@cs.wisc.edu    int result = dup(sim_fd);
64711908SBrandon.Potter@amd.com    if (result == -1)
64811908SBrandon.Potter@amd.com        return -errno;
64911856Sbrandon.potter@amd.com
65011908SBrandon.Potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone());
65111856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
65211908SBrandon.Potter@amd.com    new_hbfdp->setCOE(false);
65311908SBrandon.Potter@amd.com    return p->fds->allocFD(new_hbfdp);
65411908SBrandon.Potter@amd.com}
65511856Sbrandon.potter@amd.com
65611908SBrandon.Potter@amd.comSyscallReturn
65711908SBrandon.Potter@amd.comdup2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
65811908SBrandon.Potter@amd.com{
65911908SBrandon.Potter@amd.com    int index = 0;
66011908SBrandon.Potter@amd.com
66111908SBrandon.Potter@amd.com    int old_tgt_fd = p->getSyscallArg(tc, index);
66211908SBrandon.Potter@amd.com    auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]);
66311908SBrandon.Potter@amd.com    if (!old_hbp)
66411908SBrandon.Potter@amd.com        return -EBADF;
66511908SBrandon.Potter@amd.com    int old_sim_fd = old_hbp->getSimFD();
66611908SBrandon.Potter@amd.com
66711908SBrandon.Potter@amd.com    /**
66811908SBrandon.Potter@amd.com     * We need a valid host file descriptor number to be able to pass into
66911908SBrandon.Potter@amd.com     * the second parameter for dup2 (newfd), but we don't know what the
67011908SBrandon.Potter@amd.com     * viable numbers are; we execute the open call to retrieve one.
67111908SBrandon.Potter@amd.com     */
67211908SBrandon.Potter@amd.com    int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY));
67311908SBrandon.Potter@amd.com    if (res_fd == -1)
67411908SBrandon.Potter@amd.com        return -errno;
67511908SBrandon.Potter@amd.com
67611908SBrandon.Potter@amd.com    int new_tgt_fd = p->getSyscallArg(tc, index);
67711908SBrandon.Potter@amd.com    auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]);
67811908SBrandon.Potter@amd.com    if (new_hbp)
67911908SBrandon.Potter@amd.com        p->fds->closeFDEntry(new_tgt_fd);
68011908SBrandon.Potter@amd.com    new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone());
68111908SBrandon.Potter@amd.com    new_hbp->setSimFD(res_fd);
68211908SBrandon.Potter@amd.com    new_hbp->setCOE(false);
68311908SBrandon.Potter@amd.com
68411908SBrandon.Potter@amd.com    return p->fds->allocFD(new_hbp);
6853079Sstever@eecs.umich.edu}
6863079Sstever@eecs.umich.edu
6873079Sstever@eecs.umich.eduSyscallReturn
68811856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6892093SN/A{
69011875Sbrandon.potter@amd.com    int arg;
6916701Sgblack@eecs.umich.edu    int index = 0;
69211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
69311875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6942093SN/A
69511856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
69611856Sbrandon.potter@amd.com    if (!hbfdp)
6972093SN/A        return -EBADF;
69811856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6992093SN/A
70011875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
70111875Sbrandon.potter@amd.com
7022093SN/A    switch (cmd) {
70311875Sbrandon.potter@amd.com      case F_GETFD:
70411875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
7052093SN/A
70611875Sbrandon.potter@amd.com      case F_SETFD: {
70711875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
70811875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
7092093SN/A        return 0;
71011875Sbrandon.potter@amd.com      }
7112093SN/A
71211875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
71311875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
71411875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
71511875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
71611875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
71711875Sbrandon.potter@amd.com      // subsequent fcntls.
71811875Sbrandon.potter@amd.com      case F_GETFL:
71911875Sbrandon.potter@amd.com      case F_SETFL: {
72011875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
72111875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
72211875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
72311875Sbrandon.potter@amd.com      }
7242093SN/A
7252093SN/A      default:
72611875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
7272093SN/A        return 0;
7282093SN/A    }
7292093SN/A}
7302093SN/A
7312238SN/ASyscallReturn
73211856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
7332687Sksewell@umich.edu{
7346701Sgblack@eecs.umich.edu    int index = 0;
73511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
7362687Sksewell@umich.edu
73711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
73811856Sbrandon.potter@amd.com    if (!hbfdp)
7392687Sksewell@umich.edu        return -EBADF;
74011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
7412687Sksewell@umich.edu
74211856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
7432687Sksewell@umich.edu    switch (cmd) {
7442687Sksewell@umich.edu      case 33: //F_GETLK64
74510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7462687Sksewell@umich.edu        return -EMFILE;
7472687Sksewell@umich.edu
7482687Sksewell@umich.edu      case 34: // F_SETLK64
7492687Sksewell@umich.edu      case 35: // F_SETLKW64
75010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
75110931Sbrandon.potter@amd.com             tgt_fd);
7522687Sksewell@umich.edu        return -EMFILE;
7532687Sksewell@umich.edu
7542687Sksewell@umich.edu      default:
7552687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7562687Sksewell@umich.edu        // to the underlying OS
75710931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
75810931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7592687Sksewell@umich.edu    }
7602687Sksewell@umich.edu}
7612687Sksewell@umich.edu
7622687Sksewell@umich.eduSyscallReturn
76311908SBrandon.Potter@amd.compipeImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
76411908SBrandon.Potter@amd.com         bool pseudoPipe)
7652238SN/A{
76611908SBrandon.Potter@amd.com    Addr tgt_addr = 0;
76711908SBrandon.Potter@amd.com    if (!pseudoPipe) {
76811908SBrandon.Potter@amd.com        int index = 0;
76911908SBrandon.Potter@amd.com        tgt_addr = p->getSyscallArg(tc, index);
77011908SBrandon.Potter@amd.com    }
77111908SBrandon.Potter@amd.com
77211856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
7732093SN/A
77411856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
77511908SBrandon.Potter@amd.com    if (pipe_retval == -1)
77611908SBrandon.Potter@amd.com        return -errno;
7772238SN/A
77811856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
77911856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
78011908SBrandon.Potter@amd.com    tgt_fds[0] = p->fds->allocFD(rpfd);
7812238SN/A
78211856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
78311856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
78411908SBrandon.Potter@amd.com    tgt_fds[1] = p->fds->allocFD(wpfd);
78511856Sbrandon.potter@amd.com
78611856Sbrandon.potter@amd.com    /**
78711856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
78811856Sbrandon.potter@amd.com     * as the write end of the pipe.
78911856Sbrandon.potter@amd.com     */
79011856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
79111856Sbrandon.potter@amd.com
79211856Sbrandon.potter@amd.com    /**
79311856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
79411856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
79511856Sbrandon.potter@amd.com     */
79611908SBrandon.Potter@amd.com    if (pseudoPipe) {
79711908SBrandon.Potter@amd.com        tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
79811908SBrandon.Potter@amd.com        return tgt_fds[0];
79911908SBrandon.Potter@amd.com    }
80011908SBrandon.Potter@amd.com
80111908SBrandon.Potter@amd.com    /**
80211908SBrandon.Potter@amd.com     * Copy the target file descriptors into buffer space and then copy
80311908SBrandon.Potter@amd.com     * the buffer space back into the target address space.
80411908SBrandon.Potter@amd.com     */
80511908SBrandon.Potter@amd.com    BufferArg tgt_handle(tgt_addr, sizeof(int[2]));
80611908SBrandon.Potter@amd.com    int *buf_ptr = (int*)tgt_handle.bufferPtr();
80711908SBrandon.Potter@amd.com    buf_ptr[0] = tgt_fds[0];
80811908SBrandon.Potter@amd.com    buf_ptr[1] = tgt_fds[1];
80911908SBrandon.Potter@amd.com    tgt_handle.copyOut(tc->getMemProxy());
81011908SBrandon.Potter@amd.com    return 0;
81111908SBrandon.Potter@amd.com}
81211908SBrandon.Potter@amd.com
81311908SBrandon.Potter@amd.comSyscallReturn
81411908SBrandon.Potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
81511908SBrandon.Potter@amd.com               ThreadContext *tc)
81611908SBrandon.Potter@amd.com{
81711908SBrandon.Potter@amd.com    return pipeImpl(desc, callnum, process, tc, true);
81811908SBrandon.Potter@amd.com}
81911908SBrandon.Potter@amd.com
82011908SBrandon.Potter@amd.comSyscallReturn
82111908SBrandon.Potter@amd.compipeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
82211908SBrandon.Potter@amd.com{
82311908SBrandon.Potter@amd.com    return pipeImpl(desc, callnum, process, tc, false);
8242238SN/A}
8252238SN/A
82611885Sbrandon.potter@amd.comSyscallReturn
82711885Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, Process *process,
82811885Sbrandon.potter@amd.com            ThreadContext *tc)
82911885Sbrandon.potter@amd.com{
83011885Sbrandon.potter@amd.com    int index = 0;
83111885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
83211885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
83311885Sbrandon.potter@amd.com
83411885Sbrandon.potter@amd.com    if (pgid < 0)
83511885Sbrandon.potter@amd.com        return -EINVAL;
83611885Sbrandon.potter@amd.com
83711885Sbrandon.potter@amd.com    if (pid == 0) {
83811885Sbrandon.potter@amd.com        process->setpgid(process->pid());
83911885Sbrandon.potter@amd.com        return 0;
84011885Sbrandon.potter@amd.com    }
84111885Sbrandon.potter@amd.com
84211885Sbrandon.potter@amd.com    Process *matched_ph = NULL;
84311885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
84411885Sbrandon.potter@amd.com
84511885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
84611885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
84711885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
84811885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
84911885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
85011885Sbrandon.potter@amd.com
85111885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
85211885Sbrandon.potter@amd.com                matched_ph = walk_ph;
85311885Sbrandon.potter@amd.com        }
85411885Sbrandon.potter@amd.com    }
85511885Sbrandon.potter@amd.com
85611885Sbrandon.potter@amd.com    assert(matched_ph != NULL);
85711885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
85811885Sbrandon.potter@amd.com
85911885Sbrandon.potter@amd.com    return 0;
86011885Sbrandon.potter@amd.com}
8612238SN/A
8622238SN/ASyscallReturn
86311851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
86411851Sbrandon.potter@amd.com                 ThreadContext *tc)
8652238SN/A{
8662238SN/A    // Make up a PID.  There's no interprocess communication in
8672238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8682238SN/A    // not getting a unique value.
8692238SN/A
8703114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
8713114Sgblack@eecs.umich.edu    return process->pid();
8722238SN/A}
8732238SN/A
8742238SN/A
8752238SN/ASyscallReturn
87611851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
87711851Sbrandon.potter@amd.com                 ThreadContext *tc)
8782238SN/A{
8792238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
8802238SN/A    // simulation to be deterministic.
8812238SN/A
8822238SN/A    // EUID goes in r20.
88311906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID
88411906SBrandon.Potter@amd.com    return process->uid(); // UID
8852238SN/A}
8862238SN/A
8872238SN/A
8882238SN/ASyscallReturn
88911851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
89011851Sbrandon.potter@amd.com                 ThreadContext *tc)
8912238SN/A{
8922238SN/A    // Get current group ID.  EGID goes in r20.
89311906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID
8943114Sgblack@eecs.umich.edu    return process->gid();
8952238SN/A}
8962238SN/A
8972238SN/A
8982238SN/ASyscallReturn
89911851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
9002680Sktlim@umich.edu           ThreadContext *tc)
9012238SN/A{
9022238SN/A    // can't fathom why a benchmark would call this.
9036701Sgblack@eecs.umich.edu    int index = 0;
9046701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
9052238SN/A    return 0;
9062238SN/A}
9072238SN/A
9082238SN/ASyscallReturn
90911851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
9102680Sktlim@umich.edu           ThreadContext *tc)
9112238SN/A{
91211885Sbrandon.potter@amd.com    return process->tgid();
91311885Sbrandon.potter@amd.com}
9142238SN/A
91511885Sbrandon.potter@amd.comSyscallReturn
91611885Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, Process *process,
91711885Sbrandon.potter@amd.com           ThreadContext *tc)
91811885Sbrandon.potter@amd.com{
9193114Sgblack@eecs.umich.edu    return process->pid();
9202238SN/A}
9212238SN/A
9222238SN/ASyscallReturn
92311851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
92411851Sbrandon.potter@amd.com            ThreadContext *tc)
9252238SN/A{
9263114Sgblack@eecs.umich.edu    return process->ppid();
9272238SN/A}
9282238SN/A
9292238SN/ASyscallReturn
93011851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
9312680Sktlim@umich.edu           ThreadContext *tc)
9322238SN/A{
9335543Ssaidi@eecs.umich.edu    return process->uid();              // UID
9342238SN/A}
9352238SN/A
9362238SN/ASyscallReturn
93711851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
93811851Sbrandon.potter@amd.com            ThreadContext *tc)
9392238SN/A{
9405543Ssaidi@eecs.umich.edu    return process->euid();             // UID
9412238SN/A}
9422238SN/A
9432238SN/ASyscallReturn
94411851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
9452680Sktlim@umich.edu           ThreadContext *tc)
9462238SN/A{
9473114Sgblack@eecs.umich.edu    return process->gid();
9482238SN/A}
9492238SN/A
9502238SN/ASyscallReturn
95111851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
95211851Sbrandon.potter@amd.com            ThreadContext *tc)
9532238SN/A{
9543114Sgblack@eecs.umich.edu    return process->egid();
9552238SN/A}
9562238SN/A
9579455Smitch.hayenga+gem5@gmail.comSyscallReturn
95811856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
95911760Sbrandon.potter@amd.com{
96011799Sbrandon.potter@amd.com#if NO_FALLOCATE
96111799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
96211799Sbrandon.potter@amd.com#else
96311760Sbrandon.potter@amd.com    int index = 0;
96411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
96511856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
96611856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
96711856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
96811760Sbrandon.potter@amd.com
96911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
97011856Sbrandon.potter@amd.com    if (!ffdp)
97111760Sbrandon.potter@amd.com        return -EBADF;
97211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
97311760Sbrandon.potter@amd.com
97411760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
97511760Sbrandon.potter@amd.com    if (result < 0)
97611760Sbrandon.potter@amd.com        return -errno;
97711799Sbrandon.potter@amd.com#endif
97811760Sbrandon.potter@amd.com    return 0;
97911760Sbrandon.potter@amd.com}
98011760Sbrandon.potter@amd.com
98111760Sbrandon.potter@amd.comSyscallReturn
98211851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
98311851Sbrandon.potter@amd.com           int index)
9849455Smitch.hayenga+gem5@gmail.com{
9859455Smitch.hayenga+gem5@gmail.com    string path;
9869455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
98710223Ssteve.reinhardt@amd.com        return -EFAULT;
9889455Smitch.hayenga+gem5@gmail.com
9899455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9909455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9919455Smitch.hayenga+gem5@gmail.com
9929455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9939455Smitch.hayenga+gem5@gmail.com
9949455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9959455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9969455Smitch.hayenga+gem5@gmail.com}
99710203SAli.Saidi@ARM.com
99810203SAli.Saidi@ARM.comSyscallReturn
99911851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
100010203SAli.Saidi@ARM.com{
100110203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
100210203SAli.Saidi@ARM.com}
100310203SAli.Saidi@ARM.com
1004