syscall_emul.cc revision 10318
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
742495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
7510223Ssteve.reinhardt@amd.com             curTick(), tc->getCpuPtr()->name(), name, retval.encodedValue());
76360SN/A
771450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
785958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
79360SN/A}
80360SN/A
81360SN/A
821450SN/ASyscallReturn
833114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
842680Sktlim@umich.edu                  ThreadContext *tc)
85360SN/A{
861969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
872484SN/A
882484SN/A    return 1;
89360SN/A}
90360SN/A
91360SN/A
921450SN/ASyscallReturn
933114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
942680Sktlim@umich.edu           ThreadContext *tc)
95360SN/A{
966701Sgblack@eecs.umich.edu    int index = 0;
9710253Ssteve.reinhardt@amd.com    warn("ignoring syscall %s(%d, ...)", desc->name,
9810253Ssteve.reinhardt@amd.com         process->getSyscallArg(tc, index));
99360SN/A
1001458SN/A    return 0;
101360SN/A}
102360SN/A
103360SN/A
1041450SN/ASyscallReturn
1058149SChris.Emmons@ARM.comignoreWarnOnceFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1068149SChris.Emmons@ARM.com           ThreadContext *tc)
1078149SChris.Emmons@ARM.com{
1088149SChris.Emmons@ARM.com    int index = 0;
10910253Ssteve.reinhardt@amd.com    warn_once("ignoring syscall %s(%d, ...)", desc->name,
11010253Ssteve.reinhardt@amd.com              process->getSyscallArg(tc, index));
1118149SChris.Emmons@ARM.com
1128149SChris.Emmons@ARM.com    return 0;
1138149SChris.Emmons@ARM.com}
1148149SChris.Emmons@ARM.com
1158149SChris.Emmons@ARM.com
1168149SChris.Emmons@ARM.comSyscallReturn
1173114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1182680Sktlim@umich.edu         ThreadContext *tc)
119360SN/A{
1206029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1216029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1226701Sgblack@eecs.umich.edu        int index = 0;
1235958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1246701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1256029Ssteve.reinhardt@amd.com    } else {
1266029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1276029Ssteve.reinhardt@amd.com        tc->halt();
1282834Sksewell@umich.edu    }
129360SN/A
1301458SN/A    return 1;
131360SN/A}
132360SN/A
133360SN/A
1341450SN/ASyscallReturn
1356109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1366109Ssanchezd@stanford.edu              ThreadContext *tc)
1376109Ssanchezd@stanford.edu{
1386109Ssanchezd@stanford.edu    // really should just halt all thread contexts belonging to this
1396109Ssanchezd@stanford.edu    // process in case there's another process running...
1406701Sgblack@eecs.umich.edu    int index = 0;
1416109Ssanchezd@stanford.edu    exitSimLoop("target called exit()",
1426701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index) & 0xff);
1436109Ssanchezd@stanford.edu
1446109Ssanchezd@stanford.edu    return 1;
1456109Ssanchezd@stanford.edu}
1466109Ssanchezd@stanford.edu
1476109Ssanchezd@stanford.edu
1486109Ssanchezd@stanford.eduSyscallReturn
1493114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
150360SN/A{
15110318Sandreas.hansson@arm.com    return (int)PageBytes;
152360SN/A}
153360SN/A
154360SN/A
1551450SN/ASyscallReturn
1565748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
157360SN/A{
158360SN/A    // change brk addr to first arg
1596701Sgblack@eecs.umich.edu    int index = 0;
1606701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1615748SSteve.Reinhardt@amd.com
1625748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1635748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1645748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1655748SSteve.Reinhardt@amd.com        return p->brk_point;
1665748SSteve.Reinhardt@amd.com
1675748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1685748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1692474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
17010318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1715748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
17210318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1736687Stjones1@inf.ed.ac.uk
1746687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1756687Stjones1@inf.ed.ac.uk            else {
1766687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1778852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1786687Stjones1@inf.ed.ac.uk
1796687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
18010318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1816687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1828852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
18310318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1846687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1856687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1866687Stjones1@inf.ed.ac.uk                {
18710318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1888852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1896687Stjones1@inf.ed.ac.uk                }
1906687Stjones1@inf.ed.ac.uk            }
1912474SN/A        }
1921450SN/A    }
1935748SSteve.Reinhardt@amd.com
1945748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
1951458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1961458SN/A    return p->brk_point;
197360SN/A}
198360SN/A
199360SN/A
2001450SN/ASyscallReturn
2013114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
202360SN/A{
2036701Sgblack@eecs.umich.edu    int index = 0;
2046701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
2057508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
2067508Stjones1@inf.ed.ac.uk    int status = 0;
2077508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2087508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2091970SN/A    if (status >= 0)
2101970SN/A        p->free_fd(target_fd);
2111970SN/A    return status;
212360SN/A}
213360SN/A
214360SN/A
2151450SN/ASyscallReturn
2163114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
217360SN/A{
2186701Sgblack@eecs.umich.edu    int index = 0;
2196701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2206701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2216701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2226701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
223360SN/A
224360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
225360SN/A
226360SN/A    if (bytes_read != -1)
2278706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
228360SN/A
2291458SN/A    return bytes_read;
230360SN/A}
231360SN/A
2321450SN/ASyscallReturn
2333114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
234360SN/A{
2356701Sgblack@eecs.umich.edu    int index = 0;
2366701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2376701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2386701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2396701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
240360SN/A
2418706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
242360SN/A
243360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
244360SN/A
245360SN/A    fsync(fd);
246360SN/A
2471458SN/A    return bytes_written;
248360SN/A}
249360SN/A
250360SN/A
2511450SN/ASyscallReturn
2523114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
253360SN/A{
2546701Sgblack@eecs.umich.edu    int index = 0;
2556701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2566701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2576701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
258360SN/A
259360SN/A    off_t result = lseek(fd, offs, whence);
260360SN/A
2611458SN/A    return (result == (off_t)-1) ? -errno : result;
262360SN/A}
263360SN/A
264360SN/A
2651450SN/ASyscallReturn
2664118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2674118Sgblack@eecs.umich.edu{
2686701Sgblack@eecs.umich.edu    int index = 0;
2696701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2706701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2716701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2726701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2736701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2744118Sgblack@eecs.umich.edu
2754118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2764118Sgblack@eecs.umich.edu
2774118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2784118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2794118Sgblack@eecs.umich.edu
2804118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2814118Sgblack@eecs.umich.edu        //The seek failed.
2824118Sgblack@eecs.umich.edu        return -errno;
2834118Sgblack@eecs.umich.edu    } else {
2846111Ssteve.reinhardt@amd.com        // The seek succeeded.
2856111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2866111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2876111Ssteve.reinhardt@amd.com        // target platform
2884118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2894118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2908706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
2914118Sgblack@eecs.umich.edu        return 0;
2924118Sgblack@eecs.umich.edu    }
2934118Sgblack@eecs.umich.edu
2944118Sgblack@eecs.umich.edu
2954118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2964118Sgblack@eecs.umich.edu}
2974118Sgblack@eecs.umich.edu
2984118Sgblack@eecs.umich.edu
2994118Sgblack@eecs.umich.eduSyscallReturn
3003114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
301360SN/A{
302360SN/A    // given that we don't really implement mmap, munmap is really easy
3031458SN/A    return 0;
304360SN/A}
305360SN/A
306360SN/A
307360SN/Aconst char *hostname = "m5.eecs.umich.edu";
308360SN/A
3091450SN/ASyscallReturn
3103114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
311360SN/A{
3126701Sgblack@eecs.umich.edu    int index = 0;
3136701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3146701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3156701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
316360SN/A
317360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
318360SN/A
3198706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
320360SN/A
3211458SN/A    return 0;
322360SN/A}
323360SN/A
3241450SN/ASyscallReturn
3255513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3265513SMichael.Adler@intel.com{
3275513SMichael.Adler@intel.com    int result = 0;
3286731Svince@csl.cornell.edu    int index = 0;
3296701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3306701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3316701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3325513SMichael.Adler@intel.com
3335513SMichael.Adler@intel.com    // Is current working directory defined?
3345513SMichael.Adler@intel.com    string cwd = p->getcwd();
3355513SMichael.Adler@intel.com    if (!cwd.empty()) {
3365513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3375513SMichael.Adler@intel.com            // Buffer too small
3385513SMichael.Adler@intel.com            return -ERANGE;
3395513SMichael.Adler@intel.com        }
3405513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3415513SMichael.Adler@intel.com        result = cwd.length();
3425513SMichael.Adler@intel.com    }
3435513SMichael.Adler@intel.com    else {
3445513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3455513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3465513SMichael.Adler@intel.com        }
3475513SMichael.Adler@intel.com        else {
3485513SMichael.Adler@intel.com            result = -1;
3495513SMichael.Adler@intel.com        }
3505513SMichael.Adler@intel.com    }
3515513SMichael.Adler@intel.com
3528706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3535513SMichael.Adler@intel.com
3545513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3555513SMichael.Adler@intel.com}
3565513SMichael.Adler@intel.com
35710203SAli.Saidi@ARM.com/// Target open() handler.
35810203SAli.Saidi@ARM.comSyscallReturn
35910203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
36010203SAli.Saidi@ARM.com         ThreadContext *tc)
36110203SAli.Saidi@ARM.com{
36210203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
36310203SAli.Saidi@ARM.com}
3645513SMichael.Adler@intel.com
3655513SMichael.Adler@intel.comSyscallReturn
36610203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
36710203SAli.Saidi@ARM.com        int index)
3685513SMichael.Adler@intel.com{
3695513SMichael.Adler@intel.com    string path;
3705513SMichael.Adler@intel.com
3718852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
37210223Ssteve.reinhardt@amd.com        return -EFAULT;
3735513SMichael.Adler@intel.com
3745513SMichael.Adler@intel.com    // Adjust path for current working directory
3755513SMichael.Adler@intel.com    path = p->fullPath(path);
3765513SMichael.Adler@intel.com
3776701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3786701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3796701Sgblack@eecs.umich.edu
3806701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3815513SMichael.Adler@intel.com
3825513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3835513SMichael.Adler@intel.com
3848706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3855513SMichael.Adler@intel.com
3865513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3875513SMichael.Adler@intel.com}
3885513SMichael.Adler@intel.com
3895513SMichael.Adler@intel.comSyscallReturn
3903114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
391511SN/A{
3921706SN/A    string path;
393360SN/A
3946701Sgblack@eecs.umich.edu    int index = 0;
3958852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
39610223Ssteve.reinhardt@amd.com        return -EFAULT;
397511SN/A
3983669Sbinkertn@umich.edu    // Adjust path for current working directory
3993669Sbinkertn@umich.edu    path = p->fullPath(path);
4003669Sbinkertn@umich.edu
401511SN/A    int result = unlink(path.c_str());
4021458SN/A    return (result == -1) ? -errno : result;
403511SN/A}
404511SN/A
4055513SMichael.Adler@intel.com
4065513SMichael.Adler@intel.comSyscallReturn
4075513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4085513SMichael.Adler@intel.com{
4095513SMichael.Adler@intel.com    string path;
4105513SMichael.Adler@intel.com
4116701Sgblack@eecs.umich.edu    int index = 0;
4128852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
41310223Ssteve.reinhardt@amd.com        return -EFAULT;
4145513SMichael.Adler@intel.com
4155513SMichael.Adler@intel.com    // Adjust path for current working directory
4165513SMichael.Adler@intel.com    path = p->fullPath(path);
4175513SMichael.Adler@intel.com
4186701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4195513SMichael.Adler@intel.com
4205513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4215513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4225513SMichael.Adler@intel.com}
4235513SMichael.Adler@intel.com
4241450SN/ASyscallReturn
4253114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
426511SN/A{
4271706SN/A    string old_name;
428511SN/A
4296701Sgblack@eecs.umich.edu    int index = 0;
4308852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4311458SN/A        return -EFAULT;
432511SN/A
4331706SN/A    string new_name;
434511SN/A
4358852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4361458SN/A        return -EFAULT;
437511SN/A
4383669Sbinkertn@umich.edu    // Adjust path for current working directory
4393669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4403669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4413669Sbinkertn@umich.edu
4421706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4431458SN/A    return (result == -1) ? -errno : result;
444511SN/A}
445511SN/A
4461706SN/ASyscallReturn
4473114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4481706SN/A{
4491706SN/A    string path;
4501706SN/A
4516701Sgblack@eecs.umich.edu    int index = 0;
4528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4531706SN/A        return -EFAULT;
4541706SN/A
4556701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4561706SN/A
4573669Sbinkertn@umich.edu    // Adjust path for current working directory
4583669Sbinkertn@umich.edu    path = p->fullPath(path);
4593669Sbinkertn@umich.edu
4601706SN/A    int result = truncate(path.c_str(), length);
4611706SN/A    return (result == -1) ? -errno : result;
4621706SN/A}
4631706SN/A
4641706SN/ASyscallReturn
4656111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4666111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4671706SN/A{
4686701Sgblack@eecs.umich.edu    int index = 0;
4696701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4701706SN/A
4711706SN/A    if (fd < 0)
4721706SN/A        return -EBADF;
4731706SN/A
4746701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4751706SN/A
4761706SN/A    int result = ftruncate(fd, length);
4771706SN/A    return (result == -1) ? -errno : result;
4781706SN/A}
4791999SN/A
4801999SN/ASyscallReturn
4816703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4826703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4836703Svince@csl.cornell.edu{
4846703Svince@csl.cornell.edu    int index = 0;
4856703Svince@csl.cornell.edu    string path;
4866703Svince@csl.cornell.edu
4878852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
4886703Svince@csl.cornell.edu       return -EFAULT;
4896703Svince@csl.cornell.edu
4906744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
4916703Svince@csl.cornell.edu
4926703Svince@csl.cornell.edu    // Adjust path for current working directory
4936703Svince@csl.cornell.edu    path = process->fullPath(path);
4946703Svince@csl.cornell.edu
4956744SAli.Saidi@arm.com#if NO_STAT64
4966744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
4976744SAli.Saidi@arm.com#else
4986703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
4996744SAli.Saidi@arm.com#endif
5006703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5016703Svince@csl.cornell.edu}
5026703Svince@csl.cornell.edu
5036703Svince@csl.cornell.eduSyscallReturn
5046685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5056685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5066685Stjones1@inf.ed.ac.uk{
5076701Sgblack@eecs.umich.edu    int index = 0;
5086701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5096685Stjones1@inf.ed.ac.uk
5106685Stjones1@inf.ed.ac.uk    if (fd < 0)
5116685Stjones1@inf.ed.ac.uk        return -EBADF;
5126685Stjones1@inf.ed.ac.uk
5136744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5146685Stjones1@inf.ed.ac.uk
5156744SAli.Saidi@arm.com#if NO_STAT64
5166744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5176744SAli.Saidi@arm.com#else
5186685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5196744SAli.Saidi@arm.com#endif
5206685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5216685Stjones1@inf.ed.ac.uk}
5226685Stjones1@inf.ed.ac.uk
5236685Stjones1@inf.ed.ac.ukSyscallReturn
5245513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5255513SMichael.Adler@intel.com{
5265513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5275513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5285513SMichael.Adler@intel.com    // changing anything.
5295513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5305513SMichael.Adler@intel.com    umask(oldMask);
5315521Snate@binkert.org    return (int)oldMask;
5325513SMichael.Adler@intel.com}
5335513SMichael.Adler@intel.com
5345513SMichael.Adler@intel.comSyscallReturn
5353114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5361999SN/A{
5371999SN/A    string path;
5381999SN/A
5396701Sgblack@eecs.umich.edu    int index = 0;
5408852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5411999SN/A        return -EFAULT;
5421999SN/A
5431999SN/A    /* XXX endianess */
5446701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5451999SN/A    uid_t hostOwner = owner;
5466701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5471999SN/A    gid_t hostGroup = group;
5481999SN/A
5493669Sbinkertn@umich.edu    // Adjust path for current working directory
5503669Sbinkertn@umich.edu    path = p->fullPath(path);
5513669Sbinkertn@umich.edu
5521999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5531999SN/A    return (result == -1) ? -errno : result;
5541999SN/A}
5551999SN/A
5561999SN/ASyscallReturn
5573114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5581999SN/A{
5596701Sgblack@eecs.umich.edu    int index = 0;
5606701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5611999SN/A
5621999SN/A    if (fd < 0)
5631999SN/A        return -EBADF;
5641999SN/A
5651999SN/A    /* XXX endianess */
5666701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5671999SN/A    uid_t hostOwner = owner;
5686701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5691999SN/A    gid_t hostGroup = group;
5701999SN/A
5711999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5721999SN/A    return (result == -1) ? -errno : result;
5731999SN/A}
5742093SN/A
5752093SN/A
5762093SN/ASyscallReturn
5773114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5783079Sstever@eecs.umich.edu{
5796701Sgblack@eecs.umich.edu    int index = 0;
5806701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5813079Sstever@eecs.umich.edu    if (fd < 0)
5823079Sstever@eecs.umich.edu        return -EBADF;
5833079Sstever@eecs.umich.edu
5846701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5855282Srstrong@cs.ucsd.edu
5863079Sstever@eecs.umich.edu    int result = dup(fd);
5876111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5886111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
5893079Sstever@eecs.umich.edu}
5903079Sstever@eecs.umich.edu
5913079Sstever@eecs.umich.edu
5923079Sstever@eecs.umich.eduSyscallReturn
5933114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
5942680Sktlim@umich.edu          ThreadContext *tc)
5952093SN/A{
5966701Sgblack@eecs.umich.edu    int index = 0;
5976701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5982093SN/A
5992093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
6002093SN/A        return -EBADF;
6012093SN/A
6026701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6032093SN/A    switch (cmd) {
6042093SN/A      case 0: // F_DUPFD
6052093SN/A        // if we really wanted to support this, we'd need to do it
6062093SN/A        // in the target fd space.
6072093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
6082093SN/A        return -EMFILE;
6092093SN/A
6102093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6112093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6122093SN/A        return 0;
6132093SN/A
6142093SN/A      case 3: // F_GETFL (get file flags)
6152093SN/A      case 4: // F_SETFL (set file flags)
6162093SN/A        // not sure if this is totally valid, but we'll pass it through
6172093SN/A        // to the underlying OS
6182093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6192093SN/A        return fcntl(process->sim_fd(fd), cmd);
6202093SN/A        // return 0;
6212093SN/A
6222093SN/A      case 7: // F_GETLK  (get lock)
6232093SN/A      case 8: // F_SETLK  (set lock)
6242093SN/A      case 9: // F_SETLKW (set lock and wait)
6252093SN/A        // don't mess with file locking... just act like it's OK
6262093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6272093SN/A        return 0;
6282093SN/A
6292093SN/A      default:
6302093SN/A        warn("Unknown fcntl command %d\n", cmd);
6312093SN/A        return 0;
6322093SN/A    }
6332093SN/A}
6342093SN/A
6352238SN/ASyscallReturn
6363114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6372687Sksewell@umich.edu            ThreadContext *tc)
6382687Sksewell@umich.edu{
6396701Sgblack@eecs.umich.edu    int index = 0;
6406701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6412687Sksewell@umich.edu
6422687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6432687Sksewell@umich.edu        return -EBADF;
6442687Sksewell@umich.edu
6456701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6462687Sksewell@umich.edu    switch (cmd) {
6472687Sksewell@umich.edu      case 33: //F_GETLK64
6482687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6492687Sksewell@umich.edu        return -EMFILE;
6502687Sksewell@umich.edu
6512687Sksewell@umich.edu      case 34: // F_SETLK64
6522687Sksewell@umich.edu      case 35: // F_SETLKW64
6532687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6542687Sksewell@umich.edu        return -EMFILE;
6552687Sksewell@umich.edu
6562687Sksewell@umich.edu      default:
6572687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6582687Sksewell@umich.edu        // to the underlying OS
6592687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6602687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6612687Sksewell@umich.edu        // return 0;
6622687Sksewell@umich.edu    }
6632687Sksewell@umich.edu}
6642687Sksewell@umich.edu
6652687Sksewell@umich.eduSyscallReturn
6663114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6672680Sktlim@umich.edu         ThreadContext *tc)
6682238SN/A{
6692238SN/A    int fds[2], sim_fds[2];
6702238SN/A    int pipe_retval = pipe(fds);
6712093SN/A
6722238SN/A    if (pipe_retval < 0) {
6732238SN/A        // error
6742238SN/A        return pipe_retval;
6752238SN/A    }
6762238SN/A
6775282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6785282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6792238SN/A
6805282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6812238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6822238SN/A    // the return value of the function, and fd[1] is returned in r20.
6832680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6842238SN/A    return sim_fds[0];
6852238SN/A}
6862238SN/A
6872238SN/A
6882238SN/ASyscallReturn
6893114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6902680Sktlim@umich.edu           ThreadContext *tc)
6912238SN/A{
6922238SN/A    // Make up a PID.  There's no interprocess communication in
6932238SN/A    // fake_syscall mode, so there's no way for a process to know it's
6942238SN/A    // not getting a unique value.
6952238SN/A
6963114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
6973114Sgblack@eecs.umich.edu    return process->pid();
6982238SN/A}
6992238SN/A
7002238SN/A
7012238SN/ASyscallReturn
7023114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7032680Sktlim@umich.edu           ThreadContext *tc)
7042238SN/A{
7052238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7062238SN/A    // simulation to be deterministic.
7072238SN/A
7082238SN/A    // EUID goes in r20.
7093114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7105543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7112238SN/A}
7122238SN/A
7132238SN/A
7142238SN/ASyscallReturn
7153114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7162680Sktlim@umich.edu           ThreadContext *tc)
7172238SN/A{
7182238SN/A    // Get current group ID.  EGID goes in r20.
7193114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7203114Sgblack@eecs.umich.edu    return process->gid();
7212238SN/A}
7222238SN/A
7232238SN/A
7242238SN/ASyscallReturn
7253114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7262680Sktlim@umich.edu           ThreadContext *tc)
7272238SN/A{
7282238SN/A    // can't fathom why a benchmark would call this.
7296701Sgblack@eecs.umich.edu    int index = 0;
7306701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7312238SN/A    return 0;
7322238SN/A}
7332238SN/A
7342238SN/ASyscallReturn
7353114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7362680Sktlim@umich.edu           ThreadContext *tc)
7372238SN/A{
7382238SN/A    // Make up a PID.  There's no interprocess communication in
7392238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7402238SN/A    // not getting a unique value.
7412238SN/A
7423114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7433114Sgblack@eecs.umich.edu    return process->pid();
7442238SN/A}
7452238SN/A
7462238SN/ASyscallReturn
7473114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7482680Sktlim@umich.edu           ThreadContext *tc)
7492238SN/A{
7503114Sgblack@eecs.umich.edu    return process->ppid();
7512238SN/A}
7522238SN/A
7532238SN/ASyscallReturn
7543114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7552680Sktlim@umich.edu           ThreadContext *tc)
7562238SN/A{
7575543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7582238SN/A}
7592238SN/A
7602238SN/ASyscallReturn
7613114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7622680Sktlim@umich.edu           ThreadContext *tc)
7632238SN/A{
7645543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7652238SN/A}
7662238SN/A
7672238SN/ASyscallReturn
7683114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7692680Sktlim@umich.edu           ThreadContext *tc)
7702238SN/A{
7713114Sgblack@eecs.umich.edu    return process->gid();
7722238SN/A}
7732238SN/A
7742238SN/ASyscallReturn
7753114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7762680Sktlim@umich.edu           ThreadContext *tc)
7772238SN/A{
7783114Sgblack@eecs.umich.edu    return process->egid();
7792238SN/A}
7802238SN/A
7812238SN/A
7826109Ssanchezd@stanford.eduSyscallReturn
7836109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7846109Ssanchezd@stanford.edu           ThreadContext *tc)
7856109Ssanchezd@stanford.edu{
7866701Sgblack@eecs.umich.edu    int index = 0;
7876701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7886701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
7896701Sgblack@eecs.umich.edu
7906109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
7916701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
7926701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
7936109Ssanchezd@stanford.edu
7946109Ssanchezd@stanford.edu
7956701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
7966111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
7976111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
7986111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
7996701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8006109Ssanchezd@stanford.edu    }
8016109Ssanchezd@stanford.edu
8026111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8036109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8046109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8056109Ssanchezd@stanford.edu
8066109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8076109Ssanchezd@stanford.edu
8086111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8096109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8106111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8116109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8126109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8136109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8146109Ssanchezd@stanford.edu
8156111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8166109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8176109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8186109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8196109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8206337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8216109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8226109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8239375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8246109Ssanchezd@stanford.edu
8256109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8266109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8278149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8288149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8296109Ssanchezd@stanford.edu        #else
8306109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8316109Ssanchezd@stanford.edu        #endif
8326109Ssanchezd@stanford.edu
8336111Ssteve.reinhardt@amd.com        // Set up stack register
8346701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8356109Ssanchezd@stanford.edu
8366111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8376111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8386109Ssanchezd@stanford.edu
8396111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8406109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8416110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8426109Ssanchezd@stanford.edu        #endif
8436109Ssanchezd@stanford.edu
8446111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8456111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8466109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8476109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8486109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8496109Ssanchezd@stanford.edu        #endif
8506109Ssanchezd@stanford.edu
8517720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8526109Ssanchezd@stanford.edu
8536109Ssanchezd@stanford.edu        ctc->activate();
8546109Ssanchezd@stanford.edu
8556109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8566109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8576109Ssanchezd@stanford.edu        return 1;
8586109Ssanchezd@stanford.edu    } else {
8596109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8606109Ssanchezd@stanford.edu        return 0;
8616109Ssanchezd@stanford.edu    }
8626109Ssanchezd@stanford.edu}
8636109Ssanchezd@stanford.edu
8649455Smitch.hayenga+gem5@gmail.comSyscallReturn
86510203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
86610203SAli.Saidi@ARM.com        int index)
8679455Smitch.hayenga+gem5@gmail.com{
8689455Smitch.hayenga+gem5@gmail.com    string path;
8699455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
87010223Ssteve.reinhardt@amd.com        return -EFAULT;
8719455Smitch.hayenga+gem5@gmail.com
8729455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
8739455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
8749455Smitch.hayenga+gem5@gmail.com
8759455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
8769455Smitch.hayenga+gem5@gmail.com
8779455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
8789455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
8799455Smitch.hayenga+gem5@gmail.com}
88010203SAli.Saidi@ARM.com
88110203SAli.Saidi@ARM.comSyscallReturn
88210203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
88310203SAli.Saidi@ARM.com{
88410203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
88510203SAli.Saidi@ARM.com}
88610203SAli.Saidi@ARM.com
887