syscall_emul.cc revision 13995
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>
3513479Santhony.gutierrez@amd.com#include <sys/syscall.h>
36360SN/A#include <unistd.h>
37360SN/A
3811911SBrandon.Potter@amd.com#include <csignal>
396712Snate@binkert.org#include <iostream>
4013031Sbrandon.potter@amd.com#include <mutex>
41360SN/A#include <string>
42360SN/A
437680Sgblack@eecs.umich.edu#include "arch/utility.hh"
442474SN/A#include "base/chunk_generator.hh"
45360SN/A#include "base/trace.hh"
466658Snate@binkert.org#include "config/the_isa.hh"
472680Sktlim@umich.edu#include "cpu/thread_context.hh"
4812716Smichael.lebeane@amd.com#include "dev/net/dist_iface.hh"
492474SN/A#include "mem/page_table.hh"
5013031Sbrandon.potter@amd.com#include "sim/byteswap.hh"
51360SN/A#include "sim/process.hh"
528229Snate@binkert.org#include "sim/sim_exit.hh"
5311794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
5411794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
556029Ssteve.reinhardt@amd.com#include "sim/system.hh"
56360SN/A
57360SN/Ausing namespace std;
582107SN/Ausing namespace TheISA;
59360SN/A
6013933Sbrandon.potter@amd.comvoid
6113933Sbrandon.potter@amd.comwarnUnsupportedOS(std::string syscall_name)
6213933Sbrandon.potter@amd.com{
6313933Sbrandon.potter@amd.com    warn("Cannot invoke %s on host operating system.", syscall_name);
6413933Sbrandon.potter@amd.com}
6513933Sbrandon.potter@amd.com
661450SN/ASyscallReturn
6713995Sbrandon.potter@amd.comunimplementedFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
68360SN/A{
6911794Sbrandon.potter@amd.com    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
702484SN/A
712484SN/A    return 1;
72360SN/A}
73360SN/A
74360SN/A
751450SN/ASyscallReturn
7613995Sbrandon.potter@amd.comignoreFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
77360SN/A{
7811794Sbrandon.potter@amd.com    if (desc->needWarning()) {
7911794Sbrandon.potter@amd.com        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
8011794Sbrandon.potter@amd.com             "\n      (further warnings will be suppressed)" : "");
8110831Ssteve.reinhardt@amd.com    }
82360SN/A
838149SChris.Emmons@ARM.com    return 0;
848149SChris.Emmons@ARM.com}
858149SChris.Emmons@ARM.com
8611886Sbrandon.potter@amd.comstatic void
8711911SBrandon.Potter@amd.comexitFutexWake(ThreadContext *tc, Addr addr, uint64_t tgid)
8811886Sbrandon.potter@amd.com{
8911911SBrandon.Potter@amd.com    // Clear value at address pointed to by thread's childClearTID field.
9011911SBrandon.Potter@amd.com    BufferArg ctidBuf(addr, sizeof(long));
9111911SBrandon.Potter@amd.com    long *ctid = (long *)ctidBuf.bufferPtr();
9211911SBrandon.Potter@amd.com    *ctid = 0;
9311911SBrandon.Potter@amd.com    ctidBuf.copyOut(tc->getMemProxy());
9411886Sbrandon.potter@amd.com
9511911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
9611911SBrandon.Potter@amd.com    // Wake one of the waiting threads.
9711911SBrandon.Potter@amd.com    futex_map.wakeup(addr, tgid, 1);
9811911SBrandon.Potter@amd.com}
9911911SBrandon.Potter@amd.com
10011911SBrandon.Potter@amd.comstatic SyscallReturn
10113995Sbrandon.potter@amd.comexitImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool group)
10211911SBrandon.Potter@amd.com{
10311911SBrandon.Potter@amd.com    int index = 0;
10413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
10511911SBrandon.Potter@amd.com    int status = p->getSyscallArg(tc, index);
10611911SBrandon.Potter@amd.com
10711911SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
10811911SBrandon.Potter@amd.com
10911911SBrandon.Potter@amd.com    if (group)
11011911SBrandon.Potter@amd.com        *p->exitGroup = true;
11111911SBrandon.Potter@amd.com
11211911SBrandon.Potter@amd.com    if (p->childClearTID)
11311911SBrandon.Potter@amd.com        exitFutexWake(tc, p->childClearTID, p->tgid());
11411911SBrandon.Potter@amd.com
11511911SBrandon.Potter@amd.com    bool last_thread = true;
11611911SBrandon.Potter@amd.com    Process *parent = nullptr, *tg_lead = nullptr;
11711911SBrandon.Potter@amd.com    for (int i = 0; last_thread && i < sys->numContexts(); i++) {
11811911SBrandon.Potter@amd.com        Process *walk;
11911911SBrandon.Potter@amd.com        if (!(walk = sys->threadContexts[i]->getProcessPtr()))
12011911SBrandon.Potter@amd.com            continue;
12111911SBrandon.Potter@amd.com
12211911SBrandon.Potter@amd.com        /**
12311911SBrandon.Potter@amd.com         * Threads in a thread group require special handing. For instance,
12411911SBrandon.Potter@amd.com         * we send the SIGCHLD signal so that it appears that it came from
12511911SBrandon.Potter@amd.com         * the head of the group. We also only delete file descriptors if
12611911SBrandon.Potter@amd.com         * we are the last thread in the thread group.
12711911SBrandon.Potter@amd.com         */
12811911SBrandon.Potter@amd.com        if (walk->pid() == p->tgid())
12911911SBrandon.Potter@amd.com            tg_lead = walk;
13011911SBrandon.Potter@amd.com
13113644Sqtt2@cornell.edu        if ((sys->threadContexts[i]->status() != ThreadContext::Halted) &&
13213644Sqtt2@cornell.edu            (sys->threadContexts[i]->status() != ThreadContext::Halting) &&
13313644Sqtt2@cornell.edu            (walk != p)) {
13411911SBrandon.Potter@amd.com            /**
13511911SBrandon.Potter@amd.com             * Check if we share thread group with the pointer; this denotes
13611911SBrandon.Potter@amd.com             * that we are not the last thread active in the thread group.
13711911SBrandon.Potter@amd.com             * Note that setting this to false also prevents further
13811911SBrandon.Potter@amd.com             * iterations of the loop.
13911911SBrandon.Potter@amd.com             */
14013644Sqtt2@cornell.edu            if (walk->tgid() == p->tgid()) {
14113644Sqtt2@cornell.edu                /**
14213644Sqtt2@cornell.edu                 * If p is trying to exit_group and both walk and p are in
14313644Sqtt2@cornell.edu                 * the same thread group (i.e., sharing the same tgid),
14413644Sqtt2@cornell.edu                 * we need to halt walk's thread context. After all threads
14513644Sqtt2@cornell.edu                 * except p are halted, p becomes the last thread in the
14613644Sqtt2@cornell.edu                 * group.
14713644Sqtt2@cornell.edu                 *
14813644Sqtt2@cornell.edu                 * If p is not doing exit_group and there exists another
14913644Sqtt2@cornell.edu                 * active thread context in the group, last_thread is
15013644Sqtt2@cornell.edu                 * set to false to prevent the parent thread from killing
15113644Sqtt2@cornell.edu                 * all threads in the group.
15213644Sqtt2@cornell.edu                 */
15313644Sqtt2@cornell.edu                if (*(p->exitGroup)) {
15413644Sqtt2@cornell.edu                    sys->threadContexts[i]->halt();
15513644Sqtt2@cornell.edu                } else {
15613644Sqtt2@cornell.edu                    last_thread = false;
15713644Sqtt2@cornell.edu                }
15813644Sqtt2@cornell.edu            }
15911911SBrandon.Potter@amd.com
16011911SBrandon.Potter@amd.com            /**
16111911SBrandon.Potter@amd.com             * A corner case exists which involves execve(). After execve(),
16211911SBrandon.Potter@amd.com             * the execve will enable SIGCHLD in the process. The problem
16311911SBrandon.Potter@amd.com             * occurs when the exiting process is the root process in the
16411911SBrandon.Potter@amd.com             * system; there is no parent to receive the signal. We obviate
16511911SBrandon.Potter@amd.com             * this problem by setting the root process' ppid to zero in the
16611911SBrandon.Potter@amd.com             * Python configuration files. We really should handle the
16711911SBrandon.Potter@amd.com             * root/execve specific case more gracefully.
16811911SBrandon.Potter@amd.com             */
16911911SBrandon.Potter@amd.com            if (*p->sigchld && (p->ppid() != 0) && (walk->pid() == p->ppid()))
17011911SBrandon.Potter@amd.com                parent = walk;
17111886Sbrandon.potter@amd.com        }
17211886Sbrandon.potter@amd.com    }
17311911SBrandon.Potter@amd.com
17411911SBrandon.Potter@amd.com    if (last_thread) {
17511911SBrandon.Potter@amd.com        if (parent) {
17611911SBrandon.Potter@amd.com            assert(tg_lead);
17711911SBrandon.Potter@amd.com            sys->signalList.push_back(BasicSignal(tg_lead, parent, SIGCHLD));
17811911SBrandon.Potter@amd.com        }
17911911SBrandon.Potter@amd.com
18011911SBrandon.Potter@amd.com        /**
18111911SBrandon.Potter@amd.com         * Run though FD array of the exiting process and close all file
18211911SBrandon.Potter@amd.com         * descriptors except for the standard file descriptors.
18311911SBrandon.Potter@amd.com         * (The standard file descriptors are shared with gem5.)
18411911SBrandon.Potter@amd.com         */
18511911SBrandon.Potter@amd.com        for (int i = 0; i < p->fds->getSize(); i++) {
18611911SBrandon.Potter@amd.com            if ((*p->fds)[i])
18711911SBrandon.Potter@amd.com                p->fds->closeFDEntry(i);
18811911SBrandon.Potter@amd.com        }
18911911SBrandon.Potter@amd.com    }
19011911SBrandon.Potter@amd.com
19111911SBrandon.Potter@amd.com    tc->halt();
19213644Sqtt2@cornell.edu
19313644Sqtt2@cornell.edu    /**
19413644Sqtt2@cornell.edu     * check to see if there is no more active thread in the system. If so,
19513644Sqtt2@cornell.edu     * exit the simulation loop
19613644Sqtt2@cornell.edu     */
19713644Sqtt2@cornell.edu    int activeContexts = 0;
19813644Sqtt2@cornell.edu    for (auto &system: sys->systemList)
19913644Sqtt2@cornell.edu        activeContexts += system->numRunningContexts();
20013644Sqtt2@cornell.edu
20113644Sqtt2@cornell.edu    if (activeContexts == 0) {
20213644Sqtt2@cornell.edu        /**
20313644Sqtt2@cornell.edu         * Even though we are terminating the final thread context, dist-gem5
20413644Sqtt2@cornell.edu         * requires the simulation to remain active and provide
20513644Sqtt2@cornell.edu         * synchronization messages to the switch process. So we just halt
20613644Sqtt2@cornell.edu         * the last thread context and return. The simulation will be
20713644Sqtt2@cornell.edu         * terminated by dist-gem5 in a coordinated manner once all nodes
20813644Sqtt2@cornell.edu         * have signaled their readiness to exit. For non dist-gem5
20913644Sqtt2@cornell.edu         * simulations, readyToExit() always returns true.
21013644Sqtt2@cornell.edu         */
21113644Sqtt2@cornell.edu        if (!DistIface::readyToExit(0)) {
21213644Sqtt2@cornell.edu            return status;
21313644Sqtt2@cornell.edu        }
21413644Sqtt2@cornell.edu
21513644Sqtt2@cornell.edu        exitSimLoop("exiting with last active thread context", status & 0xff);
21613644Sqtt2@cornell.edu        return status;
21713644Sqtt2@cornell.edu    }
21813644Sqtt2@cornell.edu
21911911SBrandon.Potter@amd.com    return status;
22011886Sbrandon.potter@amd.com}
2218149SChris.Emmons@ARM.com
2228149SChris.Emmons@ARM.comSyscallReturn
22313995Sbrandon.potter@amd.comexitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
224360SN/A{
22513995Sbrandon.potter@amd.com    return exitImpl(desc, callnum, tc, false);
226360SN/A}
227360SN/A
2281450SN/ASyscallReturn
22913995Sbrandon.potter@amd.comexitGroupFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2306109Ssanchezd@stanford.edu{
23113995Sbrandon.potter@amd.com    return exitImpl(desc, callnum, tc, true);
2326109Ssanchezd@stanford.edu}
2336109Ssanchezd@stanford.edu
2346109Ssanchezd@stanford.eduSyscallReturn
23513995Sbrandon.potter@amd.comgetpagesizeFunc(SyscallDesc *desc, int num, ThreadContext *tc)
236360SN/A{
23710318Sandreas.hansson@arm.com    return (int)PageBytes;
238360SN/A}
239360SN/A
240360SN/A
2411450SN/ASyscallReturn
24213995Sbrandon.potter@amd.combrkFunc(SyscallDesc *desc, int num, ThreadContext *tc)
243360SN/A{
244360SN/A    // change brk addr to first arg
2456701Sgblack@eecs.umich.edu    int index = 0;
24613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
2476701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
2485748SSteve.Reinhardt@amd.com
24911905SBrandon.Potter@amd.com    std::shared_ptr<MemState> mem_state = p->memState;
25011905SBrandon.Potter@amd.com    Addr brk_point = mem_state->getBrkPoint();
25111905SBrandon.Potter@amd.com
2525748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
2535748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
2545748SSteve.Reinhardt@amd.com    if (new_brk == 0)
25511905SBrandon.Potter@amd.com        return brk_point;
2565748SSteve.Reinhardt@amd.com
25711905SBrandon.Potter@amd.com    if (new_brk > brk_point) {
2585748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
25911905SBrandon.Potter@amd.com        for (ChunkGenerator gen(brk_point,
26011905SBrandon.Potter@amd.com                                new_brk - brk_point,
26110318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
2625748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
26310318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
2646687Stjones1@inf.ed.ac.uk
2656687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
2666687Stjones1@inf.ed.ac.uk            else {
26711905SBrandon.Potter@amd.com                uint8_t zero = 0;
2688852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
2696687Stjones1@inf.ed.ac.uk
2706687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
27110318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
2726687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
2738852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
27410318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
2756687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
27611906SBrandon.Potter@amd.com                    p->pTable->translate(next_page)) {
27710318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
2788852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
2796687Stjones1@inf.ed.ac.uk                }
2806687Stjones1@inf.ed.ac.uk            }
2812474SN/A        }
2821450SN/A    }
2835748SSteve.Reinhardt@amd.com
28411905SBrandon.Potter@amd.com    mem_state->setBrkPoint(new_brk);
28511380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
28611905SBrandon.Potter@amd.com                    mem_state->getBrkPoint());
28711905SBrandon.Potter@amd.com    return mem_state->getBrkPoint();
288360SN/A}
289360SN/A
29011886Sbrandon.potter@amd.comSyscallReturn
29113995Sbrandon.potter@amd.comsetTidAddressFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
29211886Sbrandon.potter@amd.com{
29311886Sbrandon.potter@amd.com    int index = 0;
29413995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
29511886Sbrandon.potter@amd.com    uint64_t tidPtr = process->getSyscallArg(tc, index);
29611886Sbrandon.potter@amd.com
29711886Sbrandon.potter@amd.com    process->childClearTID = tidPtr;
29811886Sbrandon.potter@amd.com    return process->pid();
29911886Sbrandon.potter@amd.com}
300360SN/A
3011450SN/ASyscallReturn
30213995Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, ThreadContext *tc)
303360SN/A{
3046701Sgblack@eecs.umich.edu    int index = 0;
30513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
30610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
30710931Sbrandon.potter@amd.com
30811856Sbrandon.potter@amd.com    return p->fds->closeFDEntry(tgt_fd);
309360SN/A}
310360SN/A
3111450SN/ASyscallReturn
31213995Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, ThreadContext *tc)
313360SN/A{
3146701Sgblack@eecs.umich.edu    int index = 0;
31513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
31610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
3176701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
3186701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
319360SN/A
32011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
32111856Sbrandon.potter@amd.com    if (!ffdp)
32210931Sbrandon.potter@amd.com        return -EBADF;
32311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
32410931Sbrandon.potter@amd.com
32510931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
326360SN/A
3271458SN/A    return (result == (off_t)-1) ? -errno : result;
328360SN/A}
329360SN/A
330360SN/A
3311450SN/ASyscallReturn
33213995Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc)
3334118Sgblack@eecs.umich.edu{
3346701Sgblack@eecs.umich.edu    int index = 0;
33513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
33610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
3376701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
3386701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
3396701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
3406701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
3414118Sgblack@eecs.umich.edu
34211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
34311856Sbrandon.potter@amd.com    if (!ffdp)
34410931Sbrandon.potter@amd.com        return -EBADF;
34511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
34610931Sbrandon.potter@amd.com
3474118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
3484118Sgblack@eecs.umich.edu
34910931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
3504118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
3514118Sgblack@eecs.umich.edu
35211379Sbrandon.potter@amd.com    if (result == (off_t)-1)
3534118Sgblack@eecs.umich.edu        return -errno;
35411379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
35511379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
35611379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
35711379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
35811379Sbrandon.potter@amd.com    return 0;
3594118Sgblack@eecs.umich.edu}
3604118Sgblack@eecs.umich.edu
3614118Sgblack@eecs.umich.edu
3624118Sgblack@eecs.umich.eduSyscallReturn
36313995Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, ThreadContext *tc)
364360SN/A{
36511383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
36611383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
36711383Sbrandon.potter@amd.com    // memory.
3681458SN/A    return 0;
369360SN/A}
370360SN/A
371360SN/A
372360SN/Aconst char *hostname = "m5.eecs.umich.edu";
373360SN/A
3741450SN/ASyscallReturn
37513995Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, ThreadContext *tc)
376360SN/A{
3776701Sgblack@eecs.umich.edu    int index = 0;
37813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
37911906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3806701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
38111906SBrandon.Potter@amd.com    BufferArg name(buf_ptr, name_len);
382360SN/A
383360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
384360SN/A
3858706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
386360SN/A
3871458SN/A    return 0;
388360SN/A}
389360SN/A
3901450SN/ASyscallReturn
39113995Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, ThreadContext *tc)
3925513SMichael.Adler@intel.com{
3935513SMichael.Adler@intel.com    int result = 0;
3946731Svince@csl.cornell.edu    int index = 0;
39513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
39611906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3976701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
39811906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, size);
3995513SMichael.Adler@intel.com
4005513SMichael.Adler@intel.com    // Is current working directory defined?
40113883Sdavid.hashe@amd.com    string cwd = p->tgtCwd;
4025513SMichael.Adler@intel.com    if (!cwd.empty()) {
4035513SMichael.Adler@intel.com        if (cwd.length() >= size) {
4045513SMichael.Adler@intel.com            // Buffer too small
4055513SMichael.Adler@intel.com            return -ERANGE;
4065513SMichael.Adler@intel.com        }
4075513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
4085513SMichael.Adler@intel.com        result = cwd.length();
40910955Sdavid.hashe@amd.com    } else {
41011856Sbrandon.potter@amd.com        if (getcwd((char *)buf.bufferPtr(), size)) {
4115513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
41210955Sdavid.hashe@amd.com        } else {
4135513SMichael.Adler@intel.com            result = -1;
4145513SMichael.Adler@intel.com        }
4155513SMichael.Adler@intel.com    }
4165513SMichael.Adler@intel.com
4178706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4185513SMichael.Adler@intel.com
4195513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4205513SMichael.Adler@intel.com}
4215513SMichael.Adler@intel.com
42210203SAli.Saidi@ARM.comSyscallReturn
42313995Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
42410203SAli.Saidi@ARM.com{
42513995Sbrandon.potter@amd.com    return readlinkFunc(desc, callnum, tc, 0);
42610203SAli.Saidi@ARM.com}
4275513SMichael.Adler@intel.com
4285513SMichael.Adler@intel.comSyscallReturn
42913995Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc, int index)
4305513SMichael.Adler@intel.com{
4315513SMichael.Adler@intel.com    string path;
43213995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
4335513SMichael.Adler@intel.com
4348852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
43510223Ssteve.reinhardt@amd.com        return -EFAULT;
4365513SMichael.Adler@intel.com
43713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
43813883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
4395513SMichael.Adler@intel.com
44011906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
4416701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
4426701Sgblack@eecs.umich.edu
44311906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, bufsiz);
4445513SMichael.Adler@intel.com
44510955Sdavid.hashe@amd.com    int result = -1;
44610955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
44710955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
44810955Sdavid.hashe@amd.com    } else {
44911140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
45011140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
45111851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
45211140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
45311140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
45411140Sjthestness@gmail.com        //     called binary calls readlink().
45511140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
45611140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
45711140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
45811140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
45911140Sjthestness@gmail.com
46011140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
46111140Sjthestness@gmail.com        char real_path[PATH_MAX];
46211140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
46311140Sjthestness@gmail.com        if (!check_real_path) {
46411140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
46511140Sjthestness@gmail.com                  "executable: %s", p->progName());
46611140Sjthestness@gmail.com        }
46711140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
46811140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
46911140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
47010955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
47110955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
47210955Sdavid.hashe@amd.com            result = bufsiz;
47310955Sdavid.hashe@amd.com        } else {
47411140Sjthestness@gmail.com            result = real_path_len;
47510955Sdavid.hashe@amd.com        }
47611140Sjthestness@gmail.com
47711140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
47811140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
47911140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
48011140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
48110955Sdavid.hashe@amd.com    }
4825513SMichael.Adler@intel.com
4838706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4845513SMichael.Adler@intel.com
4855513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4865513SMichael.Adler@intel.com}
4875513SMichael.Adler@intel.com
4885513SMichael.Adler@intel.comSyscallReturn
48913995Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc)
490511SN/A{
49113995Sbrandon.potter@amd.com    return unlinkHelper(desc, num, tc, 0);
49210633Smichaelupton@gmail.com}
49310633Smichaelupton@gmail.com
49410633Smichaelupton@gmail.comSyscallReturn
49513995Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, ThreadContext *tc, int index)
49610633Smichaelupton@gmail.com{
4971706SN/A    string path;
49813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
499360SN/A
5008852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
50110223Ssteve.reinhardt@amd.com        return -EFAULT;
502511SN/A
50313883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
5043669Sbinkertn@umich.edu
505511SN/A    int result = unlink(path.c_str());
5061458SN/A    return (result == -1) ? -errno : result;
507511SN/A}
508511SN/A
50912795Smattdsinclair@gmail.comSyscallReturn
51013995Sbrandon.potter@amd.comlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc)
51112795Smattdsinclair@gmail.com{
51212795Smattdsinclair@gmail.com    string path;
51312795Smattdsinclair@gmail.com    string new_path;
51413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
51512795Smattdsinclair@gmail.com
51612795Smattdsinclair@gmail.com    int index = 0;
51712795Smattdsinclair@gmail.com    auto &virt_mem = tc->getMemProxy();
51812795Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index)))
51912795Smattdsinclair@gmail.com        return -EFAULT;
52012795Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index)))
52112795Smattdsinclair@gmail.com        return -EFAULT;
52212795Smattdsinclair@gmail.com
52313883Sdavid.hashe@amd.com    path = p->absolutePath(path, true);
52413883Sdavid.hashe@amd.com    new_path = p->absolutePath(new_path, true);
52512795Smattdsinclair@gmail.com
52612795Smattdsinclair@gmail.com    int result = link(path.c_str(), new_path.c_str());
52712795Smattdsinclair@gmail.com    return (result == -1) ? -errno : result;
52812795Smattdsinclair@gmail.com}
5295513SMichael.Adler@intel.com
5305513SMichael.Adler@intel.comSyscallReturn
53113995Sbrandon.potter@amd.comsymlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc)
53212796Smattdsinclair@gmail.com{
53312796Smattdsinclair@gmail.com    string path;
53412796Smattdsinclair@gmail.com    string new_path;
53513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
53612796Smattdsinclair@gmail.com
53712796Smattdsinclair@gmail.com    int index = 0;
53812796Smattdsinclair@gmail.com    auto &virt_mem = tc->getMemProxy();
53912796Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index)))
54012796Smattdsinclair@gmail.com        return -EFAULT;
54112796Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index)))
54212796Smattdsinclair@gmail.com        return -EFAULT;
54312796Smattdsinclair@gmail.com
54413883Sdavid.hashe@amd.com    path = p->absolutePath(path, true);
54513883Sdavid.hashe@amd.com    new_path = p->absolutePath(new_path, true);
54612796Smattdsinclair@gmail.com
54712796Smattdsinclair@gmail.com    int result = symlink(path.c_str(), new_path.c_str());
54812796Smattdsinclair@gmail.com    return (result == -1) ? -errno : result;
54912796Smattdsinclair@gmail.com}
55012796Smattdsinclair@gmail.com
55112796Smattdsinclair@gmail.comSyscallReturn
55213995Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, ThreadContext *tc)
5535513SMichael.Adler@intel.com{
55413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
5556701Sgblack@eecs.umich.edu    int index = 0;
55613883Sdavid.hashe@amd.com    std::string path;
5578852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
55810223Ssteve.reinhardt@amd.com        return -EFAULT;
5595513SMichael.Adler@intel.com
56013883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
5616701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
5625513SMichael.Adler@intel.com
56313883Sdavid.hashe@amd.com    auto result = mkdir(path.c_str(), mode);
5645513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
5655513SMichael.Adler@intel.com}
5665513SMichael.Adler@intel.com
5671450SN/ASyscallReturn
56813995Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, ThreadContext *tc)
569511SN/A{
5701706SN/A    string old_name;
57113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
572511SN/A
5736701Sgblack@eecs.umich.edu    int index = 0;
5748852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
5751458SN/A        return -EFAULT;
576511SN/A
5771706SN/A    string new_name;
578511SN/A
5798852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
5801458SN/A        return -EFAULT;
581511SN/A
58213883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
58313883Sdavid.hashe@amd.com    old_name = p->checkPathRedirect(old_name);
58413883Sdavid.hashe@amd.com    new_name = p->checkPathRedirect(new_name);
5853669Sbinkertn@umich.edu
5861706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
5871458SN/A    return (result == -1) ? -errno : result;
588511SN/A}
589511SN/A
5901706SN/ASyscallReturn
59113995Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc)
5921706SN/A{
5931706SN/A    string path;
59413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
5951706SN/A
5966701Sgblack@eecs.umich.edu    int index = 0;
5978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5981706SN/A        return -EFAULT;
5991706SN/A
6006701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
6011706SN/A
60213883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
60313883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
6043669Sbinkertn@umich.edu
6051706SN/A    int result = truncate(path.c_str(), length);
6061706SN/A    return (result == -1) ? -errno : result;
6071706SN/A}
6081706SN/A
6091706SN/ASyscallReturn
61013995Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc)
6111706SN/A{
6126701Sgblack@eecs.umich.edu    int index = 0;
61313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
61411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
61511856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
6161706SN/A
61711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
61811856Sbrandon.potter@amd.com    if (!ffdp)
6191706SN/A        return -EBADF;
62011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6211706SN/A
62210931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
6231706SN/A    return (result == -1) ? -errno : result;
6241706SN/A}
6251999SN/A
6261999SN/ASyscallReturn
62713995Sbrandon.potter@amd.comtruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc)
6286703Svince@csl.cornell.edu{
6296703Svince@csl.cornell.edu    int index = 0;
63013995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
6316703Svince@csl.cornell.edu    string path;
6326703Svince@csl.cornell.edu
6338852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
63411906SBrandon.Potter@amd.com        return -EFAULT;
6356703Svince@csl.cornell.edu
6366744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
6376703Svince@csl.cornell.edu
63813883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
63913883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
6406703Svince@csl.cornell.edu
6416744SAli.Saidi@arm.com#if NO_STAT64
6426744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
6436744SAli.Saidi@arm.com#else
6446703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
6456744SAli.Saidi@arm.com#endif
6466703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
6476703Svince@csl.cornell.edu}
6486703Svince@csl.cornell.edu
6496703Svince@csl.cornell.eduSyscallReturn
65013995Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc)
6516685Stjones1@inf.ed.ac.uk{
6526701Sgblack@eecs.umich.edu    int index = 0;
65313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
65411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
65511856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
6566685Stjones1@inf.ed.ac.uk
65711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
65811856Sbrandon.potter@amd.com    if (!ffdp)
6596685Stjones1@inf.ed.ac.uk        return -EBADF;
66011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6616685Stjones1@inf.ed.ac.uk
6626744SAli.Saidi@arm.com#if NO_STAT64
66310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
6646744SAli.Saidi@arm.com#else
66510931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
6666744SAli.Saidi@arm.com#endif
6676685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
6686685Stjones1@inf.ed.ac.uk}
6696685Stjones1@inf.ed.ac.uk
6706685Stjones1@inf.ed.ac.ukSyscallReturn
67113995Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, ThreadContext *tc)
6725513SMichael.Adler@intel.com{
6735513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
6745513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
6755513SMichael.Adler@intel.com    // changing anything.
6765513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
6775513SMichael.Adler@intel.com    umask(oldMask);
6785521Snate@binkert.org    return (int)oldMask;
6795513SMichael.Adler@intel.com}
6805513SMichael.Adler@intel.com
6815513SMichael.Adler@intel.comSyscallReturn
68213995Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, ThreadContext *tc)
6831999SN/A{
6841999SN/A    string path;
68513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
6861999SN/A
6876701Sgblack@eecs.umich.edu    int index = 0;
6888852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6891999SN/A        return -EFAULT;
6901999SN/A
6911999SN/A    /* XXX endianess */
6926701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
6931999SN/A    uid_t hostOwner = owner;
6946701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6951999SN/A    gid_t hostGroup = group;
6961999SN/A
69713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
69813883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
6993669Sbinkertn@umich.edu
7001999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
7011999SN/A    return (result == -1) ? -errno : result;
7021999SN/A}
7031999SN/A
7041999SN/ASyscallReturn
70513995Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, ThreadContext *tc)
7061999SN/A{
7076701Sgblack@eecs.umich.edu    int index = 0;
70813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
70911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
7101999SN/A
71111856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
71211856Sbrandon.potter@amd.com    if (!ffdp)
7131999SN/A        return -EBADF;
71411856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
7151999SN/A
7161999SN/A    /* XXX endianess */
71711856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
7181999SN/A    uid_t hostOwner = owner;
71911856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
7201999SN/A    gid_t hostGroup = group;
7211999SN/A
72210931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
7231999SN/A    return (result == -1) ? -errno : result;
7241999SN/A}
7252093SN/A
72611856Sbrandon.potter@amd.com/**
72711908SBrandon.Potter@amd.com * FIXME: The file description is not shared among file descriptors created
72811908SBrandon.Potter@amd.com * with dup. Really, it's difficult to maintain fields like file offset or
72911908SBrandon.Potter@amd.com * flags since an update to such a field won't be reflected in the metadata
73011908SBrandon.Potter@amd.com * for the fd entries that we maintain for checkpoint restoration.
73111856Sbrandon.potter@amd.com */
7322093SN/ASyscallReturn
73313995Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, ThreadContext *tc)
7343079Sstever@eecs.umich.edu{
7356701Sgblack@eecs.umich.edu    int index = 0;
73613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
73711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
73810931Sbrandon.potter@amd.com
73911856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
74011856Sbrandon.potter@amd.com    if (!old_hbfdp)
7413079Sstever@eecs.umich.edu        return -EBADF;
74211856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
7435282Srstrong@cs.ucsd.edu
74410781Snilay@cs.wisc.edu    int result = dup(sim_fd);
74511908SBrandon.Potter@amd.com    if (result == -1)
74611908SBrandon.Potter@amd.com        return -errno;
74711856Sbrandon.potter@amd.com
74811908SBrandon.Potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone());
74911856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
75011908SBrandon.Potter@amd.com    new_hbfdp->setCOE(false);
75111908SBrandon.Potter@amd.com    return p->fds->allocFD(new_hbfdp);
75211908SBrandon.Potter@amd.com}
75311856Sbrandon.potter@amd.com
75411908SBrandon.Potter@amd.comSyscallReturn
75513995Sbrandon.potter@amd.comdup2Func(SyscallDesc *desc, int num, ThreadContext *tc)
75611908SBrandon.Potter@amd.com{
75711908SBrandon.Potter@amd.com    int index = 0;
75813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
75911908SBrandon.Potter@amd.com    int old_tgt_fd = p->getSyscallArg(tc, index);
76011908SBrandon.Potter@amd.com    auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]);
76111908SBrandon.Potter@amd.com    if (!old_hbp)
76211908SBrandon.Potter@amd.com        return -EBADF;
76311908SBrandon.Potter@amd.com    int old_sim_fd = old_hbp->getSimFD();
76411908SBrandon.Potter@amd.com
76511908SBrandon.Potter@amd.com    /**
76611908SBrandon.Potter@amd.com     * We need a valid host file descriptor number to be able to pass into
76711908SBrandon.Potter@amd.com     * the second parameter for dup2 (newfd), but we don't know what the
76811908SBrandon.Potter@amd.com     * viable numbers are; we execute the open call to retrieve one.
76911908SBrandon.Potter@amd.com     */
77011908SBrandon.Potter@amd.com    int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY));
77111908SBrandon.Potter@amd.com    if (res_fd == -1)
77211908SBrandon.Potter@amd.com        return -errno;
77311908SBrandon.Potter@amd.com
77411908SBrandon.Potter@amd.com    int new_tgt_fd = p->getSyscallArg(tc, index);
77511908SBrandon.Potter@amd.com    auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]);
77611908SBrandon.Potter@amd.com    if (new_hbp)
77711908SBrandon.Potter@amd.com        p->fds->closeFDEntry(new_tgt_fd);
77811908SBrandon.Potter@amd.com    new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone());
77911908SBrandon.Potter@amd.com    new_hbp->setSimFD(res_fd);
78011908SBrandon.Potter@amd.com    new_hbp->setCOE(false);
78111908SBrandon.Potter@amd.com
78211908SBrandon.Potter@amd.com    return p->fds->allocFD(new_hbp);
7833079Sstever@eecs.umich.edu}
7843079Sstever@eecs.umich.edu
7853079Sstever@eecs.umich.eduSyscallReturn
78613995Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, ThreadContext *tc)
7872093SN/A{
78811875Sbrandon.potter@amd.com    int arg;
7896701Sgblack@eecs.umich.edu    int index = 0;
79013995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
79111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
79211875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
7932093SN/A
79411856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
79511856Sbrandon.potter@amd.com    if (!hbfdp)
7962093SN/A        return -EBADF;
79711856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
7982093SN/A
79911875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
80011875Sbrandon.potter@amd.com
8012093SN/A    switch (cmd) {
80211875Sbrandon.potter@amd.com      case F_GETFD:
80311875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
8042093SN/A
80511875Sbrandon.potter@amd.com      case F_SETFD: {
80611875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
80711875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
8082093SN/A        return 0;
80911875Sbrandon.potter@amd.com      }
8102093SN/A
81111875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
81211875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
81311875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
81411875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
81511875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
81611875Sbrandon.potter@amd.com      // subsequent fcntls.
81711875Sbrandon.potter@amd.com      case F_GETFL:
81811875Sbrandon.potter@amd.com      case F_SETFL: {
81911875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
82011875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
82111875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
82211875Sbrandon.potter@amd.com      }
8232093SN/A
8242093SN/A      default:
82511875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
8262093SN/A        return 0;
8272093SN/A    }
8282093SN/A}
8292093SN/A
8302238SN/ASyscallReturn
83113995Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, ThreadContext *tc)
8322687Sksewell@umich.edu{
8336701Sgblack@eecs.umich.edu    int index = 0;
83413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
83511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
8362687Sksewell@umich.edu
83711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
83811856Sbrandon.potter@amd.com    if (!hbfdp)
8392687Sksewell@umich.edu        return -EBADF;
84011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
8412687Sksewell@umich.edu
84211856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
8432687Sksewell@umich.edu    switch (cmd) {
8442687Sksewell@umich.edu      case 33: //F_GETLK64
84510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
8462687Sksewell@umich.edu        return -EMFILE;
8472687Sksewell@umich.edu
8482687Sksewell@umich.edu      case 34: // F_SETLK64
8492687Sksewell@umich.edu      case 35: // F_SETLKW64
85010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
85110931Sbrandon.potter@amd.com             tgt_fd);
8522687Sksewell@umich.edu        return -EMFILE;
8532687Sksewell@umich.edu
8542687Sksewell@umich.edu      default:
8552687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
8562687Sksewell@umich.edu        // to the underlying OS
85710931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
85810931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
8592687Sksewell@umich.edu    }
8602687Sksewell@umich.edu}
8612687Sksewell@umich.edu
8622687Sksewell@umich.eduSyscallReturn
86313995Sbrandon.potter@amd.compipeImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool pseudoPipe)
8642238SN/A{
86511856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
86613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
8672093SN/A
86811856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
86911908SBrandon.Potter@amd.com    if (pipe_retval == -1)
87011908SBrandon.Potter@amd.com        return -errno;
8712238SN/A
87211856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
87311856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
87411908SBrandon.Potter@amd.com    tgt_fds[0] = p->fds->allocFD(rpfd);
8752238SN/A
87611856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
87711856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
87811908SBrandon.Potter@amd.com    tgt_fds[1] = p->fds->allocFD(wpfd);
87911856Sbrandon.potter@amd.com
88011856Sbrandon.potter@amd.com    /**
88111856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
88211856Sbrandon.potter@amd.com     * as the write end of the pipe.
88311856Sbrandon.potter@amd.com     */
88411856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
88511856Sbrandon.potter@amd.com
88611856Sbrandon.potter@amd.com    /**
88711856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
88811856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
88911856Sbrandon.potter@amd.com     */
89011908SBrandon.Potter@amd.com    if (pseudoPipe) {
89111908SBrandon.Potter@amd.com        tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
89211908SBrandon.Potter@amd.com        return tgt_fds[0];
89311908SBrandon.Potter@amd.com    }
89411908SBrandon.Potter@amd.com
89513934Sjoseph.gross@amd.com    int index = 0;
89613934Sjoseph.gross@amd.com    Addr tgt_addr = p->getSyscallArg(tc, index);
89713934Sjoseph.gross@amd.com
89811908SBrandon.Potter@amd.com    /**
89911908SBrandon.Potter@amd.com     * Copy the target file descriptors into buffer space and then copy
90011908SBrandon.Potter@amd.com     * the buffer space back into the target address space.
90111908SBrandon.Potter@amd.com     */
90211908SBrandon.Potter@amd.com    BufferArg tgt_handle(tgt_addr, sizeof(int[2]));
90311908SBrandon.Potter@amd.com    int *buf_ptr = (int*)tgt_handle.bufferPtr();
90411908SBrandon.Potter@amd.com    buf_ptr[0] = tgt_fds[0];
90511908SBrandon.Potter@amd.com    buf_ptr[1] = tgt_fds[1];
90611908SBrandon.Potter@amd.com    tgt_handle.copyOut(tc->getMemProxy());
90711908SBrandon.Potter@amd.com    return 0;
90811908SBrandon.Potter@amd.com}
90911908SBrandon.Potter@amd.com
91011908SBrandon.Potter@amd.comSyscallReturn
91113995Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
91211908SBrandon.Potter@amd.com{
91313995Sbrandon.potter@amd.com    return pipeImpl(desc, callnum, tc, true);
91411908SBrandon.Potter@amd.com}
91511908SBrandon.Potter@amd.com
91611908SBrandon.Potter@amd.comSyscallReturn
91713995Sbrandon.potter@amd.compipeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
91811908SBrandon.Potter@amd.com{
91913995Sbrandon.potter@amd.com    return pipeImpl(desc, callnum, tc, false);
9202238SN/A}
9212238SN/A
92211885Sbrandon.potter@amd.comSyscallReturn
92313995Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
92411885Sbrandon.potter@amd.com{
92511885Sbrandon.potter@amd.com    int index = 0;
92613995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
92711885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
92811885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
92911885Sbrandon.potter@amd.com
93011885Sbrandon.potter@amd.com    if (pgid < 0)
93111885Sbrandon.potter@amd.com        return -EINVAL;
93211885Sbrandon.potter@amd.com
93311885Sbrandon.potter@amd.com    if (pid == 0) {
93411885Sbrandon.potter@amd.com        process->setpgid(process->pid());
93511885Sbrandon.potter@amd.com        return 0;
93611885Sbrandon.potter@amd.com    }
93711885Sbrandon.potter@amd.com
93811913SBrandon.Potter@amd.com    Process *matched_ph = nullptr;
93911885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
94011885Sbrandon.potter@amd.com
94111885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
94211885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
94311885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
94411885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
94511885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
94611885Sbrandon.potter@amd.com
94711885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
94811885Sbrandon.potter@amd.com                matched_ph = walk_ph;
94911885Sbrandon.potter@amd.com        }
95011885Sbrandon.potter@amd.com    }
95111885Sbrandon.potter@amd.com
95211913SBrandon.Potter@amd.com    assert(matched_ph);
95311885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
95411885Sbrandon.potter@amd.com
95511885Sbrandon.potter@amd.com    return 0;
95611885Sbrandon.potter@amd.com}
9572238SN/A
9582238SN/ASyscallReturn
95913995Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
9602238SN/A{
9612238SN/A    // Make up a PID.  There's no interprocess communication in
9622238SN/A    // fake_syscall mode, so there's no way for a process to know it's
9632238SN/A    // not getting a unique value.
9642238SN/A
96513995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
9663114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
9673114Sgblack@eecs.umich.edu    return process->pid();
9682238SN/A}
9692238SN/A
9702238SN/A
9712238SN/ASyscallReturn
97213995Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
9732238SN/A{
9742238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
9752238SN/A    // simulation to be deterministic.
9762238SN/A
9772238SN/A    // EUID goes in r20.
97813995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
97911906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID
98011906SBrandon.Potter@amd.com    return process->uid(); // UID
9812238SN/A}
9822238SN/A
9832238SN/A
9842238SN/ASyscallReturn
98513995Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
9862238SN/A{
9872238SN/A    // Get current group ID.  EGID goes in r20.
98813995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
98911906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID
9903114Sgblack@eecs.umich.edu    return process->gid();
9912238SN/A}
9922238SN/A
9932238SN/A
9942238SN/ASyscallReturn
99513995Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
9962238SN/A{
9972238SN/A    // can't fathom why a benchmark would call this.
9986701Sgblack@eecs.umich.edu    int index = 0;
99913995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10006701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
10012238SN/A    return 0;
10022238SN/A}
10032238SN/A
10042238SN/ASyscallReturn
100513995Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10062238SN/A{
100713995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
100811885Sbrandon.potter@amd.com    return process->tgid();
100911885Sbrandon.potter@amd.com}
10102238SN/A
101111885Sbrandon.potter@amd.comSyscallReturn
101213995Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
101311885Sbrandon.potter@amd.com{
101413995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10153114Sgblack@eecs.umich.edu    return process->pid();
10162238SN/A}
10172238SN/A
10182238SN/ASyscallReturn
101913995Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10202238SN/A{
102113995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10223114Sgblack@eecs.umich.edu    return process->ppid();
10232238SN/A}
10242238SN/A
10252238SN/ASyscallReturn
102613995Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10272238SN/A{
102813995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10295543Ssaidi@eecs.umich.edu    return process->uid();              // UID
10302238SN/A}
10312238SN/A
10322238SN/ASyscallReturn
103313995Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10342238SN/A{
103513995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10365543Ssaidi@eecs.umich.edu    return process->euid();             // UID
10372238SN/A}
10382238SN/A
10392238SN/ASyscallReturn
104013995Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10412238SN/A{
104213995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10433114Sgblack@eecs.umich.edu    return process->gid();
10442238SN/A}
10452238SN/A
10462238SN/ASyscallReturn
104713995Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10482238SN/A{
104913995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10503114Sgblack@eecs.umich.edu    return process->egid();
10512238SN/A}
10522238SN/A
10539455Smitch.hayenga+gem5@gmail.comSyscallReturn
105413995Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
105511760Sbrandon.potter@amd.com{
105613936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
105711760Sbrandon.potter@amd.com    int index = 0;
105813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
105911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
106011856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
106111856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
106211856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
106311760Sbrandon.potter@amd.com
106411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
106511856Sbrandon.potter@amd.com    if (!ffdp)
106611760Sbrandon.potter@amd.com        return -EBADF;
106711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
106811760Sbrandon.potter@amd.com
106911760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
107011760Sbrandon.potter@amd.com    if (result < 0)
107111760Sbrandon.potter@amd.com        return -errno;
107213933Sbrandon.potter@amd.com    return 0;
107313933Sbrandon.potter@amd.com#else
107413933Sbrandon.potter@amd.com    warnUnsupportedOS("fallocate");
107513933Sbrandon.potter@amd.com    return -1;
107611799Sbrandon.potter@amd.com#endif
107711760Sbrandon.potter@amd.com}
107811760Sbrandon.potter@amd.com
107911760Sbrandon.potter@amd.comSyscallReturn
108013995Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, ThreadContext *tc, int index)
10819455Smitch.hayenga+gem5@gmail.com{
10829455Smitch.hayenga+gem5@gmail.com    string path;
108313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
10849455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
108510223Ssteve.reinhardt@amd.com        return -EFAULT;
10869455Smitch.hayenga+gem5@gmail.com
108713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
108813883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
10899455Smitch.hayenga+gem5@gmail.com
10909455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
10919455Smitch.hayenga+gem5@gmail.com
10929455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
10939455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
10949455Smitch.hayenga+gem5@gmail.com}
109510203SAli.Saidi@ARM.com
109610203SAli.Saidi@ARM.comSyscallReturn
109713995Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
109810203SAli.Saidi@ARM.com{
109913995Sbrandon.potter@amd.com    return accessFunc(desc, callnum, tc, 0);
110010203SAli.Saidi@ARM.com}
110110203SAli.Saidi@ARM.com
110213031Sbrandon.potter@amd.comSyscallReturn
110313995Sbrandon.potter@amd.commknodFunc(SyscallDesc *desc, int num, ThreadContext *tc)
110413031Sbrandon.potter@amd.com{
110513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
110613031Sbrandon.potter@amd.com    int index = 0;
110713031Sbrandon.potter@amd.com    std::string path;
110813031Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
110913031Sbrandon.potter@amd.com        return -EFAULT;
111013031Sbrandon.potter@amd.com
111113883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
111213031Sbrandon.potter@amd.com    mode_t mode = p->getSyscallArg(tc, index);
111313031Sbrandon.potter@amd.com    dev_t dev = p->getSyscallArg(tc, index);
111413031Sbrandon.potter@amd.com
111513031Sbrandon.potter@amd.com    auto result = mknod(path.c_str(), mode, dev);
111613031Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
111713031Sbrandon.potter@amd.com}
111813031Sbrandon.potter@amd.com
111913031Sbrandon.potter@amd.comSyscallReturn
112013995Sbrandon.potter@amd.comchdirFunc(SyscallDesc *desc, int num, ThreadContext *tc)
112113031Sbrandon.potter@amd.com{
112213995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
112313031Sbrandon.potter@amd.com    int index = 0;
112413031Sbrandon.potter@amd.com    std::string path;
112513031Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
112613031Sbrandon.potter@amd.com        return -EFAULT;
112713031Sbrandon.potter@amd.com
112813883Sdavid.hashe@amd.com    std::string tgt_cwd;
112913883Sdavid.hashe@amd.com    if (startswith(path, "/")) {
113013883Sdavid.hashe@amd.com        tgt_cwd = path;
113113883Sdavid.hashe@amd.com    } else {
113213883Sdavid.hashe@amd.com        char buf[PATH_MAX];
113313883Sdavid.hashe@amd.com        tgt_cwd = realpath((p->tgtCwd + "/" + path).c_str(), buf);
113413883Sdavid.hashe@amd.com    }
113513883Sdavid.hashe@amd.com    std::string host_cwd = p->checkPathRedirect(tgt_cwd);
113613031Sbrandon.potter@amd.com
113713883Sdavid.hashe@amd.com    int result = chdir(host_cwd.c_str());
113813883Sdavid.hashe@amd.com
113913883Sdavid.hashe@amd.com    if (result == -1)
114013883Sdavid.hashe@amd.com        return -errno;
114113883Sdavid.hashe@amd.com
114213883Sdavid.hashe@amd.com    p->hostCwd = host_cwd;
114313883Sdavid.hashe@amd.com    p->tgtCwd = tgt_cwd;
114413883Sdavid.hashe@amd.com    return result;
114513031Sbrandon.potter@amd.com}
114613031Sbrandon.potter@amd.com
114713031Sbrandon.potter@amd.comSyscallReturn
114813995Sbrandon.potter@amd.comrmdirFunc(SyscallDesc *desc, int num, ThreadContext *tc)
114913031Sbrandon.potter@amd.com{
115013995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
115113031Sbrandon.potter@amd.com    int index = 0;
115213031Sbrandon.potter@amd.com    std::string path;
115313031Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
115413031Sbrandon.potter@amd.com        return -EFAULT;
115513031Sbrandon.potter@amd.com
115613883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
115713031Sbrandon.potter@amd.com
115813031Sbrandon.potter@amd.com    auto result = rmdir(path.c_str());
115913031Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
116013031Sbrandon.potter@amd.com}
116113031Sbrandon.potter@amd.com
116213539Sjavier.setoain@arm.com#if defined(SYS_getdents) || defined(SYS_getdents64)
116313539Sjavier.setoain@arm.comtemplate<typename DE, int SYS_NUM>
116413539Sjavier.setoain@arm.comstatic SyscallReturn
116513995Sbrandon.potter@amd.comgetdentsImpl(SyscallDesc *desc, int callnum, ThreadContext *tc)
116613031Sbrandon.potter@amd.com{
116713031Sbrandon.potter@amd.com    int index = 0;
116813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
116913031Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
117013031Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
117113031Sbrandon.potter@amd.com    unsigned count = p->getSyscallArg(tc, index);
117213031Sbrandon.potter@amd.com
117313031Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
117413031Sbrandon.potter@amd.com    if (!hbfdp)
117513031Sbrandon.potter@amd.com        return -EBADF;
117613031Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
117713031Sbrandon.potter@amd.com
117813031Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, count);
117913539Sjavier.setoain@arm.com    auto status = syscall(SYS_NUM, sim_fd, buf_arg.bufferPtr(), count);
118013031Sbrandon.potter@amd.com
118113031Sbrandon.potter@amd.com    if (status == -1)
118213031Sbrandon.potter@amd.com        return -errno;
118313031Sbrandon.potter@amd.com
118413031Sbrandon.potter@amd.com    unsigned traversed = 0;
118513031Sbrandon.potter@amd.com    while (traversed < status) {
118613539Sjavier.setoain@arm.com        DE *buffer = (DE*)((Addr)buf_arg.bufferPtr() + traversed);
118713031Sbrandon.potter@amd.com
118813031Sbrandon.potter@amd.com        auto host_reclen = buffer->d_reclen;
118913031Sbrandon.potter@amd.com
119013031Sbrandon.potter@amd.com        /**
119113031Sbrandon.potter@amd.com         * Convert the byte ordering from the host to the target before
119213031Sbrandon.potter@amd.com         * passing the data back into the target's address space to preserve
119313031Sbrandon.potter@amd.com         * endianness.
119413031Sbrandon.potter@amd.com         */
119513031Sbrandon.potter@amd.com        buffer->d_ino = htog(buffer->d_ino);
119613031Sbrandon.potter@amd.com        buffer->d_off = htog(buffer->d_off);
119713031Sbrandon.potter@amd.com        buffer->d_reclen = htog(buffer->d_reclen);
119813031Sbrandon.potter@amd.com
119913031Sbrandon.potter@amd.com        traversed += host_reclen;
120013031Sbrandon.potter@amd.com    }
120113031Sbrandon.potter@amd.com
120213031Sbrandon.potter@amd.com    buf_arg.copyOut(tc->getMemProxy());
120313031Sbrandon.potter@amd.com    return status;
120413031Sbrandon.potter@amd.com}
120513448Sciro.santilli@arm.com#endif
120613539Sjavier.setoain@arm.com
120713539Sjavier.setoain@arm.com#if defined(SYS_getdents)
120813539Sjavier.setoain@arm.comSyscallReturn
120913995Sbrandon.potter@amd.comgetdentsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
121013539Sjavier.setoain@arm.com{
121113539Sjavier.setoain@arm.com    typedef struct linux_dirent {
121213539Sjavier.setoain@arm.com        unsigned long d_ino;
121313539Sjavier.setoain@arm.com        unsigned long d_off;
121413539Sjavier.setoain@arm.com        unsigned short d_reclen;
121513539Sjavier.setoain@arm.com        char dname[];
121613539Sjavier.setoain@arm.com    } LinDent;
121713539Sjavier.setoain@arm.com
121813995Sbrandon.potter@amd.com    return getdentsImpl<LinDent, SYS_getdents>(desc, callnum, tc);
121913539Sjavier.setoain@arm.com}
122013539Sjavier.setoain@arm.com#endif
122113539Sjavier.setoain@arm.com
122213539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
122313539Sjavier.setoain@arm.comSyscallReturn
122413995Sbrandon.potter@amd.comgetdents64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
122513539Sjavier.setoain@arm.com{
122613539Sjavier.setoain@arm.com    typedef struct linux_dirent64 {
122713539Sjavier.setoain@arm.com        ino64_t d_ino;
122813539Sjavier.setoain@arm.com        off64_t d_off;
122913539Sjavier.setoain@arm.com        unsigned short d_reclen;
123013539Sjavier.setoain@arm.com        char dname[];
123113539Sjavier.setoain@arm.com    } LinDent64;
123213539Sjavier.setoain@arm.com
123313995Sbrandon.potter@amd.com    return getdentsImpl<LinDent64, SYS_getdents64>(desc, callnum, tc);
123413539Sjavier.setoain@arm.com}
123513539Sjavier.setoain@arm.com#endif
123613568Sbrandon.potter@amd.com
123713568Sbrandon.potter@amd.comSyscallReturn
123813995Sbrandon.potter@amd.comshutdownFunc(SyscallDesc *desc, int num, ThreadContext *tc)
123913568Sbrandon.potter@amd.com{
124013568Sbrandon.potter@amd.com    int index = 0;
124113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
124213568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
124313568Sbrandon.potter@amd.com    int how = p->getSyscallArg(tc, index);
124413568Sbrandon.potter@amd.com
124513568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
124613568Sbrandon.potter@amd.com    if (!sfdp)
124713568Sbrandon.potter@amd.com        return -EBADF;
124813568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
124913568Sbrandon.potter@amd.com
125013568Sbrandon.potter@amd.com    int retval = shutdown(sim_fd, how);
125113568Sbrandon.potter@amd.com
125213568Sbrandon.potter@amd.com    return (retval == -1) ? -errno : retval;
125313568Sbrandon.potter@amd.com}
125413568Sbrandon.potter@amd.com
125513568Sbrandon.potter@amd.comSyscallReturn
125613995Sbrandon.potter@amd.combindFunc(SyscallDesc *desc, int num, ThreadContext *tc)
125713568Sbrandon.potter@amd.com{
125813568Sbrandon.potter@amd.com    int index = 0;
125913995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
126013568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
126113568Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
126213568Sbrandon.potter@amd.com    int addrlen = p->getSyscallArg(tc, index);
126313568Sbrandon.potter@amd.com
126413568Sbrandon.potter@amd.com    BufferArg bufSock(buf_ptr, addrlen);
126513568Sbrandon.potter@amd.com    bufSock.copyIn(tc->getMemProxy());
126613568Sbrandon.potter@amd.com
126713568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
126813568Sbrandon.potter@amd.com    if (!sfdp)
126913568Sbrandon.potter@amd.com        return -EBADF;
127013568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
127113568Sbrandon.potter@amd.com
127213568Sbrandon.potter@amd.com    int status = ::bind(sim_fd,
127313568Sbrandon.potter@amd.com                        (struct sockaddr *)bufSock.bufferPtr(),
127413568Sbrandon.potter@amd.com                        addrlen);
127513568Sbrandon.potter@amd.com
127613568Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
127713568Sbrandon.potter@amd.com}
127813568Sbrandon.potter@amd.com
127913568Sbrandon.potter@amd.comSyscallReturn
128013995Sbrandon.potter@amd.comlistenFunc(SyscallDesc *desc, int num, ThreadContext *tc)
128113568Sbrandon.potter@amd.com{
128213568Sbrandon.potter@amd.com    int index = 0;
128313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
128413568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
128513568Sbrandon.potter@amd.com    int backlog = p->getSyscallArg(tc, index);
128613568Sbrandon.potter@amd.com
128713568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
128813568Sbrandon.potter@amd.com    if (!sfdp)
128913568Sbrandon.potter@amd.com        return -EBADF;
129013568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
129113568Sbrandon.potter@amd.com
129213568Sbrandon.potter@amd.com    int status = listen(sim_fd, backlog);
129313568Sbrandon.potter@amd.com
129413568Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
129513568Sbrandon.potter@amd.com}
129613568Sbrandon.potter@amd.com
129713568Sbrandon.potter@amd.comSyscallReturn
129813995Sbrandon.potter@amd.comconnectFunc(SyscallDesc *desc, int num, ThreadContext *tc)
129913568Sbrandon.potter@amd.com{
130013568Sbrandon.potter@amd.com    int index = 0;
130113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
130213568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
130313568Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
130413568Sbrandon.potter@amd.com    int addrlen = p->getSyscallArg(tc, index);
130513568Sbrandon.potter@amd.com
130613568Sbrandon.potter@amd.com    BufferArg addr(buf_ptr, addrlen);
130713568Sbrandon.potter@amd.com    addr.copyIn(tc->getMemProxy());
130813568Sbrandon.potter@amd.com
130913568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
131013568Sbrandon.potter@amd.com    if (!sfdp)
131113568Sbrandon.potter@amd.com        return -EBADF;
131213568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
131313568Sbrandon.potter@amd.com
131413568Sbrandon.potter@amd.com    int status = connect(sim_fd,
131513568Sbrandon.potter@amd.com                         (struct sockaddr *)addr.bufferPtr(),
131613568Sbrandon.potter@amd.com                         (socklen_t)addrlen);
131713568Sbrandon.potter@amd.com
131813568Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
131913568Sbrandon.potter@amd.com}
132013569Sbrandon.potter@amd.com
132113569Sbrandon.potter@amd.comSyscallReturn
132213995Sbrandon.potter@amd.comrecvfromFunc(SyscallDesc *desc, int num, ThreadContext *tc)
132313569Sbrandon.potter@amd.com{
132413569Sbrandon.potter@amd.com    int index = 0;
132513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
132613569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
132713569Sbrandon.potter@amd.com    Addr bufrPtr = p->getSyscallArg(tc, index);
132813569Sbrandon.potter@amd.com    size_t bufrLen = p->getSyscallArg(tc, index);
132913569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
133013569Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
133113569Sbrandon.potter@amd.com    Addr addrlenPtr = p->getSyscallArg(tc, index);
133213569Sbrandon.potter@amd.com
133313569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
133413569Sbrandon.potter@amd.com    if (!sfdp)
133513569Sbrandon.potter@amd.com        return -EBADF;
133613569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
133713569Sbrandon.potter@amd.com
133813569Sbrandon.potter@amd.com    // Reserve buffer space.
133913569Sbrandon.potter@amd.com    BufferArg bufrBuf(bufrPtr, bufrLen);
134013569Sbrandon.potter@amd.com
134113569Sbrandon.potter@amd.com    // Get address length.
134213569Sbrandon.potter@amd.com    socklen_t addrLen = 0;
134313569Sbrandon.potter@amd.com    if (addrlenPtr != 0) {
134413569Sbrandon.potter@amd.com        // Read address length parameter.
134513569Sbrandon.potter@amd.com        BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t));
134613569Sbrandon.potter@amd.com        addrlenBuf.copyIn(tc->getMemProxy());
134713569Sbrandon.potter@amd.com        addrLen = *((socklen_t *)addrlenBuf.bufferPtr());
134813569Sbrandon.potter@amd.com    }
134913569Sbrandon.potter@amd.com
135013569Sbrandon.potter@amd.com    struct sockaddr sa, *sap = NULL;
135113569Sbrandon.potter@amd.com    if (addrLen != 0) {
135213569Sbrandon.potter@amd.com        BufferArg addrBuf(addrPtr, addrLen);
135313569Sbrandon.potter@amd.com        addrBuf.copyIn(tc->getMemProxy());
135413569Sbrandon.potter@amd.com        memcpy(&sa, (struct sockaddr *)addrBuf.bufferPtr(),
135513569Sbrandon.potter@amd.com               sizeof(struct sockaddr));
135613569Sbrandon.potter@amd.com        sap = &sa;
135713569Sbrandon.potter@amd.com    }
135813569Sbrandon.potter@amd.com
135913569Sbrandon.potter@amd.com    ssize_t recvd_size = recvfrom(sim_fd,
136013569Sbrandon.potter@amd.com                                  (void *)bufrBuf.bufferPtr(),
136113569Sbrandon.potter@amd.com                                  bufrLen, flags, sap, (socklen_t *)&addrLen);
136213569Sbrandon.potter@amd.com
136313569Sbrandon.potter@amd.com    if (recvd_size == -1)
136413569Sbrandon.potter@amd.com        return -errno;
136513569Sbrandon.potter@amd.com
136613569Sbrandon.potter@amd.com    // Pass the received data out.
136713569Sbrandon.potter@amd.com    bufrBuf.copyOut(tc->getMemProxy());
136813569Sbrandon.potter@amd.com
136913569Sbrandon.potter@amd.com    // Copy address to addrPtr and pass it on.
137013569Sbrandon.potter@amd.com    if (sap != NULL) {
137113569Sbrandon.potter@amd.com        BufferArg addrBuf(addrPtr, addrLen);
137213569Sbrandon.potter@amd.com        memcpy(addrBuf.bufferPtr(), sap, sizeof(sa));
137313569Sbrandon.potter@amd.com        addrBuf.copyOut(tc->getMemProxy());
137413569Sbrandon.potter@amd.com    }
137513569Sbrandon.potter@amd.com
137613569Sbrandon.potter@amd.com    // Copy len to addrlenPtr and pass it on.
137713569Sbrandon.potter@amd.com    if (addrLen != 0) {
137813569Sbrandon.potter@amd.com        BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t));
137913569Sbrandon.potter@amd.com        *(socklen_t *)addrlenBuf.bufferPtr() = addrLen;
138013569Sbrandon.potter@amd.com        addrlenBuf.copyOut(tc->getMemProxy());
138113569Sbrandon.potter@amd.com    }
138213569Sbrandon.potter@amd.com
138313569Sbrandon.potter@amd.com    return recvd_size;
138413569Sbrandon.potter@amd.com}
138513569Sbrandon.potter@amd.com
138613569Sbrandon.potter@amd.comSyscallReturn
138713995Sbrandon.potter@amd.comsendtoFunc(SyscallDesc *desc, int num, ThreadContext *tc)
138813569Sbrandon.potter@amd.com{
138913569Sbrandon.potter@amd.com    int index = 0;
139013995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
139113569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
139213569Sbrandon.potter@amd.com    Addr bufrPtr = p->getSyscallArg(tc, index);
139313569Sbrandon.potter@amd.com    size_t bufrLen = p->getSyscallArg(tc, index);
139413569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
139513569Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
139613569Sbrandon.potter@amd.com    socklen_t addrLen = p->getSyscallArg(tc, index);
139713569Sbrandon.potter@amd.com
139813569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
139913569Sbrandon.potter@amd.com    if (!sfdp)
140013569Sbrandon.potter@amd.com        return -EBADF;
140113569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
140213569Sbrandon.potter@amd.com
140313569Sbrandon.potter@amd.com    // Reserve buffer space.
140413569Sbrandon.potter@amd.com    BufferArg bufrBuf(bufrPtr, bufrLen);
140513569Sbrandon.potter@amd.com    bufrBuf.copyIn(tc->getMemProxy());
140613569Sbrandon.potter@amd.com
140713569Sbrandon.potter@amd.com    struct sockaddr sa, *sap = nullptr;
140813569Sbrandon.potter@amd.com    memset(&sa, 0, sizeof(sockaddr));
140913569Sbrandon.potter@amd.com    if (addrLen != 0) {
141013569Sbrandon.potter@amd.com        BufferArg addrBuf(addrPtr, addrLen);
141113569Sbrandon.potter@amd.com        addrBuf.copyIn(tc->getMemProxy());
141213569Sbrandon.potter@amd.com        memcpy(&sa, (sockaddr*)addrBuf.bufferPtr(), addrLen);
141313569Sbrandon.potter@amd.com        sap = &sa;
141413569Sbrandon.potter@amd.com    }
141513569Sbrandon.potter@amd.com
141613569Sbrandon.potter@amd.com    ssize_t sent_size = sendto(sim_fd,
141713569Sbrandon.potter@amd.com                               (void *)bufrBuf.bufferPtr(),
141813569Sbrandon.potter@amd.com                               bufrLen, flags, sap, (socklen_t)addrLen);
141913569Sbrandon.potter@amd.com
142013569Sbrandon.potter@amd.com    return (sent_size == -1) ? -errno : sent_size;
142113569Sbrandon.potter@amd.com}
142213569Sbrandon.potter@amd.com
142313569Sbrandon.potter@amd.comSyscallReturn
142413995Sbrandon.potter@amd.comrecvmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc)
142513569Sbrandon.potter@amd.com{
142613569Sbrandon.potter@amd.com    int index = 0;
142713995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
142813569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
142913569Sbrandon.potter@amd.com    Addr msgPtr = p->getSyscallArg(tc, index);
143013569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
143113569Sbrandon.potter@amd.com
143213569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
143313569Sbrandon.potter@amd.com    if (!sfdp)
143413569Sbrandon.potter@amd.com        return -EBADF;
143513569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
143613569Sbrandon.potter@amd.com
143713569Sbrandon.potter@amd.com     /**
143813569Sbrandon.potter@amd.com      *  struct msghdr {
143913569Sbrandon.potter@amd.com      *     void         *msg_name;       // optional address
144013569Sbrandon.potter@amd.com      *    socklen_t     msg_namelen;    // size of address
144113569Sbrandon.potter@amd.com      *    struct iovec *msg_iov;        // iovec array
144213569Sbrandon.potter@amd.com      *    size_t        msg_iovlen;     // number entries in msg_iov
144313569Sbrandon.potter@amd.com      *    i                             // entries correspond to buffer
144413569Sbrandon.potter@amd.com      *    void         *msg_control;    // ancillary data
144513569Sbrandon.potter@amd.com      *    size_t        msg_controllen; // ancillary data buffer len
144613569Sbrandon.potter@amd.com      *    int           msg_flags;      // flags on received message
144713569Sbrandon.potter@amd.com      *  };
144813569Sbrandon.potter@amd.com      *
144913569Sbrandon.potter@amd.com      *  struct iovec {
145013569Sbrandon.potter@amd.com      *    void  *iov_base;              // starting address
145113569Sbrandon.potter@amd.com      *    size_t iov_len;               // number of bytes to transfer
145213569Sbrandon.potter@amd.com      *  };
145313569Sbrandon.potter@amd.com      */
145413569Sbrandon.potter@amd.com
145513569Sbrandon.potter@amd.com    /**
145613569Sbrandon.potter@amd.com     * The plan with this system call is to replace all of the pointers in the
145713569Sbrandon.potter@amd.com     * structure and the substructure with BufferArg class pointers. We will
145813569Sbrandon.potter@amd.com     * copy every field from the structures into our BufferArg classes.
145913569Sbrandon.potter@amd.com     */
146013569Sbrandon.potter@amd.com    BufferArg msgBuf(msgPtr, sizeof(struct msghdr));
146113569Sbrandon.potter@amd.com    msgBuf.copyIn(tc->getMemProxy());
146213569Sbrandon.potter@amd.com    struct msghdr *msgHdr = (struct msghdr *)msgBuf.bufferPtr();
146313569Sbrandon.potter@amd.com
146413569Sbrandon.potter@amd.com    /**
146513569Sbrandon.potter@amd.com     * We will use these address place holders to retain the pointers which
146613569Sbrandon.potter@amd.com     * we are going to replace with our own buffers in our simulator address
146713569Sbrandon.potter@amd.com     * space.
146813569Sbrandon.potter@amd.com     */
146913569Sbrandon.potter@amd.com    Addr msg_name_phold = 0;
147013569Sbrandon.potter@amd.com    Addr msg_iov_phold = 0;
147113569Sbrandon.potter@amd.com    Addr iovec_base_phold[msgHdr->msg_iovlen];
147213569Sbrandon.potter@amd.com    Addr msg_control_phold = 0;
147313569Sbrandon.potter@amd.com
147413569Sbrandon.potter@amd.com    /**
147513569Sbrandon.potter@amd.com     * Record msg_name pointer then replace with buffer pointer.
147613569Sbrandon.potter@amd.com     */
147713569Sbrandon.potter@amd.com    BufferArg *nameBuf = NULL;
147813569Sbrandon.potter@amd.com    if (msgHdr->msg_name) {
147913569Sbrandon.potter@amd.com        /*1*/msg_name_phold = (Addr)msgHdr->msg_name;
148013569Sbrandon.potter@amd.com        /*2*/nameBuf = new BufferArg(msg_name_phold, msgHdr->msg_namelen);
148113569Sbrandon.potter@amd.com        /*3*/nameBuf->copyIn(tc->getMemProxy());
148213569Sbrandon.potter@amd.com        /*4*/msgHdr->msg_name = nameBuf->bufferPtr();
148313569Sbrandon.potter@amd.com    }
148413569Sbrandon.potter@amd.com
148513569Sbrandon.potter@amd.com    /**
148613569Sbrandon.potter@amd.com     * Record msg_iov pointer then replace with buffer pointer. Also, setup
148713569Sbrandon.potter@amd.com     * an array of buffer pointers for the iovec structs record and replace
148813569Sbrandon.potter@amd.com     * their pointers with buffer pointers.
148913569Sbrandon.potter@amd.com     */
149013569Sbrandon.potter@amd.com    BufferArg *iovBuf = NULL;
149113569Sbrandon.potter@amd.com    BufferArg *iovecBuf[msgHdr->msg_iovlen];
149213569Sbrandon.potter@amd.com    for (int i = 0; i < msgHdr->msg_iovlen; i++) {
149313569Sbrandon.potter@amd.com        iovec_base_phold[i] = 0;
149413569Sbrandon.potter@amd.com        iovecBuf[i] = NULL;
149513569Sbrandon.potter@amd.com    }
149613569Sbrandon.potter@amd.com
149713569Sbrandon.potter@amd.com    if (msgHdr->msg_iov) {
149813569Sbrandon.potter@amd.com        /*1*/msg_iov_phold = (Addr)msgHdr->msg_iov;
149913569Sbrandon.potter@amd.com        /*2*/iovBuf = new BufferArg(msg_iov_phold, msgHdr->msg_iovlen *
150013569Sbrandon.potter@amd.com                                    sizeof(struct iovec));
150113569Sbrandon.potter@amd.com        /*3*/iovBuf->copyIn(tc->getMemProxy());
150213569Sbrandon.potter@amd.com        for (int i = 0; i < msgHdr->msg_iovlen; i++) {
150313569Sbrandon.potter@amd.com            if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) {
150413569Sbrandon.potter@amd.com                /*1*/iovec_base_phold[i] =
150513569Sbrandon.potter@amd.com                     (Addr)((struct iovec *)iovBuf->bufferPtr())[i].iov_base;
150613569Sbrandon.potter@amd.com                /*2*/iovecBuf[i] = new BufferArg(iovec_base_phold[i],
150713569Sbrandon.potter@amd.com                     ((struct iovec *)iovBuf->bufferPtr())[i].iov_len);
150813569Sbrandon.potter@amd.com                /*3*/iovecBuf[i]->copyIn(tc->getMemProxy());
150913569Sbrandon.potter@amd.com                /*4*/((struct iovec *)iovBuf->bufferPtr())[i].iov_base =
151013569Sbrandon.potter@amd.com                     iovecBuf[i]->bufferPtr();
151113569Sbrandon.potter@amd.com            }
151213569Sbrandon.potter@amd.com        }
151313569Sbrandon.potter@amd.com        /*4*/msgHdr->msg_iov = (struct iovec *)iovBuf->bufferPtr();
151413569Sbrandon.potter@amd.com    }
151513569Sbrandon.potter@amd.com
151613569Sbrandon.potter@amd.com    /**
151713569Sbrandon.potter@amd.com     * Record msg_control pointer then replace with buffer pointer.
151813569Sbrandon.potter@amd.com     */
151913569Sbrandon.potter@amd.com    BufferArg *controlBuf = NULL;
152013569Sbrandon.potter@amd.com    if (msgHdr->msg_control) {
152113569Sbrandon.potter@amd.com        /*1*/msg_control_phold = (Addr)msgHdr->msg_control;
152213569Sbrandon.potter@amd.com        /*2*/controlBuf = new BufferArg(msg_control_phold,
152313569Sbrandon.potter@amd.com                                        CMSG_ALIGN(msgHdr->msg_controllen));
152413569Sbrandon.potter@amd.com        /*3*/controlBuf->copyIn(tc->getMemProxy());
152513569Sbrandon.potter@amd.com        /*4*/msgHdr->msg_control = controlBuf->bufferPtr();
152613569Sbrandon.potter@amd.com    }
152713569Sbrandon.potter@amd.com
152813569Sbrandon.potter@amd.com    ssize_t recvd_size = recvmsg(sim_fd, msgHdr, flags);
152913569Sbrandon.potter@amd.com
153013569Sbrandon.potter@amd.com    if (recvd_size < 0)
153113569Sbrandon.potter@amd.com        return -errno;
153213569Sbrandon.potter@amd.com
153313569Sbrandon.potter@amd.com    if (msgHdr->msg_name) {
153413569Sbrandon.potter@amd.com        nameBuf->copyOut(tc->getMemProxy());
153513569Sbrandon.potter@amd.com        delete(nameBuf);
153613569Sbrandon.potter@amd.com        msgHdr->msg_name = (void *)msg_name_phold;
153713569Sbrandon.potter@amd.com    }
153813569Sbrandon.potter@amd.com
153913569Sbrandon.potter@amd.com    if (msgHdr->msg_iov) {
154013569Sbrandon.potter@amd.com        for (int i = 0; i< msgHdr->msg_iovlen; i++) {
154113569Sbrandon.potter@amd.com            if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) {
154213569Sbrandon.potter@amd.com                iovecBuf[i]->copyOut(tc->getMemProxy());
154313569Sbrandon.potter@amd.com                delete iovecBuf[i];
154413569Sbrandon.potter@amd.com                ((struct iovec *)iovBuf->bufferPtr())[i].iov_base =
154513569Sbrandon.potter@amd.com                (void *)iovec_base_phold[i];
154613569Sbrandon.potter@amd.com            }
154713569Sbrandon.potter@amd.com        }
154813569Sbrandon.potter@amd.com        iovBuf->copyOut(tc->getMemProxy());
154913569Sbrandon.potter@amd.com        delete iovBuf;
155013569Sbrandon.potter@amd.com        msgHdr->msg_iov = (struct iovec *)msg_iov_phold;
155113569Sbrandon.potter@amd.com    }
155213569Sbrandon.potter@amd.com
155313569Sbrandon.potter@amd.com    if (msgHdr->msg_control) {
155413569Sbrandon.potter@amd.com        controlBuf->copyOut(tc->getMemProxy());
155513569Sbrandon.potter@amd.com        delete(controlBuf);
155613569Sbrandon.potter@amd.com        msgHdr->msg_control = (void *)msg_control_phold;
155713569Sbrandon.potter@amd.com    }
155813569Sbrandon.potter@amd.com
155913569Sbrandon.potter@amd.com    msgBuf.copyOut(tc->getMemProxy());
156013569Sbrandon.potter@amd.com
156113569Sbrandon.potter@amd.com    return recvd_size;
156213569Sbrandon.potter@amd.com}
156313569Sbrandon.potter@amd.com
156413569Sbrandon.potter@amd.comSyscallReturn
156513995Sbrandon.potter@amd.comsendmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc)
156613569Sbrandon.potter@amd.com{
156713569Sbrandon.potter@amd.com    int index = 0;
156813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
156913569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
157013569Sbrandon.potter@amd.com    Addr msgPtr = p->getSyscallArg(tc, index);
157113569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
157213569Sbrandon.potter@amd.com
157313569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
157413569Sbrandon.potter@amd.com    if (!sfdp)
157513569Sbrandon.potter@amd.com        return -EBADF;
157613569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
157713569Sbrandon.potter@amd.com
157813569Sbrandon.potter@amd.com    /**
157913569Sbrandon.potter@amd.com     * Reserve buffer space.
158013569Sbrandon.potter@amd.com     */
158113569Sbrandon.potter@amd.com    BufferArg msgBuf(msgPtr, sizeof(struct msghdr));
158213569Sbrandon.potter@amd.com    msgBuf.copyIn(tc->getMemProxy());
158313569Sbrandon.potter@amd.com    struct msghdr msgHdr = *((struct msghdr *)msgBuf.bufferPtr());
158413569Sbrandon.potter@amd.com
158513569Sbrandon.potter@amd.com    /**
158613569Sbrandon.potter@amd.com     * Assuming msgHdr.msg_iovlen >= 1, then there is no point calling
158713569Sbrandon.potter@amd.com     * recvmsg without a buffer.
158813569Sbrandon.potter@amd.com     */
158913569Sbrandon.potter@amd.com    struct iovec *iovPtr = msgHdr.msg_iov;
159013569Sbrandon.potter@amd.com    BufferArg iovBuf((Addr)iovPtr, sizeof(struct iovec) * msgHdr.msg_iovlen);
159113569Sbrandon.potter@amd.com    iovBuf.copyIn(tc->getMemProxy());
159213569Sbrandon.potter@amd.com    struct iovec *iov = (struct iovec *)iovBuf.bufferPtr();
159313569Sbrandon.potter@amd.com    msgHdr.msg_iov = iov;
159413569Sbrandon.potter@amd.com
159513569Sbrandon.potter@amd.com    /**
159613569Sbrandon.potter@amd.com     * Cannot instantiate buffers till inside the loop.
159713569Sbrandon.potter@amd.com     * Create array to hold buffer addresses, to be used during copyIn of
159813569Sbrandon.potter@amd.com     * send data.
159913569Sbrandon.potter@amd.com     */
160013569Sbrandon.potter@amd.com    BufferArg **bufferArray = (BufferArg **)malloc(msgHdr.msg_iovlen
160113569Sbrandon.potter@amd.com                                                   * sizeof(BufferArg *));
160213569Sbrandon.potter@amd.com
160313569Sbrandon.potter@amd.com    /**
160413569Sbrandon.potter@amd.com     * Iterate through the iovec structures:
160513569Sbrandon.potter@amd.com     * Get the base buffer addreses, reserve iov_len amount of space for each.
160613569Sbrandon.potter@amd.com     * Put the buf address into the bufferArray for later retrieval.
160713569Sbrandon.potter@amd.com     */
160813569Sbrandon.potter@amd.com    for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) {
160913569Sbrandon.potter@amd.com        Addr basePtr = (Addr) iov[iovIndex].iov_base;
161013569Sbrandon.potter@amd.com        bufferArray[iovIndex] = new BufferArg(basePtr, iov[iovIndex].iov_len);
161113569Sbrandon.potter@amd.com        bufferArray[iovIndex]->copyIn(tc->getMemProxy());
161213569Sbrandon.potter@amd.com        iov[iovIndex].iov_base = bufferArray[iovIndex]->bufferPtr();
161313569Sbrandon.potter@amd.com    }
161413569Sbrandon.potter@amd.com
161513569Sbrandon.potter@amd.com    ssize_t sent_size = sendmsg(sim_fd, &msgHdr, flags);
161613569Sbrandon.potter@amd.com    int local_errno = errno;
161713569Sbrandon.potter@amd.com
161813569Sbrandon.potter@amd.com    /**
161913569Sbrandon.potter@amd.com     * Free dynamically allocated memory.
162013569Sbrandon.potter@amd.com     */
162113569Sbrandon.potter@amd.com    for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) {
162213569Sbrandon.potter@amd.com        BufferArg *baseBuf = ( BufferArg *)bufferArray[iovIndex];
162313569Sbrandon.potter@amd.com        delete(baseBuf);
162413569Sbrandon.potter@amd.com    }
162513569Sbrandon.potter@amd.com
162613569Sbrandon.potter@amd.com    /**
162713569Sbrandon.potter@amd.com     * Malloced above.
162813569Sbrandon.potter@amd.com     */
162913569Sbrandon.potter@amd.com    free(bufferArray);
163013569Sbrandon.potter@amd.com
163113569Sbrandon.potter@amd.com    return (sent_size < 0) ? -local_errno : sent_size;
163213569Sbrandon.potter@amd.com}
163313569Sbrandon.potter@amd.com
163413571Sbrandon.potter@amd.comSyscallReturn
163513995Sbrandon.potter@amd.comgetsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc)
163613571Sbrandon.potter@amd.com{
163713571Sbrandon.potter@amd.com    // union of all possible return value types from getsockopt
163813571Sbrandon.potter@amd.com    union val {
163913571Sbrandon.potter@amd.com        int i_val;
164013571Sbrandon.potter@amd.com        long l_val;
164113571Sbrandon.potter@amd.com        struct linger linger_val;
164213571Sbrandon.potter@amd.com        struct timeval timeval_val;
164313571Sbrandon.potter@amd.com    } val;
164413571Sbrandon.potter@amd.com
164513571Sbrandon.potter@amd.com    int index = 0;
164613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
164713571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
164813571Sbrandon.potter@amd.com    int level = p->getSyscallArg(tc, index);
164913571Sbrandon.potter@amd.com    int optname = p->getSyscallArg(tc, index);
165013571Sbrandon.potter@amd.com    Addr valPtr = p->getSyscallArg(tc, index);
165113571Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
165213571Sbrandon.potter@amd.com
165313571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
165413571Sbrandon.potter@amd.com    if (!sfdp)
165513571Sbrandon.potter@amd.com        return -EBADF;
165613571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
165713571Sbrandon.potter@amd.com
165813571Sbrandon.potter@amd.com    socklen_t len = sizeof(val);
165913571Sbrandon.potter@amd.com    int status = getsockopt(sim_fd, level, optname, &val, &len);
166013571Sbrandon.potter@amd.com
166113571Sbrandon.potter@amd.com    if (status == -1)
166213571Sbrandon.potter@amd.com        return -errno;
166313571Sbrandon.potter@amd.com
166413571Sbrandon.potter@amd.com    // copy val to valPtr and pass it on
166513571Sbrandon.potter@amd.com    BufferArg valBuf(valPtr, sizeof(val));
166613571Sbrandon.potter@amd.com    memcpy(valBuf.bufferPtr(), &val, sizeof(val));
166713571Sbrandon.potter@amd.com    valBuf.copyOut(tc->getMemProxy());
166813571Sbrandon.potter@amd.com
166913571Sbrandon.potter@amd.com    // copy len to lenPtr and pass  it on
167013571Sbrandon.potter@amd.com    BufferArg lenBuf(lenPtr, sizeof(len));
167113571Sbrandon.potter@amd.com    memcpy(lenBuf.bufferPtr(), &len, sizeof(len));
167213571Sbrandon.potter@amd.com    lenBuf.copyOut(tc->getMemProxy());
167313571Sbrandon.potter@amd.com
167413571Sbrandon.potter@amd.com    return status;
167513571Sbrandon.potter@amd.com}
167613571Sbrandon.potter@amd.com
167713571Sbrandon.potter@amd.comSyscallReturn
167813995Sbrandon.potter@amd.comgetsocknameFunc(SyscallDesc *desc, int num, ThreadContext *tc)
167913571Sbrandon.potter@amd.com{
168013571Sbrandon.potter@amd.com    int index = 0;
168113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
168213571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
168313571Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
168413571Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
168513571Sbrandon.potter@amd.com
168613571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
168713571Sbrandon.potter@amd.com    if (!sfdp)
168813571Sbrandon.potter@amd.com        return -EBADF;
168913571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
169013571Sbrandon.potter@amd.com
169113571Sbrandon.potter@amd.com    // lenPtr is an in-out paramenter:
169213571Sbrandon.potter@amd.com    // sending the address length in, conveying the final length out
169313571Sbrandon.potter@amd.com
169413571Sbrandon.potter@amd.com    // Read in the value of len from the passed pointer.
169513571Sbrandon.potter@amd.com    BufferArg lenBuf(lenPtr, sizeof(socklen_t));
169613571Sbrandon.potter@amd.com    lenBuf.copyIn(tc->getMemProxy());
169713571Sbrandon.potter@amd.com    socklen_t len = *(socklen_t *)lenBuf.bufferPtr();
169813571Sbrandon.potter@amd.com
169913571Sbrandon.potter@amd.com    struct sockaddr sa;
170013571Sbrandon.potter@amd.com    int status = getsockname(sim_fd, &sa, &len);
170113571Sbrandon.potter@amd.com
170213571Sbrandon.potter@amd.com    if (status == -1)
170313571Sbrandon.potter@amd.com        return -errno;
170413571Sbrandon.potter@amd.com
170513571Sbrandon.potter@amd.com    // Copy address to addrPtr and pass it on.
170613571Sbrandon.potter@amd.com    BufferArg addrBuf(addrPtr, sizeof(sa));
170713571Sbrandon.potter@amd.com    memcpy(addrBuf.bufferPtr(), &sa, sizeof(sa));
170813571Sbrandon.potter@amd.com    addrBuf.copyOut(tc->getMemProxy());
170913571Sbrandon.potter@amd.com
171013571Sbrandon.potter@amd.com    // Copy len to lenPtr and pass  it on.
171113571Sbrandon.potter@amd.com    *(socklen_t *)lenBuf.bufferPtr() = len;
171213571Sbrandon.potter@amd.com    lenBuf.copyOut(tc->getMemProxy());
171313571Sbrandon.potter@amd.com
171413571Sbrandon.potter@amd.com    return status;
171513571Sbrandon.potter@amd.com}
171613571Sbrandon.potter@amd.com
171713571Sbrandon.potter@amd.comSyscallReturn
171813995Sbrandon.potter@amd.comgetpeernameFunc(SyscallDesc *desc, int num, ThreadContext *tc)
171913571Sbrandon.potter@amd.com{
172013571Sbrandon.potter@amd.com    int index = 0;
172113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
172213571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
172313571Sbrandon.potter@amd.com    Addr sockAddrPtr = p->getSyscallArg(tc, index);
172413571Sbrandon.potter@amd.com    Addr addrlenPtr = p->getSyscallArg(tc, index);
172513571Sbrandon.potter@amd.com
172613571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
172713571Sbrandon.potter@amd.com    if (!sfdp)
172813571Sbrandon.potter@amd.com        return -EBADF;
172913571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
173013571Sbrandon.potter@amd.com
173113571Sbrandon.potter@amd.com    BufferArg bufAddrlen(addrlenPtr, sizeof(unsigned));
173213571Sbrandon.potter@amd.com    bufAddrlen.copyIn(tc->getMemProxy());
173313571Sbrandon.potter@amd.com    BufferArg bufSock(sockAddrPtr, *(unsigned *)bufAddrlen.bufferPtr());
173413571Sbrandon.potter@amd.com
173513571Sbrandon.potter@amd.com    int retval = getpeername(sim_fd,
173613571Sbrandon.potter@amd.com                             (struct sockaddr *)bufSock.bufferPtr(),
173713571Sbrandon.potter@amd.com                             (unsigned *)bufAddrlen.bufferPtr());
173813571Sbrandon.potter@amd.com
173913571Sbrandon.potter@amd.com    if (retval != -1) {
174013571Sbrandon.potter@amd.com        bufSock.copyOut(tc->getMemProxy());
174113571Sbrandon.potter@amd.com        bufAddrlen.copyOut(tc->getMemProxy());
174213571Sbrandon.potter@amd.com    }
174313571Sbrandon.potter@amd.com
174413571Sbrandon.potter@amd.com    return (retval == -1) ? -errno : retval;
174513571Sbrandon.potter@amd.com}
174613571Sbrandon.potter@amd.com
174713571Sbrandon.potter@amd.comSyscallReturn
174813995Sbrandon.potter@amd.comsetsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc)
174913571Sbrandon.potter@amd.com{
175013571Sbrandon.potter@amd.com    int index = 0;
175113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
175213571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
175313571Sbrandon.potter@amd.com    int level = p->getSyscallArg(tc, index);
175413571Sbrandon.potter@amd.com    int optname = p->getSyscallArg(tc, index);
175513571Sbrandon.potter@amd.com    Addr valPtr = p->getSyscallArg(tc, index);
175613571Sbrandon.potter@amd.com    socklen_t len = p->getSyscallArg(tc, index);
175713571Sbrandon.potter@amd.com
175813571Sbrandon.potter@amd.com    BufferArg valBuf(valPtr, len);
175913571Sbrandon.potter@amd.com    valBuf.copyIn(tc->getMemProxy());
176013571Sbrandon.potter@amd.com
176113571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
176213571Sbrandon.potter@amd.com    if (!sfdp)
176313571Sbrandon.potter@amd.com        return -EBADF;
176413571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
176513571Sbrandon.potter@amd.com
176613571Sbrandon.potter@amd.com    int status = setsockopt(sim_fd, level, optname,
176713571Sbrandon.potter@amd.com                            (struct sockaddr *)valBuf.bufferPtr(), len);
176813571Sbrandon.potter@amd.com
176913571Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
177013571Sbrandon.potter@amd.com}
177113571Sbrandon.potter@amd.com
1772