syscall_emul.cc revision 11379
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
30911379Sbrandon.potter@amd.com    if (result == (off_t)-1)
3104118Sgblack@eecs.umich.edu        return -errno;
31111379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
31211379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
31311379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
31411379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
31511379Sbrandon.potter@amd.com    return 0;
3164118Sgblack@eecs.umich.edu}
3174118Sgblack@eecs.umich.edu
3184118Sgblack@eecs.umich.edu
3194118Sgblack@eecs.umich.eduSyscallReturn
3203114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
321360SN/A{
322360SN/A    // given that we don't really implement mmap, munmap is really easy
3231458SN/A    return 0;
324360SN/A}
325360SN/A
326360SN/A
327360SN/Aconst char *hostname = "m5.eecs.umich.edu";
328360SN/A
3291450SN/ASyscallReturn
3303114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
331360SN/A{
3326701Sgblack@eecs.umich.edu    int index = 0;
3336701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3346701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3356701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
336360SN/A
337360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
338360SN/A
3398706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
340360SN/A
3411458SN/A    return 0;
342360SN/A}
343360SN/A
3441450SN/ASyscallReturn
3455513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3465513SMichael.Adler@intel.com{
3475513SMichael.Adler@intel.com    int result = 0;
3486731Svince@csl.cornell.edu    int index = 0;
3496701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3506701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3516701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3525513SMichael.Adler@intel.com
3535513SMichael.Adler@intel.com    // Is current working directory defined?
3545513SMichael.Adler@intel.com    string cwd = p->getcwd();
3555513SMichael.Adler@intel.com    if (!cwd.empty()) {
3565513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3575513SMichael.Adler@intel.com            // Buffer too small
3585513SMichael.Adler@intel.com            return -ERANGE;
3595513SMichael.Adler@intel.com        }
3605513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3615513SMichael.Adler@intel.com        result = cwd.length();
36210955Sdavid.hashe@amd.com    } else {
3635513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3645513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
36510955Sdavid.hashe@amd.com        } else {
3665513SMichael.Adler@intel.com            result = -1;
3675513SMichael.Adler@intel.com        }
3685513SMichael.Adler@intel.com    }
3695513SMichael.Adler@intel.com
3708706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3715513SMichael.Adler@intel.com
3725513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3735513SMichael.Adler@intel.com}
3745513SMichael.Adler@intel.com
37510203SAli.Saidi@ARM.com/// Target open() handler.
37610203SAli.Saidi@ARM.comSyscallReturn
37710203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
37810203SAli.Saidi@ARM.com         ThreadContext *tc)
37910203SAli.Saidi@ARM.com{
38010203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
38110203SAli.Saidi@ARM.com}
3825513SMichael.Adler@intel.com
3835513SMichael.Adler@intel.comSyscallReturn
38410203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
38510203SAli.Saidi@ARM.com        int index)
3865513SMichael.Adler@intel.com{
3875513SMichael.Adler@intel.com    string path;
3885513SMichael.Adler@intel.com
3898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
39010223Ssteve.reinhardt@amd.com        return -EFAULT;
3915513SMichael.Adler@intel.com
3925513SMichael.Adler@intel.com    // Adjust path for current working directory
3935513SMichael.Adler@intel.com    path = p->fullPath(path);
3945513SMichael.Adler@intel.com
3956701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3966701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3976701Sgblack@eecs.umich.edu
3986701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3995513SMichael.Adler@intel.com
40010955Sdavid.hashe@amd.com    int result = -1;
40110955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
40210955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
40310955Sdavid.hashe@amd.com    } else {
40411140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
40511140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
40611140Sjthestness@gmail.com        // LiveProcess' executable). It is possible that using this path in
40711140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
40811140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
40911140Sjthestness@gmail.com        //     called binary calls readlink().
41011140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
41111140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
41211140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
41311140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
41411140Sjthestness@gmail.com
41511140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
41611140Sjthestness@gmail.com        char real_path[PATH_MAX];
41711140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
41811140Sjthestness@gmail.com        if (!check_real_path) {
41911140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
42011140Sjthestness@gmail.com                  "executable: %s", p->progName());
42111140Sjthestness@gmail.com        }
42211140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
42311140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
42411140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
42510955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
42610955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
42710955Sdavid.hashe@amd.com            result = bufsiz;
42810955Sdavid.hashe@amd.com        } else {
42911140Sjthestness@gmail.com            result = real_path_len;
43010955Sdavid.hashe@amd.com        }
43111140Sjthestness@gmail.com
43211140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
43311140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
43411140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
43511140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
43610955Sdavid.hashe@amd.com    }
4375513SMichael.Adler@intel.com
4388706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4395513SMichael.Adler@intel.com
4405513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4415513SMichael.Adler@intel.com}
4425513SMichael.Adler@intel.com
4435513SMichael.Adler@intel.comSyscallReturn
4443114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
445511SN/A{
44610633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
44710633Smichaelupton@gmail.com}
44810633Smichaelupton@gmail.com
44910633Smichaelupton@gmail.comSyscallReturn
45010633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
45110633Smichaelupton@gmail.com           int index)
45210633Smichaelupton@gmail.com{
4531706SN/A    string path;
454360SN/A
4558852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
45610223Ssteve.reinhardt@amd.com        return -EFAULT;
457511SN/A
4583669Sbinkertn@umich.edu    // Adjust path for current working directory
4593669Sbinkertn@umich.edu    path = p->fullPath(path);
4603669Sbinkertn@umich.edu
461511SN/A    int result = unlink(path.c_str());
4621458SN/A    return (result == -1) ? -errno : result;
463511SN/A}
464511SN/A
4655513SMichael.Adler@intel.com
4665513SMichael.Adler@intel.comSyscallReturn
4675513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4685513SMichael.Adler@intel.com{
4695513SMichael.Adler@intel.com    string path;
4705513SMichael.Adler@intel.com
4716701Sgblack@eecs.umich.edu    int index = 0;
4728852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
47310223Ssteve.reinhardt@amd.com        return -EFAULT;
4745513SMichael.Adler@intel.com
4755513SMichael.Adler@intel.com    // Adjust path for current working directory
4765513SMichael.Adler@intel.com    path = p->fullPath(path);
4775513SMichael.Adler@intel.com
4786701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4795513SMichael.Adler@intel.com
4805513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4815513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4825513SMichael.Adler@intel.com}
4835513SMichael.Adler@intel.com
4841450SN/ASyscallReturn
4853114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
486511SN/A{
4871706SN/A    string old_name;
488511SN/A
4896701Sgblack@eecs.umich.edu    int index = 0;
4908852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4911458SN/A        return -EFAULT;
492511SN/A
4931706SN/A    string new_name;
494511SN/A
4958852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4961458SN/A        return -EFAULT;
497511SN/A
4983669Sbinkertn@umich.edu    // Adjust path for current working directory
4993669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
5003669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
5013669Sbinkertn@umich.edu
5021706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
5031458SN/A    return (result == -1) ? -errno : result;
504511SN/A}
505511SN/A
5061706SN/ASyscallReturn
5073114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5081706SN/A{
5091706SN/A    string path;
5101706SN/A
5116701Sgblack@eecs.umich.edu    int index = 0;
5128852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5131706SN/A        return -EFAULT;
5141706SN/A
5156701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5161706SN/A
5173669Sbinkertn@umich.edu    // Adjust path for current working directory
5183669Sbinkertn@umich.edu    path = p->fullPath(path);
5193669Sbinkertn@umich.edu
5201706SN/A    int result = truncate(path.c_str(), length);
5211706SN/A    return (result == -1) ? -errno : result;
5221706SN/A}
5231706SN/A
5241706SN/ASyscallReturn
5256111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
5266111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
5271706SN/A{
5286701Sgblack@eecs.umich.edu    int index = 0;
52910931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
53010931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
5311706SN/A
53210932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
53310931Sbrandon.potter@amd.com    if (sim_fd < 0)
5341706SN/A        return -EBADF;
5351706SN/A
53610931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5371706SN/A    return (result == -1) ? -errno : result;
5381706SN/A}
5391999SN/A
5401999SN/ASyscallReturn
5416703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5426703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5436703Svince@csl.cornell.edu{
5446703Svince@csl.cornell.edu    int index = 0;
5456703Svince@csl.cornell.edu    string path;
5466703Svince@csl.cornell.edu
5478852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5486703Svince@csl.cornell.edu       return -EFAULT;
5496703Svince@csl.cornell.edu
5506744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5516703Svince@csl.cornell.edu
5526703Svince@csl.cornell.edu    // Adjust path for current working directory
5536703Svince@csl.cornell.edu    path = process->fullPath(path);
5546703Svince@csl.cornell.edu
5556744SAli.Saidi@arm.com#if NO_STAT64
5566744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5576744SAli.Saidi@arm.com#else
5586703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5596744SAli.Saidi@arm.com#endif
5606703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5616703Svince@csl.cornell.edu}
5626703Svince@csl.cornell.edu
5636703Svince@csl.cornell.eduSyscallReturn
5646685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5656685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5666685Stjones1@inf.ed.ac.uk{
5676701Sgblack@eecs.umich.edu    int index = 0;
56810931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
56910931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5706685Stjones1@inf.ed.ac.uk
57110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
57210931Sbrandon.potter@amd.com    if (sim_fd < 0)
5736685Stjones1@inf.ed.ac.uk        return -EBADF;
5746685Stjones1@inf.ed.ac.uk
5756744SAli.Saidi@arm.com#if NO_STAT64
57610931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5776744SAli.Saidi@arm.com#else
57810931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5796744SAli.Saidi@arm.com#endif
5806685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5816685Stjones1@inf.ed.ac.uk}
5826685Stjones1@inf.ed.ac.uk
5836685Stjones1@inf.ed.ac.ukSyscallReturn
5845513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5855513SMichael.Adler@intel.com{
5865513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5875513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5885513SMichael.Adler@intel.com    // changing anything.
5895513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5905513SMichael.Adler@intel.com    umask(oldMask);
5915521Snate@binkert.org    return (int)oldMask;
5925513SMichael.Adler@intel.com}
5935513SMichael.Adler@intel.com
5945513SMichael.Adler@intel.comSyscallReturn
5953114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5961999SN/A{
5971999SN/A    string path;
5981999SN/A
5996701Sgblack@eecs.umich.edu    int index = 0;
6008852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6011999SN/A        return -EFAULT;
6021999SN/A
6031999SN/A    /* XXX endianess */
6046701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
6051999SN/A    uid_t hostOwner = owner;
6066701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6071999SN/A    gid_t hostGroup = group;
6081999SN/A
6093669Sbinkertn@umich.edu    // Adjust path for current working directory
6103669Sbinkertn@umich.edu    path = p->fullPath(path);
6113669Sbinkertn@umich.edu
6121999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6131999SN/A    return (result == -1) ? -errno : result;
6141999SN/A}
6151999SN/A
6161999SN/ASyscallReturn
6173114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6181999SN/A{
6196701Sgblack@eecs.umich.edu    int index = 0;
62010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6211999SN/A
62210932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
62310931Sbrandon.potter@amd.com    if (sim_fd < 0)
6241999SN/A        return -EBADF;
6251999SN/A
6261999SN/A    /* XXX endianess */
6276701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
6281999SN/A    uid_t hostOwner = owner;
6296701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
6301999SN/A    gid_t hostGroup = group;
6311999SN/A
63210931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6331999SN/A    return (result == -1) ? -errno : result;
6341999SN/A}
6352093SN/A
6362093SN/A
6372093SN/ASyscallReturn
6383114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6393079Sstever@eecs.umich.edu{
6406701Sgblack@eecs.umich.edu    int index = 0;
64110781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
64210931Sbrandon.potter@amd.com
64310932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
64410781Snilay@cs.wisc.edu    if (sim_fd < 0)
6453079Sstever@eecs.umich.edu        return -EBADF;
6463079Sstever@eecs.umich.edu
64710932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6485282Srstrong@cs.ucsd.edu
64910781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6506111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
65110932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6523079Sstever@eecs.umich.edu}
6533079Sstever@eecs.umich.edu
6543079Sstever@eecs.umich.edu
6553079Sstever@eecs.umich.eduSyscallReturn
6563114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6572680Sktlim@umich.edu          ThreadContext *tc)
6582093SN/A{
6596701Sgblack@eecs.umich.edu    int index = 0;
66010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6612093SN/A
66210932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
66310931Sbrandon.potter@amd.com    if (sim_fd < 0)
6642093SN/A        return -EBADF;
6652093SN/A
6666701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6672093SN/A    switch (cmd) {
6682093SN/A      case 0: // F_DUPFD
6692093SN/A        // if we really wanted to support this, we'd need to do it
6702093SN/A        // in the target fd space.
67110931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6722093SN/A        return -EMFILE;
6732093SN/A
6742093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6752093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6762093SN/A        return 0;
6772093SN/A
6782093SN/A      case 3: // F_GETFL (get file flags)
6792093SN/A      case 4: // F_SETFL (set file flags)
6802093SN/A        // not sure if this is totally valid, but we'll pass it through
6812093SN/A        // to the underlying OS
68210931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
68310931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6842093SN/A        // return 0;
6852093SN/A
6862093SN/A      case 7: // F_GETLK  (get lock)
6872093SN/A      case 8: // F_SETLK  (set lock)
6882093SN/A      case 9: // F_SETLKW (set lock and wait)
6892093SN/A        // don't mess with file locking... just act like it's OK
69010931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6912093SN/A        return 0;
6922093SN/A
6932093SN/A      default:
6942093SN/A        warn("Unknown fcntl command %d\n", cmd);
6952093SN/A        return 0;
6962093SN/A    }
6972093SN/A}
6982093SN/A
6992238SN/ASyscallReturn
7003114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
7012687Sksewell@umich.edu            ThreadContext *tc)
7022687Sksewell@umich.edu{
7036701Sgblack@eecs.umich.edu    int index = 0;
70410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
7052687Sksewell@umich.edu
70610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
70710931Sbrandon.potter@amd.com    if (sim_fd < 0)
7082687Sksewell@umich.edu        return -EBADF;
7092687Sksewell@umich.edu
7106701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
7112687Sksewell@umich.edu    switch (cmd) {
7122687Sksewell@umich.edu      case 33: //F_GETLK64
71310931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7142687Sksewell@umich.edu        return -EMFILE;
7152687Sksewell@umich.edu
7162687Sksewell@umich.edu      case 34: // F_SETLK64
7172687Sksewell@umich.edu      case 35: // F_SETLKW64
71810931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
71910931Sbrandon.potter@amd.com             tgt_fd);
7202687Sksewell@umich.edu        return -EMFILE;
7212687Sksewell@umich.edu
7222687Sksewell@umich.edu      default:
7232687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7242687Sksewell@umich.edu        // to the underlying OS
72510931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
72610931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7272687Sksewell@umich.edu        // return 0;
7282687Sksewell@umich.edu    }
7292687Sksewell@umich.edu}
7302687Sksewell@umich.edu
7312687Sksewell@umich.eduSyscallReturn
7323114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7332680Sktlim@umich.edu         ThreadContext *tc)
7342238SN/A{
7352238SN/A    int fds[2], sim_fds[2];
7362238SN/A    int pipe_retval = pipe(fds);
7372093SN/A
7382238SN/A    if (pipe_retval < 0) {
7392238SN/A        // error
7402238SN/A        return pipe_retval;
7412238SN/A    }
7422238SN/A
74310932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
74410932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7452238SN/A
7465282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7472238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7482238SN/A    // the return value of the function, and fd[1] is returned in r20.
7492680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7502238SN/A    return sim_fds[0];
7512238SN/A}
7522238SN/A
7532238SN/A
7542238SN/ASyscallReturn
7553114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7562680Sktlim@umich.edu           ThreadContext *tc)
7572238SN/A{
7582238SN/A    // Make up a PID.  There's no interprocess communication in
7592238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7602238SN/A    // not getting a unique value.
7612238SN/A
7623114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7633114Sgblack@eecs.umich.edu    return process->pid();
7642238SN/A}
7652238SN/A
7662238SN/A
7672238SN/ASyscallReturn
7683114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7692680Sktlim@umich.edu           ThreadContext *tc)
7702238SN/A{
7712238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7722238SN/A    // simulation to be deterministic.
7732238SN/A
7742238SN/A    // EUID goes in r20.
7753114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7765543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7772238SN/A}
7782238SN/A
7792238SN/A
7802238SN/ASyscallReturn
7813114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7822680Sktlim@umich.edu           ThreadContext *tc)
7832238SN/A{
7842238SN/A    // Get current group ID.  EGID goes in r20.
7853114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7863114Sgblack@eecs.umich.edu    return process->gid();
7872238SN/A}
7882238SN/A
7892238SN/A
7902238SN/ASyscallReturn
7913114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7922680Sktlim@umich.edu           ThreadContext *tc)
7932238SN/A{
7942238SN/A    // can't fathom why a benchmark would call this.
7956701Sgblack@eecs.umich.edu    int index = 0;
7966701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7972238SN/A    return 0;
7982238SN/A}
7992238SN/A
8002238SN/ASyscallReturn
8013114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8022680Sktlim@umich.edu           ThreadContext *tc)
8032238SN/A{
8042238SN/A    // Make up a PID.  There's no interprocess communication in
8052238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8062238SN/A    // not getting a unique value.
8072238SN/A
8083114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
8093114Sgblack@eecs.umich.edu    return process->pid();
8102238SN/A}
8112238SN/A
8122238SN/ASyscallReturn
8133114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8142680Sktlim@umich.edu           ThreadContext *tc)
8152238SN/A{
8163114Sgblack@eecs.umich.edu    return process->ppid();
8172238SN/A}
8182238SN/A
8192238SN/ASyscallReturn
8203114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8212680Sktlim@umich.edu           ThreadContext *tc)
8222238SN/A{
8235543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8242238SN/A}
8252238SN/A
8262238SN/ASyscallReturn
8273114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8282680Sktlim@umich.edu           ThreadContext *tc)
8292238SN/A{
8305543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8312238SN/A}
8322238SN/A
8332238SN/ASyscallReturn
8343114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8352680Sktlim@umich.edu           ThreadContext *tc)
8362238SN/A{
8373114Sgblack@eecs.umich.edu    return process->gid();
8382238SN/A}
8392238SN/A
8402238SN/ASyscallReturn
8413114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8422680Sktlim@umich.edu           ThreadContext *tc)
8432238SN/A{
8443114Sgblack@eecs.umich.edu    return process->egid();
8452238SN/A}
8462238SN/A
8472238SN/A
8486109Ssanchezd@stanford.eduSyscallReturn
8496109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8506109Ssanchezd@stanford.edu           ThreadContext *tc)
8516109Ssanchezd@stanford.edu{
8526701Sgblack@eecs.umich.edu    int index = 0;
8536701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8546701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8556701Sgblack@eecs.umich.edu
8566109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8576701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8586701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8596109Ssanchezd@stanford.edu
8606109Ssanchezd@stanford.edu
8616701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8626111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8636111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8646111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8656701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8666109Ssanchezd@stanford.edu    }
8676109Ssanchezd@stanford.edu
8686111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8696109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8706109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8716109Ssanchezd@stanford.edu
8726109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8736109Ssanchezd@stanford.edu
8746111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8756109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8766111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8776109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8786109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8796109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8806109Ssanchezd@stanford.edu
8816111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8826109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8836109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8846109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8856109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8866337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8876109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8886109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8899375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8906109Ssanchezd@stanford.edu
8916109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8926109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8938149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8948149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8956109Ssanchezd@stanford.edu        #else
8966109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8976109Ssanchezd@stanford.edu        #endif
8986109Ssanchezd@stanford.edu
8996111Ssteve.reinhardt@amd.com        // Set up stack register
9006701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
9016109Ssanchezd@stanford.edu
9026111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
9036111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
9046109Ssanchezd@stanford.edu
9056111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
9066109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
9076110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
9086109Ssanchezd@stanford.edu        #endif
9096109Ssanchezd@stanford.edu
9106111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
9116111Ssteve.reinhardt@amd.com        // parent, non-zero if child
9126109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
9136109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
9146109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
9156109Ssanchezd@stanford.edu        #endif
9166109Ssanchezd@stanford.edu
9177720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
9186109Ssanchezd@stanford.edu
9196109Ssanchezd@stanford.edu        ctc->activate();
9206109Ssanchezd@stanford.edu
9216109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9226109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9236109Ssanchezd@stanford.edu        return 1;
9246109Ssanchezd@stanford.edu    } else {
9256109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9266109Ssanchezd@stanford.edu        return 0;
9276109Ssanchezd@stanford.edu    }
9286109Ssanchezd@stanford.edu}
9296109Ssanchezd@stanford.edu
9309455Smitch.hayenga+gem5@gmail.comSyscallReturn
93110203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
93210203SAli.Saidi@ARM.com        int index)
9339455Smitch.hayenga+gem5@gmail.com{
9349455Smitch.hayenga+gem5@gmail.com    string path;
9359455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
93610223Ssteve.reinhardt@amd.com        return -EFAULT;
9379455Smitch.hayenga+gem5@gmail.com
9389455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9399455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9409455Smitch.hayenga+gem5@gmail.com
9419455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9429455Smitch.hayenga+gem5@gmail.com
9439455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9449455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9459455Smitch.hayenga+gem5@gmail.com}
94610203SAli.Saidi@ARM.com
94710203SAli.Saidi@ARM.comSyscallReturn
94810203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
94910203SAli.Saidi@ARM.com{
95010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
95110203SAli.Saidi@ARM.com}
95210203SAli.Saidi@ARM.com
953