syscall_emul.cc revision 10955
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;
21310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
21410931Sbrandon.potter@amd.com
21510932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
21610931Sbrandon.potter@amd.com    if (sim_fd < 0)
21710931Sbrandon.potter@amd.com        return -EBADF;
21810931Sbrandon.potter@amd.com
2197508Stjones1@inf.ed.ac.uk    int status = 0;
2207508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2217508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2221970SN/A    if (status >= 0)
22310932Sbrandon.potter@amd.com        p->resetFDEntry(tgt_fd);
2241970SN/A    return status;
225360SN/A}
226360SN/A
227360SN/A
2281450SN/ASyscallReturn
2293114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
230360SN/A{
2316701Sgblack@eecs.umich.edu    int index = 0;
23210931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2336701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2346701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2356701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
236360SN/A
23710932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
23810931Sbrandon.potter@amd.com    if (sim_fd < 0)
23910931Sbrandon.potter@amd.com        return -EBADF;
24010931Sbrandon.potter@amd.com
24110931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
242360SN/A
243360SN/A    if (bytes_read != -1)
2448706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
245360SN/A
2461458SN/A    return bytes_read;
247360SN/A}
248360SN/A
2491450SN/ASyscallReturn
2503114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
251360SN/A{
2526701Sgblack@eecs.umich.edu    int index = 0;
25310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2546701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2556701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2566701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
257360SN/A
25810932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
25910931Sbrandon.potter@amd.com    if (sim_fd < 0)
26010931Sbrandon.potter@amd.com        return -EBADF;
26110931Sbrandon.potter@amd.com
2628706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
263360SN/A
26410931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
265360SN/A
26610931Sbrandon.potter@amd.com    fsync(sim_fd);
267360SN/A
2681458SN/A    return bytes_written;
269360SN/A}
270360SN/A
271360SN/A
2721450SN/ASyscallReturn
2733114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
274360SN/A{
2756701Sgblack@eecs.umich.edu    int index = 0;
27610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2776701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2786701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
279360SN/A
28010932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
28110931Sbrandon.potter@amd.com    if (sim_fd < 0)
28210931Sbrandon.potter@amd.com        return -EBADF;
28310931Sbrandon.potter@amd.com
28410931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
285360SN/A
2861458SN/A    return (result == (off_t)-1) ? -errno : result;
287360SN/A}
288360SN/A
289360SN/A
2901450SN/ASyscallReturn
2914118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2924118Sgblack@eecs.umich.edu{
2936701Sgblack@eecs.umich.edu    int index = 0;
29410931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2956701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2966701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2976701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2986701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2994118Sgblack@eecs.umich.edu
30010932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
30110931Sbrandon.potter@amd.com    if (sim_fd < 0)
30210931Sbrandon.potter@amd.com        return -EBADF;
30310931Sbrandon.potter@amd.com
3044118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
3054118Sgblack@eecs.umich.edu
30610931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
3074118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
3084118Sgblack@eecs.umich.edu
3094118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
3104118Sgblack@eecs.umich.edu        //The seek failed.
3114118Sgblack@eecs.umich.edu        return -errno;
3124118Sgblack@eecs.umich.edu    } else {
3136111Ssteve.reinhardt@amd.com        // The seek succeeded.
3146111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
3156111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
3166111Ssteve.reinhardt@amd.com        // target platform
3174118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
3184118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
3198706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
3204118Sgblack@eecs.umich.edu        return 0;
3214118Sgblack@eecs.umich.edu    }
3224118Sgblack@eecs.umich.edu}
3234118Sgblack@eecs.umich.edu
3244118Sgblack@eecs.umich.edu
3254118Sgblack@eecs.umich.eduSyscallReturn
3263114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
327360SN/A{
328360SN/A    // given that we don't really implement mmap, munmap is really easy
3291458SN/A    return 0;
330360SN/A}
331360SN/A
332360SN/A
333360SN/Aconst char *hostname = "m5.eecs.umich.edu";
334360SN/A
3351450SN/ASyscallReturn
3363114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
337360SN/A{
3386701Sgblack@eecs.umich.edu    int index = 0;
3396701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3406701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3416701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
342360SN/A
343360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
344360SN/A
3458706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
346360SN/A
3471458SN/A    return 0;
348360SN/A}
349360SN/A
3501450SN/ASyscallReturn
3515513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3525513SMichael.Adler@intel.com{
3535513SMichael.Adler@intel.com    int result = 0;
3546731Svince@csl.cornell.edu    int index = 0;
3556701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3566701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3576701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3585513SMichael.Adler@intel.com
3595513SMichael.Adler@intel.com    // Is current working directory defined?
3605513SMichael.Adler@intel.com    string cwd = p->getcwd();
3615513SMichael.Adler@intel.com    if (!cwd.empty()) {
3625513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3635513SMichael.Adler@intel.com            // Buffer too small
3645513SMichael.Adler@intel.com            return -ERANGE;
3655513SMichael.Adler@intel.com        }
3665513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3675513SMichael.Adler@intel.com        result = cwd.length();
36810955Sdavid.hashe@amd.com    } else {
3695513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3705513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
37110955Sdavid.hashe@amd.com        } else {
3725513SMichael.Adler@intel.com            result = -1;
3735513SMichael.Adler@intel.com        }
3745513SMichael.Adler@intel.com    }
3755513SMichael.Adler@intel.com
3768706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3775513SMichael.Adler@intel.com
3785513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3795513SMichael.Adler@intel.com}
3805513SMichael.Adler@intel.com
38110203SAli.Saidi@ARM.com/// Target open() handler.
38210203SAli.Saidi@ARM.comSyscallReturn
38310203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
38410203SAli.Saidi@ARM.com         ThreadContext *tc)
38510203SAli.Saidi@ARM.com{
38610203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
38710203SAli.Saidi@ARM.com}
3885513SMichael.Adler@intel.com
3895513SMichael.Adler@intel.comSyscallReturn
39010203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
39110203SAli.Saidi@ARM.com        int index)
3925513SMichael.Adler@intel.com{
3935513SMichael.Adler@intel.com    string path;
3945513SMichael.Adler@intel.com
3958852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
39610223Ssteve.reinhardt@amd.com        return -EFAULT;
3975513SMichael.Adler@intel.com
3985513SMichael.Adler@intel.com    // Adjust path for current working directory
3995513SMichael.Adler@intel.com    path = p->fullPath(path);
4005513SMichael.Adler@intel.com
4016701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
4026701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
4036701Sgblack@eecs.umich.edu
4046701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
4055513SMichael.Adler@intel.com
40610955Sdavid.hashe@amd.com    int result = -1;
40710955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
40810955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
40910955Sdavid.hashe@amd.com    } else {
41010955Sdavid.hashe@amd.com        // readlink() will return the path of the binary given
41110955Sdavid.hashe@amd.com        // with the -c option, however it is possible that this
41210955Sdavid.hashe@amd.com        // will still result in incorrect behavior if one binary
41310955Sdavid.hashe@amd.com        // runs another, e.g., -c time -o "my_binary" where
41410955Sdavid.hashe@amd.com        // my_binary calls readlink(). this is a very unlikely case,
41510955Sdavid.hashe@amd.com        // so we issue a warning.
41610955Sdavid.hashe@amd.com        warn_once("readlink may yield unexpected results if multiple "
41710955Sdavid.hashe@amd.com                  "binaries are used\n");
41810955Sdavid.hashe@amd.com        if (strlen(p->progName()) > bufsiz) {
41910955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
42010955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
42110955Sdavid.hashe@amd.com            strncpy((char*)buf.bufferPtr(), p->progName(), bufsiz);
42210955Sdavid.hashe@amd.com            result = bufsiz;
42310955Sdavid.hashe@amd.com        } else {
42410955Sdavid.hashe@amd.com            // return the program's working path rather
42510955Sdavid.hashe@amd.com            // than the one for the gem5 binary itself.
42610955Sdavid.hashe@amd.com            strcpy((char*)buf.bufferPtr(), p->progName());
42710955Sdavid.hashe@amd.com            result = strlen((char*)buf.bufferPtr());
42810955Sdavid.hashe@amd.com        }
42910955Sdavid.hashe@amd.com    }
4305513SMichael.Adler@intel.com
4318706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4325513SMichael.Adler@intel.com
4335513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4345513SMichael.Adler@intel.com}
4355513SMichael.Adler@intel.com
4365513SMichael.Adler@intel.comSyscallReturn
4373114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
438511SN/A{
43910633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
44010633Smichaelupton@gmail.com}
44110633Smichaelupton@gmail.com
44210633Smichaelupton@gmail.comSyscallReturn
44310633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
44410633Smichaelupton@gmail.com           int index)
44510633Smichaelupton@gmail.com{
4461706SN/A    string path;
447360SN/A
4488852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
44910223Ssteve.reinhardt@amd.com        return -EFAULT;
450511SN/A
4513669Sbinkertn@umich.edu    // Adjust path for current working directory
4523669Sbinkertn@umich.edu    path = p->fullPath(path);
4533669Sbinkertn@umich.edu
454511SN/A    int result = unlink(path.c_str());
4551458SN/A    return (result == -1) ? -errno : result;
456511SN/A}
457511SN/A
4585513SMichael.Adler@intel.com
4595513SMichael.Adler@intel.comSyscallReturn
4605513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4615513SMichael.Adler@intel.com{
4625513SMichael.Adler@intel.com    string path;
4635513SMichael.Adler@intel.com
4646701Sgblack@eecs.umich.edu    int index = 0;
4658852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46610223Ssteve.reinhardt@amd.com        return -EFAULT;
4675513SMichael.Adler@intel.com
4685513SMichael.Adler@intel.com    // Adjust path for current working directory
4695513SMichael.Adler@intel.com    path = p->fullPath(path);
4705513SMichael.Adler@intel.com
4716701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4725513SMichael.Adler@intel.com
4735513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4745513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4755513SMichael.Adler@intel.com}
4765513SMichael.Adler@intel.com
4771450SN/ASyscallReturn
4783114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
479511SN/A{
4801706SN/A    string old_name;
481511SN/A
4826701Sgblack@eecs.umich.edu    int index = 0;
4838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4841458SN/A        return -EFAULT;
485511SN/A
4861706SN/A    string new_name;
487511SN/A
4888852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4891458SN/A        return -EFAULT;
490511SN/A
4913669Sbinkertn@umich.edu    // Adjust path for current working directory
4923669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4933669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4943669Sbinkertn@umich.edu
4951706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4961458SN/A    return (result == -1) ? -errno : result;
497511SN/A}
498511SN/A
4991706SN/ASyscallReturn
5003114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5011706SN/A{
5021706SN/A    string path;
5031706SN/A
5046701Sgblack@eecs.umich.edu    int index = 0;
5058852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5061706SN/A        return -EFAULT;
5071706SN/A
5086701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5091706SN/A
5103669Sbinkertn@umich.edu    // Adjust path for current working directory
5113669Sbinkertn@umich.edu    path = p->fullPath(path);
5123669Sbinkertn@umich.edu
5131706SN/A    int result = truncate(path.c_str(), length);
5141706SN/A    return (result == -1) ? -errno : result;
5151706SN/A}
5161706SN/A
5171706SN/ASyscallReturn
5186111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
5196111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
5201706SN/A{
5216701Sgblack@eecs.umich.edu    int index = 0;
52210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
52310931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
5241706SN/A
52510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
52610931Sbrandon.potter@amd.com    if (sim_fd < 0)
5271706SN/A        return -EBADF;
5281706SN/A
52910931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5301706SN/A    return (result == -1) ? -errno : result;
5311706SN/A}
5321999SN/A
5331999SN/ASyscallReturn
5346703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5356703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5366703Svince@csl.cornell.edu{
5376703Svince@csl.cornell.edu    int index = 0;
5386703Svince@csl.cornell.edu    string path;
5396703Svince@csl.cornell.edu
5408852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5416703Svince@csl.cornell.edu       return -EFAULT;
5426703Svince@csl.cornell.edu
5436744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5446703Svince@csl.cornell.edu
5456703Svince@csl.cornell.edu    // Adjust path for current working directory
5466703Svince@csl.cornell.edu    path = process->fullPath(path);
5476703Svince@csl.cornell.edu
5486744SAli.Saidi@arm.com#if NO_STAT64
5496744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5506744SAli.Saidi@arm.com#else
5516703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5526744SAli.Saidi@arm.com#endif
5536703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5546703Svince@csl.cornell.edu}
5556703Svince@csl.cornell.edu
5566703Svince@csl.cornell.eduSyscallReturn
5576685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5586685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5596685Stjones1@inf.ed.ac.uk{
5606701Sgblack@eecs.umich.edu    int index = 0;
56110931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
56210931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5636685Stjones1@inf.ed.ac.uk
56410932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
56510931Sbrandon.potter@amd.com    if (sim_fd < 0)
5666685Stjones1@inf.ed.ac.uk        return -EBADF;
5676685Stjones1@inf.ed.ac.uk
5686744SAli.Saidi@arm.com#if NO_STAT64
56910931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5706744SAli.Saidi@arm.com#else
57110931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5726744SAli.Saidi@arm.com#endif
5736685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5746685Stjones1@inf.ed.ac.uk}
5756685Stjones1@inf.ed.ac.uk
5766685Stjones1@inf.ed.ac.ukSyscallReturn
5775513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5785513SMichael.Adler@intel.com{
5795513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5805513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5815513SMichael.Adler@intel.com    // changing anything.
5825513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5835513SMichael.Adler@intel.com    umask(oldMask);
5845521Snate@binkert.org    return (int)oldMask;
5855513SMichael.Adler@intel.com}
5865513SMichael.Adler@intel.com
5875513SMichael.Adler@intel.comSyscallReturn
5883114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5891999SN/A{
5901999SN/A    string path;
5911999SN/A
5926701Sgblack@eecs.umich.edu    int index = 0;
5938852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5941999SN/A        return -EFAULT;
5951999SN/A
5961999SN/A    /* XXX endianess */
5976701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5981999SN/A    uid_t hostOwner = owner;
5996701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6001999SN/A    gid_t hostGroup = group;
6011999SN/A
6023669Sbinkertn@umich.edu    // Adjust path for current working directory
6033669Sbinkertn@umich.edu    path = p->fullPath(path);
6043669Sbinkertn@umich.edu
6051999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6061999SN/A    return (result == -1) ? -errno : result;
6071999SN/A}
6081999SN/A
6091999SN/ASyscallReturn
6103114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6111999SN/A{
6126701Sgblack@eecs.umich.edu    int index = 0;
61310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6141999SN/A
61510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
61610931Sbrandon.potter@amd.com    if (sim_fd < 0)
6171999SN/A        return -EBADF;
6181999SN/A
6191999SN/A    /* XXX endianess */
6206701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
6211999SN/A    uid_t hostOwner = owner;
6226701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
6231999SN/A    gid_t hostGroup = group;
6241999SN/A
62510931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6261999SN/A    return (result == -1) ? -errno : result;
6271999SN/A}
6282093SN/A
6292093SN/A
6302093SN/ASyscallReturn
6313114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6323079Sstever@eecs.umich.edu{
6336701Sgblack@eecs.umich.edu    int index = 0;
63410781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
63510931Sbrandon.potter@amd.com
63610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
63710781Snilay@cs.wisc.edu    if (sim_fd < 0)
6383079Sstever@eecs.umich.edu        return -EBADF;
6393079Sstever@eecs.umich.edu
64010932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6415282Srstrong@cs.ucsd.edu
64210781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6436111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
64410932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6453079Sstever@eecs.umich.edu}
6463079Sstever@eecs.umich.edu
6473079Sstever@eecs.umich.edu
6483079Sstever@eecs.umich.eduSyscallReturn
6493114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6502680Sktlim@umich.edu          ThreadContext *tc)
6512093SN/A{
6526701Sgblack@eecs.umich.edu    int index = 0;
65310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6542093SN/A
65510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
65610931Sbrandon.potter@amd.com    if (sim_fd < 0)
6572093SN/A        return -EBADF;
6582093SN/A
6596701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6602093SN/A    switch (cmd) {
6612093SN/A      case 0: // F_DUPFD
6622093SN/A        // if we really wanted to support this, we'd need to do it
6632093SN/A        // in the target fd space.
66410931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6652093SN/A        return -EMFILE;
6662093SN/A
6672093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6682093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6692093SN/A        return 0;
6702093SN/A
6712093SN/A      case 3: // F_GETFL (get file flags)
6722093SN/A      case 4: // F_SETFL (set file flags)
6732093SN/A        // not sure if this is totally valid, but we'll pass it through
6742093SN/A        // to the underlying OS
67510931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
67610931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6772093SN/A        // return 0;
6782093SN/A
6792093SN/A      case 7: // F_GETLK  (get lock)
6802093SN/A      case 8: // F_SETLK  (set lock)
6812093SN/A      case 9: // F_SETLKW (set lock and wait)
6822093SN/A        // don't mess with file locking... just act like it's OK
68310931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6842093SN/A        return 0;
6852093SN/A
6862093SN/A      default:
6872093SN/A        warn("Unknown fcntl command %d\n", cmd);
6882093SN/A        return 0;
6892093SN/A    }
6902093SN/A}
6912093SN/A
6922238SN/ASyscallReturn
6933114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6942687Sksewell@umich.edu            ThreadContext *tc)
6952687Sksewell@umich.edu{
6966701Sgblack@eecs.umich.edu    int index = 0;
69710931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6982687Sksewell@umich.edu
69910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
70010931Sbrandon.potter@amd.com    if (sim_fd < 0)
7012687Sksewell@umich.edu        return -EBADF;
7022687Sksewell@umich.edu
7036701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
7042687Sksewell@umich.edu    switch (cmd) {
7052687Sksewell@umich.edu      case 33: //F_GETLK64
70610931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7072687Sksewell@umich.edu        return -EMFILE;
7082687Sksewell@umich.edu
7092687Sksewell@umich.edu      case 34: // F_SETLK64
7102687Sksewell@umich.edu      case 35: // F_SETLKW64
71110931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
71210931Sbrandon.potter@amd.com             tgt_fd);
7132687Sksewell@umich.edu        return -EMFILE;
7142687Sksewell@umich.edu
7152687Sksewell@umich.edu      default:
7162687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7172687Sksewell@umich.edu        // to the underlying OS
71810931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
71910931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7202687Sksewell@umich.edu        // return 0;
7212687Sksewell@umich.edu    }
7222687Sksewell@umich.edu}
7232687Sksewell@umich.edu
7242687Sksewell@umich.eduSyscallReturn
7253114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7262680Sktlim@umich.edu         ThreadContext *tc)
7272238SN/A{
7282238SN/A    int fds[2], sim_fds[2];
7292238SN/A    int pipe_retval = pipe(fds);
7302093SN/A
7312238SN/A    if (pipe_retval < 0) {
7322238SN/A        // error
7332238SN/A        return pipe_retval;
7342238SN/A    }
7352238SN/A
73610932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
73710932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7382238SN/A
7395282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7402238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7412238SN/A    // the return value of the function, and fd[1] is returned in r20.
7422680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7432238SN/A    return sim_fds[0];
7442238SN/A}
7452238SN/A
7462238SN/A
7472238SN/ASyscallReturn
7483114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7492680Sktlim@umich.edu           ThreadContext *tc)
7502238SN/A{
7512238SN/A    // Make up a PID.  There's no interprocess communication in
7522238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7532238SN/A    // not getting a unique value.
7542238SN/A
7553114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7563114Sgblack@eecs.umich.edu    return process->pid();
7572238SN/A}
7582238SN/A
7592238SN/A
7602238SN/ASyscallReturn
7613114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7622680Sktlim@umich.edu           ThreadContext *tc)
7632238SN/A{
7642238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7652238SN/A    // simulation to be deterministic.
7662238SN/A
7672238SN/A    // EUID goes in r20.
7683114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7695543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7702238SN/A}
7712238SN/A
7722238SN/A
7732238SN/ASyscallReturn
7743114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7752680Sktlim@umich.edu           ThreadContext *tc)
7762238SN/A{
7772238SN/A    // Get current group ID.  EGID goes in r20.
7783114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7793114Sgblack@eecs.umich.edu    return process->gid();
7802238SN/A}
7812238SN/A
7822238SN/A
7832238SN/ASyscallReturn
7843114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7852680Sktlim@umich.edu           ThreadContext *tc)
7862238SN/A{
7872238SN/A    // can't fathom why a benchmark would call this.
7886701Sgblack@eecs.umich.edu    int index = 0;
7896701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7902238SN/A    return 0;
7912238SN/A}
7922238SN/A
7932238SN/ASyscallReturn
7943114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7952680Sktlim@umich.edu           ThreadContext *tc)
7962238SN/A{
7972238SN/A    // Make up a PID.  There's no interprocess communication in
7982238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7992238SN/A    // not getting a unique value.
8002238SN/A
8013114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
8023114Sgblack@eecs.umich.edu    return process->pid();
8032238SN/A}
8042238SN/A
8052238SN/ASyscallReturn
8063114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8072680Sktlim@umich.edu           ThreadContext *tc)
8082238SN/A{
8093114Sgblack@eecs.umich.edu    return process->ppid();
8102238SN/A}
8112238SN/A
8122238SN/ASyscallReturn
8133114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8142680Sktlim@umich.edu           ThreadContext *tc)
8152238SN/A{
8165543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8172238SN/A}
8182238SN/A
8192238SN/ASyscallReturn
8203114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8212680Sktlim@umich.edu           ThreadContext *tc)
8222238SN/A{
8235543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8242238SN/A}
8252238SN/A
8262238SN/ASyscallReturn
8273114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8282680Sktlim@umich.edu           ThreadContext *tc)
8292238SN/A{
8303114Sgblack@eecs.umich.edu    return process->gid();
8312238SN/A}
8322238SN/A
8332238SN/ASyscallReturn
8343114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8352680Sktlim@umich.edu           ThreadContext *tc)
8362238SN/A{
8373114Sgblack@eecs.umich.edu    return process->egid();
8382238SN/A}
8392238SN/A
8402238SN/A
8416109Ssanchezd@stanford.eduSyscallReturn
8426109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8436109Ssanchezd@stanford.edu           ThreadContext *tc)
8446109Ssanchezd@stanford.edu{
8456701Sgblack@eecs.umich.edu    int index = 0;
8466701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8476701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8486701Sgblack@eecs.umich.edu
8496109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8506701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8516701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8526109Ssanchezd@stanford.edu
8536109Ssanchezd@stanford.edu
8546701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8556111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8566111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8576111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8586701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8596109Ssanchezd@stanford.edu    }
8606109Ssanchezd@stanford.edu
8616111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8626109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8636109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8646109Ssanchezd@stanford.edu
8656109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8666109Ssanchezd@stanford.edu
8676111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8686109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8696111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8706109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8716109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8726109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8736109Ssanchezd@stanford.edu
8746111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8756109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8766109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8776109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8786109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8796337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8806109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8816109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8829375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8836109Ssanchezd@stanford.edu
8846109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8856109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8868149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8878149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8886109Ssanchezd@stanford.edu        #else
8896109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8906109Ssanchezd@stanford.edu        #endif
8916109Ssanchezd@stanford.edu
8926111Ssteve.reinhardt@amd.com        // Set up stack register
8936701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8946109Ssanchezd@stanford.edu
8956111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8966111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8976109Ssanchezd@stanford.edu
8986111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8996109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
9006110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
9016109Ssanchezd@stanford.edu        #endif
9026109Ssanchezd@stanford.edu
9036111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
9046111Ssteve.reinhardt@amd.com        // parent, non-zero if child
9056109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
9066109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
9076109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
9086109Ssanchezd@stanford.edu        #endif
9096109Ssanchezd@stanford.edu
9107720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
9116109Ssanchezd@stanford.edu
9126109Ssanchezd@stanford.edu        ctc->activate();
9136109Ssanchezd@stanford.edu
9146109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9156109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9166109Ssanchezd@stanford.edu        return 1;
9176109Ssanchezd@stanford.edu    } else {
9186109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9196109Ssanchezd@stanford.edu        return 0;
9206109Ssanchezd@stanford.edu    }
9216109Ssanchezd@stanford.edu}
9226109Ssanchezd@stanford.edu
9239455Smitch.hayenga+gem5@gmail.comSyscallReturn
92410203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
92510203SAli.Saidi@ARM.com        int index)
9269455Smitch.hayenga+gem5@gmail.com{
9279455Smitch.hayenga+gem5@gmail.com    string path;
9289455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
92910223Ssteve.reinhardt@amd.com        return -EFAULT;
9309455Smitch.hayenga+gem5@gmail.com
9319455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9329455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9339455Smitch.hayenga+gem5@gmail.com
9349455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9359455Smitch.hayenga+gem5@gmail.com
9369455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9379455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9389455Smitch.hayenga+gem5@gmail.com}
93910203SAli.Saidi@ARM.com
94010203SAli.Saidi@ARM.comSyscallReturn
94110203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
94210203SAli.Saidi@ARM.com{
94310203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
94410203SAli.Saidi@ARM.com}
94510203SAli.Saidi@ARM.com
946