syscall_emul.cc revision 5958
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" 412680Sktlim@umich.edu#include "cpu/thread_context.hh" 421717SN/A#include "cpu/base.hh" 432474SN/A#include "mem/page_table.hh" 44360SN/A#include "sim/process.hh" 45360SN/A 462667Sstever@eecs.umich.edu#include "sim/sim_exit.hh" 47360SN/A 48360SN/Ausing namespace std; 492107SN/Ausing namespace TheISA; 50360SN/A 51360SN/Avoid 523114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc) 53360SN/A{ 542495SN/A DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n", 552680Sktlim@umich.edu curTick,tc->getCpuPtr()->name(), name, 565958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1), 575958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 3)); 58360SN/A 592680Sktlim@umich.edu SyscallReturn retval = (*funcPtr)(this, callnum, process, tc); 60360SN/A 612495SN/A DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n", 622680Sktlim@umich.edu curTick,tc->getCpuPtr()->name(), name, retval.value()); 63360SN/A 641450SN/A if (!(flags & SyscallDesc::SuppressReturnValue)) 655958Sgblack@eecs.umich.edu process->setSyscallReturn(tc, retval); 66360SN/A} 67360SN/A 68360SN/A 691450SN/ASyscallReturn 703114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 712680Sktlim@umich.edu ThreadContext *tc) 72360SN/A{ 731969SN/A fatal("syscall %s (#%d) unimplemented.", desc->name, callnum); 742484SN/A 752484SN/A return 1; 76360SN/A} 77360SN/A 78360SN/A 791450SN/ASyscallReturn 803114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 812680Sktlim@umich.edu ThreadContext *tc) 82360SN/A{ 831969SN/A warn("ignoring syscall %s(%d, %d, ...)", desc->name, 845958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1)); 85360SN/A 861458SN/A return 0; 87360SN/A} 88360SN/A 89360SN/A 901450SN/ASyscallReturn 913114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 922680Sktlim@umich.edu ThreadContext *tc) 93360SN/A{ 942834Sksewell@umich.edu if (tc->exit()) { 955958Sgblack@eecs.umich.edu exitSimLoop("target called exit()", 965958Sgblack@eecs.umich.edu process->getSyscallArg(tc, 0) & 0xff); 972834Sksewell@umich.edu } 98360SN/A 991458SN/A return 1; 100360SN/A} 101360SN/A 102360SN/A 1031450SN/ASyscallReturn 1043114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 105360SN/A{ 1062107SN/A return (int)VMPageSize; 107360SN/A} 108360SN/A 109360SN/A 1101450SN/ASyscallReturn 1115748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 112360SN/A{ 113360SN/A // change brk addr to first arg 1145958Sgblack@eecs.umich.edu Addr new_brk = p->getSyscallArg(tc, 0); 1155748SSteve.Reinhardt@amd.com 1165748SSteve.Reinhardt@amd.com // in Linux at least, brk(0) returns the current break value 1175748SSteve.Reinhardt@amd.com // (note that the syscall and the glibc function have different behavior) 1185748SSteve.Reinhardt@amd.com if (new_brk == 0) 1195748SSteve.Reinhardt@amd.com return p->brk_point; 1205748SSteve.Reinhardt@amd.com 1215748SSteve.Reinhardt@amd.com if (new_brk > p->brk_point) { 1225748SSteve.Reinhardt@amd.com // might need to allocate some new pages 1232474SN/A for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point, 1242474SN/A VMPageSize); !gen.done(); gen.next()) { 1255748SSteve.Reinhardt@amd.com if (!p->pTable->translate(gen.addr())) 1262474SN/A p->pTable->allocate(roundDown(gen.addr(), VMPageSize), 1272474SN/A VMPageSize); 1282474SN/A } 1291450SN/A } 1305748SSteve.Reinhardt@amd.com 1315748SSteve.Reinhardt@amd.com p->brk_point = new_brk; 1321458SN/A DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point); 1331458SN/A return p->brk_point; 134360SN/A} 135360SN/A 136360SN/A 1371450SN/ASyscallReturn 1383114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 139360SN/A{ 1405958Sgblack@eecs.umich.edu int target_fd = p->getSyscallArg(tc, 0); 1411970SN/A int status = close(p->sim_fd(target_fd)); 1421970SN/A if (status >= 0) 1431970SN/A p->free_fd(target_fd); 1441970SN/A return status; 145360SN/A} 146360SN/A 147360SN/A 1481450SN/ASyscallReturn 1493114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 150360SN/A{ 1515958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 1525958Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, 2); 1535958Sgblack@eecs.umich.edu BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes); 154360SN/A 155360SN/A int bytes_read = read(fd, bufArg.bufferPtr(), nbytes); 156360SN/A 157360SN/A if (bytes_read != -1) 1582680Sktlim@umich.edu bufArg.copyOut(tc->getMemPort()); 159360SN/A 1601458SN/A return bytes_read; 161360SN/A} 162360SN/A 1631450SN/ASyscallReturn 1643114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 165360SN/A{ 1665958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 1675958Sgblack@eecs.umich.edu int nbytes = p->getSyscallArg(tc, 2); 1685958Sgblack@eecs.umich.edu BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes); 169360SN/A 1702680Sktlim@umich.edu bufArg.copyIn(tc->getMemPort()); 171360SN/A 172360SN/A int bytes_written = write(fd, bufArg.bufferPtr(), nbytes); 173360SN/A 174360SN/A fsync(fd); 175360SN/A 1761458SN/A return bytes_written; 177360SN/A} 178360SN/A 179360SN/A 1801450SN/ASyscallReturn 1813114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 182360SN/A{ 1835958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 1845958Sgblack@eecs.umich.edu uint64_t offs = p->getSyscallArg(tc, 1); 1855958Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, 2); 186360SN/A 187360SN/A off_t result = lseek(fd, offs, whence); 188360SN/A 1891458SN/A return (result == (off_t)-1) ? -errno : result; 190360SN/A} 191360SN/A 192360SN/A 1931450SN/ASyscallReturn 1944118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 1954118Sgblack@eecs.umich.edu{ 1965958Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, 0)); 1975958Sgblack@eecs.umich.edu uint64_t offset_high = p->getSyscallArg(tc, 1); 1985958Sgblack@eecs.umich.edu uint32_t offset_low = p->getSyscallArg(tc, 2); 1995958Sgblack@eecs.umich.edu Addr result_ptr = p->getSyscallArg(tc, 3); 2005958Sgblack@eecs.umich.edu int whence = p->getSyscallArg(tc, 4); 2014118Sgblack@eecs.umich.edu 2024118Sgblack@eecs.umich.edu uint64_t offset = (offset_high << 32) | offset_low; 2034118Sgblack@eecs.umich.edu 2044118Sgblack@eecs.umich.edu uint64_t result = lseek(fd, offset, whence); 2054118Sgblack@eecs.umich.edu result = TheISA::htog(result); 2064118Sgblack@eecs.umich.edu 2074118Sgblack@eecs.umich.edu if (result == (off_t)-1) { 2084118Sgblack@eecs.umich.edu //The seek failed. 2094118Sgblack@eecs.umich.edu return -errno; 2104118Sgblack@eecs.umich.edu } else { 2114118Sgblack@eecs.umich.edu //The seek succeeded. 2124118Sgblack@eecs.umich.edu //Copy "result" to "result_ptr" 2134118Sgblack@eecs.umich.edu //XXX We'll assume that the size of loff_t is 64 bits on the 2144118Sgblack@eecs.umich.edu //target platform 2154118Sgblack@eecs.umich.edu BufferArg result_buf(result_ptr, sizeof(result)); 2164118Sgblack@eecs.umich.edu memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 2174118Sgblack@eecs.umich.edu result_buf.copyOut(tc->getMemPort()); 2184118Sgblack@eecs.umich.edu return 0; 2194118Sgblack@eecs.umich.edu } 2204118Sgblack@eecs.umich.edu 2214118Sgblack@eecs.umich.edu 2224118Sgblack@eecs.umich.edu return (result == (off_t)-1) ? -errno : result; 2234118Sgblack@eecs.umich.edu} 2244118Sgblack@eecs.umich.edu 2254118Sgblack@eecs.umich.edu 2264118Sgblack@eecs.umich.eduSyscallReturn 2273114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 228360SN/A{ 229360SN/A // given that we don't really implement mmap, munmap is really easy 2301458SN/A return 0; 231360SN/A} 232360SN/A 233360SN/A 234360SN/Aconst char *hostname = "m5.eecs.umich.edu"; 235360SN/A 2361450SN/ASyscallReturn 2373114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 238360SN/A{ 2395958Sgblack@eecs.umich.edu int name_len = p->getSyscallArg(tc, 1); 2405958Sgblack@eecs.umich.edu BufferArg name(p->getSyscallArg(tc, 0), name_len); 241360SN/A 242360SN/A strncpy((char *)name.bufferPtr(), hostname, name_len); 243360SN/A 2442680Sktlim@umich.edu name.copyOut(tc->getMemPort()); 245360SN/A 2461458SN/A return 0; 247360SN/A} 248360SN/A 2491450SN/ASyscallReturn 2505513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2515513SMichael.Adler@intel.com{ 2525513SMichael.Adler@intel.com int result = 0; 2535958Sgblack@eecs.umich.edu unsigned long size = p->getSyscallArg(tc, 1); 2545958Sgblack@eecs.umich.edu BufferArg buf(p->getSyscallArg(tc, 0), size); 2555513SMichael.Adler@intel.com 2565513SMichael.Adler@intel.com // Is current working directory defined? 2575513SMichael.Adler@intel.com string cwd = p->getcwd(); 2585513SMichael.Adler@intel.com if (!cwd.empty()) { 2595513SMichael.Adler@intel.com if (cwd.length() >= size) { 2605513SMichael.Adler@intel.com // Buffer too small 2615513SMichael.Adler@intel.com return -ERANGE; 2625513SMichael.Adler@intel.com } 2635513SMichael.Adler@intel.com strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 2645513SMichael.Adler@intel.com result = cwd.length(); 2655513SMichael.Adler@intel.com } 2665513SMichael.Adler@intel.com else { 2675513SMichael.Adler@intel.com if (getcwd((char *)buf.bufferPtr(), size) != NULL) { 2685513SMichael.Adler@intel.com result = strlen((char *)buf.bufferPtr()); 2695513SMichael.Adler@intel.com } 2705513SMichael.Adler@intel.com else { 2715513SMichael.Adler@intel.com result = -1; 2725513SMichael.Adler@intel.com } 2735513SMichael.Adler@intel.com } 2745513SMichael.Adler@intel.com 2755513SMichael.Adler@intel.com buf.copyOut(tc->getMemPort()); 2765513SMichael.Adler@intel.com 2775513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 2785513SMichael.Adler@intel.com} 2795513SMichael.Adler@intel.com 2805513SMichael.Adler@intel.com 2815513SMichael.Adler@intel.comSyscallReturn 2825513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2835513SMichael.Adler@intel.com{ 2845513SMichael.Adler@intel.com string path; 2855513SMichael.Adler@intel.com 2865958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 2875513SMichael.Adler@intel.com return (TheISA::IntReg)-EFAULT; 2885513SMichael.Adler@intel.com 2895513SMichael.Adler@intel.com // Adjust path for current working directory 2905513SMichael.Adler@intel.com path = p->fullPath(path); 2915513SMichael.Adler@intel.com 2925958Sgblack@eecs.umich.edu size_t bufsiz = p->getSyscallArg(tc, 2); 2935958Sgblack@eecs.umich.edu BufferArg buf(p->getSyscallArg(tc, 1), bufsiz); 2945513SMichael.Adler@intel.com 2955513SMichael.Adler@intel.com int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 2965513SMichael.Adler@intel.com 2975513SMichael.Adler@intel.com buf.copyOut(tc->getMemPort()); 2985513SMichael.Adler@intel.com 2995513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3005513SMichael.Adler@intel.com} 3015513SMichael.Adler@intel.com 3025513SMichael.Adler@intel.comSyscallReturn 3033114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 304511SN/A{ 3051706SN/A string path; 306360SN/A 3075958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 3081450SN/A return (TheISA::IntReg)-EFAULT; 309511SN/A 3103669Sbinkertn@umich.edu // Adjust path for current working directory 3113669Sbinkertn@umich.edu path = p->fullPath(path); 3123669Sbinkertn@umich.edu 313511SN/A int result = unlink(path.c_str()); 3141458SN/A return (result == -1) ? -errno : result; 315511SN/A} 316511SN/A 3175513SMichael.Adler@intel.com 3185513SMichael.Adler@intel.comSyscallReturn 3195513SMichael.Adler@intel.commkdirFunc(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 mode_t mode = p->getSyscallArg(tc, 1); 3305513SMichael.Adler@intel.com 3315513SMichael.Adler@intel.com int result = mkdir(path.c_str(), mode); 3325513SMichael.Adler@intel.com return (result == -1) ? -errno : result; 3335513SMichael.Adler@intel.com} 3345513SMichael.Adler@intel.com 3351450SN/ASyscallReturn 3363114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 337511SN/A{ 3381706SN/A string old_name; 339511SN/A 3405958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0))) 3411458SN/A return -EFAULT; 342511SN/A 3431706SN/A string new_name; 344511SN/A 3455958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1))) 3461458SN/A return -EFAULT; 347511SN/A 3483669Sbinkertn@umich.edu // Adjust path for current working directory 3493669Sbinkertn@umich.edu old_name = p->fullPath(old_name); 3503669Sbinkertn@umich.edu new_name = p->fullPath(new_name); 3513669Sbinkertn@umich.edu 3521706SN/A int64_t result = rename(old_name.c_str(), new_name.c_str()); 3531458SN/A return (result == -1) ? -errno : result; 354511SN/A} 355511SN/A 3561706SN/ASyscallReturn 3573114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3581706SN/A{ 3591706SN/A string path; 3601706SN/A 3615958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 3621706SN/A return -EFAULT; 3631706SN/A 3645958Sgblack@eecs.umich.edu off_t length = p->getSyscallArg(tc, 1); 3651706SN/A 3663669Sbinkertn@umich.edu // Adjust path for current working directory 3673669Sbinkertn@umich.edu path = p->fullPath(path); 3683669Sbinkertn@umich.edu 3691706SN/A int result = truncate(path.c_str(), length); 3701706SN/A return (result == -1) ? -errno : result; 3711706SN/A} 3721706SN/A 3731706SN/ASyscallReturn 3743114Sgblack@eecs.umich.eduftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 3751706SN/A{ 3765958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 3771706SN/A 3781706SN/A if (fd < 0) 3791706SN/A return -EBADF; 3801706SN/A 3815958Sgblack@eecs.umich.edu off_t length = process->getSyscallArg(tc, 1); 3821706SN/A 3831706SN/A int result = ftruncate(fd, length); 3841706SN/A return (result == -1) ? -errno : result; 3851706SN/A} 3861999SN/A 3871999SN/ASyscallReturn 3885513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 3895513SMichael.Adler@intel.com{ 3905513SMichael.Adler@intel.com // Letting the simulated program change the simulator's umask seems like 3915513SMichael.Adler@intel.com // a bad idea. Compromise by just returning the current umask but not 3925513SMichael.Adler@intel.com // changing anything. 3935513SMichael.Adler@intel.com mode_t oldMask = umask(0); 3945513SMichael.Adler@intel.com umask(oldMask); 3955521Snate@binkert.org return (int)oldMask; 3965513SMichael.Adler@intel.com} 3975513SMichael.Adler@intel.com 3985513SMichael.Adler@intel.comSyscallReturn 3993114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 4001999SN/A{ 4011999SN/A string path; 4021999SN/A 4035958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0))) 4041999SN/A return -EFAULT; 4051999SN/A 4061999SN/A /* XXX endianess */ 4075958Sgblack@eecs.umich.edu uint32_t owner = p->getSyscallArg(tc, 1); 4081999SN/A uid_t hostOwner = owner; 4095958Sgblack@eecs.umich.edu uint32_t group = p->getSyscallArg(tc, 2); 4101999SN/A gid_t hostGroup = group; 4111999SN/A 4123669Sbinkertn@umich.edu // Adjust path for current working directory 4133669Sbinkertn@umich.edu path = p->fullPath(path); 4143669Sbinkertn@umich.edu 4151999SN/A int result = chown(path.c_str(), hostOwner, hostGroup); 4161999SN/A return (result == -1) ? -errno : result; 4171999SN/A} 4181999SN/A 4191999SN/ASyscallReturn 4203114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 4211999SN/A{ 4225958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 4231999SN/A 4241999SN/A if (fd < 0) 4251999SN/A return -EBADF; 4261999SN/A 4271999SN/A /* XXX endianess */ 4285958Sgblack@eecs.umich.edu uint32_t owner = process->getSyscallArg(tc, 1); 4291999SN/A uid_t hostOwner = owner; 4305958Sgblack@eecs.umich.edu uint32_t group = process->getSyscallArg(tc, 2); 4311999SN/A gid_t hostGroup = group; 4321999SN/A 4331999SN/A int result = fchown(fd, hostOwner, hostGroup); 4341999SN/A return (result == -1) ? -errno : result; 4351999SN/A} 4362093SN/A 4372093SN/A 4382093SN/ASyscallReturn 4393114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 4403079Sstever@eecs.umich.edu{ 4415958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 4423079Sstever@eecs.umich.edu if (fd < 0) 4433079Sstever@eecs.umich.edu return -EBADF; 4443079Sstever@eecs.umich.edu 4455958Sgblack@eecs.umich.edu Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0)); 4465282Srstrong@cs.ucsd.edu 4473079Sstever@eecs.umich.edu int result = dup(fd); 4485282Srstrong@cs.ucsd.edu return (result == -1) ? -errno : process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false); 4493079Sstever@eecs.umich.edu} 4503079Sstever@eecs.umich.edu 4513079Sstever@eecs.umich.edu 4523079Sstever@eecs.umich.eduSyscallReturn 4533114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, 4542680Sktlim@umich.edu ThreadContext *tc) 4552093SN/A{ 4565958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 4572093SN/A 4582093SN/A if (fd < 0 || process->sim_fd(fd) < 0) 4592093SN/A return -EBADF; 4602093SN/A 4615958Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, 1); 4622093SN/A switch (cmd) { 4632093SN/A case 0: // F_DUPFD 4642093SN/A // if we really wanted to support this, we'd need to do it 4652093SN/A // in the target fd space. 4662093SN/A warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd); 4672093SN/A return -EMFILE; 4682093SN/A 4692093SN/A case 1: // F_GETFD (get close-on-exec flag) 4702093SN/A case 2: // F_SETFD (set close-on-exec flag) 4712093SN/A return 0; 4722093SN/A 4732093SN/A case 3: // F_GETFL (get file flags) 4742093SN/A case 4: // F_SETFL (set file flags) 4752093SN/A // not sure if this is totally valid, but we'll pass it through 4762093SN/A // to the underlying OS 4772093SN/A warn("fcntl(%d, %d) passed through to host\n", fd, cmd); 4782093SN/A return fcntl(process->sim_fd(fd), cmd); 4792093SN/A // return 0; 4802093SN/A 4812093SN/A case 7: // F_GETLK (get lock) 4822093SN/A case 8: // F_SETLK (set lock) 4832093SN/A case 9: // F_SETLKW (set lock and wait) 4842093SN/A // don't mess with file locking... just act like it's OK 4852093SN/A warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd); 4862093SN/A return 0; 4872093SN/A 4882093SN/A default: 4892093SN/A warn("Unknown fcntl command %d\n", cmd); 4902093SN/A return 0; 4912093SN/A } 4922093SN/A} 4932093SN/A 4942238SN/ASyscallReturn 4953114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process, 4962687Sksewell@umich.edu ThreadContext *tc) 4972687Sksewell@umich.edu{ 4985958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 4992687Sksewell@umich.edu 5002687Sksewell@umich.edu if (fd < 0 || process->sim_fd(fd) < 0) 5012687Sksewell@umich.edu return -EBADF; 5022687Sksewell@umich.edu 5035958Sgblack@eecs.umich.edu int cmd = process->getSyscallArg(tc, 1); 5042687Sksewell@umich.edu switch (cmd) { 5052687Sksewell@umich.edu case 33: //F_GETLK64 5062687Sksewell@umich.edu warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd); 5072687Sksewell@umich.edu return -EMFILE; 5082687Sksewell@umich.edu 5092687Sksewell@umich.edu case 34: // F_SETLK64 5102687Sksewell@umich.edu case 35: // F_SETLKW64 5112687Sksewell@umich.edu warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd); 5122687Sksewell@umich.edu return -EMFILE; 5132687Sksewell@umich.edu 5142687Sksewell@umich.edu default: 5152687Sksewell@umich.edu // not sure if this is totally valid, but we'll pass it through 5162687Sksewell@umich.edu // to the underlying OS 5172687Sksewell@umich.edu warn("fcntl64(%d, %d) passed through to host\n", fd, cmd); 5182687Sksewell@umich.edu return fcntl(process->sim_fd(fd), cmd); 5192687Sksewell@umich.edu // return 0; 5202687Sksewell@umich.edu } 5212687Sksewell@umich.edu} 5222687Sksewell@umich.edu 5232687Sksewell@umich.eduSyscallReturn 5243114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5252680Sktlim@umich.edu ThreadContext *tc) 5262238SN/A{ 5272238SN/A int fds[2], sim_fds[2]; 5282238SN/A int pipe_retval = pipe(fds); 5292093SN/A 5302238SN/A if (pipe_retval < 0) { 5312238SN/A // error 5322238SN/A return pipe_retval; 5332238SN/A } 5342238SN/A 5355282Srstrong@cs.ucsd.edu sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true); 5365282Srstrong@cs.ucsd.edu sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true); 5372238SN/A 5385282Srstrong@cs.ucsd.edu process->setReadPipeSource(sim_fds[0], sim_fds[1]); 5392238SN/A // Alpha Linux convention for pipe() is that fd[0] is returned as 5402238SN/A // the return value of the function, and fd[1] is returned in r20. 5412680Sktlim@umich.edu tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]); 5422238SN/A return sim_fds[0]; 5432238SN/A} 5442238SN/A 5452238SN/A 5462238SN/ASyscallReturn 5473114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5482680Sktlim@umich.edu ThreadContext *tc) 5492238SN/A{ 5502238SN/A // Make up a PID. There's no interprocess communication in 5512238SN/A // fake_syscall mode, so there's no way for a process to know it's 5522238SN/A // not getting a unique value. 5532238SN/A 5543114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 5553114Sgblack@eecs.umich.edu return process->pid(); 5562238SN/A} 5572238SN/A 5582238SN/A 5592238SN/ASyscallReturn 5603114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5612680Sktlim@umich.edu ThreadContext *tc) 5622238SN/A{ 5632238SN/A // Make up a UID and EUID... it shouldn't matter, and we want the 5642238SN/A // simulation to be deterministic. 5652238SN/A 5662238SN/A // EUID goes in r20. 5673114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID 5685543Ssaidi@eecs.umich.edu return process->uid(); // UID 5692238SN/A} 5702238SN/A 5712238SN/A 5722238SN/ASyscallReturn 5733114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5742680Sktlim@umich.edu ThreadContext *tc) 5752238SN/A{ 5762238SN/A // Get current group ID. EGID goes in r20. 5773114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID 5783114Sgblack@eecs.umich.edu return process->gid(); 5792238SN/A} 5802238SN/A 5812238SN/A 5822238SN/ASyscallReturn 5833114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5842680Sktlim@umich.edu ThreadContext *tc) 5852238SN/A{ 5862238SN/A // can't fathom why a benchmark would call this. 5875958Sgblack@eecs.umich.edu warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0)); 5882238SN/A return 0; 5892238SN/A} 5902238SN/A 5912238SN/ASyscallReturn 5923114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5932680Sktlim@umich.edu ThreadContext *tc) 5942238SN/A{ 5952238SN/A // Make up a PID. There's no interprocess communication in 5962238SN/A // fake_syscall mode, so there's no way for a process to know it's 5972238SN/A // not getting a unique value. 5982238SN/A 5993114Sgblack@eecs.umich.edu tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID 6003114Sgblack@eecs.umich.edu return process->pid(); 6012238SN/A} 6022238SN/A 6032238SN/ASyscallReturn 6043114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6052680Sktlim@umich.edu ThreadContext *tc) 6062238SN/A{ 6073114Sgblack@eecs.umich.edu return process->ppid(); 6082238SN/A} 6092238SN/A 6102238SN/ASyscallReturn 6113114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6122680Sktlim@umich.edu ThreadContext *tc) 6132238SN/A{ 6145543Ssaidi@eecs.umich.edu return process->uid(); // UID 6152238SN/A} 6162238SN/A 6172238SN/ASyscallReturn 6183114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6192680Sktlim@umich.edu ThreadContext *tc) 6202238SN/A{ 6215543Ssaidi@eecs.umich.edu return process->euid(); // UID 6222238SN/A} 6232238SN/A 6242238SN/ASyscallReturn 6253114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6262680Sktlim@umich.edu ThreadContext *tc) 6272238SN/A{ 6283114Sgblack@eecs.umich.edu return process->gid(); 6292238SN/A} 6302238SN/A 6312238SN/ASyscallReturn 6323114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6332680Sktlim@umich.edu ThreadContext *tc) 6342238SN/A{ 6353114Sgblack@eecs.umich.edu return process->egid(); 6362238SN/A} 6372238SN/A 6382238SN/A 639