syscall_emul.cc revision 11905
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 &&
1806687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1816687Stjones1@inf.ed.ac.uk                {
18210318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1838852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1846687Stjones1@inf.ed.ac.uk                }
1856687Stjones1@inf.ed.ac.uk            }
1862474SN/A        }
1871450SN/A    }
1885748SSteve.Reinhardt@amd.com
18911905SBrandon.Potter@amd.com    mem_state->setBrkPoint(new_brk);
19011380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
19111905SBrandon.Potter@amd.com                    mem_state->getBrkPoint());
19211905SBrandon.Potter@amd.com    return mem_state->getBrkPoint();
193360SN/A}
194360SN/A
19511886Sbrandon.potter@amd.comSyscallReturn
19611886Sbrandon.potter@amd.comsetTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
19711886Sbrandon.potter@amd.com                  ThreadContext *tc)
19811886Sbrandon.potter@amd.com{
19911886Sbrandon.potter@amd.com    int index = 0;
20011886Sbrandon.potter@amd.com    uint64_t tidPtr = process->getSyscallArg(tc, index);
20111886Sbrandon.potter@amd.com
20211886Sbrandon.potter@amd.com    process->childClearTID = tidPtr;
20311886Sbrandon.potter@amd.com    return process->pid();
20411886Sbrandon.potter@amd.com}
205360SN/A
2061450SN/ASyscallReturn
20711851Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
208360SN/A{
2096701Sgblack@eecs.umich.edu    int index = 0;
21010931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
21110931Sbrandon.potter@amd.com
21211856Sbrandon.potter@amd.com    return p->fds->closeFDEntry(tgt_fd);
213360SN/A}
214360SN/A
215360SN/A
2161450SN/ASyscallReturn
21711851Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
218360SN/A{
2196701Sgblack@eecs.umich.edu    int index = 0;
22010931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2216701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2226701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
22311856Sbrandon.potter@amd.com
22411856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
22511856Sbrandon.potter@amd.com    if (!hbfdp)
22611856Sbrandon.potter@amd.com        return -EBADF;
22711856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
22811856Sbrandon.potter@amd.com
2296701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
23010931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
231360SN/A
23211684Snderumigny@gmail.com    if (bytes_read > 0)
2338706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
234360SN/A
2351458SN/A    return bytes_read;
236360SN/A}
237360SN/A
2381450SN/ASyscallReturn
23911851Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
240360SN/A{
2416701Sgblack@eecs.umich.edu    int index = 0;
24210931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2436701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2446701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
24511856Sbrandon.potter@amd.com
24611856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
24711856Sbrandon.potter@amd.com    if (!hbfdp)
24811856Sbrandon.potter@amd.com        return -EBADF;
24911856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
25011856Sbrandon.potter@amd.com
2516701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
2528706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
253360SN/A
25410931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
255360SN/A
25610931Sbrandon.potter@amd.com    fsync(sim_fd);
257360SN/A
2581458SN/A    return bytes_written;
259360SN/A}
260360SN/A
261360SN/A
2621450SN/ASyscallReturn
26311851Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
264360SN/A{
2656701Sgblack@eecs.umich.edu    int index = 0;
26610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2676701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2686701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
269360SN/A
27011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
27111856Sbrandon.potter@amd.com    if (!ffdp)
27210931Sbrandon.potter@amd.com        return -EBADF;
27311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
27410931Sbrandon.potter@amd.com
27510931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
276360SN/A
2771458SN/A    return (result == (off_t)-1) ? -errno : result;
278360SN/A}
279360SN/A
280360SN/A
2811450SN/ASyscallReturn
28211851Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2834118Sgblack@eecs.umich.edu{
2846701Sgblack@eecs.umich.edu    int index = 0;
28510931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2866701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2876701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2886701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2896701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2904118Sgblack@eecs.umich.edu
29111856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
29211856Sbrandon.potter@amd.com    if (!ffdp)
29310931Sbrandon.potter@amd.com        return -EBADF;
29411856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
29510931Sbrandon.potter@amd.com
2964118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2974118Sgblack@eecs.umich.edu
29810931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
2994118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
3004118Sgblack@eecs.umich.edu
30111379Sbrandon.potter@amd.com    if (result == (off_t)-1)
3024118Sgblack@eecs.umich.edu        return -errno;
30311379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
30411379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
30511379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
30611379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
30711379Sbrandon.potter@amd.com    return 0;
3084118Sgblack@eecs.umich.edu}
3094118Sgblack@eecs.umich.edu
3104118Sgblack@eecs.umich.edu
3114118Sgblack@eecs.umich.eduSyscallReturn
31211851Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
313360SN/A{
31411383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
31511383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
31611383Sbrandon.potter@amd.com    // memory.
3171458SN/A    return 0;
318360SN/A}
319360SN/A
320360SN/A
321360SN/Aconst char *hostname = "m5.eecs.umich.edu";
322360SN/A
3231450SN/ASyscallReturn
32411851Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
325360SN/A{
3266701Sgblack@eecs.umich.edu    int index = 0;
3276701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3286701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3296701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
330360SN/A
331360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
332360SN/A
3338706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
334360SN/A
3351458SN/A    return 0;
336360SN/A}
337360SN/A
3381450SN/ASyscallReturn
33911851Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3405513SMichael.Adler@intel.com{
3415513SMichael.Adler@intel.com    int result = 0;
3426731Svince@csl.cornell.edu    int index = 0;
3436701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3446701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3456701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3465513SMichael.Adler@intel.com
3475513SMichael.Adler@intel.com    // Is current working directory defined?
3485513SMichael.Adler@intel.com    string cwd = p->getcwd();
3495513SMichael.Adler@intel.com    if (!cwd.empty()) {
3505513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3515513SMichael.Adler@intel.com            // Buffer too small
3525513SMichael.Adler@intel.com            return -ERANGE;
3535513SMichael.Adler@intel.com        }
3545513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3555513SMichael.Adler@intel.com        result = cwd.length();
35610955Sdavid.hashe@amd.com    } else {
35711856Sbrandon.potter@amd.com        if (getcwd((char *)buf.bufferPtr(), size)) {
3585513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
35910955Sdavid.hashe@amd.com        } else {
3605513SMichael.Adler@intel.com            result = -1;
3615513SMichael.Adler@intel.com        }
3625513SMichael.Adler@intel.com    }
3635513SMichael.Adler@intel.com
3648706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3655513SMichael.Adler@intel.com
3665513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3675513SMichael.Adler@intel.com}
3685513SMichael.Adler@intel.com
36910203SAli.Saidi@ARM.com/// Target open() handler.
37010203SAli.Saidi@ARM.comSyscallReturn
37111851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
37211851Sbrandon.potter@amd.com             ThreadContext *tc)
37310203SAli.Saidi@ARM.com{
37410203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
37510203SAli.Saidi@ARM.com}
3765513SMichael.Adler@intel.com
3775513SMichael.Adler@intel.comSyscallReturn
37811851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
37911851Sbrandon.potter@amd.com             int index)
3805513SMichael.Adler@intel.com{
3815513SMichael.Adler@intel.com    string path;
3825513SMichael.Adler@intel.com
3838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
38410223Ssteve.reinhardt@amd.com        return -EFAULT;
3855513SMichael.Adler@intel.com
3865513SMichael.Adler@intel.com    // Adjust path for current working directory
3875513SMichael.Adler@intel.com    path = p->fullPath(path);
3885513SMichael.Adler@intel.com
3896701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3906701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3916701Sgblack@eecs.umich.edu
3926701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3935513SMichael.Adler@intel.com
39410955Sdavid.hashe@amd.com    int result = -1;
39510955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
39610955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
39710955Sdavid.hashe@amd.com    } else {
39811140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
39911140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
40011851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
40111140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
40211140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
40311140Sjthestness@gmail.com        //     called binary calls readlink().
40411140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
40511140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
40611140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
40711140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
40811140Sjthestness@gmail.com
40911140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
41011140Sjthestness@gmail.com        char real_path[PATH_MAX];
41111140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
41211140Sjthestness@gmail.com        if (!check_real_path) {
41311140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
41411140Sjthestness@gmail.com                  "executable: %s", p->progName());
41511140Sjthestness@gmail.com        }
41611140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
41711140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
41811140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
41910955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
42010955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
42110955Sdavid.hashe@amd.com            result = bufsiz;
42210955Sdavid.hashe@amd.com        } else {
42311140Sjthestness@gmail.com            result = real_path_len;
42410955Sdavid.hashe@amd.com        }
42511140Sjthestness@gmail.com
42611140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
42711140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
42811140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
42911140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
43010955Sdavid.hashe@amd.com    }
4315513SMichael.Adler@intel.com
4328706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4335513SMichael.Adler@intel.com
4345513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4355513SMichael.Adler@intel.com}
4365513SMichael.Adler@intel.com
4375513SMichael.Adler@intel.comSyscallReturn
43811851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
439511SN/A{
44010633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
44110633Smichaelupton@gmail.com}
44210633Smichaelupton@gmail.com
44310633Smichaelupton@gmail.comSyscallReturn
44411851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
44511851Sbrandon.potter@amd.com             int index)
44610633Smichaelupton@gmail.com{
4471706SN/A    string path;
448360SN/A
4498852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
45010223Ssteve.reinhardt@amd.com        return -EFAULT;
451511SN/A
4523669Sbinkertn@umich.edu    // Adjust path for current working directory
4533669Sbinkertn@umich.edu    path = p->fullPath(path);
4543669Sbinkertn@umich.edu
455511SN/A    int result = unlink(path.c_str());
4561458SN/A    return (result == -1) ? -errno : result;
457511SN/A}
458511SN/A
4595513SMichael.Adler@intel.com
4605513SMichael.Adler@intel.comSyscallReturn
46111851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4625513SMichael.Adler@intel.com{
4635513SMichael.Adler@intel.com    string path;
4645513SMichael.Adler@intel.com
4656701Sgblack@eecs.umich.edu    int index = 0;
4668852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46710223Ssteve.reinhardt@amd.com        return -EFAULT;
4685513SMichael.Adler@intel.com
4695513SMichael.Adler@intel.com    // Adjust path for current working directory
4705513SMichael.Adler@intel.com    path = p->fullPath(path);
4715513SMichael.Adler@intel.com
4726701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4735513SMichael.Adler@intel.com
4745513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4755513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4765513SMichael.Adler@intel.com}
4775513SMichael.Adler@intel.com
4781450SN/ASyscallReturn
47911851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
480511SN/A{
4811706SN/A    string old_name;
482511SN/A
4836701Sgblack@eecs.umich.edu    int index = 0;
4848852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4851458SN/A        return -EFAULT;
486511SN/A
4871706SN/A    string new_name;
488511SN/A
4898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4901458SN/A        return -EFAULT;
491511SN/A
4923669Sbinkertn@umich.edu    // Adjust path for current working directory
4933669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4943669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4953669Sbinkertn@umich.edu
4961706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4971458SN/A    return (result == -1) ? -errno : result;
498511SN/A}
499511SN/A
5001706SN/ASyscallReturn
50111851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5021706SN/A{
5031706SN/A    string path;
5041706SN/A
5056701Sgblack@eecs.umich.edu    int index = 0;
5068852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5071706SN/A        return -EFAULT;
5081706SN/A
5096701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5101706SN/A
5113669Sbinkertn@umich.edu    // Adjust path for current working directory
5123669Sbinkertn@umich.edu    path = p->fullPath(path);
5133669Sbinkertn@umich.edu
5141706SN/A    int result = truncate(path.c_str(), length);
5151706SN/A    return (result == -1) ? -errno : result;
5161706SN/A}
5171706SN/A
5181706SN/ASyscallReturn
51911856Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5201706SN/A{
5216701Sgblack@eecs.umich.edu    int index = 0;
52211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
52311856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
5241706SN/A
52511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
52611856Sbrandon.potter@amd.com    if (!ffdp)
5271706SN/A        return -EBADF;
52811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5291706SN/A
53010931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5311706SN/A    return (result == -1) ? -errno : result;
5321706SN/A}
5331999SN/A
5341999SN/ASyscallReturn
5356703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
53611851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
5376703Svince@csl.cornell.edu{
5386703Svince@csl.cornell.edu    int index = 0;
5396703Svince@csl.cornell.edu    string path;
5406703Svince@csl.cornell.edu
5418852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5426703Svince@csl.cornell.edu       return -EFAULT;
5436703Svince@csl.cornell.edu
5446744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5456703Svince@csl.cornell.edu
5466703Svince@csl.cornell.edu    // Adjust path for current working directory
5476703Svince@csl.cornell.edu    path = process->fullPath(path);
5486703Svince@csl.cornell.edu
5496744SAli.Saidi@arm.com#if NO_STAT64
5506744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5516744SAli.Saidi@arm.com#else
5526703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5536744SAli.Saidi@arm.com#endif
5546703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5556703Svince@csl.cornell.edu}
5566703Svince@csl.cornell.edu
5576703Svince@csl.cornell.eduSyscallReturn
55811856Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5596685Stjones1@inf.ed.ac.uk{
5606701Sgblack@eecs.umich.edu    int index = 0;
56111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
56211856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
5636685Stjones1@inf.ed.ac.uk
56411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
56511856Sbrandon.potter@amd.com    if (!ffdp)
5666685Stjones1@inf.ed.ac.uk        return -EBADF;
56711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5686685Stjones1@inf.ed.ac.uk
5696744SAli.Saidi@arm.com#if NO_STAT64
57010931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5716744SAli.Saidi@arm.com#else
57210931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5736744SAli.Saidi@arm.com#endif
5746685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5756685Stjones1@inf.ed.ac.uk}
5766685Stjones1@inf.ed.ac.uk
5776685Stjones1@inf.ed.ac.ukSyscallReturn
57811851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5795513SMichael.Adler@intel.com{
5805513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5815513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5825513SMichael.Adler@intel.com    // changing anything.
5835513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5845513SMichael.Adler@intel.com    umask(oldMask);
5855521Snate@binkert.org    return (int)oldMask;
5865513SMichael.Adler@intel.com}
5875513SMichael.Adler@intel.com
5885513SMichael.Adler@intel.comSyscallReturn
58911851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5901999SN/A{
5911999SN/A    string path;
5921999SN/A
5936701Sgblack@eecs.umich.edu    int index = 0;
5948852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5951999SN/A        return -EFAULT;
5961999SN/A
5971999SN/A    /* XXX endianess */
5986701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5991999SN/A    uid_t hostOwner = owner;
6006701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6011999SN/A    gid_t hostGroup = group;
6021999SN/A
6033669Sbinkertn@umich.edu    // Adjust path for current working directory
6043669Sbinkertn@umich.edu    path = p->fullPath(path);
6053669Sbinkertn@umich.edu
6061999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6071999SN/A    return (result == -1) ? -errno : result;
6081999SN/A}
6091999SN/A
6101999SN/ASyscallReturn
61111856Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6121999SN/A{
6136701Sgblack@eecs.umich.edu    int index = 0;
61411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6151999SN/A
61611856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
61711856Sbrandon.potter@amd.com    if (!ffdp)
6181999SN/A        return -EBADF;
61911856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6201999SN/A
6211999SN/A    /* XXX endianess */
62211856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
6231999SN/A    uid_t hostOwner = owner;
62411856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
6251999SN/A    gid_t hostGroup = group;
6261999SN/A
62710931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6281999SN/A    return (result == -1) ? -errno : result;
6291999SN/A}
6302093SN/A
6312093SN/A
63211856Sbrandon.potter@amd.com/**
63311856Sbrandon.potter@amd.com * TODO: there's a bit more involved here since file descriptors created with
63411856Sbrandon.potter@amd.com * dup are supposed to share a file description. So, there is a problem with
63511856Sbrandon.potter@amd.com * maintaining fields like file offset or flags since an update to such a
63611856Sbrandon.potter@amd.com * field won't be reflected in the metadata for the fd entries that we
63711856Sbrandon.potter@amd.com * maintain to hold metadata for checkpoint restoration.
63811856Sbrandon.potter@amd.com */
6392093SN/ASyscallReturn
64011856Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6413079Sstever@eecs.umich.edu{
6426701Sgblack@eecs.umich.edu    int index = 0;
64311856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
64410931Sbrandon.potter@amd.com
64511856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
64611856Sbrandon.potter@amd.com    if (!old_hbfdp)
6473079Sstever@eecs.umich.edu        return -EBADF;
64811856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
6495282Srstrong@cs.ucsd.edu
65010781Snilay@cs.wisc.edu    int result = dup(sim_fd);
65111856Sbrandon.potter@amd.com    int local_errno = errno;
65211856Sbrandon.potter@amd.com
65311856Sbrandon.potter@amd.com    std::shared_ptr<FDEntry> new_fdep = old_hbfdp->clone();
65411856Sbrandon.potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(new_fdep);
65511856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
65611856Sbrandon.potter@amd.com
65711856Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : p->fds->allocFD(new_fdep);
6583079Sstever@eecs.umich.edu}
6593079Sstever@eecs.umich.edu
6603079Sstever@eecs.umich.eduSyscallReturn
66111856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6622093SN/A{
66311875Sbrandon.potter@amd.com    int arg;
6646701Sgblack@eecs.umich.edu    int index = 0;
66511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
66611875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6672093SN/A
66811856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
66911856Sbrandon.potter@amd.com    if (!hbfdp)
6702093SN/A        return -EBADF;
67111856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6722093SN/A
67311875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
67411875Sbrandon.potter@amd.com
6752093SN/A    switch (cmd) {
67611875Sbrandon.potter@amd.com      case F_GETFD:
67711875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
6782093SN/A
67911875Sbrandon.potter@amd.com      case F_SETFD: {
68011875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
68111875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
6822093SN/A        return 0;
68311875Sbrandon.potter@amd.com      }
6842093SN/A
68511875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
68611875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
68711875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
68811875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
68911875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
69011875Sbrandon.potter@amd.com      // subsequent fcntls.
69111875Sbrandon.potter@amd.com      case F_GETFL:
69211875Sbrandon.potter@amd.com      case F_SETFL: {
69311875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
69411875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
69511875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
69611875Sbrandon.potter@amd.com      }
6972093SN/A
6982093SN/A      default:
69911875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
7002093SN/A        return 0;
7012093SN/A    }
7022093SN/A}
7032093SN/A
7042238SN/ASyscallReturn
70511856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
7062687Sksewell@umich.edu{
7076701Sgblack@eecs.umich.edu    int index = 0;
70811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
7092687Sksewell@umich.edu
71011856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
71111856Sbrandon.potter@amd.com    if (!hbfdp)
7122687Sksewell@umich.edu        return -EBADF;
71311856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
7142687Sksewell@umich.edu
71511856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
7162687Sksewell@umich.edu    switch (cmd) {
7172687Sksewell@umich.edu      case 33: //F_GETLK64
71810931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7192687Sksewell@umich.edu        return -EMFILE;
7202687Sksewell@umich.edu
7212687Sksewell@umich.edu      case 34: // F_SETLK64
7222687Sksewell@umich.edu      case 35: // F_SETLKW64
72310931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
72410931Sbrandon.potter@amd.com             tgt_fd);
7252687Sksewell@umich.edu        return -EMFILE;
7262687Sksewell@umich.edu
7272687Sksewell@umich.edu      default:
7282687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7292687Sksewell@umich.edu        // to the underlying OS
73010931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
73110931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7322687Sksewell@umich.edu        // return 0;
7332687Sksewell@umich.edu    }
7342687Sksewell@umich.edu}
7352687Sksewell@umich.edu
7362687Sksewell@umich.eduSyscallReturn
73711851Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
73811851Sbrandon.potter@amd.com               ThreadContext *tc)
7392238SN/A{
74011856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
7412093SN/A
74211856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
74311856Sbrandon.potter@amd.com    if (pipe_retval < 0)
7442238SN/A        return pipe_retval;
7452238SN/A
74611856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
74711856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
7482238SN/A
74911856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
75011856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
75111856Sbrandon.potter@amd.com
75211856Sbrandon.potter@amd.com    tgt_fds[0] = process->fds->allocFD(rpfd);
75311856Sbrandon.potter@amd.com    tgt_fds[1] = process->fds->allocFD(wpfd);
75411856Sbrandon.potter@amd.com
75511856Sbrandon.potter@amd.com    /**
75611856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
75711856Sbrandon.potter@amd.com     * as the write end of the pipe.
75811856Sbrandon.potter@amd.com     */
75911856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
76011856Sbrandon.potter@amd.com
76111856Sbrandon.potter@amd.com    /**
76211856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
76311856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
76411856Sbrandon.potter@amd.com     */
76511856Sbrandon.potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
7662238SN/A    return sim_fds[0];
7672238SN/A}
7682238SN/A
76911885Sbrandon.potter@amd.comSyscallReturn
77011885Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, Process *process,
77111885Sbrandon.potter@amd.com            ThreadContext *tc)
77211885Sbrandon.potter@amd.com{
77311885Sbrandon.potter@amd.com    int index = 0;
77411885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
77511885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
77611885Sbrandon.potter@amd.com
77711885Sbrandon.potter@amd.com    if (pgid < 0)
77811885Sbrandon.potter@amd.com        return -EINVAL;
77911885Sbrandon.potter@amd.com
78011885Sbrandon.potter@amd.com    if (pid == 0) {
78111885Sbrandon.potter@amd.com        process->setpgid(process->pid());
78211885Sbrandon.potter@amd.com        return 0;
78311885Sbrandon.potter@amd.com    }
78411885Sbrandon.potter@amd.com
78511885Sbrandon.potter@amd.com    Process *matched_ph = NULL;
78611885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
78711885Sbrandon.potter@amd.com
78811885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
78911885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
79011885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
79111885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
79211885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
79311885Sbrandon.potter@amd.com
79411885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
79511885Sbrandon.potter@amd.com                matched_ph = walk_ph;
79611885Sbrandon.potter@amd.com        }
79711885Sbrandon.potter@amd.com    }
79811885Sbrandon.potter@amd.com
79911885Sbrandon.potter@amd.com    assert(matched_ph != NULL);
80011885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
80111885Sbrandon.potter@amd.com
80211885Sbrandon.potter@amd.com    return 0;
80311885Sbrandon.potter@amd.com}
8042238SN/A
8052238SN/ASyscallReturn
80611851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
80711851Sbrandon.potter@amd.com                 ThreadContext *tc)
8082238SN/A{
8092238SN/A    // Make up a PID.  There's no interprocess communication in
8102238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8112238SN/A    // not getting a unique value.
8122238SN/A
8133114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
8143114Sgblack@eecs.umich.edu    return process->pid();
8152238SN/A}
8162238SN/A
8172238SN/A
8182238SN/ASyscallReturn
81911851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
82011851Sbrandon.potter@amd.com                 ThreadContext *tc)
8212238SN/A{
8222238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
8232238SN/A    // simulation to be deterministic.
8242238SN/A
8252238SN/A    // EUID goes in r20.
8263114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
8275543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8282238SN/A}
8292238SN/A
8302238SN/A
8312238SN/ASyscallReturn
83211851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
83311851Sbrandon.potter@amd.com                 ThreadContext *tc)
8342238SN/A{
8352238SN/A    // Get current group ID.  EGID goes in r20.
8363114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
8373114Sgblack@eecs.umich.edu    return process->gid();
8382238SN/A}
8392238SN/A
8402238SN/A
8412238SN/ASyscallReturn
84211851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8432680Sktlim@umich.edu           ThreadContext *tc)
8442238SN/A{
8452238SN/A    // can't fathom why a benchmark would call this.
8466701Sgblack@eecs.umich.edu    int index = 0;
8476701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8482238SN/A    return 0;
8492238SN/A}
8502238SN/A
8512238SN/ASyscallReturn
85211851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
8532680Sktlim@umich.edu           ThreadContext *tc)
8542238SN/A{
85511885Sbrandon.potter@amd.com    return process->tgid();
85611885Sbrandon.potter@amd.com}
8572238SN/A
85811885Sbrandon.potter@amd.comSyscallReturn
85911885Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, Process *process,
86011885Sbrandon.potter@amd.com           ThreadContext *tc)
86111885Sbrandon.potter@amd.com{
8623114Sgblack@eecs.umich.edu    return process->pid();
8632238SN/A}
8642238SN/A
8652238SN/ASyscallReturn
86611851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
86711851Sbrandon.potter@amd.com            ThreadContext *tc)
8682238SN/A{
8693114Sgblack@eecs.umich.edu    return process->ppid();
8702238SN/A}
8712238SN/A
8722238SN/ASyscallReturn
87311851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8742680Sktlim@umich.edu           ThreadContext *tc)
8752238SN/A{
8765543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8772238SN/A}
8782238SN/A
8792238SN/ASyscallReturn
88011851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
88111851Sbrandon.potter@amd.com            ThreadContext *tc)
8822238SN/A{
8835543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8842238SN/A}
8852238SN/A
8862238SN/ASyscallReturn
88711851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
8882680Sktlim@umich.edu           ThreadContext *tc)
8892238SN/A{
8903114Sgblack@eecs.umich.edu    return process->gid();
8912238SN/A}
8922238SN/A
8932238SN/ASyscallReturn
89411851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
89511851Sbrandon.potter@amd.com            ThreadContext *tc)
8962238SN/A{
8973114Sgblack@eecs.umich.edu    return process->egid();
8982238SN/A}
8992238SN/A
9009455Smitch.hayenga+gem5@gmail.comSyscallReturn
90111856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
90211760Sbrandon.potter@amd.com{
90311799Sbrandon.potter@amd.com#if NO_FALLOCATE
90411799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
90511799Sbrandon.potter@amd.com#else
90611760Sbrandon.potter@amd.com    int index = 0;
90711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
90811856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
90911856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
91011856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
91111760Sbrandon.potter@amd.com
91211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
91311856Sbrandon.potter@amd.com    if (!ffdp)
91411760Sbrandon.potter@amd.com        return -EBADF;
91511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
91611760Sbrandon.potter@amd.com
91711760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
91811760Sbrandon.potter@amd.com    if (result < 0)
91911760Sbrandon.potter@amd.com        return -errno;
92011799Sbrandon.potter@amd.com#endif
92111760Sbrandon.potter@amd.com    return 0;
92211760Sbrandon.potter@amd.com}
92311760Sbrandon.potter@amd.com
92411760Sbrandon.potter@amd.comSyscallReturn
92511851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
92611851Sbrandon.potter@amd.com           int index)
9279455Smitch.hayenga+gem5@gmail.com{
9289455Smitch.hayenga+gem5@gmail.com    string path;
9299455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
93010223Ssteve.reinhardt@amd.com        return -EFAULT;
9319455Smitch.hayenga+gem5@gmail.com
9329455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9339455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9349455Smitch.hayenga+gem5@gmail.com
9359455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9369455Smitch.hayenga+gem5@gmail.com
9379455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9389455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9399455Smitch.hayenga+gem5@gmail.com}
94010203SAli.Saidi@ARM.com
94110203SAli.Saidi@ARM.comSyscallReturn
94211851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
94310203SAli.Saidi@ARM.com{
94410203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
94510203SAli.Saidi@ARM.com}
94610203SAli.Saidi@ARM.com
947