syscall_emul.cc revision 11885
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.eduSyscallReturn
62411856Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6252093SN/A{
62611875Sbrandon.potter@amd.com    int arg;
6276701Sgblack@eecs.umich.edu    int index = 0;
62811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
62911875Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6302093SN/A
63111856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
63211856Sbrandon.potter@amd.com    if (!hbfdp)
6332093SN/A        return -EBADF;
63411856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6352093SN/A
63611875Sbrandon.potter@amd.com    int coe = hbfdp->getCOE();
63711875Sbrandon.potter@amd.com
6382093SN/A    switch (cmd) {
63911875Sbrandon.potter@amd.com      case F_GETFD:
64011875Sbrandon.potter@amd.com        return coe & FD_CLOEXEC;
6412093SN/A
64211875Sbrandon.potter@amd.com      case F_SETFD: {
64311875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
64411875Sbrandon.potter@amd.com        arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
6452093SN/A        return 0;
64611875Sbrandon.potter@amd.com      }
6472093SN/A
64811875Sbrandon.potter@amd.com      // Rely on the host to maintain the file status flags for this file
64911875Sbrandon.potter@amd.com      // description rather than maintain it ourselves. Admittedly, this
65011875Sbrandon.potter@amd.com      // is suboptimal (and possibly error prone), but it is difficult to
65111875Sbrandon.potter@amd.com      // maintain the flags by tracking them across the different descriptors
65211875Sbrandon.potter@amd.com      // (that refer to this file description) caused by clone, dup, and
65311875Sbrandon.potter@amd.com      // subsequent fcntls.
65411875Sbrandon.potter@amd.com      case F_GETFL:
65511875Sbrandon.potter@amd.com      case F_SETFL: {
65611875Sbrandon.potter@amd.com        arg = p->getSyscallArg(tc, index);
65711875Sbrandon.potter@amd.com        int rv = fcntl(sim_fd, cmd, arg);
65811875Sbrandon.potter@amd.com        return (rv == -1) ? -errno : rv;
65911875Sbrandon.potter@amd.com      }
6602093SN/A
6612093SN/A      default:
66211875Sbrandon.potter@amd.com        warn("fcntl: unsupported command %d\n", cmd);
6632093SN/A        return 0;
6642093SN/A    }
6652093SN/A}
6662093SN/A
6672238SN/ASyscallReturn
66811856Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
6692687Sksewell@umich.edu{
6706701Sgblack@eecs.umich.edu    int index = 0;
67111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
6722687Sksewell@umich.edu
67311856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
67411856Sbrandon.potter@amd.com    if (!hbfdp)
6752687Sksewell@umich.edu        return -EBADF;
67611856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
6772687Sksewell@umich.edu
67811856Sbrandon.potter@amd.com    int cmd = p->getSyscallArg(tc, index);
6792687Sksewell@umich.edu    switch (cmd) {
6802687Sksewell@umich.edu      case 33: //F_GETLK64
68110931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
6822687Sksewell@umich.edu        return -EMFILE;
6832687Sksewell@umich.edu
6842687Sksewell@umich.edu      case 34: // F_SETLK64
6852687Sksewell@umich.edu      case 35: // F_SETLKW64
68610931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
68710931Sbrandon.potter@amd.com             tgt_fd);
6882687Sksewell@umich.edu        return -EMFILE;
6892687Sksewell@umich.edu
6902687Sksewell@umich.edu      default:
6912687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6922687Sksewell@umich.edu        // to the underlying OS
69310931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
69410931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6952687Sksewell@umich.edu        // return 0;
6962687Sksewell@umich.edu    }
6972687Sksewell@umich.edu}
6982687Sksewell@umich.edu
6992687Sksewell@umich.eduSyscallReturn
70011851Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
70111851Sbrandon.potter@amd.com               ThreadContext *tc)
7022238SN/A{
70311856Sbrandon.potter@amd.com    int sim_fds[2], tgt_fds[2];
7042093SN/A
70511856Sbrandon.potter@amd.com    int pipe_retval = pipe(sim_fds);
70611856Sbrandon.potter@amd.com    if (pipe_retval < 0)
7072238SN/A        return pipe_retval;
7082238SN/A
70911856Sbrandon.potter@amd.com    auto rend = PipeFDEntry::EndType::read;
71011856Sbrandon.potter@amd.com    auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
7112238SN/A
71211856Sbrandon.potter@amd.com    auto wend = PipeFDEntry::EndType::write;
71311856Sbrandon.potter@amd.com    auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
71411856Sbrandon.potter@amd.com
71511856Sbrandon.potter@amd.com    tgt_fds[0] = process->fds->allocFD(rpfd);
71611856Sbrandon.potter@amd.com    tgt_fds[1] = process->fds->allocFD(wpfd);
71711856Sbrandon.potter@amd.com
71811856Sbrandon.potter@amd.com    /**
71911856Sbrandon.potter@amd.com     * Now patch the read object to record the target file descriptor chosen
72011856Sbrandon.potter@amd.com     * as the write end of the pipe.
72111856Sbrandon.potter@amd.com     */
72211856Sbrandon.potter@amd.com    rpfd->setPipeReadSource(tgt_fds[1]);
72311856Sbrandon.potter@amd.com
72411856Sbrandon.potter@amd.com    /**
72511856Sbrandon.potter@amd.com     * Alpha Linux convention for pipe() is that fd[0] is returned as
72611856Sbrandon.potter@amd.com     * the return value of the function, and fd[1] is returned in r20.
72711856Sbrandon.potter@amd.com     */
72811856Sbrandon.potter@amd.com    tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
7292238SN/A    return sim_fds[0];
7302238SN/A}
7312238SN/A
73211885Sbrandon.potter@amd.comSyscallReturn
73311885Sbrandon.potter@amd.comsetpgidFunc(SyscallDesc *desc, int callnum, Process *process,
73411885Sbrandon.potter@amd.com            ThreadContext *tc)
73511885Sbrandon.potter@amd.com{
73611885Sbrandon.potter@amd.com    int index = 0;
73711885Sbrandon.potter@amd.com    int pid = process->getSyscallArg(tc, index);
73811885Sbrandon.potter@amd.com    int pgid = process->getSyscallArg(tc, index);
73911885Sbrandon.potter@amd.com
74011885Sbrandon.potter@amd.com    if (pgid < 0)
74111885Sbrandon.potter@amd.com        return -EINVAL;
74211885Sbrandon.potter@amd.com
74311885Sbrandon.potter@amd.com    if (pid == 0) {
74411885Sbrandon.potter@amd.com        process->setpgid(process->pid());
74511885Sbrandon.potter@amd.com        return 0;
74611885Sbrandon.potter@amd.com    }
74711885Sbrandon.potter@amd.com
74811885Sbrandon.potter@amd.com    Process *matched_ph = NULL;
74911885Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
75011885Sbrandon.potter@amd.com
75111885Sbrandon.potter@amd.com    // Retrieves process pointer from active/suspended thread contexts.
75211885Sbrandon.potter@amd.com    for (int i = 0; i < sysh->numContexts(); i++) {
75311885Sbrandon.potter@amd.com        if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
75411885Sbrandon.potter@amd.com            Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
75511885Sbrandon.potter@amd.com            Process *walk_ph = (Process*)temp_h;
75611885Sbrandon.potter@amd.com
75711885Sbrandon.potter@amd.com            if (walk_ph && walk_ph->pid() == process->pid())
75811885Sbrandon.potter@amd.com                matched_ph = walk_ph;
75911885Sbrandon.potter@amd.com        }
76011885Sbrandon.potter@amd.com    }
76111885Sbrandon.potter@amd.com
76211885Sbrandon.potter@amd.com    assert(matched_ph != NULL);
76311885Sbrandon.potter@amd.com    matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
76411885Sbrandon.potter@amd.com
76511885Sbrandon.potter@amd.com    return 0;
76611885Sbrandon.potter@amd.com}
7672238SN/A
7682238SN/ASyscallReturn
76911851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
77011851Sbrandon.potter@amd.com                 ThreadContext *tc)
7712238SN/A{
7722238SN/A    // Make up a PID.  There's no interprocess communication in
7732238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7742238SN/A    // not getting a unique value.
7752238SN/A
7763114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7773114Sgblack@eecs.umich.edu    return process->pid();
7782238SN/A}
7792238SN/A
7802238SN/A
7812238SN/ASyscallReturn
78211851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
78311851Sbrandon.potter@amd.com                 ThreadContext *tc)
7842238SN/A{
7852238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7862238SN/A    // simulation to be deterministic.
7872238SN/A
7882238SN/A    // EUID goes in r20.
7893114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7905543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7912238SN/A}
7922238SN/A
7932238SN/A
7942238SN/ASyscallReturn
79511851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
79611851Sbrandon.potter@amd.com                 ThreadContext *tc)
7972238SN/A{
7982238SN/A    // Get current group ID.  EGID goes in r20.
7993114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
8003114Sgblack@eecs.umich.edu    return process->gid();
8012238SN/A}
8022238SN/A
8032238SN/A
8042238SN/ASyscallReturn
80511851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8062680Sktlim@umich.edu           ThreadContext *tc)
8072238SN/A{
8082238SN/A    // can't fathom why a benchmark would call this.
8096701Sgblack@eecs.umich.edu    int index = 0;
8106701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8112238SN/A    return 0;
8122238SN/A}
8132238SN/A
8142238SN/ASyscallReturn
81511851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
8162680Sktlim@umich.edu           ThreadContext *tc)
8172238SN/A{
81811885Sbrandon.potter@amd.com    return process->tgid();
81911885Sbrandon.potter@amd.com}
8202238SN/A
82111885Sbrandon.potter@amd.comSyscallReturn
82211885Sbrandon.potter@amd.comgettidFunc(SyscallDesc *desc, int callnum, Process *process,
82311885Sbrandon.potter@amd.com           ThreadContext *tc)
82411885Sbrandon.potter@amd.com{
8253114Sgblack@eecs.umich.edu    return process->pid();
8262238SN/A}
8272238SN/A
8282238SN/ASyscallReturn
82911851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
83011851Sbrandon.potter@amd.com            ThreadContext *tc)
8312238SN/A{
8323114Sgblack@eecs.umich.edu    return process->ppid();
8332238SN/A}
8342238SN/A
8352238SN/ASyscallReturn
83611851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
8372680Sktlim@umich.edu           ThreadContext *tc)
8382238SN/A{
8395543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8402238SN/A}
8412238SN/A
8422238SN/ASyscallReturn
84311851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
84411851Sbrandon.potter@amd.com            ThreadContext *tc)
8452238SN/A{
8465543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8472238SN/A}
8482238SN/A
8492238SN/ASyscallReturn
85011851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
8512680Sktlim@umich.edu           ThreadContext *tc)
8522238SN/A{
8533114Sgblack@eecs.umich.edu    return process->gid();
8542238SN/A}
8552238SN/A
8562238SN/ASyscallReturn
85711851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
85811851Sbrandon.potter@amd.com            ThreadContext *tc)
8592238SN/A{
8603114Sgblack@eecs.umich.edu    return process->egid();
8612238SN/A}
8622238SN/A
8632238SN/A
8646109Ssanchezd@stanford.eduSyscallReturn
86511851Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *process,
86611851Sbrandon.potter@amd.com          ThreadContext *tc)
8676109Ssanchezd@stanford.edu{
8686701Sgblack@eecs.umich.edu    int index = 0;
8696701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8706701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8716701Sgblack@eecs.umich.edu
8726109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8736701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8746701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8756109Ssanchezd@stanford.edu
8766109Ssanchezd@stanford.edu
8776701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8786111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8796111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8806111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8816701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8826109Ssanchezd@stanford.edu    }
8836109Ssanchezd@stanford.edu
8846111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
88511856Sbrandon.potter@amd.com    if ((ctc = process->findFreeContext())) {
8866109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8876109Ssanchezd@stanford.edu
8886109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8896109Ssanchezd@stanford.edu
8906111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8916109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8926111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8936109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8946109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8956109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8966109Ssanchezd@stanford.edu
8976111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8986109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8996109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
9006109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
9016109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
9026337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
9036109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
9046109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
9059375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
9066109Ssanchezd@stanford.edu
9076109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
9086109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
9098149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
9108149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
9116109Ssanchezd@stanford.edu        #else
9126109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
9136109Ssanchezd@stanford.edu        #endif
9146109Ssanchezd@stanford.edu
9156111Ssteve.reinhardt@amd.com        // Set up stack register
9166701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
9176109Ssanchezd@stanford.edu
9186111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
9196111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
9206109Ssanchezd@stanford.edu
9216111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
9226109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
9236110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
9246109Ssanchezd@stanford.edu        #endif
9256109Ssanchezd@stanford.edu
9266111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
9276111Ssteve.reinhardt@amd.com        // parent, non-zero if child
9286109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
9296109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
9306109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
9316109Ssanchezd@stanford.edu        #endif
9326109Ssanchezd@stanford.edu
9337720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
9346109Ssanchezd@stanford.edu
9356109Ssanchezd@stanford.edu        ctc->activate();
9366109Ssanchezd@stanford.edu
9376109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9386109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9396109Ssanchezd@stanford.edu        return 1;
9406109Ssanchezd@stanford.edu    } else {
9416109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9426109Ssanchezd@stanford.edu        return 0;
9436109Ssanchezd@stanford.edu    }
9446109Ssanchezd@stanford.edu}
9456109Ssanchezd@stanford.edu
9469455Smitch.hayenga+gem5@gmail.comSyscallReturn
94711856Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
94811760Sbrandon.potter@amd.com{
94911799Sbrandon.potter@amd.com#if NO_FALLOCATE
95011799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
95111799Sbrandon.potter@amd.com#else
95211760Sbrandon.potter@amd.com    int index = 0;
95311856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
95411856Sbrandon.potter@amd.com    int mode = p->getSyscallArg(tc, index);
95511856Sbrandon.potter@amd.com    off_t offset = p->getSyscallArg(tc, index);
95611856Sbrandon.potter@amd.com    off_t len = p->getSyscallArg(tc, index);
95711760Sbrandon.potter@amd.com
95811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
95911856Sbrandon.potter@amd.com    if (!ffdp)
96011760Sbrandon.potter@amd.com        return -EBADF;
96111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
96211760Sbrandon.potter@amd.com
96311760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
96411760Sbrandon.potter@amd.com    if (result < 0)
96511760Sbrandon.potter@amd.com        return -errno;
96611799Sbrandon.potter@amd.com#endif
96711760Sbrandon.potter@amd.com    return 0;
96811760Sbrandon.potter@amd.com}
96911760Sbrandon.potter@amd.com
97011760Sbrandon.potter@amd.comSyscallReturn
97111851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
97211851Sbrandon.potter@amd.com           int index)
9739455Smitch.hayenga+gem5@gmail.com{
9749455Smitch.hayenga+gem5@gmail.com    string path;
9759455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
97610223Ssteve.reinhardt@amd.com        return -EFAULT;
9779455Smitch.hayenga+gem5@gmail.com
9789455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9799455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9809455Smitch.hayenga+gem5@gmail.com
9819455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9829455Smitch.hayenga+gem5@gmail.com
9839455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9849455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9859455Smitch.hayenga+gem5@gmail.com}
98610203SAli.Saidi@ARM.com
98710203SAli.Saidi@ARM.comSyscallReturn
98811851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
98910203SAli.Saidi@ARM.com{
99010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
99110203SAli.Saidi@ARM.com}
99210203SAli.Saidi@ARM.com
993