syscall_emul.cc revision 10500
1360SN/A/*
21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
3360SN/A * All rights reserved.
4360SN/A *
5360SN/A * Redistribution and use in source and binary forms, with or without
6360SN/A * modification, are permitted provided that the following conditions are
7360SN/A * met: redistributions of source code must retain the above copyright
8360SN/A * notice, this list of conditions and the following disclaimer;
9360SN/A * redistributions in binary form must reproduce the above copyright
10360SN/A * notice, this list of conditions and the following disclaimer in the
11360SN/A * documentation and/or other materials provided with the distribution;
12360SN/A * neither the name of the copyright holders nor the names of its
13360SN/A * contributors may be used to endorse or promote products derived from
14360SN/A * this software without specific prior written permission.
15360SN/A *
16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
30360SN/A */
31360SN/A
322093SN/A#include <fcntl.h>
33360SN/A#include <unistd.h>
34360SN/A
356712Snate@binkert.org#include <cstdio>
366712Snate@binkert.org#include <iostream>
37360SN/A#include <string>
38360SN/A
397680Sgblack@eecs.umich.edu#include "arch/utility.hh"
402474SN/A#include "base/chunk_generator.hh"
41360SN/A#include "base/trace.hh"
426658Snate@binkert.org#include "config/the_isa.hh"
438229Snate@binkert.org#include "cpu/base.hh"
442680Sktlim@umich.edu#include "cpu/thread_context.hh"
458232Snate@binkert.org#include "debug/SyscallVerbose.hh"
462474SN/A#include "mem/page_table.hh"
47360SN/A#include "sim/process.hh"
488229Snate@binkert.org#include "sim/sim_exit.hh"
498229Snate@binkert.org#include "sim/syscall_emul.hh"
506029Ssteve.reinhardt@amd.com#include "sim/system.hh"
51360SN/A
52360SN/Ausing namespace std;
532107SN/Ausing namespace TheISA;
54360SN/A
55360SN/Avoid
563114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
57360SN/A{
5810253Ssteve.reinhardt@amd.com    if (DTRACE(SyscallVerbose)) {
5910253Ssteve.reinhardt@amd.com        int index = 0;
6010257Ssteve.reinhardt@amd.com        IntReg arg[4] M5_VAR_USED;
6110253Ssteve.reinhardt@amd.com
6210253Ssteve.reinhardt@amd.com        // we can't just put the calls to getSyscallArg() in the
6310253Ssteve.reinhardt@amd.com        // DPRINTF arg list, because C++ doesn't guarantee their order
6410253Ssteve.reinhardt@amd.com        for (int i = 0; i < 4; ++i)
6510253Ssteve.reinhardt@amd.com            arg[i] = process->getSyscallArg(tc, index);
6610253Ssteve.reinhardt@amd.com
6710253Ssteve.reinhardt@amd.com        DPRINTFNR("%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
6810253Ssteve.reinhardt@amd.com                  curTick(), tc->getCpuPtr()->name(), name,
6910253Ssteve.reinhardt@amd.com                  arg[0], arg[1], arg[2], arg[3]);
7010253Ssteve.reinhardt@amd.com    }
71360SN/A
722680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
73360SN/A
7410500Ssteve.reinhardt@amd.com    if (retval.needsRetry()) {
7510500Ssteve.reinhardt@amd.com        DPRINTFS(SyscallVerbose, tc->getCpuPtr(), "syscall %s needs retry\n",
7610500Ssteve.reinhardt@amd.com                 name);
7710500Ssteve.reinhardt@amd.com    } else {
7810500Ssteve.reinhardt@amd.com        DPRINTFS(SyscallVerbose, tc->getCpuPtr(), "syscall %s returns %d\n",
7910500Ssteve.reinhardt@amd.com                 name, retval.encodedValue());
8010500Ssteve.reinhardt@amd.com    }
81360SN/A
8210500Ssteve.reinhardt@amd.com    if (!(flags & SyscallDesc::SuppressReturnValue) && !retval.needsRetry())
835958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
84360SN/A}
85360SN/A
86360SN/A
871450SN/ASyscallReturn
883114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
892680Sktlim@umich.edu                  ThreadContext *tc)
90360SN/A{
911969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
922484SN/A
932484SN/A    return 1;
94360SN/A}
95360SN/A
96360SN/A
971450SN/ASyscallReturn
983114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
992680Sktlim@umich.edu           ThreadContext *tc)
100360SN/A{
1016701Sgblack@eecs.umich.edu    int index = 0;
10210253Ssteve.reinhardt@amd.com    warn("ignoring syscall %s(%d, ...)", desc->name,
10310253Ssteve.reinhardt@amd.com         process->getSyscallArg(tc, index));
104360SN/A
1051458SN/A    return 0;
106360SN/A}
107360SN/A
108360SN/A
1091450SN/ASyscallReturn
1108149SChris.Emmons@ARM.comignoreWarnOnceFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1118149SChris.Emmons@ARM.com           ThreadContext *tc)
1128149SChris.Emmons@ARM.com{
1138149SChris.Emmons@ARM.com    int index = 0;
11410253Ssteve.reinhardt@amd.com    warn_once("ignoring syscall %s(%d, ...)", desc->name,
11510253Ssteve.reinhardt@amd.com              process->getSyscallArg(tc, index));
1168149SChris.Emmons@ARM.com
1178149SChris.Emmons@ARM.com    return 0;
1188149SChris.Emmons@ARM.com}
1198149SChris.Emmons@ARM.com
1208149SChris.Emmons@ARM.com
1218149SChris.Emmons@ARM.comSyscallReturn
1223114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1232680Sktlim@umich.edu         ThreadContext *tc)
124360SN/A{
1256029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1266029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1276701Sgblack@eecs.umich.edu        int index = 0;
1285958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1296701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1306029Ssteve.reinhardt@amd.com    } else {
1316029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1326029Ssteve.reinhardt@amd.com        tc->halt();
1332834Sksewell@umich.edu    }
134360SN/A
1351458SN/A    return 1;
136360SN/A}
137360SN/A
138360SN/A
1391450SN/ASyscallReturn
1406109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1416109Ssanchezd@stanford.edu              ThreadContext *tc)
1426109Ssanchezd@stanford.edu{
14310483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
14410483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
14510483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
14610483Swiseveri@student.ethz.ch    }
14710483Swiseveri@student.ethz.ch
14810483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
14910483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
15010483Swiseveri@student.ethz.ch        int index = 0;
15110483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
15210483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
15310483Swiseveri@student.ethz.ch    }
1546109Ssanchezd@stanford.edu
1556109Ssanchezd@stanford.edu    return 1;
1566109Ssanchezd@stanford.edu}
1576109Ssanchezd@stanford.edu
1586109Ssanchezd@stanford.edu
1596109Ssanchezd@stanford.eduSyscallReturn
1603114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
161360SN/A{
16210318Sandreas.hansson@arm.com    return (int)PageBytes;
163360SN/A}
164360SN/A
165360SN/A
1661450SN/ASyscallReturn
1675748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
168360SN/A{
169360SN/A    // change brk addr to first arg
1706701Sgblack@eecs.umich.edu    int index = 0;
1716701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1725748SSteve.Reinhardt@amd.com
1735748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1745748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1755748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1765748SSteve.Reinhardt@amd.com        return p->brk_point;
1775748SSteve.Reinhardt@amd.com
1785748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1795748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1802474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
18110318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1825748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
18310318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1846687Stjones1@inf.ed.ac.uk
1856687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1866687Stjones1@inf.ed.ac.uk            else {
1876687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1888852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1896687Stjones1@inf.ed.ac.uk
1906687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
19110318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1926687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1938852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
19410318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1956687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1966687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1976687Stjones1@inf.ed.ac.uk                {
19810318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1998852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
2006687Stjones1@inf.ed.ac.uk                }
2016687Stjones1@inf.ed.ac.uk            }
2022474SN/A        }
2031450SN/A    }
2045748SSteve.Reinhardt@amd.com
2055748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
2061458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
2071458SN/A    return p->brk_point;
208360SN/A}
209360SN/A
210360SN/A
2111450SN/ASyscallReturn
2123114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
213360SN/A{
2146701Sgblack@eecs.umich.edu    int index = 0;
2156701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
2167508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
2177508Stjones1@inf.ed.ac.uk    int status = 0;
2187508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2197508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2201970SN/A    if (status >= 0)
2211970SN/A        p->free_fd(target_fd);
2221970SN/A    return status;
223360SN/A}
224360SN/A
225360SN/A
2261450SN/ASyscallReturn
2273114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
228360SN/A{
2296701Sgblack@eecs.umich.edu    int index = 0;
2306701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2316701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2326701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2336701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
234360SN/A
235360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
236360SN/A
237360SN/A    if (bytes_read != -1)
2388706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
239360SN/A
2401458SN/A    return bytes_read;
241360SN/A}
242360SN/A
2431450SN/ASyscallReturn
2443114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
245360SN/A{
2466701Sgblack@eecs.umich.edu    int index = 0;
2476701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2486701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2496701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2506701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
251360SN/A
2528706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
253360SN/A
254360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
255360SN/A
256360SN/A    fsync(fd);
257360SN/A
2581458SN/A    return bytes_written;
259360SN/A}
260360SN/A
261360SN/A
2621450SN/ASyscallReturn
2633114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
264360SN/A{
2656701Sgblack@eecs.umich.edu    int index = 0;
2666701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2676701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2686701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
269360SN/A
270360SN/A    off_t result = lseek(fd, offs, whence);
271360SN/A
2721458SN/A    return (result == (off_t)-1) ? -errno : result;
273360SN/A}
274360SN/A
275360SN/A
2761450SN/ASyscallReturn
2774118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2784118Sgblack@eecs.umich.edu{
2796701Sgblack@eecs.umich.edu    int index = 0;
2806701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2816701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2826701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2836701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2846701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2854118Sgblack@eecs.umich.edu
2864118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2874118Sgblack@eecs.umich.edu
2884118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2894118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2904118Sgblack@eecs.umich.edu
2914118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2924118Sgblack@eecs.umich.edu        //The seek failed.
2934118Sgblack@eecs.umich.edu        return -errno;
2944118Sgblack@eecs.umich.edu    } else {
2956111Ssteve.reinhardt@amd.com        // The seek succeeded.
2966111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2976111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2986111Ssteve.reinhardt@amd.com        // target platform
2994118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
3004118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
3018706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
3024118Sgblack@eecs.umich.edu        return 0;
3034118Sgblack@eecs.umich.edu    }
3044118Sgblack@eecs.umich.edu
3054118Sgblack@eecs.umich.edu
3064118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
3074118Sgblack@eecs.umich.edu}
3084118Sgblack@eecs.umich.edu
3094118Sgblack@eecs.umich.edu
3104118Sgblack@eecs.umich.eduSyscallReturn
3113114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
312360SN/A{
313360SN/A    // given that we don't really implement mmap, munmap is really easy
3141458SN/A    return 0;
315360SN/A}
316360SN/A
317360SN/A
318360SN/Aconst char *hostname = "m5.eecs.umich.edu";
319360SN/A
3201450SN/ASyscallReturn
3213114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
322360SN/A{
3236701Sgblack@eecs.umich.edu    int index = 0;
3246701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3256701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3266701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
327360SN/A
328360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
329360SN/A
3308706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
331360SN/A
3321458SN/A    return 0;
333360SN/A}
334360SN/A
3351450SN/ASyscallReturn
3365513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3375513SMichael.Adler@intel.com{
3385513SMichael.Adler@intel.com    int result = 0;
3396731Svince@csl.cornell.edu    int index = 0;
3406701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3416701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3426701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3435513SMichael.Adler@intel.com
3445513SMichael.Adler@intel.com    // Is current working directory defined?
3455513SMichael.Adler@intel.com    string cwd = p->getcwd();
3465513SMichael.Adler@intel.com    if (!cwd.empty()) {
3475513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3485513SMichael.Adler@intel.com            // Buffer too small
3495513SMichael.Adler@intel.com            return -ERANGE;
3505513SMichael.Adler@intel.com        }
3515513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3525513SMichael.Adler@intel.com        result = cwd.length();
3535513SMichael.Adler@intel.com    }
3545513SMichael.Adler@intel.com    else {
3555513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3565513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3575513SMichael.Adler@intel.com        }
3585513SMichael.Adler@intel.com        else {
3595513SMichael.Adler@intel.com            result = -1;
3605513SMichael.Adler@intel.com        }
3615513SMichael.Adler@intel.com    }
3625513SMichael.Adler@intel.com
3638706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3645513SMichael.Adler@intel.com
3655513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3665513SMichael.Adler@intel.com}
3675513SMichael.Adler@intel.com
36810203SAli.Saidi@ARM.com/// Target open() handler.
36910203SAli.Saidi@ARM.comSyscallReturn
37010203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
37110203SAli.Saidi@ARM.com         ThreadContext *tc)
37210203SAli.Saidi@ARM.com{
37310203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
37410203SAli.Saidi@ARM.com}
3755513SMichael.Adler@intel.com
3765513SMichael.Adler@intel.comSyscallReturn
37710203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
37810203SAli.Saidi@ARM.com        int index)
3795513SMichael.Adler@intel.com{
3805513SMichael.Adler@intel.com    string path;
3815513SMichael.Adler@intel.com
3828852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
38310223Ssteve.reinhardt@amd.com        return -EFAULT;
3845513SMichael.Adler@intel.com
3855513SMichael.Adler@intel.com    // Adjust path for current working directory
3865513SMichael.Adler@intel.com    path = p->fullPath(path);
3875513SMichael.Adler@intel.com
3886701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3896701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3906701Sgblack@eecs.umich.edu
3916701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3925513SMichael.Adler@intel.com
3935513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3945513SMichael.Adler@intel.com
3958706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3965513SMichael.Adler@intel.com
3975513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3985513SMichael.Adler@intel.com}
3995513SMichael.Adler@intel.com
4005513SMichael.Adler@intel.comSyscallReturn
4013114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
402511SN/A{
4031706SN/A    string path;
404360SN/A
4056701Sgblack@eecs.umich.edu    int index = 0;
4068852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
40710223Ssteve.reinhardt@amd.com        return -EFAULT;
408511SN/A
4093669Sbinkertn@umich.edu    // Adjust path for current working directory
4103669Sbinkertn@umich.edu    path = p->fullPath(path);
4113669Sbinkertn@umich.edu
412511SN/A    int result = unlink(path.c_str());
4131458SN/A    return (result == -1) ? -errno : result;
414511SN/A}
415511SN/A
4165513SMichael.Adler@intel.com
4175513SMichael.Adler@intel.comSyscallReturn
4185513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4195513SMichael.Adler@intel.com{
4205513SMichael.Adler@intel.com    string path;
4215513SMichael.Adler@intel.com
4226701Sgblack@eecs.umich.edu    int index = 0;
4238852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
42410223Ssteve.reinhardt@amd.com        return -EFAULT;
4255513SMichael.Adler@intel.com
4265513SMichael.Adler@intel.com    // Adjust path for current working directory
4275513SMichael.Adler@intel.com    path = p->fullPath(path);
4285513SMichael.Adler@intel.com
4296701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4305513SMichael.Adler@intel.com
4315513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4325513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4335513SMichael.Adler@intel.com}
4345513SMichael.Adler@intel.com
4351450SN/ASyscallReturn
4363114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
437511SN/A{
4381706SN/A    string old_name;
439511SN/A
4406701Sgblack@eecs.umich.edu    int index = 0;
4418852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4421458SN/A        return -EFAULT;
443511SN/A
4441706SN/A    string new_name;
445511SN/A
4468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4471458SN/A        return -EFAULT;
448511SN/A
4493669Sbinkertn@umich.edu    // Adjust path for current working directory
4503669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4513669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4523669Sbinkertn@umich.edu
4531706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4541458SN/A    return (result == -1) ? -errno : result;
455511SN/A}
456511SN/A
4571706SN/ASyscallReturn
4583114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4591706SN/A{
4601706SN/A    string path;
4611706SN/A
4626701Sgblack@eecs.umich.edu    int index = 0;
4638852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4641706SN/A        return -EFAULT;
4651706SN/A
4666701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4671706SN/A
4683669Sbinkertn@umich.edu    // Adjust path for current working directory
4693669Sbinkertn@umich.edu    path = p->fullPath(path);
4703669Sbinkertn@umich.edu
4711706SN/A    int result = truncate(path.c_str(), length);
4721706SN/A    return (result == -1) ? -errno : result;
4731706SN/A}
4741706SN/A
4751706SN/ASyscallReturn
4766111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4776111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4781706SN/A{
4796701Sgblack@eecs.umich.edu    int index = 0;
4806701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4811706SN/A
4821706SN/A    if (fd < 0)
4831706SN/A        return -EBADF;
4841706SN/A
4856701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4861706SN/A
4871706SN/A    int result = ftruncate(fd, length);
4881706SN/A    return (result == -1) ? -errno : result;
4891706SN/A}
4901999SN/A
4911999SN/ASyscallReturn
4926703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4936703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4946703Svince@csl.cornell.edu{
4956703Svince@csl.cornell.edu    int index = 0;
4966703Svince@csl.cornell.edu    string path;
4976703Svince@csl.cornell.edu
4988852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
4996703Svince@csl.cornell.edu       return -EFAULT;
5006703Svince@csl.cornell.edu
5016744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5026703Svince@csl.cornell.edu
5036703Svince@csl.cornell.edu    // Adjust path for current working directory
5046703Svince@csl.cornell.edu    path = process->fullPath(path);
5056703Svince@csl.cornell.edu
5066744SAli.Saidi@arm.com#if NO_STAT64
5076744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5086744SAli.Saidi@arm.com#else
5096703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5106744SAli.Saidi@arm.com#endif
5116703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5126703Svince@csl.cornell.edu}
5136703Svince@csl.cornell.edu
5146703Svince@csl.cornell.eduSyscallReturn
5156685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5166685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5176685Stjones1@inf.ed.ac.uk{
5186701Sgblack@eecs.umich.edu    int index = 0;
5196701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5206685Stjones1@inf.ed.ac.uk
5216685Stjones1@inf.ed.ac.uk    if (fd < 0)
5226685Stjones1@inf.ed.ac.uk        return -EBADF;
5236685Stjones1@inf.ed.ac.uk
5246744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5256685Stjones1@inf.ed.ac.uk
5266744SAli.Saidi@arm.com#if NO_STAT64
5276744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5286744SAli.Saidi@arm.com#else
5296685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5306744SAli.Saidi@arm.com#endif
5316685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5326685Stjones1@inf.ed.ac.uk}
5336685Stjones1@inf.ed.ac.uk
5346685Stjones1@inf.ed.ac.ukSyscallReturn
5355513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5365513SMichael.Adler@intel.com{
5375513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5385513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5395513SMichael.Adler@intel.com    // changing anything.
5405513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5415513SMichael.Adler@intel.com    umask(oldMask);
5425521Snate@binkert.org    return (int)oldMask;
5435513SMichael.Adler@intel.com}
5445513SMichael.Adler@intel.com
5455513SMichael.Adler@intel.comSyscallReturn
5463114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5471999SN/A{
5481999SN/A    string path;
5491999SN/A
5506701Sgblack@eecs.umich.edu    int index = 0;
5518852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5521999SN/A        return -EFAULT;
5531999SN/A
5541999SN/A    /* XXX endianess */
5556701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5561999SN/A    uid_t hostOwner = owner;
5576701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5581999SN/A    gid_t hostGroup = group;
5591999SN/A
5603669Sbinkertn@umich.edu    // Adjust path for current working directory
5613669Sbinkertn@umich.edu    path = p->fullPath(path);
5623669Sbinkertn@umich.edu
5631999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5641999SN/A    return (result == -1) ? -errno : result;
5651999SN/A}
5661999SN/A
5671999SN/ASyscallReturn
5683114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5691999SN/A{
5706701Sgblack@eecs.umich.edu    int index = 0;
5716701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5721999SN/A
5731999SN/A    if (fd < 0)
5741999SN/A        return -EBADF;
5751999SN/A
5761999SN/A    /* XXX endianess */
5776701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5781999SN/A    uid_t hostOwner = owner;
5796701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5801999SN/A    gid_t hostGroup = group;
5811999SN/A
5821999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5831999SN/A    return (result == -1) ? -errno : result;
5841999SN/A}
5852093SN/A
5862093SN/A
5872093SN/ASyscallReturn
5883114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5893079Sstever@eecs.umich.edu{
5906701Sgblack@eecs.umich.edu    int index = 0;
5916701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5923079Sstever@eecs.umich.edu    if (fd < 0)
5933079Sstever@eecs.umich.edu        return -EBADF;
5943079Sstever@eecs.umich.edu
5956701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5965282Srstrong@cs.ucsd.edu
5973079Sstever@eecs.umich.edu    int result = dup(fd);
5986111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5996111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
6003079Sstever@eecs.umich.edu}
6013079Sstever@eecs.umich.edu
6023079Sstever@eecs.umich.edu
6033079Sstever@eecs.umich.eduSyscallReturn
6043114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6052680Sktlim@umich.edu          ThreadContext *tc)
6062093SN/A{
6076701Sgblack@eecs.umich.edu    int index = 0;
6086701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6092093SN/A
6102093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
6112093SN/A        return -EBADF;
6122093SN/A
6136701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6142093SN/A    switch (cmd) {
6152093SN/A      case 0: // F_DUPFD
6162093SN/A        // if we really wanted to support this, we'd need to do it
6172093SN/A        // in the target fd space.
6182093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
6192093SN/A        return -EMFILE;
6202093SN/A
6212093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6222093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6232093SN/A        return 0;
6242093SN/A
6252093SN/A      case 3: // F_GETFL (get file flags)
6262093SN/A      case 4: // F_SETFL (set file flags)
6272093SN/A        // not sure if this is totally valid, but we'll pass it through
6282093SN/A        // to the underlying OS
6292093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6302093SN/A        return fcntl(process->sim_fd(fd), cmd);
6312093SN/A        // return 0;
6322093SN/A
6332093SN/A      case 7: // F_GETLK  (get lock)
6342093SN/A      case 8: // F_SETLK  (set lock)
6352093SN/A      case 9: // F_SETLKW (set lock and wait)
6362093SN/A        // don't mess with file locking... just act like it's OK
6372093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6382093SN/A        return 0;
6392093SN/A
6402093SN/A      default:
6412093SN/A        warn("Unknown fcntl command %d\n", cmd);
6422093SN/A        return 0;
6432093SN/A    }
6442093SN/A}
6452093SN/A
6462238SN/ASyscallReturn
6473114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6482687Sksewell@umich.edu            ThreadContext *tc)
6492687Sksewell@umich.edu{
6506701Sgblack@eecs.umich.edu    int index = 0;
6516701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6522687Sksewell@umich.edu
6532687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6542687Sksewell@umich.edu        return -EBADF;
6552687Sksewell@umich.edu
6566701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6572687Sksewell@umich.edu    switch (cmd) {
6582687Sksewell@umich.edu      case 33: //F_GETLK64
6592687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6602687Sksewell@umich.edu        return -EMFILE;
6612687Sksewell@umich.edu
6622687Sksewell@umich.edu      case 34: // F_SETLK64
6632687Sksewell@umich.edu      case 35: // F_SETLKW64
6642687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6652687Sksewell@umich.edu        return -EMFILE;
6662687Sksewell@umich.edu
6672687Sksewell@umich.edu      default:
6682687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6692687Sksewell@umich.edu        // to the underlying OS
6702687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6712687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6722687Sksewell@umich.edu        // return 0;
6732687Sksewell@umich.edu    }
6742687Sksewell@umich.edu}
6752687Sksewell@umich.edu
6762687Sksewell@umich.eduSyscallReturn
6773114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6782680Sktlim@umich.edu         ThreadContext *tc)
6792238SN/A{
6802238SN/A    int fds[2], sim_fds[2];
6812238SN/A    int pipe_retval = pipe(fds);
6822093SN/A
6832238SN/A    if (pipe_retval < 0) {
6842238SN/A        // error
6852238SN/A        return pipe_retval;
6862238SN/A    }
6872238SN/A
6885282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6895282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6902238SN/A
6915282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6922238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6932238SN/A    // the return value of the function, and fd[1] is returned in r20.
6942680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6952238SN/A    return sim_fds[0];
6962238SN/A}
6972238SN/A
6982238SN/A
6992238SN/ASyscallReturn
7003114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7012680Sktlim@umich.edu           ThreadContext *tc)
7022238SN/A{
7032238SN/A    // Make up a PID.  There's no interprocess communication in
7042238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7052238SN/A    // not getting a unique value.
7062238SN/A
7073114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7083114Sgblack@eecs.umich.edu    return process->pid();
7092238SN/A}
7102238SN/A
7112238SN/A
7122238SN/ASyscallReturn
7133114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7142680Sktlim@umich.edu           ThreadContext *tc)
7152238SN/A{
7162238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7172238SN/A    // simulation to be deterministic.
7182238SN/A
7192238SN/A    // EUID goes in r20.
7203114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7215543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7222238SN/A}
7232238SN/A
7242238SN/A
7252238SN/ASyscallReturn
7263114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7272680Sktlim@umich.edu           ThreadContext *tc)
7282238SN/A{
7292238SN/A    // Get current group ID.  EGID goes in r20.
7303114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7313114Sgblack@eecs.umich.edu    return process->gid();
7322238SN/A}
7332238SN/A
7342238SN/A
7352238SN/ASyscallReturn
7363114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7372680Sktlim@umich.edu           ThreadContext *tc)
7382238SN/A{
7392238SN/A    // can't fathom why a benchmark would call this.
7406701Sgblack@eecs.umich.edu    int index = 0;
7416701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7422238SN/A    return 0;
7432238SN/A}
7442238SN/A
7452238SN/ASyscallReturn
7463114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7472680Sktlim@umich.edu           ThreadContext *tc)
7482238SN/A{
7492238SN/A    // Make up a PID.  There's no interprocess communication in
7502238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7512238SN/A    // not getting a unique value.
7522238SN/A
7533114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7543114Sgblack@eecs.umich.edu    return process->pid();
7552238SN/A}
7562238SN/A
7572238SN/ASyscallReturn
7583114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7592680Sktlim@umich.edu           ThreadContext *tc)
7602238SN/A{
7613114Sgblack@eecs.umich.edu    return process->ppid();
7622238SN/A}
7632238SN/A
7642238SN/ASyscallReturn
7653114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7662680Sktlim@umich.edu           ThreadContext *tc)
7672238SN/A{
7685543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7692238SN/A}
7702238SN/A
7712238SN/ASyscallReturn
7723114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7732680Sktlim@umich.edu           ThreadContext *tc)
7742238SN/A{
7755543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7762238SN/A}
7772238SN/A
7782238SN/ASyscallReturn
7793114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7802680Sktlim@umich.edu           ThreadContext *tc)
7812238SN/A{
7823114Sgblack@eecs.umich.edu    return process->gid();
7832238SN/A}
7842238SN/A
7852238SN/ASyscallReturn
7863114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7872680Sktlim@umich.edu           ThreadContext *tc)
7882238SN/A{
7893114Sgblack@eecs.umich.edu    return process->egid();
7902238SN/A}
7912238SN/A
7922238SN/A
7936109Ssanchezd@stanford.eduSyscallReturn
7946109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7956109Ssanchezd@stanford.edu           ThreadContext *tc)
7966109Ssanchezd@stanford.edu{
7976701Sgblack@eecs.umich.edu    int index = 0;
7986701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7996701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8006701Sgblack@eecs.umich.edu
8016109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8026701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8036701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8046109Ssanchezd@stanford.edu
8056109Ssanchezd@stanford.edu
8066701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8076111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8086111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8096111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8106701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8116109Ssanchezd@stanford.edu    }
8126109Ssanchezd@stanford.edu
8136111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8146109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8156109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8166109Ssanchezd@stanford.edu
8176109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8186109Ssanchezd@stanford.edu
8196111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8206109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8216111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8226109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8236109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8246109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8256109Ssanchezd@stanford.edu
8266111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8276109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8286109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8296109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8306109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8316337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8326109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8336109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8349375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8356109Ssanchezd@stanford.edu
8366109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8376109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8388149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8398149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8406109Ssanchezd@stanford.edu        #else
8416109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8426109Ssanchezd@stanford.edu        #endif
8436109Ssanchezd@stanford.edu
8446111Ssteve.reinhardt@amd.com        // Set up stack register
8456701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8466109Ssanchezd@stanford.edu
8476111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8486111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8496109Ssanchezd@stanford.edu
8506111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8516109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8526110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8536109Ssanchezd@stanford.edu        #endif
8546109Ssanchezd@stanford.edu
8556111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8566111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8576109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8586109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8596109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8606109Ssanchezd@stanford.edu        #endif
8616109Ssanchezd@stanford.edu
8627720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8636109Ssanchezd@stanford.edu
8646109Ssanchezd@stanford.edu        ctc->activate();
8656109Ssanchezd@stanford.edu
8666109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8676109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8686109Ssanchezd@stanford.edu        return 1;
8696109Ssanchezd@stanford.edu    } else {
8706109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8716109Ssanchezd@stanford.edu        return 0;
8726109Ssanchezd@stanford.edu    }
8736109Ssanchezd@stanford.edu}
8746109Ssanchezd@stanford.edu
8759455Smitch.hayenga+gem5@gmail.comSyscallReturn
87610203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
87710203SAli.Saidi@ARM.com        int index)
8789455Smitch.hayenga+gem5@gmail.com{
8799455Smitch.hayenga+gem5@gmail.com    string path;
8809455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
88110223Ssteve.reinhardt@amd.com        return -EFAULT;
8829455Smitch.hayenga+gem5@gmail.com
8839455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
8849455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
8859455Smitch.hayenga+gem5@gmail.com
8869455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
8879455Smitch.hayenga+gem5@gmail.com
8889455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
8899455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
8909455Smitch.hayenga+gem5@gmail.com}
89110203SAli.Saidi@ARM.com
89210203SAli.Saidi@ARM.comSyscallReturn
89310203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
89410203SAli.Saidi@ARM.com{
89510203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
89610203SAli.Saidi@ARM.com}
89710203SAli.Saidi@ARM.com
898