syscall_emul.cc revision 11140
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 {
41011140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
41111140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
41211140Sjthestness@gmail.com        // LiveProcess' executable). It is possible that using this path in
41311140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
41411140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
41511140Sjthestness@gmail.com        //     called binary calls readlink().
41611140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
41711140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
41811140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
41911140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
42011140Sjthestness@gmail.com
42111140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
42211140Sjthestness@gmail.com        char real_path[PATH_MAX];
42311140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
42411140Sjthestness@gmail.com        if (!check_real_path) {
42511140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
42611140Sjthestness@gmail.com                  "executable: %s", p->progName());
42711140Sjthestness@gmail.com        }
42811140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
42911140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
43011140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
43110955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
43210955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
43310955Sdavid.hashe@amd.com            result = bufsiz;
43410955Sdavid.hashe@amd.com        } else {
43511140Sjthestness@gmail.com            result = real_path_len;
43610955Sdavid.hashe@amd.com        }
43711140Sjthestness@gmail.com
43811140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
43911140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
44011140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
44111140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
44210955Sdavid.hashe@amd.com    }
4435513SMichael.Adler@intel.com
4448706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4455513SMichael.Adler@intel.com
4465513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4475513SMichael.Adler@intel.com}
4485513SMichael.Adler@intel.com
4495513SMichael.Adler@intel.comSyscallReturn
4503114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
451511SN/A{
45210633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
45310633Smichaelupton@gmail.com}
45410633Smichaelupton@gmail.com
45510633Smichaelupton@gmail.comSyscallReturn
45610633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
45710633Smichaelupton@gmail.com           int index)
45810633Smichaelupton@gmail.com{
4591706SN/A    string path;
460360SN/A
4618852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46210223Ssteve.reinhardt@amd.com        return -EFAULT;
463511SN/A
4643669Sbinkertn@umich.edu    // Adjust path for current working directory
4653669Sbinkertn@umich.edu    path = p->fullPath(path);
4663669Sbinkertn@umich.edu
467511SN/A    int result = unlink(path.c_str());
4681458SN/A    return (result == -1) ? -errno : result;
469511SN/A}
470511SN/A
4715513SMichael.Adler@intel.com
4725513SMichael.Adler@intel.comSyscallReturn
4735513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4745513SMichael.Adler@intel.com{
4755513SMichael.Adler@intel.com    string path;
4765513SMichael.Adler@intel.com
4776701Sgblack@eecs.umich.edu    int index = 0;
4788852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
47910223Ssteve.reinhardt@amd.com        return -EFAULT;
4805513SMichael.Adler@intel.com
4815513SMichael.Adler@intel.com    // Adjust path for current working directory
4825513SMichael.Adler@intel.com    path = p->fullPath(path);
4835513SMichael.Adler@intel.com
4846701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4855513SMichael.Adler@intel.com
4865513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4875513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4885513SMichael.Adler@intel.com}
4895513SMichael.Adler@intel.com
4901450SN/ASyscallReturn
4913114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
492511SN/A{
4931706SN/A    string old_name;
494511SN/A
4956701Sgblack@eecs.umich.edu    int index = 0;
4968852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4971458SN/A        return -EFAULT;
498511SN/A
4991706SN/A    string new_name;
500511SN/A
5018852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
5021458SN/A        return -EFAULT;
503511SN/A
5043669Sbinkertn@umich.edu    // Adjust path for current working directory
5053669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
5063669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
5073669Sbinkertn@umich.edu
5081706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
5091458SN/A    return (result == -1) ? -errno : result;
510511SN/A}
511511SN/A
5121706SN/ASyscallReturn
5133114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5141706SN/A{
5151706SN/A    string path;
5161706SN/A
5176701Sgblack@eecs.umich.edu    int index = 0;
5188852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5191706SN/A        return -EFAULT;
5201706SN/A
5216701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5221706SN/A
5233669Sbinkertn@umich.edu    // Adjust path for current working directory
5243669Sbinkertn@umich.edu    path = p->fullPath(path);
5253669Sbinkertn@umich.edu
5261706SN/A    int result = truncate(path.c_str(), length);
5271706SN/A    return (result == -1) ? -errno : result;
5281706SN/A}
5291706SN/A
5301706SN/ASyscallReturn
5316111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
5326111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
5331706SN/A{
5346701Sgblack@eecs.umich.edu    int index = 0;
53510931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
53610931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
5371706SN/A
53810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
53910931Sbrandon.potter@amd.com    if (sim_fd < 0)
5401706SN/A        return -EBADF;
5411706SN/A
54210931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5431706SN/A    return (result == -1) ? -errno : result;
5441706SN/A}
5451999SN/A
5461999SN/ASyscallReturn
5476703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5486703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5496703Svince@csl.cornell.edu{
5506703Svince@csl.cornell.edu    int index = 0;
5516703Svince@csl.cornell.edu    string path;
5526703Svince@csl.cornell.edu
5538852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5546703Svince@csl.cornell.edu       return -EFAULT;
5556703Svince@csl.cornell.edu
5566744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5576703Svince@csl.cornell.edu
5586703Svince@csl.cornell.edu    // Adjust path for current working directory
5596703Svince@csl.cornell.edu    path = process->fullPath(path);
5606703Svince@csl.cornell.edu
5616744SAli.Saidi@arm.com#if NO_STAT64
5626744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5636744SAli.Saidi@arm.com#else
5646703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5656744SAli.Saidi@arm.com#endif
5666703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5676703Svince@csl.cornell.edu}
5686703Svince@csl.cornell.edu
5696703Svince@csl.cornell.eduSyscallReturn
5706685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5716685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5726685Stjones1@inf.ed.ac.uk{
5736701Sgblack@eecs.umich.edu    int index = 0;
57410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
57510931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5766685Stjones1@inf.ed.ac.uk
57710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
57810931Sbrandon.potter@amd.com    if (sim_fd < 0)
5796685Stjones1@inf.ed.ac.uk        return -EBADF;
5806685Stjones1@inf.ed.ac.uk
5816744SAli.Saidi@arm.com#if NO_STAT64
58210931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5836744SAli.Saidi@arm.com#else
58410931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5856744SAli.Saidi@arm.com#endif
5866685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5876685Stjones1@inf.ed.ac.uk}
5886685Stjones1@inf.ed.ac.uk
5896685Stjones1@inf.ed.ac.ukSyscallReturn
5905513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5915513SMichael.Adler@intel.com{
5925513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5935513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5945513SMichael.Adler@intel.com    // changing anything.
5955513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5965513SMichael.Adler@intel.com    umask(oldMask);
5975521Snate@binkert.org    return (int)oldMask;
5985513SMichael.Adler@intel.com}
5995513SMichael.Adler@intel.com
6005513SMichael.Adler@intel.comSyscallReturn
6013114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
6021999SN/A{
6031999SN/A    string path;
6041999SN/A
6056701Sgblack@eecs.umich.edu    int index = 0;
6068852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6071999SN/A        return -EFAULT;
6081999SN/A
6091999SN/A    /* XXX endianess */
6106701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
6111999SN/A    uid_t hostOwner = owner;
6126701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6131999SN/A    gid_t hostGroup = group;
6141999SN/A
6153669Sbinkertn@umich.edu    // Adjust path for current working directory
6163669Sbinkertn@umich.edu    path = p->fullPath(path);
6173669Sbinkertn@umich.edu
6181999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6191999SN/A    return (result == -1) ? -errno : result;
6201999SN/A}
6211999SN/A
6221999SN/ASyscallReturn
6233114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6241999SN/A{
6256701Sgblack@eecs.umich.edu    int index = 0;
62610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6271999SN/A
62810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
62910931Sbrandon.potter@amd.com    if (sim_fd < 0)
6301999SN/A        return -EBADF;
6311999SN/A
6321999SN/A    /* XXX endianess */
6336701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
6341999SN/A    uid_t hostOwner = owner;
6356701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
6361999SN/A    gid_t hostGroup = group;
6371999SN/A
63810931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6391999SN/A    return (result == -1) ? -errno : result;
6401999SN/A}
6412093SN/A
6422093SN/A
6432093SN/ASyscallReturn
6443114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6453079Sstever@eecs.umich.edu{
6466701Sgblack@eecs.umich.edu    int index = 0;
64710781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
64810931Sbrandon.potter@amd.com
64910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
65010781Snilay@cs.wisc.edu    if (sim_fd < 0)
6513079Sstever@eecs.umich.edu        return -EBADF;
6523079Sstever@eecs.umich.edu
65310932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6545282Srstrong@cs.ucsd.edu
65510781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6566111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
65710932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6583079Sstever@eecs.umich.edu}
6593079Sstever@eecs.umich.edu
6603079Sstever@eecs.umich.edu
6613079Sstever@eecs.umich.eduSyscallReturn
6623114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6632680Sktlim@umich.edu          ThreadContext *tc)
6642093SN/A{
6656701Sgblack@eecs.umich.edu    int index = 0;
66610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6672093SN/A
66810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
66910931Sbrandon.potter@amd.com    if (sim_fd < 0)
6702093SN/A        return -EBADF;
6712093SN/A
6726701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6732093SN/A    switch (cmd) {
6742093SN/A      case 0: // F_DUPFD
6752093SN/A        // if we really wanted to support this, we'd need to do it
6762093SN/A        // in the target fd space.
67710931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6782093SN/A        return -EMFILE;
6792093SN/A
6802093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6812093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6822093SN/A        return 0;
6832093SN/A
6842093SN/A      case 3: // F_GETFL (get file flags)
6852093SN/A      case 4: // F_SETFL (set file flags)
6862093SN/A        // not sure if this is totally valid, but we'll pass it through
6872093SN/A        // to the underlying OS
68810931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
68910931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6902093SN/A        // return 0;
6912093SN/A
6922093SN/A      case 7: // F_GETLK  (get lock)
6932093SN/A      case 8: // F_SETLK  (set lock)
6942093SN/A      case 9: // F_SETLKW (set lock and wait)
6952093SN/A        // don't mess with file locking... just act like it's OK
69610931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6972093SN/A        return 0;
6982093SN/A
6992093SN/A      default:
7002093SN/A        warn("Unknown fcntl command %d\n", cmd);
7012093SN/A        return 0;
7022093SN/A    }
7032093SN/A}
7042093SN/A
7052238SN/ASyscallReturn
7063114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
7072687Sksewell@umich.edu            ThreadContext *tc)
7082687Sksewell@umich.edu{
7096701Sgblack@eecs.umich.edu    int index = 0;
71010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
7112687Sksewell@umich.edu
71210932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
71310931Sbrandon.potter@amd.com    if (sim_fd < 0)
7142687Sksewell@umich.edu        return -EBADF;
7152687Sksewell@umich.edu
7166701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
7172687Sksewell@umich.edu    switch (cmd) {
7182687Sksewell@umich.edu      case 33: //F_GETLK64
71910931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7202687Sksewell@umich.edu        return -EMFILE;
7212687Sksewell@umich.edu
7222687Sksewell@umich.edu      case 34: // F_SETLK64
7232687Sksewell@umich.edu      case 35: // F_SETLKW64
72410931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
72510931Sbrandon.potter@amd.com             tgt_fd);
7262687Sksewell@umich.edu        return -EMFILE;
7272687Sksewell@umich.edu
7282687Sksewell@umich.edu      default:
7292687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7302687Sksewell@umich.edu        // to the underlying OS
73110931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
73210931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7332687Sksewell@umich.edu        // return 0;
7342687Sksewell@umich.edu    }
7352687Sksewell@umich.edu}
7362687Sksewell@umich.edu
7372687Sksewell@umich.eduSyscallReturn
7383114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7392680Sktlim@umich.edu         ThreadContext *tc)
7402238SN/A{
7412238SN/A    int fds[2], sim_fds[2];
7422238SN/A    int pipe_retval = pipe(fds);
7432093SN/A
7442238SN/A    if (pipe_retval < 0) {
7452238SN/A        // error
7462238SN/A        return pipe_retval;
7472238SN/A    }
7482238SN/A
74910932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
75010932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7512238SN/A
7525282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7532238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7542238SN/A    // the return value of the function, and fd[1] is returned in r20.
7552680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7562238SN/A    return sim_fds[0];
7572238SN/A}
7582238SN/A
7592238SN/A
7602238SN/ASyscallReturn
7613114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7622680Sktlim@umich.edu           ThreadContext *tc)
7632238SN/A{
7642238SN/A    // Make up a PID.  There's no interprocess communication in
7652238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7662238SN/A    // not getting a unique value.
7672238SN/A
7683114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7693114Sgblack@eecs.umich.edu    return process->pid();
7702238SN/A}
7712238SN/A
7722238SN/A
7732238SN/ASyscallReturn
7743114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7752680Sktlim@umich.edu           ThreadContext *tc)
7762238SN/A{
7772238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7782238SN/A    // simulation to be deterministic.
7792238SN/A
7802238SN/A    // EUID goes in r20.
7813114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7825543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7832238SN/A}
7842238SN/A
7852238SN/A
7862238SN/ASyscallReturn
7873114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7882680Sktlim@umich.edu           ThreadContext *tc)
7892238SN/A{
7902238SN/A    // Get current group ID.  EGID goes in r20.
7913114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7923114Sgblack@eecs.umich.edu    return process->gid();
7932238SN/A}
7942238SN/A
7952238SN/A
7962238SN/ASyscallReturn
7973114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7982680Sktlim@umich.edu           ThreadContext *tc)
7992238SN/A{
8002238SN/A    // can't fathom why a benchmark would call this.
8016701Sgblack@eecs.umich.edu    int index = 0;
8026701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8032238SN/A    return 0;
8042238SN/A}
8052238SN/A
8062238SN/ASyscallReturn
8073114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8082680Sktlim@umich.edu           ThreadContext *tc)
8092238SN/A{
8102238SN/A    // Make up a PID.  There's no interprocess communication in
8112238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8122238SN/A    // not getting a unique value.
8132238SN/A
8143114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
8153114Sgblack@eecs.umich.edu    return process->pid();
8162238SN/A}
8172238SN/A
8182238SN/ASyscallReturn
8193114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8202680Sktlim@umich.edu           ThreadContext *tc)
8212238SN/A{
8223114Sgblack@eecs.umich.edu    return process->ppid();
8232238SN/A}
8242238SN/A
8252238SN/ASyscallReturn
8263114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8272680Sktlim@umich.edu           ThreadContext *tc)
8282238SN/A{
8295543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8302238SN/A}
8312238SN/A
8322238SN/ASyscallReturn
8333114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8342680Sktlim@umich.edu           ThreadContext *tc)
8352238SN/A{
8365543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8372238SN/A}
8382238SN/A
8392238SN/ASyscallReturn
8403114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8412680Sktlim@umich.edu           ThreadContext *tc)
8422238SN/A{
8433114Sgblack@eecs.umich.edu    return process->gid();
8442238SN/A}
8452238SN/A
8462238SN/ASyscallReturn
8473114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8482680Sktlim@umich.edu           ThreadContext *tc)
8492238SN/A{
8503114Sgblack@eecs.umich.edu    return process->egid();
8512238SN/A}
8522238SN/A
8532238SN/A
8546109Ssanchezd@stanford.eduSyscallReturn
8556109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8566109Ssanchezd@stanford.edu           ThreadContext *tc)
8576109Ssanchezd@stanford.edu{
8586701Sgblack@eecs.umich.edu    int index = 0;
8596701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8606701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8616701Sgblack@eecs.umich.edu
8626109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8636701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8646701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8656109Ssanchezd@stanford.edu
8666109Ssanchezd@stanford.edu
8676701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8686111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8696111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8706111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8716701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8726109Ssanchezd@stanford.edu    }
8736109Ssanchezd@stanford.edu
8746111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8756109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8766109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8776109Ssanchezd@stanford.edu
8786109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8796109Ssanchezd@stanford.edu
8806111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8816109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8826111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8836109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8846109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8856109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8866109Ssanchezd@stanford.edu
8876111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8886109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8896109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8906109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8916109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8926337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8936109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8946109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8959375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8966109Ssanchezd@stanford.edu
8976109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8986109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8998149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
9008149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
9016109Ssanchezd@stanford.edu        #else
9026109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
9036109Ssanchezd@stanford.edu        #endif
9046109Ssanchezd@stanford.edu
9056111Ssteve.reinhardt@amd.com        // Set up stack register
9066701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
9076109Ssanchezd@stanford.edu
9086111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
9096111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
9106109Ssanchezd@stanford.edu
9116111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
9126109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
9136110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
9146109Ssanchezd@stanford.edu        #endif
9156109Ssanchezd@stanford.edu
9166111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
9176111Ssteve.reinhardt@amd.com        // parent, non-zero if child
9186109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
9196109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
9206109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
9216109Ssanchezd@stanford.edu        #endif
9226109Ssanchezd@stanford.edu
9237720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
9246109Ssanchezd@stanford.edu
9256109Ssanchezd@stanford.edu        ctc->activate();
9266109Ssanchezd@stanford.edu
9276109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9286109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9296109Ssanchezd@stanford.edu        return 1;
9306109Ssanchezd@stanford.edu    } else {
9316109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9326109Ssanchezd@stanford.edu        return 0;
9336109Ssanchezd@stanford.edu    }
9346109Ssanchezd@stanford.edu}
9356109Ssanchezd@stanford.edu
9369455Smitch.hayenga+gem5@gmail.comSyscallReturn
93710203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
93810203SAli.Saidi@ARM.com        int index)
9399455Smitch.hayenga+gem5@gmail.com{
9409455Smitch.hayenga+gem5@gmail.com    string path;
9419455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
94210223Ssteve.reinhardt@amd.com        return -EFAULT;
9439455Smitch.hayenga+gem5@gmail.com
9449455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9459455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9469455Smitch.hayenga+gem5@gmail.com
9479455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9489455Smitch.hayenga+gem5@gmail.com
9499455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9509455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9519455Smitch.hayenga+gem5@gmail.com}
95210203SAli.Saidi@ARM.com
95310203SAli.Saidi@ARM.comSyscallReturn
95410203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
95510203SAli.Saidi@ARM.com{
95610203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
95710203SAli.Saidi@ARM.com}
95810203SAli.Saidi@ARM.com
959