syscall_emul.cc revision 7508
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 322093SN/A#include <fcntl.h> 33360SN/A#include <unistd.h> 34360SN/A 356712Snate@binkert.org#include <cstdio> 366712Snate@binkert.org#include <iostream> 37360SN/A#include <string> 38360SN/A 39360SN/A#include "sim/syscall_emul.hh" 402474SN/A#include "base/chunk_generator.hh" 41360SN/A#include "base/trace.hh" 426658Snate@binkert.org#include "config/the_isa.hh" 432680Sktlim@umich.edu#include "cpu/thread_context.hh" 441717SN/A#include "cpu/base.hh" 452474SN/A#include "mem/page_table.hh" 46360SN/A#include "sim/process.hh" 476029Ssteve.reinhardt@amd.com#include "sim/system.hh" 482667Sstever@eecs.umich.edu#include "sim/sim_exit.hh" 49360SN/A 50360SN/Ausing namespace std; 512107SN/Ausing namespace TheISA; 52360SN/A 53360SN/Avoid 543114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc) 55360SN/A{ 566702Sgblack@eecs.umich.edu#if TRACING_ON 576701Sgblack@eecs.umich.edu int index = 0; 586702Sgblack@eecs.umich.edu#endif 596111Ssteve.reinhardt@amd.com DPRINTFR(SyscallVerbose, 606111Ssteve.reinhardt@amd.com "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n", 616111Ssteve.reinhardt@amd.com curTick, tc->getCpuPtr()->name(), name, 626701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index), 636701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index), 646701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index), 656701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index)); 66360SN/A 672680Sktlim@umich.edu SyscallReturn retval = (*funcPtr)(this, callnum, process, tc); 68360SN/A 692495SN/A DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n", 702680Sktlim@umich.edu curTick,tc->getCpuPtr()->name(), name, retval.value()); 71360SN/A 721450SN/A if (!(flags & SyscallDesc::SuppressReturnValue)) 735958Sgblack@eecs.umich.edu process->setSyscallReturn(tc, retval); 74360SN/A} 75360SN/A 76360SN/A 771450SN/ASyscallReturn 783114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 792680Sktlim@umich.edu ThreadContext *tc) 80360SN/A{ 811969SN/A fatal("syscall %s (#%d) unimplemented.", desc->name, callnum); 822484SN/A 832484SN/A return 1; 84360SN/A} 85360SN/A 86360SN/A 871450SN/ASyscallReturn 883114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 892680Sktlim@umich.edu ThreadContext *tc) 90360SN/A{ 916701Sgblack@eecs.umich.edu int index = 0; 921969SN/A warn("ignoring syscall %s(%d, %d, ...)", desc->name, 936701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index), process->getSyscallArg(tc, index)); 94360SN/A 951458SN/A return 0; 96360SN/A} 97360SN/A 98360SN/A 991450SN/ASyscallReturn 1003114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1012680Sktlim@umich.edu ThreadContext *tc) 102360SN/A{ 1036029Ssteve.reinhardt@amd.com if (process->system->numRunningContexts() == 1) { 1046029Ssteve.reinhardt@amd.com // Last running context... exit simulator 1056701Sgblack@eecs.umich.edu int index = 0; 1065958Sgblack@eecs.umich.edu exitSimLoop("target called exit()", 1076701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index) & 0xff); 1086029Ssteve.reinhardt@amd.com } else { 1096029Ssteve.reinhardt@amd.com // other running threads... just halt this one 1106029Ssteve.reinhardt@amd.com tc->halt(); 1112834Sksewell@umich.edu } 112360SN/A 1131458SN/A return 1; 114360SN/A} 115360SN/A 116360SN/A 1171450SN/ASyscallReturn 1186109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1196109Ssanchezd@stanford.edu ThreadContext *tc) 1206109Ssanchezd@stanford.edu{ 1216109Ssanchezd@stanford.edu // really should just halt all thread contexts belonging to this 1226109Ssanchezd@stanford.edu // process in case there's another process running... 1236701Sgblack@eecs.umich.edu int index = 0; 1246109Ssanchezd@stanford.edu exitSimLoop("target called exit()", 1256701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index) & 0xff); 1266109Ssanchezd@stanford.edu 1276109Ssanchezd@stanford.edu return 1; 1286109Ssanchezd@stanford.edu} 1296109Ssanchezd@stanford.edu 1306109Ssanchezd@stanford.edu 1316109Ssanchezd@stanford.eduSyscallReturn 1323114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 133360SN/A{ 1342107SN/A return (int)VMPageSize; 135360SN/A} 136360SN/A 137360SN/A 1381450SN/ASyscallReturn 1395748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 140360SN/A{ 141360SN/A // change brk addr to first arg 1426701Sgblack@eecs.umich.edu int index = 0; 1436701Sgblack@eecs.umich.edu Addr new_brk = p->getSyscallArg(tc, index); 1445748SSteve.Reinhardt@amd.com 1455748SSteve.Reinhardt@amd.com // in Linux at least, brk(0) returns the current break value 1465748SSteve.Reinhardt@amd.com // (note that the syscall and the glibc function have different behavior) 1475748SSteve.Reinhardt@amd.com if (new_brk == 0) 1485748SSteve.Reinhardt@amd.com return p->brk_point; 1495748SSteve.Reinhardt@amd.com 1505748SSteve.Reinhardt@amd.com if (new_brk > p->brk_point) { 1515748SSteve.Reinhardt@amd.com // might need to allocate some new pages 1522474SN/A for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point, 1532474SN/A VMPageSize); !gen.done(); gen.next()) { 1545748SSteve.Reinhardt@amd.com if (!p->pTable->translate(gen.addr())) 1552474SN/A p->pTable->allocate(roundDown(gen.addr(), VMPageSize), 1562474SN/A VMPageSize); 1576687Stjones1@inf.ed.ac.uk 1586687Stjones1@inf.ed.ac.uk // if the address is already there, zero it out 1596687Stjones1@inf.ed.ac.uk else { 1606687Stjones1@inf.ed.ac.uk uint8_t zero = 0; 1616687Stjones1@inf.ed.ac.uk TranslatingPort *tp = tc->getMemPort(); 1626687Stjones1@inf.ed.ac.uk 1636687Stjones1@inf.ed.ac.uk // split non-page aligned accesses 1646687Stjones1@inf.ed.ac.uk Addr next_page = roundUp(gen.addr(), VMPageSize); 1656687Stjones1@inf.ed.ac.uk uint32_t size_needed = next_page - gen.addr(); 1666687Stjones1@inf.ed.ac.uk tp->memsetBlob(gen.addr(), zero, size_needed); 1676687Stjones1@inf.ed.ac.uk if (gen.addr() + VMPageSize > next_page && 1686687Stjones1@inf.ed.ac.uk next_page < new_brk && 1696687Stjones1@inf.ed.ac.uk p->pTable->translate(next_page)) 1706687Stjones1@inf.ed.ac.uk { 1716687Stjones1@inf.ed.ac.uk size_needed = VMPageSize - size_needed; 1726687Stjones1@inf.ed.ac.uk tp->memsetBlob(next_page, zero, size_needed); 1736687Stjones1@inf.ed.ac.uk } 1746687Stjones1@inf.ed.ac.uk } 1752474SN/A } 1761450SN/A } 1775748SSteve.Reinhardt@amd.com 1785748SSteve.Reinhardt@amd.com p->brk_point = new_brk; 1791458SN/A DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point); 1801458SN/A return p->brk_point; 181360SN/A} 182360SN/A 183360SN/A 1841450SN/ASyscallReturn 1853114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 186360SN/A{ 1876701Sgblack@eecs.umich.edu int index = 0; 1886701Sgblack@eecs.umich.edu int target_fd = p->getSyscallArg(tc, index); 1897508Stjones1@inf.ed.ac.uk int sim_fd = p->sim_fd(target_fd); 1907508Stjones1@inf.ed.ac.uk int status = 0; 1917508Stjones1@inf.ed.ac.uk if (sim_fd > 2) 1927508Stjones1@inf.ed.ac.uk status = close(sim_fd); 1931970SN/A if (status >= 0) 1941970SN/A p->free_fd(target_fd); 1951970SN/A return status; 196360SN/A} 197360SN/A 198360SN/A 1991450SN/ASyscallReturn 2003114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 201360SN/A{ 2026701Sgblack@eecs.umich.edu int index = 0; 2036701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2046701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 2056701Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, index); 2066701Sgblack@eecs.umich.edu BufferArg bufArg(bufPtr, nbytes); 207360SN/A 208360SN/A int bytes_read = read(fd, bufArg.bufferPtr(), nbytes); 209360SN/A 210360SN/A if (bytes_read != -1) 2112680Sktlim@umich.edu bufArg.copyOut(tc->getMemPort()); 212360SN/A 2131458SN/A return bytes_read; 214360SN/A} 215360SN/A 2161450SN/ASyscallReturn 2173114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 218360SN/A{ 2196701Sgblack@eecs.umich.edu int index = 0; 2206701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2216701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 2226701Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, index); 2236701Sgblack@eecs.umich.edu BufferArg bufArg(bufPtr, nbytes); 224360SN/A 2252680Sktlim@umich.edu bufArg.copyIn(tc->getMemPort()); 226360SN/A 227360SN/A int bytes_written = write(fd, bufArg.bufferPtr(), nbytes); 228360SN/A 229360SN/A fsync(fd); 230360SN/A 2311458SN/A return bytes_written; 232360SN/A} 233360SN/A 234360SN/A 2351450SN/ASyscallReturn 2363114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 237360SN/A{ 2386701Sgblack@eecs.umich.edu int index = 0; 2396701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2406701Sgblack@eecs.umich.edu uint64_t offs = p->getSyscallArg(tc, index); 2416701Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, index); 242360SN/A 243360SN/A off_t result = lseek(fd, offs, whence); 244360SN/A 2451458SN/A return (result == (off_t)-1) ? -errno : result; 246360SN/A} 247360SN/A 248360SN/A 2491450SN/ASyscallReturn 2504118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2514118Sgblack@eecs.umich.edu{ 2526701Sgblack@eecs.umich.edu int index = 0; 2536701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2546701Sgblack@eecs.umich.edu uint64_t offset_high = p->getSyscallArg(tc, index); 2556701Sgblack@eecs.umich.edu uint32_t offset_low = p->getSyscallArg(tc, index); 2566701Sgblack@eecs.umich.edu Addr result_ptr = p->getSyscallArg(tc, index); 2576701Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, index); 2584118Sgblack@eecs.umich.edu 2594118Sgblack@eecs.umich.edu uint64_t offset = (offset_high << 32) | offset_low; 2604118Sgblack@eecs.umich.edu 2614118Sgblack@eecs.umich.edu uint64_t result = lseek(fd, offset, whence); 2624118Sgblack@eecs.umich.edu result = TheISA::htog(result); 2634118Sgblack@eecs.umich.edu 2644118Sgblack@eecs.umich.edu if (result == (off_t)-1) { 2654118Sgblack@eecs.umich.edu //The seek failed. 2664118Sgblack@eecs.umich.edu return -errno; 2674118Sgblack@eecs.umich.edu } else { 2686111Ssteve.reinhardt@amd.com // The seek succeeded. 2696111Ssteve.reinhardt@amd.com // Copy "result" to "result_ptr" 2706111Ssteve.reinhardt@amd.com // XXX We'll assume that the size of loff_t is 64 bits on the 2716111Ssteve.reinhardt@amd.com // target platform 2724118Sgblack@eecs.umich.edu BufferArg result_buf(result_ptr, sizeof(result)); 2734118Sgblack@eecs.umich.edu memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 2744118Sgblack@eecs.umich.edu result_buf.copyOut(tc->getMemPort()); 2754118Sgblack@eecs.umich.edu return 0; 2764118Sgblack@eecs.umich.edu } 2774118Sgblack@eecs.umich.edu 2784118Sgblack@eecs.umich.edu 2794118Sgblack@eecs.umich.edu return (result == (off_t)-1) ? -errno : result; 2804118Sgblack@eecs.umich.edu} 2814118Sgblack@eecs.umich.edu 2824118Sgblack@eecs.umich.edu 2834118Sgblack@eecs.umich.eduSyscallReturn 2843114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 285360SN/A{ 286360SN/A // given that we don't really implement mmap, munmap is really easy 2871458SN/A return 0; 288360SN/A} 289360SN/A 290360SN/A 291360SN/Aconst char *hostname = "m5.eecs.umich.edu"; 292360SN/A 2931450SN/ASyscallReturn 2943114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 295360SN/A{ 2966701Sgblack@eecs.umich.edu int index = 0; 2976701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 2986701Sgblack@eecs.umich.edu int name_len = p->getSyscallArg(tc, index); 2996701Sgblack@eecs.umich.edu BufferArg name(bufPtr, name_len); 300360SN/A 301360SN/A strncpy((char *)name.bufferPtr(), hostname, name_len); 302360SN/A 3032680Sktlim@umich.edu name.copyOut(tc->getMemPort()); 304360SN/A 3051458SN/A return 0; 306360SN/A} 307360SN/A 3081450SN/ASyscallReturn 3095513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3105513SMichael.Adler@intel.com{ 3115513SMichael.Adler@intel.com int result = 0; 3126731Svince@csl.cornell.edu int index = 0; 3136701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 3146701Sgblack@eecs.umich.edu unsigned long size = p->getSyscallArg(tc, index); 3156701Sgblack@eecs.umich.edu BufferArg buf(bufPtr, size); 3165513SMichael.Adler@intel.com 3175513SMichael.Adler@intel.com // Is current working directory defined? 3185513SMichael.Adler@intel.com string cwd = p->getcwd(); 3195513SMichael.Adler@intel.com if (!cwd.empty()) { 3205513SMichael.Adler@intel.com if (cwd.length() >= size) { 3215513SMichael.Adler@intel.com // Buffer too small 3225513SMichael.Adler@intel.com return -ERANGE; 3235513SMichael.Adler@intel.com } 3245513SMichael.Adler@intel.com strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 3255513SMichael.Adler@intel.com result = cwd.length(); 3265513SMichael.Adler@intel.com } 3275513SMichael.Adler@intel.com else { 3285513SMichael.Adler@intel.com if (getcwd((char *)buf.bufferPtr(), size) != NULL) { 3295513SMichael.Adler@intel.com result = strlen((char *)buf.bufferPtr()); 3305513SMichael.Adler@intel.com } 3315513SMichael.Adler@intel.com else { 3325513SMichael.Adler@intel.com result = -1; 3335513SMichael.Adler@intel.com } 3345513SMichael.Adler@intel.com } 3355513SMichael.Adler@intel.com 3365513SMichael.Adler@intel.com buf.copyOut(tc->getMemPort()); 3375513SMichael.Adler@intel.com 3385513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3395513SMichael.Adler@intel.com} 3405513SMichael.Adler@intel.com 3415513SMichael.Adler@intel.com 3425513SMichael.Adler@intel.comSyscallReturn 3435513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3445513SMichael.Adler@intel.com{ 3455513SMichael.Adler@intel.com string path; 3465513SMichael.Adler@intel.com 3476701Sgblack@eecs.umich.edu int index = 0; 3486701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index))) 3495513SMichael.Adler@intel.com return (TheISA::IntReg)-EFAULT; 3505513SMichael.Adler@intel.com 3515513SMichael.Adler@intel.com // Adjust path for current working directory 3525513SMichael.Adler@intel.com path = p->fullPath(path); 3535513SMichael.Adler@intel.com 3546701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 3556701Sgblack@eecs.umich.edu size_t bufsiz = p->getSyscallArg(tc, index); 3566701Sgblack@eecs.umich.edu 3576701Sgblack@eecs.umich.edu BufferArg buf(bufPtr, bufsiz); 3585513SMichael.Adler@intel.com 3595513SMichael.Adler@intel.com int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 3605513SMichael.Adler@intel.com 3615513SMichael.Adler@intel.com buf.copyOut(tc->getMemPort()); 3625513SMichael.Adler@intel.com 3635513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3645513SMichael.Adler@intel.com} 3655513SMichael.Adler@intel.com 3665513SMichael.Adler@intel.comSyscallReturn 3673114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 368511SN/A{ 3691706SN/A string path; 370360SN/A 3716701Sgblack@eecs.umich.edu int index = 0; 3726701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index))) 3731450SN/A return (TheISA::IntReg)-EFAULT; 374511SN/A 3753669Sbinkertn@umich.edu // Adjust path for current working directory 3763669Sbinkertn@umich.edu path = p->fullPath(path); 3773669Sbinkertn@umich.edu 378511SN/A int result = unlink(path.c_str()); 3791458SN/A return (result == -1) ? -errno : result; 380511SN/A} 381511SN/A 3825513SMichael.Adler@intel.com 3835513SMichael.Adler@intel.comSyscallReturn 3845513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3855513SMichael.Adler@intel.com{ 3865513SMichael.Adler@intel.com string path; 3875513SMichael.Adler@intel.com 3886701Sgblack@eecs.umich.edu int index = 0; 3896701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index))) 3905513SMichael.Adler@intel.com return (TheISA::IntReg)-EFAULT; 3915513SMichael.Adler@intel.com 3925513SMichael.Adler@intel.com // Adjust path for current working directory 3935513SMichael.Adler@intel.com path = p->fullPath(path); 3945513SMichael.Adler@intel.com 3956701Sgblack@eecs.umich.edu mode_t mode = p->getSyscallArg(tc, index); 3965513SMichael.Adler@intel.com 3975513SMichael.Adler@intel.com int result = mkdir(path.c_str(), mode); 3985513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3995513SMichael.Adler@intel.com} 4005513SMichael.Adler@intel.com 4011450SN/ASyscallReturn 4023114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 403511SN/A{ 4041706SN/A string old_name; 405511SN/A 4066701Sgblack@eecs.umich.edu int index = 0; 4076701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index))) 4081458SN/A return -EFAULT; 409511SN/A 4101706SN/A string new_name; 411511SN/A 4126701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index))) 4131458SN/A return -EFAULT; 414511SN/A 4153669Sbinkertn@umich.edu // Adjust path for current working directory 4163669Sbinkertn@umich.edu old_name = p->fullPath(old_name); 4173669Sbinkertn@umich.edu new_name = p->fullPath(new_name); 4183669Sbinkertn@umich.edu 4191706SN/A int64_t result = rename(old_name.c_str(), new_name.c_str()); 4201458SN/A return (result == -1) ? -errno : result; 421511SN/A} 422511SN/A 4231706SN/ASyscallReturn 4243114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 4251706SN/A{ 4261706SN/A string path; 4271706SN/A 4286701Sgblack@eecs.umich.edu int index = 0; 4296701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index))) 4301706SN/A return -EFAULT; 4311706SN/A 4326701Sgblack@eecs.umich.edu off_t length = p->getSyscallArg(tc, index); 4331706SN/A 4343669Sbinkertn@umich.edu // Adjust path for current working directory 4353669Sbinkertn@umich.edu path = p->fullPath(path); 4363669Sbinkertn@umich.edu 4371706SN/A int result = truncate(path.c_str(), length); 4381706SN/A return (result == -1) ? -errno : result; 4391706SN/A} 4401706SN/A 4411706SN/ASyscallReturn 4426111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num, 4436111Ssteve.reinhardt@amd.com LiveProcess *process, ThreadContext *tc) 4441706SN/A{ 4456701Sgblack@eecs.umich.edu int index = 0; 4466701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 4471706SN/A 4481706SN/A if (fd < 0) 4491706SN/A return -EBADF; 4501706SN/A 4516701Sgblack@eecs.umich.edu off_t length = process->getSyscallArg(tc, index); 4521706SN/A 4531706SN/A int result = ftruncate(fd, length); 4541706SN/A return (result == -1) ? -errno : result; 4551706SN/A} 4561999SN/A 4571999SN/ASyscallReturn 4586703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num, 4596703Svince@csl.cornell.edu LiveProcess *process, ThreadContext *tc) 4606703Svince@csl.cornell.edu{ 4616703Svince@csl.cornell.edu int index = 0; 4626703Svince@csl.cornell.edu string path; 4636703Svince@csl.cornell.edu 4646703Svince@csl.cornell.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index))) 4656703Svince@csl.cornell.edu return -EFAULT; 4666703Svince@csl.cornell.edu 4676744SAli.Saidi@arm.com int64_t length = process->getSyscallArg(tc, index, 64); 4686703Svince@csl.cornell.edu 4696703Svince@csl.cornell.edu // Adjust path for current working directory 4706703Svince@csl.cornell.edu path = process->fullPath(path); 4716703Svince@csl.cornell.edu 4726744SAli.Saidi@arm.com#if NO_STAT64 4736744SAli.Saidi@arm.com int result = truncate(path.c_str(), length); 4746744SAli.Saidi@arm.com#else 4756703Svince@csl.cornell.edu int result = truncate64(path.c_str(), length); 4766744SAli.Saidi@arm.com#endif 4776703Svince@csl.cornell.edu return (result == -1) ? -errno : result; 4786703Svince@csl.cornell.edu} 4796703Svince@csl.cornell.edu 4806703Svince@csl.cornell.eduSyscallReturn 4816685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num, 4826685Stjones1@inf.ed.ac.uk LiveProcess *process, ThreadContext *tc) 4836685Stjones1@inf.ed.ac.uk{ 4846701Sgblack@eecs.umich.edu int index = 0; 4856701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 4866685Stjones1@inf.ed.ac.uk 4876685Stjones1@inf.ed.ac.uk if (fd < 0) 4886685Stjones1@inf.ed.ac.uk return -EBADF; 4896685Stjones1@inf.ed.ac.uk 4906744SAli.Saidi@arm.com int64_t length = process->getSyscallArg(tc, index, 64); 4916685Stjones1@inf.ed.ac.uk 4926744SAli.Saidi@arm.com#if NO_STAT64 4936744SAli.Saidi@arm.com int result = ftruncate(fd, length); 4946744SAli.Saidi@arm.com#else 4956685Stjones1@inf.ed.ac.uk int result = ftruncate64(fd, length); 4966744SAli.Saidi@arm.com#endif 4976685Stjones1@inf.ed.ac.uk return (result == -1) ? -errno : result; 4986685Stjones1@inf.ed.ac.uk} 4996685Stjones1@inf.ed.ac.uk 5006685Stjones1@inf.ed.ac.ukSyscallReturn 5015513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 5025513SMichael.Adler@intel.com{ 5035513SMichael.Adler@intel.com // Letting the simulated program change the simulator's umask seems like 5045513SMichael.Adler@intel.com // a bad idea. Compromise by just returning the current umask but not 5055513SMichael.Adler@intel.com // changing anything. 5065513SMichael.Adler@intel.com mode_t oldMask = umask(0); 5075513SMichael.Adler@intel.com umask(oldMask); 5085521Snate@binkert.org return (int)oldMask; 5095513SMichael.Adler@intel.com} 5105513SMichael.Adler@intel.com 5115513SMichael.Adler@intel.comSyscallReturn 5123114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 5131999SN/A{ 5141999SN/A string path; 5151999SN/A 5166701Sgblack@eecs.umich.edu int index = 0; 5176701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index))) 5181999SN/A return -EFAULT; 5191999SN/A 5201999SN/A /* XXX endianess */ 5216701Sgblack@eecs.umich.edu uint32_t owner = p->getSyscallArg(tc, index); 5221999SN/A uid_t hostOwner = owner; 5236701Sgblack@eecs.umich.edu uint32_t group = p->getSyscallArg(tc, index); 5241999SN/A gid_t hostGroup = group; 5251999SN/A 5263669Sbinkertn@umich.edu // Adjust path for current working directory 5273669Sbinkertn@umich.edu path = p->fullPath(path); 5283669Sbinkertn@umich.edu 5291999SN/A int result = chown(path.c_str(), hostOwner, hostGroup); 5301999SN/A return (result == -1) ? -errno : result; 5311999SN/A} 5321999SN/A 5331999SN/ASyscallReturn 5343114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 5351999SN/A{ 5366701Sgblack@eecs.umich.edu int index = 0; 5376701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 5381999SN/A 5391999SN/A if (fd < 0) 5401999SN/A return -EBADF; 5411999SN/A 5421999SN/A /* XXX endianess */ 5436701Sgblack@eecs.umich.edu uint32_t owner = process->getSyscallArg(tc, index); 5441999SN/A uid_t hostOwner = owner; 5456701Sgblack@eecs.umich.edu uint32_t group = process->getSyscallArg(tc, index); 5461999SN/A gid_t hostGroup = group; 5471999SN/A 5481999SN/A int result = fchown(fd, hostOwner, hostGroup); 5491999SN/A return (result == -1) ? -errno : result; 5501999SN/A} 5512093SN/A 5522093SN/A 5532093SN/ASyscallReturn 5543114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 5553079Sstever@eecs.umich.edu{ 5566701Sgblack@eecs.umich.edu int index = 0; 5576701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 5583079Sstever@eecs.umich.edu if (fd < 0) 5593079Sstever@eecs.umich.edu return -EBADF; 5603079Sstever@eecs.umich.edu 5616701Sgblack@eecs.umich.edu Process::FdMap *fdo = process->sim_fd_obj(fd); 5625282Srstrong@cs.ucsd.edu 5633079Sstever@eecs.umich.edu int result = dup(fd); 5646111Ssteve.reinhardt@amd.com return (result == -1) ? -errno : 5656111Ssteve.reinhardt@amd.com process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false); 5663079Sstever@eecs.umich.edu} 5673079Sstever@eecs.umich.edu 5683079Sstever@eecs.umich.edu 5693079Sstever@eecs.umich.eduSyscallReturn 5703114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, 5712680Sktlim@umich.edu ThreadContext *tc) 5722093SN/A{ 5736701Sgblack@eecs.umich.edu int index = 0; 5746701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 5752093SN/A 5762093SN/A if (fd < 0 || process->sim_fd(fd) < 0) 5772093SN/A return -EBADF; 5782093SN/A 5796701Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, index); 5802093SN/A switch (cmd) { 5812093SN/A case 0: // F_DUPFD 5822093SN/A // if we really wanted to support this, we'd need to do it 5832093SN/A // in the target fd space. 5842093SN/A warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd); 5852093SN/A return -EMFILE; 5862093SN/A 5872093SN/A case 1: // F_GETFD (get close-on-exec flag) 5882093SN/A case 2: // F_SETFD (set close-on-exec flag) 5892093SN/A return 0; 5902093SN/A 5912093SN/A case 3: // F_GETFL (get file flags) 5922093SN/A case 4: // F_SETFL (set file flags) 5932093SN/A // not sure if this is totally valid, but we'll pass it through 5942093SN/A // to the underlying OS 5952093SN/A warn("fcntl(%d, %d) passed through to host\n", fd, cmd); 5962093SN/A return fcntl(process->sim_fd(fd), cmd); 5972093SN/A // return 0; 5982093SN/A 5992093SN/A case 7: // F_GETLK (get lock) 6002093SN/A case 8: // F_SETLK (set lock) 6012093SN/A case 9: // F_SETLKW (set lock and wait) 6022093SN/A // don't mess with file locking... just act like it's OK 6032093SN/A warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd); 6042093SN/A return 0; 6052093SN/A 6062093SN/A default: 6072093SN/A warn("Unknown fcntl command %d\n", cmd); 6082093SN/A return 0; 6092093SN/A } 6102093SN/A} 6112093SN/A 6122238SN/ASyscallReturn 6133114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process, 6142687Sksewell@umich.edu ThreadContext *tc) 6152687Sksewell@umich.edu{ 6166701Sgblack@eecs.umich.edu int index = 0; 6176701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6182687Sksewell@umich.edu 6192687Sksewell@umich.edu if (fd < 0 || process->sim_fd(fd) < 0) 6202687Sksewell@umich.edu return -EBADF; 6212687Sksewell@umich.edu 6226701Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, index); 6232687Sksewell@umich.edu switch (cmd) { 6242687Sksewell@umich.edu case 33: //F_GETLK64 6252687Sksewell@umich.edu warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd); 6262687Sksewell@umich.edu return -EMFILE; 6272687Sksewell@umich.edu 6282687Sksewell@umich.edu case 34: // F_SETLK64 6292687Sksewell@umich.edu case 35: // F_SETLKW64 6302687Sksewell@umich.edu warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd); 6312687Sksewell@umich.edu return -EMFILE; 6322687Sksewell@umich.edu 6332687Sksewell@umich.edu default: 6342687Sksewell@umich.edu // not sure if this is totally valid, but we'll pass it through 6352687Sksewell@umich.edu // to the underlying OS 6362687Sksewell@umich.edu warn("fcntl64(%d, %d) passed through to host\n", fd, cmd); 6372687Sksewell@umich.edu return fcntl(process->sim_fd(fd), cmd); 6382687Sksewell@umich.edu // return 0; 6392687Sksewell@umich.edu } 6402687Sksewell@umich.edu} 6412687Sksewell@umich.edu 6422687Sksewell@umich.eduSyscallReturn 6433114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6442680Sktlim@umich.edu ThreadContext *tc) 6452238SN/A{ 6462238SN/A int fds[2], sim_fds[2]; 6472238SN/A int pipe_retval = pipe(fds); 6482093SN/A 6492238SN/A if (pipe_retval < 0) { 6502238SN/A // error 6512238SN/A return pipe_retval; 6522238SN/A } 6532238SN/A 6545282Srstrong@cs.ucsd.edu sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true); 6555282Srstrong@cs.ucsd.edu sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true); 6562238SN/A 6575282Srstrong@cs.ucsd.edu process->setReadPipeSource(sim_fds[0], sim_fds[1]); 6582238SN/A // Alpha Linux convention for pipe() is that fd[0] is returned as 6592238SN/A // the return value of the function, and fd[1] is returned in r20. 6602680Sktlim@umich.edu tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]); 6612238SN/A return sim_fds[0]; 6622238SN/A} 6632238SN/A 6642238SN/A 6652238SN/ASyscallReturn 6663114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6672680Sktlim@umich.edu ThreadContext *tc) 6682238SN/A{ 6692238SN/A // Make up a PID. There's no interprocess communication in 6702238SN/A // fake_syscall mode, so there's no way for a process to know it's 6712238SN/A // not getting a unique value. 6722238SN/A 6733114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 6743114Sgblack@eecs.umich.edu return process->pid(); 6752238SN/A} 6762238SN/A 6772238SN/A 6782238SN/ASyscallReturn 6793114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6802680Sktlim@umich.edu ThreadContext *tc) 6812238SN/A{ 6822238SN/A // Make up a UID and EUID... it shouldn't matter, and we want the 6832238SN/A // simulation to be deterministic. 6842238SN/A 6852238SN/A // EUID goes in r20. 6863114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID 6875543Ssaidi@eecs.umich.edu return process->uid(); // UID 6882238SN/A} 6892238SN/A 6902238SN/A 6912238SN/ASyscallReturn 6923114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6932680Sktlim@umich.edu ThreadContext *tc) 6942238SN/A{ 6952238SN/A // Get current group ID. EGID goes in r20. 6963114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID 6973114Sgblack@eecs.umich.edu return process->gid(); 6982238SN/A} 6992238SN/A 7002238SN/A 7012238SN/ASyscallReturn 7023114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7032680Sktlim@umich.edu ThreadContext *tc) 7042238SN/A{ 7052238SN/A // can't fathom why a benchmark would call this. 7066701Sgblack@eecs.umich.edu int index = 0; 7076701Sgblack@eecs.umich.edu warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index)); 7082238SN/A return 0; 7092238SN/A} 7102238SN/A 7112238SN/ASyscallReturn 7123114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7132680Sktlim@umich.edu ThreadContext *tc) 7142238SN/A{ 7152238SN/A // Make up a PID. There's no interprocess communication in 7162238SN/A // fake_syscall mode, so there's no way for a process to know it's 7172238SN/A // not getting a unique value. 7182238SN/A 7193114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID 7203114Sgblack@eecs.umich.edu return process->pid(); 7212238SN/A} 7222238SN/A 7232238SN/ASyscallReturn 7243114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7252680Sktlim@umich.edu ThreadContext *tc) 7262238SN/A{ 7273114Sgblack@eecs.umich.edu return process->ppid(); 7282238SN/A} 7292238SN/A 7302238SN/ASyscallReturn 7313114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7322680Sktlim@umich.edu ThreadContext *tc) 7332238SN/A{ 7345543Ssaidi@eecs.umich.edu return process->uid(); // UID 7352238SN/A} 7362238SN/A 7372238SN/ASyscallReturn 7383114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7392680Sktlim@umich.edu ThreadContext *tc) 7402238SN/A{ 7415543Ssaidi@eecs.umich.edu return process->euid(); // UID 7422238SN/A} 7432238SN/A 7442238SN/ASyscallReturn 7453114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7462680Sktlim@umich.edu ThreadContext *tc) 7472238SN/A{ 7483114Sgblack@eecs.umich.edu return process->gid(); 7492238SN/A} 7502238SN/A 7512238SN/ASyscallReturn 7523114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7532680Sktlim@umich.edu ThreadContext *tc) 7542238SN/A{ 7553114Sgblack@eecs.umich.edu return process->egid(); 7562238SN/A} 7572238SN/A 7582238SN/A 7596109Ssanchezd@stanford.eduSyscallReturn 7606109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7616109Ssanchezd@stanford.edu ThreadContext *tc) 7626109Ssanchezd@stanford.edu{ 7636701Sgblack@eecs.umich.edu int index = 0; 7646701Sgblack@eecs.umich.edu IntReg flags = process->getSyscallArg(tc, index); 7656701Sgblack@eecs.umich.edu IntReg newStack = process->getSyscallArg(tc, index); 7666701Sgblack@eecs.umich.edu 7676109Ssanchezd@stanford.edu DPRINTF(SyscallVerbose, "In sys_clone:\n"); 7686701Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, " Flags=%llx\n", flags); 7696701Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack); 7706109Ssanchezd@stanford.edu 7716109Ssanchezd@stanford.edu 7726701Sgblack@eecs.umich.edu if (flags != 0x10f00) { 7736111Ssteve.reinhardt@amd.com warn("This sys_clone implementation assumes flags " 7746111Ssteve.reinhardt@amd.com "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD " 7756111Ssteve.reinhardt@amd.com "(0x10f00), and may not work correctly with given flags " 7766701Sgblack@eecs.umich.edu "0x%llx\n", flags); 7776109Ssanchezd@stanford.edu } 7786109Ssanchezd@stanford.edu 7796111Ssteve.reinhardt@amd.com ThreadContext* ctc; // child thread context 7806109Ssanchezd@stanford.edu if ( ( ctc = process->findFreeContext() ) != NULL ) { 7816109Ssanchezd@stanford.edu DPRINTF(SyscallVerbose, " Found unallocated thread context\n"); 7826109Ssanchezd@stanford.edu 7836109Ssanchezd@stanford.edu ctc->clearArchRegs(); 7846109Ssanchezd@stanford.edu 7856111Ssteve.reinhardt@amd.com // Arch-specific cloning code 7866109Ssanchezd@stanford.edu #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA 7876111Ssteve.reinhardt@amd.com // Cloning the misc. regs for these archs is enough 7886109Ssanchezd@stanford.edu TheISA::copyMiscRegs(tc, ctc); 7896109Ssanchezd@stanford.edu #elif THE_ISA == SPARC_ISA 7906109Ssanchezd@stanford.edu TheISA::copyRegs(tc, ctc); 7916109Ssanchezd@stanford.edu 7926111Ssteve.reinhardt@amd.com // TODO: Explain what this code actually does :-) 7936109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 6, 0); 7946109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 4, 0); 7956109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2); 7966109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 5, NWindows); 7976337Sgblack@eecs.umich.edu ctc->setMiscReg(MISCREG_CWP, 0); 7986109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 7, 0); 7996109Ssanchezd@stanford.edu ctc->setMiscRegNoEffect(MISCREG_TL, 0); 8006109Ssanchezd@stanford.edu ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY); 8016109Ssanchezd@stanford.edu 8026109Ssanchezd@stanford.edu for (int y = 8; y < 32; y++) 8036109Ssanchezd@stanford.edu ctc->setIntReg(y, tc->readIntReg(y)); 8046109Ssanchezd@stanford.edu #else 8056109Ssanchezd@stanford.edu fatal("sys_clone is not implemented for this ISA\n"); 8066109Ssanchezd@stanford.edu #endif 8076109Ssanchezd@stanford.edu 8086111Ssteve.reinhardt@amd.com // Set up stack register 8096701Sgblack@eecs.umich.edu ctc->setIntReg(TheISA::StackPointerReg, newStack); 8106109Ssanchezd@stanford.edu 8116111Ssteve.reinhardt@amd.com // Set up syscall return values in parent and child 8126111Ssteve.reinhardt@amd.com ctc->setIntReg(ReturnValueReg, 0); // return value, child 8136109Ssanchezd@stanford.edu 8146111Ssteve.reinhardt@amd.com // Alpha needs SyscallSuccessReg=0 in child 8156109Ssanchezd@stanford.edu #if THE_ISA == ALPHA_ISA 8166110Ssteve.reinhardt@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 8176109Ssanchezd@stanford.edu #endif 8186109Ssanchezd@stanford.edu 8196111Ssteve.reinhardt@amd.com // In SPARC/Linux, clone returns 0 on pseudo-return register if 8206111Ssteve.reinhardt@amd.com // parent, non-zero if child 8216109Ssanchezd@stanford.edu #if THE_ISA == SPARC_ISA 8226109Ssanchezd@stanford.edu tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 8236109Ssanchezd@stanford.edu ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 8246109Ssanchezd@stanford.edu #endif 8256109Ssanchezd@stanford.edu 8266109Ssanchezd@stanford.edu ctc->setPC(tc->readNextPC()); 8276109Ssanchezd@stanford.edu ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst)); 8286134Sgblack@eecs.umich.edu ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst)); 8296109Ssanchezd@stanford.edu 8306109Ssanchezd@stanford.edu ctc->activate(); 8316109Ssanchezd@stanford.edu 8326109Ssanchezd@stanford.edu // Should return nonzero child TID in parent's syscall return register, 8336109Ssanchezd@stanford.edu // but for our pthread library any non-zero value will work 8346109Ssanchezd@stanford.edu return 1; 8356109Ssanchezd@stanford.edu } else { 8366109Ssanchezd@stanford.edu fatal("Called sys_clone, but no unallocated thread contexts found!\n"); 8376109Ssanchezd@stanford.edu return 0; 8386109Ssanchezd@stanford.edu } 8396109Ssanchezd@stanford.edu} 8406109Ssanchezd@stanford.edu 841