syscall_emul.cc revision 13933
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
6711851Sbrandon.potter@amd.comunimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
682680Sktlim@umich.edu                  ThreadContext *tc)
69360SN/A{
7011794Sbrandon.potter@amd.com    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
712484SN/A
722484SN/A    return 1;
73360SN/A}
74360SN/A
75360SN/A
761450SN/ASyscallReturn
7711851Sbrandon.potter@amd.comignoreFunc(SyscallDesc *desc, int callnum, Process *process,
782680Sktlim@umich.edu           ThreadContext *tc)
79360SN/A{
8011794Sbrandon.potter@amd.com    if (desc->needWarning()) {
8111794Sbrandon.potter@amd.com        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
8211794Sbrandon.potter@amd.com             "\n      (further warnings will be suppressed)" : "");
8310831Ssteve.reinhardt@amd.com    }
84360SN/A
858149SChris.Emmons@ARM.com    return 0;
868149SChris.Emmons@ARM.com}
878149SChris.Emmons@ARM.com
8811886Sbrandon.potter@amd.comstatic void
8911911SBrandon.Potter@amd.comexitFutexWake(ThreadContext *tc, Addr addr, uint64_t tgid)
9011886Sbrandon.potter@amd.com{
9111911SBrandon.Potter@amd.com    // Clear value at address pointed to by thread's childClearTID field.
9211911SBrandon.Potter@amd.com    BufferArg ctidBuf(addr, sizeof(long));
9311911SBrandon.Potter@amd.com    long *ctid = (long *)ctidBuf.bufferPtr();
9411911SBrandon.Potter@amd.com    *ctid = 0;
9511911SBrandon.Potter@amd.com    ctidBuf.copyOut(tc->getMemProxy());
9611886Sbrandon.potter@amd.com
9711911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
9811911SBrandon.Potter@amd.com    // Wake one of the waiting threads.
9911911SBrandon.Potter@amd.com    futex_map.wakeup(addr, tgid, 1);
10011911SBrandon.Potter@amd.com}
10111911SBrandon.Potter@amd.com
10211911SBrandon.Potter@amd.comstatic SyscallReturn
10311911SBrandon.Potter@amd.comexitImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
10411911SBrandon.Potter@amd.com         bool group)
10511911SBrandon.Potter@amd.com{
10611911SBrandon.Potter@amd.com    int index = 0;
10711911SBrandon.Potter@amd.com    int status = p->getSyscallArg(tc, index);
10811911SBrandon.Potter@amd.com
10911911SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
11011911SBrandon.Potter@amd.com
11111911SBrandon.Potter@amd.com    if (group)
11211911SBrandon.Potter@amd.com        *p->exitGroup = true;
11311911SBrandon.Potter@amd.com
11411911SBrandon.Potter@amd.com    if (p->childClearTID)
11511911SBrandon.Potter@amd.com        exitFutexWake(tc, p->childClearTID, p->tgid());
11611911SBrandon.Potter@amd.com
11711911SBrandon.Potter@amd.com    bool last_thread = true;
11811911SBrandon.Potter@amd.com    Process *parent = nullptr, *tg_lead = nullptr;
11911911SBrandon.Potter@amd.com    for (int i = 0; last_thread && i < sys->numContexts(); i++) {
12011911SBrandon.Potter@amd.com        Process *walk;
12111911SBrandon.Potter@amd.com        if (!(walk = sys->threadContexts[i]->getProcessPtr()))
12211911SBrandon.Potter@amd.com            continue;
12311911SBrandon.Potter@amd.com
12411911SBrandon.Potter@amd.com        /**
12511911SBrandon.Potter@amd.com         * Threads in a thread group require special handing. For instance,
12611911SBrandon.Potter@amd.com         * we send the SIGCHLD signal so that it appears that it came from
12711911SBrandon.Potter@amd.com         * the head of the group. We also only delete file descriptors if
12811911SBrandon.Potter@amd.com         * we are the last thread in the thread group.
12911911SBrandon.Potter@amd.com         */
13011911SBrandon.Potter@amd.com        if (walk->pid() == p->tgid())
13111911SBrandon.Potter@amd.com            tg_lead = walk;
13211911SBrandon.Potter@amd.com
13313644Sqtt2@cornell.edu        if ((sys->threadContexts[i]->status() != ThreadContext::Halted) &&
13413644Sqtt2@cornell.edu            (sys->threadContexts[i]->status() != ThreadContext::Halting) &&
13513644Sqtt2@cornell.edu            (walk != p)) {
13611911SBrandon.Potter@amd.com            /**
13711911SBrandon.Potter@amd.com             * Check if we share thread group with the pointer; this denotes
13811911SBrandon.Potter@amd.com             * that we are not the last thread active in the thread group.
13911911SBrandon.Potter@amd.com             * Note that setting this to false also prevents further
14011911SBrandon.Potter@amd.com             * iterations of the loop.
14111911SBrandon.Potter@amd.com             */
14213644Sqtt2@cornell.edu            if (walk->tgid() == p->tgid()) {
14313644Sqtt2@cornell.edu                /**
14413644Sqtt2@cornell.edu                 * If p is trying to exit_group and both walk and p are in
14513644Sqtt2@cornell.edu                 * the same thread group (i.e., sharing the same tgid),
14613644Sqtt2@cornell.edu                 * we need to halt walk's thread context. After all threads
14713644Sqtt2@cornell.edu                 * except p are halted, p becomes the last thread in the
14813644Sqtt2@cornell.edu                 * group.
14913644Sqtt2@cornell.edu                 *
15013644Sqtt2@cornell.edu                 * If p is not doing exit_group and there exists another
15113644Sqtt2@cornell.edu                 * active thread context in the group, last_thread is
15213644Sqtt2@cornell.edu                 * set to false to prevent the parent thread from killing
15313644Sqtt2@cornell.edu                 * all threads in the group.
15413644Sqtt2@cornell.edu                 */
15513644Sqtt2@cornell.edu                if (*(p->exitGroup)) {
15613644Sqtt2@cornell.edu                    sys->threadContexts[i]->halt();
15713644Sqtt2@cornell.edu                } else {
15813644Sqtt2@cornell.edu                    last_thread = false;
15913644Sqtt2@cornell.edu                }
16013644Sqtt2@cornell.edu            }
16111911SBrandon.Potter@amd.com
16211911SBrandon.Potter@amd.com            /**
16311911SBrandon.Potter@amd.com             * A corner case exists which involves execve(). After execve(),
16411911SBrandon.Potter@amd.com             * the execve will enable SIGCHLD in the process. The problem
16511911SBrandon.Potter@amd.com             * occurs when the exiting process is the root process in the
16611911SBrandon.Potter@amd.com             * system; there is no parent to receive the signal. We obviate
16711911SBrandon.Potter@amd.com             * this problem by setting the root process' ppid to zero in the
16811911SBrandon.Potter@amd.com             * Python configuration files. We really should handle the
16911911SBrandon.Potter@amd.com             * root/execve specific case more gracefully.
17011911SBrandon.Potter@amd.com             */
17111911SBrandon.Potter@amd.com            if (*p->sigchld && (p->ppid() != 0) && (walk->pid() == p->ppid()))
17211911SBrandon.Potter@amd.com                parent = walk;
17311886Sbrandon.potter@amd.com        }
17411886Sbrandon.potter@amd.com    }
17511911SBrandon.Potter@amd.com
17611911SBrandon.Potter@amd.com    if (last_thread) {
17711911SBrandon.Potter@amd.com        if (parent) {
17811911SBrandon.Potter@amd.com            assert(tg_lead);
17911911SBrandon.Potter@amd.com            sys->signalList.push_back(BasicSignal(tg_lead, parent, SIGCHLD));
18011911SBrandon.Potter@amd.com        }
18111911SBrandon.Potter@amd.com
18211911SBrandon.Potter@amd.com        /**
18311911SBrandon.Potter@amd.com         * Run though FD array of the exiting process and close all file
18411911SBrandon.Potter@amd.com         * descriptors except for the standard file descriptors.
18511911SBrandon.Potter@amd.com         * (The standard file descriptors are shared with gem5.)
18611911SBrandon.Potter@amd.com         */
18711911SBrandon.Potter@amd.com        for (int i = 0; i < p->fds->getSize(); i++) {
18811911SBrandon.Potter@amd.com            if ((*p->fds)[i])
18911911SBrandon.Potter@amd.com                p->fds->closeFDEntry(i);
19011911SBrandon.Potter@amd.com        }
19111911SBrandon.Potter@amd.com    }
19211911SBrandon.Potter@amd.com
19311911SBrandon.Potter@amd.com    tc->halt();
19413644Sqtt2@cornell.edu
19513644Sqtt2@cornell.edu    /**
19613644Sqtt2@cornell.edu     * check to see if there is no more active thread in the system. If so,
19713644Sqtt2@cornell.edu     * exit the simulation loop
19813644Sqtt2@cornell.edu     */
19913644Sqtt2@cornell.edu    int activeContexts = 0;
20013644Sqtt2@cornell.edu    for (auto &system: sys->systemList)
20113644Sqtt2@cornell.edu        activeContexts += system->numRunningContexts();
20213644Sqtt2@cornell.edu
20313644Sqtt2@cornell.edu    if (activeContexts == 0) {
20413644Sqtt2@cornell.edu        /**
20513644Sqtt2@cornell.edu         * Even though we are terminating the final thread context, dist-gem5
20613644Sqtt2@cornell.edu         * requires the simulation to remain active and provide
20713644Sqtt2@cornell.edu         * synchronization messages to the switch process. So we just halt
20813644Sqtt2@cornell.edu         * the last thread context and return. The simulation will be
20913644Sqtt2@cornell.edu         * terminated by dist-gem5 in a coordinated manner once all nodes
21013644Sqtt2@cornell.edu         * have signaled their readiness to exit. For non dist-gem5
21113644Sqtt2@cornell.edu         * simulations, readyToExit() always returns true.
21213644Sqtt2@cornell.edu         */
21313644Sqtt2@cornell.edu        if (!DistIface::readyToExit(0)) {
21413644Sqtt2@cornell.edu            return status;
21513644Sqtt2@cornell.edu        }
21613644Sqtt2@cornell.edu
21713644Sqtt2@cornell.edu        exitSimLoop("exiting with last active thread context", status & 0xff);
21813644Sqtt2@cornell.edu        return status;
21913644Sqtt2@cornell.edu    }
22013644Sqtt2@cornell.edu
22111911SBrandon.Potter@amd.com    return status;
22211886Sbrandon.potter@amd.com}
2238149SChris.Emmons@ARM.com
2248149SChris.Emmons@ARM.comSyscallReturn
22511886Sbrandon.potter@amd.comexitFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
226360SN/A{
22711911SBrandon.Potter@amd.com    return exitImpl(desc, callnum, p, tc, false);
228360SN/A}
229360SN/A
2301450SN/ASyscallReturn
23111911SBrandon.Potter@amd.comexitGroupFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
2326109Ssanchezd@stanford.edu{
23311911SBrandon.Potter@amd.com    return exitImpl(desc, callnum, p, tc, true);
2346109Ssanchezd@stanford.edu}
2356109Ssanchezd@stanford.edu
2366109Ssanchezd@stanford.eduSyscallReturn
23711851Sbrandon.potter@amd.comgetpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
238360SN/A{
23910318Sandreas.hansson@arm.com    return (int)PageBytes;
240360SN/A}
241360SN/A
242360SN/A
2431450SN/ASyscallReturn
24411851Sbrandon.potter@amd.combrkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
245360SN/A{
246360SN/A    // change brk addr to first arg
2476701Sgblack@eecs.umich.edu    int index = 0;
2486701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
2495748SSteve.Reinhardt@amd.com
25011905SBrandon.Potter@amd.com    std::shared_ptr<MemState> mem_state = p->memState;
25111905SBrandon.Potter@amd.com    Addr brk_point = mem_state->getBrkPoint();
25211905SBrandon.Potter@amd.com
2535748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
2545748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
2555748SSteve.Reinhardt@amd.com    if (new_brk == 0)
25611905SBrandon.Potter@amd.com        return brk_point;
2575748SSteve.Reinhardt@amd.com
25811905SBrandon.Potter@amd.com    if (new_brk > brk_point) {
2595748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
26011905SBrandon.Potter@amd.com        for (ChunkGenerator gen(brk_point,
26111905SBrandon.Potter@amd.com                                new_brk - brk_point,
26210318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
2635748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
26410318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
2656687Stjones1@inf.ed.ac.uk
2666687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
2676687Stjones1@inf.ed.ac.uk            else {
26811905SBrandon.Potter@amd.com                uint8_t zero = 0;
2698852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
2706687Stjones1@inf.ed.ac.uk
2716687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
27210318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
2736687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
2748852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
27510318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
2766687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
27711906SBrandon.Potter@amd.com                    p->pTable->translate(next_page)) {
27810318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
2798852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
2806687Stjones1@inf.ed.ac.uk                }
2816687Stjones1@inf.ed.ac.uk            }
2822474SN/A        }
2831450SN/A    }
2845748SSteve.Reinhardt@amd.com
28511905SBrandon.Potter@amd.com    mem_state->setBrkPoint(new_brk);
28611380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
28711905SBrandon.Potter@amd.com                    mem_state->getBrkPoint());
28811905SBrandon.Potter@amd.com    return mem_state->getBrkPoint();
289360SN/A}
290360SN/A
29111886Sbrandon.potter@amd.comSyscallReturn
29211886Sbrandon.potter@amd.comsetTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
29311886Sbrandon.potter@amd.com                  ThreadContext *tc)
29411886Sbrandon.potter@amd.com{
29511886Sbrandon.potter@amd.com    int index = 0;
29611886Sbrandon.potter@amd.com    uint64_t tidPtr = process->getSyscallArg(tc, index);
29711886Sbrandon.potter@amd.com
29811886Sbrandon.potter@amd.com    process->childClearTID = tidPtr;
29911886Sbrandon.potter@amd.com    return process->pid();
30011886Sbrandon.potter@amd.com}
301360SN/A
3021450SN/ASyscallReturn
30311851Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
304360SN/A{
3056701Sgblack@eecs.umich.edu    int index = 0;
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
31211851Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
313360SN/A{
3146701Sgblack@eecs.umich.edu    int index = 0;
31510931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
3166701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
3176701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
318360SN/A
31911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
32011856Sbrandon.potter@amd.com    if (!ffdp)
32110931Sbrandon.potter@amd.com        return -EBADF;
32211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
32310931Sbrandon.potter@amd.com
32410931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
325360SN/A
3261458SN/A    return (result == (off_t)-1) ? -errno : result;
327360SN/A}
328360SN/A
329360SN/A
3301450SN/ASyscallReturn
33111851Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3324118Sgblack@eecs.umich.edu{
3336701Sgblack@eecs.umich.edu    int index = 0;
33410931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
3356701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
3366701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
3376701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
3386701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
3394118Sgblack@eecs.umich.edu
34011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
34111856Sbrandon.potter@amd.com    if (!ffdp)
34210931Sbrandon.potter@amd.com        return -EBADF;
34311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
34410931Sbrandon.potter@amd.com
3454118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
3464118Sgblack@eecs.umich.edu
34710931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
3484118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
3494118Sgblack@eecs.umich.edu
35011379Sbrandon.potter@amd.com    if (result == (off_t)-1)
3514118Sgblack@eecs.umich.edu        return -errno;
35211379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
35311379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
35411379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
35511379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
35611379Sbrandon.potter@amd.com    return 0;
3574118Sgblack@eecs.umich.edu}
3584118Sgblack@eecs.umich.edu
3594118Sgblack@eecs.umich.edu
3604118Sgblack@eecs.umich.eduSyscallReturn
36111851Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
362360SN/A{
36311383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
36411383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
36511383Sbrandon.potter@amd.com    // memory.
3661458SN/A    return 0;
367360SN/A}
368360SN/A
369360SN/A
370360SN/Aconst char *hostname = "m5.eecs.umich.edu";
371360SN/A
3721450SN/ASyscallReturn
37311851Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
374360SN/A{
3756701Sgblack@eecs.umich.edu    int index = 0;
37611906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3776701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
37811906SBrandon.Potter@amd.com    BufferArg name(buf_ptr, name_len);
379360SN/A
380360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
381360SN/A
3828706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
383360SN/A
3841458SN/A    return 0;
385360SN/A}
386360SN/A
3871450SN/ASyscallReturn
38811851Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3895513SMichael.Adler@intel.com{
3905513SMichael.Adler@intel.com    int result = 0;
3916731Svince@csl.cornell.edu    int index = 0;
39211906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
3936701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
39411906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, size);
3955513SMichael.Adler@intel.com
3965513SMichael.Adler@intel.com    // Is current working directory defined?
39713883Sdavid.hashe@amd.com    string cwd = p->tgtCwd;
3985513SMichael.Adler@intel.com    if (!cwd.empty()) {
3995513SMichael.Adler@intel.com        if (cwd.length() >= size) {
4005513SMichael.Adler@intel.com            // Buffer too small
4015513SMichael.Adler@intel.com            return -ERANGE;
4025513SMichael.Adler@intel.com        }
4035513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
4045513SMichael.Adler@intel.com        result = cwd.length();
40510955Sdavid.hashe@amd.com    } else {
40611856Sbrandon.potter@amd.com        if (getcwd((char *)buf.bufferPtr(), size)) {
4075513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
40810955Sdavid.hashe@amd.com        } else {
4095513SMichael.Adler@intel.com            result = -1;
4105513SMichael.Adler@intel.com        }
4115513SMichael.Adler@intel.com    }
4125513SMichael.Adler@intel.com
4138706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4145513SMichael.Adler@intel.com
4155513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4165513SMichael.Adler@intel.com}
4175513SMichael.Adler@intel.com
41810203SAli.Saidi@ARM.comSyscallReturn
41911851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
42011851Sbrandon.potter@amd.com             ThreadContext *tc)
42110203SAli.Saidi@ARM.com{
42210203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
42310203SAli.Saidi@ARM.com}
4245513SMichael.Adler@intel.com
4255513SMichael.Adler@intel.comSyscallReturn
42611851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
42711851Sbrandon.potter@amd.com             int index)
4285513SMichael.Adler@intel.com{
4295513SMichael.Adler@intel.com    string path;
4305513SMichael.Adler@intel.com
4318852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
43210223Ssteve.reinhardt@amd.com        return -EFAULT;
4335513SMichael.Adler@intel.com
43413883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
43513883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
4365513SMichael.Adler@intel.com
43711906SBrandon.Potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
4386701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
4396701Sgblack@eecs.umich.edu
44011906SBrandon.Potter@amd.com    BufferArg buf(buf_ptr, bufsiz);
4415513SMichael.Adler@intel.com
44210955Sdavid.hashe@amd.com    int result = -1;
44310955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
44410955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
44510955Sdavid.hashe@amd.com    } else {
44611140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
44711140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
44811851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
44911140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
45011140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
45111140Sjthestness@gmail.com        //     called binary calls readlink().
45211140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
45311140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
45411140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
45511140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
45611140Sjthestness@gmail.com
45711140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
45811140Sjthestness@gmail.com        char real_path[PATH_MAX];
45911140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
46011140Sjthestness@gmail.com        if (!check_real_path) {
46111140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
46211140Sjthestness@gmail.com                  "executable: %s", p->progName());
46311140Sjthestness@gmail.com        }
46411140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
46511140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
46611140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
46710955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
46810955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
46910955Sdavid.hashe@amd.com            result = bufsiz;
47010955Sdavid.hashe@amd.com        } else {
47111140Sjthestness@gmail.com            result = real_path_len;
47210955Sdavid.hashe@amd.com        }
47311140Sjthestness@gmail.com
47411140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
47511140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
47611140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
47711140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
47810955Sdavid.hashe@amd.com    }
4795513SMichael.Adler@intel.com
4808706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4815513SMichael.Adler@intel.com
4825513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4835513SMichael.Adler@intel.com}
4845513SMichael.Adler@intel.com
4855513SMichael.Adler@intel.comSyscallReturn
48611851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
487511SN/A{
48810633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
48910633Smichaelupton@gmail.com}
49010633Smichaelupton@gmail.com
49110633Smichaelupton@gmail.comSyscallReturn
49211851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
49311851Sbrandon.potter@amd.com             int index)
49410633Smichaelupton@gmail.com{
4951706SN/A    string path;
496360SN/A
4978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
49810223Ssteve.reinhardt@amd.com        return -EFAULT;
499511SN/A
50013883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
5013669Sbinkertn@umich.edu
502511SN/A    int result = unlink(path.c_str());
5031458SN/A    return (result == -1) ? -errno : result;
504511SN/A}
505511SN/A
50612795Smattdsinclair@gmail.comSyscallReturn
50712795Smattdsinclair@gmail.comlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
50812795Smattdsinclair@gmail.com{
50912795Smattdsinclair@gmail.com    string path;
51012795Smattdsinclair@gmail.com    string new_path;
51112795Smattdsinclair@gmail.com
51212795Smattdsinclair@gmail.com    int index = 0;
51312795Smattdsinclair@gmail.com    auto &virt_mem = tc->getMemProxy();
51412795Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index)))
51512795Smattdsinclair@gmail.com        return -EFAULT;
51612795Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index)))
51712795Smattdsinclair@gmail.com        return -EFAULT;
51812795Smattdsinclair@gmail.com
51913883Sdavid.hashe@amd.com    path = p->absolutePath(path, true);
52013883Sdavid.hashe@amd.com    new_path = p->absolutePath(new_path, true);
52112795Smattdsinclair@gmail.com
52212795Smattdsinclair@gmail.com    int result = link(path.c_str(), new_path.c_str());
52312795Smattdsinclair@gmail.com    return (result == -1) ? -errno : result;
52412795Smattdsinclair@gmail.com}
5255513SMichael.Adler@intel.com
5265513SMichael.Adler@intel.comSyscallReturn
52712796Smattdsinclair@gmail.comsymlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
52812796Smattdsinclair@gmail.com{
52912796Smattdsinclair@gmail.com    string path;
53012796Smattdsinclair@gmail.com    string new_path;
53112796Smattdsinclair@gmail.com
53212796Smattdsinclair@gmail.com    int index = 0;
53312796Smattdsinclair@gmail.com    auto &virt_mem = tc->getMemProxy();
53412796Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index)))
53512796Smattdsinclair@gmail.com        return -EFAULT;
53612796Smattdsinclair@gmail.com    if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index)))
53712796Smattdsinclair@gmail.com        return -EFAULT;
53812796Smattdsinclair@gmail.com
53913883Sdavid.hashe@amd.com    path = p->absolutePath(path, true);
54013883Sdavid.hashe@amd.com    new_path = p->absolutePath(new_path, true);
54112796Smattdsinclair@gmail.com
54212796Smattdsinclair@gmail.com    int result = symlink(path.c_str(), new_path.c_str());
54312796Smattdsinclair@gmail.com    return (result == -1) ? -errno : result;
54412796Smattdsinclair@gmail.com}
54512796Smattdsinclair@gmail.com
54612796Smattdsinclair@gmail.comSyscallReturn
54711851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5485513SMichael.Adler@intel.com{
5496701Sgblack@eecs.umich.edu    int index = 0;
55013883Sdavid.hashe@amd.com    std::string path;
5518852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
55210223Ssteve.reinhardt@amd.com        return -EFAULT;
5535513SMichael.Adler@intel.com
55413883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
5556701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
5565513SMichael.Adler@intel.com
55713883Sdavid.hashe@amd.com    auto result = mkdir(path.c_str(), mode);
5585513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
5595513SMichael.Adler@intel.com}
5605513SMichael.Adler@intel.com
5611450SN/ASyscallReturn
56211851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
563511SN/A{
5641706SN/A    string old_name;
565511SN/A
5666701Sgblack@eecs.umich.edu    int index = 0;
5678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
5681458SN/A        return -EFAULT;
569511SN/A
5701706SN/A    string new_name;
571511SN/A
5728852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
5731458SN/A        return -EFAULT;
574511SN/A
57513883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
57613883Sdavid.hashe@amd.com    old_name = p->checkPathRedirect(old_name);
57713883Sdavid.hashe@amd.com    new_name = p->checkPathRedirect(new_name);
5783669Sbinkertn@umich.edu
5791706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
5801458SN/A    return (result == -1) ? -errno : result;
581511SN/A}
582511SN/A
5831706SN/ASyscallReturn
58411851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5851706SN/A{
5861706SN/A    string path;
5871706SN/A
5886701Sgblack@eecs.umich.edu    int index = 0;
5898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5901706SN/A        return -EFAULT;
5911706SN/A
5926701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5931706SN/A
59413883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
59513883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
5963669Sbinkertn@umich.edu
5971706SN/A    int result = truncate(path.c_str(), length);
5981706SN/A    return (result == -1) ? -errno : result;
5991706SN/A}
6001706SN/A
6011706SN/ASyscallReturn
60211856Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6031706SN/A{
6046701Sgblack@eecs.umich.edu    int index = 0;
60511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
60611856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
6071706SN/A
60811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
60911856Sbrandon.potter@amd.com    if (!ffdp)
6101706SN/A        return -EBADF;
61111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6121706SN/A
61310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
6141706SN/A    return (result == -1) ? -errno : result;
6151706SN/A}
6161999SN/A
6171999SN/ASyscallReturn
6186703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
61911851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
6206703Svince@csl.cornell.edu{
6216703Svince@csl.cornell.edu    int index = 0;
6226703Svince@csl.cornell.edu    string path;
6236703Svince@csl.cornell.edu
6248852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
62511906SBrandon.Potter@amd.com        return -EFAULT;
6266703Svince@csl.cornell.edu
6276744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
6286703Svince@csl.cornell.edu
62913883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
63013883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
6316703Svince@csl.cornell.edu
6326744SAli.Saidi@arm.com#if NO_STAT64
6336744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
6346744SAli.Saidi@arm.com#else
6356703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
6366744SAli.Saidi@arm.com#endif
6376703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
6386703Svince@csl.cornell.edu}
6396703Svince@csl.cornell.edu
6406703Svince@csl.cornell.eduSyscallReturn
64111856Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6426685Stjones1@inf.ed.ac.uk{
6436701Sgblack@eecs.umich.edu    int index = 0;
64411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
64511856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
6466685Stjones1@inf.ed.ac.uk
64711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
64811856Sbrandon.potter@amd.com    if (!ffdp)
6496685Stjones1@inf.ed.ac.uk        return -EBADF;
65011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
6516685Stjones1@inf.ed.ac.uk
6526744SAli.Saidi@arm.com#if NO_STAT64
65310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
6546744SAli.Saidi@arm.com#else
65510931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
6566744SAli.Saidi@arm.com#endif
6576685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
6586685Stjones1@inf.ed.ac.uk}
6596685Stjones1@inf.ed.ac.uk
6606685Stjones1@inf.ed.ac.ukSyscallReturn
66111851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
6625513SMichael.Adler@intel.com{
6635513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
6645513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
6655513SMichael.Adler@intel.com    // changing anything.
6665513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
6675513SMichael.Adler@intel.com    umask(oldMask);
6685521Snate@binkert.org    return (int)oldMask;
6695513SMichael.Adler@intel.com}
6705513SMichael.Adler@intel.com
6715513SMichael.Adler@intel.comSyscallReturn
67211851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6731999SN/A{
6741999SN/A    string path;
6751999SN/A
6766701Sgblack@eecs.umich.edu    int index = 0;
6778852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6781999SN/A        return -EFAULT;
6791999SN/A
6801999SN/A    /* XXX endianess */
6816701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
6821999SN/A    uid_t hostOwner = owner;
6836701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6841999SN/A    gid_t hostGroup = group;
6851999SN/A
68613883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
68713883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
6883669Sbinkertn@umich.edu
6891999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6901999SN/A    return (result == -1) ? -errno : result;
6911999SN/A}
6921999SN/A
6931999SN/ASyscallReturn
69411856Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6951999SN/A{
6966701Sgblack@eecs.umich.edu    int index = 0;
69711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6981999SN/A
69911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
70011856Sbrandon.potter@amd.com    if (!ffdp)
7011999SN/A        return -EBADF;
70211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
7031999SN/A
7041999SN/A    /* XXX endianess */
70511856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
7061999SN/A    uid_t hostOwner = owner;
70711856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
7081999SN/A    gid_t hostGroup = group;
7091999SN/A
71010931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
7111999SN/A    return (result == -1) ? -errno : result;
7121999SN/A}
7132093SN/A
71411856Sbrandon.potter@amd.com/**
71511908SBrandon.Potter@amd.com * FIXME: The file description is not shared among file descriptors created
71611908SBrandon.Potter@amd.com * with dup. Really, it's difficult to maintain fields like file offset or
71711908SBrandon.Potter@amd.com * flags since an update to such a field won't be reflected in the metadata
71811908SBrandon.Potter@amd.com * for the fd entries that we maintain for checkpoint restoration.
71911856Sbrandon.potter@amd.com */
7202093SN/ASyscallReturn
72111856Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
7223079Sstever@eecs.umich.edu{
7236701Sgblack@eecs.umich.edu    int index = 0;
72411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
72510931Sbrandon.potter@amd.com
72611856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
72711856Sbrandon.potter@amd.com    if (!old_hbfdp)
7283079Sstever@eecs.umich.edu        return -EBADF;
72911856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
7305282Srstrong@cs.ucsd.edu
73110781Snilay@cs.wisc.edu    int result = dup(sim_fd);
73211908SBrandon.Potter@amd.com    if (result == -1)
73311908SBrandon.Potter@amd.com        return -errno;
73411856Sbrandon.potter@amd.com
73511908SBrandon.Potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone());
73611856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
73711908SBrandon.Potter@amd.com    new_hbfdp->setCOE(false);
73811908SBrandon.Potter@amd.com    return p->fds->allocFD(new_hbfdp);
73911908SBrandon.Potter@amd.com}
74011856Sbrandon.potter@amd.com
74111908SBrandon.Potter@amd.comSyscallReturn
74211908SBrandon.Potter@amd.comdup2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
74311908SBrandon.Potter@amd.com{
74411908SBrandon.Potter@amd.com    int index = 0;
74511908SBrandon.Potter@amd.com
74611908SBrandon.Potter@amd.com    int old_tgt_fd = p->getSyscallArg(tc, index);
74711908SBrandon.Potter@amd.com    auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]);
74811908SBrandon.Potter@amd.com    if (!old_hbp)
74911908SBrandon.Potter@amd.com        return -EBADF;
75011908SBrandon.Potter@amd.com    int old_sim_fd = old_hbp->getSimFD();
75111908SBrandon.Potter@amd.com
75211908SBrandon.Potter@amd.com    /**
75311908SBrandon.Potter@amd.com     * We need a valid host file descriptor number to be able to pass into
75411908SBrandon.Potter@amd.com     * the second parameter for dup2 (newfd), but we don't know what the
75511908SBrandon.Potter@amd.com     * viable numbers are; we execute the open call to retrieve one.
75611908SBrandon.Potter@amd.com     */
75711908SBrandon.Potter@amd.com    int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY));
75811908SBrandon.Potter@amd.com    if (res_fd == -1)
75911908SBrandon.Potter@amd.com        return -errno;
76011908SBrandon.Potter@amd.com
76111908SBrandon.Potter@amd.com    int new_tgt_fd = p->getSyscallArg(tc, index);
76211908SBrandon.Potter@amd.com    auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]);
76311908SBrandon.Potter@amd.com    if (new_hbp)
76411908SBrandon.Potter@amd.com        p->fds->closeFDEntry(new_tgt_fd);
76511908SBrandon.Potter@amd.com    new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone());
76611908SBrandon.Potter@amd.com    new_hbp->setSimFD(res_fd);
76711908SBrandon.Potter@amd.com    new_hbp->setCOE(false);
76811908SBrandon.Potter@amd.com
76911908SBrandon.Potter@amd.com    return p->fds->allocFD(new_hbp);
7703079Sstever@eecs.umich.edu}
7713079Sstever@eecs.umich.edu
7723079Sstever@eecs.umich.eduSyscallReturn
77311856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
7742093SN/A{
77511875Sbrandon.potter@amd.com    int arg;
7766701Sgblack@eecs.umich.edu    int index = 0;
77711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
77811875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
7792093SN/A
78011856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
78111856Sbrandon.potter@amd.com    if (!hbfdp)
7822093SN/A        return -EBADF;
78311856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
7842093SN/A
78511875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
78611875Sbrandon.potter@amd.com
7872093SN/A    switch (cmd) {
78811875Sbrandon.potter@amd.com      case F_GETFD:
78911875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
7902093SN/A
79111875Sbrandon.potter@amd.com      case F_SETFD: {
79211875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
79311875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
7942093SN/A        return 0;
79511875Sbrandon.potter@amd.com      }
7962093SN/A
79711875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
79811875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
79911875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
80011875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
80111875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
80211875Sbrandon.potter@amd.com      // subsequent fcntls.
80311875Sbrandon.potter@amd.com      case F_GETFL:
80411875Sbrandon.potter@amd.com      case F_SETFL: {
80511875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
80611875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
80711875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
80811875Sbrandon.potter@amd.com      }
8092093SN/A
8102093SN/A      default:
81111875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
8122093SN/A        return 0;
8132093SN/A    }
8142093SN/A}
8152093SN/A
8162238SN/ASyscallReturn
81711856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
8182687Sksewell@umich.edu{
8196701Sgblack@eecs.umich.edu    int index = 0;
82011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
8212687Sksewell@umich.edu
82211856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
82311856Sbrandon.potter@amd.com    if (!hbfdp)
8242687Sksewell@umich.edu        return -EBADF;
82511856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
8262687Sksewell@umich.edu
82711856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
8282687Sksewell@umich.edu    switch (cmd) {
8292687Sksewell@umich.edu      case 33: //F_GETLK64
83010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
8312687Sksewell@umich.edu        return -EMFILE;
8322687Sksewell@umich.edu
8332687Sksewell@umich.edu      case 34: // F_SETLK64
8342687Sksewell@umich.edu      case 35: // F_SETLKW64
83510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
83610931Sbrandon.potter@amd.com             tgt_fd);
8372687Sksewell@umich.edu        return -EMFILE;
8382687Sksewell@umich.edu
8392687Sksewell@umich.edu      default:
8402687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
8412687Sksewell@umich.edu        // to the underlying OS
84210931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
84310931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
8442687Sksewell@umich.edu    }
8452687Sksewell@umich.edu}
8462687Sksewell@umich.edu
8472687Sksewell@umich.eduSyscallReturn
84811908SBrandon.Potter@amd.compipeImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
84911908SBrandon.Potter@amd.com         bool pseudoPipe)
8502238SN/A{
85111908SBrandon.Potter@amd.com    Addr tgt_addr = 0;
85211908SBrandon.Potter@amd.com    if (!pseudoPipe) {
85311908SBrandon.Potter@amd.com        int index = 0;
85411908SBrandon.Potter@amd.com        tgt_addr = p->getSyscallArg(tc, index);
85511908SBrandon.Potter@amd.com    }
85611908SBrandon.Potter@amd.com
85711856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
8582093SN/A
85911856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
86011908SBrandon.Potter@amd.com    if (pipe_retval == -1)
86111908SBrandon.Potter@amd.com        return -errno;
8622238SN/A
86311856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
86411856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
86511908SBrandon.Potter@amd.com    tgt_fds[0] = p->fds->allocFD(rpfd);
8662238SN/A
86711856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
86811856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
86911908SBrandon.Potter@amd.com    tgt_fds[1] = p->fds->allocFD(wpfd);
87011856Sbrandon.potter@amd.com
87111856Sbrandon.potter@amd.com    /**
87211856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
87311856Sbrandon.potter@amd.com     * as the write end of the pipe.
87411856Sbrandon.potter@amd.com     */
87511856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
87611856Sbrandon.potter@amd.com
87711856Sbrandon.potter@amd.com    /**
87811856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
87911856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
88011856Sbrandon.potter@amd.com     */
88111908SBrandon.Potter@amd.com    if (pseudoPipe) {
88211908SBrandon.Potter@amd.com        tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
88311908SBrandon.Potter@amd.com        return tgt_fds[0];
88411908SBrandon.Potter@amd.com    }
88511908SBrandon.Potter@amd.com
88611908SBrandon.Potter@amd.com    /**
88711908SBrandon.Potter@amd.com     * Copy the target file descriptors into buffer space and then copy
88811908SBrandon.Potter@amd.com     * the buffer space back into the target address space.
88911908SBrandon.Potter@amd.com     */
89011908SBrandon.Potter@amd.com    BufferArg tgt_handle(tgt_addr, sizeof(int[2]));
89111908SBrandon.Potter@amd.com    int *buf_ptr = (int*)tgt_handle.bufferPtr();
89211908SBrandon.Potter@amd.com    buf_ptr[0] = tgt_fds[0];
89311908SBrandon.Potter@amd.com    buf_ptr[1] = tgt_fds[1];
89411908SBrandon.Potter@amd.com    tgt_handle.copyOut(tc->getMemProxy());
89511908SBrandon.Potter@amd.com    return 0;
89611908SBrandon.Potter@amd.com}
89711908SBrandon.Potter@amd.com
89811908SBrandon.Potter@amd.comSyscallReturn
89911908SBrandon.Potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
90011908SBrandon.Potter@amd.com               ThreadContext *tc)
90111908SBrandon.Potter@amd.com{
90211908SBrandon.Potter@amd.com    return pipeImpl(desc, callnum, process, tc, true);
90311908SBrandon.Potter@amd.com}
90411908SBrandon.Potter@amd.com
90511908SBrandon.Potter@amd.comSyscallReturn
90611908SBrandon.Potter@amd.compipeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
90711908SBrandon.Potter@amd.com{
90811908SBrandon.Potter@amd.com    return pipeImpl(desc, callnum, process, tc, false);
9092238SN/A}
9102238SN/A
91111885Sbrandon.potter@amd.comSyscallReturn
91211885Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, Process *process,
91311885Sbrandon.potter@amd.com            ThreadContext *tc)
91411885Sbrandon.potter@amd.com{
91511885Sbrandon.potter@amd.com    int index = 0;
91611885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
91711885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
91811885Sbrandon.potter@amd.com
91911885Sbrandon.potter@amd.com    if (pgid < 0)
92011885Sbrandon.potter@amd.com        return -EINVAL;
92111885Sbrandon.potter@amd.com
92211885Sbrandon.potter@amd.com    if (pid == 0) {
92311885Sbrandon.potter@amd.com        process->setpgid(process->pid());
92411885Sbrandon.potter@amd.com        return 0;
92511885Sbrandon.potter@amd.com    }
92611885Sbrandon.potter@amd.com
92711913SBrandon.Potter@amd.com    Process *matched_ph = nullptr;
92811885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
92911885Sbrandon.potter@amd.com
93011885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
93111885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
93211885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
93311885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
93411885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
93511885Sbrandon.potter@amd.com
93611885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
93711885Sbrandon.potter@amd.com                matched_ph = walk_ph;
93811885Sbrandon.potter@amd.com        }
93911885Sbrandon.potter@amd.com    }
94011885Sbrandon.potter@amd.com
94111913SBrandon.Potter@amd.com    assert(matched_ph);
94211885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
94311885Sbrandon.potter@amd.com
94411885Sbrandon.potter@amd.com    return 0;
94511885Sbrandon.potter@amd.com}
9462238SN/A
9472238SN/ASyscallReturn
94811851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
94911851Sbrandon.potter@amd.com                 ThreadContext *tc)
9502238SN/A{
9512238SN/A    // Make up a PID.  There's no interprocess communication in
9522238SN/A    // fake_syscall mode, so there's no way for a process to know it's
9532238SN/A    // not getting a unique value.
9542238SN/A
9553114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
9563114Sgblack@eecs.umich.edu    return process->pid();
9572238SN/A}
9582238SN/A
9592238SN/A
9602238SN/ASyscallReturn
96111851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
96211851Sbrandon.potter@amd.com                 ThreadContext *tc)
9632238SN/A{
9642238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
9652238SN/A    // simulation to be deterministic.
9662238SN/A
9672238SN/A    // EUID goes in r20.
96811906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID
96911906SBrandon.Potter@amd.com    return process->uid(); // UID
9702238SN/A}
9712238SN/A
9722238SN/A
9732238SN/ASyscallReturn
97411851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
97511851Sbrandon.potter@amd.com                 ThreadContext *tc)
9762238SN/A{
9772238SN/A    // Get current group ID.  EGID goes in r20.
97811906SBrandon.Potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID
9793114Sgblack@eecs.umich.edu    return process->gid();
9802238SN/A}
9812238SN/A
9822238SN/A
9832238SN/ASyscallReturn
98411851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
9852680Sktlim@umich.edu           ThreadContext *tc)
9862238SN/A{
9872238SN/A    // can't fathom why a benchmark would call this.
9886701Sgblack@eecs.umich.edu    int index = 0;
9896701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
9902238SN/A    return 0;
9912238SN/A}
9922238SN/A
9932238SN/ASyscallReturn
99411851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
9952680Sktlim@umich.edu           ThreadContext *tc)
9962238SN/A{
99711885Sbrandon.potter@amd.com    return process->tgid();
99811885Sbrandon.potter@amd.com}
9992238SN/A
100011885Sbrandon.potter@amd.comSyscallReturn
100111885Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, Process *process,
100211885Sbrandon.potter@amd.com           ThreadContext *tc)
100311885Sbrandon.potter@amd.com{
10043114Sgblack@eecs.umich.edu    return process->pid();
10052238SN/A}
10062238SN/A
10072238SN/ASyscallReturn
100811851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
100911851Sbrandon.potter@amd.com            ThreadContext *tc)
10102238SN/A{
10113114Sgblack@eecs.umich.edu    return process->ppid();
10122238SN/A}
10132238SN/A
10142238SN/ASyscallReturn
101511851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
10162680Sktlim@umich.edu           ThreadContext *tc)
10172238SN/A{
10185543Ssaidi@eecs.umich.edu    return process->uid();              // UID
10192238SN/A}
10202238SN/A
10212238SN/ASyscallReturn
102211851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
102311851Sbrandon.potter@amd.com            ThreadContext *tc)
10242238SN/A{
10255543Ssaidi@eecs.umich.edu    return process->euid();             // UID
10262238SN/A}
10272238SN/A
10282238SN/ASyscallReturn
102911851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
10302680Sktlim@umich.edu           ThreadContext *tc)
10312238SN/A{
10323114Sgblack@eecs.umich.edu    return process->gid();
10332238SN/A}
10342238SN/A
10352238SN/ASyscallReturn
103611851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
103711851Sbrandon.potter@amd.com            ThreadContext *tc)
10382238SN/A{
10393114Sgblack@eecs.umich.edu    return process->egid();
10402238SN/A}
10412238SN/A
10429455Smitch.hayenga+gem5@gmail.comSyscallReturn
104311856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
104411760Sbrandon.potter@amd.com{
104513933Sbrandon.potter@amd.com#if __linux__
104611760Sbrandon.potter@amd.com    int index = 0;
104711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
104811856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
104911856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
105011856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
105111760Sbrandon.potter@amd.com
105211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
105311856Sbrandon.potter@amd.com    if (!ffdp)
105411760Sbrandon.potter@amd.com        return -EBADF;
105511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
105611760Sbrandon.potter@amd.com
105711760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
105811760Sbrandon.potter@amd.com    if (result < 0)
105911760Sbrandon.potter@amd.com        return -errno;
106013933Sbrandon.potter@amd.com    return 0;
106113933Sbrandon.potter@amd.com#else
106213933Sbrandon.potter@amd.com    warnUnsupportedOS("fallocate");
106313933Sbrandon.potter@amd.com    return -1;
106411799Sbrandon.potter@amd.com#endif
106511760Sbrandon.potter@amd.com}
106611760Sbrandon.potter@amd.com
106711760Sbrandon.potter@amd.comSyscallReturn
106811851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
106911851Sbrandon.potter@amd.com           int index)
10709455Smitch.hayenga+gem5@gmail.com{
10719455Smitch.hayenga+gem5@gmail.com    string path;
10729455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
107310223Ssteve.reinhardt@amd.com        return -EFAULT;
10749455Smitch.hayenga+gem5@gmail.com
107513883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
107613883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
10779455Smitch.hayenga+gem5@gmail.com
10789455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
10799455Smitch.hayenga+gem5@gmail.com
10809455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
10819455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
10829455Smitch.hayenga+gem5@gmail.com}
108310203SAli.Saidi@ARM.com
108410203SAli.Saidi@ARM.comSyscallReturn
108511851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
108610203SAli.Saidi@ARM.com{
108710203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
108810203SAli.Saidi@ARM.com}
108910203SAli.Saidi@ARM.com
109013031Sbrandon.potter@amd.comSyscallReturn
109113031Sbrandon.potter@amd.commknodFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
109213031Sbrandon.potter@amd.com{
109313031Sbrandon.potter@amd.com    int index = 0;
109413031Sbrandon.potter@amd.com    std::string path;
109513031Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
109613031Sbrandon.potter@amd.com        return -EFAULT;
109713031Sbrandon.potter@amd.com
109813883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
109913031Sbrandon.potter@amd.com    mode_t mode = p->getSyscallArg(tc, index);
110013031Sbrandon.potter@amd.com    dev_t dev = p->getSyscallArg(tc, index);
110113031Sbrandon.potter@amd.com
110213031Sbrandon.potter@amd.com    auto result = mknod(path.c_str(), mode, dev);
110313031Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
110413031Sbrandon.potter@amd.com}
110513031Sbrandon.potter@amd.com
110613031Sbrandon.potter@amd.comSyscallReturn
110713031Sbrandon.potter@amd.comchdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
110813031Sbrandon.potter@amd.com{
110913031Sbrandon.potter@amd.com    int index = 0;
111013031Sbrandon.potter@amd.com    std::string path;
111113031Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
111213031Sbrandon.potter@amd.com        return -EFAULT;
111313031Sbrandon.potter@amd.com
111413883Sdavid.hashe@amd.com    std::string tgt_cwd;
111513883Sdavid.hashe@amd.com    if (startswith(path, "/")) {
111613883Sdavid.hashe@amd.com        tgt_cwd = path;
111713883Sdavid.hashe@amd.com    } else {
111813883Sdavid.hashe@amd.com        char buf[PATH_MAX];
111913883Sdavid.hashe@amd.com        tgt_cwd = realpath((p->tgtCwd + "/" + path).c_str(), buf);
112013883Sdavid.hashe@amd.com    }
112113883Sdavid.hashe@amd.com    std::string host_cwd = p->checkPathRedirect(tgt_cwd);
112213031Sbrandon.potter@amd.com
112313883Sdavid.hashe@amd.com    int result = chdir(host_cwd.c_str());
112413883Sdavid.hashe@amd.com
112513883Sdavid.hashe@amd.com    if (result == -1)
112613883Sdavid.hashe@amd.com        return -errno;
112713883Sdavid.hashe@amd.com
112813883Sdavid.hashe@amd.com    p->hostCwd = host_cwd;
112913883Sdavid.hashe@amd.com    p->tgtCwd = tgt_cwd;
113013883Sdavid.hashe@amd.com    return result;
113113031Sbrandon.potter@amd.com}
113213031Sbrandon.potter@amd.com
113313031Sbrandon.potter@amd.comSyscallReturn
113413031Sbrandon.potter@amd.comrmdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
113513031Sbrandon.potter@amd.com{
113613031Sbrandon.potter@amd.com    int index = 0;
113713031Sbrandon.potter@amd.com    std::string path;
113813031Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
113913031Sbrandon.potter@amd.com        return -EFAULT;
114013031Sbrandon.potter@amd.com
114113883Sdavid.hashe@amd.com    path = p->checkPathRedirect(path);
114213031Sbrandon.potter@amd.com
114313031Sbrandon.potter@amd.com    auto result = rmdir(path.c_str());
114413031Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
114513031Sbrandon.potter@amd.com}
114613031Sbrandon.potter@amd.com
114713539Sjavier.setoain@arm.com#if defined(SYS_getdents) || defined(SYS_getdents64)
114813539Sjavier.setoain@arm.comtemplate<typename DE, int SYS_NUM>
114913539Sjavier.setoain@arm.comstatic SyscallReturn
115013539Sjavier.setoain@arm.comgetdentsImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
115113031Sbrandon.potter@amd.com{
115213031Sbrandon.potter@amd.com    int index = 0;
115313031Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
115413031Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
115513031Sbrandon.potter@amd.com    unsigned count = p->getSyscallArg(tc, index);
115613031Sbrandon.potter@amd.com
115713031Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
115813031Sbrandon.potter@amd.com    if (!hbfdp)
115913031Sbrandon.potter@amd.com        return -EBADF;
116013031Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
116113031Sbrandon.potter@amd.com
116213031Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, count);
116313539Sjavier.setoain@arm.com    auto status = syscall(SYS_NUM, sim_fd, buf_arg.bufferPtr(), count);
116413031Sbrandon.potter@amd.com
116513031Sbrandon.potter@amd.com    if (status == -1)
116613031Sbrandon.potter@amd.com        return -errno;
116713031Sbrandon.potter@amd.com
116813031Sbrandon.potter@amd.com    unsigned traversed = 0;
116913031Sbrandon.potter@amd.com    while (traversed < status) {
117013539Sjavier.setoain@arm.com        DE *buffer = (DE*)((Addr)buf_arg.bufferPtr() + traversed);
117113031Sbrandon.potter@amd.com
117213031Sbrandon.potter@amd.com        auto host_reclen = buffer->d_reclen;
117313031Sbrandon.potter@amd.com
117413031Sbrandon.potter@amd.com        /**
117513031Sbrandon.potter@amd.com         * Convert the byte ordering from the host to the target before
117613031Sbrandon.potter@amd.com         * passing the data back into the target's address space to preserve
117713031Sbrandon.potter@amd.com         * endianness.
117813031Sbrandon.potter@amd.com         */
117913031Sbrandon.potter@amd.com        buffer->d_ino = htog(buffer->d_ino);
118013031Sbrandon.potter@amd.com        buffer->d_off = htog(buffer->d_off);
118113031Sbrandon.potter@amd.com        buffer->d_reclen = htog(buffer->d_reclen);
118213031Sbrandon.potter@amd.com
118313031Sbrandon.potter@amd.com        traversed += host_reclen;
118413031Sbrandon.potter@amd.com    }
118513031Sbrandon.potter@amd.com
118613031Sbrandon.potter@amd.com    buf_arg.copyOut(tc->getMemProxy());
118713031Sbrandon.potter@amd.com    return status;
118813031Sbrandon.potter@amd.com}
118913448Sciro.santilli@arm.com#endif
119013539Sjavier.setoain@arm.com
119113539Sjavier.setoain@arm.com#if defined(SYS_getdents)
119213539Sjavier.setoain@arm.comSyscallReturn
119313539Sjavier.setoain@arm.comgetdentsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
119413539Sjavier.setoain@arm.com{
119513539Sjavier.setoain@arm.com    typedef struct linux_dirent {
119613539Sjavier.setoain@arm.com        unsigned long d_ino;
119713539Sjavier.setoain@arm.com        unsigned long d_off;
119813539Sjavier.setoain@arm.com        unsigned short d_reclen;
119913539Sjavier.setoain@arm.com        char dname[];
120013539Sjavier.setoain@arm.com    } LinDent;
120113539Sjavier.setoain@arm.com
120213539Sjavier.setoain@arm.com    return getdentsImpl<LinDent, SYS_getdents>(desc, callnum, p, tc);
120313539Sjavier.setoain@arm.com}
120413539Sjavier.setoain@arm.com#endif
120513539Sjavier.setoain@arm.com
120613539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
120713539Sjavier.setoain@arm.comSyscallReturn
120813539Sjavier.setoain@arm.comgetdents64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
120913539Sjavier.setoain@arm.com{
121013539Sjavier.setoain@arm.com    typedef struct linux_dirent64 {
121113539Sjavier.setoain@arm.com        ino64_t d_ino;
121213539Sjavier.setoain@arm.com        off64_t d_off;
121313539Sjavier.setoain@arm.com        unsigned short d_reclen;
121413539Sjavier.setoain@arm.com        char dname[];
121513539Sjavier.setoain@arm.com    } LinDent64;
121613539Sjavier.setoain@arm.com
121713539Sjavier.setoain@arm.com    return getdentsImpl<LinDent64, SYS_getdents64>(desc, callnum, p, tc);
121813539Sjavier.setoain@arm.com}
121913539Sjavier.setoain@arm.com#endif
122013568Sbrandon.potter@amd.com
122113568Sbrandon.potter@amd.comSyscallReturn
122213568Sbrandon.potter@amd.comshutdownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
122313568Sbrandon.potter@amd.com{
122413568Sbrandon.potter@amd.com    int index = 0;
122513568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
122613568Sbrandon.potter@amd.com    int how = p->getSyscallArg(tc, index);
122713568Sbrandon.potter@amd.com
122813568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
122913568Sbrandon.potter@amd.com    if (!sfdp)
123013568Sbrandon.potter@amd.com        return -EBADF;
123113568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
123213568Sbrandon.potter@amd.com
123313568Sbrandon.potter@amd.com    int retval = shutdown(sim_fd, how);
123413568Sbrandon.potter@amd.com
123513568Sbrandon.potter@amd.com    return (retval == -1) ? -errno : retval;
123613568Sbrandon.potter@amd.com}
123713568Sbrandon.potter@amd.com
123813568Sbrandon.potter@amd.comSyscallReturn
123913568Sbrandon.potter@amd.combindFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
124013568Sbrandon.potter@amd.com{
124113568Sbrandon.potter@amd.com    int index = 0;
124213568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
124313568Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
124413568Sbrandon.potter@amd.com    int addrlen = p->getSyscallArg(tc, index);
124513568Sbrandon.potter@amd.com
124613568Sbrandon.potter@amd.com    BufferArg bufSock(buf_ptr, addrlen);
124713568Sbrandon.potter@amd.com    bufSock.copyIn(tc->getMemProxy());
124813568Sbrandon.potter@amd.com
124913568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
125013568Sbrandon.potter@amd.com    if (!sfdp)
125113568Sbrandon.potter@amd.com        return -EBADF;
125213568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
125313568Sbrandon.potter@amd.com
125413568Sbrandon.potter@amd.com    int status = ::bind(sim_fd,
125513568Sbrandon.potter@amd.com                        (struct sockaddr *)bufSock.bufferPtr(),
125613568Sbrandon.potter@amd.com                        addrlen);
125713568Sbrandon.potter@amd.com
125813568Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
125913568Sbrandon.potter@amd.com}
126013568Sbrandon.potter@amd.com
126113568Sbrandon.potter@amd.comSyscallReturn
126213568Sbrandon.potter@amd.comlistenFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
126313568Sbrandon.potter@amd.com{
126413568Sbrandon.potter@amd.com    int index = 0;
126513568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
126613568Sbrandon.potter@amd.com    int backlog = p->getSyscallArg(tc, index);
126713568Sbrandon.potter@amd.com
126813568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
126913568Sbrandon.potter@amd.com    if (!sfdp)
127013568Sbrandon.potter@amd.com        return -EBADF;
127113568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
127213568Sbrandon.potter@amd.com
127313568Sbrandon.potter@amd.com    int status = listen(sim_fd, backlog);
127413568Sbrandon.potter@amd.com
127513568Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
127613568Sbrandon.potter@amd.com}
127713568Sbrandon.potter@amd.com
127813568Sbrandon.potter@amd.comSyscallReturn
127913568Sbrandon.potter@amd.comconnectFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
128013568Sbrandon.potter@amd.com{
128113568Sbrandon.potter@amd.com    int index = 0;
128213568Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
128313568Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
128413568Sbrandon.potter@amd.com    int addrlen = p->getSyscallArg(tc, index);
128513568Sbrandon.potter@amd.com
128613568Sbrandon.potter@amd.com    BufferArg addr(buf_ptr, addrlen);
128713568Sbrandon.potter@amd.com    addr.copyIn(tc->getMemProxy());
128813568Sbrandon.potter@amd.com
128913568Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
129013568Sbrandon.potter@amd.com    if (!sfdp)
129113568Sbrandon.potter@amd.com        return -EBADF;
129213568Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
129313568Sbrandon.potter@amd.com
129413568Sbrandon.potter@amd.com    int status = connect(sim_fd,
129513568Sbrandon.potter@amd.com                         (struct sockaddr *)addr.bufferPtr(),
129613568Sbrandon.potter@amd.com                         (socklen_t)addrlen);
129713568Sbrandon.potter@amd.com
129813568Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
129913568Sbrandon.potter@amd.com}
130013569Sbrandon.potter@amd.com
130113569Sbrandon.potter@amd.comSyscallReturn
130213569Sbrandon.potter@amd.comrecvfromFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
130313569Sbrandon.potter@amd.com{
130413569Sbrandon.potter@amd.com    int index = 0;
130513569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
130613569Sbrandon.potter@amd.com    Addr bufrPtr = p->getSyscallArg(tc, index);
130713569Sbrandon.potter@amd.com    size_t bufrLen = p->getSyscallArg(tc, index);
130813569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
130913569Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
131013569Sbrandon.potter@amd.com    Addr addrlenPtr = p->getSyscallArg(tc, index);
131113569Sbrandon.potter@amd.com
131213569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
131313569Sbrandon.potter@amd.com    if (!sfdp)
131413569Sbrandon.potter@amd.com        return -EBADF;
131513569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
131613569Sbrandon.potter@amd.com
131713569Sbrandon.potter@amd.com    // Reserve buffer space.
131813569Sbrandon.potter@amd.com    BufferArg bufrBuf(bufrPtr, bufrLen);
131913569Sbrandon.potter@amd.com
132013569Sbrandon.potter@amd.com    // Get address length.
132113569Sbrandon.potter@amd.com    socklen_t addrLen = 0;
132213569Sbrandon.potter@amd.com    if (addrlenPtr != 0) {
132313569Sbrandon.potter@amd.com        // Read address length parameter.
132413569Sbrandon.potter@amd.com        BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t));
132513569Sbrandon.potter@amd.com        addrlenBuf.copyIn(tc->getMemProxy());
132613569Sbrandon.potter@amd.com        addrLen = *((socklen_t *)addrlenBuf.bufferPtr());
132713569Sbrandon.potter@amd.com    }
132813569Sbrandon.potter@amd.com
132913569Sbrandon.potter@amd.com    struct sockaddr sa, *sap = NULL;
133013569Sbrandon.potter@amd.com    if (addrLen != 0) {
133113569Sbrandon.potter@amd.com        BufferArg addrBuf(addrPtr, addrLen);
133213569Sbrandon.potter@amd.com        addrBuf.copyIn(tc->getMemProxy());
133313569Sbrandon.potter@amd.com        memcpy(&sa, (struct sockaddr *)addrBuf.bufferPtr(),
133413569Sbrandon.potter@amd.com               sizeof(struct sockaddr));
133513569Sbrandon.potter@amd.com        sap = &sa;
133613569Sbrandon.potter@amd.com    }
133713569Sbrandon.potter@amd.com
133813569Sbrandon.potter@amd.com    ssize_t recvd_size = recvfrom(sim_fd,
133913569Sbrandon.potter@amd.com                                  (void *)bufrBuf.bufferPtr(),
134013569Sbrandon.potter@amd.com                                  bufrLen, flags, sap, (socklen_t *)&addrLen);
134113569Sbrandon.potter@amd.com
134213569Sbrandon.potter@amd.com    if (recvd_size == -1)
134313569Sbrandon.potter@amd.com        return -errno;
134413569Sbrandon.potter@amd.com
134513569Sbrandon.potter@amd.com    // Pass the received data out.
134613569Sbrandon.potter@amd.com    bufrBuf.copyOut(tc->getMemProxy());
134713569Sbrandon.potter@amd.com
134813569Sbrandon.potter@amd.com    // Copy address to addrPtr and pass it on.
134913569Sbrandon.potter@amd.com    if (sap != NULL) {
135013569Sbrandon.potter@amd.com        BufferArg addrBuf(addrPtr, addrLen);
135113569Sbrandon.potter@amd.com        memcpy(addrBuf.bufferPtr(), sap, sizeof(sa));
135213569Sbrandon.potter@amd.com        addrBuf.copyOut(tc->getMemProxy());
135313569Sbrandon.potter@amd.com    }
135413569Sbrandon.potter@amd.com
135513569Sbrandon.potter@amd.com    // Copy len to addrlenPtr and pass it on.
135613569Sbrandon.potter@amd.com    if (addrLen != 0) {
135713569Sbrandon.potter@amd.com        BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t));
135813569Sbrandon.potter@amd.com        *(socklen_t *)addrlenBuf.bufferPtr() = addrLen;
135913569Sbrandon.potter@amd.com        addrlenBuf.copyOut(tc->getMemProxy());
136013569Sbrandon.potter@amd.com    }
136113569Sbrandon.potter@amd.com
136213569Sbrandon.potter@amd.com    return recvd_size;
136313569Sbrandon.potter@amd.com}
136413569Sbrandon.potter@amd.com
136513569Sbrandon.potter@amd.comSyscallReturn
136613569Sbrandon.potter@amd.comsendtoFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
136713569Sbrandon.potter@amd.com{
136813569Sbrandon.potter@amd.com    int index = 0;
136913569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
137013569Sbrandon.potter@amd.com    Addr bufrPtr = p->getSyscallArg(tc, index);
137113569Sbrandon.potter@amd.com    size_t bufrLen = p->getSyscallArg(tc, index);
137213569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
137313569Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
137413569Sbrandon.potter@amd.com    socklen_t addrLen = p->getSyscallArg(tc, index);
137513569Sbrandon.potter@amd.com
137613569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
137713569Sbrandon.potter@amd.com    if (!sfdp)
137813569Sbrandon.potter@amd.com        return -EBADF;
137913569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
138013569Sbrandon.potter@amd.com
138113569Sbrandon.potter@amd.com    // Reserve buffer space.
138213569Sbrandon.potter@amd.com    BufferArg bufrBuf(bufrPtr, bufrLen);
138313569Sbrandon.potter@amd.com    bufrBuf.copyIn(tc->getMemProxy());
138413569Sbrandon.potter@amd.com
138513569Sbrandon.potter@amd.com    struct sockaddr sa, *sap = nullptr;
138613569Sbrandon.potter@amd.com    memset(&sa, 0, sizeof(sockaddr));
138713569Sbrandon.potter@amd.com    if (addrLen != 0) {
138813569Sbrandon.potter@amd.com        BufferArg addrBuf(addrPtr, addrLen);
138913569Sbrandon.potter@amd.com        addrBuf.copyIn(tc->getMemProxy());
139013569Sbrandon.potter@amd.com        memcpy(&sa, (sockaddr*)addrBuf.bufferPtr(), addrLen);
139113569Sbrandon.potter@amd.com        sap = &sa;
139213569Sbrandon.potter@amd.com    }
139313569Sbrandon.potter@amd.com
139413569Sbrandon.potter@amd.com    ssize_t sent_size = sendto(sim_fd,
139513569Sbrandon.potter@amd.com                               (void *)bufrBuf.bufferPtr(),
139613569Sbrandon.potter@amd.com                               bufrLen, flags, sap, (socklen_t)addrLen);
139713569Sbrandon.potter@amd.com
139813569Sbrandon.potter@amd.com    return (sent_size == -1) ? -errno : sent_size;
139913569Sbrandon.potter@amd.com}
140013569Sbrandon.potter@amd.com
140113569Sbrandon.potter@amd.comSyscallReturn
140213569Sbrandon.potter@amd.comrecvmsgFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
140313569Sbrandon.potter@amd.com{
140413569Sbrandon.potter@amd.com    int index = 0;
140513569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
140613569Sbrandon.potter@amd.com    Addr msgPtr = p->getSyscallArg(tc, index);
140713569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
140813569Sbrandon.potter@amd.com
140913569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
141013569Sbrandon.potter@amd.com    if (!sfdp)
141113569Sbrandon.potter@amd.com        return -EBADF;
141213569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
141313569Sbrandon.potter@amd.com
141413569Sbrandon.potter@amd.com     /**
141513569Sbrandon.potter@amd.com      *  struct msghdr {
141613569Sbrandon.potter@amd.com      *     void         *msg_name;       // optional address
141713569Sbrandon.potter@amd.com      *    socklen_t     msg_namelen;    // size of address
141813569Sbrandon.potter@amd.com      *    struct iovec *msg_iov;        // iovec array
141913569Sbrandon.potter@amd.com      *    size_t        msg_iovlen;     // number entries in msg_iov
142013569Sbrandon.potter@amd.com      *    i                             // entries correspond to buffer
142113569Sbrandon.potter@amd.com      *    void         *msg_control;    // ancillary data
142213569Sbrandon.potter@amd.com      *    size_t        msg_controllen; // ancillary data buffer len
142313569Sbrandon.potter@amd.com      *    int           msg_flags;      // flags on received message
142413569Sbrandon.potter@amd.com      *  };
142513569Sbrandon.potter@amd.com      *
142613569Sbrandon.potter@amd.com      *  struct iovec {
142713569Sbrandon.potter@amd.com      *    void  *iov_base;              // starting address
142813569Sbrandon.potter@amd.com      *    size_t iov_len;               // number of bytes to transfer
142913569Sbrandon.potter@amd.com      *  };
143013569Sbrandon.potter@amd.com      */
143113569Sbrandon.potter@amd.com
143213569Sbrandon.potter@amd.com    /**
143313569Sbrandon.potter@amd.com     * The plan with this system call is to replace all of the pointers in the
143413569Sbrandon.potter@amd.com     * structure and the substructure with BufferArg class pointers. We will
143513569Sbrandon.potter@amd.com     * copy every field from the structures into our BufferArg classes.
143613569Sbrandon.potter@amd.com     */
143713569Sbrandon.potter@amd.com    BufferArg msgBuf(msgPtr, sizeof(struct msghdr));
143813569Sbrandon.potter@amd.com    msgBuf.copyIn(tc->getMemProxy());
143913569Sbrandon.potter@amd.com    struct msghdr *msgHdr = (struct msghdr *)msgBuf.bufferPtr();
144013569Sbrandon.potter@amd.com
144113569Sbrandon.potter@amd.com    /**
144213569Sbrandon.potter@amd.com     * We will use these address place holders to retain the pointers which
144313569Sbrandon.potter@amd.com     * we are going to replace with our own buffers in our simulator address
144413569Sbrandon.potter@amd.com     * space.
144513569Sbrandon.potter@amd.com     */
144613569Sbrandon.potter@amd.com    Addr msg_name_phold = 0;
144713569Sbrandon.potter@amd.com    Addr msg_iov_phold = 0;
144813569Sbrandon.potter@amd.com    Addr iovec_base_phold[msgHdr->msg_iovlen];
144913569Sbrandon.potter@amd.com    Addr msg_control_phold = 0;
145013569Sbrandon.potter@amd.com
145113569Sbrandon.potter@amd.com    /**
145213569Sbrandon.potter@amd.com     * Record msg_name pointer then replace with buffer pointer.
145313569Sbrandon.potter@amd.com     */
145413569Sbrandon.potter@amd.com    BufferArg *nameBuf = NULL;
145513569Sbrandon.potter@amd.com    if (msgHdr->msg_name) {
145613569Sbrandon.potter@amd.com        /*1*/msg_name_phold = (Addr)msgHdr->msg_name;
145713569Sbrandon.potter@amd.com        /*2*/nameBuf = new BufferArg(msg_name_phold, msgHdr->msg_namelen);
145813569Sbrandon.potter@amd.com        /*3*/nameBuf->copyIn(tc->getMemProxy());
145913569Sbrandon.potter@amd.com        /*4*/msgHdr->msg_name = nameBuf->bufferPtr();
146013569Sbrandon.potter@amd.com    }
146113569Sbrandon.potter@amd.com
146213569Sbrandon.potter@amd.com    /**
146313569Sbrandon.potter@amd.com     * Record msg_iov pointer then replace with buffer pointer. Also, setup
146413569Sbrandon.potter@amd.com     * an array of buffer pointers for the iovec structs record and replace
146513569Sbrandon.potter@amd.com     * their pointers with buffer pointers.
146613569Sbrandon.potter@amd.com     */
146713569Sbrandon.potter@amd.com    BufferArg *iovBuf = NULL;
146813569Sbrandon.potter@amd.com    BufferArg *iovecBuf[msgHdr->msg_iovlen];
146913569Sbrandon.potter@amd.com    for (int i = 0; i < msgHdr->msg_iovlen; i++) {
147013569Sbrandon.potter@amd.com        iovec_base_phold[i] = 0;
147113569Sbrandon.potter@amd.com        iovecBuf[i] = NULL;
147213569Sbrandon.potter@amd.com    }
147313569Sbrandon.potter@amd.com
147413569Sbrandon.potter@amd.com    if (msgHdr->msg_iov) {
147513569Sbrandon.potter@amd.com        /*1*/msg_iov_phold = (Addr)msgHdr->msg_iov;
147613569Sbrandon.potter@amd.com        /*2*/iovBuf = new BufferArg(msg_iov_phold, msgHdr->msg_iovlen *
147713569Sbrandon.potter@amd.com                                    sizeof(struct iovec));
147813569Sbrandon.potter@amd.com        /*3*/iovBuf->copyIn(tc->getMemProxy());
147913569Sbrandon.potter@amd.com        for (int i = 0; i < msgHdr->msg_iovlen; i++) {
148013569Sbrandon.potter@amd.com            if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) {
148113569Sbrandon.potter@amd.com                /*1*/iovec_base_phold[i] =
148213569Sbrandon.potter@amd.com                     (Addr)((struct iovec *)iovBuf->bufferPtr())[i].iov_base;
148313569Sbrandon.potter@amd.com                /*2*/iovecBuf[i] = new BufferArg(iovec_base_phold[i],
148413569Sbrandon.potter@amd.com                     ((struct iovec *)iovBuf->bufferPtr())[i].iov_len);
148513569Sbrandon.potter@amd.com                /*3*/iovecBuf[i]->copyIn(tc->getMemProxy());
148613569Sbrandon.potter@amd.com                /*4*/((struct iovec *)iovBuf->bufferPtr())[i].iov_base =
148713569Sbrandon.potter@amd.com                     iovecBuf[i]->bufferPtr();
148813569Sbrandon.potter@amd.com            }
148913569Sbrandon.potter@amd.com        }
149013569Sbrandon.potter@amd.com        /*4*/msgHdr->msg_iov = (struct iovec *)iovBuf->bufferPtr();
149113569Sbrandon.potter@amd.com    }
149213569Sbrandon.potter@amd.com
149313569Sbrandon.potter@amd.com    /**
149413569Sbrandon.potter@amd.com     * Record msg_control pointer then replace with buffer pointer.
149513569Sbrandon.potter@amd.com     */
149613569Sbrandon.potter@amd.com    BufferArg *controlBuf = NULL;
149713569Sbrandon.potter@amd.com    if (msgHdr->msg_control) {
149813569Sbrandon.potter@amd.com        /*1*/msg_control_phold = (Addr)msgHdr->msg_control;
149913569Sbrandon.potter@amd.com        /*2*/controlBuf = new BufferArg(msg_control_phold,
150013569Sbrandon.potter@amd.com                                        CMSG_ALIGN(msgHdr->msg_controllen));
150113569Sbrandon.potter@amd.com        /*3*/controlBuf->copyIn(tc->getMemProxy());
150213569Sbrandon.potter@amd.com        /*4*/msgHdr->msg_control = controlBuf->bufferPtr();
150313569Sbrandon.potter@amd.com    }
150413569Sbrandon.potter@amd.com
150513569Sbrandon.potter@amd.com    ssize_t recvd_size = recvmsg(sim_fd, msgHdr, flags);
150613569Sbrandon.potter@amd.com
150713569Sbrandon.potter@amd.com    if (recvd_size < 0)
150813569Sbrandon.potter@amd.com        return -errno;
150913569Sbrandon.potter@amd.com
151013569Sbrandon.potter@amd.com    if (msgHdr->msg_name) {
151113569Sbrandon.potter@amd.com        nameBuf->copyOut(tc->getMemProxy());
151213569Sbrandon.potter@amd.com        delete(nameBuf);
151313569Sbrandon.potter@amd.com        msgHdr->msg_name = (void *)msg_name_phold;
151413569Sbrandon.potter@amd.com    }
151513569Sbrandon.potter@amd.com
151613569Sbrandon.potter@amd.com    if (msgHdr->msg_iov) {
151713569Sbrandon.potter@amd.com        for (int i = 0; i< msgHdr->msg_iovlen; i++) {
151813569Sbrandon.potter@amd.com            if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) {
151913569Sbrandon.potter@amd.com                iovecBuf[i]->copyOut(tc->getMemProxy());
152013569Sbrandon.potter@amd.com                delete iovecBuf[i];
152113569Sbrandon.potter@amd.com                ((struct iovec *)iovBuf->bufferPtr())[i].iov_base =
152213569Sbrandon.potter@amd.com                (void *)iovec_base_phold[i];
152313569Sbrandon.potter@amd.com            }
152413569Sbrandon.potter@amd.com        }
152513569Sbrandon.potter@amd.com        iovBuf->copyOut(tc->getMemProxy());
152613569Sbrandon.potter@amd.com        delete iovBuf;
152713569Sbrandon.potter@amd.com        msgHdr->msg_iov = (struct iovec *)msg_iov_phold;
152813569Sbrandon.potter@amd.com    }
152913569Sbrandon.potter@amd.com
153013569Sbrandon.potter@amd.com    if (msgHdr->msg_control) {
153113569Sbrandon.potter@amd.com        controlBuf->copyOut(tc->getMemProxy());
153213569Sbrandon.potter@amd.com        delete(controlBuf);
153313569Sbrandon.potter@amd.com        msgHdr->msg_control = (void *)msg_control_phold;
153413569Sbrandon.potter@amd.com    }
153513569Sbrandon.potter@amd.com
153613569Sbrandon.potter@amd.com    msgBuf.copyOut(tc->getMemProxy());
153713569Sbrandon.potter@amd.com
153813569Sbrandon.potter@amd.com    return recvd_size;
153913569Sbrandon.potter@amd.com}
154013569Sbrandon.potter@amd.com
154113569Sbrandon.potter@amd.comSyscallReturn
154213569Sbrandon.potter@amd.comsendmsgFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
154313569Sbrandon.potter@amd.com{
154413569Sbrandon.potter@amd.com    int index = 0;
154513569Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
154613569Sbrandon.potter@amd.com    Addr msgPtr = p->getSyscallArg(tc, index);
154713569Sbrandon.potter@amd.com    int flags = p->getSyscallArg(tc, index);
154813569Sbrandon.potter@amd.com
154913569Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
155013569Sbrandon.potter@amd.com    if (!sfdp)
155113569Sbrandon.potter@amd.com        return -EBADF;
155213569Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
155313569Sbrandon.potter@amd.com
155413569Sbrandon.potter@amd.com    /**
155513569Sbrandon.potter@amd.com     * Reserve buffer space.
155613569Sbrandon.potter@amd.com     */
155713569Sbrandon.potter@amd.com    BufferArg msgBuf(msgPtr, sizeof(struct msghdr));
155813569Sbrandon.potter@amd.com    msgBuf.copyIn(tc->getMemProxy());
155913569Sbrandon.potter@amd.com    struct msghdr msgHdr = *((struct msghdr *)msgBuf.bufferPtr());
156013569Sbrandon.potter@amd.com
156113569Sbrandon.potter@amd.com    /**
156213569Sbrandon.potter@amd.com     * Assuming msgHdr.msg_iovlen >= 1, then there is no point calling
156313569Sbrandon.potter@amd.com     * recvmsg without a buffer.
156413569Sbrandon.potter@amd.com     */
156513569Sbrandon.potter@amd.com    struct iovec *iovPtr = msgHdr.msg_iov;
156613569Sbrandon.potter@amd.com    BufferArg iovBuf((Addr)iovPtr, sizeof(struct iovec) * msgHdr.msg_iovlen);
156713569Sbrandon.potter@amd.com    iovBuf.copyIn(tc->getMemProxy());
156813569Sbrandon.potter@amd.com    struct iovec *iov = (struct iovec *)iovBuf.bufferPtr();
156913569Sbrandon.potter@amd.com    msgHdr.msg_iov = iov;
157013569Sbrandon.potter@amd.com
157113569Sbrandon.potter@amd.com    /**
157213569Sbrandon.potter@amd.com     * Cannot instantiate buffers till inside the loop.
157313569Sbrandon.potter@amd.com     * Create array to hold buffer addresses, to be used during copyIn of
157413569Sbrandon.potter@amd.com     * send data.
157513569Sbrandon.potter@amd.com     */
157613569Sbrandon.potter@amd.com    BufferArg **bufferArray = (BufferArg **)malloc(msgHdr.msg_iovlen
157713569Sbrandon.potter@amd.com                                                   * sizeof(BufferArg *));
157813569Sbrandon.potter@amd.com
157913569Sbrandon.potter@amd.com    /**
158013569Sbrandon.potter@amd.com     * Iterate through the iovec structures:
158113569Sbrandon.potter@amd.com     * Get the base buffer addreses, reserve iov_len amount of space for each.
158213569Sbrandon.potter@amd.com     * Put the buf address into the bufferArray for later retrieval.
158313569Sbrandon.potter@amd.com     */
158413569Sbrandon.potter@amd.com    for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) {
158513569Sbrandon.potter@amd.com        Addr basePtr = (Addr) iov[iovIndex].iov_base;
158613569Sbrandon.potter@amd.com        bufferArray[iovIndex] = new BufferArg(basePtr, iov[iovIndex].iov_len);
158713569Sbrandon.potter@amd.com        bufferArray[iovIndex]->copyIn(tc->getMemProxy());
158813569Sbrandon.potter@amd.com        iov[iovIndex].iov_base = bufferArray[iovIndex]->bufferPtr();
158913569Sbrandon.potter@amd.com    }
159013569Sbrandon.potter@amd.com
159113569Sbrandon.potter@amd.com    ssize_t sent_size = sendmsg(sim_fd, &msgHdr, flags);
159213569Sbrandon.potter@amd.com    int local_errno = errno;
159313569Sbrandon.potter@amd.com
159413569Sbrandon.potter@amd.com    /**
159513569Sbrandon.potter@amd.com     * Free dynamically allocated memory.
159613569Sbrandon.potter@amd.com     */
159713569Sbrandon.potter@amd.com    for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) {
159813569Sbrandon.potter@amd.com        BufferArg *baseBuf = ( BufferArg *)bufferArray[iovIndex];
159913569Sbrandon.potter@amd.com        delete(baseBuf);
160013569Sbrandon.potter@amd.com    }
160113569Sbrandon.potter@amd.com
160213569Sbrandon.potter@amd.com    /**
160313569Sbrandon.potter@amd.com     * Malloced above.
160413569Sbrandon.potter@amd.com     */
160513569Sbrandon.potter@amd.com    free(bufferArray);
160613569Sbrandon.potter@amd.com
160713569Sbrandon.potter@amd.com    return (sent_size < 0) ? -local_errno : sent_size;
160813569Sbrandon.potter@amd.com}
160913569Sbrandon.potter@amd.com
161013571Sbrandon.potter@amd.comSyscallReturn
161113571Sbrandon.potter@amd.comgetsockoptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
161213571Sbrandon.potter@amd.com{
161313571Sbrandon.potter@amd.com    // union of all possible return value types from getsockopt
161413571Sbrandon.potter@amd.com    union val {
161513571Sbrandon.potter@amd.com        int i_val;
161613571Sbrandon.potter@amd.com        long l_val;
161713571Sbrandon.potter@amd.com        struct linger linger_val;
161813571Sbrandon.potter@amd.com        struct timeval timeval_val;
161913571Sbrandon.potter@amd.com    } val;
162013571Sbrandon.potter@amd.com
162113571Sbrandon.potter@amd.com    int index = 0;
162213571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
162313571Sbrandon.potter@amd.com    int level = p->getSyscallArg(tc, index);
162413571Sbrandon.potter@amd.com    int optname = p->getSyscallArg(tc, index);
162513571Sbrandon.potter@amd.com    Addr valPtr = p->getSyscallArg(tc, index);
162613571Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
162713571Sbrandon.potter@amd.com
162813571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
162913571Sbrandon.potter@amd.com    if (!sfdp)
163013571Sbrandon.potter@amd.com        return -EBADF;
163113571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
163213571Sbrandon.potter@amd.com
163313571Sbrandon.potter@amd.com    socklen_t len = sizeof(val);
163413571Sbrandon.potter@amd.com    int status = getsockopt(sim_fd, level, optname, &val, &len);
163513571Sbrandon.potter@amd.com
163613571Sbrandon.potter@amd.com    if (status == -1)
163713571Sbrandon.potter@amd.com        return -errno;
163813571Sbrandon.potter@amd.com
163913571Sbrandon.potter@amd.com    // copy val to valPtr and pass it on
164013571Sbrandon.potter@amd.com    BufferArg valBuf(valPtr, sizeof(val));
164113571Sbrandon.potter@amd.com    memcpy(valBuf.bufferPtr(), &val, sizeof(val));
164213571Sbrandon.potter@amd.com    valBuf.copyOut(tc->getMemProxy());
164313571Sbrandon.potter@amd.com
164413571Sbrandon.potter@amd.com    // copy len to lenPtr and pass  it on
164513571Sbrandon.potter@amd.com    BufferArg lenBuf(lenPtr, sizeof(len));
164613571Sbrandon.potter@amd.com    memcpy(lenBuf.bufferPtr(), &len, sizeof(len));
164713571Sbrandon.potter@amd.com    lenBuf.copyOut(tc->getMemProxy());
164813571Sbrandon.potter@amd.com
164913571Sbrandon.potter@amd.com    return status;
165013571Sbrandon.potter@amd.com}
165113571Sbrandon.potter@amd.com
165213571Sbrandon.potter@amd.comSyscallReturn
165313571Sbrandon.potter@amd.comgetsocknameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
165413571Sbrandon.potter@amd.com{
165513571Sbrandon.potter@amd.com    int index = 0;
165613571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
165713571Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
165813571Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
165913571Sbrandon.potter@amd.com
166013571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
166113571Sbrandon.potter@amd.com    if (!sfdp)
166213571Sbrandon.potter@amd.com        return -EBADF;
166313571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
166413571Sbrandon.potter@amd.com
166513571Sbrandon.potter@amd.com    // lenPtr is an in-out paramenter:
166613571Sbrandon.potter@amd.com    // sending the address length in, conveying the final length out
166713571Sbrandon.potter@amd.com
166813571Sbrandon.potter@amd.com    // Read in the value of len from the passed pointer.
166913571Sbrandon.potter@amd.com    BufferArg lenBuf(lenPtr, sizeof(socklen_t));
167013571Sbrandon.potter@amd.com    lenBuf.copyIn(tc->getMemProxy());
167113571Sbrandon.potter@amd.com    socklen_t len = *(socklen_t *)lenBuf.bufferPtr();
167213571Sbrandon.potter@amd.com
167313571Sbrandon.potter@amd.com    struct sockaddr sa;
167413571Sbrandon.potter@amd.com    int status = getsockname(sim_fd, &sa, &len);
167513571Sbrandon.potter@amd.com
167613571Sbrandon.potter@amd.com    if (status == -1)
167713571Sbrandon.potter@amd.com        return -errno;
167813571Sbrandon.potter@amd.com
167913571Sbrandon.potter@amd.com    // Copy address to addrPtr and pass it on.
168013571Sbrandon.potter@amd.com    BufferArg addrBuf(addrPtr, sizeof(sa));
168113571Sbrandon.potter@amd.com    memcpy(addrBuf.bufferPtr(), &sa, sizeof(sa));
168213571Sbrandon.potter@amd.com    addrBuf.copyOut(tc->getMemProxy());
168313571Sbrandon.potter@amd.com
168413571Sbrandon.potter@amd.com    // Copy len to lenPtr and pass  it on.
168513571Sbrandon.potter@amd.com    *(socklen_t *)lenBuf.bufferPtr() = len;
168613571Sbrandon.potter@amd.com    lenBuf.copyOut(tc->getMemProxy());
168713571Sbrandon.potter@amd.com
168813571Sbrandon.potter@amd.com    return status;
168913571Sbrandon.potter@amd.com}
169013571Sbrandon.potter@amd.com
169113571Sbrandon.potter@amd.comSyscallReturn
169213571Sbrandon.potter@amd.comgetpeernameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
169313571Sbrandon.potter@amd.com{
169413571Sbrandon.potter@amd.com    int index = 0;
169513571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
169613571Sbrandon.potter@amd.com    Addr sockAddrPtr = p->getSyscallArg(tc, index);
169713571Sbrandon.potter@amd.com    Addr addrlenPtr = p->getSyscallArg(tc, index);
169813571Sbrandon.potter@amd.com
169913571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
170013571Sbrandon.potter@amd.com    if (!sfdp)
170113571Sbrandon.potter@amd.com        return -EBADF;
170213571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
170313571Sbrandon.potter@amd.com
170413571Sbrandon.potter@amd.com    BufferArg bufAddrlen(addrlenPtr, sizeof(unsigned));
170513571Sbrandon.potter@amd.com    bufAddrlen.copyIn(tc->getMemProxy());
170613571Sbrandon.potter@amd.com    BufferArg bufSock(sockAddrPtr, *(unsigned *)bufAddrlen.bufferPtr());
170713571Sbrandon.potter@amd.com
170813571Sbrandon.potter@amd.com    int retval = getpeername(sim_fd,
170913571Sbrandon.potter@amd.com                             (struct sockaddr *)bufSock.bufferPtr(),
171013571Sbrandon.potter@amd.com                             (unsigned *)bufAddrlen.bufferPtr());
171113571Sbrandon.potter@amd.com
171213571Sbrandon.potter@amd.com    if (retval != -1) {
171313571Sbrandon.potter@amd.com        bufSock.copyOut(tc->getMemProxy());
171413571Sbrandon.potter@amd.com        bufAddrlen.copyOut(tc->getMemProxy());
171513571Sbrandon.potter@amd.com    }
171613571Sbrandon.potter@amd.com
171713571Sbrandon.potter@amd.com    return (retval == -1) ? -errno : retval;
171813571Sbrandon.potter@amd.com}
171913571Sbrandon.potter@amd.com
172013571Sbrandon.potter@amd.comSyscallReturn
172113571Sbrandon.potter@amd.comsetsockoptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
172213571Sbrandon.potter@amd.com{
172313571Sbrandon.potter@amd.com    int index = 0;
172413571Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
172513571Sbrandon.potter@amd.com    int level = p->getSyscallArg(tc, index);
172613571Sbrandon.potter@amd.com    int optname = p->getSyscallArg(tc, index);
172713571Sbrandon.potter@amd.com    Addr valPtr = p->getSyscallArg(tc, index);
172813571Sbrandon.potter@amd.com    socklen_t len = p->getSyscallArg(tc, index);
172913571Sbrandon.potter@amd.com
173013571Sbrandon.potter@amd.com    BufferArg valBuf(valPtr, len);
173113571Sbrandon.potter@amd.com    valBuf.copyIn(tc->getMemProxy());
173213571Sbrandon.potter@amd.com
173313571Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
173413571Sbrandon.potter@amd.com    if (!sfdp)
173513571Sbrandon.potter@amd.com        return -EBADF;
173613571Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
173713571Sbrandon.potter@amd.com
173813571Sbrandon.potter@amd.com    int status = setsockopt(sim_fd, level, optname,
173913571Sbrandon.potter@amd.com                            (struct sockaddr *)valBuf.bufferPtr(), len);
174013571Sbrandon.potter@amd.com
174113571Sbrandon.potter@amd.com    return (status == -1) ? -errno : status;
174213571Sbrandon.potter@amd.com}
174313571Sbrandon.potter@amd.com
1744