syscall_emul.cc revision 11383
1360SN/A/*
21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
3360SN/A * All rights reserved.
4360SN/A *
5360SN/A * Redistribution and use in source and binary forms, with or without
6360SN/A * modification, are permitted provided that the following conditions are
7360SN/A * met: redistributions of source code must retain the above copyright
8360SN/A * notice, this list of conditions and the following disclaimer;
9360SN/A * redistributions in binary form must reproduce the above copyright
10360SN/A * notice, this list of conditions and the following disclaimer in the
11360SN/A * documentation and/or other materials provided with the distribution;
12360SN/A * neither the name of the copyright holders nor the names of its
13360SN/A * contributors may be used to endorse or promote products derived from
14360SN/A * this software without specific prior written permission.
15360SN/A *
16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
30360SN/A */
31360SN/A
322093SN/A#include <fcntl.h>
33360SN/A#include <unistd.h>
34360SN/A
356712Snate@binkert.org#include <cstdio>
366712Snate@binkert.org#include <iostream>
37360SN/A#include <string>
38360SN/A
397680Sgblack@eecs.umich.edu#include "arch/utility.hh"
402474SN/A#include "base/chunk_generator.hh"
41360SN/A#include "base/trace.hh"
426658Snate@binkert.org#include "config/the_isa.hh"
438229Snate@binkert.org#include "cpu/base.hh"
442680Sktlim@umich.edu#include "cpu/thread_context.hh"
4511380Salexandru.dutu@amd.com#include "debug/SyscallBase.hh"
468232Snate@binkert.org#include "debug/SyscallVerbose.hh"
472474SN/A#include "mem/page_table.hh"
48360SN/A#include "sim/process.hh"
498229Snate@binkert.org#include "sim/sim_exit.hh"
508229Snate@binkert.org#include "sim/syscall_emul.hh"
516029Ssteve.reinhardt@amd.com#include "sim/system.hh"
52360SN/A
53360SN/Ausing namespace std;
542107SN/Ausing namespace TheISA;
55360SN/A
56360SN/Avoid
573114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
58360SN/A{
5911380Salexandru.dutu@amd.com    if (DTRACE(SyscallBase)) {
6010253Ssteve.reinhardt@amd.com        int index = 0;
6111380Salexandru.dutu@amd.com        IntReg arg[6] M5_VAR_USED;
6210253Ssteve.reinhardt@amd.com
6310253Ssteve.reinhardt@amd.com        // we can't just put the calls to getSyscallArg() in the
6410253Ssteve.reinhardt@amd.com        // DPRINTF arg list, because C++ doesn't guarantee their order
6511380Salexandru.dutu@amd.com        for (int i = 0; i < 6; ++i)
6610253Ssteve.reinhardt@amd.com            arg[i] = process->getSyscallArg(tc, index);
6710253Ssteve.reinhardt@amd.com
6811380Salexandru.dutu@amd.com        // Linux supports up to six system call arguments through registers
6911380Salexandru.dutu@amd.com        // so we want to print all six. Check to the relevant man page to
7011380Salexandru.dutu@amd.com        // verify how many are actually used by a given system call.
7111380Salexandru.dutu@amd.com        DPRINTF_SYSCALL(Base,
7211380Salexandru.dutu@amd.com                        "%s called w/arguments %d, %d, %d, %d, %d, %d\n",
7311380Salexandru.dutu@amd.com                        name, arg[0], arg[1], arg[2], arg[3], arg[4],
7411380Salexandru.dutu@amd.com                        arg[5]);
7510253Ssteve.reinhardt@amd.com    }
76360SN/A
772680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
78360SN/A
7910500Ssteve.reinhardt@amd.com    if (retval.needsRetry()) {
8011380Salexandru.dutu@amd.com        DPRINTF_SYSCALL(Base, "%s needs retry\n", name);
8110500Ssteve.reinhardt@amd.com    } else {
8211380Salexandru.dutu@amd.com        DPRINTF_SYSCALL(Base, "%s returns %d\n", name,
8311380Salexandru.dutu@amd.com                        retval.encodedValue());
8410500Ssteve.reinhardt@amd.com    }
85360SN/A
8610500Ssteve.reinhardt@amd.com    if (!(flags & SyscallDesc::SuppressReturnValue) && !retval.needsRetry())
875958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
88360SN/A}
89360SN/A
90360SN/A
911450SN/ASyscallReturn
923114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
932680Sktlim@umich.edu                  ThreadContext *tc)
94360SN/A{
951969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
962484SN/A
972484SN/A    return 1;
98360SN/A}
99360SN/A
100360SN/A
1011450SN/ASyscallReturn
1023114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1032680Sktlim@umich.edu           ThreadContext *tc)
104360SN/A{
1056701Sgblack@eecs.umich.edu    int index = 0;
10610831Ssteve.reinhardt@amd.com    const char *extra_text = "";
107360SN/A
10810831Ssteve.reinhardt@amd.com    if (desc->warnOnce()) {
10910831Ssteve.reinhardt@amd.com        if (desc->warned)
11010831Ssteve.reinhardt@amd.com            return 0;
111360SN/A
11210831Ssteve.reinhardt@amd.com        desc->warned = true;
11310831Ssteve.reinhardt@amd.com        extra_text = "\n      (further warnings will be suppressed)";
11410831Ssteve.reinhardt@amd.com    }
115360SN/A
11610831Ssteve.reinhardt@amd.com    warn("ignoring syscall %s(%d, ...)%s", desc->name,
11710831Ssteve.reinhardt@amd.com         process->getSyscallArg(tc, index), extra_text);
1188149SChris.Emmons@ARM.com
1198149SChris.Emmons@ARM.com    return 0;
1208149SChris.Emmons@ARM.com}
1218149SChris.Emmons@ARM.com
1228149SChris.Emmons@ARM.com
1238149SChris.Emmons@ARM.comSyscallReturn
1243114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1252680Sktlim@umich.edu         ThreadContext *tc)
126360SN/A{
1276029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1286029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1296701Sgblack@eecs.umich.edu        int index = 0;
1305958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1316701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1326029Ssteve.reinhardt@amd.com    } else {
1336029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1346029Ssteve.reinhardt@amd.com        tc->halt();
1352834Sksewell@umich.edu    }
136360SN/A
1371458SN/A    return 1;
138360SN/A}
139360SN/A
140360SN/A
1411450SN/ASyscallReturn
1426109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1436109Ssanchezd@stanford.edu              ThreadContext *tc)
1446109Ssanchezd@stanford.edu{
14510483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
14610483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
14710483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
14810483Swiseveri@student.ethz.ch    }
14910483Swiseveri@student.ethz.ch
15010483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
15110483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
15210483Swiseveri@student.ethz.ch        int index = 0;
15310483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
15410483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
15510483Swiseveri@student.ethz.ch    }
1566109Ssanchezd@stanford.edu
1576109Ssanchezd@stanford.edu    return 1;
1586109Ssanchezd@stanford.edu}
1596109Ssanchezd@stanford.edu
1606109Ssanchezd@stanford.edu
1616109Ssanchezd@stanford.eduSyscallReturn
1623114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
163360SN/A{
16410318Sandreas.hansson@arm.com    return (int)PageBytes;
165360SN/A}
166360SN/A
167360SN/A
1681450SN/ASyscallReturn
1695748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
170360SN/A{
171360SN/A    // change brk addr to first arg
1726701Sgblack@eecs.umich.edu    int index = 0;
1736701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1745748SSteve.Reinhardt@amd.com
1755748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1765748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1775748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1785748SSteve.Reinhardt@amd.com        return p->brk_point;
1795748SSteve.Reinhardt@amd.com
1805748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1815748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1822474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
18310318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1845748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
18510318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1866687Stjones1@inf.ed.ac.uk
1876687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1886687Stjones1@inf.ed.ac.uk            else {
1896687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1908852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1916687Stjones1@inf.ed.ac.uk
1926687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
19310318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1946687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1958852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
19610318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1976687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1986687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1996687Stjones1@inf.ed.ac.uk                {
20010318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
2018852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
2026687Stjones1@inf.ed.ac.uk                }
2036687Stjones1@inf.ed.ac.uk            }
2042474SN/A        }
2051450SN/A    }
2065748SSteve.Reinhardt@amd.com
2075748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
20811380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
20911380Salexandru.dutu@amd.com                    p->brk_point);
2101458SN/A    return p->brk_point;
211360SN/A}
212360SN/A
213360SN/A
2141450SN/ASyscallReturn
2153114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
216360SN/A{
2176701Sgblack@eecs.umich.edu    int index = 0;
21810931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
21910931Sbrandon.potter@amd.com
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
2247508Stjones1@inf.ed.ac.uk    int status = 0;
2257508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2267508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2271970SN/A    if (status >= 0)
22810932Sbrandon.potter@amd.com        p->resetFDEntry(tgt_fd);
2291970SN/A    return status;
230360SN/A}
231360SN/A
232360SN/A
2331450SN/ASyscallReturn
2343114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
235360SN/A{
2366701Sgblack@eecs.umich.edu    int index = 0;
23710931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2386701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2396701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2406701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
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    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
247360SN/A
248360SN/A    if (bytes_read != -1)
2498706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
250360SN/A
2511458SN/A    return bytes_read;
252360SN/A}
253360SN/A
2541450SN/ASyscallReturn
2553114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
256360SN/A{
2576701Sgblack@eecs.umich.edu    int index = 0;
25810931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2596701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2606701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2616701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
262360SN/A
26310932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
26410931Sbrandon.potter@amd.com    if (sim_fd < 0)
26510931Sbrandon.potter@amd.com        return -EBADF;
26610931Sbrandon.potter@amd.com
2678706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
268360SN/A
26910931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
270360SN/A
27110931Sbrandon.potter@amd.com    fsync(sim_fd);
272360SN/A
2731458SN/A    return bytes_written;
274360SN/A}
275360SN/A
276360SN/A
2771450SN/ASyscallReturn
2783114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
279360SN/A{
2806701Sgblack@eecs.umich.edu    int index = 0;
28110931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2826701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2836701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
284360SN/A
28510932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
28610931Sbrandon.potter@amd.com    if (sim_fd < 0)
28710931Sbrandon.potter@amd.com        return -EBADF;
28810931Sbrandon.potter@amd.com
28910931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
290360SN/A
2911458SN/A    return (result == (off_t)-1) ? -errno : result;
292360SN/A}
293360SN/A
294360SN/A
2951450SN/ASyscallReturn
2964118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2974118Sgblack@eecs.umich.edu{
2986701Sgblack@eecs.umich.edu    int index = 0;
29910931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
3006701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
3016701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
3026701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
3036701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
3044118Sgblack@eecs.umich.edu
30510932Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
30610931Sbrandon.potter@amd.com    if (sim_fd < 0)
30710931Sbrandon.potter@amd.com        return -EBADF;
30810931Sbrandon.potter@amd.com
3094118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
3104118Sgblack@eecs.umich.edu
31110931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
3124118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
3134118Sgblack@eecs.umich.edu
31411379Sbrandon.potter@amd.com    if (result == (off_t)-1)
3154118Sgblack@eecs.umich.edu        return -errno;
31611379Sbrandon.potter@amd.com    // Assuming that the size of loff_t is 64 bits on the target platform
31711379Sbrandon.potter@amd.com    BufferArg result_buf(result_ptr, sizeof(result));
31811379Sbrandon.potter@amd.com    memcpy(result_buf.bufferPtr(), &result, sizeof(result));
31911379Sbrandon.potter@amd.com    result_buf.copyOut(tc->getMemProxy());
32011379Sbrandon.potter@amd.com    return 0;
3214118Sgblack@eecs.umich.edu}
3224118Sgblack@eecs.umich.edu
3234118Sgblack@eecs.umich.edu
3244118Sgblack@eecs.umich.eduSyscallReturn
3253114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
326360SN/A{
32711383Sbrandon.potter@amd.com    // With mmap more fully implemented, it might be worthwhile to bite
32811383Sbrandon.potter@amd.com    // the bullet and implement munmap. Should allow us to reuse simulated
32911383Sbrandon.potter@amd.com    // memory.
3301458SN/A    return 0;
331360SN/A}
332360SN/A
333360SN/A
334360SN/Aconst char *hostname = "m5.eecs.umich.edu";
335360SN/A
3361450SN/ASyscallReturn
3373114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
338360SN/A{
3396701Sgblack@eecs.umich.edu    int index = 0;
3406701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3416701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3426701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
343360SN/A
344360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
345360SN/A
3468706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
347360SN/A
3481458SN/A    return 0;
349360SN/A}
350360SN/A
3511450SN/ASyscallReturn
3525513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3535513SMichael.Adler@intel.com{
3545513SMichael.Adler@intel.com    int result = 0;
3556731Svince@csl.cornell.edu    int index = 0;
3566701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3576701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3586701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3595513SMichael.Adler@intel.com
3605513SMichael.Adler@intel.com    // Is current working directory defined?
3615513SMichael.Adler@intel.com    string cwd = p->getcwd();
3625513SMichael.Adler@intel.com    if (!cwd.empty()) {
3635513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3645513SMichael.Adler@intel.com            // Buffer too small
3655513SMichael.Adler@intel.com            return -ERANGE;
3665513SMichael.Adler@intel.com        }
3675513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3685513SMichael.Adler@intel.com        result = cwd.length();
36910955Sdavid.hashe@amd.com    } else {
3705513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3715513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
37210955Sdavid.hashe@amd.com        } else {
3735513SMichael.Adler@intel.com            result = -1;
3745513SMichael.Adler@intel.com        }
3755513SMichael.Adler@intel.com    }
3765513SMichael.Adler@intel.com
3778706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3785513SMichael.Adler@intel.com
3795513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3805513SMichael.Adler@intel.com}
3815513SMichael.Adler@intel.com
38210203SAli.Saidi@ARM.com/// Target open() handler.
38310203SAli.Saidi@ARM.comSyscallReturn
38410203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
38510203SAli.Saidi@ARM.com         ThreadContext *tc)
38610203SAli.Saidi@ARM.com{
38710203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
38810203SAli.Saidi@ARM.com}
3895513SMichael.Adler@intel.com
3905513SMichael.Adler@intel.comSyscallReturn
39110203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
39210203SAli.Saidi@ARM.com        int index)
3935513SMichael.Adler@intel.com{
3945513SMichael.Adler@intel.com    string path;
3955513SMichael.Adler@intel.com
3968852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
39710223Ssteve.reinhardt@amd.com        return -EFAULT;
3985513SMichael.Adler@intel.com
3995513SMichael.Adler@intel.com    // Adjust path for current working directory
4005513SMichael.Adler@intel.com    path = p->fullPath(path);
4015513SMichael.Adler@intel.com
4026701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
4036701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
4046701Sgblack@eecs.umich.edu
4056701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
4065513SMichael.Adler@intel.com
40710955Sdavid.hashe@amd.com    int result = -1;
40810955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
40910955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
41010955Sdavid.hashe@amd.com    } else {
41111140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
41211140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
41311140Sjthestness@gmail.com        // LiveProcess' executable). It is possible that using this path in
41411140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
41511140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
41611140Sjthestness@gmail.com        //     called binary calls readlink().
41711140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
41811140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
41911140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
42011140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
42111140Sjthestness@gmail.com
42211140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
42311140Sjthestness@gmail.com        char real_path[PATH_MAX];
42411140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
42511140Sjthestness@gmail.com        if (!check_real_path) {
42611140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
42711140Sjthestness@gmail.com                  "executable: %s", p->progName());
42811140Sjthestness@gmail.com        }
42911140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
43011140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
43111140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
43210955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
43310955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
43410955Sdavid.hashe@amd.com            result = bufsiz;
43510955Sdavid.hashe@amd.com        } else {
43611140Sjthestness@gmail.com            result = real_path_len;
43710955Sdavid.hashe@amd.com        }
43811140Sjthestness@gmail.com
43911140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
44011140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
44111140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
44211140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
44310955Sdavid.hashe@amd.com    }
4445513SMichael.Adler@intel.com
4458706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4465513SMichael.Adler@intel.com
4475513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4485513SMichael.Adler@intel.com}
4495513SMichael.Adler@intel.com
4505513SMichael.Adler@intel.comSyscallReturn
4513114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
452511SN/A{
45310633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
45410633Smichaelupton@gmail.com}
45510633Smichaelupton@gmail.com
45610633Smichaelupton@gmail.comSyscallReturn
45710633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
45810633Smichaelupton@gmail.com           int index)
45910633Smichaelupton@gmail.com{
4601706SN/A    string path;
461360SN/A
4628852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46310223Ssteve.reinhardt@amd.com        return -EFAULT;
464511SN/A
4653669Sbinkertn@umich.edu    // Adjust path for current working directory
4663669Sbinkertn@umich.edu    path = p->fullPath(path);
4673669Sbinkertn@umich.edu
468511SN/A    int result = unlink(path.c_str());
4691458SN/A    return (result == -1) ? -errno : result;
470511SN/A}
471511SN/A
4725513SMichael.Adler@intel.com
4735513SMichael.Adler@intel.comSyscallReturn
4745513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4755513SMichael.Adler@intel.com{
4765513SMichael.Adler@intel.com    string path;
4775513SMichael.Adler@intel.com
4786701Sgblack@eecs.umich.edu    int index = 0;
4798852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
48010223Ssteve.reinhardt@amd.com        return -EFAULT;
4815513SMichael.Adler@intel.com
4825513SMichael.Adler@intel.com    // Adjust path for current working directory
4835513SMichael.Adler@intel.com    path = p->fullPath(path);
4845513SMichael.Adler@intel.com
4856701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4865513SMichael.Adler@intel.com
4875513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4885513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4895513SMichael.Adler@intel.com}
4905513SMichael.Adler@intel.com
4911450SN/ASyscallReturn
4923114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
493511SN/A{
4941706SN/A    string old_name;
495511SN/A
4966701Sgblack@eecs.umich.edu    int index = 0;
4978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4981458SN/A        return -EFAULT;
499511SN/A
5001706SN/A    string new_name;
501511SN/A
5028852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
5031458SN/A        return -EFAULT;
504511SN/A
5053669Sbinkertn@umich.edu    // Adjust path for current working directory
5063669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
5073669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
5083669Sbinkertn@umich.edu
5091706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
5101458SN/A    return (result == -1) ? -errno : result;
511511SN/A}
512511SN/A
5131706SN/ASyscallReturn
5143114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5151706SN/A{
5161706SN/A    string path;
5171706SN/A
5186701Sgblack@eecs.umich.edu    int index = 0;
5198852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5201706SN/A        return -EFAULT;
5211706SN/A
5226701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5231706SN/A
5243669Sbinkertn@umich.edu    // Adjust path for current working directory
5253669Sbinkertn@umich.edu    path = p->fullPath(path);
5263669Sbinkertn@umich.edu
5271706SN/A    int result = truncate(path.c_str(), length);
5281706SN/A    return (result == -1) ? -errno : result;
5291706SN/A}
5301706SN/A
5311706SN/ASyscallReturn
5326111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
5336111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
5341706SN/A{
5356701Sgblack@eecs.umich.edu    int index = 0;
53610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
53710931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
5381706SN/A
53910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
54010931Sbrandon.potter@amd.com    if (sim_fd < 0)
5411706SN/A        return -EBADF;
5421706SN/A
54310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5441706SN/A    return (result == -1) ? -errno : result;
5451706SN/A}
5461999SN/A
5471999SN/ASyscallReturn
5486703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5496703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5506703Svince@csl.cornell.edu{
5516703Svince@csl.cornell.edu    int index = 0;
5526703Svince@csl.cornell.edu    string path;
5536703Svince@csl.cornell.edu
5548852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5556703Svince@csl.cornell.edu       return -EFAULT;
5566703Svince@csl.cornell.edu
5576744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5586703Svince@csl.cornell.edu
5596703Svince@csl.cornell.edu    // Adjust path for current working directory
5606703Svince@csl.cornell.edu    path = process->fullPath(path);
5616703Svince@csl.cornell.edu
5626744SAli.Saidi@arm.com#if NO_STAT64
5636744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5646744SAli.Saidi@arm.com#else
5656703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5666744SAli.Saidi@arm.com#endif
5676703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5686703Svince@csl.cornell.edu}
5696703Svince@csl.cornell.edu
5706703Svince@csl.cornell.eduSyscallReturn
5716685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5726685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5736685Stjones1@inf.ed.ac.uk{
5746701Sgblack@eecs.umich.edu    int index = 0;
57510931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
57610931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5776685Stjones1@inf.ed.ac.uk
57810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
57910931Sbrandon.potter@amd.com    if (sim_fd < 0)
5806685Stjones1@inf.ed.ac.uk        return -EBADF;
5816685Stjones1@inf.ed.ac.uk
5826744SAli.Saidi@arm.com#if NO_STAT64
58310931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5846744SAli.Saidi@arm.com#else
58510931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5866744SAli.Saidi@arm.com#endif
5876685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5886685Stjones1@inf.ed.ac.uk}
5896685Stjones1@inf.ed.ac.uk
5906685Stjones1@inf.ed.ac.ukSyscallReturn
5915513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5925513SMichael.Adler@intel.com{
5935513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5945513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5955513SMichael.Adler@intel.com    // changing anything.
5965513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5975513SMichael.Adler@intel.com    umask(oldMask);
5985521Snate@binkert.org    return (int)oldMask;
5995513SMichael.Adler@intel.com}
6005513SMichael.Adler@intel.com
6015513SMichael.Adler@intel.comSyscallReturn
6023114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
6031999SN/A{
6041999SN/A    string path;
6051999SN/A
6066701Sgblack@eecs.umich.edu    int index = 0;
6078852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6081999SN/A        return -EFAULT;
6091999SN/A
6101999SN/A    /* XXX endianess */
6116701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
6121999SN/A    uid_t hostOwner = owner;
6136701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6141999SN/A    gid_t hostGroup = group;
6151999SN/A
6163669Sbinkertn@umich.edu    // Adjust path for current working directory
6173669Sbinkertn@umich.edu    path = p->fullPath(path);
6183669Sbinkertn@umich.edu
6191999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6201999SN/A    return (result == -1) ? -errno : result;
6211999SN/A}
6221999SN/A
6231999SN/ASyscallReturn
6243114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6251999SN/A{
6266701Sgblack@eecs.umich.edu    int index = 0;
62710931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6281999SN/A
62910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
63010931Sbrandon.potter@amd.com    if (sim_fd < 0)
6311999SN/A        return -EBADF;
6321999SN/A
6331999SN/A    /* XXX endianess */
6346701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
6351999SN/A    uid_t hostOwner = owner;
6366701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
6371999SN/A    gid_t hostGroup = group;
6381999SN/A
63910931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6401999SN/A    return (result == -1) ? -errno : result;
6411999SN/A}
6422093SN/A
6432093SN/A
6442093SN/ASyscallReturn
6453114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6463079Sstever@eecs.umich.edu{
6476701Sgblack@eecs.umich.edu    int index = 0;
64810781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
64910931Sbrandon.potter@amd.com
65010932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
65110781Snilay@cs.wisc.edu    if (sim_fd < 0)
6523079Sstever@eecs.umich.edu        return -EBADF;
6533079Sstever@eecs.umich.edu
65410932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6555282Srstrong@cs.ucsd.edu
65610781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6576111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
65810932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6593079Sstever@eecs.umich.edu}
6603079Sstever@eecs.umich.edu
6613079Sstever@eecs.umich.edu
6623079Sstever@eecs.umich.eduSyscallReturn
6633114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6642680Sktlim@umich.edu          ThreadContext *tc)
6652093SN/A{
6666701Sgblack@eecs.umich.edu    int index = 0;
66710931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6682093SN/A
66910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
67010931Sbrandon.potter@amd.com    if (sim_fd < 0)
6712093SN/A        return -EBADF;
6722093SN/A
6736701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6742093SN/A    switch (cmd) {
6752093SN/A      case 0: // F_DUPFD
6762093SN/A        // if we really wanted to support this, we'd need to do it
6772093SN/A        // in the target fd space.
67810931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6792093SN/A        return -EMFILE;
6802093SN/A
6812093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6822093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6832093SN/A        return 0;
6842093SN/A
6852093SN/A      case 3: // F_GETFL (get file flags)
6862093SN/A      case 4: // F_SETFL (set file flags)
6872093SN/A        // not sure if this is totally valid, but we'll pass it through
6882093SN/A        // to the underlying OS
68910931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
69010931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6912093SN/A        // return 0;
6922093SN/A
6932093SN/A      case 7: // F_GETLK  (get lock)
6942093SN/A      case 8: // F_SETLK  (set lock)
6952093SN/A      case 9: // F_SETLKW (set lock and wait)
6962093SN/A        // don't mess with file locking... just act like it's OK
69710931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6982093SN/A        return 0;
6992093SN/A
7002093SN/A      default:
7012093SN/A        warn("Unknown fcntl command %d\n", cmd);
7022093SN/A        return 0;
7032093SN/A    }
7042093SN/A}
7052093SN/A
7062238SN/ASyscallReturn
7073114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
7082687Sksewell@umich.edu            ThreadContext *tc)
7092687Sksewell@umich.edu{
7106701Sgblack@eecs.umich.edu    int index = 0;
71110931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
7122687Sksewell@umich.edu
71310932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
71410931Sbrandon.potter@amd.com    if (sim_fd < 0)
7152687Sksewell@umich.edu        return -EBADF;
7162687Sksewell@umich.edu
7176701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
7182687Sksewell@umich.edu    switch (cmd) {
7192687Sksewell@umich.edu      case 33: //F_GETLK64
72010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7212687Sksewell@umich.edu        return -EMFILE;
7222687Sksewell@umich.edu
7232687Sksewell@umich.edu      case 34: // F_SETLK64
7242687Sksewell@umich.edu      case 35: // F_SETLKW64
72510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
72610931Sbrandon.potter@amd.com             tgt_fd);
7272687Sksewell@umich.edu        return -EMFILE;
7282687Sksewell@umich.edu
7292687Sksewell@umich.edu      default:
7302687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7312687Sksewell@umich.edu        // to the underlying OS
73210931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
73310931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7342687Sksewell@umich.edu        // return 0;
7352687Sksewell@umich.edu    }
7362687Sksewell@umich.edu}
7372687Sksewell@umich.edu
7382687Sksewell@umich.eduSyscallReturn
7393114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7402680Sktlim@umich.edu         ThreadContext *tc)
7412238SN/A{
7422238SN/A    int fds[2], sim_fds[2];
7432238SN/A    int pipe_retval = pipe(fds);
7442093SN/A
7452238SN/A    if (pipe_retval < 0) {
7462238SN/A        // error
7472238SN/A        return pipe_retval;
7482238SN/A    }
7492238SN/A
75010932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
75110932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7522238SN/A
7535282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7542238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7552238SN/A    // the return value of the function, and fd[1] is returned in r20.
7562680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7572238SN/A    return sim_fds[0];
7582238SN/A}
7592238SN/A
7602238SN/A
7612238SN/ASyscallReturn
7623114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7632680Sktlim@umich.edu           ThreadContext *tc)
7642238SN/A{
7652238SN/A    // Make up a PID.  There's no interprocess communication in
7662238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7672238SN/A    // not getting a unique value.
7682238SN/A
7693114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7703114Sgblack@eecs.umich.edu    return process->pid();
7712238SN/A}
7722238SN/A
7732238SN/A
7742238SN/ASyscallReturn
7753114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7762680Sktlim@umich.edu           ThreadContext *tc)
7772238SN/A{
7782238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7792238SN/A    // simulation to be deterministic.
7802238SN/A
7812238SN/A    // EUID goes in r20.
7823114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7835543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7842238SN/A}
7852238SN/A
7862238SN/A
7872238SN/ASyscallReturn
7883114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7892680Sktlim@umich.edu           ThreadContext *tc)
7902238SN/A{
7912238SN/A    // Get current group ID.  EGID goes in r20.
7923114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7933114Sgblack@eecs.umich.edu    return process->gid();
7942238SN/A}
7952238SN/A
7962238SN/A
7972238SN/ASyscallReturn
7983114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7992680Sktlim@umich.edu           ThreadContext *tc)
8002238SN/A{
8012238SN/A    // can't fathom why a benchmark would call this.
8026701Sgblack@eecs.umich.edu    int index = 0;
8036701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8042238SN/A    return 0;
8052238SN/A}
8062238SN/A
8072238SN/ASyscallReturn
8083114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8092680Sktlim@umich.edu           ThreadContext *tc)
8102238SN/A{
8112238SN/A    // Make up a PID.  There's no interprocess communication in
8122238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8132238SN/A    // not getting a unique value.
8142238SN/A
8153114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
8163114Sgblack@eecs.umich.edu    return process->pid();
8172238SN/A}
8182238SN/A
8192238SN/ASyscallReturn
8203114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8212680Sktlim@umich.edu           ThreadContext *tc)
8222238SN/A{
8233114Sgblack@eecs.umich.edu    return process->ppid();
8242238SN/A}
8252238SN/A
8262238SN/ASyscallReturn
8273114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8282680Sktlim@umich.edu           ThreadContext *tc)
8292238SN/A{
8305543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8312238SN/A}
8322238SN/A
8332238SN/ASyscallReturn
8343114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8352680Sktlim@umich.edu           ThreadContext *tc)
8362238SN/A{
8375543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8382238SN/A}
8392238SN/A
8402238SN/ASyscallReturn
8413114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8422680Sktlim@umich.edu           ThreadContext *tc)
8432238SN/A{
8443114Sgblack@eecs.umich.edu    return process->gid();
8452238SN/A}
8462238SN/A
8472238SN/ASyscallReturn
8483114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8492680Sktlim@umich.edu           ThreadContext *tc)
8502238SN/A{
8513114Sgblack@eecs.umich.edu    return process->egid();
8522238SN/A}
8532238SN/A
8542238SN/A
8556109Ssanchezd@stanford.eduSyscallReturn
8566109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8576109Ssanchezd@stanford.edu           ThreadContext *tc)
8586109Ssanchezd@stanford.edu{
8596701Sgblack@eecs.umich.edu    int index = 0;
8606701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8616701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8626701Sgblack@eecs.umich.edu
8636109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8646701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8656701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8666109Ssanchezd@stanford.edu
8676109Ssanchezd@stanford.edu
8686701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8696111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8706111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8716111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8726701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8736109Ssanchezd@stanford.edu    }
8746109Ssanchezd@stanford.edu
8756111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8766109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8776109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8786109Ssanchezd@stanford.edu
8796109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8806109Ssanchezd@stanford.edu
8816111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8826109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8836111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8846109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8856109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8866109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8876109Ssanchezd@stanford.edu
8886111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8896109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8906109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8916109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8926109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8936337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8946109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8956109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8969375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8976109Ssanchezd@stanford.edu
8986109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8996109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
9008149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
9018149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
9026109Ssanchezd@stanford.edu        #else
9036109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
9046109Ssanchezd@stanford.edu        #endif
9056109Ssanchezd@stanford.edu
9066111Ssteve.reinhardt@amd.com        // Set up stack register
9076701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
9086109Ssanchezd@stanford.edu
9096111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
9106111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
9116109Ssanchezd@stanford.edu
9126111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
9136109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
9146110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
9156109Ssanchezd@stanford.edu        #endif
9166109Ssanchezd@stanford.edu
9176111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
9186111Ssteve.reinhardt@amd.com        // parent, non-zero if child
9196109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
9206109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
9216109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
9226109Ssanchezd@stanford.edu        #endif
9236109Ssanchezd@stanford.edu
9247720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
9256109Ssanchezd@stanford.edu
9266109Ssanchezd@stanford.edu        ctc->activate();
9276109Ssanchezd@stanford.edu
9286109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9296109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9306109Ssanchezd@stanford.edu        return 1;
9316109Ssanchezd@stanford.edu    } else {
9326109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9336109Ssanchezd@stanford.edu        return 0;
9346109Ssanchezd@stanford.edu    }
9356109Ssanchezd@stanford.edu}
9366109Ssanchezd@stanford.edu
9379455Smitch.hayenga+gem5@gmail.comSyscallReturn
93810203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
93910203SAli.Saidi@ARM.com        int index)
9409455Smitch.hayenga+gem5@gmail.com{
9419455Smitch.hayenga+gem5@gmail.com    string path;
9429455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
94310223Ssteve.reinhardt@amd.com        return -EFAULT;
9449455Smitch.hayenga+gem5@gmail.com
9459455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9469455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9479455Smitch.hayenga+gem5@gmail.com
9489455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9499455Smitch.hayenga+gem5@gmail.com
9509455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9519455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9529455Smitch.hayenga+gem5@gmail.com}
95310203SAli.Saidi@ARM.com
95410203SAli.Saidi@ARM.comSyscallReturn
95510203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
95610203SAli.Saidi@ARM.com{
95710203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
95810203SAli.Saidi@ARM.com}
95910203SAli.Saidi@ARM.com
960