syscall_emul.cc revision 11793
1360SN/A/* 21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 3360SN/A * All rights reserved. 4360SN/A * 5360SN/A * Redistribution and use in source and binary forms, with or without 6360SN/A * modification, are permitted provided that the following conditions are 7360SN/A * met: redistributions of source code must retain the above copyright 8360SN/A * notice, this list of conditions and the following disclaimer; 9360SN/A * redistributions in binary form must reproduce the above copyright 10360SN/A * notice, this list of conditions and the following disclaimer in the 11360SN/A * documentation and/or other materials provided with the distribution; 12360SN/A * neither the name of the copyright holders nor the names of its 13360SN/A * contributors may be used to endorse or promote products derived from 14360SN/A * this software without specific prior written permission. 15360SN/A * 16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292665Ssaidi@eecs.umich.edu * Ali Saidi 30360SN/A */ 31360SN/A 3211793Sbrandon.potter@amd.com#include "sim/syscall_emul.hh" 3311793Sbrandon.potter@amd.com 342093SN/A#include <fcntl.h> 35360SN/A#include <unistd.h> 36360SN/A 376712Snate@binkert.org#include <cstdio> 386712Snate@binkert.org#include <iostream> 39360SN/A#include <string> 40360SN/A 417680Sgblack@eecs.umich.edu#include "arch/utility.hh" 422474SN/A#include "base/chunk_generator.hh" 43360SN/A#include "base/trace.hh" 446658Snate@binkert.org#include "config/the_isa.hh" 458229Snate@binkert.org#include "cpu/base.hh" 462680Sktlim@umich.edu#include "cpu/thread_context.hh" 4711380Salexandru.dutu@amd.com#include "debug/SyscallBase.hh" 488232Snate@binkert.org#include "debug/SyscallVerbose.hh" 492474SN/A#include "mem/page_table.hh" 50360SN/A#include "sim/process.hh" 518229Snate@binkert.org#include "sim/sim_exit.hh" 526029Ssteve.reinhardt@amd.com#include "sim/system.hh" 53360SN/A 54360SN/Ausing namespace std; 552107SN/Ausing namespace TheISA; 56360SN/A 57360SN/Avoid 583114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc) 59360SN/A{ 6011380Salexandru.dutu@amd.com if (DTRACE(SyscallBase)) { 6110253Ssteve.reinhardt@amd.com int index = 0; 6211380Salexandru.dutu@amd.com IntReg arg[6] M5_VAR_USED; 6310253Ssteve.reinhardt@amd.com 6410253Ssteve.reinhardt@amd.com // we can't just put the calls to getSyscallArg() in the 6510253Ssteve.reinhardt@amd.com // DPRINTF arg list, because C++ doesn't guarantee their order 6611380Salexandru.dutu@amd.com for (int i = 0; i < 6; ++i) 6710253Ssteve.reinhardt@amd.com arg[i] = process->getSyscallArg(tc, index); 6810253Ssteve.reinhardt@amd.com 6911380Salexandru.dutu@amd.com // Linux supports up to six system call arguments through registers 7011380Salexandru.dutu@amd.com // so we want to print all six. Check to the relevant man page to 7111380Salexandru.dutu@amd.com // verify how many are actually used by a given system call. 7211380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Base, 7311380Salexandru.dutu@amd.com "%s called w/arguments %d, %d, %d, %d, %d, %d\n", 7411380Salexandru.dutu@amd.com name, arg[0], arg[1], arg[2], arg[3], arg[4], 7511380Salexandru.dutu@amd.com arg[5]); 7610253Ssteve.reinhardt@amd.com } 77360SN/A 782680Sktlim@umich.edu SyscallReturn retval = (*funcPtr)(this, callnum, process, tc); 79360SN/A 8010500Ssteve.reinhardt@amd.com if (retval.needsRetry()) { 8111380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Base, "%s needs retry\n", name); 8210500Ssteve.reinhardt@amd.com } else { 8311380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Base, "%s returns %d\n", name, 8411380Salexandru.dutu@amd.com retval.encodedValue()); 8510500Ssteve.reinhardt@amd.com } 86360SN/A 8710500Ssteve.reinhardt@amd.com if (!(flags & SyscallDesc::SuppressReturnValue) && !retval.needsRetry()) 885958Sgblack@eecs.umich.edu process->setSyscallReturn(tc, retval); 89360SN/A} 90360SN/A 91360SN/A 921450SN/ASyscallReturn 933114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 942680Sktlim@umich.edu ThreadContext *tc) 95360SN/A{ 961969SN/A fatal("syscall %s (#%d) unimplemented.", desc->name, callnum); 972484SN/A 982484SN/A return 1; 99360SN/A} 100360SN/A 101360SN/A 1021450SN/ASyscallReturn 1033114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1042680Sktlim@umich.edu ThreadContext *tc) 105360SN/A{ 1066701Sgblack@eecs.umich.edu int index = 0; 10710831Ssteve.reinhardt@amd.com const char *extra_text = ""; 108360SN/A 10910831Ssteve.reinhardt@amd.com if (desc->warnOnce()) { 11010831Ssteve.reinhardt@amd.com if (desc->warned) 11110831Ssteve.reinhardt@amd.com return 0; 112360SN/A 11310831Ssteve.reinhardt@amd.com desc->warned = true; 11410831Ssteve.reinhardt@amd.com extra_text = "\n (further warnings will be suppressed)"; 11510831Ssteve.reinhardt@amd.com } 116360SN/A 11710831Ssteve.reinhardt@amd.com warn("ignoring syscall %s(%d, ...)%s", desc->name, 11810831Ssteve.reinhardt@amd.com process->getSyscallArg(tc, index), extra_text); 1198149SChris.Emmons@ARM.com 1208149SChris.Emmons@ARM.com return 0; 1218149SChris.Emmons@ARM.com} 1228149SChris.Emmons@ARM.com 1238149SChris.Emmons@ARM.com 1248149SChris.Emmons@ARM.comSyscallReturn 1253114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1262680Sktlim@umich.edu ThreadContext *tc) 127360SN/A{ 1286029Ssteve.reinhardt@amd.com if (process->system->numRunningContexts() == 1) { 1296029Ssteve.reinhardt@amd.com // Last running context... exit simulator 1306701Sgblack@eecs.umich.edu int index = 0; 1315958Sgblack@eecs.umich.edu exitSimLoop("target called exit()", 1326701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index) & 0xff); 1336029Ssteve.reinhardt@amd.com } else { 1346029Ssteve.reinhardt@amd.com // other running threads... just halt this one 1356029Ssteve.reinhardt@amd.com tc->halt(); 1362834Sksewell@umich.edu } 137360SN/A 1381458SN/A return 1; 139360SN/A} 140360SN/A 141360SN/A 1421450SN/ASyscallReturn 1436109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1446109Ssanchezd@stanford.edu ThreadContext *tc) 1456109Ssanchezd@stanford.edu{ 14610483Swiseveri@student.ethz.ch // halt all threads belonging to this process 14710483Swiseveri@student.ethz.ch for (auto i: process->contextIds) { 14810483Swiseveri@student.ethz.ch process->system->getThreadContext(i)->halt(); 14910483Swiseveri@student.ethz.ch } 15010483Swiseveri@student.ethz.ch 15110483Swiseveri@student.ethz.ch if (!process->system->numRunningContexts()) { 15210483Swiseveri@student.ethz.ch // all threads belonged to this process... exit simulator 15310483Swiseveri@student.ethz.ch int index = 0; 15410483Swiseveri@student.ethz.ch exitSimLoop("target called exit()", 15510483Swiseveri@student.ethz.ch process->getSyscallArg(tc, index) & 0xff); 15610483Swiseveri@student.ethz.ch } 1576109Ssanchezd@stanford.edu 1586109Ssanchezd@stanford.edu return 1; 1596109Ssanchezd@stanford.edu} 1606109Ssanchezd@stanford.edu 1616109Ssanchezd@stanford.edu 1626109Ssanchezd@stanford.eduSyscallReturn 1633114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 164360SN/A{ 16510318Sandreas.hansson@arm.com return (int)PageBytes; 166360SN/A} 167360SN/A 168360SN/A 1691450SN/ASyscallReturn 1705748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 171360SN/A{ 172360SN/A // change brk addr to first arg 1736701Sgblack@eecs.umich.edu int index = 0; 1746701Sgblack@eecs.umich.edu Addr new_brk = p->getSyscallArg(tc, index); 1755748SSteve.Reinhardt@amd.com 1765748SSteve.Reinhardt@amd.com // in Linux at least, brk(0) returns the current break value 1775748SSteve.Reinhardt@amd.com // (note that the syscall and the glibc function have different behavior) 1785748SSteve.Reinhardt@amd.com if (new_brk == 0) 1795748SSteve.Reinhardt@amd.com return p->brk_point; 1805748SSteve.Reinhardt@amd.com 1815748SSteve.Reinhardt@amd.com if (new_brk > p->brk_point) { 1825748SSteve.Reinhardt@amd.com // might need to allocate some new pages 1832474SN/A for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point, 18410318Sandreas.hansson@arm.com PageBytes); !gen.done(); gen.next()) { 1855748SSteve.Reinhardt@amd.com if (!p->pTable->translate(gen.addr())) 18610318Sandreas.hansson@arm.com p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes); 1876687Stjones1@inf.ed.ac.uk 1886687Stjones1@inf.ed.ac.uk // if the address is already there, zero it out 1896687Stjones1@inf.ed.ac.uk else { 1906687Stjones1@inf.ed.ac.uk uint8_t zero = 0; 1918852Sandreas.hansson@arm.com SETranslatingPortProxy &tp = tc->getMemProxy(); 1926687Stjones1@inf.ed.ac.uk 1936687Stjones1@inf.ed.ac.uk // split non-page aligned accesses 19410318Sandreas.hansson@arm.com Addr next_page = roundUp(gen.addr(), PageBytes); 1956687Stjones1@inf.ed.ac.uk uint32_t size_needed = next_page - gen.addr(); 1968852Sandreas.hansson@arm.com tp.memsetBlob(gen.addr(), zero, size_needed); 19710318Sandreas.hansson@arm.com if (gen.addr() + PageBytes > next_page && 1986687Stjones1@inf.ed.ac.uk next_page < new_brk && 1996687Stjones1@inf.ed.ac.uk p->pTable->translate(next_page)) 2006687Stjones1@inf.ed.ac.uk { 20110318Sandreas.hansson@arm.com size_needed = PageBytes - size_needed; 2028852Sandreas.hansson@arm.com tp.memsetBlob(next_page, zero, size_needed); 2036687Stjones1@inf.ed.ac.uk } 2046687Stjones1@inf.ed.ac.uk } 2052474SN/A } 2061450SN/A } 2075748SSteve.Reinhardt@amd.com 2085748SSteve.Reinhardt@amd.com p->brk_point = new_brk; 20911380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n", 21011380Salexandru.dutu@amd.com p->brk_point); 2111458SN/A return p->brk_point; 212360SN/A} 213360SN/A 214360SN/A 2151450SN/ASyscallReturn 2163114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 217360SN/A{ 2186701Sgblack@eecs.umich.edu int index = 0; 21910931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 22010931Sbrandon.potter@amd.com 22110932Sbrandon.potter@amd.com int sim_fd = p->getSimFD(tgt_fd); 22210931Sbrandon.potter@amd.com if (sim_fd < 0) 22310931Sbrandon.potter@amd.com return -EBADF; 22410931Sbrandon.potter@amd.com 2257508Stjones1@inf.ed.ac.uk int status = 0; 2267508Stjones1@inf.ed.ac.uk if (sim_fd > 2) 2277508Stjones1@inf.ed.ac.uk status = close(sim_fd); 2281970SN/A if (status >= 0) 22910932Sbrandon.potter@amd.com p->resetFDEntry(tgt_fd); 2301970SN/A return status; 231360SN/A} 232360SN/A 233360SN/A 2341450SN/ASyscallReturn 2353114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 236360SN/A{ 2376701Sgblack@eecs.umich.edu int index = 0; 23810931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 2396701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 2406701Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, index); 2416701Sgblack@eecs.umich.edu BufferArg bufArg(bufPtr, nbytes); 242360SN/A 24310932Sbrandon.potter@amd.com int sim_fd = p->getSimFD(tgt_fd); 24410931Sbrandon.potter@amd.com if (sim_fd < 0) 24510931Sbrandon.potter@amd.com return -EBADF; 24610931Sbrandon.potter@amd.com 24710931Sbrandon.potter@amd.com int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes); 248360SN/A 24911684Snderumigny@gmail.com if (bytes_read > 0) 2508706Sandreas.hansson@arm.com bufArg.copyOut(tc->getMemProxy()); 251360SN/A 2521458SN/A return bytes_read; 253360SN/A} 254360SN/A 2551450SN/ASyscallReturn 2563114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 257360SN/A{ 2586701Sgblack@eecs.umich.edu int index = 0; 25910931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 2606701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 2616701Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, index); 2626701Sgblack@eecs.umich.edu BufferArg bufArg(bufPtr, nbytes); 263360SN/A 26410932Sbrandon.potter@amd.com int sim_fd = p->getSimFD(tgt_fd); 26510931Sbrandon.potter@amd.com if (sim_fd < 0) 26610931Sbrandon.potter@amd.com return -EBADF; 26710931Sbrandon.potter@amd.com 2688706Sandreas.hansson@arm.com bufArg.copyIn(tc->getMemProxy()); 269360SN/A 27010931Sbrandon.potter@amd.com int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes); 271360SN/A 27210931Sbrandon.potter@amd.com fsync(sim_fd); 273360SN/A 2741458SN/A return bytes_written; 275360SN/A} 276360SN/A 277360SN/A 2781450SN/ASyscallReturn 2793114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 280360SN/A{ 2816701Sgblack@eecs.umich.edu int index = 0; 28210931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 2836701Sgblack@eecs.umich.edu uint64_t offs = p->getSyscallArg(tc, index); 2846701Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, index); 285360SN/A 28610932Sbrandon.potter@amd.com int sim_fd = p->getSimFD(tgt_fd); 28710931Sbrandon.potter@amd.com if (sim_fd < 0) 28810931Sbrandon.potter@amd.com return -EBADF; 28910931Sbrandon.potter@amd.com 29010931Sbrandon.potter@amd.com off_t result = lseek(sim_fd, offs, whence); 291360SN/A 2921458SN/A return (result == (off_t)-1) ? -errno : result; 293360SN/A} 294360SN/A 295360SN/A 2961450SN/ASyscallReturn 2974118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2984118Sgblack@eecs.umich.edu{ 2996701Sgblack@eecs.umich.edu int index = 0; 30010931Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 3016701Sgblack@eecs.umich.edu uint64_t offset_high = p->getSyscallArg(tc, index); 3026701Sgblack@eecs.umich.edu uint32_t offset_low = p->getSyscallArg(tc, index); 3036701Sgblack@eecs.umich.edu Addr result_ptr = p->getSyscallArg(tc, index); 3046701Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, index); 3054118Sgblack@eecs.umich.edu 30610932Sbrandon.potter@amd.com int sim_fd = p->getSimFD(tgt_fd); 30710931Sbrandon.potter@amd.com if (sim_fd < 0) 30810931Sbrandon.potter@amd.com return -EBADF; 30910931Sbrandon.potter@amd.com 3104118Sgblack@eecs.umich.edu uint64_t offset = (offset_high << 32) | offset_low; 3114118Sgblack@eecs.umich.edu 31210931Sbrandon.potter@amd.com uint64_t result = lseek(sim_fd, offset, whence); 3134118Sgblack@eecs.umich.edu result = TheISA::htog(result); 3144118Sgblack@eecs.umich.edu 31511379Sbrandon.potter@amd.com if (result == (off_t)-1) 3164118Sgblack@eecs.umich.edu return -errno; 31711379Sbrandon.potter@amd.com // Assuming that the size of loff_t is 64 bits on the target platform 31811379Sbrandon.potter@amd.com BufferArg result_buf(result_ptr, sizeof(result)); 31911379Sbrandon.potter@amd.com memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 32011379Sbrandon.potter@amd.com result_buf.copyOut(tc->getMemProxy()); 32111379Sbrandon.potter@amd.com return 0; 3224118Sgblack@eecs.umich.edu} 3234118Sgblack@eecs.umich.edu 3244118Sgblack@eecs.umich.edu 3254118Sgblack@eecs.umich.eduSyscallReturn 3263114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 327360SN/A{ 32811383Sbrandon.potter@amd.com // With mmap more fully implemented, it might be worthwhile to bite 32911383Sbrandon.potter@amd.com // the bullet and implement munmap. Should allow us to reuse simulated 33011383Sbrandon.potter@amd.com // memory. 3311458SN/A return 0; 332360SN/A} 333360SN/A 334360SN/A 335360SN/Aconst char *hostname = "m5.eecs.umich.edu"; 336360SN/A 3371450SN/ASyscallReturn 3383114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 339360SN/A{ 3406701Sgblack@eecs.umich.edu int index = 0; 3416701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 3426701Sgblack@eecs.umich.edu int name_len = p->getSyscallArg(tc, index); 3436701Sgblack@eecs.umich.edu BufferArg name(bufPtr, name_len); 344360SN/A 345360SN/A strncpy((char *)name.bufferPtr(), hostname, name_len); 346360SN/A 3478706Sandreas.hansson@arm.com name.copyOut(tc->getMemProxy()); 348360SN/A 3491458SN/A return 0; 350360SN/A} 351360SN/A 3521450SN/ASyscallReturn 3535513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3545513SMichael.Adler@intel.com{ 3555513SMichael.Adler@intel.com int result = 0; 3566731Svince@csl.cornell.edu int index = 0; 3576701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 3586701Sgblack@eecs.umich.edu unsigned long size = p->getSyscallArg(tc, index); 3596701Sgblack@eecs.umich.edu BufferArg buf(bufPtr, size); 3605513SMichael.Adler@intel.com 3615513SMichael.Adler@intel.com // Is current working directory defined? 3625513SMichael.Adler@intel.com string cwd = p->getcwd(); 3635513SMichael.Adler@intel.com if (!cwd.empty()) { 3645513SMichael.Adler@intel.com if (cwd.length() >= size) { 3655513SMichael.Adler@intel.com // Buffer too small 3665513SMichael.Adler@intel.com return -ERANGE; 3675513SMichael.Adler@intel.com } 3685513SMichael.Adler@intel.com strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 3695513SMichael.Adler@intel.com result = cwd.length(); 37010955Sdavid.hashe@amd.com } else { 3715513SMichael.Adler@intel.com if (getcwd((char *)buf.bufferPtr(), size) != NULL) { 3725513SMichael.Adler@intel.com result = strlen((char *)buf.bufferPtr()); 37310955Sdavid.hashe@amd.com } else { 3745513SMichael.Adler@intel.com result = -1; 3755513SMichael.Adler@intel.com } 3765513SMichael.Adler@intel.com } 3775513SMichael.Adler@intel.com 3788706Sandreas.hansson@arm.com buf.copyOut(tc->getMemProxy()); 3795513SMichael.Adler@intel.com 3805513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3815513SMichael.Adler@intel.com} 3825513SMichael.Adler@intel.com 38310203SAli.Saidi@ARM.com/// Target open() handler. 38410203SAli.Saidi@ARM.comSyscallReturn 38510203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 38610203SAli.Saidi@ARM.com ThreadContext *tc) 38710203SAli.Saidi@ARM.com{ 38810203SAli.Saidi@ARM.com return readlinkFunc(desc, callnum, process, tc, 0); 38910203SAli.Saidi@ARM.com} 3905513SMichael.Adler@intel.com 3915513SMichael.Adler@intel.comSyscallReturn 39210203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc, 39310203SAli.Saidi@ARM.com int index) 3945513SMichael.Adler@intel.com{ 3955513SMichael.Adler@intel.com string path; 3965513SMichael.Adler@intel.com 3978852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 39810223Ssteve.reinhardt@amd.com return -EFAULT; 3995513SMichael.Adler@intel.com 4005513SMichael.Adler@intel.com // Adjust path for current working directory 4015513SMichael.Adler@intel.com path = p->fullPath(path); 4025513SMichael.Adler@intel.com 4036701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 4046701Sgblack@eecs.umich.edu size_t bufsiz = p->getSyscallArg(tc, index); 4056701Sgblack@eecs.umich.edu 4066701Sgblack@eecs.umich.edu BufferArg buf(bufPtr, bufsiz); 4075513SMichael.Adler@intel.com 40810955Sdavid.hashe@amd.com int result = -1; 40910955Sdavid.hashe@amd.com if (path != "/proc/self/exe") { 41010955Sdavid.hashe@amd.com result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 41110955Sdavid.hashe@amd.com } else { 41211140Sjthestness@gmail.com // Emulate readlink() called on '/proc/self/exe' should return the 41311140Sjthestness@gmail.com // absolute path of the binary running in the simulated system (the 41411140Sjthestness@gmail.com // LiveProcess' executable). It is possible that using this path in 41511140Sjthestness@gmail.com // the simulated system will result in unexpected behavior if: 41611140Sjthestness@gmail.com // 1) One binary runs another (e.g., -c time -o "my_binary"), and 41711140Sjthestness@gmail.com // called binary calls readlink(). 41811140Sjthestness@gmail.com // 2) The host's full path to the running benchmark changes from one 41911140Sjthestness@gmail.com // simulation to another. This can result in different simulated 42011140Sjthestness@gmail.com // performance since the simulated system will process the binary 42111140Sjthestness@gmail.com // path differently, even if the binary itself does not change. 42211140Sjthestness@gmail.com 42311140Sjthestness@gmail.com // Get the absolute canonical path to the running application 42411140Sjthestness@gmail.com char real_path[PATH_MAX]; 42511140Sjthestness@gmail.com char *check_real_path = realpath(p->progName(), real_path); 42611140Sjthestness@gmail.com if (!check_real_path) { 42711140Sjthestness@gmail.com fatal("readlink('/proc/self/exe') unable to resolve path to " 42811140Sjthestness@gmail.com "executable: %s", p->progName()); 42911140Sjthestness@gmail.com } 43011140Sjthestness@gmail.com strncpy((char*)buf.bufferPtr(), real_path, bufsiz); 43111140Sjthestness@gmail.com size_t real_path_len = strlen(real_path); 43211140Sjthestness@gmail.com if (real_path_len > bufsiz) { 43310955Sdavid.hashe@amd.com // readlink will truncate the contents of the 43410955Sdavid.hashe@amd.com // path to ensure it is no more than bufsiz 43510955Sdavid.hashe@amd.com result = bufsiz; 43610955Sdavid.hashe@amd.com } else { 43711140Sjthestness@gmail.com result = real_path_len; 43810955Sdavid.hashe@amd.com } 43911140Sjthestness@gmail.com 44011140Sjthestness@gmail.com // Issue a warning about potential unexpected results 44111140Sjthestness@gmail.com warn_once("readlink() called on '/proc/self/exe' may yield unexpected " 44211140Sjthestness@gmail.com "results in various settings.\n Returning '%s'\n", 44311140Sjthestness@gmail.com (char*)buf.bufferPtr()); 44410955Sdavid.hashe@amd.com } 4455513SMichael.Adler@intel.com 4468706Sandreas.hansson@arm.com buf.copyOut(tc->getMemProxy()); 4475513SMichael.Adler@intel.com 4485513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 4495513SMichael.Adler@intel.com} 4505513SMichael.Adler@intel.com 4515513SMichael.Adler@intel.comSyscallReturn 4523114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 453511SN/A{ 45410633Smichaelupton@gmail.com return unlinkHelper(desc, num, p, tc, 0); 45510633Smichaelupton@gmail.com} 45610633Smichaelupton@gmail.com 45710633Smichaelupton@gmail.comSyscallReturn 45810633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc, 45910633Smichaelupton@gmail.com int index) 46010633Smichaelupton@gmail.com{ 4611706SN/A string path; 462360SN/A 4638852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 46410223Ssteve.reinhardt@amd.com return -EFAULT; 465511SN/A 4663669Sbinkertn@umich.edu // Adjust path for current working directory 4673669Sbinkertn@umich.edu path = p->fullPath(path); 4683669Sbinkertn@umich.edu 469511SN/A int result = unlink(path.c_str()); 4701458SN/A return (result == -1) ? -errno : result; 471511SN/A} 472511SN/A 4735513SMichael.Adler@intel.com 4745513SMichael.Adler@intel.comSyscallReturn 4755513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 4765513SMichael.Adler@intel.com{ 4775513SMichael.Adler@intel.com string path; 4785513SMichael.Adler@intel.com 4796701Sgblack@eecs.umich.edu int index = 0; 4808852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 48110223Ssteve.reinhardt@amd.com return -EFAULT; 4825513SMichael.Adler@intel.com 4835513SMichael.Adler@intel.com // Adjust path for current working directory 4845513SMichael.Adler@intel.com path = p->fullPath(path); 4855513SMichael.Adler@intel.com 4866701Sgblack@eecs.umich.edu mode_t mode = p->getSyscallArg(tc, index); 4875513SMichael.Adler@intel.com 4885513SMichael.Adler@intel.com int result = mkdir(path.c_str(), mode); 4895513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 4905513SMichael.Adler@intel.com} 4915513SMichael.Adler@intel.com 4921450SN/ASyscallReturn 4933114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 494511SN/A{ 4951706SN/A string old_name; 496511SN/A 4976701Sgblack@eecs.umich.edu int index = 0; 4988852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index))) 4991458SN/A return -EFAULT; 500511SN/A 5011706SN/A string new_name; 502511SN/A 5038852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index))) 5041458SN/A return -EFAULT; 505511SN/A 5063669Sbinkertn@umich.edu // Adjust path for current working directory 5073669Sbinkertn@umich.edu old_name = p->fullPath(old_name); 5083669Sbinkertn@umich.edu new_name = p->fullPath(new_name); 5093669Sbinkertn@umich.edu 5101706SN/A int64_t result = rename(old_name.c_str(), new_name.c_str()); 5111458SN/A return (result == -1) ? -errno : result; 512511SN/A} 513511SN/A 5141706SN/ASyscallReturn 5153114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 5161706SN/A{ 5171706SN/A string path; 5181706SN/A 5196701Sgblack@eecs.umich.edu int index = 0; 5208852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 5211706SN/A return -EFAULT; 5221706SN/A 5236701Sgblack@eecs.umich.edu off_t length = p->getSyscallArg(tc, index); 5241706SN/A 5253669Sbinkertn@umich.edu // Adjust path for current working directory 5263669Sbinkertn@umich.edu path = p->fullPath(path); 5273669Sbinkertn@umich.edu 5281706SN/A int result = truncate(path.c_str(), length); 5291706SN/A return (result == -1) ? -errno : result; 5301706SN/A} 5311706SN/A 5321706SN/ASyscallReturn 5336111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num, 5346111Ssteve.reinhardt@amd.com LiveProcess *process, ThreadContext *tc) 5351706SN/A{ 5366701Sgblack@eecs.umich.edu int index = 0; 53710931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 53810931Sbrandon.potter@amd.com off_t length = process->getSyscallArg(tc, index); 5391706SN/A 54010932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 54110931Sbrandon.potter@amd.com if (sim_fd < 0) 5421706SN/A return -EBADF; 5431706SN/A 54410931Sbrandon.potter@amd.com int result = ftruncate(sim_fd, length); 5451706SN/A return (result == -1) ? -errno : result; 5461706SN/A} 5471999SN/A 5481999SN/ASyscallReturn 5496703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num, 5506703Svince@csl.cornell.edu LiveProcess *process, ThreadContext *tc) 5516703Svince@csl.cornell.edu{ 5526703Svince@csl.cornell.edu int index = 0; 5536703Svince@csl.cornell.edu string path; 5546703Svince@csl.cornell.edu 5558852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index))) 5566703Svince@csl.cornell.edu return -EFAULT; 5576703Svince@csl.cornell.edu 5586744SAli.Saidi@arm.com int64_t length = process->getSyscallArg(tc, index, 64); 5596703Svince@csl.cornell.edu 5606703Svince@csl.cornell.edu // Adjust path for current working directory 5616703Svince@csl.cornell.edu path = process->fullPath(path); 5626703Svince@csl.cornell.edu 5636744SAli.Saidi@arm.com#if NO_STAT64 5646744SAli.Saidi@arm.com int result = truncate(path.c_str(), length); 5656744SAli.Saidi@arm.com#else 5666703Svince@csl.cornell.edu int result = truncate64(path.c_str(), length); 5676744SAli.Saidi@arm.com#endif 5686703Svince@csl.cornell.edu return (result == -1) ? -errno : result; 5696703Svince@csl.cornell.edu} 5706703Svince@csl.cornell.edu 5716703Svince@csl.cornell.eduSyscallReturn 5726685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num, 5736685Stjones1@inf.ed.ac.uk LiveProcess *process, ThreadContext *tc) 5746685Stjones1@inf.ed.ac.uk{ 5756701Sgblack@eecs.umich.edu int index = 0; 57610931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 57710931Sbrandon.potter@amd.com int64_t length = process->getSyscallArg(tc, index, 64); 5786685Stjones1@inf.ed.ac.uk 57910932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 58010931Sbrandon.potter@amd.com if (sim_fd < 0) 5816685Stjones1@inf.ed.ac.uk return -EBADF; 5826685Stjones1@inf.ed.ac.uk 5836744SAli.Saidi@arm.com#if NO_STAT64 58410931Sbrandon.potter@amd.com int result = ftruncate(sim_fd, length); 5856744SAli.Saidi@arm.com#else 58610931Sbrandon.potter@amd.com int result = ftruncate64(sim_fd, length); 5876744SAli.Saidi@arm.com#endif 5886685Stjones1@inf.ed.ac.uk return (result == -1) ? -errno : result; 5896685Stjones1@inf.ed.ac.uk} 5906685Stjones1@inf.ed.ac.uk 5916685Stjones1@inf.ed.ac.ukSyscallReturn 5925513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 5935513SMichael.Adler@intel.com{ 5945513SMichael.Adler@intel.com // Letting the simulated program change the simulator's umask seems like 5955513SMichael.Adler@intel.com // a bad idea. Compromise by just returning the current umask but not 5965513SMichael.Adler@intel.com // changing anything. 5975513SMichael.Adler@intel.com mode_t oldMask = umask(0); 5985513SMichael.Adler@intel.com umask(oldMask); 5995521Snate@binkert.org return (int)oldMask; 6005513SMichael.Adler@intel.com} 6015513SMichael.Adler@intel.com 6025513SMichael.Adler@intel.comSyscallReturn 6033114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 6041999SN/A{ 6051999SN/A string path; 6061999SN/A 6076701Sgblack@eecs.umich.edu int index = 0; 6088852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 6091999SN/A return -EFAULT; 6101999SN/A 6111999SN/A /* XXX endianess */ 6126701Sgblack@eecs.umich.edu uint32_t owner = p->getSyscallArg(tc, index); 6131999SN/A uid_t hostOwner = owner; 6146701Sgblack@eecs.umich.edu uint32_t group = p->getSyscallArg(tc, index); 6151999SN/A gid_t hostGroup = group; 6161999SN/A 6173669Sbinkertn@umich.edu // Adjust path for current working directory 6183669Sbinkertn@umich.edu path = p->fullPath(path); 6193669Sbinkertn@umich.edu 6201999SN/A int result = chown(path.c_str(), hostOwner, hostGroup); 6211999SN/A return (result == -1) ? -errno : result; 6221999SN/A} 6231999SN/A 6241999SN/ASyscallReturn 6253114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 6261999SN/A{ 6276701Sgblack@eecs.umich.edu int index = 0; 62810931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 6291999SN/A 63010932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 63110931Sbrandon.potter@amd.com if (sim_fd < 0) 6321999SN/A return -EBADF; 6331999SN/A 6341999SN/A /* XXX endianess */ 6356701Sgblack@eecs.umich.edu uint32_t owner = process->getSyscallArg(tc, index); 6361999SN/A uid_t hostOwner = owner; 6376701Sgblack@eecs.umich.edu uint32_t group = process->getSyscallArg(tc, index); 6381999SN/A gid_t hostGroup = group; 6391999SN/A 64010931Sbrandon.potter@amd.com int result = fchown(sim_fd, hostOwner, hostGroup); 6411999SN/A return (result == -1) ? -errno : result; 6421999SN/A} 6432093SN/A 6442093SN/A 6452093SN/ASyscallReturn 6463114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 6473079Sstever@eecs.umich.edu{ 6486701Sgblack@eecs.umich.edu int index = 0; 64910781Snilay@cs.wisc.edu int tgt_fd = process->getSyscallArg(tc, index); 65010931Sbrandon.potter@amd.com 65110932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 65210781Snilay@cs.wisc.edu if (sim_fd < 0) 6533079Sstever@eecs.umich.edu return -EBADF; 6543079Sstever@eecs.umich.edu 65510932Sbrandon.potter@amd.com FDEntry *fde = process->getFDEntry(tgt_fd); 6565282Srstrong@cs.ucsd.edu 65710781Snilay@cs.wisc.edu int result = dup(sim_fd); 6586111Ssteve.reinhardt@amd.com return (result == -1) ? -errno : 65910932Sbrandon.potter@amd.com process->allocFD(result, fde->filename, fde->flags, fde->mode, false); 6603079Sstever@eecs.umich.edu} 6613079Sstever@eecs.umich.edu 6623079Sstever@eecs.umich.edu 6633079Sstever@eecs.umich.eduSyscallReturn 6643114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, 6652680Sktlim@umich.edu ThreadContext *tc) 6662093SN/A{ 6676701Sgblack@eecs.umich.edu int index = 0; 66810931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 6692093SN/A 67010932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 67110931Sbrandon.potter@amd.com if (sim_fd < 0) 6722093SN/A return -EBADF; 6732093SN/A 6746701Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, index); 6752093SN/A switch (cmd) { 6762093SN/A case 0: // F_DUPFD 6772093SN/A // if we really wanted to support this, we'd need to do it 6782093SN/A // in the target fd space. 67910931Sbrandon.potter@amd.com warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd); 6802093SN/A return -EMFILE; 6812093SN/A 6822093SN/A case 1: // F_GETFD (get close-on-exec flag) 6832093SN/A case 2: // F_SETFD (set close-on-exec flag) 6842093SN/A return 0; 6852093SN/A 6862093SN/A case 3: // F_GETFL (get file flags) 6872093SN/A case 4: // F_SETFL (set file flags) 6882093SN/A // not sure if this is totally valid, but we'll pass it through 6892093SN/A // to the underlying OS 69010931Sbrandon.potter@amd.com warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd); 69110931Sbrandon.potter@amd.com return fcntl(sim_fd, cmd); 6922093SN/A // return 0; 6932093SN/A 6942093SN/A case 7: // F_GETLK (get lock) 6952093SN/A case 8: // F_SETLK (set lock) 6962093SN/A case 9: // F_SETLKW (set lock and wait) 6972093SN/A // don't mess with file locking... just act like it's OK 69810931Sbrandon.potter@amd.com warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd); 6992093SN/A return 0; 7002093SN/A 7012093SN/A default: 7022093SN/A warn("Unknown fcntl command %d\n", cmd); 7032093SN/A return 0; 7042093SN/A } 7052093SN/A} 7062093SN/A 7072238SN/ASyscallReturn 7083114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process, 7092687Sksewell@umich.edu ThreadContext *tc) 7102687Sksewell@umich.edu{ 7116701Sgblack@eecs.umich.edu int index = 0; 71210931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 7132687Sksewell@umich.edu 71410932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 71510931Sbrandon.potter@amd.com if (sim_fd < 0) 7162687Sksewell@umich.edu return -EBADF; 7172687Sksewell@umich.edu 7186701Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, index); 7192687Sksewell@umich.edu switch (cmd) { 7202687Sksewell@umich.edu case 33: //F_GETLK64 72110931Sbrandon.potter@amd.com warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd); 7222687Sksewell@umich.edu return -EMFILE; 7232687Sksewell@umich.edu 7242687Sksewell@umich.edu case 34: // F_SETLK64 7252687Sksewell@umich.edu case 35: // F_SETLKW64 72610931Sbrandon.potter@amd.com warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", 72710931Sbrandon.potter@amd.com tgt_fd); 7282687Sksewell@umich.edu return -EMFILE; 7292687Sksewell@umich.edu 7302687Sksewell@umich.edu default: 7312687Sksewell@umich.edu // not sure if this is totally valid, but we'll pass it through 7322687Sksewell@umich.edu // to the underlying OS 73310931Sbrandon.potter@amd.com warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd); 73410931Sbrandon.potter@amd.com return fcntl(sim_fd, cmd); 7352687Sksewell@umich.edu // return 0; 7362687Sksewell@umich.edu } 7372687Sksewell@umich.edu} 7382687Sksewell@umich.edu 7392687Sksewell@umich.eduSyscallReturn 7403114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7412680Sktlim@umich.edu ThreadContext *tc) 7422238SN/A{ 7432238SN/A int fds[2], sim_fds[2]; 7442238SN/A int pipe_retval = pipe(fds); 7452093SN/A 7462238SN/A if (pipe_retval < 0) { 7472238SN/A // error 7482238SN/A return pipe_retval; 7492238SN/A } 7502238SN/A 75110932Sbrandon.potter@amd.com sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true); 75210932Sbrandon.potter@amd.com sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true); 7532238SN/A 7545282Srstrong@cs.ucsd.edu process->setReadPipeSource(sim_fds[0], sim_fds[1]); 7552238SN/A // Alpha Linux convention for pipe() is that fd[0] is returned as 7562238SN/A // the return value of the function, and fd[1] is returned in r20. 7572680Sktlim@umich.edu tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]); 7582238SN/A return sim_fds[0]; 7592238SN/A} 7602238SN/A 7612238SN/A 7622238SN/ASyscallReturn 7633114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7642680Sktlim@umich.edu ThreadContext *tc) 7652238SN/A{ 7662238SN/A // Make up a PID. There's no interprocess communication in 7672238SN/A // fake_syscall mode, so there's no way for a process to know it's 7682238SN/A // not getting a unique value. 7692238SN/A 7703114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 7713114Sgblack@eecs.umich.edu return process->pid(); 7722238SN/A} 7732238SN/A 7742238SN/A 7752238SN/ASyscallReturn 7763114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7772680Sktlim@umich.edu ThreadContext *tc) 7782238SN/A{ 7792238SN/A // Make up a UID and EUID... it shouldn't matter, and we want the 7802238SN/A // simulation to be deterministic. 7812238SN/A 7822238SN/A // EUID goes in r20. 7833114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID 7845543Ssaidi@eecs.umich.edu return process->uid(); // UID 7852238SN/A} 7862238SN/A 7872238SN/A 7882238SN/ASyscallReturn 7893114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7902680Sktlim@umich.edu ThreadContext *tc) 7912238SN/A{ 7922238SN/A // Get current group ID. EGID goes in r20. 7933114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID 7943114Sgblack@eecs.umich.edu return process->gid(); 7952238SN/A} 7962238SN/A 7972238SN/A 7982238SN/ASyscallReturn 7993114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8002680Sktlim@umich.edu ThreadContext *tc) 8012238SN/A{ 8022238SN/A // can't fathom why a benchmark would call this. 8036701Sgblack@eecs.umich.edu int index = 0; 8046701Sgblack@eecs.umich.edu warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index)); 8052238SN/A return 0; 8062238SN/A} 8072238SN/A 8082238SN/ASyscallReturn 8093114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8102680Sktlim@umich.edu ThreadContext *tc) 8112238SN/A{ 8122238SN/A // Make up a PID. There's no interprocess communication in 8132238SN/A // fake_syscall mode, so there's no way for a process to know it's 8142238SN/A // not getting a unique value. 8152238SN/A 8163114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID 8173114Sgblack@eecs.umich.edu return process->pid(); 8182238SN/A} 8192238SN/A 8202238SN/ASyscallReturn 8213114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8222680Sktlim@umich.edu ThreadContext *tc) 8232238SN/A{ 8243114Sgblack@eecs.umich.edu return process->ppid(); 8252238SN/A} 8262238SN/A 8272238SN/ASyscallReturn 8283114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8292680Sktlim@umich.edu ThreadContext *tc) 8302238SN/A{ 8315543Ssaidi@eecs.umich.edu return process->uid(); // UID 8322238SN/A} 8332238SN/A 8342238SN/ASyscallReturn 8353114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8362680Sktlim@umich.edu ThreadContext *tc) 8372238SN/A{ 8385543Ssaidi@eecs.umich.edu return process->euid(); // UID 8392238SN/A} 8402238SN/A 8412238SN/ASyscallReturn 8423114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8432680Sktlim@umich.edu ThreadContext *tc) 8442238SN/A{ 8453114Sgblack@eecs.umich.edu return process->gid(); 8462238SN/A} 8472238SN/A 8482238SN/ASyscallReturn 8493114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8502680Sktlim@umich.edu ThreadContext *tc) 8512238SN/A{ 8523114Sgblack@eecs.umich.edu return process->egid(); 8532238SN/A} 8542238SN/A 8552238SN/A 8566109Ssanchezd@stanford.eduSyscallReturn 8576109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8586109Ssanchezd@stanford.edu ThreadContext *tc) 8596109Ssanchezd@stanford.edu{ 8606701Sgblack@eecs.umich.edu int index = 0; 8616701Sgblack@eecs.umich.edu IntReg flags = process->getSyscallArg(tc, index); 8626701Sgblack@eecs.umich.edu IntReg newStack = process->getSyscallArg(tc, index); 8636701Sgblack@eecs.umich.edu 8646109Ssanchezd@stanford.edu DPRINTF(SyscallVerbose, "In sys_clone:\n"); 8656701Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, " Flags=%llx\n", flags); 8666701Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack); 8676109Ssanchezd@stanford.edu 8686109Ssanchezd@stanford.edu 8696701Sgblack@eecs.umich.edu if (flags != 0x10f00) { 8706111Ssteve.reinhardt@amd.com warn("This sys_clone implementation assumes flags " 8716111Ssteve.reinhardt@amd.com "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD " 8726111Ssteve.reinhardt@amd.com "(0x10f00), and may not work correctly with given flags " 8736701Sgblack@eecs.umich.edu "0x%llx\n", flags); 8746109Ssanchezd@stanford.edu } 8756109Ssanchezd@stanford.edu 8766111Ssteve.reinhardt@amd.com ThreadContext* ctc; // child thread context 8776109Ssanchezd@stanford.edu if ( ( ctc = process->findFreeContext() ) != NULL ) { 8786109Ssanchezd@stanford.edu DPRINTF(SyscallVerbose, " Found unallocated thread context\n"); 8796109Ssanchezd@stanford.edu 8806109Ssanchezd@stanford.edu ctc->clearArchRegs(); 8816109Ssanchezd@stanford.edu 8826111Ssteve.reinhardt@amd.com // Arch-specific cloning code 8836109Ssanchezd@stanford.edu #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA 8846111Ssteve.reinhardt@amd.com // Cloning the misc. regs for these archs is enough 8856109Ssanchezd@stanford.edu TheISA::copyMiscRegs(tc, ctc); 8866109Ssanchezd@stanford.edu #elif THE_ISA == SPARC_ISA 8876109Ssanchezd@stanford.edu TheISA::copyRegs(tc, ctc); 8886109Ssanchezd@stanford.edu 8896111Ssteve.reinhardt@amd.com // TODO: Explain what this code actually does :-) 8906109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 6, 0); 8916109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 4, 0); 8926109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2); 8936109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 5, NWindows); 8946337Sgblack@eecs.umich.edu ctc->setMiscReg(MISCREG_CWP, 0); 8956109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 7, 0); 8966109Ssanchezd@stanford.edu ctc->setMiscRegNoEffect(MISCREG_TL, 0); 8979375Sgblack@eecs.umich.edu ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY); 8986109Ssanchezd@stanford.edu 8996109Ssanchezd@stanford.edu for (int y = 8; y < 32; y++) 9006109Ssanchezd@stanford.edu ctc->setIntReg(y, tc->readIntReg(y)); 9018149SChris.Emmons@ARM.com #elif THE_ISA == ARM_ISA 9028149SChris.Emmons@ARM.com TheISA::copyRegs(tc, ctc); 9036109Ssanchezd@stanford.edu #else 9046109Ssanchezd@stanford.edu fatal("sys_clone is not implemented for this ISA\n"); 9056109Ssanchezd@stanford.edu #endif 9066109Ssanchezd@stanford.edu 9076111Ssteve.reinhardt@amd.com // Set up stack register 9086701Sgblack@eecs.umich.edu ctc->setIntReg(TheISA::StackPointerReg, newStack); 9096109Ssanchezd@stanford.edu 9106111Ssteve.reinhardt@amd.com // Set up syscall return values in parent and child 9116111Ssteve.reinhardt@amd.com ctc->setIntReg(ReturnValueReg, 0); // return value, child 9126109Ssanchezd@stanford.edu 9136111Ssteve.reinhardt@amd.com // Alpha needs SyscallSuccessReg=0 in child 9146109Ssanchezd@stanford.edu #if THE_ISA == ALPHA_ISA 9156110Ssteve.reinhardt@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 9166109Ssanchezd@stanford.edu #endif 9176109Ssanchezd@stanford.edu 9186111Ssteve.reinhardt@amd.com // In SPARC/Linux, clone returns 0 on pseudo-return register if 9196111Ssteve.reinhardt@amd.com // parent, non-zero if child 9206109Ssanchezd@stanford.edu #if THE_ISA == SPARC_ISA 9216109Ssanchezd@stanford.edu tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 9226109Ssanchezd@stanford.edu ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 9236109Ssanchezd@stanford.edu #endif 9246109Ssanchezd@stanford.edu 9257720Sgblack@eecs.umich.edu ctc->pcState(tc->nextInstAddr()); 9266109Ssanchezd@stanford.edu 9276109Ssanchezd@stanford.edu ctc->activate(); 9286109Ssanchezd@stanford.edu 9296109Ssanchezd@stanford.edu // Should return nonzero child TID in parent's syscall return register, 9306109Ssanchezd@stanford.edu // but for our pthread library any non-zero value will work 9316109Ssanchezd@stanford.edu return 1; 9326109Ssanchezd@stanford.edu } else { 9336109Ssanchezd@stanford.edu fatal("Called sys_clone, but no unallocated thread contexts found!\n"); 9346109Ssanchezd@stanford.edu return 0; 9356109Ssanchezd@stanford.edu } 9366109Ssanchezd@stanford.edu} 9376109Ssanchezd@stanford.edu 9389455Smitch.hayenga+gem5@gmail.comSyscallReturn 93911760Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 94011760Sbrandon.potter@amd.com ThreadContext *tc) 94111760Sbrandon.potter@amd.com{ 94211760Sbrandon.potter@amd.com int index = 0; 94311760Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 94411760Sbrandon.potter@amd.com int mode = process->getSyscallArg(tc, index); 94511760Sbrandon.potter@amd.com off_t offset = process->getSyscallArg(tc, index); 94611760Sbrandon.potter@amd.com off_t len = process->getSyscallArg(tc, index); 94711760Sbrandon.potter@amd.com 94811760Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 94911760Sbrandon.potter@amd.com if (sim_fd < 0) 95011760Sbrandon.potter@amd.com return -EBADF; 95111760Sbrandon.potter@amd.com 95211760Sbrandon.potter@amd.com int result = fallocate(sim_fd, mode, offset, len); 95311760Sbrandon.potter@amd.com if (result < 0) 95411760Sbrandon.potter@amd.com return -errno; 95511760Sbrandon.potter@amd.com 95611760Sbrandon.potter@amd.com return 0; 95711760Sbrandon.potter@amd.com} 95811760Sbrandon.potter@amd.com 95911760Sbrandon.potter@amd.comSyscallReturn 96010203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc, 96110203SAli.Saidi@ARM.com int index) 9629455Smitch.hayenga+gem5@gmail.com{ 9639455Smitch.hayenga+gem5@gmail.com string path; 9649455Smitch.hayenga+gem5@gmail.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 96510223Ssteve.reinhardt@amd.com return -EFAULT; 9669455Smitch.hayenga+gem5@gmail.com 9679455Smitch.hayenga+gem5@gmail.com // Adjust path for current working directory 9689455Smitch.hayenga+gem5@gmail.com path = p->fullPath(path); 9699455Smitch.hayenga+gem5@gmail.com 9709455Smitch.hayenga+gem5@gmail.com mode_t mode = p->getSyscallArg(tc, index); 9719455Smitch.hayenga+gem5@gmail.com 9729455Smitch.hayenga+gem5@gmail.com int result = access(path.c_str(), mode); 9739455Smitch.hayenga+gem5@gmail.com return (result == -1) ? -errno : result; 9749455Smitch.hayenga+gem5@gmail.com} 97510203SAli.Saidi@ARM.com 97610203SAli.Saidi@ARM.comSyscallReturn 97710203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc) 97810203SAli.Saidi@ARM.com{ 97910203SAli.Saidi@ARM.com return accessFunc(desc, callnum, p, tc, 0); 98010203SAli.Saidi@ARM.com} 98110203SAli.Saidi@ARM.com 982