syscall_emul.cc revision 11851
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
17510932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
17610931Sbrandon.potter@amd.com    if (sim_fd < 0)
17710931Sbrandon.potter@amd.com        return -EBADF;
17810931Sbrandon.potter@amd.com
1797508Stjones1@inf.ed.ac.uk    int status = 0;
1807508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
1817508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
1821970SN/A    if (status >= 0)
18310932Sbrandon.potter@amd.com        p->resetFDEntry(tgt_fd);
1841970SN/A    return status;
185360SN/A}
186360SN/A
187360SN/A
1881450SN/ASyscallReturn
18911851Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
190360SN/A{
1916701Sgblack@eecs.umich.edu    int index = 0;
19210931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
1936701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
1946701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
1956701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
196360SN/A
19710932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
19810931Sbrandon.potter@amd.com    if (sim_fd < 0)
19910931Sbrandon.potter@amd.com        return -EBADF;
20010931Sbrandon.potter@amd.com
20110931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
202360SN/A
20311684Snderumigny@gmail.com    if (bytes_read > 0)
2048706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
205360SN/A
2061458SN/A    return bytes_read;
207360SN/A}
208360SN/A
2091450SN/ASyscallReturn
21011851Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
211360SN/A{
2126701Sgblack@eecs.umich.edu    int index = 0;
21310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2146701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2156701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2166701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
217360SN/A
21810932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
21910931Sbrandon.potter@amd.com    if (sim_fd < 0)
22010931Sbrandon.potter@amd.com        return -EBADF;
22110931Sbrandon.potter@amd.com
2228706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
223360SN/A
22410931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
225360SN/A
22610931Sbrandon.potter@amd.com    fsync(sim_fd);
227360SN/A
2281458SN/A    return bytes_written;
229360SN/A}
230360SN/A
231360SN/A
2321450SN/ASyscallReturn
23311851Sbrandon.potter@amd.comlseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
234360SN/A{
2356701Sgblack@eecs.umich.edu    int index = 0;
23610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2376701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2386701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
239360SN/A
24010932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
24110931Sbrandon.potter@amd.com    if (sim_fd < 0)
24210931Sbrandon.potter@amd.com        return -EBADF;
24310931Sbrandon.potter@amd.com
24410931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
245360SN/A
2461458SN/A    return (result == (off_t)-1) ? -errno : result;
247360SN/A}
248360SN/A
249360SN/A
2501450SN/ASyscallReturn
25111851Sbrandon.potter@amd.com_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2524118Sgblack@eecs.umich.edu{
2536701Sgblack@eecs.umich.edu    int index = 0;
25410931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2556701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2566701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2576701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2586701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2594118Sgblack@eecs.umich.edu
26010932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
26110931Sbrandon.potter@amd.com    if (sim_fd < 0)
26210931Sbrandon.potter@amd.com        return -EBADF;
26310931Sbrandon.potter@amd.com
2644118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2654118Sgblack@eecs.umich.edu
26610931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
2674118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2684118Sgblack@eecs.umich.edu
26911379Sbrandon.potter@amd.com    if (result == (off_t)-1)
2704118Sgblack@eecs.umich.edu        return -errno;
27111379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
27211379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
27311379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
27411379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
27511379Sbrandon.potter@amd.com    return 0;
2764118Sgblack@eecs.umich.edu}
2774118Sgblack@eecs.umich.edu
2784118Sgblack@eecs.umich.edu
2794118Sgblack@eecs.umich.eduSyscallReturn
28011851Sbrandon.potter@amd.communmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
281360SN/A{
28211383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
28311383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
28411383Sbrandon.potter@amd.com    // memory.
2851458SN/A    return 0;
286360SN/A}
287360SN/A
288360SN/A
289360SN/Aconst char *hostname = "m5.eecs.umich.edu";
290360SN/A
2911450SN/ASyscallReturn
29211851Sbrandon.potter@amd.comgethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
293360SN/A{
2946701Sgblack@eecs.umich.edu    int index = 0;
2956701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2966701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
2976701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
298360SN/A
299360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
300360SN/A
3018706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
302360SN/A
3031458SN/A    return 0;
304360SN/A}
305360SN/A
3061450SN/ASyscallReturn
30711851Sbrandon.potter@amd.comgetcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
3085513SMichael.Adler@intel.com{
3095513SMichael.Adler@intel.com    int result = 0;
3106731Svince@csl.cornell.edu    int index = 0;
3116701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3126701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3136701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3145513SMichael.Adler@intel.com
3155513SMichael.Adler@intel.com    // Is current working directory defined?
3165513SMichael.Adler@intel.com    string cwd = p->getcwd();
3175513SMichael.Adler@intel.com    if (!cwd.empty()) {
3185513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3195513SMichael.Adler@intel.com            // Buffer too small
3205513SMichael.Adler@intel.com            return -ERANGE;
3215513SMichael.Adler@intel.com        }
3225513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3235513SMichael.Adler@intel.com        result = cwd.length();
32410955Sdavid.hashe@amd.com    } else {
3255513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3265513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
32710955Sdavid.hashe@amd.com        } else {
3285513SMichael.Adler@intel.com            result = -1;
3295513SMichael.Adler@intel.com        }
3305513SMichael.Adler@intel.com    }
3315513SMichael.Adler@intel.com
3328706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3335513SMichael.Adler@intel.com
3345513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3355513SMichael.Adler@intel.com}
3365513SMichael.Adler@intel.com
33710203SAli.Saidi@ARM.com/// Target open() handler.
33810203SAli.Saidi@ARM.comSyscallReturn
33911851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int callnum, Process *process,
34011851Sbrandon.potter@amd.com             ThreadContext *tc)
34110203SAli.Saidi@ARM.com{
34210203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
34310203SAli.Saidi@ARM.com}
3445513SMichael.Adler@intel.com
3455513SMichael.Adler@intel.comSyscallReturn
34611851Sbrandon.potter@amd.comreadlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
34711851Sbrandon.potter@amd.com             int index)
3485513SMichael.Adler@intel.com{
3495513SMichael.Adler@intel.com    string path;
3505513SMichael.Adler@intel.com
3518852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
35210223Ssteve.reinhardt@amd.com        return -EFAULT;
3535513SMichael.Adler@intel.com
3545513SMichael.Adler@intel.com    // Adjust path for current working directory
3555513SMichael.Adler@intel.com    path = p->fullPath(path);
3565513SMichael.Adler@intel.com
3576701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3586701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3596701Sgblack@eecs.umich.edu
3606701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3615513SMichael.Adler@intel.com
36210955Sdavid.hashe@amd.com    int result = -1;
36310955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
36410955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
36510955Sdavid.hashe@amd.com    } else {
36611140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
36711140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
36811851Sbrandon.potter@amd.com        // Process' executable). It is possible that using this path in
36911140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
37011140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
37111140Sjthestness@gmail.com        //     called binary calls readlink().
37211140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
37311140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
37411140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
37511140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
37611140Sjthestness@gmail.com
37711140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
37811140Sjthestness@gmail.com        char real_path[PATH_MAX];
37911140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
38011140Sjthestness@gmail.com        if (!check_real_path) {
38111140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
38211140Sjthestness@gmail.com                  "executable: %s", p->progName());
38311140Sjthestness@gmail.com        }
38411140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
38511140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
38611140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
38710955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
38810955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
38910955Sdavid.hashe@amd.com            result = bufsiz;
39010955Sdavid.hashe@amd.com        } else {
39111140Sjthestness@gmail.com            result = real_path_len;
39210955Sdavid.hashe@amd.com        }
39311140Sjthestness@gmail.com
39411140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
39511140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
39611140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
39711140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
39810955Sdavid.hashe@amd.com    }
3995513SMichael.Adler@intel.com
4008706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4015513SMichael.Adler@intel.com
4025513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4035513SMichael.Adler@intel.com}
4045513SMichael.Adler@intel.com
4055513SMichael.Adler@intel.comSyscallReturn
40611851Sbrandon.potter@amd.comunlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
407511SN/A{
40810633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
40910633Smichaelupton@gmail.com}
41010633Smichaelupton@gmail.com
41110633Smichaelupton@gmail.comSyscallReturn
41211851Sbrandon.potter@amd.comunlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
41311851Sbrandon.potter@amd.com             int index)
41410633Smichaelupton@gmail.com{
4151706SN/A    string path;
416360SN/A
4178852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
41810223Ssteve.reinhardt@amd.com        return -EFAULT;
419511SN/A
4203669Sbinkertn@umich.edu    // Adjust path for current working directory
4213669Sbinkertn@umich.edu    path = p->fullPath(path);
4223669Sbinkertn@umich.edu
423511SN/A    int result = unlink(path.c_str());
4241458SN/A    return (result == -1) ? -errno : result;
425511SN/A}
426511SN/A
4275513SMichael.Adler@intel.com
4285513SMichael.Adler@intel.comSyscallReturn
42911851Sbrandon.potter@amd.commkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4305513SMichael.Adler@intel.com{
4315513SMichael.Adler@intel.com    string path;
4325513SMichael.Adler@intel.com
4336701Sgblack@eecs.umich.edu    int index = 0;
4348852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
43510223Ssteve.reinhardt@amd.com        return -EFAULT;
4365513SMichael.Adler@intel.com
4375513SMichael.Adler@intel.com    // Adjust path for current working directory
4385513SMichael.Adler@intel.com    path = p->fullPath(path);
4395513SMichael.Adler@intel.com
4406701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4415513SMichael.Adler@intel.com
4425513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4435513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4445513SMichael.Adler@intel.com}
4455513SMichael.Adler@intel.com
4461450SN/ASyscallReturn
44711851Sbrandon.potter@amd.comrenameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
448511SN/A{
4491706SN/A    string old_name;
450511SN/A
4516701Sgblack@eecs.umich.edu    int index = 0;
4528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4531458SN/A        return -EFAULT;
454511SN/A
4551706SN/A    string new_name;
456511SN/A
4578852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4581458SN/A        return -EFAULT;
459511SN/A
4603669Sbinkertn@umich.edu    // Adjust path for current working directory
4613669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4623669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4633669Sbinkertn@umich.edu
4641706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4651458SN/A    return (result == -1) ? -errno : result;
466511SN/A}
467511SN/A
4681706SN/ASyscallReturn
46911851Sbrandon.potter@amd.comtruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
4701706SN/A{
4711706SN/A    string path;
4721706SN/A
4736701Sgblack@eecs.umich.edu    int index = 0;
4748852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4751706SN/A        return -EFAULT;
4761706SN/A
4776701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4781706SN/A
4793669Sbinkertn@umich.edu    // Adjust path for current working directory
4803669Sbinkertn@umich.edu    path = p->fullPath(path);
4813669Sbinkertn@umich.edu
4821706SN/A    int result = truncate(path.c_str(), length);
4831706SN/A    return (result == -1) ? -errno : result;
4841706SN/A}
4851706SN/A
4861706SN/ASyscallReturn
4876111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
48811851Sbrandon.potter@amd.com              Process *process, ThreadContext *tc)
4891706SN/A{
4906701Sgblack@eecs.umich.edu    int index = 0;
49110931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
49210931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
4931706SN/A
49410932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
49510931Sbrandon.potter@amd.com    if (sim_fd < 0)
4961706SN/A        return -EBADF;
4971706SN/A
49810931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
4991706SN/A    return (result == -1) ? -errno : result;
5001706SN/A}
5011999SN/A
5021999SN/ASyscallReturn
5036703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
50411851Sbrandon.potter@amd.com               Process *process, ThreadContext *tc)
5056703Svince@csl.cornell.edu{
5066703Svince@csl.cornell.edu    int index = 0;
5076703Svince@csl.cornell.edu    string path;
5086703Svince@csl.cornell.edu
5098852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5106703Svince@csl.cornell.edu       return -EFAULT;
5116703Svince@csl.cornell.edu
5126744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5136703Svince@csl.cornell.edu
5146703Svince@csl.cornell.edu    // Adjust path for current working directory
5156703Svince@csl.cornell.edu    path = process->fullPath(path);
5166703Svince@csl.cornell.edu
5176744SAli.Saidi@arm.com#if NO_STAT64
5186744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5196744SAli.Saidi@arm.com#else
5206703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5216744SAli.Saidi@arm.com#endif
5226703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5236703Svince@csl.cornell.edu}
5246703Svince@csl.cornell.edu
5256703Svince@csl.cornell.eduSyscallReturn
5266685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
52711851Sbrandon.potter@amd.com                Process *process, ThreadContext *tc)
5286685Stjones1@inf.ed.ac.uk{
5296701Sgblack@eecs.umich.edu    int index = 0;
53010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
53110931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5326685Stjones1@inf.ed.ac.uk
53310932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
53410931Sbrandon.potter@amd.com    if (sim_fd < 0)
5356685Stjones1@inf.ed.ac.uk        return -EBADF;
5366685Stjones1@inf.ed.ac.uk
5376744SAli.Saidi@arm.com#if NO_STAT64
53810931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5396744SAli.Saidi@arm.com#else
54010931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5416744SAli.Saidi@arm.com#endif
5426685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5436685Stjones1@inf.ed.ac.uk}
5446685Stjones1@inf.ed.ac.uk
5456685Stjones1@inf.ed.ac.ukSyscallReturn
54611851Sbrandon.potter@amd.comumaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5475513SMichael.Adler@intel.com{
5485513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5495513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5505513SMichael.Adler@intel.com    // changing anything.
5515513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5525513SMichael.Adler@intel.com    umask(oldMask);
5535521Snate@binkert.org    return (int)oldMask;
5545513SMichael.Adler@intel.com}
5555513SMichael.Adler@intel.com
5565513SMichael.Adler@intel.comSyscallReturn
55711851Sbrandon.potter@amd.comchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
5581999SN/A{
5591999SN/A    string path;
5601999SN/A
5616701Sgblack@eecs.umich.edu    int index = 0;
5628852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5631999SN/A        return -EFAULT;
5641999SN/A
5651999SN/A    /* XXX endianess */
5666701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5671999SN/A    uid_t hostOwner = owner;
5686701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5691999SN/A    gid_t hostGroup = group;
5701999SN/A
5713669Sbinkertn@umich.edu    // Adjust path for current working directory
5723669Sbinkertn@umich.edu    path = p->fullPath(path);
5733669Sbinkertn@umich.edu
5741999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5751999SN/A    return (result == -1) ? -errno : result;
5761999SN/A}
5771999SN/A
5781999SN/ASyscallReturn
57911851Sbrandon.potter@amd.comfchownFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
5801999SN/A{
5816701Sgblack@eecs.umich.edu    int index = 0;
58210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5831999SN/A
58410932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
58510931Sbrandon.potter@amd.com    if (sim_fd < 0)
5861999SN/A        return -EBADF;
5871999SN/A
5881999SN/A    /* XXX endianess */
5896701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5901999SN/A    uid_t hostOwner = owner;
5916701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5921999SN/A    gid_t hostGroup = group;
5931999SN/A
59410931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
5951999SN/A    return (result == -1) ? -errno : result;
5961999SN/A}
5972093SN/A
5982093SN/A
5992093SN/ASyscallReturn
60011851Sbrandon.potter@amd.comdupFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
6013079Sstever@eecs.umich.edu{
6026701Sgblack@eecs.umich.edu    int index = 0;
60310781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
60410931Sbrandon.potter@amd.com
60510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
60610781Snilay@cs.wisc.edu    if (sim_fd < 0)
6073079Sstever@eecs.umich.edu        return -EBADF;
6083079Sstever@eecs.umich.edu
60910932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6105282Srstrong@cs.ucsd.edu
61110781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6126111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
61310932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6143079Sstever@eecs.umich.edu}
6153079Sstever@eecs.umich.edu
6163079Sstever@eecs.umich.edu
6173079Sstever@eecs.umich.eduSyscallReturn
61811851Sbrandon.potter@amd.comfcntlFunc(SyscallDesc *desc, int num, Process *process,
6192680Sktlim@umich.edu          ThreadContext *tc)
6202093SN/A{
6216701Sgblack@eecs.umich.edu    int index = 0;
62210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6232093SN/A
62410932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
62510931Sbrandon.potter@amd.com    if (sim_fd < 0)
6262093SN/A        return -EBADF;
6272093SN/A
6286701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6292093SN/A    switch (cmd) {
6302093SN/A      case 0: // F_DUPFD
6312093SN/A        // if we really wanted to support this, we'd need to do it
6322093SN/A        // in the target fd space.
63310931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6342093SN/A        return -EMFILE;
6352093SN/A
6362093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6372093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6382093SN/A        return 0;
6392093SN/A
6402093SN/A      case 3: // F_GETFL (get file flags)
6412093SN/A      case 4: // F_SETFL (set file flags)
6422093SN/A        // not sure if this is totally valid, but we'll pass it through
6432093SN/A        // to the underlying OS
64410931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
64510931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6462093SN/A        // return 0;
6472093SN/A
6482093SN/A      case 7: // F_GETLK  (get lock)
6492093SN/A      case 8: // F_SETLK  (set lock)
6502093SN/A      case 9: // F_SETLKW (set lock and wait)
6512093SN/A        // don't mess with file locking... just act like it's OK
65210931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6532093SN/A        return 0;
6542093SN/A
6552093SN/A      default:
6562093SN/A        warn("Unknown fcntl command %d\n", cmd);
6572093SN/A        return 0;
6582093SN/A    }
6592093SN/A}
6602093SN/A
6612238SN/ASyscallReturn
66211851Sbrandon.potter@amd.comfcntl64Func(SyscallDesc *desc, int num, Process *process,
6632687Sksewell@umich.edu            ThreadContext *tc)
6642687Sksewell@umich.edu{
6656701Sgblack@eecs.umich.edu    int index = 0;
66610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6672687Sksewell@umich.edu
66810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
66910931Sbrandon.potter@amd.com    if (sim_fd < 0)
6702687Sksewell@umich.edu        return -EBADF;
6712687Sksewell@umich.edu
6726701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6732687Sksewell@umich.edu    switch (cmd) {
6742687Sksewell@umich.edu      case 33: //F_GETLK64
67510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
6762687Sksewell@umich.edu        return -EMFILE;
6772687Sksewell@umich.edu
6782687Sksewell@umich.edu      case 34: // F_SETLK64
6792687Sksewell@umich.edu      case 35: // F_SETLKW64
68010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
68110931Sbrandon.potter@amd.com             tgt_fd);
6822687Sksewell@umich.edu        return -EMFILE;
6832687Sksewell@umich.edu
6842687Sksewell@umich.edu      default:
6852687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6862687Sksewell@umich.edu        // to the underlying OS
68710931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
68810931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6892687Sksewell@umich.edu        // return 0;
6902687Sksewell@umich.edu    }
6912687Sksewell@umich.edu}
6922687Sksewell@umich.edu
6932687Sksewell@umich.eduSyscallReturn
69411851Sbrandon.potter@amd.compipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
69511851Sbrandon.potter@amd.com               ThreadContext *tc)
6962238SN/A{
6972238SN/A    int fds[2], sim_fds[2];
6982238SN/A    int pipe_retval = pipe(fds);
6992093SN/A
7002238SN/A    if (pipe_retval < 0) {
7012238SN/A        // error
7022238SN/A        return pipe_retval;
7032238SN/A    }
7042238SN/A
70510932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
70610932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7072238SN/A
7085282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7092238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7102238SN/A    // the return value of the function, and fd[1] is returned in r20.
7112680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7122238SN/A    return sim_fds[0];
7132238SN/A}
7142238SN/A
7152238SN/A
7162238SN/ASyscallReturn
71711851Sbrandon.potter@amd.comgetpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
71811851Sbrandon.potter@amd.com                 ThreadContext *tc)
7192238SN/A{
7202238SN/A    // Make up a PID.  There's no interprocess communication in
7212238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7222238SN/A    // not getting a unique value.
7232238SN/A
7243114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7253114Sgblack@eecs.umich.edu    return process->pid();
7262238SN/A}
7272238SN/A
7282238SN/A
7292238SN/ASyscallReturn
73011851Sbrandon.potter@amd.comgetuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
73111851Sbrandon.potter@amd.com                 ThreadContext *tc)
7322238SN/A{
7332238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7342238SN/A    // simulation to be deterministic.
7352238SN/A
7362238SN/A    // EUID goes in r20.
7373114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7385543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7392238SN/A}
7402238SN/A
7412238SN/A
7422238SN/ASyscallReturn
74311851Sbrandon.potter@amd.comgetgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
74411851Sbrandon.potter@amd.com                 ThreadContext *tc)
7452238SN/A{
7462238SN/A    // Get current group ID.  EGID goes in r20.
7473114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7483114Sgblack@eecs.umich.edu    return process->gid();
7492238SN/A}
7502238SN/A
7512238SN/A
7522238SN/ASyscallReturn
75311851Sbrandon.potter@amd.comsetuidFunc(SyscallDesc *desc, int callnum, Process *process,
7542680Sktlim@umich.edu           ThreadContext *tc)
7552238SN/A{
7562238SN/A    // can't fathom why a benchmark would call this.
7576701Sgblack@eecs.umich.edu    int index = 0;
7586701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7592238SN/A    return 0;
7602238SN/A}
7612238SN/A
7622238SN/ASyscallReturn
76311851Sbrandon.potter@amd.comgetpidFunc(SyscallDesc *desc, int callnum, Process *process,
7642680Sktlim@umich.edu           ThreadContext *tc)
7652238SN/A{
7662238SN/A    // Make up a PID.  There's no interprocess communication in
7672238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7682238SN/A    // not getting a unique value.
7692238SN/A
7703114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7713114Sgblack@eecs.umich.edu    return process->pid();
7722238SN/A}
7732238SN/A
7742238SN/ASyscallReturn
77511851Sbrandon.potter@amd.comgetppidFunc(SyscallDesc *desc, int callnum, Process *process,
77611851Sbrandon.potter@amd.com            ThreadContext *tc)
7772238SN/A{
7783114Sgblack@eecs.umich.edu    return process->ppid();
7792238SN/A}
7802238SN/A
7812238SN/ASyscallReturn
78211851Sbrandon.potter@amd.comgetuidFunc(SyscallDesc *desc, int callnum, Process *process,
7832680Sktlim@umich.edu           ThreadContext *tc)
7842238SN/A{
7855543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7862238SN/A}
7872238SN/A
7882238SN/ASyscallReturn
78911851Sbrandon.potter@amd.comgeteuidFunc(SyscallDesc *desc, int callnum, Process *process,
79011851Sbrandon.potter@amd.com            ThreadContext *tc)
7912238SN/A{
7925543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7932238SN/A}
7942238SN/A
7952238SN/ASyscallReturn
79611851Sbrandon.potter@amd.comgetgidFunc(SyscallDesc *desc, int callnum, Process *process,
7972680Sktlim@umich.edu           ThreadContext *tc)
7982238SN/A{
7993114Sgblack@eecs.umich.edu    return process->gid();
8002238SN/A}
8012238SN/A
8022238SN/ASyscallReturn
80311851Sbrandon.potter@amd.comgetegidFunc(SyscallDesc *desc, int callnum, Process *process,
80411851Sbrandon.potter@amd.com            ThreadContext *tc)
8052238SN/A{
8063114Sgblack@eecs.umich.edu    return process->egid();
8072238SN/A}
8082238SN/A
8092238SN/A
8106109Ssanchezd@stanford.eduSyscallReturn
81111851Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *process,
81211851Sbrandon.potter@amd.com          ThreadContext *tc)
8136109Ssanchezd@stanford.edu{
8146701Sgblack@eecs.umich.edu    int index = 0;
8156701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8166701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8176701Sgblack@eecs.umich.edu
8186109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8196701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8206701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8216109Ssanchezd@stanford.edu
8226109Ssanchezd@stanford.edu
8236701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8246111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8256111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8266111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8276701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8286109Ssanchezd@stanford.edu    }
8296109Ssanchezd@stanford.edu
8306111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8316109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8326109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8336109Ssanchezd@stanford.edu
8346109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8356109Ssanchezd@stanford.edu
8366111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8376109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8386111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8396109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8406109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8416109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8426109Ssanchezd@stanford.edu
8436111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8446109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8456109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8466109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8476109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8486337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8496109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8506109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8519375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8526109Ssanchezd@stanford.edu
8536109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8546109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8558149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8568149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8576109Ssanchezd@stanford.edu        #else
8586109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8596109Ssanchezd@stanford.edu        #endif
8606109Ssanchezd@stanford.edu
8616111Ssteve.reinhardt@amd.com        // Set up stack register
8626701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8636109Ssanchezd@stanford.edu
8646111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8656111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8666109Ssanchezd@stanford.edu
8676111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8686109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8696110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8706109Ssanchezd@stanford.edu        #endif
8716109Ssanchezd@stanford.edu
8726111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8736111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8746109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8756109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8766109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8776109Ssanchezd@stanford.edu        #endif
8786109Ssanchezd@stanford.edu
8797720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8806109Ssanchezd@stanford.edu
8816109Ssanchezd@stanford.edu        ctc->activate();
8826109Ssanchezd@stanford.edu
8836109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8846109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8856109Ssanchezd@stanford.edu        return 1;
8866109Ssanchezd@stanford.edu    } else {
8876109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8886109Ssanchezd@stanford.edu        return 0;
8896109Ssanchezd@stanford.edu    }
8906109Ssanchezd@stanford.edu}
8916109Ssanchezd@stanford.edu
8929455Smitch.hayenga+gem5@gmail.comSyscallReturn
89311851Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, Process *process,
89411760Sbrandon.potter@amd.com              ThreadContext *tc)
89511760Sbrandon.potter@amd.com{
89611799Sbrandon.potter@amd.com#if NO_FALLOCATE
89711799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
89811799Sbrandon.potter@amd.com#else
89911760Sbrandon.potter@amd.com    int index = 0;
90011760Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
90111760Sbrandon.potter@amd.com    int mode = process->getSyscallArg(tc, index);
90211760Sbrandon.potter@amd.com    off_t offset = process->getSyscallArg(tc, index);
90311760Sbrandon.potter@amd.com    off_t len = process->getSyscallArg(tc, index);
90411760Sbrandon.potter@amd.com
90511760Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
90611760Sbrandon.potter@amd.com    if (sim_fd < 0)
90711760Sbrandon.potter@amd.com        return -EBADF;
90811760Sbrandon.potter@amd.com
90911760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
91011760Sbrandon.potter@amd.com    if (result < 0)
91111760Sbrandon.potter@amd.com        return -errno;
91211799Sbrandon.potter@amd.com#endif
91311760Sbrandon.potter@amd.com    return 0;
91411760Sbrandon.potter@amd.com}
91511760Sbrandon.potter@amd.com
91611760Sbrandon.potter@amd.comSyscallReturn
91711851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
91811851Sbrandon.potter@amd.com           int index)
9199455Smitch.hayenga+gem5@gmail.com{
9209455Smitch.hayenga+gem5@gmail.com    string path;
9219455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
92210223Ssteve.reinhardt@amd.com        return -EFAULT;
9239455Smitch.hayenga+gem5@gmail.com
9249455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9259455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9269455Smitch.hayenga+gem5@gmail.com
9279455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9289455Smitch.hayenga+gem5@gmail.com
9299455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9309455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9319455Smitch.hayenga+gem5@gmail.com}
93210203SAli.Saidi@ARM.com
93310203SAli.Saidi@ARM.comSyscallReturn
93411851Sbrandon.potter@amd.comaccessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
93510203SAli.Saidi@ARM.com{
93610203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
93710203SAli.Saidi@ARM.com}
93810203SAli.Saidi@ARM.com
939