syscall_emul.cc revision 10203
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{
586702Sgblack@eecs.umich.edu#if TRACING_ON
596701Sgblack@eecs.umich.edu    int index = 0;
606702Sgblack@eecs.umich.edu#endif
616111Ssteve.reinhardt@amd.com    DPRINTFR(SyscallVerbose,
626111Ssteve.reinhardt@amd.com             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
637823Ssteve.reinhardt@amd.com             curTick(), tc->getCpuPtr()->name(), name,
646701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
656701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
666701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
676701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index));
68360SN/A
692680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
70360SN/A
712495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
727823Ssteve.reinhardt@amd.com             curTick(),tc->getCpuPtr()->name(), name, retval.value());
73360SN/A
741450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
755958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
76360SN/A}
77360SN/A
78360SN/A
791450SN/ASyscallReturn
803114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
812680Sktlim@umich.edu                  ThreadContext *tc)
82360SN/A{
831969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
842484SN/A
852484SN/A    return 1;
86360SN/A}
87360SN/A
88360SN/A
891450SN/ASyscallReturn
903114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
912680Sktlim@umich.edu           ThreadContext *tc)
92360SN/A{
936701Sgblack@eecs.umich.edu    int index = 0;
941969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
956701Sgblack@eecs.umich.edu         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
96360SN/A
971458SN/A    return 0;
98360SN/A}
99360SN/A
100360SN/A
1011450SN/ASyscallReturn
1028149SChris.Emmons@ARM.comignoreWarnOnceFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1038149SChris.Emmons@ARM.com           ThreadContext *tc)
1048149SChris.Emmons@ARM.com{
1058149SChris.Emmons@ARM.com    int index = 0;
1068149SChris.Emmons@ARM.com    warn_once("ignoring syscall %s(%d, %d, ...)", desc->name,
1078149SChris.Emmons@ARM.com         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
1088149SChris.Emmons@ARM.com
1098149SChris.Emmons@ARM.com    return 0;
1108149SChris.Emmons@ARM.com}
1118149SChris.Emmons@ARM.com
1128149SChris.Emmons@ARM.com
1138149SChris.Emmons@ARM.comSyscallReturn
1143114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1152680Sktlim@umich.edu         ThreadContext *tc)
116360SN/A{
1176029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1186029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1196701Sgblack@eecs.umich.edu        int index = 0;
1205958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1216701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1226029Ssteve.reinhardt@amd.com    } else {
1236029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1246029Ssteve.reinhardt@amd.com        tc->halt();
1252834Sksewell@umich.edu    }
126360SN/A
1271458SN/A    return 1;
128360SN/A}
129360SN/A
130360SN/A
1311450SN/ASyscallReturn
1326109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1336109Ssanchezd@stanford.edu              ThreadContext *tc)
1346109Ssanchezd@stanford.edu{
1356109Ssanchezd@stanford.edu    // really should just halt all thread contexts belonging to this
1366109Ssanchezd@stanford.edu    // process in case there's another process running...
1376701Sgblack@eecs.umich.edu    int index = 0;
1386109Ssanchezd@stanford.edu    exitSimLoop("target called exit()",
1396701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index) & 0xff);
1406109Ssanchezd@stanford.edu
1416109Ssanchezd@stanford.edu    return 1;
1426109Ssanchezd@stanford.edu}
1436109Ssanchezd@stanford.edu
1446109Ssanchezd@stanford.edu
1456109Ssanchezd@stanford.eduSyscallReturn
1463114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
147360SN/A{
1482107SN/A    return (int)VMPageSize;
149360SN/A}
150360SN/A
151360SN/A
1521450SN/ASyscallReturn
1535748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
154360SN/A{
155360SN/A    // change brk addr to first arg
1566701Sgblack@eecs.umich.edu    int index = 0;
1576701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1585748SSteve.Reinhardt@amd.com
1595748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1605748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1615748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1625748SSteve.Reinhardt@amd.com        return p->brk_point;
1635748SSteve.Reinhardt@amd.com
1645748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1655748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1662474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1672474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1685748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
1698601Ssteve.reinhardt@amd.com                p->allocateMem(roundDown(gen.addr(), VMPageSize), VMPageSize);
1706687Stjones1@inf.ed.ac.uk
1716687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1726687Stjones1@inf.ed.ac.uk            else {
1736687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1748852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1756687Stjones1@inf.ed.ac.uk
1766687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
1776687Stjones1@inf.ed.ac.uk                Addr next_page = roundUp(gen.addr(), VMPageSize);
1786687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1798852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
1806687Stjones1@inf.ed.ac.uk                if (gen.addr() + VMPageSize > next_page &&
1816687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1826687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1836687Stjones1@inf.ed.ac.uk                {
1846687Stjones1@inf.ed.ac.uk                    size_needed = VMPageSize - size_needed;
1858852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1866687Stjones1@inf.ed.ac.uk                }
1876687Stjones1@inf.ed.ac.uk            }
1882474SN/A        }
1891450SN/A    }
1905748SSteve.Reinhardt@amd.com
1915748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
1921458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1931458SN/A    return p->brk_point;
194360SN/A}
195360SN/A
196360SN/A
1971450SN/ASyscallReturn
1983114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
199360SN/A{
2006701Sgblack@eecs.umich.edu    int index = 0;
2016701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
2027508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
2037508Stjones1@inf.ed.ac.uk    int status = 0;
2047508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2057508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2061970SN/A    if (status >= 0)
2071970SN/A        p->free_fd(target_fd);
2081970SN/A    return status;
209360SN/A}
210360SN/A
211360SN/A
2121450SN/ASyscallReturn
2133114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
214360SN/A{
2156701Sgblack@eecs.umich.edu    int index = 0;
2166701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2176701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2186701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2196701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
220360SN/A
221360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
222360SN/A
223360SN/A    if (bytes_read != -1)
2248706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
225360SN/A
2261458SN/A    return bytes_read;
227360SN/A}
228360SN/A
2291450SN/ASyscallReturn
2303114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
231360SN/A{
2326701Sgblack@eecs.umich.edu    int index = 0;
2336701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2346701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2356701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2366701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
237360SN/A
2388706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
239360SN/A
240360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
241360SN/A
242360SN/A    fsync(fd);
243360SN/A
2441458SN/A    return bytes_written;
245360SN/A}
246360SN/A
247360SN/A
2481450SN/ASyscallReturn
2493114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
250360SN/A{
2516701Sgblack@eecs.umich.edu    int index = 0;
2526701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2536701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2546701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
255360SN/A
256360SN/A    off_t result = lseek(fd, offs, whence);
257360SN/A
2581458SN/A    return (result == (off_t)-1) ? -errno : result;
259360SN/A}
260360SN/A
261360SN/A
2621450SN/ASyscallReturn
2634118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2644118Sgblack@eecs.umich.edu{
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 offset_high = p->getSyscallArg(tc, index);
2686701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2696701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2706701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2714118Sgblack@eecs.umich.edu
2724118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2734118Sgblack@eecs.umich.edu
2744118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2754118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2764118Sgblack@eecs.umich.edu
2774118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2784118Sgblack@eecs.umich.edu        //The seek failed.
2794118Sgblack@eecs.umich.edu        return -errno;
2804118Sgblack@eecs.umich.edu    } else {
2816111Ssteve.reinhardt@amd.com        // The seek succeeded.
2826111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2836111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2846111Ssteve.reinhardt@amd.com        // target platform
2854118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2864118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2878706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
2884118Sgblack@eecs.umich.edu        return 0;
2894118Sgblack@eecs.umich.edu    }
2904118Sgblack@eecs.umich.edu
2914118Sgblack@eecs.umich.edu
2924118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2934118Sgblack@eecs.umich.edu}
2944118Sgblack@eecs.umich.edu
2954118Sgblack@eecs.umich.edu
2964118Sgblack@eecs.umich.eduSyscallReturn
2973114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
298360SN/A{
299360SN/A    // given that we don't really implement mmap, munmap is really easy
3001458SN/A    return 0;
301360SN/A}
302360SN/A
303360SN/A
304360SN/Aconst char *hostname = "m5.eecs.umich.edu";
305360SN/A
3061450SN/ASyscallReturn
3073114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
308360SN/A{
3096701Sgblack@eecs.umich.edu    int index = 0;
3106701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3116701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3126701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
313360SN/A
314360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
315360SN/A
3168706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
317360SN/A
3181458SN/A    return 0;
319360SN/A}
320360SN/A
3211450SN/ASyscallReturn
3225513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3235513SMichael.Adler@intel.com{
3245513SMichael.Adler@intel.com    int result = 0;
3256731Svince@csl.cornell.edu    int index = 0;
3266701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3276701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3286701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3295513SMichael.Adler@intel.com
3305513SMichael.Adler@intel.com    // Is current working directory defined?
3315513SMichael.Adler@intel.com    string cwd = p->getcwd();
3325513SMichael.Adler@intel.com    if (!cwd.empty()) {
3335513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3345513SMichael.Adler@intel.com            // Buffer too small
3355513SMichael.Adler@intel.com            return -ERANGE;
3365513SMichael.Adler@intel.com        }
3375513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3385513SMichael.Adler@intel.com        result = cwd.length();
3395513SMichael.Adler@intel.com    }
3405513SMichael.Adler@intel.com    else {
3415513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3425513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3435513SMichael.Adler@intel.com        }
3445513SMichael.Adler@intel.com        else {
3455513SMichael.Adler@intel.com            result = -1;
3465513SMichael.Adler@intel.com        }
3475513SMichael.Adler@intel.com    }
3485513SMichael.Adler@intel.com
3498706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3505513SMichael.Adler@intel.com
3515513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3525513SMichael.Adler@intel.com}
3535513SMichael.Adler@intel.com
35410203SAli.Saidi@ARM.com/// Target open() handler.
35510203SAli.Saidi@ARM.comSyscallReturn
35610203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
35710203SAli.Saidi@ARM.com         ThreadContext *tc)
35810203SAli.Saidi@ARM.com{
35910203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
36010203SAli.Saidi@ARM.com}
3615513SMichael.Adler@intel.com
3625513SMichael.Adler@intel.comSyscallReturn
36310203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
36410203SAli.Saidi@ARM.com        int index)
3655513SMichael.Adler@intel.com{
3665513SMichael.Adler@intel.com    string path;
3675513SMichael.Adler@intel.com
3688852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
3695513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3705513SMichael.Adler@intel.com
3715513SMichael.Adler@intel.com    // Adjust path for current working directory
3725513SMichael.Adler@intel.com    path = p->fullPath(path);
3735513SMichael.Adler@intel.com
3746701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3756701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3766701Sgblack@eecs.umich.edu
3776701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3785513SMichael.Adler@intel.com
3795513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3805513SMichael.Adler@intel.com
3818706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3825513SMichael.Adler@intel.com
3835513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3845513SMichael.Adler@intel.com}
3855513SMichael.Adler@intel.com
3865513SMichael.Adler@intel.comSyscallReturn
3873114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
388511SN/A{
3891706SN/A    string path;
390360SN/A
3916701Sgblack@eecs.umich.edu    int index = 0;
3928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
3931450SN/A        return (TheISA::IntReg)-EFAULT;
394511SN/A
3953669Sbinkertn@umich.edu    // Adjust path for current working directory
3963669Sbinkertn@umich.edu    path = p->fullPath(path);
3973669Sbinkertn@umich.edu
398511SN/A    int result = unlink(path.c_str());
3991458SN/A    return (result == -1) ? -errno : result;
400511SN/A}
401511SN/A
4025513SMichael.Adler@intel.com
4035513SMichael.Adler@intel.comSyscallReturn
4045513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4055513SMichael.Adler@intel.com{
4065513SMichael.Adler@intel.com    string path;
4075513SMichael.Adler@intel.com
4086701Sgblack@eecs.umich.edu    int index = 0;
4098852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4105513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
4115513SMichael.Adler@intel.com
4125513SMichael.Adler@intel.com    // Adjust path for current working directory
4135513SMichael.Adler@intel.com    path = p->fullPath(path);
4145513SMichael.Adler@intel.com
4156701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4165513SMichael.Adler@intel.com
4175513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4185513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4195513SMichael.Adler@intel.com}
4205513SMichael.Adler@intel.com
4211450SN/ASyscallReturn
4223114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
423511SN/A{
4241706SN/A    string old_name;
425511SN/A
4266701Sgblack@eecs.umich.edu    int index = 0;
4278852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4281458SN/A        return -EFAULT;
429511SN/A
4301706SN/A    string new_name;
431511SN/A
4328852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4331458SN/A        return -EFAULT;
434511SN/A
4353669Sbinkertn@umich.edu    // Adjust path for current working directory
4363669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4373669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4383669Sbinkertn@umich.edu
4391706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4401458SN/A    return (result == -1) ? -errno : result;
441511SN/A}
442511SN/A
4431706SN/ASyscallReturn
4443114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4451706SN/A{
4461706SN/A    string path;
4471706SN/A
4486701Sgblack@eecs.umich.edu    int index = 0;
4498852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4501706SN/A        return -EFAULT;
4511706SN/A
4526701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4531706SN/A
4543669Sbinkertn@umich.edu    // Adjust path for current working directory
4553669Sbinkertn@umich.edu    path = p->fullPath(path);
4563669Sbinkertn@umich.edu
4571706SN/A    int result = truncate(path.c_str(), length);
4581706SN/A    return (result == -1) ? -errno : result;
4591706SN/A}
4601706SN/A
4611706SN/ASyscallReturn
4626111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4636111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4641706SN/A{
4656701Sgblack@eecs.umich.edu    int index = 0;
4666701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4671706SN/A
4681706SN/A    if (fd < 0)
4691706SN/A        return -EBADF;
4701706SN/A
4716701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4721706SN/A
4731706SN/A    int result = ftruncate(fd, length);
4741706SN/A    return (result == -1) ? -errno : result;
4751706SN/A}
4761999SN/A
4771999SN/ASyscallReturn
4786703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4796703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4806703Svince@csl.cornell.edu{
4816703Svince@csl.cornell.edu    int index = 0;
4826703Svince@csl.cornell.edu    string path;
4836703Svince@csl.cornell.edu
4848852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
4856703Svince@csl.cornell.edu       return -EFAULT;
4866703Svince@csl.cornell.edu
4876744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
4886703Svince@csl.cornell.edu
4896703Svince@csl.cornell.edu    // Adjust path for current working directory
4906703Svince@csl.cornell.edu    path = process->fullPath(path);
4916703Svince@csl.cornell.edu
4926744SAli.Saidi@arm.com#if NO_STAT64
4936744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
4946744SAli.Saidi@arm.com#else
4956703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
4966744SAli.Saidi@arm.com#endif
4976703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
4986703Svince@csl.cornell.edu}
4996703Svince@csl.cornell.edu
5006703Svince@csl.cornell.eduSyscallReturn
5016685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5026685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5036685Stjones1@inf.ed.ac.uk{
5046701Sgblack@eecs.umich.edu    int index = 0;
5056701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5066685Stjones1@inf.ed.ac.uk
5076685Stjones1@inf.ed.ac.uk    if (fd < 0)
5086685Stjones1@inf.ed.ac.uk        return -EBADF;
5096685Stjones1@inf.ed.ac.uk
5106744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5116685Stjones1@inf.ed.ac.uk
5126744SAli.Saidi@arm.com#if NO_STAT64
5136744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5146744SAli.Saidi@arm.com#else
5156685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5166744SAli.Saidi@arm.com#endif
5176685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5186685Stjones1@inf.ed.ac.uk}
5196685Stjones1@inf.ed.ac.uk
5206685Stjones1@inf.ed.ac.ukSyscallReturn
5215513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5225513SMichael.Adler@intel.com{
5235513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5245513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5255513SMichael.Adler@intel.com    // changing anything.
5265513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5275513SMichael.Adler@intel.com    umask(oldMask);
5285521Snate@binkert.org    return (int)oldMask;
5295513SMichael.Adler@intel.com}
5305513SMichael.Adler@intel.com
5315513SMichael.Adler@intel.comSyscallReturn
5323114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5331999SN/A{
5341999SN/A    string path;
5351999SN/A
5366701Sgblack@eecs.umich.edu    int index = 0;
5378852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5381999SN/A        return -EFAULT;
5391999SN/A
5401999SN/A    /* XXX endianess */
5416701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5421999SN/A    uid_t hostOwner = owner;
5436701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5441999SN/A    gid_t hostGroup = group;
5451999SN/A
5463669Sbinkertn@umich.edu    // Adjust path for current working directory
5473669Sbinkertn@umich.edu    path = p->fullPath(path);
5483669Sbinkertn@umich.edu
5491999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5501999SN/A    return (result == -1) ? -errno : result;
5511999SN/A}
5521999SN/A
5531999SN/ASyscallReturn
5543114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5551999SN/A{
5566701Sgblack@eecs.umich.edu    int index = 0;
5576701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5581999SN/A
5591999SN/A    if (fd < 0)
5601999SN/A        return -EBADF;
5611999SN/A
5621999SN/A    /* XXX endianess */
5636701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5641999SN/A    uid_t hostOwner = owner;
5656701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5661999SN/A    gid_t hostGroup = group;
5671999SN/A
5681999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5691999SN/A    return (result == -1) ? -errno : result;
5701999SN/A}
5712093SN/A
5722093SN/A
5732093SN/ASyscallReturn
5743114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5753079Sstever@eecs.umich.edu{
5766701Sgblack@eecs.umich.edu    int index = 0;
5776701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5783079Sstever@eecs.umich.edu    if (fd < 0)
5793079Sstever@eecs.umich.edu        return -EBADF;
5803079Sstever@eecs.umich.edu
5816701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5825282Srstrong@cs.ucsd.edu
5833079Sstever@eecs.umich.edu    int result = dup(fd);
5846111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5856111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
5863079Sstever@eecs.umich.edu}
5873079Sstever@eecs.umich.edu
5883079Sstever@eecs.umich.edu
5893079Sstever@eecs.umich.eduSyscallReturn
5903114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
5912680Sktlim@umich.edu          ThreadContext *tc)
5922093SN/A{
5936701Sgblack@eecs.umich.edu    int index = 0;
5946701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5952093SN/A
5962093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
5972093SN/A        return -EBADF;
5982093SN/A
5996701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6002093SN/A    switch (cmd) {
6012093SN/A      case 0: // F_DUPFD
6022093SN/A        // if we really wanted to support this, we'd need to do it
6032093SN/A        // in the target fd space.
6042093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
6052093SN/A        return -EMFILE;
6062093SN/A
6072093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6082093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6092093SN/A        return 0;
6102093SN/A
6112093SN/A      case 3: // F_GETFL (get file flags)
6122093SN/A      case 4: // F_SETFL (set file flags)
6132093SN/A        // not sure if this is totally valid, but we'll pass it through
6142093SN/A        // to the underlying OS
6152093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6162093SN/A        return fcntl(process->sim_fd(fd), cmd);
6172093SN/A        // return 0;
6182093SN/A
6192093SN/A      case 7: // F_GETLK  (get lock)
6202093SN/A      case 8: // F_SETLK  (set lock)
6212093SN/A      case 9: // F_SETLKW (set lock and wait)
6222093SN/A        // don't mess with file locking... just act like it's OK
6232093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6242093SN/A        return 0;
6252093SN/A
6262093SN/A      default:
6272093SN/A        warn("Unknown fcntl command %d\n", cmd);
6282093SN/A        return 0;
6292093SN/A    }
6302093SN/A}
6312093SN/A
6322238SN/ASyscallReturn
6333114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6342687Sksewell@umich.edu            ThreadContext *tc)
6352687Sksewell@umich.edu{
6366701Sgblack@eecs.umich.edu    int index = 0;
6376701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6382687Sksewell@umich.edu
6392687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6402687Sksewell@umich.edu        return -EBADF;
6412687Sksewell@umich.edu
6426701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6432687Sksewell@umich.edu    switch (cmd) {
6442687Sksewell@umich.edu      case 33: //F_GETLK64
6452687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6462687Sksewell@umich.edu        return -EMFILE;
6472687Sksewell@umich.edu
6482687Sksewell@umich.edu      case 34: // F_SETLK64
6492687Sksewell@umich.edu      case 35: // F_SETLKW64
6502687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6512687Sksewell@umich.edu        return -EMFILE;
6522687Sksewell@umich.edu
6532687Sksewell@umich.edu      default:
6542687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6552687Sksewell@umich.edu        // to the underlying OS
6562687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6572687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6582687Sksewell@umich.edu        // return 0;
6592687Sksewell@umich.edu    }
6602687Sksewell@umich.edu}
6612687Sksewell@umich.edu
6622687Sksewell@umich.eduSyscallReturn
6633114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6642680Sktlim@umich.edu         ThreadContext *tc)
6652238SN/A{
6662238SN/A    int fds[2], sim_fds[2];
6672238SN/A    int pipe_retval = pipe(fds);
6682093SN/A
6692238SN/A    if (pipe_retval < 0) {
6702238SN/A        // error
6712238SN/A        return pipe_retval;
6722238SN/A    }
6732238SN/A
6745282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6755282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6762238SN/A
6775282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6782238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6792238SN/A    // the return value of the function, and fd[1] is returned in r20.
6802680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6812238SN/A    return sim_fds[0];
6822238SN/A}
6832238SN/A
6842238SN/A
6852238SN/ASyscallReturn
6863114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6872680Sktlim@umich.edu           ThreadContext *tc)
6882238SN/A{
6892238SN/A    // Make up a PID.  There's no interprocess communication in
6902238SN/A    // fake_syscall mode, so there's no way for a process to know it's
6912238SN/A    // not getting a unique value.
6922238SN/A
6933114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
6943114Sgblack@eecs.umich.edu    return process->pid();
6952238SN/A}
6962238SN/A
6972238SN/A
6982238SN/ASyscallReturn
6993114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7002680Sktlim@umich.edu           ThreadContext *tc)
7012238SN/A{
7022238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7032238SN/A    // simulation to be deterministic.
7042238SN/A
7052238SN/A    // EUID goes in r20.
7063114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7075543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7082238SN/A}
7092238SN/A
7102238SN/A
7112238SN/ASyscallReturn
7123114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7132680Sktlim@umich.edu           ThreadContext *tc)
7142238SN/A{
7152238SN/A    // Get current group ID.  EGID goes in r20.
7163114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7173114Sgblack@eecs.umich.edu    return process->gid();
7182238SN/A}
7192238SN/A
7202238SN/A
7212238SN/ASyscallReturn
7223114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7232680Sktlim@umich.edu           ThreadContext *tc)
7242238SN/A{
7252238SN/A    // can't fathom why a benchmark would call this.
7266701Sgblack@eecs.umich.edu    int index = 0;
7276701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7282238SN/A    return 0;
7292238SN/A}
7302238SN/A
7312238SN/ASyscallReturn
7323114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7332680Sktlim@umich.edu           ThreadContext *tc)
7342238SN/A{
7352238SN/A    // Make up a PID.  There's no interprocess communication in
7362238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7372238SN/A    // not getting a unique value.
7382238SN/A
7393114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7403114Sgblack@eecs.umich.edu    return process->pid();
7412238SN/A}
7422238SN/A
7432238SN/ASyscallReturn
7443114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7452680Sktlim@umich.edu           ThreadContext *tc)
7462238SN/A{
7473114Sgblack@eecs.umich.edu    return process->ppid();
7482238SN/A}
7492238SN/A
7502238SN/ASyscallReturn
7513114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7522680Sktlim@umich.edu           ThreadContext *tc)
7532238SN/A{
7545543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7552238SN/A}
7562238SN/A
7572238SN/ASyscallReturn
7583114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7592680Sktlim@umich.edu           ThreadContext *tc)
7602238SN/A{
7615543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7622238SN/A}
7632238SN/A
7642238SN/ASyscallReturn
7653114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7662680Sktlim@umich.edu           ThreadContext *tc)
7672238SN/A{
7683114Sgblack@eecs.umich.edu    return process->gid();
7692238SN/A}
7702238SN/A
7712238SN/ASyscallReturn
7723114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7732680Sktlim@umich.edu           ThreadContext *tc)
7742238SN/A{
7753114Sgblack@eecs.umich.edu    return process->egid();
7762238SN/A}
7772238SN/A
7782238SN/A
7796109Ssanchezd@stanford.eduSyscallReturn
7806109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7816109Ssanchezd@stanford.edu           ThreadContext *tc)
7826109Ssanchezd@stanford.edu{
7836701Sgblack@eecs.umich.edu    int index = 0;
7846701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7856701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
7866701Sgblack@eecs.umich.edu
7876109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
7886701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
7896701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
7906109Ssanchezd@stanford.edu
7916109Ssanchezd@stanford.edu
7926701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
7936111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
7946111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
7956111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
7966701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
7976109Ssanchezd@stanford.edu    }
7986109Ssanchezd@stanford.edu
7996111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8006109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8016109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8026109Ssanchezd@stanford.edu
8036109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8046109Ssanchezd@stanford.edu
8056111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8066109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8076111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8086109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8096109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8106109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8116109Ssanchezd@stanford.edu
8126111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8136109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8146109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8156109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8166109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8176337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8186109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8196109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8209375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8216109Ssanchezd@stanford.edu
8226109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8236109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8248149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8258149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8266109Ssanchezd@stanford.edu        #else
8276109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8286109Ssanchezd@stanford.edu        #endif
8296109Ssanchezd@stanford.edu
8306111Ssteve.reinhardt@amd.com        // Set up stack register
8316701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8326109Ssanchezd@stanford.edu
8336111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8346111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8356109Ssanchezd@stanford.edu
8366111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8376109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8386110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8396109Ssanchezd@stanford.edu        #endif
8406109Ssanchezd@stanford.edu
8416111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8426111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8436109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8446109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8456109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8466109Ssanchezd@stanford.edu        #endif
8476109Ssanchezd@stanford.edu
8487720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8496109Ssanchezd@stanford.edu
8506109Ssanchezd@stanford.edu        ctc->activate();
8516109Ssanchezd@stanford.edu
8526109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8536109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8546109Ssanchezd@stanford.edu        return 1;
8556109Ssanchezd@stanford.edu    } else {
8566109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8576109Ssanchezd@stanford.edu        return 0;
8586109Ssanchezd@stanford.edu    }
8596109Ssanchezd@stanford.edu}
8606109Ssanchezd@stanford.edu
8619455Smitch.hayenga+gem5@gmail.comSyscallReturn
86210203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
86310203SAli.Saidi@ARM.com        int index)
8649455Smitch.hayenga+gem5@gmail.com{
8659455Smitch.hayenga+gem5@gmail.com    string path;
8669455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
8679455Smitch.hayenga+gem5@gmail.com        return (TheISA::IntReg)-EFAULT;
8689455Smitch.hayenga+gem5@gmail.com
8699455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
8709455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
8719455Smitch.hayenga+gem5@gmail.com
8729455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
8739455Smitch.hayenga+gem5@gmail.com
8749455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
8759455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
8769455Smitch.hayenga+gem5@gmail.com}
87710203SAli.Saidi@ARM.com
87810203SAli.Saidi@ARM.comSyscallReturn
87910203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
88010203SAli.Saidi@ARM.com{
88110203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
88210203SAli.Saidi@ARM.com}
88310203SAli.Saidi@ARM.com
884