syscall_emul.cc revision 10633
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));
23110559Sandreas.hansson@arm.com    assert(fd >= 0);
2326701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2336701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2346701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
235360SN/A
236360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
237360SN/A
238360SN/A    if (bytes_read != -1)
2398706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
240360SN/A
2411458SN/A    return bytes_read;
242360SN/A}
243360SN/A
2441450SN/ASyscallReturn
2453114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
246360SN/A{
2476701Sgblack@eecs.umich.edu    int index = 0;
2486701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2496701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2506701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2516701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
252360SN/A
2538706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
254360SN/A
255360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
256360SN/A
257360SN/A    fsync(fd);
258360SN/A
2591458SN/A    return bytes_written;
260360SN/A}
261360SN/A
262360SN/A
2631450SN/ASyscallReturn
2643114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
265360SN/A{
2666701Sgblack@eecs.umich.edu    int index = 0;
2676701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
26810559Sandreas.hansson@arm.com    assert(fd >= 0);
2696701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2706701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
271360SN/A
272360SN/A    off_t result = lseek(fd, offs, whence);
273360SN/A
2741458SN/A    return (result == (off_t)-1) ? -errno : result;
275360SN/A}
276360SN/A
277360SN/A
2781450SN/ASyscallReturn
2794118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2804118Sgblack@eecs.umich.edu{
2816701Sgblack@eecs.umich.edu    int index = 0;
2826701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
28310559Sandreas.hansson@arm.com    assert(fd >= 0);
2846701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2856701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2866701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2876701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2884118Sgblack@eecs.umich.edu
2894118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2904118Sgblack@eecs.umich.edu
2914118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2924118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2934118Sgblack@eecs.umich.edu
2944118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2954118Sgblack@eecs.umich.edu        //The seek failed.
2964118Sgblack@eecs.umich.edu        return -errno;
2974118Sgblack@eecs.umich.edu    } else {
2986111Ssteve.reinhardt@amd.com        // The seek succeeded.
2996111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
3006111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
3016111Ssteve.reinhardt@amd.com        // target platform
3024118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
3034118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
3048706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
3054118Sgblack@eecs.umich.edu        return 0;
3064118Sgblack@eecs.umich.edu    }
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{
40310633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
40410633Smichaelupton@gmail.com}
40510633Smichaelupton@gmail.com
40610633Smichaelupton@gmail.comSyscallReturn
40710633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
40810633Smichaelupton@gmail.com           int index)
40910633Smichaelupton@gmail.com{
4101706SN/A    string path;
411360SN/A
4128852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
41310223Ssteve.reinhardt@amd.com        return -EFAULT;
414511SN/A
4153669Sbinkertn@umich.edu    // Adjust path for current working directory
4163669Sbinkertn@umich.edu    path = p->fullPath(path);
4173669Sbinkertn@umich.edu
418511SN/A    int result = unlink(path.c_str());
4191458SN/A    return (result == -1) ? -errno : result;
420511SN/A}
421511SN/A
4225513SMichael.Adler@intel.com
4235513SMichael.Adler@intel.comSyscallReturn
4245513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4255513SMichael.Adler@intel.com{
4265513SMichael.Adler@intel.com    string path;
4275513SMichael.Adler@intel.com
4286701Sgblack@eecs.umich.edu    int index = 0;
4298852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
43010223Ssteve.reinhardt@amd.com        return -EFAULT;
4315513SMichael.Adler@intel.com
4325513SMichael.Adler@intel.com    // Adjust path for current working directory
4335513SMichael.Adler@intel.com    path = p->fullPath(path);
4345513SMichael.Adler@intel.com
4356701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4365513SMichael.Adler@intel.com
4375513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4385513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4395513SMichael.Adler@intel.com}
4405513SMichael.Adler@intel.com
4411450SN/ASyscallReturn
4423114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
443511SN/A{
4441706SN/A    string old_name;
445511SN/A
4466701Sgblack@eecs.umich.edu    int index = 0;
4478852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4481458SN/A        return -EFAULT;
449511SN/A
4501706SN/A    string new_name;
451511SN/A
4528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4531458SN/A        return -EFAULT;
454511SN/A
4553669Sbinkertn@umich.edu    // Adjust path for current working directory
4563669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4573669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4583669Sbinkertn@umich.edu
4591706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4601458SN/A    return (result == -1) ? -errno : result;
461511SN/A}
462511SN/A
4631706SN/ASyscallReturn
4643114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4651706SN/A{
4661706SN/A    string path;
4671706SN/A
4686701Sgblack@eecs.umich.edu    int index = 0;
4698852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4701706SN/A        return -EFAULT;
4711706SN/A
4726701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4731706SN/A
4743669Sbinkertn@umich.edu    // Adjust path for current working directory
4753669Sbinkertn@umich.edu    path = p->fullPath(path);
4763669Sbinkertn@umich.edu
4771706SN/A    int result = truncate(path.c_str(), length);
4781706SN/A    return (result == -1) ? -errno : result;
4791706SN/A}
4801706SN/A
4811706SN/ASyscallReturn
4826111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4836111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4841706SN/A{
4856701Sgblack@eecs.umich.edu    int index = 0;
4866701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4871706SN/A
4881706SN/A    if (fd < 0)
4891706SN/A        return -EBADF;
4901706SN/A
4916701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4921706SN/A
4931706SN/A    int result = ftruncate(fd, length);
4941706SN/A    return (result == -1) ? -errno : result;
4951706SN/A}
4961999SN/A
4971999SN/ASyscallReturn
4986703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4996703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5006703Svince@csl.cornell.edu{
5016703Svince@csl.cornell.edu    int index = 0;
5026703Svince@csl.cornell.edu    string path;
5036703Svince@csl.cornell.edu
5048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5056703Svince@csl.cornell.edu       return -EFAULT;
5066703Svince@csl.cornell.edu
5076744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5086703Svince@csl.cornell.edu
5096703Svince@csl.cornell.edu    // Adjust path for current working directory
5106703Svince@csl.cornell.edu    path = process->fullPath(path);
5116703Svince@csl.cornell.edu
5126744SAli.Saidi@arm.com#if NO_STAT64
5136744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5146744SAli.Saidi@arm.com#else
5156703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5166744SAli.Saidi@arm.com#endif
5176703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5186703Svince@csl.cornell.edu}
5196703Svince@csl.cornell.edu
5206703Svince@csl.cornell.eduSyscallReturn
5216685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5226685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5236685Stjones1@inf.ed.ac.uk{
5246701Sgblack@eecs.umich.edu    int index = 0;
5256701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5266685Stjones1@inf.ed.ac.uk
5276685Stjones1@inf.ed.ac.uk    if (fd < 0)
5286685Stjones1@inf.ed.ac.uk        return -EBADF;
5296685Stjones1@inf.ed.ac.uk
5306744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5316685Stjones1@inf.ed.ac.uk
5326744SAli.Saidi@arm.com#if NO_STAT64
5336744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5346744SAli.Saidi@arm.com#else
5356685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5366744SAli.Saidi@arm.com#endif
5376685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5386685Stjones1@inf.ed.ac.uk}
5396685Stjones1@inf.ed.ac.uk
5406685Stjones1@inf.ed.ac.ukSyscallReturn
5415513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5425513SMichael.Adler@intel.com{
5435513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5445513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5455513SMichael.Adler@intel.com    // changing anything.
5465513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5475513SMichael.Adler@intel.com    umask(oldMask);
5485521Snate@binkert.org    return (int)oldMask;
5495513SMichael.Adler@intel.com}
5505513SMichael.Adler@intel.com
5515513SMichael.Adler@intel.comSyscallReturn
5523114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5531999SN/A{
5541999SN/A    string path;
5551999SN/A
5566701Sgblack@eecs.umich.edu    int index = 0;
5578852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5581999SN/A        return -EFAULT;
5591999SN/A
5601999SN/A    /* XXX endianess */
5616701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5621999SN/A    uid_t hostOwner = owner;
5636701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5641999SN/A    gid_t hostGroup = group;
5651999SN/A
5663669Sbinkertn@umich.edu    // Adjust path for current working directory
5673669Sbinkertn@umich.edu    path = p->fullPath(path);
5683669Sbinkertn@umich.edu
5691999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5701999SN/A    return (result == -1) ? -errno : result;
5711999SN/A}
5721999SN/A
5731999SN/ASyscallReturn
5743114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5751999SN/A{
5766701Sgblack@eecs.umich.edu    int index = 0;
5776701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5781999SN/A
5791999SN/A    if (fd < 0)
5801999SN/A        return -EBADF;
5811999SN/A
5821999SN/A    /* XXX endianess */
5836701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5841999SN/A    uid_t hostOwner = owner;
5856701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5861999SN/A    gid_t hostGroup = group;
5871999SN/A
5881999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5891999SN/A    return (result == -1) ? -errno : result;
5901999SN/A}
5912093SN/A
5922093SN/A
5932093SN/ASyscallReturn
5943114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5953079Sstever@eecs.umich.edu{
5966701Sgblack@eecs.umich.edu    int index = 0;
5976701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5983079Sstever@eecs.umich.edu    if (fd < 0)
5993079Sstever@eecs.umich.edu        return -EBADF;
6003079Sstever@eecs.umich.edu
6016701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
6025282Srstrong@cs.ucsd.edu
6033079Sstever@eecs.umich.edu    int result = dup(fd);
6046111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
6056111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
6063079Sstever@eecs.umich.edu}
6073079Sstever@eecs.umich.edu
6083079Sstever@eecs.umich.edu
6093079Sstever@eecs.umich.eduSyscallReturn
6103114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6112680Sktlim@umich.edu          ThreadContext *tc)
6122093SN/A{
6136701Sgblack@eecs.umich.edu    int index = 0;
6146701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6152093SN/A
6162093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
6172093SN/A        return -EBADF;
6182093SN/A
6196701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6202093SN/A    switch (cmd) {
6212093SN/A      case 0: // F_DUPFD
6222093SN/A        // if we really wanted to support this, we'd need to do it
6232093SN/A        // in the target fd space.
6242093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
6252093SN/A        return -EMFILE;
6262093SN/A
6272093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6282093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6292093SN/A        return 0;
6302093SN/A
6312093SN/A      case 3: // F_GETFL (get file flags)
6322093SN/A      case 4: // F_SETFL (set file flags)
6332093SN/A        // not sure if this is totally valid, but we'll pass it through
6342093SN/A        // to the underlying OS
6352093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6362093SN/A        return fcntl(process->sim_fd(fd), cmd);
6372093SN/A        // return 0;
6382093SN/A
6392093SN/A      case 7: // F_GETLK  (get lock)
6402093SN/A      case 8: // F_SETLK  (set lock)
6412093SN/A      case 9: // F_SETLKW (set lock and wait)
6422093SN/A        // don't mess with file locking... just act like it's OK
6432093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6442093SN/A        return 0;
6452093SN/A
6462093SN/A      default:
6472093SN/A        warn("Unknown fcntl command %d\n", cmd);
6482093SN/A        return 0;
6492093SN/A    }
6502093SN/A}
6512093SN/A
6522238SN/ASyscallReturn
6533114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6542687Sksewell@umich.edu            ThreadContext *tc)
6552687Sksewell@umich.edu{
6566701Sgblack@eecs.umich.edu    int index = 0;
6576701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6582687Sksewell@umich.edu
6592687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6602687Sksewell@umich.edu        return -EBADF;
6612687Sksewell@umich.edu
6626701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6632687Sksewell@umich.edu    switch (cmd) {
6642687Sksewell@umich.edu      case 33: //F_GETLK64
6652687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6662687Sksewell@umich.edu        return -EMFILE;
6672687Sksewell@umich.edu
6682687Sksewell@umich.edu      case 34: // F_SETLK64
6692687Sksewell@umich.edu      case 35: // F_SETLKW64
6702687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6712687Sksewell@umich.edu        return -EMFILE;
6722687Sksewell@umich.edu
6732687Sksewell@umich.edu      default:
6742687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6752687Sksewell@umich.edu        // to the underlying OS
6762687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6772687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6782687Sksewell@umich.edu        // return 0;
6792687Sksewell@umich.edu    }
6802687Sksewell@umich.edu}
6812687Sksewell@umich.edu
6822687Sksewell@umich.eduSyscallReturn
6833114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6842680Sktlim@umich.edu         ThreadContext *tc)
6852238SN/A{
6862238SN/A    int fds[2], sim_fds[2];
6872238SN/A    int pipe_retval = pipe(fds);
6882093SN/A
6892238SN/A    if (pipe_retval < 0) {
6902238SN/A        // error
6912238SN/A        return pipe_retval;
6922238SN/A    }
6932238SN/A
6945282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6955282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6962238SN/A
6975282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6982238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6992238SN/A    // the return value of the function, and fd[1] is returned in r20.
7002680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7012238SN/A    return sim_fds[0];
7022238SN/A}
7032238SN/A
7042238SN/A
7052238SN/ASyscallReturn
7063114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7072680Sktlim@umich.edu           ThreadContext *tc)
7082238SN/A{
7092238SN/A    // Make up a PID.  There's no interprocess communication in
7102238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7112238SN/A    // not getting a unique value.
7122238SN/A
7133114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7143114Sgblack@eecs.umich.edu    return process->pid();
7152238SN/A}
7162238SN/A
7172238SN/A
7182238SN/ASyscallReturn
7193114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7202680Sktlim@umich.edu           ThreadContext *tc)
7212238SN/A{
7222238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7232238SN/A    // simulation to be deterministic.
7242238SN/A
7252238SN/A    // EUID goes in r20.
7263114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7275543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7282238SN/A}
7292238SN/A
7302238SN/A
7312238SN/ASyscallReturn
7323114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7332680Sktlim@umich.edu           ThreadContext *tc)
7342238SN/A{
7352238SN/A    // Get current group ID.  EGID goes in r20.
7363114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7373114Sgblack@eecs.umich.edu    return process->gid();
7382238SN/A}
7392238SN/A
7402238SN/A
7412238SN/ASyscallReturn
7423114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7432680Sktlim@umich.edu           ThreadContext *tc)
7442238SN/A{
7452238SN/A    // can't fathom why a benchmark would call this.
7466701Sgblack@eecs.umich.edu    int index = 0;
7476701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7482238SN/A    return 0;
7492238SN/A}
7502238SN/A
7512238SN/ASyscallReturn
7523114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7532680Sktlim@umich.edu           ThreadContext *tc)
7542238SN/A{
7552238SN/A    // Make up a PID.  There's no interprocess communication in
7562238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7572238SN/A    // not getting a unique value.
7582238SN/A
7593114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7603114Sgblack@eecs.umich.edu    return process->pid();
7612238SN/A}
7622238SN/A
7632238SN/ASyscallReturn
7643114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7652680Sktlim@umich.edu           ThreadContext *tc)
7662238SN/A{
7673114Sgblack@eecs.umich.edu    return process->ppid();
7682238SN/A}
7692238SN/A
7702238SN/ASyscallReturn
7713114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7722680Sktlim@umich.edu           ThreadContext *tc)
7732238SN/A{
7745543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7752238SN/A}
7762238SN/A
7772238SN/ASyscallReturn
7783114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7792680Sktlim@umich.edu           ThreadContext *tc)
7802238SN/A{
7815543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7822238SN/A}
7832238SN/A
7842238SN/ASyscallReturn
7853114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7862680Sktlim@umich.edu           ThreadContext *tc)
7872238SN/A{
7883114Sgblack@eecs.umich.edu    return process->gid();
7892238SN/A}
7902238SN/A
7912238SN/ASyscallReturn
7923114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7932680Sktlim@umich.edu           ThreadContext *tc)
7942238SN/A{
7953114Sgblack@eecs.umich.edu    return process->egid();
7962238SN/A}
7972238SN/A
7982238SN/A
7996109Ssanchezd@stanford.eduSyscallReturn
8006109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8016109Ssanchezd@stanford.edu           ThreadContext *tc)
8026109Ssanchezd@stanford.edu{
8036701Sgblack@eecs.umich.edu    int index = 0;
8046701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8056701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8066701Sgblack@eecs.umich.edu
8076109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8086701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8096701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8106109Ssanchezd@stanford.edu
8116109Ssanchezd@stanford.edu
8126701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8136111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8146111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8156111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8166701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8176109Ssanchezd@stanford.edu    }
8186109Ssanchezd@stanford.edu
8196111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8206109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8216109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8226109Ssanchezd@stanford.edu
8236109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8246109Ssanchezd@stanford.edu
8256111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8266109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8276111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8286109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8296109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8306109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8316109Ssanchezd@stanford.edu
8326111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8336109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8346109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8356109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8366109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8376337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8386109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8396109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8409375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8416109Ssanchezd@stanford.edu
8426109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8436109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8448149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8458149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8466109Ssanchezd@stanford.edu        #else
8476109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8486109Ssanchezd@stanford.edu        #endif
8496109Ssanchezd@stanford.edu
8506111Ssteve.reinhardt@amd.com        // Set up stack register
8516701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8526109Ssanchezd@stanford.edu
8536111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8546111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8556109Ssanchezd@stanford.edu
8566111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8576109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8586110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8596109Ssanchezd@stanford.edu        #endif
8606109Ssanchezd@stanford.edu
8616111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8626111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8636109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8646109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8656109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8666109Ssanchezd@stanford.edu        #endif
8676109Ssanchezd@stanford.edu
8687720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8696109Ssanchezd@stanford.edu
8706109Ssanchezd@stanford.edu        ctc->activate();
8716109Ssanchezd@stanford.edu
8726109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8736109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8746109Ssanchezd@stanford.edu        return 1;
8756109Ssanchezd@stanford.edu    } else {
8766109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8776109Ssanchezd@stanford.edu        return 0;
8786109Ssanchezd@stanford.edu    }
8796109Ssanchezd@stanford.edu}
8806109Ssanchezd@stanford.edu
8819455Smitch.hayenga+gem5@gmail.comSyscallReturn
88210203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
88310203SAli.Saidi@ARM.com        int index)
8849455Smitch.hayenga+gem5@gmail.com{
8859455Smitch.hayenga+gem5@gmail.com    string path;
8869455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
88710223Ssteve.reinhardt@amd.com        return -EFAULT;
8889455Smitch.hayenga+gem5@gmail.com
8899455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
8909455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
8919455Smitch.hayenga+gem5@gmail.com
8929455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
8939455Smitch.hayenga+gem5@gmail.com
8949455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
8959455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
8969455Smitch.hayenga+gem5@gmail.com}
89710203SAli.Saidi@ARM.com
89810203SAli.Saidi@ARM.comSyscallReturn
89910203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
90010203SAli.Saidi@ARM.com{
90110203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
90210203SAli.Saidi@ARM.com}
90310203SAli.Saidi@ARM.com
904