syscall_emul.cc revision 10223
12SN/A/* 21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292SN/A * Ali Saidi 302SN/A */ 312SN/A 32456SN/A#include <fcntl.h> 332SN/A#include <unistd.h> 342SN/A 352SN/A#include <cstdio> 362SN/A#include <iostream> 37148SN/A#include <string> 3856SN/A 395889Snate@binkert.org#include "arch/utility.hh" 40441SN/A#include "base/chunk_generator.hh" 4156SN/A#include "base/trace.hh" 4256SN/A#include "config/the_isa.hh" 4356SN/A#include "cpu/base.hh" 44441SN/A#include "cpu/thread_context.hh" 45433SN/A#include "debug/SyscallVerbose.hh" 462SN/A#include "mem/page_table.hh" 472SN/A#include "sim/process.hh" 482SN/A#include "sim/sim_exit.hh" 49729SN/A#include "sim/syscall_emul.hh" 50388SN/A#include "sim/system.hh" 515887Snate@binkert.org 525887Snate@binkert.orgusing namespace std; 535887Snate@binkert.orgusing namespace TheISA; 545887Snate@binkert.org 555887Snate@binkert.orgvoid 56388SN/ASyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc) 575887Snate@binkert.org{ 585887Snate@binkert.org#if TRACING_ON 59388SN/A int index = 0; 60388SN/A#endif 615887Snate@binkert.org DPRINTFR(SyscallVerbose, 625887Snate@binkert.org "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n", 63441SN/A curTick(), tc->getCpuPtr()->name(), name, 645887Snate@binkert.org process->getSyscallArg(tc, index), 655887Snate@binkert.org process->getSyscallArg(tc, index), 66441SN/A process->getSyscallArg(tc, index), 67441SN/A process->getSyscallArg(tc, index)); 68388SN/A 695886Snate@binkert.org SyscallReturn retval = (*funcPtr)(this, callnum, process, tc); 70388SN/A 715887Snate@binkert.org DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n", 725887Snate@binkert.org curTick(), tc->getCpuPtr()->name(), name, retval.encodedValue()); 735887Snate@binkert.org 745887Snate@binkert.org if (!(flags & SyscallDesc::SuppressReturnValue)) 755887Snate@binkert.org process->setSyscallReturn(tc, retval); 765887Snate@binkert.org} 775887Snate@binkert.org 785887Snate@binkert.org 795887Snate@binkert.orgSyscallReturn 805887Snate@binkert.orgunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 815887Snate@binkert.org ThreadContext *tc) 82388SN/A{ 83388SN/A fatal("syscall %s (#%d) unimplemented.", desc->name, callnum); 84388SN/A 855889Snate@binkert.org return 1; 865889Snate@binkert.org} 875889Snate@binkert.org 885889Snate@binkert.org 895889Snate@binkert.orgSyscallReturn 905889Snate@binkert.orgignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 915886Snate@binkert.org ThreadContext *tc) 92388SN/A{ 936130Snate@binkert.org int index = 0; 94388SN/A warn("ignoring syscall %s(%d, %d, ...)", desc->name, 95388SN/A process->getSyscallArg(tc, index), process->getSyscallArg(tc, index)); 965886Snate@binkert.org 975886Snate@binkert.org return 0; 98388SN/A} 995887Snate@binkert.org 1005887Snate@binkert.org 1015887Snate@binkert.orgSyscallReturn 102388SN/AignoreWarnOnceFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 103388SN/A ThreadContext *tc) 1045886Snate@binkert.org{ 1055886Snate@binkert.org int index = 0; 1065886Snate@binkert.org warn_once("ignoring syscall %s(%d, %d, ...)", desc->name, 1075887Snate@binkert.org process->getSyscallArg(tc, index), process->getSyscallArg(tc, index)); 1085887Snate@binkert.org 1095887Snate@binkert.org return 0; 1105886Snate@binkert.org} 1115886Snate@binkert.org 1125889Snate@binkert.org 1135889Snate@binkert.orgSyscallReturn 1145889Snate@binkert.orgexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1155889Snate@binkert.org ThreadContext *tc) 1166026Snate@binkert.org{ 1176026Snate@binkert.org if (process->system->numRunningContexts() == 1) { 1186026Snate@binkert.org // Last running context... exit simulator 1196026Snate@binkert.org int index = 0; 1206026Snate@binkert.org exitSimLoop("target called exit()", 1216026Snate@binkert.org process->getSyscallArg(tc, index) & 0xff); 1226026Snate@binkert.org } else { 1236026Snate@binkert.org // other running threads... just halt this one 1245889Snate@binkert.org tc->halt(); 1255889Snate@binkert.org } 1265889Snate@binkert.org 1275889Snate@binkert.org return 1; 1285886Snate@binkert.org} 1295889Snate@binkert.org 130582SN/A 1315889Snate@binkert.orgSyscallReturn 1325889Snate@binkert.orgexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 1335889Snate@binkert.org ThreadContext *tc) 134582SN/A{ 135582SN/A // really should just halt all thread contexts belonging to this 1365886Snate@binkert.org // process in case there's another process running... 137388SN/A int index = 0; 138388SN/A exitSimLoop("target called exit()", 139388SN/A process->getSyscallArg(tc, index) & 0xff); 1406026Snate@binkert.org 1416026Snate@binkert.org return 1; 1426026Snate@binkert.org} 1436026Snate@binkert.org 1446026Snate@binkert.org 1456026Snate@binkert.orgSyscallReturn 1466026Snate@binkert.orggetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 1476026Snate@binkert.org{ 1486026Snate@binkert.org return (int)VMPageSize; 1496026Snate@binkert.org} 1506026Snate@binkert.org 1516026Snate@binkert.org 1526026Snate@binkert.orgSyscallReturn 1536026Snate@binkert.orgbrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 1546026Snate@binkert.org{ 1556026Snate@binkert.org // change brk addr to first arg 1566026Snate@binkert.org int index = 0; 1576026Snate@binkert.org Addr new_brk = p->getSyscallArg(tc, index); 1582SN/A 1595886Snate@binkert.org // in Linux at least, brk(0) returns the current break value 1602SN/A // (note that the syscall and the glibc function have different behavior) 161388SN/A if (new_brk == 0) 162388SN/A return p->brk_point; 1632SN/A 1642SN/A if (new_brk > p->brk_point) { 1652SN/A // might need to allocate some new pages 1662SN/A for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point, 1672SN/A VMPageSize); !gen.done(); gen.next()) { 1682SN/A if (!p->pTable->translate(gen.addr())) 1692SN/A p->allocateMem(roundDown(gen.addr(), VMPageSize), VMPageSize); 1705599Snate@binkert.org 1715599Snate@binkert.org // if the address is already there, zero it out 1722SN/A else { 1732SN/A uint8_t zero = 0; 1742SN/A SETranslatingPortProxy &tp = tc->getMemProxy(); 1752SN/A 1762SN/A // split non-page aligned accesses 1772SN/A Addr next_page = roundUp(gen.addr(), VMPageSize); 1782SN/A uint32_t size_needed = next_page - gen.addr(); 1792SN/A tp.memsetBlob(gen.addr(), zero, size_needed); 1802SN/A if (gen.addr() + VMPageSize > next_page && 1812SN/A next_page < new_brk && 1822SN/A p->pTable->translate(next_page)) 1832SN/A { 184388SN/A size_needed = VMPageSize - size_needed; 1855886Snate@binkert.org tp.memsetBlob(next_page, zero, size_needed); 1862SN/A } 1876000Snate@binkert.org } 188582SN/A } 189695SN/A } 190388SN/A 191388SN/A p->brk_point = new_brk; 192388SN/A DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point); 193388SN/A return p->brk_point; 1942SN/A} 1957462Snate@binkert.org 196388SN/A 197388SN/ASyscallReturn 198388SN/AcloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 1992SN/A{ 200388SN/A int index = 0; 2012SN/A int target_fd = p->getSyscallArg(tc, index); 2022SN/A int sim_fd = p->sim_fd(target_fd); 2036001Snate@binkert.org int status = 0; 2046001Snate@binkert.org if (sim_fd > 2) 2056001Snate@binkert.org status = close(sim_fd); 2066001Snate@binkert.org if (status >= 0) 2076001Snate@binkert.org p->free_fd(target_fd); 2086001Snate@binkert.org return status; 2096128Snate@binkert.org} 2106001Snate@binkert.org 2116001Snate@binkert.org 2126001Snate@binkert.orgSyscallReturn 2136001Snate@binkert.orgreadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2146001Snate@binkert.org{ 2156001Snate@binkert.org int index = 0; 2166001Snate@binkert.org int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2176001Snate@binkert.org Addr bufPtr = p->getSyscallArg(tc, index); 2186001Snate@binkert.org int nbytes = p->getSyscallArg(tc, index); 2196128Snate@binkert.org BufferArg bufArg(bufPtr, nbytes); 2206001Snate@binkert.org 2216001Snate@binkert.org int bytes_read = read(fd, bufArg.bufferPtr(), nbytes); 2226001Snate@binkert.org 2236001Snate@binkert.org if (bytes_read != -1) 2246001Snate@binkert.org bufArg.copyOut(tc->getMemProxy()); 2256001Snate@binkert.org 2266001Snate@binkert.org return bytes_read; 2276001Snate@binkert.org} 2286001Snate@binkert.org 2296128Snate@binkert.orgSyscallReturn 2306001Snate@binkert.orgwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2316001Snate@binkert.org{ 2326001Snate@binkert.org int index = 0; 2336001Snate@binkert.org int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2346001Snate@binkert.org Addr bufPtr = p->getSyscallArg(tc, index); 2356001Snate@binkert.org int nbytes = p->getSyscallArg(tc, index); 2366001Snate@binkert.org BufferArg bufArg(bufPtr, nbytes); 2376001Snate@binkert.org 238695SN/A bufArg.copyIn(tc->getMemProxy()); 2397831Snate@binkert.org 2407831Snate@binkert.org int bytes_written = write(fd, bufArg.bufferPtr(), nbytes); 2417831Snate@binkert.org 2427831Snate@binkert.org fsync(fd); 2437831Snate@binkert.org 2447831Snate@binkert.org return bytes_written; 2457831Snate@binkert.org} 2467831Snate@binkert.org 2477831Snate@binkert.org 2487831Snate@binkert.orgSyscallReturn 2497831Snate@binkert.orglseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2507831Snate@binkert.org{ 2517831Snate@binkert.org int index = 0; 2527831Snate@binkert.org int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2537831Snate@binkert.org uint64_t offs = p->getSyscallArg(tc, index); 2547831Snate@binkert.org int whence = p->getSyscallArg(tc, index); 2557831Snate@binkert.org 2567831Snate@binkert.org off_t result = lseek(fd, offs, whence); 2577831Snate@binkert.org 2587831Snate@binkert.org return (result == (off_t)-1) ? -errno : result; 2597831Snate@binkert.org} 2607831Snate@binkert.org 2617831Snate@binkert.org 2627831Snate@binkert.orgSyscallReturn 2637831Snate@binkert.org_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2647831Snate@binkert.org{ 2657831Snate@binkert.org int index = 0; 2667831Snate@binkert.org int fd = p->sim_fd(p->getSyscallArg(tc, index)); 2677831Snate@binkert.org uint64_t offset_high = p->getSyscallArg(tc, index); 2687831Snate@binkert.org uint32_t offset_low = p->getSyscallArg(tc, index); 2697831Snate@binkert.org Addr result_ptr = p->getSyscallArg(tc, index); 2707831Snate@binkert.org int whence = p->getSyscallArg(tc, index); 2717831Snate@binkert.org 2727831Snate@binkert.org uint64_t offset = (offset_high << 32) | offset_low; 2737831Snate@binkert.org 2747831Snate@binkert.org uint64_t result = lseek(fd, offset, whence); 2757831Snate@binkert.org result = TheISA::htog(result); 2767831Snate@binkert.org 2777831Snate@binkert.org if (result == (off_t)-1) { 2787831Snate@binkert.org //The seek failed. 2797831Snate@binkert.org return -errno; 2807831Snate@binkert.org } else { 2817831Snate@binkert.org // The seek succeeded. 2827831Snate@binkert.org // Copy "result" to "result_ptr" 2837831Snate@binkert.org // XXX We'll assume that the size of loff_t is 64 bits on the 2847831Snate@binkert.org // target platform 2857831Snate@binkert.org BufferArg result_buf(result_ptr, sizeof(result)); 2867831Snate@binkert.org memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 2877831Snate@binkert.org result_buf.copyOut(tc->getMemProxy()); 2887831Snate@binkert.org return 0; 2897831Snate@binkert.org } 2907831Snate@binkert.org 2917831Snate@binkert.org 2927831Snate@binkert.org return (result == (off_t)-1) ? -errno : result; 2937831Snate@binkert.org} 2947831Snate@binkert.org 2957831Snate@binkert.org 2967831Snate@binkert.orgSyscallReturn 2977831Snate@binkert.orgmunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 2987831Snate@binkert.org{ 2997831Snate@binkert.org // given that we don't really implement mmap, munmap is really easy 3007831Snate@binkert.org return 0; 3017831Snate@binkert.org} 3027831Snate@binkert.org 3037831Snate@binkert.org 3047831Snate@binkert.orgconst char *hostname = "m5.eecs.umich.edu"; 3057831Snate@binkert.org 3067831Snate@binkert.orgSyscallReturn 3077831Snate@binkert.orggethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3087831Snate@binkert.org{ 3097831Snate@binkert.org int index = 0; 3107831Snate@binkert.org Addr bufPtr = p->getSyscallArg(tc, index); 3117831Snate@binkert.org int name_len = p->getSyscallArg(tc, index); 3127831Snate@binkert.org BufferArg name(bufPtr, name_len); 3137831Snate@binkert.org 3147831Snate@binkert.org strncpy((char *)name.bufferPtr(), hostname, name_len); 3157831Snate@binkert.org 3167831Snate@binkert.org name.copyOut(tc->getMemProxy()); 3177831Snate@binkert.org 3187831Snate@binkert.org return 0; 3197831Snate@binkert.org} 3207831Snate@binkert.org 3217831Snate@binkert.orgSyscallReturn 322388SN/AgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 323388SN/A{ 324388SN/A int result = 0; 325388SN/A int index = 0; 326388SN/A Addr bufPtr = p->getSyscallArg(tc, index); 327388SN/A unsigned long size = p->getSyscallArg(tc, index); 328388SN/A BufferArg buf(bufPtr, size); 3297461Snate@binkert.org 330388SN/A // Is current working directory defined? 331388SN/A string cwd = p->getcwd(); 332388SN/A if (!cwd.empty()) { 333388SN/A if (cwd.length() >= size) { 334388SN/A // Buffer too small 335388SN/A return -ERANGE; 336388SN/A } 337388SN/A strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 3387461Snate@binkert.org result = cwd.length(); 339388SN/A } 340388SN/A else { 341388SN/A if (getcwd((char *)buf.bufferPtr(), size) != NULL) { 342388SN/A result = strlen((char *)buf.bufferPtr()); 343388SN/A } 344388SN/A else { 345388SN/A result = -1; 346388SN/A } 347695SN/A } 3487461Snate@binkert.org 349388SN/A buf.copyOut(tc->getMemProxy()); 3507461Snate@binkert.org 3517461Snate@binkert.org return (result == -1) ? -errno : result; 3527461Snate@binkert.org} 353388SN/A 354388SN/A/// Target open() handler. 355388SN/ASyscallReturn 356388SN/AreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 357142SN/A ThreadContext *tc) 3586000Snate@binkert.org{ 3596000Snate@binkert.org return readlinkFunc(desc, callnum, process, tc, 0); 3606000Snate@binkert.org} 3616000Snate@binkert.org 3626000Snate@binkert.orgSyscallReturn 3636000Snate@binkert.orgreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc, 3646000Snate@binkert.org int index) 3656000Snate@binkert.org{ 3666000Snate@binkert.org string path; 3676000Snate@binkert.org 3686000Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 3696000Snate@binkert.org return -EFAULT; 3706000Snate@binkert.org 3716000Snate@binkert.org // Adjust path for current working directory 3726000Snate@binkert.org path = p->fullPath(path); 3736000Snate@binkert.org 3746000Snate@binkert.org Addr bufPtr = p->getSyscallArg(tc, index); 3756000Snate@binkert.org size_t bufsiz = p->getSyscallArg(tc, index); 3766000Snate@binkert.org 3776000Snate@binkert.org BufferArg buf(bufPtr, bufsiz); 3786000Snate@binkert.org 3796000Snate@binkert.org int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 3806000Snate@binkert.org 3816000Snate@binkert.org buf.copyOut(tc->getMemProxy()); 3826000Snate@binkert.org 3836000Snate@binkert.org return (result == -1) ? -errno : result; 3846000Snate@binkert.org} 3856000Snate@binkert.org 3866000Snate@binkert.orgSyscallReturn 3876000Snate@binkert.orgunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 3886000Snate@binkert.org{ 3896227Snate@binkert.org string path; 3906000Snate@binkert.org 3916000Snate@binkert.org int index = 0; 3926000Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 3936000Snate@binkert.org return -EFAULT; 3946000Snate@binkert.org 3956000Snate@binkert.org // Adjust path for current working directory 3966000Snate@binkert.org path = p->fullPath(path); 3976000Snate@binkert.org 3986000Snate@binkert.org int result = unlink(path.c_str()); 3996000Snate@binkert.org return (result == -1) ? -errno : result; 4006000Snate@binkert.org} 4016000Snate@binkert.org 4026001Snate@binkert.org 4032SN/ASyscallReturn 4045887Snate@binkert.orgmkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 405695SN/A{ 4065887Snate@binkert.org string path; 4075887Snate@binkert.org 4085886Snate@binkert.org int index = 0; 4095886Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 4105886Snate@binkert.org return -EFAULT; 4115889Snate@binkert.org 412695SN/A // Adjust path for current working directory 413695SN/A path = p->fullPath(path); 4145599Snate@binkert.org 4155887Snate@binkert.org mode_t mode = p->getSyscallArg(tc, index); 4165886Snate@binkert.org 4177462Snate@binkert.org int result = mkdir(path.c_str(), mode); 4185886Snate@binkert.org return (result == -1) ? -errno : result; 419695SN/A} 420695SN/A 4215887Snate@binkert.orgSyscallReturn 422695SN/ArenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 4236001Snate@binkert.org{ 4246001Snate@binkert.org string old_name; 4256001Snate@binkert.org 4266001Snate@binkert.org int index = 0; 4276001Snate@binkert.org if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index))) 428695SN/A return -EFAULT; 4296001Snate@binkert.org 4306001Snate@binkert.org string new_name; 4316001Snate@binkert.org 4326001Snate@binkert.org if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index))) 4336001Snate@binkert.org return -EFAULT; 4346001Snate@binkert.org 4356001Snate@binkert.org // Adjust path for current working directory 4366001Snate@binkert.org old_name = p->fullPath(old_name); 4376001Snate@binkert.org new_name = p->fullPath(new_name); 438695SN/A 4392SN/A int64_t result = rename(old_name.c_str(), new_name.c_str()); 4402SN/A return (result == -1) ? -errno : result; 441695SN/A} 4422SN/A 443456SN/ASyscallReturn 444695SN/AtruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 445456SN/A{ 4465887Snate@binkert.org string path; 4475887Snate@binkert.org 448695SN/A int index = 0; 4495886Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 4505886Snate@binkert.org return -EFAULT; 451695SN/A 452695SN/A off_t length = p->getSyscallArg(tc, index); 453695SN/A 454695SN/A // Adjust path for current working directory 455456SN/A path = p->fullPath(path); 456456SN/A 457456SN/A int result = truncate(path.c_str(), length); 458394SN/A return (result == -1) ? -errno : result; 459148SN/A} 460148SN/A 461148SN/ASyscallReturn 462148SN/AftruncateFunc(SyscallDesc *desc, int num, 4637811Ssteve.reinhardt@amd.com LiveProcess *process, ThreadContext *tc) 464{ 465 int index = 0; 466 int fd = process->sim_fd(process->getSyscallArg(tc, index)); 467 468 if (fd < 0) 469 return -EBADF; 470 471 off_t length = process->getSyscallArg(tc, index); 472 473 int result = ftruncate(fd, length); 474 return (result == -1) ? -errno : result; 475} 476 477SyscallReturn 478truncate64Func(SyscallDesc *desc, int num, 479 LiveProcess *process, ThreadContext *tc) 480{ 481 int index = 0; 482 string path; 483 484 if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index))) 485 return -EFAULT; 486 487 int64_t length = process->getSyscallArg(tc, index, 64); 488 489 // Adjust path for current working directory 490 path = process->fullPath(path); 491 492#if NO_STAT64 493 int result = truncate(path.c_str(), length); 494#else 495 int result = truncate64(path.c_str(), length); 496#endif 497 return (result == -1) ? -errno : result; 498} 499 500SyscallReturn 501ftruncate64Func(SyscallDesc *desc, int num, 502 LiveProcess *process, ThreadContext *tc) 503{ 504 int index = 0; 505 int fd = process->sim_fd(process->getSyscallArg(tc, index)); 506 507 if (fd < 0) 508 return -EBADF; 509 510 int64_t length = process->getSyscallArg(tc, index, 64); 511 512#if NO_STAT64 513 int result = ftruncate(fd, length); 514#else 515 int result = ftruncate64(fd, length); 516#endif 517 return (result == -1) ? -errno : result; 518} 519 520SyscallReturn 521umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 522{ 523 // Letting the simulated program change the simulator's umask seems like 524 // a bad idea. Compromise by just returning the current umask but not 525 // changing anything. 526 mode_t oldMask = umask(0); 527 umask(oldMask); 528 return (int)oldMask; 529} 530 531SyscallReturn 532chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 533{ 534 string path; 535 536 int index = 0; 537 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 538 return -EFAULT; 539 540 /* XXX endianess */ 541 uint32_t owner = p->getSyscallArg(tc, index); 542 uid_t hostOwner = owner; 543 uint32_t group = p->getSyscallArg(tc, index); 544 gid_t hostGroup = group; 545 546 // Adjust path for current working directory 547 path = p->fullPath(path); 548 549 int result = chown(path.c_str(), hostOwner, hostGroup); 550 return (result == -1) ? -errno : result; 551} 552 553SyscallReturn 554fchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 555{ 556 int index = 0; 557 int fd = process->sim_fd(process->getSyscallArg(tc, index)); 558 559 if (fd < 0) 560 return -EBADF; 561 562 /* XXX endianess */ 563 uint32_t owner = process->getSyscallArg(tc, index); 564 uid_t hostOwner = owner; 565 uint32_t group = process->getSyscallArg(tc, index); 566 gid_t hostGroup = group; 567 568 int result = fchown(fd, hostOwner, hostGroup); 569 return (result == -1) ? -errno : result; 570} 571 572 573SyscallReturn 574dupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc) 575{ 576 int index = 0; 577 int fd = process->sim_fd(process->getSyscallArg(tc, index)); 578 if (fd < 0) 579 return -EBADF; 580 581 Process::FdMap *fdo = process->sim_fd_obj(fd); 582 583 int result = dup(fd); 584 return (result == -1) ? -errno : 585 process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false); 586} 587 588 589SyscallReturn 590fcntlFunc(SyscallDesc *desc, int num, LiveProcess *process, 591 ThreadContext *tc) 592{ 593 int index = 0; 594 int fd = process->getSyscallArg(tc, index); 595 596 if (fd < 0 || process->sim_fd(fd) < 0) 597 return -EBADF; 598 599 int cmd = process->getSyscallArg(tc, index); 600 switch (cmd) { 601 case 0: // F_DUPFD 602 // if we really wanted to support this, we'd need to do it 603 // in the target fd space. 604 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd); 605 return -EMFILE; 606 607 case 1: // F_GETFD (get close-on-exec flag) 608 case 2: // F_SETFD (set close-on-exec flag) 609 return 0; 610 611 case 3: // F_GETFL (get file flags) 612 case 4: // F_SETFL (set file flags) 613 // not sure if this is totally valid, but we'll pass it through 614 // to the underlying OS 615 warn("fcntl(%d, %d) passed through to host\n", fd, cmd); 616 return fcntl(process->sim_fd(fd), cmd); 617 // return 0; 618 619 case 7: // F_GETLK (get lock) 620 case 8: // F_SETLK (set lock) 621 case 9: // F_SETLKW (set lock and wait) 622 // don't mess with file locking... just act like it's OK 623 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd); 624 return 0; 625 626 default: 627 warn("Unknown fcntl command %d\n", cmd); 628 return 0; 629 } 630} 631 632SyscallReturn 633fcntl64Func(SyscallDesc *desc, int num, LiveProcess *process, 634 ThreadContext *tc) 635{ 636 int index = 0; 637 int fd = process->getSyscallArg(tc, index); 638 639 if (fd < 0 || process->sim_fd(fd) < 0) 640 return -EBADF; 641 642 int cmd = process->getSyscallArg(tc, index); 643 switch (cmd) { 644 case 33: //F_GETLK64 645 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd); 646 return -EMFILE; 647 648 case 34: // F_SETLK64 649 case 35: // F_SETLKW64 650 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd); 651 return -EMFILE; 652 653 default: 654 // not sure if this is totally valid, but we'll pass it through 655 // to the underlying OS 656 warn("fcntl64(%d, %d) passed through to host\n", fd, cmd); 657 return fcntl(process->sim_fd(fd), cmd); 658 // return 0; 659 } 660} 661 662SyscallReturn 663pipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 664 ThreadContext *tc) 665{ 666 int fds[2], sim_fds[2]; 667 int pipe_retval = pipe(fds); 668 669 if (pipe_retval < 0) { 670 // error 671 return pipe_retval; 672 } 673 674 sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true); 675 sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true); 676 677 process->setReadPipeSource(sim_fds[0], sim_fds[1]); 678 // Alpha Linux convention for pipe() is that fd[0] is returned as 679 // the return value of the function, and fd[1] is returned in r20. 680 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]); 681 return sim_fds[0]; 682} 683 684 685SyscallReturn 686getpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 687 ThreadContext *tc) 688{ 689 // Make up a PID. There's no interprocess communication in 690 // fake_syscall mode, so there's no way for a process to know it's 691 // not getting a unique value. 692 693 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 694 return process->pid(); 695} 696 697 698SyscallReturn 699getuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 700 ThreadContext *tc) 701{ 702 // Make up a UID and EUID... it shouldn't matter, and we want the 703 // simulation to be deterministic. 704 705 // EUID goes in r20. 706 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID 707 return process->uid(); // UID 708} 709 710 711SyscallReturn 712getgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 713 ThreadContext *tc) 714{ 715 // Get current group ID. EGID goes in r20. 716 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID 717 return process->gid(); 718} 719 720 721SyscallReturn 722setuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 723 ThreadContext *tc) 724{ 725 // can't fathom why a benchmark would call this. 726 int index = 0; 727 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index)); 728 return 0; 729} 730 731SyscallReturn 732getpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 733 ThreadContext *tc) 734{ 735 // Make up a PID. There's no interprocess communication in 736 // fake_syscall mode, so there's no way for a process to know it's 737 // not getting a unique value. 738 739 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID 740 return process->pid(); 741} 742 743SyscallReturn 744getppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 745 ThreadContext *tc) 746{ 747 return process->ppid(); 748} 749 750SyscallReturn 751getuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 752 ThreadContext *tc) 753{ 754 return process->uid(); // UID 755} 756 757SyscallReturn 758geteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 759 ThreadContext *tc) 760{ 761 return process->euid(); // UID 762} 763 764SyscallReturn 765getgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 766 ThreadContext *tc) 767{ 768 return process->gid(); 769} 770 771SyscallReturn 772getegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 773 ThreadContext *tc) 774{ 775 return process->egid(); 776} 777 778 779SyscallReturn 780cloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 781 ThreadContext *tc) 782{ 783 int index = 0; 784 IntReg flags = process->getSyscallArg(tc, index); 785 IntReg newStack = process->getSyscallArg(tc, index); 786 787 DPRINTF(SyscallVerbose, "In sys_clone:\n"); 788 DPRINTF(SyscallVerbose, " Flags=%llx\n", flags); 789 DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack); 790 791 792 if (flags != 0x10f00) { 793 warn("This sys_clone implementation assumes flags " 794 "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD " 795 "(0x10f00), and may not work correctly with given flags " 796 "0x%llx\n", flags); 797 } 798 799 ThreadContext* ctc; // child thread context 800 if ( ( ctc = process->findFreeContext() ) != NULL ) { 801 DPRINTF(SyscallVerbose, " Found unallocated thread context\n"); 802 803 ctc->clearArchRegs(); 804 805 // Arch-specific cloning code 806 #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA 807 // Cloning the misc. regs for these archs is enough 808 TheISA::copyMiscRegs(tc, ctc); 809 #elif THE_ISA == SPARC_ISA 810 TheISA::copyRegs(tc, ctc); 811 812 // TODO: Explain what this code actually does :-) 813 ctc->setIntReg(NumIntArchRegs + 6, 0); 814 ctc->setIntReg(NumIntArchRegs + 4, 0); 815 ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2); 816 ctc->setIntReg(NumIntArchRegs + 5, NWindows); 817 ctc->setMiscReg(MISCREG_CWP, 0); 818 ctc->setIntReg(NumIntArchRegs + 7, 0); 819 ctc->setMiscRegNoEffect(MISCREG_TL, 0); 820 ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY); 821 822 for (int y = 8; y < 32; y++) 823 ctc->setIntReg(y, tc->readIntReg(y)); 824 #elif THE_ISA == ARM_ISA 825 TheISA::copyRegs(tc, ctc); 826 #else 827 fatal("sys_clone is not implemented for this ISA\n"); 828 #endif 829 830 // Set up stack register 831 ctc->setIntReg(TheISA::StackPointerReg, newStack); 832 833 // Set up syscall return values in parent and child 834 ctc->setIntReg(ReturnValueReg, 0); // return value, child 835 836 // Alpha needs SyscallSuccessReg=0 in child 837 #if THE_ISA == ALPHA_ISA 838 ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 839 #endif 840 841 // In SPARC/Linux, clone returns 0 on pseudo-return register if 842 // parent, non-zero if child 843 #if THE_ISA == SPARC_ISA 844 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 845 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 846 #endif 847 848 ctc->pcState(tc->nextInstAddr()); 849 850 ctc->activate(); 851 852 // Should return nonzero child TID in parent's syscall return register, 853 // but for our pthread library any non-zero value will work 854 return 1; 855 } else { 856 fatal("Called sys_clone, but no unallocated thread contexts found!\n"); 857 return 0; 858 } 859} 860 861SyscallReturn 862accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc, 863 int index) 864{ 865 string path; 866 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 867 return -EFAULT; 868 869 // Adjust path for current working directory 870 path = p->fullPath(path); 871 872 mode_t mode = p->getSyscallArg(tc, index); 873 874 int result = access(path.c_str(), mode); 875 return (result == -1) ? -errno : result; 876} 877 878SyscallReturn 879accessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc) 880{ 881 return accessFunc(desc, callnum, p, tc, 0); 882} 883 884