syscall_emul.cc revision 11856
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
3211793Sbrandon.potter@amd.com#include "sim/syscall_emul.hh"
3311793Sbrandon.potter@amd.com
342093SN/A#include <fcntl.h>
35360SN/A#include <unistd.h>
36360SN/A
376712Snate@binkert.org#include <iostream>
38360SN/A#include <string>
39360SN/A
407680Sgblack@eecs.umich.edu#include "arch/utility.hh"
412474SN/A#include "base/chunk_generator.hh"
42360SN/A#include "base/trace.hh"
436658Snate@binkert.org#include "config/the_isa.hh"
442680Sktlim@umich.edu#include "cpu/thread_context.hh"
452474SN/A#include "mem/page_table.hh"
46360SN/A#include "sim/process.hh"
478229Snate@binkert.org#include "sim/sim_exit.hh"
4811794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
4911794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
506029Ssteve.reinhardt@amd.com#include "sim/system.hh"
51360SN/A
52360SN/Ausing namespace std;
532107SN/Ausing namespace TheISA;
54360SN/A
551450SN/ASyscallReturn
5611851Sbrandon.potter@amd.comunimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
572680Sktlim@umich.edu                  ThreadContext *tc)
58360SN/A{
5911794Sbrandon.potter@amd.com    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
602484SN/A
612484SN/A    return 1;
62360SN/A}
63360SN/A
64360SN/A
651450SN/ASyscallReturn
6611851Sbrandon.potter@amd.comignoreFunc(SyscallDesc *desc, int callnum, Process *process,
672680Sktlim@umich.edu           ThreadContext *tc)
68360SN/A{
6911794Sbrandon.potter@amd.com    if (desc->needWarning()) {
7011794Sbrandon.potter@amd.com        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
7111794Sbrandon.potter@amd.com             "\n      (further warnings will be suppressed)" : "");
7210831Ssteve.reinhardt@amd.com    }
73360SN/A
748149SChris.Emmons@ARM.com    return 0;
758149SChris.Emmons@ARM.com}
768149SChris.Emmons@ARM.com
778149SChris.Emmons@ARM.com
788149SChris.Emmons@ARM.comSyscallReturn
7911851Sbrandon.potter@amd.comexitFunc(SyscallDesc *desc, int callnum, Process *process,
802680Sktlim@umich.edu         ThreadContext *tc)
81360SN/A{
826029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
836029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
846701Sgblack@eecs.umich.edu        int index = 0;
855958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
866701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
876029Ssteve.reinhardt@amd.com    } else {
886029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
896029Ssteve.reinhardt@amd.com        tc->halt();
902834Sksewell@umich.edu    }
91360SN/A
921458SN/A    return 1;
93360SN/A}
94360SN/A
95360SN/A
961450SN/ASyscallReturn
9711851Sbrandon.potter@amd.comexitGroupFunc(SyscallDesc *desc, int callnum, Process *process,
986109Ssanchezd@stanford.edu              ThreadContext *tc)
996109Ssanchezd@stanford.edu{
10010483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
10110483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
10210483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
10310483Swiseveri@student.ethz.ch    }
10410483Swiseveri@student.ethz.ch
10510483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
10610483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
10710483Swiseveri@student.ethz.ch        int index = 0;
10810483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
10910483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
11010483Swiseveri@student.ethz.ch    }
1116109Ssanchezd@stanford.edu
1126109Ssanchezd@stanford.edu    return 1;
1136109Ssanchezd@stanford.edu}
1146109Ssanchezd@stanford.edu
1156109Ssanchezd@stanford.edu
1166109Ssanchezd@stanford.eduSyscallReturn
11711851Sbrandon.potter@amd.comgetpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
118360SN/A{
11910318Sandreas.hansson@arm.com    return (int)PageBytes;
120360SN/A}
121360SN/A
122360SN/A
1231450SN/ASyscallReturn
12411851Sbrandon.potter@amd.combrkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
125360SN/A{
126360SN/A    // change brk addr to first arg
1276701Sgblack@eecs.umich.edu    int index = 0;
1286701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1295748SSteve.Reinhardt@amd.com
1305748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1315748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1325748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1335748SSteve.Reinhardt@amd.com        return p->brk_point;
1345748SSteve.Reinhardt@amd.com
1355748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1365748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1372474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
13810318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1395748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
14010318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1416687Stjones1@inf.ed.ac.uk
1426687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1436687Stjones1@inf.ed.ac.uk            else {
1446687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1458852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1466687Stjones1@inf.ed.ac.uk
1476687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
14810318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1496687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1508852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
15110318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1526687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1536687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1546687Stjones1@inf.ed.ac.uk                {
15510318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1568852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1576687Stjones1@inf.ed.ac.uk                }
1586687Stjones1@inf.ed.ac.uk            }
1592474SN/A        }
1601450SN/A    }
1615748SSteve.Reinhardt@amd.com
1625748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
16311380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
16411380Salexandru.dutu@amd.com                    p->brk_point);
1651458SN/A    return p->brk_point;
166360SN/A}
167360SN/A
168360SN/A
1691450SN/ASyscallReturn
17011851Sbrandon.potter@amd.comcloseFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
171360SN/A{
1726701Sgblack@eecs.umich.edu    int index = 0;
17310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
17410931Sbrandon.potter@amd.com
17511856Sbrandon.potter@amd.com    return p->fds->closeFDEntry(tgt_fd);
176360SN/A}
177360SN/A
178360SN/A
1791450SN/ASyscallReturn
18011851Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
181360SN/A{
1826701Sgblack@eecs.umich.edu    int index = 0;
18310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
1846701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
1856701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
18611856Sbrandon.potter@amd.com
18711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
18811856Sbrandon.potter@amd.com    if (!hbfdp)
18911856Sbrandon.potter@amd.com        return -EBADF;
19011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
19111856Sbrandon.potter@amd.com
1926701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
19310931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
194360SN/A
19511684Snderumigny@gmail.com    if (bytes_read > 0)
1968706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
197360SN/A
1981458SN/A    return bytes_read;
199360SN/A}
200360SN/A
2011450SN/ASyscallReturn
20211851Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
203360SN/A{
2046701Sgblack@eecs.umich.edu    int index = 0;
20510931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2066701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2076701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
20811856Sbrandon.potter@amd.com
20911856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
21011856Sbrandon.potter@amd.com    if (!hbfdp)
21111856Sbrandon.potter@amd.com        return -EBADF;
21211856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
21311856Sbrandon.potter@amd.com
2146701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
2158706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
216360SN/A
21710931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
218360SN/A
21910931Sbrandon.potter@amd.com    fsync(sim_fd);
220360SN/A
2211458SN/A    return bytes_written;
222360SN/A}
223360SN/A
224360SN/A
2251450SN/ASyscallReturn
22611851Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
227360SN/A{
2286701Sgblack@eecs.umich.edu    int index = 0;
22910931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2306701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2316701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
232360SN/A
23311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
23411856Sbrandon.potter@amd.com    if (!ffdp)
23510931Sbrandon.potter@amd.com        return -EBADF;
23611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
23710931Sbrandon.potter@amd.com
23810931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
239360SN/A
2401458SN/A    return (result == (off_t)-1) ? -errno : result;
241360SN/A}
242360SN/A
243360SN/A
2441450SN/ASyscallReturn
24511851Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2464118Sgblack@eecs.umich.edu{
2476701Sgblack@eecs.umich.edu    int index = 0;
24810931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2496701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2506701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2516701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2526701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2534118Sgblack@eecs.umich.edu
25411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
25511856Sbrandon.potter@amd.com    if (!ffdp)
25610931Sbrandon.potter@amd.com        return -EBADF;
25711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
25810931Sbrandon.potter@amd.com
2594118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2604118Sgblack@eecs.umich.edu
26110931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
2624118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2634118Sgblack@eecs.umich.edu
26411379Sbrandon.potter@amd.com    if (result == (off_t)-1)
2654118Sgblack@eecs.umich.edu        return -errno;
26611379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
26711379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
26811379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
26911379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
27011379Sbrandon.potter@amd.com    return 0;
2714118Sgblack@eecs.umich.edu}
2724118Sgblack@eecs.umich.edu
2734118Sgblack@eecs.umich.edu
2744118Sgblack@eecs.umich.eduSyscallReturn
27511851Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
276360SN/A{
27711383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
27811383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
27911383Sbrandon.potter@amd.com    // memory.
2801458SN/A    return 0;
281360SN/A}
282360SN/A
283360SN/A
284360SN/Aconst char *hostname = "m5.eecs.umich.edu";
285360SN/A
2861450SN/ASyscallReturn
28711851Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
288360SN/A{
2896701Sgblack@eecs.umich.edu    int index = 0;
2906701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2916701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
2926701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
293360SN/A
294360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
295360SN/A
2968706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
297360SN/A
2981458SN/A    return 0;
299360SN/A}
300360SN/A
3011450SN/ASyscallReturn
30211851Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3035513SMichael.Adler@intel.com{
3045513SMichael.Adler@intel.com    int result = 0;
3056731Svince@csl.cornell.edu    int index = 0;
3066701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3076701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3086701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3095513SMichael.Adler@intel.com
3105513SMichael.Adler@intel.com    // Is current working directory defined?
3115513SMichael.Adler@intel.com    string cwd = p->getcwd();
3125513SMichael.Adler@intel.com    if (!cwd.empty()) {
3135513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3145513SMichael.Adler@intel.com            // Buffer too small
3155513SMichael.Adler@intel.com            return -ERANGE;
3165513SMichael.Adler@intel.com        }
3175513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3185513SMichael.Adler@intel.com        result = cwd.length();
31910955Sdavid.hashe@amd.com    } else {
32011856Sbrandon.potter@amd.com        if (getcwd((char *)buf.bufferPtr(), size)) {
3215513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
32210955Sdavid.hashe@amd.com        } else {
3235513SMichael.Adler@intel.com            result = -1;
3245513SMichael.Adler@intel.com        }
3255513SMichael.Adler@intel.com    }
3265513SMichael.Adler@intel.com
3278706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3285513SMichael.Adler@intel.com
3295513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3305513SMichael.Adler@intel.com}
3315513SMichael.Adler@intel.com
33210203SAli.Saidi@ARM.com/// Target open() handler.
33310203SAli.Saidi@ARM.comSyscallReturn
33411851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
33511851Sbrandon.potter@amd.com             ThreadContext *tc)
33610203SAli.Saidi@ARM.com{
33710203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
33810203SAli.Saidi@ARM.com}
3395513SMichael.Adler@intel.com
3405513SMichael.Adler@intel.comSyscallReturn
34111851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
34211851Sbrandon.potter@amd.com             int index)
3435513SMichael.Adler@intel.com{
3445513SMichael.Adler@intel.com    string path;
3455513SMichael.Adler@intel.com
3468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
34710223Ssteve.reinhardt@amd.com        return -EFAULT;
3485513SMichael.Adler@intel.com
3495513SMichael.Adler@intel.com    // Adjust path for current working directory
3505513SMichael.Adler@intel.com    path = p->fullPath(path);
3515513SMichael.Adler@intel.com
3526701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3536701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3546701Sgblack@eecs.umich.edu
3556701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3565513SMichael.Adler@intel.com
35710955Sdavid.hashe@amd.com    int result = -1;
35810955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
35910955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
36010955Sdavid.hashe@amd.com    } else {
36111140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
36211140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
36311851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
36411140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
36511140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
36611140Sjthestness@gmail.com        //     called binary calls readlink().
36711140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
36811140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
36911140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
37011140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
37111140Sjthestness@gmail.com
37211140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
37311140Sjthestness@gmail.com        char real_path[PATH_MAX];
37411140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
37511140Sjthestness@gmail.com        if (!check_real_path) {
37611140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
37711140Sjthestness@gmail.com                  "executable: %s", p->progName());
37811140Sjthestness@gmail.com        }
37911140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
38011140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
38111140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
38210955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
38310955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
38410955Sdavid.hashe@amd.com            result = bufsiz;
38510955Sdavid.hashe@amd.com        } else {
38611140Sjthestness@gmail.com            result = real_path_len;
38710955Sdavid.hashe@amd.com        }
38811140Sjthestness@gmail.com
38911140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
39011140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
39111140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
39211140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
39310955Sdavid.hashe@amd.com    }
3945513SMichael.Adler@intel.com
3958706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3965513SMichael.Adler@intel.com
3975513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3985513SMichael.Adler@intel.com}
3995513SMichael.Adler@intel.com
4005513SMichael.Adler@intel.comSyscallReturn
40111851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
402511SN/A{
40310633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
40410633Smichaelupton@gmail.com}
40510633Smichaelupton@gmail.com
40610633Smichaelupton@gmail.comSyscallReturn
40711851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
40811851Sbrandon.potter@amd.com             int index)
40910633Smichaelupton@gmail.com{
4101706SN/A    string path;
411360SN/A
4128852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
41310223Ssteve.reinhardt@amd.com        return -EFAULT;
414511SN/A
4153669Sbinkertn@umich.edu    // Adjust path for current working directory
4163669Sbinkertn@umich.edu    path = p->fullPath(path);
4173669Sbinkertn@umich.edu
418511SN/A    int result = unlink(path.c_str());
4191458SN/A    return (result == -1) ? -errno : result;
420511SN/A}
421511SN/A
4225513SMichael.Adler@intel.com
4235513SMichael.Adler@intel.comSyscallReturn
42411851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4255513SMichael.Adler@intel.com{
4265513SMichael.Adler@intel.com    string path;
4275513SMichael.Adler@intel.com
4286701Sgblack@eecs.umich.edu    int index = 0;
4298852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
43010223Ssteve.reinhardt@amd.com        return -EFAULT;
4315513SMichael.Adler@intel.com
4325513SMichael.Adler@intel.com    // Adjust path for current working directory
4335513SMichael.Adler@intel.com    path = p->fullPath(path);
4345513SMichael.Adler@intel.com
4356701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4365513SMichael.Adler@intel.com
4375513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4385513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4395513SMichael.Adler@intel.com}
4405513SMichael.Adler@intel.com
4411450SN/ASyscallReturn
44211851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
443511SN/A{
4441706SN/A    string old_name;
445511SN/A
4466701Sgblack@eecs.umich.edu    int index = 0;
4478852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4481458SN/A        return -EFAULT;
449511SN/A
4501706SN/A    string new_name;
451511SN/A
4528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4531458SN/A        return -EFAULT;
454511SN/A
4553669Sbinkertn@umich.edu    // Adjust path for current working directory
4563669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4573669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4583669Sbinkertn@umich.edu
4591706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4601458SN/A    return (result == -1) ? -errno : result;
461511SN/A}
462511SN/A
4631706SN/ASyscallReturn
46411851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4651706SN/A{
4661706SN/A    string path;
4671706SN/A
4686701Sgblack@eecs.umich.edu    int index = 0;
4698852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4701706SN/A        return -EFAULT;
4711706SN/A
4726701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4731706SN/A
4743669Sbinkertn@umich.edu    // Adjust path for current working directory
4753669Sbinkertn@umich.edu    path = p->fullPath(path);
4763669Sbinkertn@umich.edu
4771706SN/A    int result = truncate(path.c_str(), length);
4781706SN/A    return (result == -1) ? -errno : result;
4791706SN/A}
4801706SN/A
4811706SN/ASyscallReturn
48211856Sbrandon.potter@amd.comftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4831706SN/A{
4846701Sgblack@eecs.umich.edu    int index = 0;
48511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
48611856Sbrandon.potter@amd.com    off_t length = p->getSyscallArg(tc, index);
4871706SN/A
48811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
48911856Sbrandon.potter@amd.com    if (!ffdp)
4901706SN/A        return -EBADF;
49111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
4921706SN/A
49310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
4941706SN/A    return (result == -1) ? -errno : result;
4951706SN/A}
4961999SN/A
4971999SN/ASyscallReturn
4986703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
49911851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
5006703Svince@csl.cornell.edu{
5016703Svince@csl.cornell.edu    int index = 0;
5026703Svince@csl.cornell.edu    string path;
5036703Svince@csl.cornell.edu
5048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5056703Svince@csl.cornell.edu       return -EFAULT;
5066703Svince@csl.cornell.edu
5076744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5086703Svince@csl.cornell.edu
5096703Svince@csl.cornell.edu    // Adjust path for current working directory
5106703Svince@csl.cornell.edu    path = process->fullPath(path);
5116703Svince@csl.cornell.edu
5126744SAli.Saidi@arm.com#if NO_STAT64
5136744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5146744SAli.Saidi@arm.com#else
5156703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5166744SAli.Saidi@arm.com#endif
5176703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5186703Svince@csl.cornell.edu}
5196703Svince@csl.cornell.edu
5206703Svince@csl.cornell.eduSyscallReturn
52111856Sbrandon.potter@amd.comftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5226685Stjones1@inf.ed.ac.uk{
5236701Sgblack@eecs.umich.edu    int index = 0;
52411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
52511856Sbrandon.potter@amd.com    int64_t length = p->getSyscallArg(tc, index, 64);
5266685Stjones1@inf.ed.ac.uk
52711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
52811856Sbrandon.potter@amd.com    if (!ffdp)
5296685Stjones1@inf.ed.ac.uk        return -EBADF;
53011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5316685Stjones1@inf.ed.ac.uk
5326744SAli.Saidi@arm.com#if NO_STAT64
53310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5346744SAli.Saidi@arm.com#else
53510931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5366744SAli.Saidi@arm.com#endif
5376685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5386685Stjones1@inf.ed.ac.uk}
5396685Stjones1@inf.ed.ac.uk
5406685Stjones1@inf.ed.ac.ukSyscallReturn
54111851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5425513SMichael.Adler@intel.com{
5435513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5445513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5455513SMichael.Adler@intel.com    // changing anything.
5465513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5475513SMichael.Adler@intel.com    umask(oldMask);
5485521Snate@binkert.org    return (int)oldMask;
5495513SMichael.Adler@intel.com}
5505513SMichael.Adler@intel.com
5515513SMichael.Adler@intel.comSyscallReturn
55211851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5531999SN/A{
5541999SN/A    string path;
5551999SN/A
5566701Sgblack@eecs.umich.edu    int index = 0;
5578852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5581999SN/A        return -EFAULT;
5591999SN/A
5601999SN/A    /* XXX endianess */
5616701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5621999SN/A    uid_t hostOwner = owner;
5636701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5641999SN/A    gid_t hostGroup = group;
5651999SN/A
5663669Sbinkertn@umich.edu    // Adjust path for current working directory
5673669Sbinkertn@umich.edu    path = p->fullPath(path);
5683669Sbinkertn@umich.edu
5691999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5701999SN/A    return (result == -1) ? -errno : result;
5711999SN/A}
5721999SN/A
5731999SN/ASyscallReturn
57411856Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5751999SN/A{
5766701Sgblack@eecs.umich.edu    int index = 0;
57711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
5781999SN/A
57911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
58011856Sbrandon.potter@amd.com    if (!ffdp)
5811999SN/A        return -EBADF;
58211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
5831999SN/A
5841999SN/A    /* XXX endianess */
58511856Sbrandon.potter@amd.com    uint32_t owner = p->getSyscallArg(tc, index);
5861999SN/A    uid_t hostOwner = owner;
58711856Sbrandon.potter@amd.com    uint32_t group = p->getSyscallArg(tc, index);
5881999SN/A    gid_t hostGroup = group;
5891999SN/A
59010931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
5911999SN/A    return (result == -1) ? -errno : result;
5921999SN/A}
5932093SN/A
5942093SN/A
59511856Sbrandon.potter@amd.com/**
59611856Sbrandon.potter@amd.com * TODO: there's a bit more involved here since file descriptors created with
59711856Sbrandon.potter@amd.com * dup are supposed to share a file description. So, there is a problem with
59811856Sbrandon.potter@amd.com * maintaining fields like file offset or flags since an update to such a
59911856Sbrandon.potter@amd.com * field won't be reflected in the metadata for the fd entries that we
60011856Sbrandon.potter@amd.com * maintain to hold metadata for checkpoint restoration.
60111856Sbrandon.potter@amd.com */
6022093SN/ASyscallReturn
60311856Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6043079Sstever@eecs.umich.edu{
6056701Sgblack@eecs.umich.edu    int index = 0;
60611856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
60710931Sbrandon.potter@amd.com
60811856Sbrandon.potter@amd.com    auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
60911856Sbrandon.potter@amd.com    if (!old_hbfdp)
6103079Sstever@eecs.umich.edu        return -EBADF;
61111856Sbrandon.potter@amd.com    int sim_fd = old_hbfdp->getSimFD();
6125282Srstrong@cs.ucsd.edu
61310781Snilay@cs.wisc.edu    int result = dup(sim_fd);
61411856Sbrandon.potter@amd.com    int local_errno = errno;
61511856Sbrandon.potter@amd.com
61611856Sbrandon.potter@amd.com    std::shared_ptr<FDEntry> new_fdep = old_hbfdp->clone();
61711856Sbrandon.potter@amd.com    auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(new_fdep);
61811856Sbrandon.potter@amd.com    new_hbfdp->setSimFD(result);
61911856Sbrandon.potter@amd.com
62011856Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : p->fds->allocFD(new_fdep);
6213079Sstever@eecs.umich.edu}
6223079Sstever@eecs.umich.edu
6233079Sstever@eecs.umich.edu
6243079Sstever@eecs.umich.eduSyscallReturn
62511856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6262093SN/A{
6276701Sgblack@eecs.umich.edu    int index = 0;
62811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6292093SN/A
63011856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
63111856Sbrandon.potter@amd.com    if (!hbfdp)
6322093SN/A        return -EBADF;
63311856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6342093SN/A
63511856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6362093SN/A    switch (cmd) {
6372093SN/A      case 0: // F_DUPFD
6382093SN/A        // if we really wanted to support this, we'd need to do it
6392093SN/A        // in the target fd space.
64010931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6412093SN/A        return -EMFILE;
6422093SN/A
6432093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6442093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6452093SN/A        return 0;
6462093SN/A
6472093SN/A      case 3: // F_GETFL (get file flags)
6482093SN/A      case 4: // F_SETFL (set file flags)
6492093SN/A        // not sure if this is totally valid, but we'll pass it through
6502093SN/A        // to the underlying OS
65110931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
65210931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6532093SN/A        // return 0;
6542093SN/A
6552093SN/A      case 7: // F_GETLK  (get lock)
6562093SN/A      case 8: // F_SETLK  (set lock)
6572093SN/A      case 9: // F_SETLKW (set lock and wait)
6582093SN/A        // don't mess with file locking... just act like it's OK
65910931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6602093SN/A        return 0;
6612093SN/A
6622093SN/A      default:
6632093SN/A        warn("Unknown fcntl command %d\n", cmd);
6642093SN/A        return 0;
6652093SN/A    }
6662093SN/A}
6672093SN/A
6682238SN/ASyscallReturn
66911856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6702687Sksewell@umich.edu{
6716701Sgblack@eecs.umich.edu    int index = 0;
67211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6732687Sksewell@umich.edu
67411856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
67511856Sbrandon.potter@amd.com    if (!hbfdp)
6762687Sksewell@umich.edu        return -EBADF;
67711856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6782687Sksewell@umich.edu
67911856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6802687Sksewell@umich.edu    switch (cmd) {
6812687Sksewell@umich.edu      case 33: //F_GETLK64
68210931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
6832687Sksewell@umich.edu        return -EMFILE;
6842687Sksewell@umich.edu
6852687Sksewell@umich.edu      case 34: // F_SETLK64
6862687Sksewell@umich.edu      case 35: // F_SETLKW64
68710931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
68810931Sbrandon.potter@amd.com             tgt_fd);
6892687Sksewell@umich.edu        return -EMFILE;
6902687Sksewell@umich.edu
6912687Sksewell@umich.edu      default:
6922687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6932687Sksewell@umich.edu        // to the underlying OS
69410931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
69510931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6962687Sksewell@umich.edu        // return 0;
6972687Sksewell@umich.edu    }
6982687Sksewell@umich.edu}
6992687Sksewell@umich.edu
7002687Sksewell@umich.eduSyscallReturn
70111851Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
70211851Sbrandon.potter@amd.com               ThreadContext *tc)
7032238SN/A{
70411856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
7052093SN/A
70611856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
70711856Sbrandon.potter@amd.com    if (pipe_retval < 0)
7082238SN/A        return pipe_retval;
7092238SN/A
71011856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
71111856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
7122238SN/A
71311856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
71411856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
71511856Sbrandon.potter@amd.com
71611856Sbrandon.potter@amd.com    tgt_fds[0] = process->fds->allocFD(rpfd);
71711856Sbrandon.potter@amd.com    tgt_fds[1] = process->fds->allocFD(wpfd);
71811856Sbrandon.potter@amd.com
71911856Sbrandon.potter@amd.com    /**
72011856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
72111856Sbrandon.potter@amd.com     * as the write end of the pipe.
72211856Sbrandon.potter@amd.com     */
72311856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
72411856Sbrandon.potter@amd.com
72511856Sbrandon.potter@amd.com    /**
72611856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
72711856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
72811856Sbrandon.potter@amd.com     */
72911856Sbrandon.potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
7302238SN/A    return sim_fds[0];
7312238SN/A}
7322238SN/A
7332238SN/A
7342238SN/ASyscallReturn
73511851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
73611851Sbrandon.potter@amd.com                 ThreadContext *tc)
7372238SN/A{
7382238SN/A    // Make up a PID.  There's no interprocess communication in
7392238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7402238SN/A    // not getting a unique value.
7412238SN/A
7423114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7433114Sgblack@eecs.umich.edu    return process->pid();
7442238SN/A}
7452238SN/A
7462238SN/A
7472238SN/ASyscallReturn
74811851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
74911851Sbrandon.potter@amd.com                 ThreadContext *tc)
7502238SN/A{
7512238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7522238SN/A    // simulation to be deterministic.
7532238SN/A
7542238SN/A    // EUID goes in r20.
7553114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7565543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7572238SN/A}
7582238SN/A
7592238SN/A
7602238SN/ASyscallReturn
76111851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
76211851Sbrandon.potter@amd.com                 ThreadContext *tc)
7632238SN/A{
7642238SN/A    // Get current group ID.  EGID goes in r20.
7653114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7663114Sgblack@eecs.umich.edu    return process->gid();
7672238SN/A}
7682238SN/A
7692238SN/A
7702238SN/ASyscallReturn
77111851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
7722680Sktlim@umich.edu           ThreadContext *tc)
7732238SN/A{
7742238SN/A    // can't fathom why a benchmark would call this.
7756701Sgblack@eecs.umich.edu    int index = 0;
7766701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7772238SN/A    return 0;
7782238SN/A}
7792238SN/A
7802238SN/ASyscallReturn
78111851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
7822680Sktlim@umich.edu           ThreadContext *tc)
7832238SN/A{
7842238SN/A    // Make up a PID.  There's no interprocess communication in
7852238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7862238SN/A    // not getting a unique value.
7872238SN/A
7883114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7893114Sgblack@eecs.umich.edu    return process->pid();
7902238SN/A}
7912238SN/A
7922238SN/ASyscallReturn
79311851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
79411851Sbrandon.potter@amd.com            ThreadContext *tc)
7952238SN/A{
7963114Sgblack@eecs.umich.edu    return process->ppid();
7972238SN/A}
7982238SN/A
7992238SN/ASyscallReturn
80011851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8012680Sktlim@umich.edu           ThreadContext *tc)
8022238SN/A{
8035543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8042238SN/A}
8052238SN/A
8062238SN/ASyscallReturn
80711851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
80811851Sbrandon.potter@amd.com            ThreadContext *tc)
8092238SN/A{
8105543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8112238SN/A}
8122238SN/A
8132238SN/ASyscallReturn
81411851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
8152680Sktlim@umich.edu           ThreadContext *tc)
8162238SN/A{
8173114Sgblack@eecs.umich.edu    return process->gid();
8182238SN/A}
8192238SN/A
8202238SN/ASyscallReturn
82111851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
82211851Sbrandon.potter@amd.com            ThreadContext *tc)
8232238SN/A{
8243114Sgblack@eecs.umich.edu    return process->egid();
8252238SN/A}
8262238SN/A
8272238SN/A
8286109Ssanchezd@stanford.eduSyscallReturn
82911851Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *process,
83011851Sbrandon.potter@amd.com          ThreadContext *tc)
8316109Ssanchezd@stanford.edu{
8326701Sgblack@eecs.umich.edu    int index = 0;
8336701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8346701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8356701Sgblack@eecs.umich.edu
8366109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8376701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8386701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8396109Ssanchezd@stanford.edu
8406109Ssanchezd@stanford.edu
8416701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8426111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8436111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8446111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8456701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8466109Ssanchezd@stanford.edu    }
8476109Ssanchezd@stanford.edu
8486111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
84911856Sbrandon.potter@amd.com    if ((ctc = process->findFreeContext())) {
8506109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8516109Ssanchezd@stanford.edu
8526109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8536109Ssanchezd@stanford.edu
8546111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8556109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8566111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8576109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8586109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8596109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8606109Ssanchezd@stanford.edu
8616111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8626109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8636109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8646109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8656109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8666337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8676109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8686109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8699375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8706109Ssanchezd@stanford.edu
8716109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8726109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8738149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8748149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8756109Ssanchezd@stanford.edu        #else
8766109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8776109Ssanchezd@stanford.edu        #endif
8786109Ssanchezd@stanford.edu
8796111Ssteve.reinhardt@amd.com        // Set up stack register
8806701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8816109Ssanchezd@stanford.edu
8826111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8836111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8846109Ssanchezd@stanford.edu
8856111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8866109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8876110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8886109Ssanchezd@stanford.edu        #endif
8896109Ssanchezd@stanford.edu
8906111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8916111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8926109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8936109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8946109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8956109Ssanchezd@stanford.edu        #endif
8966109Ssanchezd@stanford.edu
8977720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8986109Ssanchezd@stanford.edu
8996109Ssanchezd@stanford.edu        ctc->activate();
9006109Ssanchezd@stanford.edu
9016109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9026109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9036109Ssanchezd@stanford.edu        return 1;
9046109Ssanchezd@stanford.edu    } else {
9056109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9066109Ssanchezd@stanford.edu        return 0;
9076109Ssanchezd@stanford.edu    }
9086109Ssanchezd@stanford.edu}
9096109Ssanchezd@stanford.edu
9109455Smitch.hayenga+gem5@gmail.comSyscallReturn
91111856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
91211760Sbrandon.potter@amd.com{
91311799Sbrandon.potter@amd.com#if NO_FALLOCATE
91411799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
91511799Sbrandon.potter@amd.com#else
91611760Sbrandon.potter@amd.com    int index = 0;
91711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
91811856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
91911856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
92011856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
92111760Sbrandon.potter@amd.com
92211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
92311856Sbrandon.potter@amd.com    if (!ffdp)
92411760Sbrandon.potter@amd.com        return -EBADF;
92511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
92611760Sbrandon.potter@amd.com
92711760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
92811760Sbrandon.potter@amd.com    if (result < 0)
92911760Sbrandon.potter@amd.com        return -errno;
93011799Sbrandon.potter@amd.com#endif
93111760Sbrandon.potter@amd.com    return 0;
93211760Sbrandon.potter@amd.com}
93311760Sbrandon.potter@amd.com
93411760Sbrandon.potter@amd.comSyscallReturn
93511851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
93611851Sbrandon.potter@amd.com           int index)
9379455Smitch.hayenga+gem5@gmail.com{
9389455Smitch.hayenga+gem5@gmail.com    string path;
9399455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
94010223Ssteve.reinhardt@amd.com        return -EFAULT;
9419455Smitch.hayenga+gem5@gmail.com
9429455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9439455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9449455Smitch.hayenga+gem5@gmail.com
9459455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9469455Smitch.hayenga+gem5@gmail.com
9479455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9489455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9499455Smitch.hayenga+gem5@gmail.com}
95010203SAli.Saidi@ARM.com
95110203SAli.Saidi@ARM.comSyscallReturn
95211851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
95310203SAli.Saidi@ARM.com{
95410203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
95510203SAli.Saidi@ARM.com}
95610203SAli.Saidi@ARM.com
957