syscall_emul.cc revision 11799
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 <cstdio>
386712Snate@binkert.org#include <iostream>
39360SN/A#include <string>
40360SN/A
417680Sgblack@eecs.umich.edu#include "arch/utility.hh"
422474SN/A#include "base/chunk_generator.hh"
43360SN/A#include "base/trace.hh"
446658Snate@binkert.org#include "config/the_isa.hh"
458229Snate@binkert.org#include "cpu/base.hh"
462680Sktlim@umich.edu#include "cpu/thread_context.hh"
472474SN/A#include "mem/page_table.hh"
48360SN/A#include "sim/process.hh"
498229Snate@binkert.org#include "sim/sim_exit.hh"
5011794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
5111794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
526029Ssteve.reinhardt@amd.com#include "sim/system.hh"
53360SN/A
54360SN/Ausing namespace std;
552107SN/Ausing namespace TheISA;
56360SN/A
571450SN/ASyscallReturn
583114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
592680Sktlim@umich.edu                  ThreadContext *tc)
60360SN/A{
6111794Sbrandon.potter@amd.com    fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
622484SN/A
632484SN/A    return 1;
64360SN/A}
65360SN/A
66360SN/A
671450SN/ASyscallReturn
683114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
692680Sktlim@umich.edu           ThreadContext *tc)
70360SN/A{
7111794Sbrandon.potter@amd.com    if (desc->needWarning()) {
7211794Sbrandon.potter@amd.com        warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
7311794Sbrandon.potter@amd.com             "\n      (further warnings will be suppressed)" : "");
7410831Ssteve.reinhardt@amd.com    }
75360SN/A
768149SChris.Emmons@ARM.com    return 0;
778149SChris.Emmons@ARM.com}
788149SChris.Emmons@ARM.com
798149SChris.Emmons@ARM.com
808149SChris.Emmons@ARM.comSyscallReturn
813114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
822680Sktlim@umich.edu         ThreadContext *tc)
83360SN/A{
846029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
856029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
866701Sgblack@eecs.umich.edu        int index = 0;
875958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
886701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
896029Ssteve.reinhardt@amd.com    } else {
906029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
916029Ssteve.reinhardt@amd.com        tc->halt();
922834Sksewell@umich.edu    }
93360SN/A
941458SN/A    return 1;
95360SN/A}
96360SN/A
97360SN/A
981450SN/ASyscallReturn
996109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1006109Ssanchezd@stanford.edu              ThreadContext *tc)
1016109Ssanchezd@stanford.edu{
10210483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
10310483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
10410483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
10510483Swiseveri@student.ethz.ch    }
10610483Swiseveri@student.ethz.ch
10710483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
10810483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
10910483Swiseveri@student.ethz.ch        int index = 0;
11010483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
11110483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
11210483Swiseveri@student.ethz.ch    }
1136109Ssanchezd@stanford.edu
1146109Ssanchezd@stanford.edu    return 1;
1156109Ssanchezd@stanford.edu}
1166109Ssanchezd@stanford.edu
1176109Ssanchezd@stanford.edu
1186109Ssanchezd@stanford.eduSyscallReturn
1193114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
120360SN/A{
12110318Sandreas.hansson@arm.com    return (int)PageBytes;
122360SN/A}
123360SN/A
124360SN/A
1251450SN/ASyscallReturn
1265748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
127360SN/A{
128360SN/A    // change brk addr to first arg
1296701Sgblack@eecs.umich.edu    int index = 0;
1306701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1315748SSteve.Reinhardt@amd.com
1325748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1335748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1345748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1355748SSteve.Reinhardt@amd.com        return p->brk_point;
1365748SSteve.Reinhardt@amd.com
1375748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1385748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1392474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
14010318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1415748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
14210318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1436687Stjones1@inf.ed.ac.uk
1446687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1456687Stjones1@inf.ed.ac.uk            else {
1466687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1478852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1486687Stjones1@inf.ed.ac.uk
1496687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
15010318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1516687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1528852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
15310318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1546687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1556687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1566687Stjones1@inf.ed.ac.uk                {
15710318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1588852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1596687Stjones1@inf.ed.ac.uk                }
1606687Stjones1@inf.ed.ac.uk            }
1612474SN/A        }
1621450SN/A    }
1635748SSteve.Reinhardt@amd.com
1645748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
16511380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
16611380Salexandru.dutu@amd.com                    p->brk_point);
1671458SN/A    return p->brk_point;
168360SN/A}
169360SN/A
170360SN/A
1711450SN/ASyscallReturn
1723114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
173360SN/A{
1746701Sgblack@eecs.umich.edu    int index = 0;
17510931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
17610931Sbrandon.potter@amd.com
17710932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
17810931Sbrandon.potter@amd.com    if (sim_fd < 0)
17910931Sbrandon.potter@amd.com        return -EBADF;
18010931Sbrandon.potter@amd.com
1817508Stjones1@inf.ed.ac.uk    int status = 0;
1827508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
1837508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
1841970SN/A    if (status >= 0)
18510932Sbrandon.potter@amd.com        p->resetFDEntry(tgt_fd);
1861970SN/A    return status;
187360SN/A}
188360SN/A
189360SN/A
1901450SN/ASyscallReturn
1913114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
192360SN/A{
1936701Sgblack@eecs.umich.edu    int index = 0;
19410931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
1956701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
1966701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
1976701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
198360SN/A
19910932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
20010931Sbrandon.potter@amd.com    if (sim_fd < 0)
20110931Sbrandon.potter@amd.com        return -EBADF;
20210931Sbrandon.potter@amd.com
20310931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
204360SN/A
20511684Snderumigny@gmail.com    if (bytes_read > 0)
2068706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
207360SN/A
2081458SN/A    return bytes_read;
209360SN/A}
210360SN/A
2111450SN/ASyscallReturn
2123114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
213360SN/A{
2146701Sgblack@eecs.umich.edu    int index = 0;
21510931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2166701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2176701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2186701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
219360SN/A
22010932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
22110931Sbrandon.potter@amd.com    if (sim_fd < 0)
22210931Sbrandon.potter@amd.com        return -EBADF;
22310931Sbrandon.potter@amd.com
2248706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
225360SN/A
22610931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
227360SN/A
22810931Sbrandon.potter@amd.com    fsync(sim_fd);
229360SN/A
2301458SN/A    return bytes_written;
231360SN/A}
232360SN/A
233360SN/A
2341450SN/ASyscallReturn
2353114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
236360SN/A{
2376701Sgblack@eecs.umich.edu    int index = 0;
23810931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2396701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2406701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
241360SN/A
24210932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
24310931Sbrandon.potter@amd.com    if (sim_fd < 0)
24410931Sbrandon.potter@amd.com        return -EBADF;
24510931Sbrandon.potter@amd.com
24610931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
247360SN/A
2481458SN/A    return (result == (off_t)-1) ? -errno : result;
249360SN/A}
250360SN/A
251360SN/A
2521450SN/ASyscallReturn
2534118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2544118Sgblack@eecs.umich.edu{
2556701Sgblack@eecs.umich.edu    int index = 0;
25610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2576701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2586701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2596701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2606701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2614118Sgblack@eecs.umich.edu
26210932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
26310931Sbrandon.potter@amd.com    if (sim_fd < 0)
26410931Sbrandon.potter@amd.com        return -EBADF;
26510931Sbrandon.potter@amd.com
2664118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2674118Sgblack@eecs.umich.edu
26810931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
2694118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2704118Sgblack@eecs.umich.edu
27111379Sbrandon.potter@amd.com    if (result == (off_t)-1)
2724118Sgblack@eecs.umich.edu        return -errno;
27311379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
27411379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
27511379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
27611379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
27711379Sbrandon.potter@amd.com    return 0;
2784118Sgblack@eecs.umich.edu}
2794118Sgblack@eecs.umich.edu
2804118Sgblack@eecs.umich.edu
2814118Sgblack@eecs.umich.eduSyscallReturn
2823114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
283360SN/A{
28411383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
28511383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
28611383Sbrandon.potter@amd.com    // memory.
2871458SN/A    return 0;
288360SN/A}
289360SN/A
290360SN/A
291360SN/Aconst char *hostname = "m5.eecs.umich.edu";
292360SN/A
2931450SN/ASyscallReturn
2943114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
295360SN/A{
2966701Sgblack@eecs.umich.edu    int index = 0;
2976701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2986701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
2996701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
300360SN/A
301360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
302360SN/A
3038706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
304360SN/A
3051458SN/A    return 0;
306360SN/A}
307360SN/A
3081450SN/ASyscallReturn
3095513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3105513SMichael.Adler@intel.com{
3115513SMichael.Adler@intel.com    int result = 0;
3126731Svince@csl.cornell.edu    int index = 0;
3136701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3146701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3156701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3165513SMichael.Adler@intel.com
3175513SMichael.Adler@intel.com    // Is current working directory defined?
3185513SMichael.Adler@intel.com    string cwd = p->getcwd();
3195513SMichael.Adler@intel.com    if (!cwd.empty()) {
3205513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3215513SMichael.Adler@intel.com            // Buffer too small
3225513SMichael.Adler@intel.com            return -ERANGE;
3235513SMichael.Adler@intel.com        }
3245513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3255513SMichael.Adler@intel.com        result = cwd.length();
32610955Sdavid.hashe@amd.com    } else {
3275513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3285513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
32910955Sdavid.hashe@amd.com        } else {
3305513SMichael.Adler@intel.com            result = -1;
3315513SMichael.Adler@intel.com        }
3325513SMichael.Adler@intel.com    }
3335513SMichael.Adler@intel.com
3348706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3355513SMichael.Adler@intel.com
3365513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3375513SMichael.Adler@intel.com}
3385513SMichael.Adler@intel.com
33910203SAli.Saidi@ARM.com/// Target open() handler.
34010203SAli.Saidi@ARM.comSyscallReturn
34110203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
34210203SAli.Saidi@ARM.com         ThreadContext *tc)
34310203SAli.Saidi@ARM.com{
34410203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
34510203SAli.Saidi@ARM.com}
3465513SMichael.Adler@intel.com
3475513SMichael.Adler@intel.comSyscallReturn
34810203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
34910203SAli.Saidi@ARM.com        int index)
3505513SMichael.Adler@intel.com{
3515513SMichael.Adler@intel.com    string path;
3525513SMichael.Adler@intel.com
3538852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
35410223Ssteve.reinhardt@amd.com        return -EFAULT;
3555513SMichael.Adler@intel.com
3565513SMichael.Adler@intel.com    // Adjust path for current working directory
3575513SMichael.Adler@intel.com    path = p->fullPath(path);
3585513SMichael.Adler@intel.com
3596701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3606701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3616701Sgblack@eecs.umich.edu
3626701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3635513SMichael.Adler@intel.com
36410955Sdavid.hashe@amd.com    int result = -1;
36510955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
36610955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
36710955Sdavid.hashe@amd.com    } else {
36811140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
36911140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
37011140Sjthestness@gmail.com        // LiveProcess' executable). It is possible that using this path in
37111140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
37211140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
37311140Sjthestness@gmail.com        //     called binary calls readlink().
37411140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
37511140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
37611140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
37711140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
37811140Sjthestness@gmail.com
37911140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
38011140Sjthestness@gmail.com        char real_path[PATH_MAX];
38111140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
38211140Sjthestness@gmail.com        if (!check_real_path) {
38311140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
38411140Sjthestness@gmail.com                  "executable: %s", p->progName());
38511140Sjthestness@gmail.com        }
38611140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
38711140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
38811140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
38910955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
39010955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
39110955Sdavid.hashe@amd.com            result = bufsiz;
39210955Sdavid.hashe@amd.com        } else {
39311140Sjthestness@gmail.com            result = real_path_len;
39410955Sdavid.hashe@amd.com        }
39511140Sjthestness@gmail.com
39611140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
39711140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
39811140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
39911140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
40010955Sdavid.hashe@amd.com    }
4015513SMichael.Adler@intel.com
4028706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4035513SMichael.Adler@intel.com
4045513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4055513SMichael.Adler@intel.com}
4065513SMichael.Adler@intel.com
4075513SMichael.Adler@intel.comSyscallReturn
4083114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
409511SN/A{
41010633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
41110633Smichaelupton@gmail.com}
41210633Smichaelupton@gmail.com
41310633Smichaelupton@gmail.comSyscallReturn
41410633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
41510633Smichaelupton@gmail.com           int index)
41610633Smichaelupton@gmail.com{
4171706SN/A    string path;
418360SN/A
4198852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
42010223Ssteve.reinhardt@amd.com        return -EFAULT;
421511SN/A
4223669Sbinkertn@umich.edu    // Adjust path for current working directory
4233669Sbinkertn@umich.edu    path = p->fullPath(path);
4243669Sbinkertn@umich.edu
425511SN/A    int result = unlink(path.c_str());
4261458SN/A    return (result == -1) ? -errno : result;
427511SN/A}
428511SN/A
4295513SMichael.Adler@intel.com
4305513SMichael.Adler@intel.comSyscallReturn
4315513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4325513SMichael.Adler@intel.com{
4335513SMichael.Adler@intel.com    string path;
4345513SMichael.Adler@intel.com
4356701Sgblack@eecs.umich.edu    int index = 0;
4368852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
43710223Ssteve.reinhardt@amd.com        return -EFAULT;
4385513SMichael.Adler@intel.com
4395513SMichael.Adler@intel.com    // Adjust path for current working directory
4405513SMichael.Adler@intel.com    path = p->fullPath(path);
4415513SMichael.Adler@intel.com
4426701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4435513SMichael.Adler@intel.com
4445513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4455513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4465513SMichael.Adler@intel.com}
4475513SMichael.Adler@intel.com
4481450SN/ASyscallReturn
4493114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
450511SN/A{
4511706SN/A    string old_name;
452511SN/A
4536701Sgblack@eecs.umich.edu    int index = 0;
4548852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4551458SN/A        return -EFAULT;
456511SN/A
4571706SN/A    string new_name;
458511SN/A
4598852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4601458SN/A        return -EFAULT;
461511SN/A
4623669Sbinkertn@umich.edu    // Adjust path for current working directory
4633669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4643669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4653669Sbinkertn@umich.edu
4661706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4671458SN/A    return (result == -1) ? -errno : result;
468511SN/A}
469511SN/A
4701706SN/ASyscallReturn
4713114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4721706SN/A{
4731706SN/A    string path;
4741706SN/A
4756701Sgblack@eecs.umich.edu    int index = 0;
4768852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4771706SN/A        return -EFAULT;
4781706SN/A
4796701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4801706SN/A
4813669Sbinkertn@umich.edu    // Adjust path for current working directory
4823669Sbinkertn@umich.edu    path = p->fullPath(path);
4833669Sbinkertn@umich.edu
4841706SN/A    int result = truncate(path.c_str(), length);
4851706SN/A    return (result == -1) ? -errno : result;
4861706SN/A}
4871706SN/A
4881706SN/ASyscallReturn
4896111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4906111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4911706SN/A{
4926701Sgblack@eecs.umich.edu    int index = 0;
49310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
49410931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
4951706SN/A
49610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
49710931Sbrandon.potter@amd.com    if (sim_fd < 0)
4981706SN/A        return -EBADF;
4991706SN/A
50010931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5011706SN/A    return (result == -1) ? -errno : result;
5021706SN/A}
5031999SN/A
5041999SN/ASyscallReturn
5056703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5066703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5076703Svince@csl.cornell.edu{
5086703Svince@csl.cornell.edu    int index = 0;
5096703Svince@csl.cornell.edu    string path;
5106703Svince@csl.cornell.edu
5118852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5126703Svince@csl.cornell.edu       return -EFAULT;
5136703Svince@csl.cornell.edu
5146744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5156703Svince@csl.cornell.edu
5166703Svince@csl.cornell.edu    // Adjust path for current working directory
5176703Svince@csl.cornell.edu    path = process->fullPath(path);
5186703Svince@csl.cornell.edu
5196744SAli.Saidi@arm.com#if NO_STAT64
5206744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5216744SAli.Saidi@arm.com#else
5226703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5236744SAli.Saidi@arm.com#endif
5246703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5256703Svince@csl.cornell.edu}
5266703Svince@csl.cornell.edu
5276703Svince@csl.cornell.eduSyscallReturn
5286685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5296685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5306685Stjones1@inf.ed.ac.uk{
5316701Sgblack@eecs.umich.edu    int index = 0;
53210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
53310931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5346685Stjones1@inf.ed.ac.uk
53510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
53610931Sbrandon.potter@amd.com    if (sim_fd < 0)
5376685Stjones1@inf.ed.ac.uk        return -EBADF;
5386685Stjones1@inf.ed.ac.uk
5396744SAli.Saidi@arm.com#if NO_STAT64
54010931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5416744SAli.Saidi@arm.com#else
54210931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5436744SAli.Saidi@arm.com#endif
5446685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5456685Stjones1@inf.ed.ac.uk}
5466685Stjones1@inf.ed.ac.uk
5476685Stjones1@inf.ed.ac.ukSyscallReturn
5485513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5495513SMichael.Adler@intel.com{
5505513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5515513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5525513SMichael.Adler@intel.com    // changing anything.
5535513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5545513SMichael.Adler@intel.com    umask(oldMask);
5555521Snate@binkert.org    return (int)oldMask;
5565513SMichael.Adler@intel.com}
5575513SMichael.Adler@intel.com
5585513SMichael.Adler@intel.comSyscallReturn
5593114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5601999SN/A{
5611999SN/A    string path;
5621999SN/A
5636701Sgblack@eecs.umich.edu    int index = 0;
5648852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5651999SN/A        return -EFAULT;
5661999SN/A
5671999SN/A    /* XXX endianess */
5686701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5691999SN/A    uid_t hostOwner = owner;
5706701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5711999SN/A    gid_t hostGroup = group;
5721999SN/A
5733669Sbinkertn@umich.edu    // Adjust path for current working directory
5743669Sbinkertn@umich.edu    path = p->fullPath(path);
5753669Sbinkertn@umich.edu
5761999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5771999SN/A    return (result == -1) ? -errno : result;
5781999SN/A}
5791999SN/A
5801999SN/ASyscallReturn
5813114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5821999SN/A{
5836701Sgblack@eecs.umich.edu    int index = 0;
58410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5851999SN/A
58610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
58710931Sbrandon.potter@amd.com    if (sim_fd < 0)
5881999SN/A        return -EBADF;
5891999SN/A
5901999SN/A    /* XXX endianess */
5916701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5921999SN/A    uid_t hostOwner = owner;
5936701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5941999SN/A    gid_t hostGroup = group;
5951999SN/A
59610931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
5971999SN/A    return (result == -1) ? -errno : result;
5981999SN/A}
5992093SN/A
6002093SN/A
6012093SN/ASyscallReturn
6023114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6033079Sstever@eecs.umich.edu{
6046701Sgblack@eecs.umich.edu    int index = 0;
60510781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
60610931Sbrandon.potter@amd.com
60710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
60810781Snilay@cs.wisc.edu    if (sim_fd < 0)
6093079Sstever@eecs.umich.edu        return -EBADF;
6103079Sstever@eecs.umich.edu
61110932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6125282Srstrong@cs.ucsd.edu
61310781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6146111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
61510932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6163079Sstever@eecs.umich.edu}
6173079Sstever@eecs.umich.edu
6183079Sstever@eecs.umich.edu
6193079Sstever@eecs.umich.eduSyscallReturn
6203114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6212680Sktlim@umich.edu          ThreadContext *tc)
6222093SN/A{
6236701Sgblack@eecs.umich.edu    int index = 0;
62410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6252093SN/A
62610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
62710931Sbrandon.potter@amd.com    if (sim_fd < 0)
6282093SN/A        return -EBADF;
6292093SN/A
6306701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6312093SN/A    switch (cmd) {
6322093SN/A      case 0: // F_DUPFD
6332093SN/A        // if we really wanted to support this, we'd need to do it
6342093SN/A        // in the target fd space.
63510931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6362093SN/A        return -EMFILE;
6372093SN/A
6382093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6392093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6402093SN/A        return 0;
6412093SN/A
6422093SN/A      case 3: // F_GETFL (get file flags)
6432093SN/A      case 4: // F_SETFL (set file flags)
6442093SN/A        // not sure if this is totally valid, but we'll pass it through
6452093SN/A        // to the underlying OS
64610931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
64710931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6482093SN/A        // return 0;
6492093SN/A
6502093SN/A      case 7: // F_GETLK  (get lock)
6512093SN/A      case 8: // F_SETLK  (set lock)
6522093SN/A      case 9: // F_SETLKW (set lock and wait)
6532093SN/A        // don't mess with file locking... just act like it's OK
65410931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6552093SN/A        return 0;
6562093SN/A
6572093SN/A      default:
6582093SN/A        warn("Unknown fcntl command %d\n", cmd);
6592093SN/A        return 0;
6602093SN/A    }
6612093SN/A}
6622093SN/A
6632238SN/ASyscallReturn
6643114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6652687Sksewell@umich.edu            ThreadContext *tc)
6662687Sksewell@umich.edu{
6676701Sgblack@eecs.umich.edu    int index = 0;
66810931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6692687Sksewell@umich.edu
67010932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
67110931Sbrandon.potter@amd.com    if (sim_fd < 0)
6722687Sksewell@umich.edu        return -EBADF;
6732687Sksewell@umich.edu
6746701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6752687Sksewell@umich.edu    switch (cmd) {
6762687Sksewell@umich.edu      case 33: //F_GETLK64
67710931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
6782687Sksewell@umich.edu        return -EMFILE;
6792687Sksewell@umich.edu
6802687Sksewell@umich.edu      case 34: // F_SETLK64
6812687Sksewell@umich.edu      case 35: // F_SETLKW64
68210931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
68310931Sbrandon.potter@amd.com             tgt_fd);
6842687Sksewell@umich.edu        return -EMFILE;
6852687Sksewell@umich.edu
6862687Sksewell@umich.edu      default:
6872687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6882687Sksewell@umich.edu        // to the underlying OS
68910931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
69010931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6912687Sksewell@umich.edu        // return 0;
6922687Sksewell@umich.edu    }
6932687Sksewell@umich.edu}
6942687Sksewell@umich.edu
6952687Sksewell@umich.eduSyscallReturn
6963114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6972680Sktlim@umich.edu         ThreadContext *tc)
6982238SN/A{
6992238SN/A    int fds[2], sim_fds[2];
7002238SN/A    int pipe_retval = pipe(fds);
7012093SN/A
7022238SN/A    if (pipe_retval < 0) {
7032238SN/A        // error
7042238SN/A        return pipe_retval;
7052238SN/A    }
7062238SN/A
70710932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
70810932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7092238SN/A
7105282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7112238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7122238SN/A    // the return value of the function, and fd[1] is returned in r20.
7132680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7142238SN/A    return sim_fds[0];
7152238SN/A}
7162238SN/A
7172238SN/A
7182238SN/ASyscallReturn
7193114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7202680Sktlim@umich.edu           ThreadContext *tc)
7212238SN/A{
7222238SN/A    // Make up a PID.  There's no interprocess communication in
7232238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7242238SN/A    // not getting a unique value.
7252238SN/A
7263114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7273114Sgblack@eecs.umich.edu    return process->pid();
7282238SN/A}
7292238SN/A
7302238SN/A
7312238SN/ASyscallReturn
7323114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7332680Sktlim@umich.edu           ThreadContext *tc)
7342238SN/A{
7352238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7362238SN/A    // simulation to be deterministic.
7372238SN/A
7382238SN/A    // EUID goes in r20.
7393114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7405543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7412238SN/A}
7422238SN/A
7432238SN/A
7442238SN/ASyscallReturn
7453114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7462680Sktlim@umich.edu           ThreadContext *tc)
7472238SN/A{
7482238SN/A    // Get current group ID.  EGID goes in r20.
7493114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7503114Sgblack@eecs.umich.edu    return process->gid();
7512238SN/A}
7522238SN/A
7532238SN/A
7542238SN/ASyscallReturn
7553114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7562680Sktlim@umich.edu           ThreadContext *tc)
7572238SN/A{
7582238SN/A    // can't fathom why a benchmark would call this.
7596701Sgblack@eecs.umich.edu    int index = 0;
7606701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7612238SN/A    return 0;
7622238SN/A}
7632238SN/A
7642238SN/ASyscallReturn
7653114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7662680Sktlim@umich.edu           ThreadContext *tc)
7672238SN/A{
7682238SN/A    // Make up a PID.  There's no interprocess communication in
7692238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7702238SN/A    // not getting a unique value.
7712238SN/A
7723114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7733114Sgblack@eecs.umich.edu    return process->pid();
7742238SN/A}
7752238SN/A
7762238SN/ASyscallReturn
7773114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7782680Sktlim@umich.edu           ThreadContext *tc)
7792238SN/A{
7803114Sgblack@eecs.umich.edu    return process->ppid();
7812238SN/A}
7822238SN/A
7832238SN/ASyscallReturn
7843114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7852680Sktlim@umich.edu           ThreadContext *tc)
7862238SN/A{
7875543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7882238SN/A}
7892238SN/A
7902238SN/ASyscallReturn
7913114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7922680Sktlim@umich.edu           ThreadContext *tc)
7932238SN/A{
7945543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7952238SN/A}
7962238SN/A
7972238SN/ASyscallReturn
7983114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7992680Sktlim@umich.edu           ThreadContext *tc)
8002238SN/A{
8013114Sgblack@eecs.umich.edu    return process->gid();
8022238SN/A}
8032238SN/A
8042238SN/ASyscallReturn
8053114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8062680Sktlim@umich.edu           ThreadContext *tc)
8072238SN/A{
8083114Sgblack@eecs.umich.edu    return process->egid();
8092238SN/A}
8102238SN/A
8112238SN/A
8126109Ssanchezd@stanford.eduSyscallReturn
8136109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8146109Ssanchezd@stanford.edu           ThreadContext *tc)
8156109Ssanchezd@stanford.edu{
8166701Sgblack@eecs.umich.edu    int index = 0;
8176701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8186701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8196701Sgblack@eecs.umich.edu
8206109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8216701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8226701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8236109Ssanchezd@stanford.edu
8246109Ssanchezd@stanford.edu
8256701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8266111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8276111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8286111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8296701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8306109Ssanchezd@stanford.edu    }
8316109Ssanchezd@stanford.edu
8326111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8336109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8346109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8356109Ssanchezd@stanford.edu
8366109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8376109Ssanchezd@stanford.edu
8386111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8396109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8406111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8416109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8426109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8436109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8446109Ssanchezd@stanford.edu
8456111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8466109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8476109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8486109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8496109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8506337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8516109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8526109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8539375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8546109Ssanchezd@stanford.edu
8556109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8566109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8578149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8588149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8596109Ssanchezd@stanford.edu        #else
8606109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8616109Ssanchezd@stanford.edu        #endif
8626109Ssanchezd@stanford.edu
8636111Ssteve.reinhardt@amd.com        // Set up stack register
8646701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8656109Ssanchezd@stanford.edu
8666111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8676111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8686109Ssanchezd@stanford.edu
8696111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8706109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8716110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8726109Ssanchezd@stanford.edu        #endif
8736109Ssanchezd@stanford.edu
8746111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8756111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8766109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8776109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8786109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8796109Ssanchezd@stanford.edu        #endif
8806109Ssanchezd@stanford.edu
8817720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8826109Ssanchezd@stanford.edu
8836109Ssanchezd@stanford.edu        ctc->activate();
8846109Ssanchezd@stanford.edu
8856109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8866109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8876109Ssanchezd@stanford.edu        return 1;
8886109Ssanchezd@stanford.edu    } else {
8896109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8906109Ssanchezd@stanford.edu        return 0;
8916109Ssanchezd@stanford.edu    }
8926109Ssanchezd@stanford.edu}
8936109Ssanchezd@stanford.edu
8949455Smitch.hayenga+gem5@gmail.comSyscallReturn
89511760Sbrandon.potter@amd.comfallocateFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
89611760Sbrandon.potter@amd.com              ThreadContext *tc)
89711760Sbrandon.potter@amd.com{
89811799Sbrandon.potter@amd.com#if NO_FALLOCATE
89911799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
90011799Sbrandon.potter@amd.com#else
90111760Sbrandon.potter@amd.com    int index = 0;
90211760Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
90311760Sbrandon.potter@amd.com    int mode = process->getSyscallArg(tc, index);
90411760Sbrandon.potter@amd.com    off_t offset = process->getSyscallArg(tc, index);
90511760Sbrandon.potter@amd.com    off_t len = process->getSyscallArg(tc, index);
90611760Sbrandon.potter@amd.com
90711760Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
90811760Sbrandon.potter@amd.com    if (sim_fd < 0)
90911760Sbrandon.potter@amd.com        return -EBADF;
91011760Sbrandon.potter@amd.com
91111760Sbrandon.potter@amd.com    int result = fallocate(sim_fd, mode, offset, len);
91211760Sbrandon.potter@amd.com    if (result < 0)
91311760Sbrandon.potter@amd.com        return -errno;
91411799Sbrandon.potter@amd.com#endif
91511760Sbrandon.potter@amd.com    return 0;
91611760Sbrandon.potter@amd.com}
91711760Sbrandon.potter@amd.com
91811760Sbrandon.potter@amd.comSyscallReturn
91910203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
92010203SAli.Saidi@ARM.com        int index)
9219455Smitch.hayenga+gem5@gmail.com{
9229455Smitch.hayenga+gem5@gmail.com    string path;
9239455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
92410223Ssteve.reinhardt@amd.com        return -EFAULT;
9259455Smitch.hayenga+gem5@gmail.com
9269455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9279455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9289455Smitch.hayenga+gem5@gmail.com
9299455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9309455Smitch.hayenga+gem5@gmail.com
9319455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9329455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9339455Smitch.hayenga+gem5@gmail.com}
93410203SAli.Saidi@ARM.com
93510203SAli.Saidi@ARM.comSyscallReturn
93610203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
93710203SAli.Saidi@ARM.com{
93810203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
93910203SAli.Saidi@ARM.com}
94010203SAli.Saidi@ARM.com
941