syscall_emul.cc revision 11380
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{
327360SN/A    // given that we don't really implement mmap, munmap is really easy
3281458SN/A    return 0;
329360SN/A}
330360SN/A
331360SN/A
332360SN/Aconst char *hostname = "m5.eecs.umich.edu";
333360SN/A
3341450SN/ASyscallReturn
3353114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
336360SN/A{
3376701Sgblack@eecs.umich.edu    int index = 0;
3386701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3396701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3406701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
341360SN/A
342360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
343360SN/A
3448706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
345360SN/A
3461458SN/A    return 0;
347360SN/A}
348360SN/A
3491450SN/ASyscallReturn
3505513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3515513SMichael.Adler@intel.com{
3525513SMichael.Adler@intel.com    int result = 0;
3536731Svince@csl.cornell.edu    int index = 0;
3546701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3556701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3566701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3575513SMichael.Adler@intel.com
3585513SMichael.Adler@intel.com    // Is current working directory defined?
3595513SMichael.Adler@intel.com    string cwd = p->getcwd();
3605513SMichael.Adler@intel.com    if (!cwd.empty()) {
3615513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3625513SMichael.Adler@intel.com            // Buffer too small
3635513SMichael.Adler@intel.com            return -ERANGE;
3645513SMichael.Adler@intel.com        }
3655513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3665513SMichael.Adler@intel.com        result = cwd.length();
36710955Sdavid.hashe@amd.com    } else {
3685513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3695513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
37010955Sdavid.hashe@amd.com        } else {
3715513SMichael.Adler@intel.com            result = -1;
3725513SMichael.Adler@intel.com        }
3735513SMichael.Adler@intel.com    }
3745513SMichael.Adler@intel.com
3758706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3765513SMichael.Adler@intel.com
3775513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3785513SMichael.Adler@intel.com}
3795513SMichael.Adler@intel.com
38010203SAli.Saidi@ARM.com/// Target open() handler.
38110203SAli.Saidi@ARM.comSyscallReturn
38210203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
38310203SAli.Saidi@ARM.com         ThreadContext *tc)
38410203SAli.Saidi@ARM.com{
38510203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
38610203SAli.Saidi@ARM.com}
3875513SMichael.Adler@intel.com
3885513SMichael.Adler@intel.comSyscallReturn
38910203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
39010203SAli.Saidi@ARM.com        int index)
3915513SMichael.Adler@intel.com{
3925513SMichael.Adler@intel.com    string path;
3935513SMichael.Adler@intel.com
3948852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
39510223Ssteve.reinhardt@amd.com        return -EFAULT;
3965513SMichael.Adler@intel.com
3975513SMichael.Adler@intel.com    // Adjust path for current working directory
3985513SMichael.Adler@intel.com    path = p->fullPath(path);
3995513SMichael.Adler@intel.com
4006701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
4016701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
4026701Sgblack@eecs.umich.edu
4036701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
4045513SMichael.Adler@intel.com
40510955Sdavid.hashe@amd.com    int result = -1;
40610955Sdavid.hashe@amd.com    if (path != "/proc/self/exe") {
40710955Sdavid.hashe@amd.com        result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
40810955Sdavid.hashe@amd.com    } else {
40911140Sjthestness@gmail.com        // Emulate readlink() called on '/proc/self/exe' should return the
41011140Sjthestness@gmail.com        // absolute path of the binary running in the simulated system (the
41111140Sjthestness@gmail.com        // LiveProcess' executable). It is possible that using this path in
41211140Sjthestness@gmail.com        // the simulated system will result in unexpected behavior if:
41311140Sjthestness@gmail.com        //  1) One binary runs another (e.g., -c time -o "my_binary"), and
41411140Sjthestness@gmail.com        //     called binary calls readlink().
41511140Sjthestness@gmail.com        //  2) The host's full path to the running benchmark changes from one
41611140Sjthestness@gmail.com        //     simulation to another. This can result in different simulated
41711140Sjthestness@gmail.com        //     performance since the simulated system will process the binary
41811140Sjthestness@gmail.com        //     path differently, even if the binary itself does not change.
41911140Sjthestness@gmail.com
42011140Sjthestness@gmail.com        // Get the absolute canonical path to the running application
42111140Sjthestness@gmail.com        char real_path[PATH_MAX];
42211140Sjthestness@gmail.com        char *check_real_path = realpath(p->progName(), real_path);
42311140Sjthestness@gmail.com        if (!check_real_path) {
42411140Sjthestness@gmail.com            fatal("readlink('/proc/self/exe') unable to resolve path to "
42511140Sjthestness@gmail.com                  "executable: %s", p->progName());
42611140Sjthestness@gmail.com        }
42711140Sjthestness@gmail.com        strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
42811140Sjthestness@gmail.com        size_t real_path_len = strlen(real_path);
42911140Sjthestness@gmail.com        if (real_path_len > bufsiz) {
43010955Sdavid.hashe@amd.com            // readlink will truncate the contents of the
43110955Sdavid.hashe@amd.com            // path to ensure it is no more than bufsiz
43210955Sdavid.hashe@amd.com            result = bufsiz;
43310955Sdavid.hashe@amd.com        } else {
43411140Sjthestness@gmail.com            result = real_path_len;
43510955Sdavid.hashe@amd.com        }
43611140Sjthestness@gmail.com
43711140Sjthestness@gmail.com        // Issue a warning about potential unexpected results
43811140Sjthestness@gmail.com        warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
43911140Sjthestness@gmail.com                  "results in various settings.\n      Returning '%s'\n",
44011140Sjthestness@gmail.com                  (char*)buf.bufferPtr());
44110955Sdavid.hashe@amd.com    }
4425513SMichael.Adler@intel.com
4438706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4445513SMichael.Adler@intel.com
4455513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4465513SMichael.Adler@intel.com}
4475513SMichael.Adler@intel.com
4485513SMichael.Adler@intel.comSyscallReturn
4493114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
450511SN/A{
45110633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
45210633Smichaelupton@gmail.com}
45310633Smichaelupton@gmail.com
45410633Smichaelupton@gmail.comSyscallReturn
45510633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
45610633Smichaelupton@gmail.com           int index)
45710633Smichaelupton@gmail.com{
4581706SN/A    string path;
459360SN/A
4608852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
46110223Ssteve.reinhardt@amd.com        return -EFAULT;
462511SN/A
4633669Sbinkertn@umich.edu    // Adjust path for current working directory
4643669Sbinkertn@umich.edu    path = p->fullPath(path);
4653669Sbinkertn@umich.edu
466511SN/A    int result = unlink(path.c_str());
4671458SN/A    return (result == -1) ? -errno : result;
468511SN/A}
469511SN/A
4705513SMichael.Adler@intel.com
4715513SMichael.Adler@intel.comSyscallReturn
4725513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4735513SMichael.Adler@intel.com{
4745513SMichael.Adler@intel.com    string path;
4755513SMichael.Adler@intel.com
4766701Sgblack@eecs.umich.edu    int index = 0;
4778852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
47810223Ssteve.reinhardt@amd.com        return -EFAULT;
4795513SMichael.Adler@intel.com
4805513SMichael.Adler@intel.com    // Adjust path for current working directory
4815513SMichael.Adler@intel.com    path = p->fullPath(path);
4825513SMichael.Adler@intel.com
4836701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4845513SMichael.Adler@intel.com
4855513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4865513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4875513SMichael.Adler@intel.com}
4885513SMichael.Adler@intel.com
4891450SN/ASyscallReturn
4903114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
491511SN/A{
4921706SN/A    string old_name;
493511SN/A
4946701Sgblack@eecs.umich.edu    int index = 0;
4958852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4961458SN/A        return -EFAULT;
497511SN/A
4981706SN/A    string new_name;
499511SN/A
5008852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
5011458SN/A        return -EFAULT;
502511SN/A
5033669Sbinkertn@umich.edu    // Adjust path for current working directory
5043669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
5053669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
5063669Sbinkertn@umich.edu
5071706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
5081458SN/A    return (result == -1) ? -errno : result;
509511SN/A}
510511SN/A
5111706SN/ASyscallReturn
5123114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5131706SN/A{
5141706SN/A    string path;
5151706SN/A
5166701Sgblack@eecs.umich.edu    int index = 0;
5178852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5181706SN/A        return -EFAULT;
5191706SN/A
5206701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
5211706SN/A
5223669Sbinkertn@umich.edu    // Adjust path for current working directory
5233669Sbinkertn@umich.edu    path = p->fullPath(path);
5243669Sbinkertn@umich.edu
5251706SN/A    int result = truncate(path.c_str(), length);
5261706SN/A    return (result == -1) ? -errno : result;
5271706SN/A}
5281706SN/A
5291706SN/ASyscallReturn
5306111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
5316111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
5321706SN/A{
5336701Sgblack@eecs.umich.edu    int index = 0;
53410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
53510931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
5361706SN/A
53710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
53810931Sbrandon.potter@amd.com    if (sim_fd < 0)
5391706SN/A        return -EBADF;
5401706SN/A
54110931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5421706SN/A    return (result == -1) ? -errno : result;
5431706SN/A}
5441999SN/A
5451999SN/ASyscallReturn
5466703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5476703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5486703Svince@csl.cornell.edu{
5496703Svince@csl.cornell.edu    int index = 0;
5506703Svince@csl.cornell.edu    string path;
5516703Svince@csl.cornell.edu
5528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5536703Svince@csl.cornell.edu       return -EFAULT;
5546703Svince@csl.cornell.edu
5556744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5566703Svince@csl.cornell.edu
5576703Svince@csl.cornell.edu    // Adjust path for current working directory
5586703Svince@csl.cornell.edu    path = process->fullPath(path);
5596703Svince@csl.cornell.edu
5606744SAli.Saidi@arm.com#if NO_STAT64
5616744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5626744SAli.Saidi@arm.com#else
5636703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5646744SAli.Saidi@arm.com#endif
5656703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5666703Svince@csl.cornell.edu}
5676703Svince@csl.cornell.edu
5686703Svince@csl.cornell.eduSyscallReturn
5696685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5706685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5716685Stjones1@inf.ed.ac.uk{
5726701Sgblack@eecs.umich.edu    int index = 0;
57310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
57410931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5756685Stjones1@inf.ed.ac.uk
57610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
57710931Sbrandon.potter@amd.com    if (sim_fd < 0)
5786685Stjones1@inf.ed.ac.uk        return -EBADF;
5796685Stjones1@inf.ed.ac.uk
5806744SAli.Saidi@arm.com#if NO_STAT64
58110931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5826744SAli.Saidi@arm.com#else
58310931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5846744SAli.Saidi@arm.com#endif
5856685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5866685Stjones1@inf.ed.ac.uk}
5876685Stjones1@inf.ed.ac.uk
5886685Stjones1@inf.ed.ac.ukSyscallReturn
5895513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5905513SMichael.Adler@intel.com{
5915513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5925513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5935513SMichael.Adler@intel.com    // changing anything.
5945513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5955513SMichael.Adler@intel.com    umask(oldMask);
5965521Snate@binkert.org    return (int)oldMask;
5975513SMichael.Adler@intel.com}
5985513SMichael.Adler@intel.com
5995513SMichael.Adler@intel.comSyscallReturn
6003114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
6011999SN/A{
6021999SN/A    string path;
6031999SN/A
6046701Sgblack@eecs.umich.edu    int index = 0;
6058852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6061999SN/A        return -EFAULT;
6071999SN/A
6081999SN/A    /* XXX endianess */
6096701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
6101999SN/A    uid_t hostOwner = owner;
6116701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
6121999SN/A    gid_t hostGroup = group;
6131999SN/A
6143669Sbinkertn@umich.edu    // Adjust path for current working directory
6153669Sbinkertn@umich.edu    path = p->fullPath(path);
6163669Sbinkertn@umich.edu
6171999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
6181999SN/A    return (result == -1) ? -errno : result;
6191999SN/A}
6201999SN/A
6211999SN/ASyscallReturn
6223114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6231999SN/A{
6246701Sgblack@eecs.umich.edu    int index = 0;
62510931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6261999SN/A
62710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
62810931Sbrandon.potter@amd.com    if (sim_fd < 0)
6291999SN/A        return -EBADF;
6301999SN/A
6311999SN/A    /* XXX endianess */
6326701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
6331999SN/A    uid_t hostOwner = owner;
6346701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
6351999SN/A    gid_t hostGroup = group;
6361999SN/A
63710931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6381999SN/A    return (result == -1) ? -errno : result;
6391999SN/A}
6402093SN/A
6412093SN/A
6422093SN/ASyscallReturn
6433114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6443079Sstever@eecs.umich.edu{
6456701Sgblack@eecs.umich.edu    int index = 0;
64610781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
64710931Sbrandon.potter@amd.com
64810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
64910781Snilay@cs.wisc.edu    if (sim_fd < 0)
6503079Sstever@eecs.umich.edu        return -EBADF;
6513079Sstever@eecs.umich.edu
65210932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
6535282Srstrong@cs.ucsd.edu
65410781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6556111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
65610932Sbrandon.potter@amd.com        process->allocFD(result, fde->filename, fde->flags, fde->mode, false);
6573079Sstever@eecs.umich.edu}
6583079Sstever@eecs.umich.edu
6593079Sstever@eecs.umich.edu
6603079Sstever@eecs.umich.eduSyscallReturn
6613114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6622680Sktlim@umich.edu          ThreadContext *tc)
6632093SN/A{
6646701Sgblack@eecs.umich.edu    int index = 0;
66510931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6662093SN/A
66710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
66810931Sbrandon.potter@amd.com    if (sim_fd < 0)
6692093SN/A        return -EBADF;
6702093SN/A
6716701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6722093SN/A    switch (cmd) {
6732093SN/A      case 0: // F_DUPFD
6742093SN/A        // if we really wanted to support this, we'd need to do it
6752093SN/A        // in the target fd space.
67610931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6772093SN/A        return -EMFILE;
6782093SN/A
6792093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6802093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6812093SN/A        return 0;
6822093SN/A
6832093SN/A      case 3: // F_GETFL (get file flags)
6842093SN/A      case 4: // F_SETFL (set file flags)
6852093SN/A        // not sure if this is totally valid, but we'll pass it through
6862093SN/A        // to the underlying OS
68710931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
68810931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6892093SN/A        // return 0;
6902093SN/A
6912093SN/A      case 7: // F_GETLK  (get lock)
6922093SN/A      case 8: // F_SETLK  (set lock)
6932093SN/A      case 9: // F_SETLKW (set lock and wait)
6942093SN/A        // don't mess with file locking... just act like it's OK
69510931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6962093SN/A        return 0;
6972093SN/A
6982093SN/A      default:
6992093SN/A        warn("Unknown fcntl command %d\n", cmd);
7002093SN/A        return 0;
7012093SN/A    }
7022093SN/A}
7032093SN/A
7042238SN/ASyscallReturn
7053114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
7062687Sksewell@umich.edu            ThreadContext *tc)
7072687Sksewell@umich.edu{
7086701Sgblack@eecs.umich.edu    int index = 0;
70910931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
7102687Sksewell@umich.edu
71110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
71210931Sbrandon.potter@amd.com    if (sim_fd < 0)
7132687Sksewell@umich.edu        return -EBADF;
7142687Sksewell@umich.edu
7156701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
7162687Sksewell@umich.edu    switch (cmd) {
7172687Sksewell@umich.edu      case 33: //F_GETLK64
71810931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
7192687Sksewell@umich.edu        return -EMFILE;
7202687Sksewell@umich.edu
7212687Sksewell@umich.edu      case 34: // F_SETLK64
7222687Sksewell@umich.edu      case 35: // F_SETLKW64
72310931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
72410931Sbrandon.potter@amd.com             tgt_fd);
7252687Sksewell@umich.edu        return -EMFILE;
7262687Sksewell@umich.edu
7272687Sksewell@umich.edu      default:
7282687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
7292687Sksewell@umich.edu        // to the underlying OS
73010931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
73110931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
7322687Sksewell@umich.edu        // return 0;
7332687Sksewell@umich.edu    }
7342687Sksewell@umich.edu}
7352687Sksewell@umich.edu
7362687Sksewell@umich.eduSyscallReturn
7373114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7382680Sktlim@umich.edu         ThreadContext *tc)
7392238SN/A{
7402238SN/A    int fds[2], sim_fds[2];
7412238SN/A    int pipe_retval = pipe(fds);
7422093SN/A
7432238SN/A    if (pipe_retval < 0) {
7442238SN/A        // error
7452238SN/A        return pipe_retval;
7462238SN/A    }
7472238SN/A
74810932Sbrandon.potter@amd.com    sim_fds[0] = process->allocFD(fds[0], "PIPE-READ", O_WRONLY, -1, true);
74910932Sbrandon.potter@amd.com    sim_fds[1] = process->allocFD(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7502238SN/A
7515282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7522238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7532238SN/A    // the return value of the function, and fd[1] is returned in r20.
7542680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7552238SN/A    return sim_fds[0];
7562238SN/A}
7572238SN/A
7582238SN/A
7592238SN/ASyscallReturn
7603114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7612680Sktlim@umich.edu           ThreadContext *tc)
7622238SN/A{
7632238SN/A    // Make up a PID.  There's no interprocess communication in
7642238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7652238SN/A    // not getting a unique value.
7662238SN/A
7673114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7683114Sgblack@eecs.umich.edu    return process->pid();
7692238SN/A}
7702238SN/A
7712238SN/A
7722238SN/ASyscallReturn
7733114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7742680Sktlim@umich.edu           ThreadContext *tc)
7752238SN/A{
7762238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7772238SN/A    // simulation to be deterministic.
7782238SN/A
7792238SN/A    // EUID goes in r20.
7803114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7815543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7822238SN/A}
7832238SN/A
7842238SN/A
7852238SN/ASyscallReturn
7863114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7872680Sktlim@umich.edu           ThreadContext *tc)
7882238SN/A{
7892238SN/A    // Get current group ID.  EGID goes in r20.
7903114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7913114Sgblack@eecs.umich.edu    return process->gid();
7922238SN/A}
7932238SN/A
7942238SN/A
7952238SN/ASyscallReturn
7963114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7972680Sktlim@umich.edu           ThreadContext *tc)
7982238SN/A{
7992238SN/A    // can't fathom why a benchmark would call this.
8006701Sgblack@eecs.umich.edu    int index = 0;
8016701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
8022238SN/A    return 0;
8032238SN/A}
8042238SN/A
8052238SN/ASyscallReturn
8063114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8072680Sktlim@umich.edu           ThreadContext *tc)
8082238SN/A{
8092238SN/A    // Make up a PID.  There's no interprocess communication in
8102238SN/A    // fake_syscall mode, so there's no way for a process to know it's
8112238SN/A    // not getting a unique value.
8122238SN/A
8133114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
8143114Sgblack@eecs.umich.edu    return process->pid();
8152238SN/A}
8162238SN/A
8172238SN/ASyscallReturn
8183114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8192680Sktlim@umich.edu           ThreadContext *tc)
8202238SN/A{
8213114Sgblack@eecs.umich.edu    return process->ppid();
8222238SN/A}
8232238SN/A
8242238SN/ASyscallReturn
8253114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8262680Sktlim@umich.edu           ThreadContext *tc)
8272238SN/A{
8285543Ssaidi@eecs.umich.edu    return process->uid();              // UID
8292238SN/A}
8302238SN/A
8312238SN/ASyscallReturn
8323114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8332680Sktlim@umich.edu           ThreadContext *tc)
8342238SN/A{
8355543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8362238SN/A}
8372238SN/A
8382238SN/ASyscallReturn
8393114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8402680Sktlim@umich.edu           ThreadContext *tc)
8412238SN/A{
8423114Sgblack@eecs.umich.edu    return process->gid();
8432238SN/A}
8442238SN/A
8452238SN/ASyscallReturn
8463114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8472680Sktlim@umich.edu           ThreadContext *tc)
8482238SN/A{
8493114Sgblack@eecs.umich.edu    return process->egid();
8502238SN/A}
8512238SN/A
8522238SN/A
8536109Ssanchezd@stanford.eduSyscallReturn
8546109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8556109Ssanchezd@stanford.edu           ThreadContext *tc)
8566109Ssanchezd@stanford.edu{
8576701Sgblack@eecs.umich.edu    int index = 0;
8586701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8596701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8606701Sgblack@eecs.umich.edu
8616109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8626701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8636701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8646109Ssanchezd@stanford.edu
8656109Ssanchezd@stanford.edu
8666701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8676111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8686111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8696111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8706701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8716109Ssanchezd@stanford.edu    }
8726109Ssanchezd@stanford.edu
8736111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8746109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8756109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8766109Ssanchezd@stanford.edu
8776109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8786109Ssanchezd@stanford.edu
8796111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8806109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8816111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8826109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8836109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8846109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8856109Ssanchezd@stanford.edu
8866111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8876109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8886109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8896109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8906109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8916337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8926109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8936109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8949375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8956109Ssanchezd@stanford.edu
8966109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8976109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8988149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8998149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
9006109Ssanchezd@stanford.edu        #else
9016109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
9026109Ssanchezd@stanford.edu        #endif
9036109Ssanchezd@stanford.edu
9046111Ssteve.reinhardt@amd.com        // Set up stack register
9056701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
9066109Ssanchezd@stanford.edu
9076111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
9086111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
9096109Ssanchezd@stanford.edu
9106111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
9116109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
9126110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
9136109Ssanchezd@stanford.edu        #endif
9146109Ssanchezd@stanford.edu
9156111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
9166111Ssteve.reinhardt@amd.com        // parent, non-zero if child
9176109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
9186109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
9196109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
9206109Ssanchezd@stanford.edu        #endif
9216109Ssanchezd@stanford.edu
9227720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
9236109Ssanchezd@stanford.edu
9246109Ssanchezd@stanford.edu        ctc->activate();
9256109Ssanchezd@stanford.edu
9266109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
9276109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
9286109Ssanchezd@stanford.edu        return 1;
9296109Ssanchezd@stanford.edu    } else {
9306109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
9316109Ssanchezd@stanford.edu        return 0;
9326109Ssanchezd@stanford.edu    }
9336109Ssanchezd@stanford.edu}
9346109Ssanchezd@stanford.edu
9359455Smitch.hayenga+gem5@gmail.comSyscallReturn
93610203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
93710203SAli.Saidi@ARM.com        int index)
9389455Smitch.hayenga+gem5@gmail.com{
9399455Smitch.hayenga+gem5@gmail.com    string path;
9409455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
94110223Ssteve.reinhardt@amd.com        return -EFAULT;
9429455Smitch.hayenga+gem5@gmail.com
9439455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9449455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9459455Smitch.hayenga+gem5@gmail.com
9469455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9479455Smitch.hayenga+gem5@gmail.com
9489455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9499455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9509455Smitch.hayenga+gem5@gmail.com}
95110203SAli.Saidi@ARM.com
95210203SAli.Saidi@ARM.comSyscallReturn
95310203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
95410203SAli.Saidi@ARM.com{
95510203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
95610203SAli.Saidi@ARM.com}
95710203SAli.Saidi@ARM.com
958