syscall_emul.cc revision 10831
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;
10210831Ssteve.reinhardt@amd.com    const char *extra_text = "";
103360SN/A
10410831Ssteve.reinhardt@amd.com    if (desc->warnOnce()) {
10510831Ssteve.reinhardt@amd.com        if (desc->warned)
10610831Ssteve.reinhardt@amd.com            return 0;
107360SN/A
10810831Ssteve.reinhardt@amd.com        desc->warned = true;
10910831Ssteve.reinhardt@amd.com        extra_text = "\n      (further warnings will be suppressed)";
11010831Ssteve.reinhardt@amd.com    }
111360SN/A
11210831Ssteve.reinhardt@amd.com    warn("ignoring syscall %s(%d, ...)%s", desc->name,
11310831Ssteve.reinhardt@amd.com         process->getSyscallArg(tc, index), extra_text);
1148149SChris.Emmons@ARM.com
1158149SChris.Emmons@ARM.com    return 0;
1168149SChris.Emmons@ARM.com}
1178149SChris.Emmons@ARM.com
1188149SChris.Emmons@ARM.com
1198149SChris.Emmons@ARM.comSyscallReturn
1203114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1212680Sktlim@umich.edu         ThreadContext *tc)
122360SN/A{
1236029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1246029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1256701Sgblack@eecs.umich.edu        int index = 0;
1265958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1276701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1286029Ssteve.reinhardt@amd.com    } else {
1296029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1306029Ssteve.reinhardt@amd.com        tc->halt();
1312834Sksewell@umich.edu    }
132360SN/A
1331458SN/A    return 1;
134360SN/A}
135360SN/A
136360SN/A
1371450SN/ASyscallReturn
1386109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1396109Ssanchezd@stanford.edu              ThreadContext *tc)
1406109Ssanchezd@stanford.edu{
14110483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
14210483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
14310483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
14410483Swiseveri@student.ethz.ch    }
14510483Swiseveri@student.ethz.ch
14610483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
14710483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
14810483Swiseveri@student.ethz.ch        int index = 0;
14910483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
15010483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
15110483Swiseveri@student.ethz.ch    }
1526109Ssanchezd@stanford.edu
1536109Ssanchezd@stanford.edu    return 1;
1546109Ssanchezd@stanford.edu}
1556109Ssanchezd@stanford.edu
1566109Ssanchezd@stanford.edu
1576109Ssanchezd@stanford.eduSyscallReturn
1583114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
159360SN/A{
16010318Sandreas.hansson@arm.com    return (int)PageBytes;
161360SN/A}
162360SN/A
163360SN/A
1641450SN/ASyscallReturn
1655748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
166360SN/A{
167360SN/A    // change brk addr to first arg
1686701Sgblack@eecs.umich.edu    int index = 0;
1696701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1705748SSteve.Reinhardt@amd.com
1715748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1725748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1735748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1745748SSteve.Reinhardt@amd.com        return p->brk_point;
1755748SSteve.Reinhardt@amd.com
1765748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1775748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1782474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
17910318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1805748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
18110318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1826687Stjones1@inf.ed.ac.uk
1836687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1846687Stjones1@inf.ed.ac.uk            else {
1856687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1868852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1876687Stjones1@inf.ed.ac.uk
1886687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
18910318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1906687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1918852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
19210318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1936687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1946687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1956687Stjones1@inf.ed.ac.uk                {
19610318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1978852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1986687Stjones1@inf.ed.ac.uk                }
1996687Stjones1@inf.ed.ac.uk            }
2002474SN/A        }
2011450SN/A    }
2025748SSteve.Reinhardt@amd.com
2035748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
2041458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
2051458SN/A    return p->brk_point;
206360SN/A}
207360SN/A
208360SN/A
2091450SN/ASyscallReturn
2103114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
211360SN/A{
2126701Sgblack@eecs.umich.edu    int index = 0;
2136701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
2147508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
2157508Stjones1@inf.ed.ac.uk    int status = 0;
2167508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2177508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2181970SN/A    if (status >= 0)
2191970SN/A        p->free_fd(target_fd);
2201970SN/A    return status;
221360SN/A}
222360SN/A
223360SN/A
2241450SN/ASyscallReturn
2253114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
226360SN/A{
2276701Sgblack@eecs.umich.edu    int index = 0;
2286701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
22910559Sandreas.hansson@arm.com    assert(fd >= 0);
2306701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2316701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2326701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
233360SN/A
234360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
235360SN/A
236360SN/A    if (bytes_read != -1)
2378706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
238360SN/A
2391458SN/A    return bytes_read;
240360SN/A}
241360SN/A
2421450SN/ASyscallReturn
2433114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
244360SN/A{
2456701Sgblack@eecs.umich.edu    int index = 0;
2466701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2476701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2486701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2496701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
250360SN/A
2518706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
252360SN/A
253360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
254360SN/A
255360SN/A    fsync(fd);
256360SN/A
2571458SN/A    return bytes_written;
258360SN/A}
259360SN/A
260360SN/A
2611450SN/ASyscallReturn
2623114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
263360SN/A{
2646701Sgblack@eecs.umich.edu    int index = 0;
2656701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
26610559Sandreas.hansson@arm.com    assert(fd >= 0);
2676701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2686701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
269360SN/A
270360SN/A    off_t result = lseek(fd, offs, whence);
271360SN/A
2721458SN/A    return (result == (off_t)-1) ? -errno : result;
273360SN/A}
274360SN/A
275360SN/A
2761450SN/ASyscallReturn
2774118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2784118Sgblack@eecs.umich.edu{
2796701Sgblack@eecs.umich.edu    int index = 0;
2806701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
28110559Sandreas.hansson@arm.com    assert(fd >= 0);
2826701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2836701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2846701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2856701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2864118Sgblack@eecs.umich.edu
2874118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2884118Sgblack@eecs.umich.edu
2894118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2904118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2914118Sgblack@eecs.umich.edu
2924118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2934118Sgblack@eecs.umich.edu        //The seek failed.
2944118Sgblack@eecs.umich.edu        return -errno;
2954118Sgblack@eecs.umich.edu    } else {
2966111Ssteve.reinhardt@amd.com        // The seek succeeded.
2976111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2986111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2996111Ssteve.reinhardt@amd.com        // target platform
3004118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
3014118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
3028706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
3034118Sgblack@eecs.umich.edu        return 0;
3044118Sgblack@eecs.umich.edu    }
3054118Sgblack@eecs.umich.edu}
3064118Sgblack@eecs.umich.edu
3074118Sgblack@eecs.umich.edu
3084118Sgblack@eecs.umich.eduSyscallReturn
3093114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
310360SN/A{
311360SN/A    // given that we don't really implement mmap, munmap is really easy
3121458SN/A    return 0;
313360SN/A}
314360SN/A
315360SN/A
316360SN/Aconst char *hostname = "m5.eecs.umich.edu";
317360SN/A
3181450SN/ASyscallReturn
3193114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
320360SN/A{
3216701Sgblack@eecs.umich.edu    int index = 0;
3226701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3236701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3246701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
325360SN/A
326360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
327360SN/A
3288706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
329360SN/A
3301458SN/A    return 0;
331360SN/A}
332360SN/A
3331450SN/ASyscallReturn
3345513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3355513SMichael.Adler@intel.com{
3365513SMichael.Adler@intel.com    int result = 0;
3376731Svince@csl.cornell.edu    int index = 0;
3386701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3396701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3406701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3415513SMichael.Adler@intel.com
3425513SMichael.Adler@intel.com    // Is current working directory defined?
3435513SMichael.Adler@intel.com    string cwd = p->getcwd();
3445513SMichael.Adler@intel.com    if (!cwd.empty()) {
3455513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3465513SMichael.Adler@intel.com            // Buffer too small
3475513SMichael.Adler@intel.com            return -ERANGE;
3485513SMichael.Adler@intel.com        }
3495513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3505513SMichael.Adler@intel.com        result = cwd.length();
3515513SMichael.Adler@intel.com    }
3525513SMichael.Adler@intel.com    else {
3535513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3545513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3555513SMichael.Adler@intel.com        }
3565513SMichael.Adler@intel.com        else {
3575513SMichael.Adler@intel.com            result = -1;
3585513SMichael.Adler@intel.com        }
3595513SMichael.Adler@intel.com    }
3605513SMichael.Adler@intel.com
3618706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3625513SMichael.Adler@intel.com
3635513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3645513SMichael.Adler@intel.com}
3655513SMichael.Adler@intel.com
36610203SAli.Saidi@ARM.com/// Target open() handler.
36710203SAli.Saidi@ARM.comSyscallReturn
36810203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
36910203SAli.Saidi@ARM.com         ThreadContext *tc)
37010203SAli.Saidi@ARM.com{
37110203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
37210203SAli.Saidi@ARM.com}
3735513SMichael.Adler@intel.com
3745513SMichael.Adler@intel.comSyscallReturn
37510203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
37610203SAli.Saidi@ARM.com        int index)
3775513SMichael.Adler@intel.com{
3785513SMichael.Adler@intel.com    string path;
3795513SMichael.Adler@intel.com
3808852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
38110223Ssteve.reinhardt@amd.com        return -EFAULT;
3825513SMichael.Adler@intel.com
3835513SMichael.Adler@intel.com    // Adjust path for current working directory
3845513SMichael.Adler@intel.com    path = p->fullPath(path);
3855513SMichael.Adler@intel.com
3866701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3876701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3886701Sgblack@eecs.umich.edu
3896701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3905513SMichael.Adler@intel.com
3915513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3925513SMichael.Adler@intel.com
3938706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3945513SMichael.Adler@intel.com
3955513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3965513SMichael.Adler@intel.com}
3975513SMichael.Adler@intel.com
3985513SMichael.Adler@intel.comSyscallReturn
3993114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
400511SN/A{
40110633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
40210633Smichaelupton@gmail.com}
40310633Smichaelupton@gmail.com
40410633Smichaelupton@gmail.comSyscallReturn
40510633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
40610633Smichaelupton@gmail.com           int index)
40710633Smichaelupton@gmail.com{
4081706SN/A    string path;
409360SN/A
4108852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
41110223Ssteve.reinhardt@amd.com        return -EFAULT;
412511SN/A
4133669Sbinkertn@umich.edu    // Adjust path for current working directory
4143669Sbinkertn@umich.edu    path = p->fullPath(path);
4153669Sbinkertn@umich.edu
416511SN/A    int result = unlink(path.c_str());
4171458SN/A    return (result == -1) ? -errno : result;
418511SN/A}
419511SN/A
4205513SMichael.Adler@intel.com
4215513SMichael.Adler@intel.comSyscallReturn
4225513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4235513SMichael.Adler@intel.com{
4245513SMichael.Adler@intel.com    string path;
4255513SMichael.Adler@intel.com
4266701Sgblack@eecs.umich.edu    int index = 0;
4278852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
42810223Ssteve.reinhardt@amd.com        return -EFAULT;
4295513SMichael.Adler@intel.com
4305513SMichael.Adler@intel.com    // Adjust path for current working directory
4315513SMichael.Adler@intel.com    path = p->fullPath(path);
4325513SMichael.Adler@intel.com
4336701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4345513SMichael.Adler@intel.com
4355513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4365513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4375513SMichael.Adler@intel.com}
4385513SMichael.Adler@intel.com
4391450SN/ASyscallReturn
4403114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
441511SN/A{
4421706SN/A    string old_name;
443511SN/A
4446701Sgblack@eecs.umich.edu    int index = 0;
4458852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4461458SN/A        return -EFAULT;
447511SN/A
4481706SN/A    string new_name;
449511SN/A
4508852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4511458SN/A        return -EFAULT;
452511SN/A
4533669Sbinkertn@umich.edu    // Adjust path for current working directory
4543669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4553669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4563669Sbinkertn@umich.edu
4571706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4581458SN/A    return (result == -1) ? -errno : result;
459511SN/A}
460511SN/A
4611706SN/ASyscallReturn
4623114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4631706SN/A{
4641706SN/A    string path;
4651706SN/A
4666701Sgblack@eecs.umich.edu    int index = 0;
4678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4681706SN/A        return -EFAULT;
4691706SN/A
4706701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4711706SN/A
4723669Sbinkertn@umich.edu    // Adjust path for current working directory
4733669Sbinkertn@umich.edu    path = p->fullPath(path);
4743669Sbinkertn@umich.edu
4751706SN/A    int result = truncate(path.c_str(), length);
4761706SN/A    return (result == -1) ? -errno : result;
4771706SN/A}
4781706SN/A
4791706SN/ASyscallReturn
4806111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4816111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4821706SN/A{
4836701Sgblack@eecs.umich.edu    int index = 0;
4846701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4851706SN/A
4861706SN/A    if (fd < 0)
4871706SN/A        return -EBADF;
4881706SN/A
4896701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4901706SN/A
4911706SN/A    int result = ftruncate(fd, length);
4921706SN/A    return (result == -1) ? -errno : result;
4931706SN/A}
4941999SN/A
4951999SN/ASyscallReturn
4966703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4976703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4986703Svince@csl.cornell.edu{
4996703Svince@csl.cornell.edu    int index = 0;
5006703Svince@csl.cornell.edu    string path;
5016703Svince@csl.cornell.edu
5028852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5036703Svince@csl.cornell.edu       return -EFAULT;
5046703Svince@csl.cornell.edu
5056744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5066703Svince@csl.cornell.edu
5076703Svince@csl.cornell.edu    // Adjust path for current working directory
5086703Svince@csl.cornell.edu    path = process->fullPath(path);
5096703Svince@csl.cornell.edu
5106744SAli.Saidi@arm.com#if NO_STAT64
5116744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5126744SAli.Saidi@arm.com#else
5136703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5146744SAli.Saidi@arm.com#endif
5156703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5166703Svince@csl.cornell.edu}
5176703Svince@csl.cornell.edu
5186703Svince@csl.cornell.eduSyscallReturn
5196685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5206685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5216685Stjones1@inf.ed.ac.uk{
5226701Sgblack@eecs.umich.edu    int index = 0;
5236701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5246685Stjones1@inf.ed.ac.uk
5256685Stjones1@inf.ed.ac.uk    if (fd < 0)
5266685Stjones1@inf.ed.ac.uk        return -EBADF;
5276685Stjones1@inf.ed.ac.uk
5286744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5296685Stjones1@inf.ed.ac.uk
5306744SAli.Saidi@arm.com#if NO_STAT64
5316744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5326744SAli.Saidi@arm.com#else
5336685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5346744SAli.Saidi@arm.com#endif
5356685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5366685Stjones1@inf.ed.ac.uk}
5376685Stjones1@inf.ed.ac.uk
5386685Stjones1@inf.ed.ac.ukSyscallReturn
5395513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5405513SMichael.Adler@intel.com{
5415513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5425513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5435513SMichael.Adler@intel.com    // changing anything.
5445513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5455513SMichael.Adler@intel.com    umask(oldMask);
5465521Snate@binkert.org    return (int)oldMask;
5475513SMichael.Adler@intel.com}
5485513SMichael.Adler@intel.com
5495513SMichael.Adler@intel.comSyscallReturn
5503114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5511999SN/A{
5521999SN/A    string path;
5531999SN/A
5546701Sgblack@eecs.umich.edu    int index = 0;
5558852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5561999SN/A        return -EFAULT;
5571999SN/A
5581999SN/A    /* XXX endianess */
5596701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5601999SN/A    uid_t hostOwner = owner;
5616701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5621999SN/A    gid_t hostGroup = group;
5631999SN/A
5643669Sbinkertn@umich.edu    // Adjust path for current working directory
5653669Sbinkertn@umich.edu    path = p->fullPath(path);
5663669Sbinkertn@umich.edu
5671999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5681999SN/A    return (result == -1) ? -errno : result;
5691999SN/A}
5701999SN/A
5711999SN/ASyscallReturn
5723114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5731999SN/A{
5746701Sgblack@eecs.umich.edu    int index = 0;
5756701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5761999SN/A
5771999SN/A    if (fd < 0)
5781999SN/A        return -EBADF;
5791999SN/A
5801999SN/A    /* XXX endianess */
5816701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5821999SN/A    uid_t hostOwner = owner;
5836701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5841999SN/A    gid_t hostGroup = group;
5851999SN/A
5861999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5871999SN/A    return (result == -1) ? -errno : result;
5881999SN/A}
5892093SN/A
5902093SN/A
5912093SN/ASyscallReturn
5923114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5933079Sstever@eecs.umich.edu{
5946701Sgblack@eecs.umich.edu    int index = 0;
59510781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
59610781Snilay@cs.wisc.edu    int sim_fd = process->sim_fd(tgt_fd);
59710781Snilay@cs.wisc.edu    if (sim_fd < 0)
5983079Sstever@eecs.umich.edu        return -EBADF;
5993079Sstever@eecs.umich.edu
60010781Snilay@cs.wisc.edu    Process::FdMap *fdo = process->sim_fd_obj(tgt_fd);
6015282Srstrong@cs.ucsd.edu
60210781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6036111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
6046111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
6053079Sstever@eecs.umich.edu}
6063079Sstever@eecs.umich.edu
6073079Sstever@eecs.umich.edu
6083079Sstever@eecs.umich.eduSyscallReturn
6093114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6102680Sktlim@umich.edu          ThreadContext *tc)
6112093SN/A{
6126701Sgblack@eecs.umich.edu    int index = 0;
6136701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6142093SN/A
6152093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
6162093SN/A        return -EBADF;
6172093SN/A
6186701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6192093SN/A    switch (cmd) {
6202093SN/A      case 0: // F_DUPFD
6212093SN/A        // if we really wanted to support this, we'd need to do it
6222093SN/A        // in the target fd space.
6232093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
6242093SN/A        return -EMFILE;
6252093SN/A
6262093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6272093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6282093SN/A        return 0;
6292093SN/A
6302093SN/A      case 3: // F_GETFL (get file flags)
6312093SN/A      case 4: // F_SETFL (set file flags)
6322093SN/A        // not sure if this is totally valid, but we'll pass it through
6332093SN/A        // to the underlying OS
6342093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6352093SN/A        return fcntl(process->sim_fd(fd), cmd);
6362093SN/A        // return 0;
6372093SN/A
6382093SN/A      case 7: // F_GETLK  (get lock)
6392093SN/A      case 8: // F_SETLK  (set lock)
6402093SN/A      case 9: // F_SETLKW (set lock and wait)
6412093SN/A        // don't mess with file locking... just act like it's OK
6422093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6432093SN/A        return 0;
6442093SN/A
6452093SN/A      default:
6462093SN/A        warn("Unknown fcntl command %d\n", cmd);
6472093SN/A        return 0;
6482093SN/A    }
6492093SN/A}
6502093SN/A
6512238SN/ASyscallReturn
6523114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6532687Sksewell@umich.edu            ThreadContext *tc)
6542687Sksewell@umich.edu{
6556701Sgblack@eecs.umich.edu    int index = 0;
6566701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6572687Sksewell@umich.edu
6582687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6592687Sksewell@umich.edu        return -EBADF;
6602687Sksewell@umich.edu
6616701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6622687Sksewell@umich.edu    switch (cmd) {
6632687Sksewell@umich.edu      case 33: //F_GETLK64
6642687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6652687Sksewell@umich.edu        return -EMFILE;
6662687Sksewell@umich.edu
6672687Sksewell@umich.edu      case 34: // F_SETLK64
6682687Sksewell@umich.edu      case 35: // F_SETLKW64
6692687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6702687Sksewell@umich.edu        return -EMFILE;
6712687Sksewell@umich.edu
6722687Sksewell@umich.edu      default:
6732687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6742687Sksewell@umich.edu        // to the underlying OS
6752687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6762687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6772687Sksewell@umich.edu        // return 0;
6782687Sksewell@umich.edu    }
6792687Sksewell@umich.edu}
6802687Sksewell@umich.edu
6812687Sksewell@umich.eduSyscallReturn
6823114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6832680Sktlim@umich.edu         ThreadContext *tc)
6842238SN/A{
6852238SN/A    int fds[2], sim_fds[2];
6862238SN/A    int pipe_retval = pipe(fds);
6872093SN/A
6882238SN/A    if (pipe_retval < 0) {
6892238SN/A        // error
6902238SN/A        return pipe_retval;
6912238SN/A    }
6922238SN/A
6935282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6945282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6952238SN/A
6965282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6972238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6982238SN/A    // the return value of the function, and fd[1] is returned in r20.
6992680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7002238SN/A    return sim_fds[0];
7012238SN/A}
7022238SN/A
7032238SN/A
7042238SN/ASyscallReturn
7053114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7062680Sktlim@umich.edu           ThreadContext *tc)
7072238SN/A{
7082238SN/A    // Make up a PID.  There's no interprocess communication in
7092238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7102238SN/A    // not getting a unique value.
7112238SN/A
7123114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7133114Sgblack@eecs.umich.edu    return process->pid();
7142238SN/A}
7152238SN/A
7162238SN/A
7172238SN/ASyscallReturn
7183114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7192680Sktlim@umich.edu           ThreadContext *tc)
7202238SN/A{
7212238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7222238SN/A    // simulation to be deterministic.
7232238SN/A
7242238SN/A    // EUID goes in r20.
7253114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7265543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7272238SN/A}
7282238SN/A
7292238SN/A
7302238SN/ASyscallReturn
7313114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7322680Sktlim@umich.edu           ThreadContext *tc)
7332238SN/A{
7342238SN/A    // Get current group ID.  EGID goes in r20.
7353114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7363114Sgblack@eecs.umich.edu    return process->gid();
7372238SN/A}
7382238SN/A
7392238SN/A
7402238SN/ASyscallReturn
7413114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7422680Sktlim@umich.edu           ThreadContext *tc)
7432238SN/A{
7442238SN/A    // can't fathom why a benchmark would call this.
7456701Sgblack@eecs.umich.edu    int index = 0;
7466701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7472238SN/A    return 0;
7482238SN/A}
7492238SN/A
7502238SN/ASyscallReturn
7513114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7522680Sktlim@umich.edu           ThreadContext *tc)
7532238SN/A{
7542238SN/A    // Make up a PID.  There's no interprocess communication in
7552238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7562238SN/A    // not getting a unique value.
7572238SN/A
7583114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7593114Sgblack@eecs.umich.edu    return process->pid();
7602238SN/A}
7612238SN/A
7622238SN/ASyscallReturn
7633114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7642680Sktlim@umich.edu           ThreadContext *tc)
7652238SN/A{
7663114Sgblack@eecs.umich.edu    return process->ppid();
7672238SN/A}
7682238SN/A
7692238SN/ASyscallReturn
7703114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7712680Sktlim@umich.edu           ThreadContext *tc)
7722238SN/A{
7735543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7742238SN/A}
7752238SN/A
7762238SN/ASyscallReturn
7773114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7782680Sktlim@umich.edu           ThreadContext *tc)
7792238SN/A{
7805543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7812238SN/A}
7822238SN/A
7832238SN/ASyscallReturn
7843114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7852680Sktlim@umich.edu           ThreadContext *tc)
7862238SN/A{
7873114Sgblack@eecs.umich.edu    return process->gid();
7882238SN/A}
7892238SN/A
7902238SN/ASyscallReturn
7913114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7922680Sktlim@umich.edu           ThreadContext *tc)
7932238SN/A{
7943114Sgblack@eecs.umich.edu    return process->egid();
7952238SN/A}
7962238SN/A
7972238SN/A
7986109Ssanchezd@stanford.eduSyscallReturn
7996109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8006109Ssanchezd@stanford.edu           ThreadContext *tc)
8016109Ssanchezd@stanford.edu{
8026701Sgblack@eecs.umich.edu    int index = 0;
8036701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8046701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8056701Sgblack@eecs.umich.edu
8066109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8076701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8086701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8096109Ssanchezd@stanford.edu
8106109Ssanchezd@stanford.edu
8116701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8126111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8136111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8146111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8156701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8166109Ssanchezd@stanford.edu    }
8176109Ssanchezd@stanford.edu
8186111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8196109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8206109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8216109Ssanchezd@stanford.edu
8226109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8236109Ssanchezd@stanford.edu
8246111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8256109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8266111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8276109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8286109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8296109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8306109Ssanchezd@stanford.edu
8316111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8326109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8336109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8346109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8356109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8366337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8376109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8386109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8399375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8406109Ssanchezd@stanford.edu
8416109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8426109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8438149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8448149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8456109Ssanchezd@stanford.edu        #else
8466109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8476109Ssanchezd@stanford.edu        #endif
8486109Ssanchezd@stanford.edu
8496111Ssteve.reinhardt@amd.com        // Set up stack register
8506701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8516109Ssanchezd@stanford.edu
8526111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8536111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8546109Ssanchezd@stanford.edu
8556111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8566109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8576110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8586109Ssanchezd@stanford.edu        #endif
8596109Ssanchezd@stanford.edu
8606111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8616111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8626109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8636109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8646109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8656109Ssanchezd@stanford.edu        #endif
8666109Ssanchezd@stanford.edu
8677720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8686109Ssanchezd@stanford.edu
8696109Ssanchezd@stanford.edu        ctc->activate();
8706109Ssanchezd@stanford.edu
8716109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8726109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8736109Ssanchezd@stanford.edu        return 1;
8746109Ssanchezd@stanford.edu    } else {
8756109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8766109Ssanchezd@stanford.edu        return 0;
8776109Ssanchezd@stanford.edu    }
8786109Ssanchezd@stanford.edu}
8796109Ssanchezd@stanford.edu
8809455Smitch.hayenga+gem5@gmail.comSyscallReturn
88110203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
88210203SAli.Saidi@ARM.com        int index)
8839455Smitch.hayenga+gem5@gmail.com{
8849455Smitch.hayenga+gem5@gmail.com    string path;
8859455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
88610223Ssteve.reinhardt@amd.com        return -EFAULT;
8879455Smitch.hayenga+gem5@gmail.com
8889455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
8899455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
8909455Smitch.hayenga+gem5@gmail.com
8919455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
8929455Smitch.hayenga+gem5@gmail.com
8939455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
8949455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
8959455Smitch.hayenga+gem5@gmail.com}
89610203SAli.Saidi@ARM.com
89710203SAli.Saidi@ARM.comSyscallReturn
89810203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
89910203SAli.Saidi@ARM.com{
90010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
90110203SAli.Saidi@ARM.com}
90210203SAli.Saidi@ARM.com
903