syscall_emul.cc revision 14124
1360SN/A/* 21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 3360SN/A * All rights reserved. 4360SN/A * 5360SN/A * Redistribution and use in source and binary forms, with or without 6360SN/A * modification, are permitted provided that the following conditions are 7360SN/A * met: redistributions of source code must retain the above copyright 8360SN/A * notice, this list of conditions and the following disclaimer; 9360SN/A * redistributions in binary form must reproduce the above copyright 10360SN/A * notice, this list of conditions and the following disclaimer in the 11360SN/A * documentation and/or other materials provided with the distribution; 12360SN/A * neither the name of the copyright holders nor the names of its 13360SN/A * contributors may be used to endorse or promote products derived from 14360SN/A * this software without specific prior written permission. 15360SN/A * 16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292665Ssaidi@eecs.umich.edu * Ali Saidi 30360SN/A */ 31360SN/A 3211793Sbrandon.potter@amd.com#include "sim/syscall_emul.hh" 3311793Sbrandon.potter@amd.com 342093SN/A#include <fcntl.h> 3513479Santhony.gutierrez@amd.com#include <sys/syscall.h> 36360SN/A#include <unistd.h> 37360SN/A 3811911SBrandon.Potter@amd.com#include <csignal> 396712Snate@binkert.org#include <iostream> 4013031Sbrandon.potter@amd.com#include <mutex> 41360SN/A#include <string> 42360SN/A 437680Sgblack@eecs.umich.edu#include "arch/utility.hh" 442474SN/A#include "base/chunk_generator.hh" 45360SN/A#include "base/trace.hh" 466658Snate@binkert.org#include "config/the_isa.hh" 472680Sktlim@umich.edu#include "cpu/thread_context.hh" 4812716Smichael.lebeane@amd.com#include "dev/net/dist_iface.hh" 492474SN/A#include "mem/page_table.hh" 5013031Sbrandon.potter@amd.com#include "sim/byteswap.hh" 51360SN/A#include "sim/process.hh" 528229Snate@binkert.org#include "sim/sim_exit.hh" 5311794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh" 5411794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh" 556029Ssteve.reinhardt@amd.com#include "sim/system.hh" 56360SN/A 57360SN/Ausing namespace std; 582107SN/Ausing namespace TheISA; 59360SN/A 6013933Sbrandon.potter@amd.comvoid 6113933Sbrandon.potter@amd.comwarnUnsupportedOS(std::string syscall_name) 6213933Sbrandon.potter@amd.com{ 6313933Sbrandon.potter@amd.com warn("Cannot invoke %s on host operating system.", syscall_name); 6413933Sbrandon.potter@amd.com} 6513933Sbrandon.potter@amd.com 661450SN/ASyscallReturn 6713995Sbrandon.potter@amd.comunimplementedFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 68360SN/A{ 6911794Sbrandon.potter@amd.com fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum); 702484SN/A 712484SN/A return 1; 72360SN/A} 73360SN/A 74360SN/A 751450SN/ASyscallReturn 7613995Sbrandon.potter@amd.comignoreFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 77360SN/A{ 7811794Sbrandon.potter@amd.com if (desc->needWarning()) { 7911794Sbrandon.potter@amd.com warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ? 8011794Sbrandon.potter@amd.com "\n (further warnings will be suppressed)" : ""); 8110831Ssteve.reinhardt@amd.com } 82360SN/A 838149SChris.Emmons@ARM.com return 0; 848149SChris.Emmons@ARM.com} 858149SChris.Emmons@ARM.com 8611886Sbrandon.potter@amd.comstatic void 8711911SBrandon.Potter@amd.comexitFutexWake(ThreadContext *tc, Addr addr, uint64_t tgid) 8811886Sbrandon.potter@amd.com{ 8911911SBrandon.Potter@amd.com // Clear value at address pointed to by thread's childClearTID field. 9011911SBrandon.Potter@amd.com BufferArg ctidBuf(addr, sizeof(long)); 9111911SBrandon.Potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 9211911SBrandon.Potter@amd.com *ctid = 0; 9314024Sgabeblack@google.com ctidBuf.copyOut(tc->getVirtProxy()); 9411886Sbrandon.potter@amd.com 9511911SBrandon.Potter@amd.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 9611911SBrandon.Potter@amd.com // Wake one of the waiting threads. 9711911SBrandon.Potter@amd.com futex_map.wakeup(addr, tgid, 1); 9811911SBrandon.Potter@amd.com} 9911911SBrandon.Potter@amd.com 10011911SBrandon.Potter@amd.comstatic SyscallReturn 10113995Sbrandon.potter@amd.comexitImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool group) 10211911SBrandon.Potter@amd.com{ 10311911SBrandon.Potter@amd.com int index = 0; 10413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 10511911SBrandon.Potter@amd.com int status = p->getSyscallArg(tc, index); 10611911SBrandon.Potter@amd.com 10711911SBrandon.Potter@amd.com System *sys = tc->getSystemPtr(); 10811911SBrandon.Potter@amd.com 10911911SBrandon.Potter@amd.com if (group) 11011911SBrandon.Potter@amd.com *p->exitGroup = true; 11111911SBrandon.Potter@amd.com 11211911SBrandon.Potter@amd.com if (p->childClearTID) 11311911SBrandon.Potter@amd.com exitFutexWake(tc, p->childClearTID, p->tgid()); 11411911SBrandon.Potter@amd.com 11511911SBrandon.Potter@amd.com bool last_thread = true; 11611911SBrandon.Potter@amd.com Process *parent = nullptr, *tg_lead = nullptr; 11711911SBrandon.Potter@amd.com for (int i = 0; last_thread && i < sys->numContexts(); i++) { 11811911SBrandon.Potter@amd.com Process *walk; 11911911SBrandon.Potter@amd.com if (!(walk = sys->threadContexts[i]->getProcessPtr())) 12011911SBrandon.Potter@amd.com continue; 12111911SBrandon.Potter@amd.com 12211911SBrandon.Potter@amd.com /** 12311911SBrandon.Potter@amd.com * Threads in a thread group require special handing. For instance, 12411911SBrandon.Potter@amd.com * we send the SIGCHLD signal so that it appears that it came from 12511911SBrandon.Potter@amd.com * the head of the group. We also only delete file descriptors if 12611911SBrandon.Potter@amd.com * we are the last thread in the thread group. 12711911SBrandon.Potter@amd.com */ 12811911SBrandon.Potter@amd.com if (walk->pid() == p->tgid()) 12911911SBrandon.Potter@amd.com tg_lead = walk; 13011911SBrandon.Potter@amd.com 13113644Sqtt2@cornell.edu if ((sys->threadContexts[i]->status() != ThreadContext::Halted) && 13213644Sqtt2@cornell.edu (sys->threadContexts[i]->status() != ThreadContext::Halting) && 13313644Sqtt2@cornell.edu (walk != p)) { 13411911SBrandon.Potter@amd.com /** 13511911SBrandon.Potter@amd.com * Check if we share thread group with the pointer; this denotes 13611911SBrandon.Potter@amd.com * that we are not the last thread active in the thread group. 13711911SBrandon.Potter@amd.com * Note that setting this to false also prevents further 13811911SBrandon.Potter@amd.com * iterations of the loop. 13911911SBrandon.Potter@amd.com */ 14013644Sqtt2@cornell.edu if (walk->tgid() == p->tgid()) { 14113644Sqtt2@cornell.edu /** 14213644Sqtt2@cornell.edu * If p is trying to exit_group and both walk and p are in 14313644Sqtt2@cornell.edu * the same thread group (i.e., sharing the same tgid), 14413644Sqtt2@cornell.edu * we need to halt walk's thread context. After all threads 14513644Sqtt2@cornell.edu * except p are halted, p becomes the last thread in the 14613644Sqtt2@cornell.edu * group. 14713644Sqtt2@cornell.edu * 14813644Sqtt2@cornell.edu * If p is not doing exit_group and there exists another 14913644Sqtt2@cornell.edu * active thread context in the group, last_thread is 15013644Sqtt2@cornell.edu * set to false to prevent the parent thread from killing 15113644Sqtt2@cornell.edu * all threads in the group. 15213644Sqtt2@cornell.edu */ 15313644Sqtt2@cornell.edu if (*(p->exitGroup)) { 15413644Sqtt2@cornell.edu sys->threadContexts[i]->halt(); 15513644Sqtt2@cornell.edu } else { 15613644Sqtt2@cornell.edu last_thread = false; 15713644Sqtt2@cornell.edu } 15813644Sqtt2@cornell.edu } 15911911SBrandon.Potter@amd.com 16011911SBrandon.Potter@amd.com /** 16111911SBrandon.Potter@amd.com * A corner case exists which involves execve(). After execve(), 16211911SBrandon.Potter@amd.com * the execve will enable SIGCHLD in the process. The problem 16311911SBrandon.Potter@amd.com * occurs when the exiting process is the root process in the 16411911SBrandon.Potter@amd.com * system; there is no parent to receive the signal. We obviate 16511911SBrandon.Potter@amd.com * this problem by setting the root process' ppid to zero in the 16611911SBrandon.Potter@amd.com * Python configuration files. We really should handle the 16711911SBrandon.Potter@amd.com * root/execve specific case more gracefully. 16811911SBrandon.Potter@amd.com */ 16911911SBrandon.Potter@amd.com if (*p->sigchld && (p->ppid() != 0) && (walk->pid() == p->ppid())) 17011911SBrandon.Potter@amd.com parent = walk; 17111886Sbrandon.potter@amd.com } 17211886Sbrandon.potter@amd.com } 17311911SBrandon.Potter@amd.com 17411911SBrandon.Potter@amd.com if (last_thread) { 17511911SBrandon.Potter@amd.com if (parent) { 17611911SBrandon.Potter@amd.com assert(tg_lead); 17711911SBrandon.Potter@amd.com sys->signalList.push_back(BasicSignal(tg_lead, parent, SIGCHLD)); 17811911SBrandon.Potter@amd.com } 17911911SBrandon.Potter@amd.com 18011911SBrandon.Potter@amd.com /** 18111911SBrandon.Potter@amd.com * Run though FD array of the exiting process and close all file 18211911SBrandon.Potter@amd.com * descriptors except for the standard file descriptors. 18311911SBrandon.Potter@amd.com * (The standard file descriptors are shared with gem5.) 18411911SBrandon.Potter@amd.com */ 18511911SBrandon.Potter@amd.com for (int i = 0; i < p->fds->getSize(); i++) { 18611911SBrandon.Potter@amd.com if ((*p->fds)[i]) 18711911SBrandon.Potter@amd.com p->fds->closeFDEntry(i); 18811911SBrandon.Potter@amd.com } 18911911SBrandon.Potter@amd.com } 19011911SBrandon.Potter@amd.com 19111911SBrandon.Potter@amd.com tc->halt(); 19213644Sqtt2@cornell.edu 19313644Sqtt2@cornell.edu /** 19413644Sqtt2@cornell.edu * check to see if there is no more active thread in the system. If so, 19513644Sqtt2@cornell.edu * exit the simulation loop 19613644Sqtt2@cornell.edu */ 19713644Sqtt2@cornell.edu int activeContexts = 0; 19813644Sqtt2@cornell.edu for (auto &system: sys->systemList) 19913644Sqtt2@cornell.edu activeContexts += system->numRunningContexts(); 20013644Sqtt2@cornell.edu 20113644Sqtt2@cornell.edu if (activeContexts == 0) { 20213644Sqtt2@cornell.edu /** 20313644Sqtt2@cornell.edu * Even though we are terminating the final thread context, dist-gem5 20413644Sqtt2@cornell.edu * requires the simulation to remain active and provide 20513644Sqtt2@cornell.edu * synchronization messages to the switch process. So we just halt 20613644Sqtt2@cornell.edu * the last thread context and return. The simulation will be 20713644Sqtt2@cornell.edu * terminated by dist-gem5 in a coordinated manner once all nodes 20813644Sqtt2@cornell.edu * have signaled their readiness to exit. For non dist-gem5 20913644Sqtt2@cornell.edu * simulations, readyToExit() always returns true. 21013644Sqtt2@cornell.edu */ 21113644Sqtt2@cornell.edu if (!DistIface::readyToExit(0)) { 21213644Sqtt2@cornell.edu return status; 21313644Sqtt2@cornell.edu } 21413644Sqtt2@cornell.edu 21513644Sqtt2@cornell.edu exitSimLoop("exiting with last active thread context", status & 0xff); 21613644Sqtt2@cornell.edu return status; 21713644Sqtt2@cornell.edu } 21813644Sqtt2@cornell.edu 21911911SBrandon.Potter@amd.com return status; 22011886Sbrandon.potter@amd.com} 2218149SChris.Emmons@ARM.com 2228149SChris.Emmons@ARM.comSyscallReturn 22313995Sbrandon.potter@amd.comexitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 224360SN/A{ 22513995Sbrandon.potter@amd.com return exitImpl(desc, callnum, tc, false); 226360SN/A} 227360SN/A 2281450SN/ASyscallReturn 22913995Sbrandon.potter@amd.comexitGroupFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2306109Ssanchezd@stanford.edu{ 23113995Sbrandon.potter@amd.com return exitImpl(desc, callnum, tc, true); 2326109Ssanchezd@stanford.edu} 2336109Ssanchezd@stanford.edu 2346109Ssanchezd@stanford.eduSyscallReturn 23513995Sbrandon.potter@amd.comgetpagesizeFunc(SyscallDesc *desc, int num, ThreadContext *tc) 236360SN/A{ 23710318Sandreas.hansson@arm.com return (int)PageBytes; 238360SN/A} 239360SN/A 240360SN/A 2411450SN/ASyscallReturn 24213995Sbrandon.potter@amd.combrkFunc(SyscallDesc *desc, int num, ThreadContext *tc) 243360SN/A{ 244360SN/A // change brk addr to first arg 2456701Sgblack@eecs.umich.edu int index = 0; 24613995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 2476701Sgblack@eecs.umich.edu Addr new_brk = p->getSyscallArg(tc, index); 2485748SSteve.Reinhardt@amd.com 24911905SBrandon.Potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 25011905SBrandon.Potter@amd.com Addr brk_point = mem_state->getBrkPoint(); 25111905SBrandon.Potter@amd.com 2525748SSteve.Reinhardt@amd.com // in Linux at least, brk(0) returns the current break value 2535748SSteve.Reinhardt@amd.com // (note that the syscall and the glibc function have different behavior) 2545748SSteve.Reinhardt@amd.com if (new_brk == 0) 25511905SBrandon.Potter@amd.com return brk_point; 2565748SSteve.Reinhardt@amd.com 25711905SBrandon.Potter@amd.com if (new_brk > brk_point) { 2585748SSteve.Reinhardt@amd.com // might need to allocate some new pages 25911905SBrandon.Potter@amd.com for (ChunkGenerator gen(brk_point, 26011905SBrandon.Potter@amd.com new_brk - brk_point, 26110318Sandreas.hansson@arm.com PageBytes); !gen.done(); gen.next()) { 2625748SSteve.Reinhardt@amd.com if (!p->pTable->translate(gen.addr())) 26310318Sandreas.hansson@arm.com p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes); 2646687Stjones1@inf.ed.ac.uk 2656687Stjones1@inf.ed.ac.uk // if the address is already there, zero it out 2666687Stjones1@inf.ed.ac.uk else { 26711905SBrandon.Potter@amd.com uint8_t zero = 0; 26814024Sgabeblack@google.com PortProxy &tp = tc->getVirtProxy(); 2696687Stjones1@inf.ed.ac.uk 2706687Stjones1@inf.ed.ac.uk // split non-page aligned accesses 27110318Sandreas.hansson@arm.com Addr next_page = roundUp(gen.addr(), PageBytes); 2726687Stjones1@inf.ed.ac.uk uint32_t size_needed = next_page - gen.addr(); 2738852Sandreas.hansson@arm.com tp.memsetBlob(gen.addr(), zero, size_needed); 27410318Sandreas.hansson@arm.com if (gen.addr() + PageBytes > next_page && 2756687Stjones1@inf.ed.ac.uk next_page < new_brk && 27611906SBrandon.Potter@amd.com p->pTable->translate(next_page)) { 27710318Sandreas.hansson@arm.com size_needed = PageBytes - size_needed; 2788852Sandreas.hansson@arm.com tp.memsetBlob(next_page, zero, size_needed); 2796687Stjones1@inf.ed.ac.uk } 2806687Stjones1@inf.ed.ac.uk } 2812474SN/A } 2821450SN/A } 2835748SSteve.Reinhardt@amd.com 28411905SBrandon.Potter@amd.com mem_state->setBrkPoint(new_brk); 28511380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n", 28611905SBrandon.Potter@amd.com mem_state->getBrkPoint()); 28711905SBrandon.Potter@amd.com return mem_state->getBrkPoint(); 288360SN/A} 289360SN/A 29011886Sbrandon.potter@amd.comSyscallReturn 29113995Sbrandon.potter@amd.comsetTidAddressFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 29211886Sbrandon.potter@amd.com{ 29311886Sbrandon.potter@amd.com int index = 0; 29413995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 29511886Sbrandon.potter@amd.com uint64_t tidPtr = process->getSyscallArg(tc, index); 29611886Sbrandon.potter@amd.com 29711886Sbrandon.potter@amd.com process->childClearTID = tidPtr; 29811886Sbrandon.potter@amd.com return process->pid(); 29911886Sbrandon.potter@amd.com} 300360SN/A 3011450SN/ASyscallReturn 30213995Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, ThreadContext *tc) 303360SN/A{ 3046701Sgblack@eecs.umich.edu int index = 0; 30513995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 30610931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 30710931Sbrandon.potter@amd.com 30811856Sbrandon.potter@amd.com return p->fds->closeFDEntry(tgt_fd); 309360SN/A} 310360SN/A 3111450SN/ASyscallReturn 31213995Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, ThreadContext *tc) 313360SN/A{ 3146701Sgblack@eecs.umich.edu int index = 0; 31513995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 31610931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 3176701Sgblack@eecs.umich.edu uint64_t offs = p->getSyscallArg(tc, index); 3186701Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, index); 319360SN/A 32011856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 32111856Sbrandon.potter@amd.com if (!ffdp) 32210931Sbrandon.potter@amd.com return -EBADF; 32311856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 32410931Sbrandon.potter@amd.com 32510931Sbrandon.potter@amd.com off_t result = lseek(sim_fd, offs, whence); 326360SN/A 3271458SN/A return (result == (off_t)-1) ? -errno : result; 328360SN/A} 329360SN/A 330360SN/A 3311450SN/ASyscallReturn 33213995Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc) 3334118Sgblack@eecs.umich.edu{ 3346701Sgblack@eecs.umich.edu int index = 0; 33513995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 33610931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 3376701Sgblack@eecs.umich.edu uint64_t offset_high = p->getSyscallArg(tc, index); 3386701Sgblack@eecs.umich.edu uint32_t offset_low = p->getSyscallArg(tc, index); 3396701Sgblack@eecs.umich.edu Addr result_ptr = p->getSyscallArg(tc, index); 3406701Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, index); 3414118Sgblack@eecs.umich.edu 34211856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 34311856Sbrandon.potter@amd.com if (!ffdp) 34410931Sbrandon.potter@amd.com return -EBADF; 34511856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 34610931Sbrandon.potter@amd.com 3474118Sgblack@eecs.umich.edu uint64_t offset = (offset_high << 32) | offset_low; 3484118Sgblack@eecs.umich.edu 34910931Sbrandon.potter@amd.com uint64_t result = lseek(sim_fd, offset, whence); 3504118Sgblack@eecs.umich.edu result = TheISA::htog(result); 3514118Sgblack@eecs.umich.edu 35211379Sbrandon.potter@amd.com if (result == (off_t)-1) 3534118Sgblack@eecs.umich.edu return -errno; 35411379Sbrandon.potter@amd.com // Assuming that the size of loff_t is 64 bits on the target platform 35511379Sbrandon.potter@amd.com BufferArg result_buf(result_ptr, sizeof(result)); 35611379Sbrandon.potter@amd.com memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 35714024Sgabeblack@google.com result_buf.copyOut(tc->getVirtProxy()); 35811379Sbrandon.potter@amd.com return 0; 3594118Sgblack@eecs.umich.edu} 3604118Sgblack@eecs.umich.edu 3614118Sgblack@eecs.umich.edu 3624118Sgblack@eecs.umich.eduSyscallReturn 36313995Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, ThreadContext *tc) 364360SN/A{ 36511383Sbrandon.potter@amd.com // With mmap more fully implemented, it might be worthwhile to bite 36611383Sbrandon.potter@amd.com // the bullet and implement munmap. Should allow us to reuse simulated 36711383Sbrandon.potter@amd.com // memory. 3681458SN/A return 0; 369360SN/A} 370360SN/A 371360SN/A 372360SN/Aconst char *hostname = "m5.eecs.umich.edu"; 373360SN/A 3741450SN/ASyscallReturn 37513995Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, ThreadContext *tc) 376360SN/A{ 3776701Sgblack@eecs.umich.edu int index = 0; 37813995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 37911906SBrandon.Potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 3806701Sgblack@eecs.umich.edu int name_len = p->getSyscallArg(tc, index); 38111906SBrandon.Potter@amd.com BufferArg name(buf_ptr, name_len); 382360SN/A 383360SN/A strncpy((char *)name.bufferPtr(), hostname, name_len); 384360SN/A 38514024Sgabeblack@google.com name.copyOut(tc->getVirtProxy()); 386360SN/A 3871458SN/A return 0; 388360SN/A} 389360SN/A 3901450SN/ASyscallReturn 39113995Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, ThreadContext *tc) 3925513SMichael.Adler@intel.com{ 3935513SMichael.Adler@intel.com int result = 0; 3946731Svince@csl.cornell.edu int index = 0; 39513995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 39611906SBrandon.Potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 3976701Sgblack@eecs.umich.edu unsigned long size = p->getSyscallArg(tc, index); 39811906SBrandon.Potter@amd.com BufferArg buf(buf_ptr, size); 3995513SMichael.Adler@intel.com 4005513SMichael.Adler@intel.com // Is current working directory defined? 40113883Sdavid.hashe@amd.com string cwd = p->tgtCwd; 4025513SMichael.Adler@intel.com if (!cwd.empty()) { 4035513SMichael.Adler@intel.com if (cwd.length() >= size) { 4045513SMichael.Adler@intel.com // Buffer too small 4055513SMichael.Adler@intel.com return -ERANGE; 4065513SMichael.Adler@intel.com } 4075513SMichael.Adler@intel.com strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 4085513SMichael.Adler@intel.com result = cwd.length(); 40910955Sdavid.hashe@amd.com } else { 41011856Sbrandon.potter@amd.com if (getcwd((char *)buf.bufferPtr(), size)) { 4115513SMichael.Adler@intel.com result = strlen((char *)buf.bufferPtr()); 41210955Sdavid.hashe@amd.com } else { 4135513SMichael.Adler@intel.com result = -1; 4145513SMichael.Adler@intel.com } 4155513SMichael.Adler@intel.com } 4165513SMichael.Adler@intel.com 41714024Sgabeblack@google.com buf.copyOut(tc->getVirtProxy()); 4185513SMichael.Adler@intel.com 4195513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 4205513SMichael.Adler@intel.com} 4215513SMichael.Adler@intel.com 42210203SAli.Saidi@ARM.comSyscallReturn 42313995Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 42410203SAli.Saidi@ARM.com{ 42513995Sbrandon.potter@amd.com return readlinkFunc(desc, callnum, tc, 0); 42610203SAli.Saidi@ARM.com} 4275513SMichael.Adler@intel.com 4285513SMichael.Adler@intel.comSyscallReturn 42913995Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc, int index) 4305513SMichael.Adler@intel.com{ 4315513SMichael.Adler@intel.com string path; 43213995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 4335513SMichael.Adler@intel.com 43414024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 43510223Ssteve.reinhardt@amd.com return -EFAULT; 4365513SMichael.Adler@intel.com 43713883Sdavid.hashe@amd.com // Adjust path for cwd and redirection 43813883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 4395513SMichael.Adler@intel.com 44011906SBrandon.Potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 4416701Sgblack@eecs.umich.edu size_t bufsiz = p->getSyscallArg(tc, index); 4426701Sgblack@eecs.umich.edu 44311906SBrandon.Potter@amd.com BufferArg buf(buf_ptr, bufsiz); 4445513SMichael.Adler@intel.com 44510955Sdavid.hashe@amd.com int result = -1; 44610955Sdavid.hashe@amd.com if (path != "/proc/self/exe") { 44710955Sdavid.hashe@amd.com result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 44810955Sdavid.hashe@amd.com } else { 44911140Sjthestness@gmail.com // Emulate readlink() called on '/proc/self/exe' should return the 45011140Sjthestness@gmail.com // absolute path of the binary running in the simulated system (the 45111851Sbrandon.potter@amd.com // Process' executable). It is possible that using this path in 45211140Sjthestness@gmail.com // the simulated system will result in unexpected behavior if: 45311140Sjthestness@gmail.com // 1) One binary runs another (e.g., -c time -o "my_binary"), and 45411140Sjthestness@gmail.com // called binary calls readlink(). 45511140Sjthestness@gmail.com // 2) The host's full path to the running benchmark changes from one 45611140Sjthestness@gmail.com // simulation to another. This can result in different simulated 45711140Sjthestness@gmail.com // performance since the simulated system will process the binary 45811140Sjthestness@gmail.com // path differently, even if the binary itself does not change. 45911140Sjthestness@gmail.com 46011140Sjthestness@gmail.com // Get the absolute canonical path to the running application 46111140Sjthestness@gmail.com char real_path[PATH_MAX]; 46211140Sjthestness@gmail.com char *check_real_path = realpath(p->progName(), real_path); 46311140Sjthestness@gmail.com if (!check_real_path) { 46411140Sjthestness@gmail.com fatal("readlink('/proc/self/exe') unable to resolve path to " 46511140Sjthestness@gmail.com "executable: %s", p->progName()); 46611140Sjthestness@gmail.com } 46711140Sjthestness@gmail.com strncpy((char*)buf.bufferPtr(), real_path, bufsiz); 46811140Sjthestness@gmail.com size_t real_path_len = strlen(real_path); 46911140Sjthestness@gmail.com if (real_path_len > bufsiz) { 47010955Sdavid.hashe@amd.com // readlink will truncate the contents of the 47110955Sdavid.hashe@amd.com // path to ensure it is no more than bufsiz 47210955Sdavid.hashe@amd.com result = bufsiz; 47310955Sdavid.hashe@amd.com } else { 47411140Sjthestness@gmail.com result = real_path_len; 47510955Sdavid.hashe@amd.com } 47611140Sjthestness@gmail.com 47711140Sjthestness@gmail.com // Issue a warning about potential unexpected results 47811140Sjthestness@gmail.com warn_once("readlink() called on '/proc/self/exe' may yield unexpected " 47911140Sjthestness@gmail.com "results in various settings.\n Returning '%s'\n", 48011140Sjthestness@gmail.com (char*)buf.bufferPtr()); 48110955Sdavid.hashe@amd.com } 4825513SMichael.Adler@intel.com 48314024Sgabeblack@google.com buf.copyOut(tc->getVirtProxy()); 4845513SMichael.Adler@intel.com 4855513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 4865513SMichael.Adler@intel.com} 4875513SMichael.Adler@intel.com 4885513SMichael.Adler@intel.comSyscallReturn 48913995Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc) 490511SN/A{ 49113995Sbrandon.potter@amd.com return unlinkHelper(desc, num, tc, 0); 49210633Smichaelupton@gmail.com} 49310633Smichaelupton@gmail.com 49410633Smichaelupton@gmail.comSyscallReturn 49513995Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, ThreadContext *tc, int index) 49610633Smichaelupton@gmail.com{ 4971706SN/A string path; 49813995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 499360SN/A 50014024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 50110223Ssteve.reinhardt@amd.com return -EFAULT; 502511SN/A 50313883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 5043669Sbinkertn@umich.edu 505511SN/A int result = unlink(path.c_str()); 5061458SN/A return (result == -1) ? -errno : result; 507511SN/A} 508511SN/A 50912795Smattdsinclair@gmail.comSyscallReturn 51013995Sbrandon.potter@amd.comlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc) 51112795Smattdsinclair@gmail.com{ 51212795Smattdsinclair@gmail.com string path; 51312795Smattdsinclair@gmail.com string new_path; 51413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 51512795Smattdsinclair@gmail.com 51612795Smattdsinclair@gmail.com int index = 0; 51714024Sgabeblack@google.com auto &virt_mem = tc->getVirtProxy(); 51812795Smattdsinclair@gmail.com if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index))) 51912795Smattdsinclair@gmail.com return -EFAULT; 52012795Smattdsinclair@gmail.com if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index))) 52112795Smattdsinclair@gmail.com return -EFAULT; 52212795Smattdsinclair@gmail.com 52313883Sdavid.hashe@amd.com path = p->absolutePath(path, true); 52413883Sdavid.hashe@amd.com new_path = p->absolutePath(new_path, true); 52512795Smattdsinclair@gmail.com 52612795Smattdsinclair@gmail.com int result = link(path.c_str(), new_path.c_str()); 52712795Smattdsinclair@gmail.com return (result == -1) ? -errno : result; 52812795Smattdsinclair@gmail.com} 5295513SMichael.Adler@intel.com 5305513SMichael.Adler@intel.comSyscallReturn 53113995Sbrandon.potter@amd.comsymlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc) 53212796Smattdsinclair@gmail.com{ 53312796Smattdsinclair@gmail.com string path; 53412796Smattdsinclair@gmail.com string new_path; 53513995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 53612796Smattdsinclair@gmail.com 53712796Smattdsinclair@gmail.com int index = 0; 53814024Sgabeblack@google.com auto &virt_mem = tc->getVirtProxy(); 53912796Smattdsinclair@gmail.com if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index))) 54012796Smattdsinclair@gmail.com return -EFAULT; 54112796Smattdsinclair@gmail.com if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index))) 54212796Smattdsinclair@gmail.com return -EFAULT; 54312796Smattdsinclair@gmail.com 54413883Sdavid.hashe@amd.com path = p->absolutePath(path, true); 54513883Sdavid.hashe@amd.com new_path = p->absolutePath(new_path, true); 54612796Smattdsinclair@gmail.com 54712796Smattdsinclair@gmail.com int result = symlink(path.c_str(), new_path.c_str()); 54812796Smattdsinclair@gmail.com return (result == -1) ? -errno : result; 54912796Smattdsinclair@gmail.com} 55012796Smattdsinclair@gmail.com 55112796Smattdsinclair@gmail.comSyscallReturn 55213995Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, ThreadContext *tc) 5535513SMichael.Adler@intel.com{ 55413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 5556701Sgblack@eecs.umich.edu int index = 0; 55613883Sdavid.hashe@amd.com std::string path; 55714024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 55810223Ssteve.reinhardt@amd.com return -EFAULT; 5595513SMichael.Adler@intel.com 56013883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 5616701Sgblack@eecs.umich.edu mode_t mode = p->getSyscallArg(tc, index); 5625513SMichael.Adler@intel.com 56313883Sdavid.hashe@amd.com auto result = mkdir(path.c_str(), mode); 5645513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 5655513SMichael.Adler@intel.com} 5665513SMichael.Adler@intel.com 5671450SN/ASyscallReturn 56813995Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, ThreadContext *tc) 569511SN/A{ 5701706SN/A string old_name; 57113995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 572511SN/A 5736701Sgblack@eecs.umich.edu int index = 0; 57414024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString( 57514024Sgabeblack@google.com old_name, p->getSyscallArg(tc, index))) { 5761458SN/A return -EFAULT; 57714024Sgabeblack@google.com } 578511SN/A 5791706SN/A string new_name; 580511SN/A 58114024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString( 58214024Sgabeblack@google.com new_name, p->getSyscallArg(tc, index))) { 5831458SN/A return -EFAULT; 58414024Sgabeblack@google.com } 585511SN/A 58613883Sdavid.hashe@amd.com // Adjust path for cwd and redirection 58713883Sdavid.hashe@amd.com old_name = p->checkPathRedirect(old_name); 58813883Sdavid.hashe@amd.com new_name = p->checkPathRedirect(new_name); 5893669Sbinkertn@umich.edu 5901706SN/A int64_t result = rename(old_name.c_str(), new_name.c_str()); 5911458SN/A return (result == -1) ? -errno : result; 592511SN/A} 593511SN/A 5941706SN/ASyscallReturn 59513995Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc) 5961706SN/A{ 5971706SN/A string path; 59813995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 5991706SN/A 6006701Sgblack@eecs.umich.edu int index = 0; 60114024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 6021706SN/A return -EFAULT; 6031706SN/A 6046701Sgblack@eecs.umich.edu off_t length = p->getSyscallArg(tc, index); 6051706SN/A 60613883Sdavid.hashe@amd.com // Adjust path for cwd and redirection 60713883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 6083669Sbinkertn@umich.edu 6091706SN/A int result = truncate(path.c_str(), length); 6101706SN/A return (result == -1) ? -errno : result; 6111706SN/A} 6121706SN/A 6131706SN/ASyscallReturn 61413995Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc) 6151706SN/A{ 6166701Sgblack@eecs.umich.edu int index = 0; 61713995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 61811856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 61911856Sbrandon.potter@amd.com off_t length = p->getSyscallArg(tc, index); 6201706SN/A 62111856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 62211856Sbrandon.potter@amd.com if (!ffdp) 6231706SN/A return -EBADF; 62411856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 6251706SN/A 62610931Sbrandon.potter@amd.com int result = ftruncate(sim_fd, length); 6271706SN/A return (result == -1) ? -errno : result; 6281706SN/A} 6291999SN/A 6301999SN/ASyscallReturn 63113995Sbrandon.potter@amd.comtruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc) 6326703Svince@csl.cornell.edu{ 6336703Svince@csl.cornell.edu int index = 0; 63413995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 6356703Svince@csl.cornell.edu string path; 6366703Svince@csl.cornell.edu 63714024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString( 63814024Sgabeblack@google.com path, process->getSyscallArg(tc, index))) { 63911906SBrandon.Potter@amd.com return -EFAULT; 64014024Sgabeblack@google.com } 6416703Svince@csl.cornell.edu 6426744SAli.Saidi@arm.com int64_t length = process->getSyscallArg(tc, index, 64); 6436703Svince@csl.cornell.edu 64413883Sdavid.hashe@amd.com // Adjust path for cwd and redirection 64513883Sdavid.hashe@amd.com path = process->checkPathRedirect(path); 6466703Svince@csl.cornell.edu 6476744SAli.Saidi@arm.com#if NO_STAT64 6486744SAli.Saidi@arm.com int result = truncate(path.c_str(), length); 6496744SAli.Saidi@arm.com#else 6506703Svince@csl.cornell.edu int result = truncate64(path.c_str(), length); 6516744SAli.Saidi@arm.com#endif 6526703Svince@csl.cornell.edu return (result == -1) ? -errno : result; 6536703Svince@csl.cornell.edu} 6546703Svince@csl.cornell.edu 6556703Svince@csl.cornell.eduSyscallReturn 65613995Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc) 6576685Stjones1@inf.ed.ac.uk{ 6586701Sgblack@eecs.umich.edu int index = 0; 65913995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 66011856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 66111856Sbrandon.potter@amd.com int64_t length = p->getSyscallArg(tc, index, 64); 6626685Stjones1@inf.ed.ac.uk 66311856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 66411856Sbrandon.potter@amd.com if (!ffdp) 6656685Stjones1@inf.ed.ac.uk return -EBADF; 66611856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 6676685Stjones1@inf.ed.ac.uk 6686744SAli.Saidi@arm.com#if NO_STAT64 66910931Sbrandon.potter@amd.com int result = ftruncate(sim_fd, length); 6706744SAli.Saidi@arm.com#else 67110931Sbrandon.potter@amd.com int result = ftruncate64(sim_fd, length); 6726744SAli.Saidi@arm.com#endif 6736685Stjones1@inf.ed.ac.uk return (result == -1) ? -errno : result; 6746685Stjones1@inf.ed.ac.uk} 6756685Stjones1@inf.ed.ac.uk 6766685Stjones1@inf.ed.ac.ukSyscallReturn 67713995Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, ThreadContext *tc) 6785513SMichael.Adler@intel.com{ 6795513SMichael.Adler@intel.com // Letting the simulated program change the simulator's umask seems like 6805513SMichael.Adler@intel.com // a bad idea. Compromise by just returning the current umask but not 6815513SMichael.Adler@intel.com // changing anything. 6825513SMichael.Adler@intel.com mode_t oldMask = umask(0); 6835513SMichael.Adler@intel.com umask(oldMask); 6845521Snate@binkert.org return (int)oldMask; 6855513SMichael.Adler@intel.com} 6865513SMichael.Adler@intel.com 6875513SMichael.Adler@intel.comSyscallReturn 68813995Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, ThreadContext *tc) 6891999SN/A{ 6901999SN/A string path; 69113995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 6921999SN/A 6936701Sgblack@eecs.umich.edu int index = 0; 69414024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 6951999SN/A return -EFAULT; 6961999SN/A 6971999SN/A /* XXX endianess */ 6986701Sgblack@eecs.umich.edu uint32_t owner = p->getSyscallArg(tc, index); 6991999SN/A uid_t hostOwner = owner; 7006701Sgblack@eecs.umich.edu uint32_t group = p->getSyscallArg(tc, index); 7011999SN/A gid_t hostGroup = group; 7021999SN/A 70313883Sdavid.hashe@amd.com // Adjust path for cwd and redirection 70413883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 7053669Sbinkertn@umich.edu 7061999SN/A int result = chown(path.c_str(), hostOwner, hostGroup); 7071999SN/A return (result == -1) ? -errno : result; 7081999SN/A} 7091999SN/A 7101999SN/ASyscallReturn 71113995Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, ThreadContext *tc) 7121999SN/A{ 7136701Sgblack@eecs.umich.edu int index = 0; 71413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 71511856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 7161999SN/A 71711856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 71811856Sbrandon.potter@amd.com if (!ffdp) 7191999SN/A return -EBADF; 72011856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 7211999SN/A 7221999SN/A /* XXX endianess */ 72311856Sbrandon.potter@amd.com uint32_t owner = p->getSyscallArg(tc, index); 7241999SN/A uid_t hostOwner = owner; 72511856Sbrandon.potter@amd.com uint32_t group = p->getSyscallArg(tc, index); 7261999SN/A gid_t hostGroup = group; 7271999SN/A 72810931Sbrandon.potter@amd.com int result = fchown(sim_fd, hostOwner, hostGroup); 7291999SN/A return (result == -1) ? -errno : result; 7301999SN/A} 7312093SN/A 73211856Sbrandon.potter@amd.com/** 73311908SBrandon.Potter@amd.com * FIXME: The file description is not shared among file descriptors created 73411908SBrandon.Potter@amd.com * with dup. Really, it's difficult to maintain fields like file offset or 73511908SBrandon.Potter@amd.com * flags since an update to such a field won't be reflected in the metadata 73611908SBrandon.Potter@amd.com * for the fd entries that we maintain for checkpoint restoration. 73711856Sbrandon.potter@amd.com */ 7382093SN/ASyscallReturn 73913995Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, ThreadContext *tc) 7403079Sstever@eecs.umich.edu{ 7416701Sgblack@eecs.umich.edu int index = 0; 74213995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 74311856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 74410931Sbrandon.potter@amd.com 74511856Sbrandon.potter@amd.com auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 74611856Sbrandon.potter@amd.com if (!old_hbfdp) 7473079Sstever@eecs.umich.edu return -EBADF; 74811856Sbrandon.potter@amd.com int sim_fd = old_hbfdp->getSimFD(); 7495282Srstrong@cs.ucsd.edu 75010781Snilay@cs.wisc.edu int result = dup(sim_fd); 75111908SBrandon.Potter@amd.com if (result == -1) 75211908SBrandon.Potter@amd.com return -errno; 75311856Sbrandon.potter@amd.com 75411908SBrandon.Potter@amd.com auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone()); 75511856Sbrandon.potter@amd.com new_hbfdp->setSimFD(result); 75611908SBrandon.Potter@amd.com new_hbfdp->setCOE(false); 75711908SBrandon.Potter@amd.com return p->fds->allocFD(new_hbfdp); 75811908SBrandon.Potter@amd.com} 75911856Sbrandon.potter@amd.com 76011908SBrandon.Potter@amd.comSyscallReturn 76113995Sbrandon.potter@amd.comdup2Func(SyscallDesc *desc, int num, ThreadContext *tc) 76211908SBrandon.Potter@amd.com{ 76311908SBrandon.Potter@amd.com int index = 0; 76413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 76511908SBrandon.Potter@amd.com int old_tgt_fd = p->getSyscallArg(tc, index); 76611908SBrandon.Potter@amd.com auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]); 76711908SBrandon.Potter@amd.com if (!old_hbp) 76811908SBrandon.Potter@amd.com return -EBADF; 76911908SBrandon.Potter@amd.com int old_sim_fd = old_hbp->getSimFD(); 77011908SBrandon.Potter@amd.com 77111908SBrandon.Potter@amd.com /** 77211908SBrandon.Potter@amd.com * We need a valid host file descriptor number to be able to pass into 77311908SBrandon.Potter@amd.com * the second parameter for dup2 (newfd), but we don't know what the 77411908SBrandon.Potter@amd.com * viable numbers are; we execute the open call to retrieve one. 77511908SBrandon.Potter@amd.com */ 77611908SBrandon.Potter@amd.com int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY)); 77711908SBrandon.Potter@amd.com if (res_fd == -1) 77811908SBrandon.Potter@amd.com return -errno; 77911908SBrandon.Potter@amd.com 78011908SBrandon.Potter@amd.com int new_tgt_fd = p->getSyscallArg(tc, index); 78111908SBrandon.Potter@amd.com auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]); 78211908SBrandon.Potter@amd.com if (new_hbp) 78311908SBrandon.Potter@amd.com p->fds->closeFDEntry(new_tgt_fd); 78411908SBrandon.Potter@amd.com new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone()); 78511908SBrandon.Potter@amd.com new_hbp->setSimFD(res_fd); 78611908SBrandon.Potter@amd.com new_hbp->setCOE(false); 78711908SBrandon.Potter@amd.com 78811908SBrandon.Potter@amd.com return p->fds->allocFD(new_hbp); 7893079Sstever@eecs.umich.edu} 7903079Sstever@eecs.umich.edu 7913079Sstever@eecs.umich.eduSyscallReturn 79213995Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, ThreadContext *tc) 7932093SN/A{ 79411875Sbrandon.potter@amd.com int arg; 7956701Sgblack@eecs.umich.edu int index = 0; 79613995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 79711856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 79811875Sbrandon.potter@amd.com int cmd = p->getSyscallArg(tc, index); 7992093SN/A 80011856Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 80111856Sbrandon.potter@amd.com if (!hbfdp) 8022093SN/A return -EBADF; 80311856Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 8042093SN/A 80511875Sbrandon.potter@amd.com int coe = hbfdp->getCOE(); 80611875Sbrandon.potter@amd.com 8072093SN/A switch (cmd) { 80811875Sbrandon.potter@amd.com case F_GETFD: 80911875Sbrandon.potter@amd.com return coe & FD_CLOEXEC; 8102093SN/A 81111875Sbrandon.potter@amd.com case F_SETFD: { 81211875Sbrandon.potter@amd.com arg = p->getSyscallArg(tc, index); 81311875Sbrandon.potter@amd.com arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false); 8142093SN/A return 0; 81511875Sbrandon.potter@amd.com } 8162093SN/A 81711875Sbrandon.potter@amd.com // Rely on the host to maintain the file status flags for this file 81811875Sbrandon.potter@amd.com // description rather than maintain it ourselves. Admittedly, this 81911875Sbrandon.potter@amd.com // is suboptimal (and possibly error prone), but it is difficult to 82011875Sbrandon.potter@amd.com // maintain the flags by tracking them across the different descriptors 82111875Sbrandon.potter@amd.com // (that refer to this file description) caused by clone, dup, and 82211875Sbrandon.potter@amd.com // subsequent fcntls. 82311875Sbrandon.potter@amd.com case F_GETFL: 82411875Sbrandon.potter@amd.com case F_SETFL: { 82511875Sbrandon.potter@amd.com arg = p->getSyscallArg(tc, index); 82611875Sbrandon.potter@amd.com int rv = fcntl(sim_fd, cmd, arg); 82711875Sbrandon.potter@amd.com return (rv == -1) ? -errno : rv; 82811875Sbrandon.potter@amd.com } 8292093SN/A 8302093SN/A default: 83111875Sbrandon.potter@amd.com warn("fcntl: unsupported command %d\n", cmd); 8322093SN/A return 0; 8332093SN/A } 8342093SN/A} 8352093SN/A 8362238SN/ASyscallReturn 83713995Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, ThreadContext *tc) 8382687Sksewell@umich.edu{ 8396701Sgblack@eecs.umich.edu int index = 0; 84013995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 84111856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 8422687Sksewell@umich.edu 84311856Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 84411856Sbrandon.potter@amd.com if (!hbfdp) 8452687Sksewell@umich.edu return -EBADF; 84611856Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 8472687Sksewell@umich.edu 84811856Sbrandon.potter@amd.com int cmd = p->getSyscallArg(tc, index); 8492687Sksewell@umich.edu switch (cmd) { 8502687Sksewell@umich.edu case 33: //F_GETLK64 85110931Sbrandon.potter@amd.com warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd); 8522687Sksewell@umich.edu return -EMFILE; 8532687Sksewell@umich.edu 8542687Sksewell@umich.edu case 34: // F_SETLK64 8552687Sksewell@umich.edu case 35: // F_SETLKW64 85610931Sbrandon.potter@amd.com warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", 85710931Sbrandon.potter@amd.com tgt_fd); 8582687Sksewell@umich.edu return -EMFILE; 8592687Sksewell@umich.edu 8602687Sksewell@umich.edu default: 8612687Sksewell@umich.edu // not sure if this is totally valid, but we'll pass it through 8622687Sksewell@umich.edu // to the underlying OS 86310931Sbrandon.potter@amd.com warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd); 86410931Sbrandon.potter@amd.com return fcntl(sim_fd, cmd); 8652687Sksewell@umich.edu } 8662687Sksewell@umich.edu} 8672687Sksewell@umich.edu 8682687Sksewell@umich.eduSyscallReturn 86913995Sbrandon.potter@amd.compipeImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool pseudoPipe) 8702238SN/A{ 87114124Sbrandon.potter@amd.com Addr tgt_addr = 0; 87214124Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 87314124Sbrandon.potter@amd.com if (!pseudoPipe) { 87414124Sbrandon.potter@amd.com int index = 0; 87514124Sbrandon.potter@amd.com tgt_addr = p->getSyscallArg(tc, index); 87614124Sbrandon.potter@amd.com } 87714124Sbrandon.potter@amd.com 87811856Sbrandon.potter@amd.com int sim_fds[2], tgt_fds[2]; 8792093SN/A 88011856Sbrandon.potter@amd.com int pipe_retval = pipe(sim_fds); 88111908SBrandon.Potter@amd.com if (pipe_retval == -1) 88211908SBrandon.Potter@amd.com return -errno; 8832238SN/A 88411856Sbrandon.potter@amd.com auto rend = PipeFDEntry::EndType::read; 88511856Sbrandon.potter@amd.com auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend); 88611908SBrandon.Potter@amd.com tgt_fds[0] = p->fds->allocFD(rpfd); 8872238SN/A 88811856Sbrandon.potter@amd.com auto wend = PipeFDEntry::EndType::write; 88911856Sbrandon.potter@amd.com auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend); 89011908SBrandon.Potter@amd.com tgt_fds[1] = p->fds->allocFD(wpfd); 89111856Sbrandon.potter@amd.com 89211856Sbrandon.potter@amd.com /** 89311856Sbrandon.potter@amd.com * Now patch the read object to record the target file descriptor chosen 89411856Sbrandon.potter@amd.com * as the write end of the pipe. 89511856Sbrandon.potter@amd.com */ 89611856Sbrandon.potter@amd.com rpfd->setPipeReadSource(tgt_fds[1]); 89711856Sbrandon.potter@amd.com 89811856Sbrandon.potter@amd.com /** 89911856Sbrandon.potter@amd.com * Alpha Linux convention for pipe() is that fd[0] is returned as 90011856Sbrandon.potter@amd.com * the return value of the function, and fd[1] is returned in r20. 90111856Sbrandon.potter@amd.com */ 90211908SBrandon.Potter@amd.com if (pseudoPipe) { 90311908SBrandon.Potter@amd.com tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]); 90411908SBrandon.Potter@amd.com return tgt_fds[0]; 90511908SBrandon.Potter@amd.com } 90611908SBrandon.Potter@amd.com 90711908SBrandon.Potter@amd.com /** 90811908SBrandon.Potter@amd.com * Copy the target file descriptors into buffer space and then copy 90911908SBrandon.Potter@amd.com * the buffer space back into the target address space. 91011908SBrandon.Potter@amd.com */ 91111908SBrandon.Potter@amd.com BufferArg tgt_handle(tgt_addr, sizeof(int[2])); 91211908SBrandon.Potter@amd.com int *buf_ptr = (int*)tgt_handle.bufferPtr(); 91311908SBrandon.Potter@amd.com buf_ptr[0] = tgt_fds[0]; 91411908SBrandon.Potter@amd.com buf_ptr[1] = tgt_fds[1]; 91514024Sgabeblack@google.com tgt_handle.copyOut(tc->getVirtProxy()); 91611908SBrandon.Potter@amd.com return 0; 91711908SBrandon.Potter@amd.com} 91811908SBrandon.Potter@amd.com 91911908SBrandon.Potter@amd.comSyscallReturn 92013995Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 92111908SBrandon.Potter@amd.com{ 92213995Sbrandon.potter@amd.com return pipeImpl(desc, callnum, tc, true); 92311908SBrandon.Potter@amd.com} 92411908SBrandon.Potter@amd.com 92511908SBrandon.Potter@amd.comSyscallReturn 92613995Sbrandon.potter@amd.compipeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 92711908SBrandon.Potter@amd.com{ 92813995Sbrandon.potter@amd.com return pipeImpl(desc, callnum, tc, false); 9292238SN/A} 9302238SN/A 93111885Sbrandon.potter@amd.comSyscallReturn 93213995Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 93311885Sbrandon.potter@amd.com{ 93411885Sbrandon.potter@amd.com int index = 0; 93513995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 93611885Sbrandon.potter@amd.com int pid = process->getSyscallArg(tc, index); 93711885Sbrandon.potter@amd.com int pgid = process->getSyscallArg(tc, index); 93811885Sbrandon.potter@amd.com 93911885Sbrandon.potter@amd.com if (pgid < 0) 94011885Sbrandon.potter@amd.com return -EINVAL; 94111885Sbrandon.potter@amd.com 94211885Sbrandon.potter@amd.com if (pid == 0) { 94311885Sbrandon.potter@amd.com process->setpgid(process->pid()); 94411885Sbrandon.potter@amd.com return 0; 94511885Sbrandon.potter@amd.com } 94611885Sbrandon.potter@amd.com 94711913SBrandon.Potter@amd.com Process *matched_ph = nullptr; 94811885Sbrandon.potter@amd.com System *sysh = tc->getSystemPtr(); 94911885Sbrandon.potter@amd.com 95011885Sbrandon.potter@amd.com // Retrieves process pointer from active/suspended thread contexts. 95111885Sbrandon.potter@amd.com for (int i = 0; i < sysh->numContexts(); i++) { 95211885Sbrandon.potter@amd.com if (sysh->threadContexts[i]->status() != ThreadContext::Halted) { 95311885Sbrandon.potter@amd.com Process *temp_h = sysh->threadContexts[i]->getProcessPtr(); 95411885Sbrandon.potter@amd.com Process *walk_ph = (Process*)temp_h; 95511885Sbrandon.potter@amd.com 95611885Sbrandon.potter@amd.com if (walk_ph && walk_ph->pid() == process->pid()) 95711885Sbrandon.potter@amd.com matched_ph = walk_ph; 95811885Sbrandon.potter@amd.com } 95911885Sbrandon.potter@amd.com } 96011885Sbrandon.potter@amd.com 96111913SBrandon.Potter@amd.com assert(matched_ph); 96211885Sbrandon.potter@amd.com matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid); 96311885Sbrandon.potter@amd.com 96411885Sbrandon.potter@amd.com return 0; 96511885Sbrandon.potter@amd.com} 9662238SN/A 9672238SN/ASyscallReturn 96813995Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9692238SN/A{ 9702238SN/A // Make up a PID. There's no interprocess communication in 9712238SN/A // fake_syscall mode, so there's no way for a process to know it's 9722238SN/A // not getting a unique value. 9732238SN/A 97413995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 9753114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 9763114Sgblack@eecs.umich.edu return process->pid(); 9772238SN/A} 9782238SN/A 9792238SN/A 9802238SN/ASyscallReturn 98113995Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9822238SN/A{ 9832238SN/A // Make up a UID and EUID... it shouldn't matter, and we want the 9842238SN/A // simulation to be deterministic. 9852238SN/A 9862238SN/A // EUID goes in r20. 98713995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 98811906SBrandon.Potter@amd.com tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID 98911906SBrandon.Potter@amd.com return process->uid(); // UID 9902238SN/A} 9912238SN/A 9922238SN/A 9932238SN/ASyscallReturn 99413995Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9952238SN/A{ 9962238SN/A // Get current group ID. EGID goes in r20. 99713995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 99811906SBrandon.Potter@amd.com tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID 9993114Sgblack@eecs.umich.edu return process->gid(); 10002238SN/A} 10012238SN/A 10022238SN/A 10032238SN/ASyscallReturn 100413995Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10052238SN/A{ 10062238SN/A // can't fathom why a benchmark would call this. 10076701Sgblack@eecs.umich.edu int index = 0; 100813995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10096701Sgblack@eecs.umich.edu warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index)); 10102238SN/A return 0; 10112238SN/A} 10122238SN/A 10132238SN/ASyscallReturn 101413995Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10152238SN/A{ 101613995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 101711885Sbrandon.potter@amd.com return process->tgid(); 101811885Sbrandon.potter@amd.com} 10192238SN/A 102011885Sbrandon.potter@amd.comSyscallReturn 102113995Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 102211885Sbrandon.potter@amd.com{ 102313995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10243114Sgblack@eecs.umich.edu return process->pid(); 10252238SN/A} 10262238SN/A 10272238SN/ASyscallReturn 102813995Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10292238SN/A{ 103013995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10313114Sgblack@eecs.umich.edu return process->ppid(); 10322238SN/A} 10332238SN/A 10342238SN/ASyscallReturn 103513995Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10362238SN/A{ 103713995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10385543Ssaidi@eecs.umich.edu return process->uid(); // UID 10392238SN/A} 10402238SN/A 10412238SN/ASyscallReturn 104213995Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10432238SN/A{ 104413995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10455543Ssaidi@eecs.umich.edu return process->euid(); // UID 10462238SN/A} 10472238SN/A 10482238SN/ASyscallReturn 104913995Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10502238SN/A{ 105113995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10523114Sgblack@eecs.umich.edu return process->gid(); 10532238SN/A} 10542238SN/A 10552238SN/ASyscallReturn 105613995Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10572238SN/A{ 105813995Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 10593114Sgblack@eecs.umich.edu return process->egid(); 10602238SN/A} 10612238SN/A 10629455Smitch.hayenga+gem5@gmail.comSyscallReturn 106313995Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 106411760Sbrandon.potter@amd.com{ 106513936SAndrea.Mondelli@ucf.edu#if defined(__linux__) 106611760Sbrandon.potter@amd.com int index = 0; 106713995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 106811856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 106911856Sbrandon.potter@amd.com int mode = p->getSyscallArg(tc, index); 107011856Sbrandon.potter@amd.com off_t offset = p->getSyscallArg(tc, index); 107111856Sbrandon.potter@amd.com off_t len = p->getSyscallArg(tc, index); 107211760Sbrandon.potter@amd.com 107311856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 107411856Sbrandon.potter@amd.com if (!ffdp) 107511760Sbrandon.potter@amd.com return -EBADF; 107611856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 107711760Sbrandon.potter@amd.com 107811760Sbrandon.potter@amd.com int result = fallocate(sim_fd, mode, offset, len); 107911760Sbrandon.potter@amd.com if (result < 0) 108011760Sbrandon.potter@amd.com return -errno; 108113933Sbrandon.potter@amd.com return 0; 108213933Sbrandon.potter@amd.com#else 108313933Sbrandon.potter@amd.com warnUnsupportedOS("fallocate"); 108413933Sbrandon.potter@amd.com return -1; 108511799Sbrandon.potter@amd.com#endif 108611760Sbrandon.potter@amd.com} 108711760Sbrandon.potter@amd.com 108811760Sbrandon.potter@amd.comSyscallReturn 108913995Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, ThreadContext *tc, int index) 10909455Smitch.hayenga+gem5@gmail.com{ 10919455Smitch.hayenga+gem5@gmail.com string path; 109213995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 109314024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 109410223Ssteve.reinhardt@amd.com return -EFAULT; 10959455Smitch.hayenga+gem5@gmail.com 109613883Sdavid.hashe@amd.com // Adjust path for cwd and redirection 109713883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 10989455Smitch.hayenga+gem5@gmail.com 10999455Smitch.hayenga+gem5@gmail.com mode_t mode = p->getSyscallArg(tc, index); 11009455Smitch.hayenga+gem5@gmail.com 11019455Smitch.hayenga+gem5@gmail.com int result = access(path.c_str(), mode); 11029455Smitch.hayenga+gem5@gmail.com return (result == -1) ? -errno : result; 11039455Smitch.hayenga+gem5@gmail.com} 110410203SAli.Saidi@ARM.com 110510203SAli.Saidi@ARM.comSyscallReturn 110613995Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 110710203SAli.Saidi@ARM.com{ 110813995Sbrandon.potter@amd.com return accessFunc(desc, callnum, tc, 0); 110910203SAli.Saidi@ARM.com} 111010203SAli.Saidi@ARM.com 111113031Sbrandon.potter@amd.comSyscallReturn 111213995Sbrandon.potter@amd.commknodFunc(SyscallDesc *desc, int num, ThreadContext *tc) 111313031Sbrandon.potter@amd.com{ 111413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 111513031Sbrandon.potter@amd.com int index = 0; 111613031Sbrandon.potter@amd.com std::string path; 111714024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 111813031Sbrandon.potter@amd.com return -EFAULT; 111913031Sbrandon.potter@amd.com 112013883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 112113031Sbrandon.potter@amd.com mode_t mode = p->getSyscallArg(tc, index); 112213031Sbrandon.potter@amd.com dev_t dev = p->getSyscallArg(tc, index); 112313031Sbrandon.potter@amd.com 112413031Sbrandon.potter@amd.com auto result = mknod(path.c_str(), mode, dev); 112513031Sbrandon.potter@amd.com return (result == -1) ? -errno : result; 112613031Sbrandon.potter@amd.com} 112713031Sbrandon.potter@amd.com 112813031Sbrandon.potter@amd.comSyscallReturn 112913995Sbrandon.potter@amd.comchdirFunc(SyscallDesc *desc, int num, ThreadContext *tc) 113013031Sbrandon.potter@amd.com{ 113113995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 113213031Sbrandon.potter@amd.com int index = 0; 113313031Sbrandon.potter@amd.com std::string path; 113414024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 113513031Sbrandon.potter@amd.com return -EFAULT; 113613031Sbrandon.potter@amd.com 113713883Sdavid.hashe@amd.com std::string tgt_cwd; 113813883Sdavid.hashe@amd.com if (startswith(path, "/")) { 113913883Sdavid.hashe@amd.com tgt_cwd = path; 114013883Sdavid.hashe@amd.com } else { 114113883Sdavid.hashe@amd.com char buf[PATH_MAX]; 114213883Sdavid.hashe@amd.com tgt_cwd = realpath((p->tgtCwd + "/" + path).c_str(), buf); 114313883Sdavid.hashe@amd.com } 114413883Sdavid.hashe@amd.com std::string host_cwd = p->checkPathRedirect(tgt_cwd); 114513031Sbrandon.potter@amd.com 114613883Sdavid.hashe@amd.com int result = chdir(host_cwd.c_str()); 114713883Sdavid.hashe@amd.com 114813883Sdavid.hashe@amd.com if (result == -1) 114913883Sdavid.hashe@amd.com return -errno; 115013883Sdavid.hashe@amd.com 115113883Sdavid.hashe@amd.com p->hostCwd = host_cwd; 115213883Sdavid.hashe@amd.com p->tgtCwd = tgt_cwd; 115313883Sdavid.hashe@amd.com return result; 115413031Sbrandon.potter@amd.com} 115513031Sbrandon.potter@amd.com 115613031Sbrandon.potter@amd.comSyscallReturn 115713995Sbrandon.potter@amd.comrmdirFunc(SyscallDesc *desc, int num, ThreadContext *tc) 115813031Sbrandon.potter@amd.com{ 115913995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 116013031Sbrandon.potter@amd.com int index = 0; 116113031Sbrandon.potter@amd.com std::string path; 116214024Sgabeblack@google.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 116313031Sbrandon.potter@amd.com return -EFAULT; 116413031Sbrandon.potter@amd.com 116513883Sdavid.hashe@amd.com path = p->checkPathRedirect(path); 116613031Sbrandon.potter@amd.com 116713031Sbrandon.potter@amd.com auto result = rmdir(path.c_str()); 116813031Sbrandon.potter@amd.com return (result == -1) ? -errno : result; 116913031Sbrandon.potter@amd.com} 117013031Sbrandon.potter@amd.com 117113539Sjavier.setoain@arm.com#if defined(SYS_getdents) || defined(SYS_getdents64) 117213539Sjavier.setoain@arm.comtemplate<typename DE, int SYS_NUM> 117313539Sjavier.setoain@arm.comstatic SyscallReturn 117413995Sbrandon.potter@amd.comgetdentsImpl(SyscallDesc *desc, int callnum, ThreadContext *tc) 117513031Sbrandon.potter@amd.com{ 117613031Sbrandon.potter@amd.com int index = 0; 117713995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 117813031Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 117913031Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 118013031Sbrandon.potter@amd.com unsigned count = p->getSyscallArg(tc, index); 118113031Sbrandon.potter@amd.com 118213031Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 118313031Sbrandon.potter@amd.com if (!hbfdp) 118413031Sbrandon.potter@amd.com return -EBADF; 118513031Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 118613031Sbrandon.potter@amd.com 118713031Sbrandon.potter@amd.com BufferArg buf_arg(buf_ptr, count); 118813539Sjavier.setoain@arm.com auto status = syscall(SYS_NUM, sim_fd, buf_arg.bufferPtr(), count); 118913031Sbrandon.potter@amd.com 119013031Sbrandon.potter@amd.com if (status == -1) 119113031Sbrandon.potter@amd.com return -errno; 119213031Sbrandon.potter@amd.com 119313031Sbrandon.potter@amd.com unsigned traversed = 0; 119413031Sbrandon.potter@amd.com while (traversed < status) { 119513539Sjavier.setoain@arm.com DE *buffer = (DE*)((Addr)buf_arg.bufferPtr() + traversed); 119613031Sbrandon.potter@amd.com 119713031Sbrandon.potter@amd.com auto host_reclen = buffer->d_reclen; 119813031Sbrandon.potter@amd.com 119913031Sbrandon.potter@amd.com /** 120013031Sbrandon.potter@amd.com * Convert the byte ordering from the host to the target before 120113031Sbrandon.potter@amd.com * passing the data back into the target's address space to preserve 120213031Sbrandon.potter@amd.com * endianness. 120313031Sbrandon.potter@amd.com */ 120413031Sbrandon.potter@amd.com buffer->d_ino = htog(buffer->d_ino); 120513031Sbrandon.potter@amd.com buffer->d_off = htog(buffer->d_off); 120613031Sbrandon.potter@amd.com buffer->d_reclen = htog(buffer->d_reclen); 120713031Sbrandon.potter@amd.com 120813031Sbrandon.potter@amd.com traversed += host_reclen; 120913031Sbrandon.potter@amd.com } 121013031Sbrandon.potter@amd.com 121114024Sgabeblack@google.com buf_arg.copyOut(tc->getVirtProxy()); 121213031Sbrandon.potter@amd.com return status; 121313031Sbrandon.potter@amd.com} 121413448Sciro.santilli@arm.com#endif 121513539Sjavier.setoain@arm.com 121613539Sjavier.setoain@arm.com#if defined(SYS_getdents) 121713539Sjavier.setoain@arm.comSyscallReturn 121813995Sbrandon.potter@amd.comgetdentsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 121913539Sjavier.setoain@arm.com{ 122013539Sjavier.setoain@arm.com typedef struct linux_dirent { 122113539Sjavier.setoain@arm.com unsigned long d_ino; 122213539Sjavier.setoain@arm.com unsigned long d_off; 122313539Sjavier.setoain@arm.com unsigned short d_reclen; 122413539Sjavier.setoain@arm.com char dname[]; 122513539Sjavier.setoain@arm.com } LinDent; 122613539Sjavier.setoain@arm.com 122713995Sbrandon.potter@amd.com return getdentsImpl<LinDent, SYS_getdents>(desc, callnum, tc); 122813539Sjavier.setoain@arm.com} 122913539Sjavier.setoain@arm.com#endif 123013539Sjavier.setoain@arm.com 123113539Sjavier.setoain@arm.com#if defined(SYS_getdents64) 123213539Sjavier.setoain@arm.comSyscallReturn 123313995Sbrandon.potter@amd.comgetdents64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 123413539Sjavier.setoain@arm.com{ 123513539Sjavier.setoain@arm.com typedef struct linux_dirent64 { 123613539Sjavier.setoain@arm.com ino64_t d_ino; 123713539Sjavier.setoain@arm.com off64_t d_off; 123813539Sjavier.setoain@arm.com unsigned short d_reclen; 123913539Sjavier.setoain@arm.com char dname[]; 124013539Sjavier.setoain@arm.com } LinDent64; 124113539Sjavier.setoain@arm.com 124213995Sbrandon.potter@amd.com return getdentsImpl<LinDent64, SYS_getdents64>(desc, callnum, tc); 124313539Sjavier.setoain@arm.com} 124413539Sjavier.setoain@arm.com#endif 124513568Sbrandon.potter@amd.com 124613568Sbrandon.potter@amd.comSyscallReturn 124713995Sbrandon.potter@amd.comshutdownFunc(SyscallDesc *desc, int num, ThreadContext *tc) 124813568Sbrandon.potter@amd.com{ 124913568Sbrandon.potter@amd.com int index = 0; 125013995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 125113568Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 125213568Sbrandon.potter@amd.com int how = p->getSyscallArg(tc, index); 125313568Sbrandon.potter@amd.com 125413568Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 125513568Sbrandon.potter@amd.com if (!sfdp) 125613568Sbrandon.potter@amd.com return -EBADF; 125713568Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 125813568Sbrandon.potter@amd.com 125913568Sbrandon.potter@amd.com int retval = shutdown(sim_fd, how); 126013568Sbrandon.potter@amd.com 126113568Sbrandon.potter@amd.com return (retval == -1) ? -errno : retval; 126213568Sbrandon.potter@amd.com} 126313568Sbrandon.potter@amd.com 126413568Sbrandon.potter@amd.comSyscallReturn 126513995Sbrandon.potter@amd.combindFunc(SyscallDesc *desc, int num, ThreadContext *tc) 126613568Sbrandon.potter@amd.com{ 126713568Sbrandon.potter@amd.com int index = 0; 126813995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 126913568Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 127013568Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 127113568Sbrandon.potter@amd.com int addrlen = p->getSyscallArg(tc, index); 127213568Sbrandon.potter@amd.com 127313568Sbrandon.potter@amd.com BufferArg bufSock(buf_ptr, addrlen); 127414024Sgabeblack@google.com bufSock.copyIn(tc->getVirtProxy()); 127513568Sbrandon.potter@amd.com 127613568Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 127713568Sbrandon.potter@amd.com if (!sfdp) 127813568Sbrandon.potter@amd.com return -EBADF; 127913568Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 128013568Sbrandon.potter@amd.com 128113568Sbrandon.potter@amd.com int status = ::bind(sim_fd, 128213568Sbrandon.potter@amd.com (struct sockaddr *)bufSock.bufferPtr(), 128313568Sbrandon.potter@amd.com addrlen); 128413568Sbrandon.potter@amd.com 128513568Sbrandon.potter@amd.com return (status == -1) ? -errno : status; 128613568Sbrandon.potter@amd.com} 128713568Sbrandon.potter@amd.com 128813568Sbrandon.potter@amd.comSyscallReturn 128913995Sbrandon.potter@amd.comlistenFunc(SyscallDesc *desc, int num, ThreadContext *tc) 129013568Sbrandon.potter@amd.com{ 129113568Sbrandon.potter@amd.com int index = 0; 129213995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 129313568Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 129413568Sbrandon.potter@amd.com int backlog = p->getSyscallArg(tc, index); 129513568Sbrandon.potter@amd.com 129613568Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 129713568Sbrandon.potter@amd.com if (!sfdp) 129813568Sbrandon.potter@amd.com return -EBADF; 129913568Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 130013568Sbrandon.potter@amd.com 130113568Sbrandon.potter@amd.com int status = listen(sim_fd, backlog); 130213568Sbrandon.potter@amd.com 130313568Sbrandon.potter@amd.com return (status == -1) ? -errno : status; 130413568Sbrandon.potter@amd.com} 130513568Sbrandon.potter@amd.com 130613568Sbrandon.potter@amd.comSyscallReturn 130713995Sbrandon.potter@amd.comconnectFunc(SyscallDesc *desc, int num, ThreadContext *tc) 130813568Sbrandon.potter@amd.com{ 130913568Sbrandon.potter@amd.com int index = 0; 131013995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 131113568Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 131213568Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 131313568Sbrandon.potter@amd.com int addrlen = p->getSyscallArg(tc, index); 131413568Sbrandon.potter@amd.com 131513568Sbrandon.potter@amd.com BufferArg addr(buf_ptr, addrlen); 131614024Sgabeblack@google.com addr.copyIn(tc->getVirtProxy()); 131713568Sbrandon.potter@amd.com 131813568Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 131913568Sbrandon.potter@amd.com if (!sfdp) 132013568Sbrandon.potter@amd.com return -EBADF; 132113568Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 132213568Sbrandon.potter@amd.com 132313568Sbrandon.potter@amd.com int status = connect(sim_fd, 132413568Sbrandon.potter@amd.com (struct sockaddr *)addr.bufferPtr(), 132513568Sbrandon.potter@amd.com (socklen_t)addrlen); 132613568Sbrandon.potter@amd.com 132713568Sbrandon.potter@amd.com return (status == -1) ? -errno : status; 132813568Sbrandon.potter@amd.com} 132913569Sbrandon.potter@amd.com 133013569Sbrandon.potter@amd.comSyscallReturn 133113995Sbrandon.potter@amd.comrecvfromFunc(SyscallDesc *desc, int num, ThreadContext *tc) 133213569Sbrandon.potter@amd.com{ 133313569Sbrandon.potter@amd.com int index = 0; 133413995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 133513569Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 133613569Sbrandon.potter@amd.com Addr bufrPtr = p->getSyscallArg(tc, index); 133713569Sbrandon.potter@amd.com size_t bufrLen = p->getSyscallArg(tc, index); 133813569Sbrandon.potter@amd.com int flags = p->getSyscallArg(tc, index); 133913569Sbrandon.potter@amd.com Addr addrPtr = p->getSyscallArg(tc, index); 134013569Sbrandon.potter@amd.com Addr addrlenPtr = p->getSyscallArg(tc, index); 134113569Sbrandon.potter@amd.com 134213569Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 134313569Sbrandon.potter@amd.com if (!sfdp) 134413569Sbrandon.potter@amd.com return -EBADF; 134513569Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 134613569Sbrandon.potter@amd.com 134713569Sbrandon.potter@amd.com // Reserve buffer space. 134813569Sbrandon.potter@amd.com BufferArg bufrBuf(bufrPtr, bufrLen); 134913569Sbrandon.potter@amd.com 135013569Sbrandon.potter@amd.com // Get address length. 135113569Sbrandon.potter@amd.com socklen_t addrLen = 0; 135213569Sbrandon.potter@amd.com if (addrlenPtr != 0) { 135313569Sbrandon.potter@amd.com // Read address length parameter. 135413569Sbrandon.potter@amd.com BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t)); 135514024Sgabeblack@google.com addrlenBuf.copyIn(tc->getVirtProxy()); 135613569Sbrandon.potter@amd.com addrLen = *((socklen_t *)addrlenBuf.bufferPtr()); 135713569Sbrandon.potter@amd.com } 135813569Sbrandon.potter@amd.com 135913569Sbrandon.potter@amd.com struct sockaddr sa, *sap = NULL; 136013569Sbrandon.potter@amd.com if (addrLen != 0) { 136113569Sbrandon.potter@amd.com BufferArg addrBuf(addrPtr, addrLen); 136214024Sgabeblack@google.com addrBuf.copyIn(tc->getVirtProxy()); 136313569Sbrandon.potter@amd.com memcpy(&sa, (struct sockaddr *)addrBuf.bufferPtr(), 136413569Sbrandon.potter@amd.com sizeof(struct sockaddr)); 136513569Sbrandon.potter@amd.com sap = &sa; 136613569Sbrandon.potter@amd.com } 136713569Sbrandon.potter@amd.com 136813569Sbrandon.potter@amd.com ssize_t recvd_size = recvfrom(sim_fd, 136913569Sbrandon.potter@amd.com (void *)bufrBuf.bufferPtr(), 137013569Sbrandon.potter@amd.com bufrLen, flags, sap, (socklen_t *)&addrLen); 137113569Sbrandon.potter@amd.com 137213569Sbrandon.potter@amd.com if (recvd_size == -1) 137313569Sbrandon.potter@amd.com return -errno; 137413569Sbrandon.potter@amd.com 137513569Sbrandon.potter@amd.com // Pass the received data out. 137614024Sgabeblack@google.com bufrBuf.copyOut(tc->getVirtProxy()); 137713569Sbrandon.potter@amd.com 137813569Sbrandon.potter@amd.com // Copy address to addrPtr and pass it on. 137913569Sbrandon.potter@amd.com if (sap != NULL) { 138013569Sbrandon.potter@amd.com BufferArg addrBuf(addrPtr, addrLen); 138113569Sbrandon.potter@amd.com memcpy(addrBuf.bufferPtr(), sap, sizeof(sa)); 138214024Sgabeblack@google.com addrBuf.copyOut(tc->getVirtProxy()); 138313569Sbrandon.potter@amd.com } 138413569Sbrandon.potter@amd.com 138513569Sbrandon.potter@amd.com // Copy len to addrlenPtr and pass it on. 138613569Sbrandon.potter@amd.com if (addrLen != 0) { 138713569Sbrandon.potter@amd.com BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t)); 138813569Sbrandon.potter@amd.com *(socklen_t *)addrlenBuf.bufferPtr() = addrLen; 138914024Sgabeblack@google.com addrlenBuf.copyOut(tc->getVirtProxy()); 139013569Sbrandon.potter@amd.com } 139113569Sbrandon.potter@amd.com 139213569Sbrandon.potter@amd.com return recvd_size; 139313569Sbrandon.potter@amd.com} 139413569Sbrandon.potter@amd.com 139513569Sbrandon.potter@amd.comSyscallReturn 139613995Sbrandon.potter@amd.comsendtoFunc(SyscallDesc *desc, int num, ThreadContext *tc) 139713569Sbrandon.potter@amd.com{ 139813569Sbrandon.potter@amd.com int index = 0; 139913995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 140013569Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 140113569Sbrandon.potter@amd.com Addr bufrPtr = p->getSyscallArg(tc, index); 140213569Sbrandon.potter@amd.com size_t bufrLen = p->getSyscallArg(tc, index); 140313569Sbrandon.potter@amd.com int flags = p->getSyscallArg(tc, index); 140413569Sbrandon.potter@amd.com Addr addrPtr = p->getSyscallArg(tc, index); 140513569Sbrandon.potter@amd.com socklen_t addrLen = p->getSyscallArg(tc, index); 140613569Sbrandon.potter@amd.com 140713569Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 140813569Sbrandon.potter@amd.com if (!sfdp) 140913569Sbrandon.potter@amd.com return -EBADF; 141013569Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 141113569Sbrandon.potter@amd.com 141213569Sbrandon.potter@amd.com // Reserve buffer space. 141313569Sbrandon.potter@amd.com BufferArg bufrBuf(bufrPtr, bufrLen); 141414024Sgabeblack@google.com bufrBuf.copyIn(tc->getVirtProxy()); 141513569Sbrandon.potter@amd.com 141613569Sbrandon.potter@amd.com struct sockaddr sa, *sap = nullptr; 141713569Sbrandon.potter@amd.com memset(&sa, 0, sizeof(sockaddr)); 141813569Sbrandon.potter@amd.com if (addrLen != 0) { 141913569Sbrandon.potter@amd.com BufferArg addrBuf(addrPtr, addrLen); 142014024Sgabeblack@google.com addrBuf.copyIn(tc->getVirtProxy()); 142113569Sbrandon.potter@amd.com memcpy(&sa, (sockaddr*)addrBuf.bufferPtr(), addrLen); 142213569Sbrandon.potter@amd.com sap = &sa; 142313569Sbrandon.potter@amd.com } 142413569Sbrandon.potter@amd.com 142513569Sbrandon.potter@amd.com ssize_t sent_size = sendto(sim_fd, 142613569Sbrandon.potter@amd.com (void *)bufrBuf.bufferPtr(), 142713569Sbrandon.potter@amd.com bufrLen, flags, sap, (socklen_t)addrLen); 142813569Sbrandon.potter@amd.com 142913569Sbrandon.potter@amd.com return (sent_size == -1) ? -errno : sent_size; 143013569Sbrandon.potter@amd.com} 143113569Sbrandon.potter@amd.com 143213569Sbrandon.potter@amd.comSyscallReturn 143313995Sbrandon.potter@amd.comrecvmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc) 143413569Sbrandon.potter@amd.com{ 143513569Sbrandon.potter@amd.com int index = 0; 143613995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 143713569Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 143813569Sbrandon.potter@amd.com Addr msgPtr = p->getSyscallArg(tc, index); 143913569Sbrandon.potter@amd.com int flags = p->getSyscallArg(tc, index); 144013569Sbrandon.potter@amd.com 144113569Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 144213569Sbrandon.potter@amd.com if (!sfdp) 144313569Sbrandon.potter@amd.com return -EBADF; 144413569Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 144513569Sbrandon.potter@amd.com 144613569Sbrandon.potter@amd.com /** 144713569Sbrandon.potter@amd.com * struct msghdr { 144813569Sbrandon.potter@amd.com * void *msg_name; // optional address 144913569Sbrandon.potter@amd.com * socklen_t msg_namelen; // size of address 145013569Sbrandon.potter@amd.com * struct iovec *msg_iov; // iovec array 145113569Sbrandon.potter@amd.com * size_t msg_iovlen; // number entries in msg_iov 145213569Sbrandon.potter@amd.com * i // entries correspond to buffer 145313569Sbrandon.potter@amd.com * void *msg_control; // ancillary data 145413569Sbrandon.potter@amd.com * size_t msg_controllen; // ancillary data buffer len 145513569Sbrandon.potter@amd.com * int msg_flags; // flags on received message 145613569Sbrandon.potter@amd.com * }; 145713569Sbrandon.potter@amd.com * 145813569Sbrandon.potter@amd.com * struct iovec { 145913569Sbrandon.potter@amd.com * void *iov_base; // starting address 146013569Sbrandon.potter@amd.com * size_t iov_len; // number of bytes to transfer 146113569Sbrandon.potter@amd.com * }; 146213569Sbrandon.potter@amd.com */ 146313569Sbrandon.potter@amd.com 146413569Sbrandon.potter@amd.com /** 146513569Sbrandon.potter@amd.com * The plan with this system call is to replace all of the pointers in the 146613569Sbrandon.potter@amd.com * structure and the substructure with BufferArg class pointers. We will 146713569Sbrandon.potter@amd.com * copy every field from the structures into our BufferArg classes. 146813569Sbrandon.potter@amd.com */ 146913569Sbrandon.potter@amd.com BufferArg msgBuf(msgPtr, sizeof(struct msghdr)); 147014024Sgabeblack@google.com msgBuf.copyIn(tc->getVirtProxy()); 147113569Sbrandon.potter@amd.com struct msghdr *msgHdr = (struct msghdr *)msgBuf.bufferPtr(); 147213569Sbrandon.potter@amd.com 147313569Sbrandon.potter@amd.com /** 147413569Sbrandon.potter@amd.com * We will use these address place holders to retain the pointers which 147513569Sbrandon.potter@amd.com * we are going to replace with our own buffers in our simulator address 147613569Sbrandon.potter@amd.com * space. 147713569Sbrandon.potter@amd.com */ 147813569Sbrandon.potter@amd.com Addr msg_name_phold = 0; 147913569Sbrandon.potter@amd.com Addr msg_iov_phold = 0; 148013569Sbrandon.potter@amd.com Addr iovec_base_phold[msgHdr->msg_iovlen]; 148113569Sbrandon.potter@amd.com Addr msg_control_phold = 0; 148213569Sbrandon.potter@amd.com 148313569Sbrandon.potter@amd.com /** 148413569Sbrandon.potter@amd.com * Record msg_name pointer then replace with buffer pointer. 148513569Sbrandon.potter@amd.com */ 148613569Sbrandon.potter@amd.com BufferArg *nameBuf = NULL; 148713569Sbrandon.potter@amd.com if (msgHdr->msg_name) { 148813569Sbrandon.potter@amd.com /*1*/msg_name_phold = (Addr)msgHdr->msg_name; 148913569Sbrandon.potter@amd.com /*2*/nameBuf = new BufferArg(msg_name_phold, msgHdr->msg_namelen); 149014024Sgabeblack@google.com /*3*/nameBuf->copyIn(tc->getVirtProxy()); 149113569Sbrandon.potter@amd.com /*4*/msgHdr->msg_name = nameBuf->bufferPtr(); 149213569Sbrandon.potter@amd.com } 149313569Sbrandon.potter@amd.com 149413569Sbrandon.potter@amd.com /** 149513569Sbrandon.potter@amd.com * Record msg_iov pointer then replace with buffer pointer. Also, setup 149613569Sbrandon.potter@amd.com * an array of buffer pointers for the iovec structs record and replace 149713569Sbrandon.potter@amd.com * their pointers with buffer pointers. 149813569Sbrandon.potter@amd.com */ 149913569Sbrandon.potter@amd.com BufferArg *iovBuf = NULL; 150013569Sbrandon.potter@amd.com BufferArg *iovecBuf[msgHdr->msg_iovlen]; 150113569Sbrandon.potter@amd.com for (int i = 0; i < msgHdr->msg_iovlen; i++) { 150213569Sbrandon.potter@amd.com iovec_base_phold[i] = 0; 150313569Sbrandon.potter@amd.com iovecBuf[i] = NULL; 150413569Sbrandon.potter@amd.com } 150513569Sbrandon.potter@amd.com 150613569Sbrandon.potter@amd.com if (msgHdr->msg_iov) { 150713569Sbrandon.potter@amd.com /*1*/msg_iov_phold = (Addr)msgHdr->msg_iov; 150813569Sbrandon.potter@amd.com /*2*/iovBuf = new BufferArg(msg_iov_phold, msgHdr->msg_iovlen * 150913569Sbrandon.potter@amd.com sizeof(struct iovec)); 151014024Sgabeblack@google.com /*3*/iovBuf->copyIn(tc->getVirtProxy()); 151113569Sbrandon.potter@amd.com for (int i = 0; i < msgHdr->msg_iovlen; i++) { 151213569Sbrandon.potter@amd.com if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) { 151313569Sbrandon.potter@amd.com /*1*/iovec_base_phold[i] = 151413569Sbrandon.potter@amd.com (Addr)((struct iovec *)iovBuf->bufferPtr())[i].iov_base; 151513569Sbrandon.potter@amd.com /*2*/iovecBuf[i] = new BufferArg(iovec_base_phold[i], 151613569Sbrandon.potter@amd.com ((struct iovec *)iovBuf->bufferPtr())[i].iov_len); 151714024Sgabeblack@google.com /*3*/iovecBuf[i]->copyIn(tc->getVirtProxy()); 151813569Sbrandon.potter@amd.com /*4*/((struct iovec *)iovBuf->bufferPtr())[i].iov_base = 151913569Sbrandon.potter@amd.com iovecBuf[i]->bufferPtr(); 152013569Sbrandon.potter@amd.com } 152113569Sbrandon.potter@amd.com } 152213569Sbrandon.potter@amd.com /*4*/msgHdr->msg_iov = (struct iovec *)iovBuf->bufferPtr(); 152313569Sbrandon.potter@amd.com } 152413569Sbrandon.potter@amd.com 152513569Sbrandon.potter@amd.com /** 152613569Sbrandon.potter@amd.com * Record msg_control pointer then replace with buffer pointer. 152713569Sbrandon.potter@amd.com */ 152813569Sbrandon.potter@amd.com BufferArg *controlBuf = NULL; 152913569Sbrandon.potter@amd.com if (msgHdr->msg_control) { 153013569Sbrandon.potter@amd.com /*1*/msg_control_phold = (Addr)msgHdr->msg_control; 153113569Sbrandon.potter@amd.com /*2*/controlBuf = new BufferArg(msg_control_phold, 153213569Sbrandon.potter@amd.com CMSG_ALIGN(msgHdr->msg_controllen)); 153314024Sgabeblack@google.com /*3*/controlBuf->copyIn(tc->getVirtProxy()); 153413569Sbrandon.potter@amd.com /*4*/msgHdr->msg_control = controlBuf->bufferPtr(); 153513569Sbrandon.potter@amd.com } 153613569Sbrandon.potter@amd.com 153713569Sbrandon.potter@amd.com ssize_t recvd_size = recvmsg(sim_fd, msgHdr, flags); 153813569Sbrandon.potter@amd.com 153913569Sbrandon.potter@amd.com if (recvd_size < 0) 154013569Sbrandon.potter@amd.com return -errno; 154113569Sbrandon.potter@amd.com 154213569Sbrandon.potter@amd.com if (msgHdr->msg_name) { 154314024Sgabeblack@google.com nameBuf->copyOut(tc->getVirtProxy()); 154413569Sbrandon.potter@amd.com delete(nameBuf); 154513569Sbrandon.potter@amd.com msgHdr->msg_name = (void *)msg_name_phold; 154613569Sbrandon.potter@amd.com } 154713569Sbrandon.potter@amd.com 154813569Sbrandon.potter@amd.com if (msgHdr->msg_iov) { 154913569Sbrandon.potter@amd.com for (int i = 0; i< msgHdr->msg_iovlen; i++) { 155013569Sbrandon.potter@amd.com if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) { 155114024Sgabeblack@google.com iovecBuf[i]->copyOut(tc->getVirtProxy()); 155213569Sbrandon.potter@amd.com delete iovecBuf[i]; 155313569Sbrandon.potter@amd.com ((struct iovec *)iovBuf->bufferPtr())[i].iov_base = 155413569Sbrandon.potter@amd.com (void *)iovec_base_phold[i]; 155513569Sbrandon.potter@amd.com } 155613569Sbrandon.potter@amd.com } 155714024Sgabeblack@google.com iovBuf->copyOut(tc->getVirtProxy()); 155813569Sbrandon.potter@amd.com delete iovBuf; 155913569Sbrandon.potter@amd.com msgHdr->msg_iov = (struct iovec *)msg_iov_phold; 156013569Sbrandon.potter@amd.com } 156113569Sbrandon.potter@amd.com 156213569Sbrandon.potter@amd.com if (msgHdr->msg_control) { 156314024Sgabeblack@google.com controlBuf->copyOut(tc->getVirtProxy()); 156413569Sbrandon.potter@amd.com delete(controlBuf); 156513569Sbrandon.potter@amd.com msgHdr->msg_control = (void *)msg_control_phold; 156613569Sbrandon.potter@amd.com } 156713569Sbrandon.potter@amd.com 156814024Sgabeblack@google.com msgBuf.copyOut(tc->getVirtProxy()); 156913569Sbrandon.potter@amd.com 157013569Sbrandon.potter@amd.com return recvd_size; 157113569Sbrandon.potter@amd.com} 157213569Sbrandon.potter@amd.com 157313569Sbrandon.potter@amd.comSyscallReturn 157413995Sbrandon.potter@amd.comsendmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc) 157513569Sbrandon.potter@amd.com{ 157613569Sbrandon.potter@amd.com int index = 0; 157713995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 157813569Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 157913569Sbrandon.potter@amd.com Addr msgPtr = p->getSyscallArg(tc, index); 158013569Sbrandon.potter@amd.com int flags = p->getSyscallArg(tc, index); 158113569Sbrandon.potter@amd.com 158213569Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 158313569Sbrandon.potter@amd.com if (!sfdp) 158413569Sbrandon.potter@amd.com return -EBADF; 158513569Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 158613569Sbrandon.potter@amd.com 158713569Sbrandon.potter@amd.com /** 158813569Sbrandon.potter@amd.com * Reserve buffer space. 158913569Sbrandon.potter@amd.com */ 159013569Sbrandon.potter@amd.com BufferArg msgBuf(msgPtr, sizeof(struct msghdr)); 159114024Sgabeblack@google.com msgBuf.copyIn(tc->getVirtProxy()); 159213569Sbrandon.potter@amd.com struct msghdr msgHdr = *((struct msghdr *)msgBuf.bufferPtr()); 159313569Sbrandon.potter@amd.com 159413569Sbrandon.potter@amd.com /** 159513569Sbrandon.potter@amd.com * Assuming msgHdr.msg_iovlen >= 1, then there is no point calling 159613569Sbrandon.potter@amd.com * recvmsg without a buffer. 159713569Sbrandon.potter@amd.com */ 159813569Sbrandon.potter@amd.com struct iovec *iovPtr = msgHdr.msg_iov; 159913569Sbrandon.potter@amd.com BufferArg iovBuf((Addr)iovPtr, sizeof(struct iovec) * msgHdr.msg_iovlen); 160014024Sgabeblack@google.com iovBuf.copyIn(tc->getVirtProxy()); 160113569Sbrandon.potter@amd.com struct iovec *iov = (struct iovec *)iovBuf.bufferPtr(); 160213569Sbrandon.potter@amd.com msgHdr.msg_iov = iov; 160313569Sbrandon.potter@amd.com 160413569Sbrandon.potter@amd.com /** 160513569Sbrandon.potter@amd.com * Cannot instantiate buffers till inside the loop. 160613569Sbrandon.potter@amd.com * Create array to hold buffer addresses, to be used during copyIn of 160713569Sbrandon.potter@amd.com * send data. 160813569Sbrandon.potter@amd.com */ 160913569Sbrandon.potter@amd.com BufferArg **bufferArray = (BufferArg **)malloc(msgHdr.msg_iovlen 161013569Sbrandon.potter@amd.com * sizeof(BufferArg *)); 161113569Sbrandon.potter@amd.com 161213569Sbrandon.potter@amd.com /** 161313569Sbrandon.potter@amd.com * Iterate through the iovec structures: 161413569Sbrandon.potter@amd.com * Get the base buffer addreses, reserve iov_len amount of space for each. 161513569Sbrandon.potter@amd.com * Put the buf address into the bufferArray for later retrieval. 161613569Sbrandon.potter@amd.com */ 161713569Sbrandon.potter@amd.com for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) { 161813569Sbrandon.potter@amd.com Addr basePtr = (Addr) iov[iovIndex].iov_base; 161913569Sbrandon.potter@amd.com bufferArray[iovIndex] = new BufferArg(basePtr, iov[iovIndex].iov_len); 162014024Sgabeblack@google.com bufferArray[iovIndex]->copyIn(tc->getVirtProxy()); 162113569Sbrandon.potter@amd.com iov[iovIndex].iov_base = bufferArray[iovIndex]->bufferPtr(); 162213569Sbrandon.potter@amd.com } 162313569Sbrandon.potter@amd.com 162413569Sbrandon.potter@amd.com ssize_t sent_size = sendmsg(sim_fd, &msgHdr, flags); 162513569Sbrandon.potter@amd.com int local_errno = errno; 162613569Sbrandon.potter@amd.com 162713569Sbrandon.potter@amd.com /** 162813569Sbrandon.potter@amd.com * Free dynamically allocated memory. 162913569Sbrandon.potter@amd.com */ 163013569Sbrandon.potter@amd.com for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) { 163113569Sbrandon.potter@amd.com BufferArg *baseBuf = ( BufferArg *)bufferArray[iovIndex]; 163213569Sbrandon.potter@amd.com delete(baseBuf); 163313569Sbrandon.potter@amd.com } 163413569Sbrandon.potter@amd.com 163513569Sbrandon.potter@amd.com /** 163613569Sbrandon.potter@amd.com * Malloced above. 163713569Sbrandon.potter@amd.com */ 163813569Sbrandon.potter@amd.com free(bufferArray); 163913569Sbrandon.potter@amd.com 164013569Sbrandon.potter@amd.com return (sent_size < 0) ? -local_errno : sent_size; 164113569Sbrandon.potter@amd.com} 164213569Sbrandon.potter@amd.com 164313571Sbrandon.potter@amd.comSyscallReturn 164413995Sbrandon.potter@amd.comgetsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc) 164513571Sbrandon.potter@amd.com{ 164613571Sbrandon.potter@amd.com // union of all possible return value types from getsockopt 164713571Sbrandon.potter@amd.com union val { 164813571Sbrandon.potter@amd.com int i_val; 164913571Sbrandon.potter@amd.com long l_val; 165013571Sbrandon.potter@amd.com struct linger linger_val; 165113571Sbrandon.potter@amd.com struct timeval timeval_val; 165213571Sbrandon.potter@amd.com } val; 165313571Sbrandon.potter@amd.com 165413571Sbrandon.potter@amd.com int index = 0; 165513995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 165613571Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 165713571Sbrandon.potter@amd.com int level = p->getSyscallArg(tc, index); 165813571Sbrandon.potter@amd.com int optname = p->getSyscallArg(tc, index); 165913571Sbrandon.potter@amd.com Addr valPtr = p->getSyscallArg(tc, index); 166013571Sbrandon.potter@amd.com Addr lenPtr = p->getSyscallArg(tc, index); 166113571Sbrandon.potter@amd.com 166213571Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 166313571Sbrandon.potter@amd.com if (!sfdp) 166413571Sbrandon.potter@amd.com return -EBADF; 166513571Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 166613571Sbrandon.potter@amd.com 166713571Sbrandon.potter@amd.com socklen_t len = sizeof(val); 166813571Sbrandon.potter@amd.com int status = getsockopt(sim_fd, level, optname, &val, &len); 166913571Sbrandon.potter@amd.com 167013571Sbrandon.potter@amd.com if (status == -1) 167113571Sbrandon.potter@amd.com return -errno; 167213571Sbrandon.potter@amd.com 167313571Sbrandon.potter@amd.com // copy val to valPtr and pass it on 167413571Sbrandon.potter@amd.com BufferArg valBuf(valPtr, sizeof(val)); 167513571Sbrandon.potter@amd.com memcpy(valBuf.bufferPtr(), &val, sizeof(val)); 167614024Sgabeblack@google.com valBuf.copyOut(tc->getVirtProxy()); 167713571Sbrandon.potter@amd.com 167813571Sbrandon.potter@amd.com // copy len to lenPtr and pass it on 167913571Sbrandon.potter@amd.com BufferArg lenBuf(lenPtr, sizeof(len)); 168013571Sbrandon.potter@amd.com memcpy(lenBuf.bufferPtr(), &len, sizeof(len)); 168114024Sgabeblack@google.com lenBuf.copyOut(tc->getVirtProxy()); 168213571Sbrandon.potter@amd.com 168313571Sbrandon.potter@amd.com return status; 168413571Sbrandon.potter@amd.com} 168513571Sbrandon.potter@amd.com 168613571Sbrandon.potter@amd.comSyscallReturn 168713995Sbrandon.potter@amd.comgetsocknameFunc(SyscallDesc *desc, int num, ThreadContext *tc) 168813571Sbrandon.potter@amd.com{ 168913571Sbrandon.potter@amd.com int index = 0; 169013995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 169113571Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 169213571Sbrandon.potter@amd.com Addr addrPtr = p->getSyscallArg(tc, index); 169313571Sbrandon.potter@amd.com Addr lenPtr = p->getSyscallArg(tc, index); 169413571Sbrandon.potter@amd.com 169513571Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 169613571Sbrandon.potter@amd.com if (!sfdp) 169713571Sbrandon.potter@amd.com return -EBADF; 169813571Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 169913571Sbrandon.potter@amd.com 170013571Sbrandon.potter@amd.com // lenPtr is an in-out paramenter: 170113571Sbrandon.potter@amd.com // sending the address length in, conveying the final length out 170213571Sbrandon.potter@amd.com 170313571Sbrandon.potter@amd.com // Read in the value of len from the passed pointer. 170413571Sbrandon.potter@amd.com BufferArg lenBuf(lenPtr, sizeof(socklen_t)); 170514024Sgabeblack@google.com lenBuf.copyIn(tc->getVirtProxy()); 170613571Sbrandon.potter@amd.com socklen_t len = *(socklen_t *)lenBuf.bufferPtr(); 170713571Sbrandon.potter@amd.com 170813571Sbrandon.potter@amd.com struct sockaddr sa; 170913571Sbrandon.potter@amd.com int status = getsockname(sim_fd, &sa, &len); 171013571Sbrandon.potter@amd.com 171113571Sbrandon.potter@amd.com if (status == -1) 171213571Sbrandon.potter@amd.com return -errno; 171313571Sbrandon.potter@amd.com 171413571Sbrandon.potter@amd.com // Copy address to addrPtr and pass it on. 171513571Sbrandon.potter@amd.com BufferArg addrBuf(addrPtr, sizeof(sa)); 171613571Sbrandon.potter@amd.com memcpy(addrBuf.bufferPtr(), &sa, sizeof(sa)); 171714024Sgabeblack@google.com addrBuf.copyOut(tc->getVirtProxy()); 171813571Sbrandon.potter@amd.com 171913571Sbrandon.potter@amd.com // Copy len to lenPtr and pass it on. 172013571Sbrandon.potter@amd.com *(socklen_t *)lenBuf.bufferPtr() = len; 172114024Sgabeblack@google.com lenBuf.copyOut(tc->getVirtProxy()); 172213571Sbrandon.potter@amd.com 172313571Sbrandon.potter@amd.com return status; 172413571Sbrandon.potter@amd.com} 172513571Sbrandon.potter@amd.com 172613571Sbrandon.potter@amd.comSyscallReturn 172713995Sbrandon.potter@amd.comgetpeernameFunc(SyscallDesc *desc, int num, ThreadContext *tc) 172813571Sbrandon.potter@amd.com{ 172913571Sbrandon.potter@amd.com int index = 0; 173013995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 173113571Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 173213571Sbrandon.potter@amd.com Addr sockAddrPtr = p->getSyscallArg(tc, index); 173313571Sbrandon.potter@amd.com Addr addrlenPtr = p->getSyscallArg(tc, index); 173413571Sbrandon.potter@amd.com 173513571Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 173613571Sbrandon.potter@amd.com if (!sfdp) 173713571Sbrandon.potter@amd.com return -EBADF; 173813571Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 173913571Sbrandon.potter@amd.com 174013571Sbrandon.potter@amd.com BufferArg bufAddrlen(addrlenPtr, sizeof(unsigned)); 174114024Sgabeblack@google.com bufAddrlen.copyIn(tc->getVirtProxy()); 174213571Sbrandon.potter@amd.com BufferArg bufSock(sockAddrPtr, *(unsigned *)bufAddrlen.bufferPtr()); 174313571Sbrandon.potter@amd.com 174413571Sbrandon.potter@amd.com int retval = getpeername(sim_fd, 174513571Sbrandon.potter@amd.com (struct sockaddr *)bufSock.bufferPtr(), 174613571Sbrandon.potter@amd.com (unsigned *)bufAddrlen.bufferPtr()); 174713571Sbrandon.potter@amd.com 174813571Sbrandon.potter@amd.com if (retval != -1) { 174914024Sgabeblack@google.com bufSock.copyOut(tc->getVirtProxy()); 175014024Sgabeblack@google.com bufAddrlen.copyOut(tc->getVirtProxy()); 175113571Sbrandon.potter@amd.com } 175213571Sbrandon.potter@amd.com 175313571Sbrandon.potter@amd.com return (retval == -1) ? -errno : retval; 175413571Sbrandon.potter@amd.com} 175513571Sbrandon.potter@amd.com 175613571Sbrandon.potter@amd.comSyscallReturn 175713995Sbrandon.potter@amd.comsetsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc) 175813571Sbrandon.potter@amd.com{ 175913571Sbrandon.potter@amd.com int index = 0; 176013995Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 176113571Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 176213571Sbrandon.potter@amd.com int level = p->getSyscallArg(tc, index); 176313571Sbrandon.potter@amd.com int optname = p->getSyscallArg(tc, index); 176413571Sbrandon.potter@amd.com Addr valPtr = p->getSyscallArg(tc, index); 176513571Sbrandon.potter@amd.com socklen_t len = p->getSyscallArg(tc, index); 176613571Sbrandon.potter@amd.com 176713571Sbrandon.potter@amd.com BufferArg valBuf(valPtr, len); 176814024Sgabeblack@google.com valBuf.copyIn(tc->getVirtProxy()); 176913571Sbrandon.potter@amd.com 177013571Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 177113571Sbrandon.potter@amd.com if (!sfdp) 177213571Sbrandon.potter@amd.com return -EBADF; 177313571Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 177413571Sbrandon.potter@amd.com 177513571Sbrandon.potter@amd.com int status = setsockopt(sim_fd, level, optname, 177613571Sbrandon.potter@amd.com (struct sockaddr *)valBuf.bufferPtr(), len); 177713571Sbrandon.potter@amd.com 177813571Sbrandon.potter@amd.com return (status == -1) ? -errno : status; 177913571Sbrandon.potter@amd.com} 178013571Sbrandon.potter@amd.com 1781