syscall_emul.cc revision 6687
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 35360SN/A#include <string> 36360SN/A#include <iostream> 37360SN/A 38360SN/A#include "sim/syscall_emul.hh" 392474SN/A#include "base/chunk_generator.hh" 40360SN/A#include "base/trace.hh" 416658Snate@binkert.org#include "config/the_isa.hh" 422680Sktlim@umich.edu#include "cpu/thread_context.hh" 431717SN/A#include "cpu/base.hh" 442474SN/A#include "mem/page_table.hh" 45360SN/A#include "sim/process.hh" 466029Ssteve.reinhardt@amd.com#include "sim/system.hh" 472667Sstever@eecs.umich.edu#include "sim/sim_exit.hh" 48360SN/A 49360SN/Ausing namespace std; 502107SN/Ausing namespace TheISA; 51360SN/A 52360SN/Avoid 533114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc) 54360SN/A{ 556111Ssteve.reinhardt@amd.com DPRINTFR(SyscallVerbose, 566111Ssteve.reinhardt@amd.com "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n", 576111Ssteve.reinhardt@amd.com curTick, tc->getCpuPtr()->name(), name, 585958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1), 595958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 3)); 60360SN/A 612680Sktlim@umich.edu SyscallReturn retval = (*funcPtr)(this, callnum, process, tc); 62360SN/A 632495SN/A DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n", 642680Sktlim@umich.edu curTick,tc->getCpuPtr()->name(), name, retval.value()); 65360SN/A 661450SN/A if (!(flags & SyscallDesc::SuppressReturnValue)) 675958Sgblack@eecs.umich.edu process->setSyscallReturn(tc, retval); 68360SN/A} 69360SN/A 70360SN/A 711450SN/ASyscallReturn 723114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 732680Sktlim@umich.edu ThreadContext *tc) 74360SN/A{ 751969SN/A fatal("syscall %s (#%d) unimplemented.", desc->name, callnum); 762484SN/A 772484SN/A return 1; 78360SN/A} 79360SN/A 80360SN/A 811450SN/ASyscallReturn 823114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 832680Sktlim@umich.edu ThreadContext *tc) 84360SN/A{ 851969SN/A warn("ignoring syscall %s(%d, %d, ...)", desc->name, 865958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1)); 87360SN/A 881458SN/A return 0; 89360SN/A} 90360SN/A 91360SN/A 921450SN/ASyscallReturn 933114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 942680Sktlim@umich.edu ThreadContext *tc) 95360SN/A{ 966029Ssteve.reinhardt@amd.com if (process->system->numRunningContexts() == 1) { 976029Ssteve.reinhardt@amd.com // Last running context... exit simulator 985958Sgblack@eecs.umich.edu exitSimLoop("target called exit()", 996029Ssteve.reinhardt@amd.com process->getSyscallArg(tc, 0) & 0xff); 1006029Ssteve.reinhardt@amd.com } else { 1016029Ssteve.reinhardt@amd.com // other running threads... just halt this one 1026029Ssteve.reinhardt@amd.com tc->halt(); 1032834Sksewell@umich.edu } 104360SN/A 1051458SN/A return 1; 106360SN/A} 107360SN/A 108360SN/A 1091450SN/ASyscallReturn 1106109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1116109Ssanchezd@stanford.edu ThreadContext *tc) 1126109Ssanchezd@stanford.edu{ 1136109Ssanchezd@stanford.edu // really should just halt all thread contexts belonging to this 1146109Ssanchezd@stanford.edu // process in case there's another process running... 1156109Ssanchezd@stanford.edu exitSimLoop("target called exit()", 1166109Ssanchezd@stanford.edu process->getSyscallArg(tc, 0) & 0xff); 1176109Ssanchezd@stanford.edu 1186109Ssanchezd@stanford.edu return 1; 1196109Ssanchezd@stanford.edu} 1206109Ssanchezd@stanford.edu 1216109Ssanchezd@stanford.edu 1226109Ssanchezd@stanford.eduSyscallReturn 1233114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 124360SN/A{ 1252107SN/A return (int)VMPageSize; 126360SN/A} 127360SN/A 128360SN/A 1291450SN/ASyscallReturn 1305748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 131360SN/A{ 132360SN/A // change brk addr to first arg 1335958Sgblack@eecs.umich.edu Addr new_brk = p->getSyscallArg(tc, 0); 1345748SSteve.Reinhardt@amd.com 1355748SSteve.Reinhardt@amd.com // in Linux at least, brk(0) returns the current break value 1365748SSteve.Reinhardt@amd.com // (note that the syscall and the glibc function have different behavior) 1375748SSteve.Reinhardt@amd.com if (new_brk == 0) 1385748SSteve.Reinhardt@amd.com return p->brk_point; 1395748SSteve.Reinhardt@amd.com 1405748SSteve.Reinhardt@amd.com if (new_brk > p->brk_point) { 1415748SSteve.Reinhardt@amd.com // might need to allocate some new pages 1422474SN/A for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point, 1432474SN/A VMPageSize); !gen.done(); gen.next()) { 1445748SSteve.Reinhardt@amd.com if (!p->pTable->translate(gen.addr())) 1452474SN/A p->pTable->allocate(roundDown(gen.addr(), VMPageSize), 1462474SN/A VMPageSize); 1476687Stjones1@inf.ed.ac.uk 1486687Stjones1@inf.ed.ac.uk // if the address is already there, zero it out 1496687Stjones1@inf.ed.ac.uk else { 1506687Stjones1@inf.ed.ac.uk uint8_t zero = 0; 1516687Stjones1@inf.ed.ac.uk TranslatingPort *tp = tc->getMemPort(); 1526687Stjones1@inf.ed.ac.uk 1536687Stjones1@inf.ed.ac.uk // split non-page aligned accesses 1546687Stjones1@inf.ed.ac.uk Addr next_page = roundUp(gen.addr(), VMPageSize); 1556687Stjones1@inf.ed.ac.uk uint32_t size_needed = next_page - gen.addr(); 1566687Stjones1@inf.ed.ac.uk tp->memsetBlob(gen.addr(), zero, size_needed); 1576687Stjones1@inf.ed.ac.uk if (gen.addr() + VMPageSize > next_page && 1586687Stjones1@inf.ed.ac.uk next_page < new_brk && 1596687Stjones1@inf.ed.ac.uk p->pTable->translate(next_page)) 1606687Stjones1@inf.ed.ac.uk { 1616687Stjones1@inf.ed.ac.uk size_needed = VMPageSize - size_needed; 1626687Stjones1@inf.ed.ac.uk tp->memsetBlob(next_page, zero, size_needed); 1636687Stjones1@inf.ed.ac.uk } 1646687Stjones1@inf.ed.ac.uk } 1652474SN/A } 1661450SN/A } 1675748SSteve.Reinhardt@amd.com 1685748SSteve.Reinhardt@amd.com p->brk_point = new_brk; 1691458SN/A DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point); 1701458SN/A return p->brk_point; 171360SN/A} 172360SN/A 173360SN/A 1741450SN/ASyscallReturn 1753114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 176360SN/A{ 1775958Sgblack@eecs.umich.edu int target_fd = p->getSyscallArg(tc, 0); 1781970SN/A int status = close(p->sim_fd(target_fd)); 1791970SN/A if (status >= 0) 1801970SN/A p->free_fd(target_fd); 1811970SN/A return status; 182360SN/A} 183360SN/A 184360SN/A 1851450SN/ASyscallReturn 1863114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 187360SN/A{ 1885958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 1895958Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, 2); 1905958Sgblack@eecs.umich.edu BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes); 191360SN/A 192360SN/A int bytes_read = read(fd, bufArg.bufferPtr(), nbytes); 193360SN/A 194360SN/A if (bytes_read != -1) 1952680Sktlim@umich.edu bufArg.copyOut(tc->getMemPort()); 196360SN/A 1971458SN/A return bytes_read; 198360SN/A} 199360SN/A 2001450SN/ASyscallReturn 2013114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 202360SN/A{ 2035958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 2045958Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, 2); 2055958Sgblack@eecs.umich.edu BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes); 206360SN/A 2072680Sktlim@umich.edu bufArg.copyIn(tc->getMemPort()); 208360SN/A 209360SN/A int bytes_written = write(fd, bufArg.bufferPtr(), nbytes); 210360SN/A 211360SN/A fsync(fd); 212360SN/A 2131458SN/A return bytes_written; 214360SN/A} 215360SN/A 216360SN/A 2171450SN/ASyscallReturn 2183114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 219360SN/A{ 2205958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 2215958Sgblack@eecs.umich.edu uint64_t offs = p->getSyscallArg(tc, 1); 2225958Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, 2); 223360SN/A 224360SN/A off_t result = lseek(fd, offs, whence); 225360SN/A 2261458SN/A return (result == (off_t)-1) ? -errno : result; 227360SN/A} 228360SN/A 229360SN/A 2301450SN/ASyscallReturn 2314118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2324118Sgblack@eecs.umich.edu{ 2335958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 2345958Sgblack@eecs.umich.edu uint64_t offset_high = p->getSyscallArg(tc, 1); 2355958Sgblack@eecs.umich.edu uint32_t offset_low = p->getSyscallArg(tc, 2); 2365958Sgblack@eecs.umich.edu Addr result_ptr = p->getSyscallArg(tc, 3); 2375958Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, 4); 2384118Sgblack@eecs.umich.edu 2394118Sgblack@eecs.umich.edu uint64_t offset = (offset_high << 32) | offset_low; 2404118Sgblack@eecs.umich.edu 2414118Sgblack@eecs.umich.edu uint64_t result = lseek(fd, offset, whence); 2424118Sgblack@eecs.umich.edu result = TheISA::htog(result); 2434118Sgblack@eecs.umich.edu 2444118Sgblack@eecs.umich.edu if (result == (off_t)-1) { 2454118Sgblack@eecs.umich.edu //The seek failed. 2464118Sgblack@eecs.umich.edu return -errno; 2474118Sgblack@eecs.umich.edu } else { 2486111Ssteve.reinhardt@amd.com // The seek succeeded. 2496111Ssteve.reinhardt@amd.com // Copy "result" to "result_ptr" 2506111Ssteve.reinhardt@amd.com // XXX We'll assume that the size of loff_t is 64 bits on the 2516111Ssteve.reinhardt@amd.com // target platform 2524118Sgblack@eecs.umich.edu BufferArg result_buf(result_ptr, sizeof(result)); 2534118Sgblack@eecs.umich.edu memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 2544118Sgblack@eecs.umich.edu result_buf.copyOut(tc->getMemPort()); 2554118Sgblack@eecs.umich.edu return 0; 2564118Sgblack@eecs.umich.edu } 2574118Sgblack@eecs.umich.edu 2584118Sgblack@eecs.umich.edu 2594118Sgblack@eecs.umich.edu return (result == (off_t)-1) ? -errno : result; 2604118Sgblack@eecs.umich.edu} 2614118Sgblack@eecs.umich.edu 2624118Sgblack@eecs.umich.edu 2634118Sgblack@eecs.umich.eduSyscallReturn 2643114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 265360SN/A{ 266360SN/A // given that we don't really implement mmap, munmap is really easy 2671458SN/A return 0; 268360SN/A} 269360SN/A 270360SN/A 271360SN/Aconst char *hostname = "m5.eecs.umich.edu"; 272360SN/A 2731450SN/ASyscallReturn 2743114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 275360SN/A{ 2765958Sgblack@eecs.umich.edu int name_len = p->getSyscallArg(tc, 1); 2775958Sgblack@eecs.umich.edu BufferArg name(p->getSyscallArg(tc, 0), name_len); 278360SN/A 279360SN/A strncpy((char *)name.bufferPtr(), hostname, name_len); 280360SN/A 2812680Sktlim@umich.edu name.copyOut(tc->getMemPort()); 282360SN/A 2831458SN/A return 0; 284360SN/A} 285360SN/A 2861450SN/ASyscallReturn 2875513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2885513SMichael.Adler@intel.com{ 2895513SMichael.Adler@intel.com int result = 0; 2905958Sgblack@eecs.umich.edu unsigned long size = p->getSyscallArg(tc, 1); 2915958Sgblack@eecs.umich.edu BufferArg buf(p->getSyscallArg(tc, 0), size); 2925513SMichael.Adler@intel.com 2935513SMichael.Adler@intel.com // Is current working directory defined? 2945513SMichael.Adler@intel.com string cwd = p->getcwd(); 2955513SMichael.Adler@intel.com if (!cwd.empty()) { 2965513SMichael.Adler@intel.com if (cwd.length() >= size) { 2975513SMichael.Adler@intel.com // Buffer too small 2985513SMichael.Adler@intel.com return -ERANGE; 2995513SMichael.Adler@intel.com } 3005513SMichael.Adler@intel.com strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 3015513SMichael.Adler@intel.com result = cwd.length(); 3025513SMichael.Adler@intel.com } 3035513SMichael.Adler@intel.com else { 3045513SMichael.Adler@intel.com if (getcwd((char *)buf.bufferPtr(), size) != NULL) { 3055513SMichael.Adler@intel.com result = strlen((char *)buf.bufferPtr()); 3065513SMichael.Adler@intel.com } 3075513SMichael.Adler@intel.com else { 3085513SMichael.Adler@intel.com result = -1; 3095513SMichael.Adler@intel.com } 3105513SMichael.Adler@intel.com } 3115513SMichael.Adler@intel.com 3125513SMichael.Adler@intel.com buf.copyOut(tc->getMemPort()); 3135513SMichael.Adler@intel.com 3145513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3155513SMichael.Adler@intel.com} 3165513SMichael.Adler@intel.com 3175513SMichael.Adler@intel.com 3185513SMichael.Adler@intel.comSyscallReturn 3195513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3205513SMichael.Adler@intel.com{ 3215513SMichael.Adler@intel.com string path; 3225513SMichael.Adler@intel.com 3235958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 3245513SMichael.Adler@intel.com return (TheISA::IntReg)-EFAULT; 3255513SMichael.Adler@intel.com 3265513SMichael.Adler@intel.com // Adjust path for current working directory 3275513SMichael.Adler@intel.com path = p->fullPath(path); 3285513SMichael.Adler@intel.com 3295958Sgblack@eecs.umich.edu size_t bufsiz = p->getSyscallArg(tc, 2); 3305958Sgblack@eecs.umich.edu BufferArg buf(p->getSyscallArg(tc, 1), bufsiz); 3315513SMichael.Adler@intel.com 3325513SMichael.Adler@intel.com int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 3335513SMichael.Adler@intel.com 3345513SMichael.Adler@intel.com buf.copyOut(tc->getMemPort()); 3355513SMichael.Adler@intel.com 3365513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3375513SMichael.Adler@intel.com} 3385513SMichael.Adler@intel.com 3395513SMichael.Adler@intel.comSyscallReturn 3403114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 341511SN/A{ 3421706SN/A string path; 343360SN/A 3445958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 3451450SN/A return (TheISA::IntReg)-EFAULT; 346511SN/A 3473669Sbinkertn@umich.edu // Adjust path for current working directory 3483669Sbinkertn@umich.edu path = p->fullPath(path); 3493669Sbinkertn@umich.edu 350511SN/A int result = unlink(path.c_str()); 3511458SN/A return (result == -1) ? -errno : result; 352511SN/A} 353511SN/A 3545513SMichael.Adler@intel.com 3555513SMichael.Adler@intel.comSyscallReturn 3565513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3575513SMichael.Adler@intel.com{ 3585513SMichael.Adler@intel.com string path; 3595513SMichael.Adler@intel.com 3605958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 3615513SMichael.Adler@intel.com return (TheISA::IntReg)-EFAULT; 3625513SMichael.Adler@intel.com 3635513SMichael.Adler@intel.com // Adjust path for current working directory 3645513SMichael.Adler@intel.com path = p->fullPath(path); 3655513SMichael.Adler@intel.com 3665958Sgblack@eecs.umich.edu mode_t mode = p->getSyscallArg(tc, 1); 3675513SMichael.Adler@intel.com 3685513SMichael.Adler@intel.com int result = mkdir(path.c_str(), mode); 3695513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3705513SMichael.Adler@intel.com} 3715513SMichael.Adler@intel.com 3721450SN/ASyscallReturn 3733114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 374511SN/A{ 3751706SN/A string old_name; 376511SN/A 3775958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0))) 3781458SN/A return -EFAULT; 379511SN/A 3801706SN/A string new_name; 381511SN/A 3825958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1))) 3831458SN/A return -EFAULT; 384511SN/A 3853669Sbinkertn@umich.edu // Adjust path for current working directory 3863669Sbinkertn@umich.edu old_name = p->fullPath(old_name); 3873669Sbinkertn@umich.edu new_name = p->fullPath(new_name); 3883669Sbinkertn@umich.edu 3891706SN/A int64_t result = rename(old_name.c_str(), new_name.c_str()); 3901458SN/A return (result == -1) ? -errno : result; 391511SN/A} 392511SN/A 3931706SN/ASyscallReturn 3943114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3951706SN/A{ 3961706SN/A string path; 3971706SN/A 3985958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 3991706SN/A return -EFAULT; 4001706SN/A 4015958Sgblack@eecs.umich.edu off_t length = p->getSyscallArg(tc, 1); 4021706SN/A 4033669Sbinkertn@umich.edu // Adjust path for current working directory 4043669Sbinkertn@umich.edu path = p->fullPath(path); 4053669Sbinkertn@umich.edu 4061706SN/A int result = truncate(path.c_str(), length); 4071706SN/A return (result == -1) ? -errno : result; 4081706SN/A} 4091706SN/A 4101706SN/ASyscallReturn 4116111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num, 4126111Ssteve.reinhardt@amd.com LiveProcess *process, ThreadContext *tc) 4131706SN/A{ 4145958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 4151706SN/A 4161706SN/A if (fd < 0) 4171706SN/A return -EBADF; 4181706SN/A 4195958Sgblack@eecs.umich.edu off_t length = process->getSyscallArg(tc, 1); 4201706SN/A 4211706SN/A int result = ftruncate(fd, length); 4221706SN/A return (result == -1) ? -errno : result; 4231706SN/A} 4241999SN/A 4251999SN/ASyscallReturn 4266685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num, 4276685Stjones1@inf.ed.ac.uk LiveProcess *process, ThreadContext *tc) 4286685Stjones1@inf.ed.ac.uk{ 4296685Stjones1@inf.ed.ac.uk int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 4306685Stjones1@inf.ed.ac.uk 4316685Stjones1@inf.ed.ac.uk if (fd < 0) 4326685Stjones1@inf.ed.ac.uk return -EBADF; 4336685Stjones1@inf.ed.ac.uk 4346685Stjones1@inf.ed.ac.uk // I'm not sure why, but the length argument is in arg reg 3 4356685Stjones1@inf.ed.ac.uk loff_t length = process->getSyscallArg(tc, 3); 4366685Stjones1@inf.ed.ac.uk 4376685Stjones1@inf.ed.ac.uk int result = ftruncate64(fd, length); 4386685Stjones1@inf.ed.ac.uk return (result == -1) ? -errno : result; 4396685Stjones1@inf.ed.ac.uk} 4406685Stjones1@inf.ed.ac.uk 4416685Stjones1@inf.ed.ac.ukSyscallReturn 4425513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 4435513SMichael.Adler@intel.com{ 4445513SMichael.Adler@intel.com // Letting the simulated program change the simulator's umask seems like 4455513SMichael.Adler@intel.com // a bad idea. Compromise by just returning the current umask but not 4465513SMichael.Adler@intel.com // changing anything. 4475513SMichael.Adler@intel.com mode_t oldMask = umask(0); 4485513SMichael.Adler@intel.com umask(oldMask); 4495521Snate@binkert.org return (int)oldMask; 4505513SMichael.Adler@intel.com} 4515513SMichael.Adler@intel.com 4525513SMichael.Adler@intel.comSyscallReturn 4533114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 4541999SN/A{ 4551999SN/A string path; 4561999SN/A 4575958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 4581999SN/A return -EFAULT; 4591999SN/A 4601999SN/A /* XXX endianess */ 4615958Sgblack@eecs.umich.edu uint32_t owner = p->getSyscallArg(tc, 1); 4621999SN/A uid_t hostOwner = owner; 4635958Sgblack@eecs.umich.edu uint32_t group = p->getSyscallArg(tc, 2); 4641999SN/A gid_t hostGroup = group; 4651999SN/A 4663669Sbinkertn@umich.edu // Adjust path for current working directory 4673669Sbinkertn@umich.edu path = p->fullPath(path); 4683669Sbinkertn@umich.edu 4691999SN/A int result = chown(path.c_str(), hostOwner, hostGroup); 4701999SN/A return (result == -1) ? -errno : result; 4711999SN/A} 4721999SN/A 4731999SN/ASyscallReturn 4743114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 4751999SN/A{ 4765958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 4771999SN/A 4781999SN/A if (fd < 0) 4791999SN/A return -EBADF; 4801999SN/A 4811999SN/A /* XXX endianess */ 4825958Sgblack@eecs.umich.edu uint32_t owner = process->getSyscallArg(tc, 1); 4831999SN/A uid_t hostOwner = owner; 4845958Sgblack@eecs.umich.edu uint32_t group = process->getSyscallArg(tc, 2); 4851999SN/A gid_t hostGroup = group; 4861999SN/A 4871999SN/A int result = fchown(fd, hostOwner, hostGroup); 4881999SN/A return (result == -1) ? -errno : result; 4891999SN/A} 4902093SN/A 4912093SN/A 4922093SN/ASyscallReturn 4933114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 4943079Sstever@eecs.umich.edu{ 4955958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 4963079Sstever@eecs.umich.edu if (fd < 0) 4973079Sstever@eecs.umich.edu return -EBADF; 4983079Sstever@eecs.umich.edu 4995958Sgblack@eecs.umich.edu Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0)); 5005282Srstrong@cs.ucsd.edu 5013079Sstever@eecs.umich.edu int result = dup(fd); 5026111Ssteve.reinhardt@amd.com return (result == -1) ? -errno : 5036111Ssteve.reinhardt@amd.com process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false); 5043079Sstever@eecs.umich.edu} 5053079Sstever@eecs.umich.edu 5063079Sstever@eecs.umich.edu 5073079Sstever@eecs.umich.eduSyscallReturn 5083114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, 5092680Sktlim@umich.edu ThreadContext *tc) 5102093SN/A{ 5115958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 5122093SN/A 5132093SN/A if (fd < 0 || process->sim_fd(fd) < 0) 5142093SN/A return -EBADF; 5152093SN/A 5165958Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, 1); 5172093SN/A switch (cmd) { 5182093SN/A case 0: // F_DUPFD 5192093SN/A // if we really wanted to support this, we'd need to do it 5202093SN/A // in the target fd space. 5212093SN/A warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd); 5222093SN/A return -EMFILE; 5232093SN/A 5242093SN/A case 1: // F_GETFD (get close-on-exec flag) 5252093SN/A case 2: // F_SETFD (set close-on-exec flag) 5262093SN/A return 0; 5272093SN/A 5282093SN/A case 3: // F_GETFL (get file flags) 5292093SN/A case 4: // F_SETFL (set file flags) 5302093SN/A // not sure if this is totally valid, but we'll pass it through 5312093SN/A // to the underlying OS 5322093SN/A warn("fcntl(%d, %d) passed through to host\n", fd, cmd); 5332093SN/A return fcntl(process->sim_fd(fd), cmd); 5342093SN/A // return 0; 5352093SN/A 5362093SN/A case 7: // F_GETLK (get lock) 5372093SN/A case 8: // F_SETLK (set lock) 5382093SN/A case 9: // F_SETLKW (set lock and wait) 5392093SN/A // don't mess with file locking... just act like it's OK 5402093SN/A warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd); 5412093SN/A return 0; 5422093SN/A 5432093SN/A default: 5442093SN/A warn("Unknown fcntl command %d\n", cmd); 5452093SN/A return 0; 5462093SN/A } 5472093SN/A} 5482093SN/A 5492238SN/ASyscallReturn 5503114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process, 5512687Sksewell@umich.edu ThreadContext *tc) 5522687Sksewell@umich.edu{ 5535958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 5542687Sksewell@umich.edu 5552687Sksewell@umich.edu if (fd < 0 || process->sim_fd(fd) < 0) 5562687Sksewell@umich.edu return -EBADF; 5572687Sksewell@umich.edu 5585958Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, 1); 5592687Sksewell@umich.edu switch (cmd) { 5602687Sksewell@umich.edu case 33: //F_GETLK64 5612687Sksewell@umich.edu warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd); 5622687Sksewell@umich.edu return -EMFILE; 5632687Sksewell@umich.edu 5642687Sksewell@umich.edu case 34: // F_SETLK64 5652687Sksewell@umich.edu case 35: // F_SETLKW64 5662687Sksewell@umich.edu warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd); 5672687Sksewell@umich.edu return -EMFILE; 5682687Sksewell@umich.edu 5692687Sksewell@umich.edu default: 5702687Sksewell@umich.edu // not sure if this is totally valid, but we'll pass it through 5712687Sksewell@umich.edu // to the underlying OS 5722687Sksewell@umich.edu warn("fcntl64(%d, %d) passed through to host\n", fd, cmd); 5732687Sksewell@umich.edu return fcntl(process->sim_fd(fd), cmd); 5742687Sksewell@umich.edu // return 0; 5752687Sksewell@umich.edu } 5762687Sksewell@umich.edu} 5772687Sksewell@umich.edu 5782687Sksewell@umich.eduSyscallReturn 5793114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5802680Sktlim@umich.edu ThreadContext *tc) 5812238SN/A{ 5822238SN/A int fds[2], sim_fds[2]; 5832238SN/A int pipe_retval = pipe(fds); 5842093SN/A 5852238SN/A if (pipe_retval < 0) { 5862238SN/A // error 5872238SN/A return pipe_retval; 5882238SN/A } 5892238SN/A 5905282Srstrong@cs.ucsd.edu sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true); 5915282Srstrong@cs.ucsd.edu sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true); 5922238SN/A 5935282Srstrong@cs.ucsd.edu process->setReadPipeSource(sim_fds[0], sim_fds[1]); 5942238SN/A // Alpha Linux convention for pipe() is that fd[0] is returned as 5952238SN/A // the return value of the function, and fd[1] is returned in r20. 5962680Sktlim@umich.edu tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]); 5972238SN/A return sim_fds[0]; 5982238SN/A} 5992238SN/A 6002238SN/A 6012238SN/ASyscallReturn 6023114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6032680Sktlim@umich.edu ThreadContext *tc) 6042238SN/A{ 6052238SN/A // Make up a PID. There's no interprocess communication in 6062238SN/A // fake_syscall mode, so there's no way for a process to know it's 6072238SN/A // not getting a unique value. 6082238SN/A 6093114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 6103114Sgblack@eecs.umich.edu return process->pid(); 6112238SN/A} 6122238SN/A 6132238SN/A 6142238SN/ASyscallReturn 6153114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6162680Sktlim@umich.edu ThreadContext *tc) 6172238SN/A{ 6182238SN/A // Make up a UID and EUID... it shouldn't matter, and we want the 6192238SN/A // simulation to be deterministic. 6202238SN/A 6212238SN/A // EUID goes in r20. 6223114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID 6235543Ssaidi@eecs.umich.edu return process->uid(); // UID 6242238SN/A} 6252238SN/A 6262238SN/A 6272238SN/ASyscallReturn 6283114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6292680Sktlim@umich.edu ThreadContext *tc) 6302238SN/A{ 6312238SN/A // Get current group ID. EGID goes in r20. 6323114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID 6333114Sgblack@eecs.umich.edu return process->gid(); 6342238SN/A} 6352238SN/A 6362238SN/A 6372238SN/ASyscallReturn 6383114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6392680Sktlim@umich.edu ThreadContext *tc) 6402238SN/A{ 6412238SN/A // can't fathom why a benchmark would call this. 6425958Sgblack@eecs.umich.edu warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0)); 6432238SN/A return 0; 6442238SN/A} 6452238SN/A 6462238SN/ASyscallReturn 6473114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6482680Sktlim@umich.edu ThreadContext *tc) 6492238SN/A{ 6502238SN/A // Make up a PID. There's no interprocess communication in 6512238SN/A // fake_syscall mode, so there's no way for a process to know it's 6522238SN/A // not getting a unique value. 6532238SN/A 6543114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID 6553114Sgblack@eecs.umich.edu return process->pid(); 6562238SN/A} 6572238SN/A 6582238SN/ASyscallReturn 6593114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6602680Sktlim@umich.edu ThreadContext *tc) 6612238SN/A{ 6623114Sgblack@eecs.umich.edu return process->ppid(); 6632238SN/A} 6642238SN/A 6652238SN/ASyscallReturn 6663114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6672680Sktlim@umich.edu ThreadContext *tc) 6682238SN/A{ 6695543Ssaidi@eecs.umich.edu return process->uid(); // UID 6702238SN/A} 6712238SN/A 6722238SN/ASyscallReturn 6733114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6742680Sktlim@umich.edu ThreadContext *tc) 6752238SN/A{ 6765543Ssaidi@eecs.umich.edu return process->euid(); // UID 6772238SN/A} 6782238SN/A 6792238SN/ASyscallReturn 6803114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6812680Sktlim@umich.edu ThreadContext *tc) 6822238SN/A{ 6833114Sgblack@eecs.umich.edu return process->gid(); 6842238SN/A} 6852238SN/A 6862238SN/ASyscallReturn 6873114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6882680Sktlim@umich.edu ThreadContext *tc) 6892238SN/A{ 6903114Sgblack@eecs.umich.edu return process->egid(); 6912238SN/A} 6922238SN/A 6932238SN/A 6946109Ssanchezd@stanford.eduSyscallReturn 6956109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6966109Ssanchezd@stanford.edu ThreadContext *tc) 6976109Ssanchezd@stanford.edu{ 6986109Ssanchezd@stanford.edu DPRINTF(SyscallVerbose, "In sys_clone:\n"); 6996110Ssteve.reinhardt@amd.com DPRINTF(SyscallVerbose, " Flags=%llx\n", process->getSyscallArg(tc, 0)); 7006111Ssteve.reinhardt@amd.com DPRINTF(SyscallVerbose, " Child stack=%llx\n", 7016111Ssteve.reinhardt@amd.com process->getSyscallArg(tc, 1)); 7026109Ssanchezd@stanford.edu 7036109Ssanchezd@stanford.edu 7046110Ssteve.reinhardt@amd.com if (process->getSyscallArg(tc, 0) != 0x10f00) { 7056111Ssteve.reinhardt@amd.com warn("This sys_clone implementation assumes flags " 7066111Ssteve.reinhardt@amd.com "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD " 7076111Ssteve.reinhardt@amd.com "(0x10f00), and may not work correctly with given flags " 7086111Ssteve.reinhardt@amd.com "0x%llx\n", process->getSyscallArg(tc, 0)); 7096109Ssanchezd@stanford.edu } 7106109Ssanchezd@stanford.edu 7116111Ssteve.reinhardt@amd.com ThreadContext* ctc; // child thread context 7126109Ssanchezd@stanford.edu if ( ( ctc = process->findFreeContext() ) != NULL ) { 7136109Ssanchezd@stanford.edu DPRINTF(SyscallVerbose, " Found unallocated thread context\n"); 7146109Ssanchezd@stanford.edu 7156109Ssanchezd@stanford.edu ctc->clearArchRegs(); 7166109Ssanchezd@stanford.edu 7176111Ssteve.reinhardt@amd.com // Arch-specific cloning code 7186109Ssanchezd@stanford.edu #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA 7196111Ssteve.reinhardt@amd.com // Cloning the misc. regs for these archs is enough 7206109Ssanchezd@stanford.edu TheISA::copyMiscRegs(tc, ctc); 7216109Ssanchezd@stanford.edu #elif THE_ISA == SPARC_ISA 7226109Ssanchezd@stanford.edu TheISA::copyRegs(tc, ctc); 7236109Ssanchezd@stanford.edu 7246111Ssteve.reinhardt@amd.com // TODO: Explain what this code actually does :-) 7256109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 6, 0); 7266109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 4, 0); 7276109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2); 7286109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 5, NWindows); 7296337Sgblack@eecs.umich.edu ctc->setMiscReg(MISCREG_CWP, 0); 7306109Ssanchezd@stanford.edu ctc->setIntReg(NumIntArchRegs + 7, 0); 7316109Ssanchezd@stanford.edu ctc->setMiscRegNoEffect(MISCREG_TL, 0); 7326109Ssanchezd@stanford.edu ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY); 7336109Ssanchezd@stanford.edu 7346109Ssanchezd@stanford.edu for (int y = 8; y < 32; y++) 7356109Ssanchezd@stanford.edu ctc->setIntReg(y, tc->readIntReg(y)); 7366109Ssanchezd@stanford.edu #else 7376109Ssanchezd@stanford.edu fatal("sys_clone is not implemented for this ISA\n"); 7386109Ssanchezd@stanford.edu #endif 7396109Ssanchezd@stanford.edu 7406111Ssteve.reinhardt@amd.com // Set up stack register 7416110Ssteve.reinhardt@amd.com ctc->setIntReg(TheISA::StackPointerReg, process->getSyscallArg(tc, 1)); 7426109Ssanchezd@stanford.edu 7436111Ssteve.reinhardt@amd.com // Set up syscall return values in parent and child 7446111Ssteve.reinhardt@amd.com ctc->setIntReg(ReturnValueReg, 0); // return value, child 7456109Ssanchezd@stanford.edu 7466111Ssteve.reinhardt@amd.com // Alpha needs SyscallSuccessReg=0 in child 7476109Ssanchezd@stanford.edu #if THE_ISA == ALPHA_ISA 7486110Ssteve.reinhardt@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 7496109Ssanchezd@stanford.edu #endif 7506109Ssanchezd@stanford.edu 7516111Ssteve.reinhardt@amd.com // In SPARC/Linux, clone returns 0 on pseudo-return register if 7526111Ssteve.reinhardt@amd.com // parent, non-zero if child 7536109Ssanchezd@stanford.edu #if THE_ISA == SPARC_ISA 7546109Ssanchezd@stanford.edu tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 7556109Ssanchezd@stanford.edu ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 7566109Ssanchezd@stanford.edu #endif 7576109Ssanchezd@stanford.edu 7586109Ssanchezd@stanford.edu ctc->setPC(tc->readNextPC()); 7596109Ssanchezd@stanford.edu ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst)); 7606134Sgblack@eecs.umich.edu ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst)); 7616109Ssanchezd@stanford.edu 7626109Ssanchezd@stanford.edu ctc->activate(); 7636109Ssanchezd@stanford.edu 7646109Ssanchezd@stanford.edu // Should return nonzero child TID in parent's syscall return register, 7656109Ssanchezd@stanford.edu // but for our pthread library any non-zero value will work 7666109Ssanchezd@stanford.edu return 1; 7676109Ssanchezd@stanford.edu } else { 7686109Ssanchezd@stanford.edu fatal("Called sys_clone, but no unallocated thread contexts found!\n"); 7696109Ssanchezd@stanford.edu return 0; 7706109Ssanchezd@stanford.edu } 7716109Ssanchezd@stanford.edu} 7726109Ssanchezd@stanford.edu 773