syscall_emul.cc revision 10931
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"
458232Snate@binkert.org#include "debug/SyscallVerbose.hh"
462474SN/A#include "mem/page_table.hh"
47360SN/A#include "sim/process.hh"
488229Snate@binkert.org#include "sim/sim_exit.hh"
498229Snate@binkert.org#include "sim/syscall_emul.hh"
506029Ssteve.reinhardt@amd.com#include "sim/system.hh"
51360SN/A
52360SN/Ausing namespace std;
532107SN/Ausing namespace TheISA;
54360SN/A
55360SN/Avoid
563114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
57360SN/A{
5810253Ssteve.reinhardt@amd.com    if (DTRACE(SyscallVerbose)) {
5910253Ssteve.reinhardt@amd.com        int index = 0;
6010257Ssteve.reinhardt@amd.com        IntReg arg[4] M5_VAR_USED;
6110253Ssteve.reinhardt@amd.com
6210253Ssteve.reinhardt@amd.com        // we can't just put the calls to getSyscallArg() in the
6310253Ssteve.reinhardt@amd.com        // DPRINTF arg list, because C++ doesn't guarantee their order
6410253Ssteve.reinhardt@amd.com        for (int i = 0; i < 4; ++i)
6510253Ssteve.reinhardt@amd.com            arg[i] = process->getSyscallArg(tc, index);
6610253Ssteve.reinhardt@amd.com
6710253Ssteve.reinhardt@amd.com        DPRINTFNR("%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
6810253Ssteve.reinhardt@amd.com                  curTick(), tc->getCpuPtr()->name(), name,
6910253Ssteve.reinhardt@amd.com                  arg[0], arg[1], arg[2], arg[3]);
7010253Ssteve.reinhardt@amd.com    }
71360SN/A
722680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
73360SN/A
7410500Ssteve.reinhardt@amd.com    if (retval.needsRetry()) {
7510500Ssteve.reinhardt@amd.com        DPRINTFS(SyscallVerbose, tc->getCpuPtr(), "syscall %s needs retry\n",
7610500Ssteve.reinhardt@amd.com                 name);
7710500Ssteve.reinhardt@amd.com    } else {
7810500Ssteve.reinhardt@amd.com        DPRINTFS(SyscallVerbose, tc->getCpuPtr(), "syscall %s returns %d\n",
7910500Ssteve.reinhardt@amd.com                 name, retval.encodedValue());
8010500Ssteve.reinhardt@amd.com    }
81360SN/A
8210500Ssteve.reinhardt@amd.com    if (!(flags & SyscallDesc::SuppressReturnValue) && !retval.needsRetry())
835958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
84360SN/A}
85360SN/A
86360SN/A
871450SN/ASyscallReturn
883114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
892680Sktlim@umich.edu                  ThreadContext *tc)
90360SN/A{
911969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
922484SN/A
932484SN/A    return 1;
94360SN/A}
95360SN/A
96360SN/A
971450SN/ASyscallReturn
983114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
992680Sktlim@umich.edu           ThreadContext *tc)
100360SN/A{
1016701Sgblack@eecs.umich.edu    int index = 0;
10210831Ssteve.reinhardt@amd.com    const char *extra_text = "";
103360SN/A
10410831Ssteve.reinhardt@amd.com    if (desc->warnOnce()) {
10510831Ssteve.reinhardt@amd.com        if (desc->warned)
10610831Ssteve.reinhardt@amd.com            return 0;
107360SN/A
10810831Ssteve.reinhardt@amd.com        desc->warned = true;
10910831Ssteve.reinhardt@amd.com        extra_text = "\n      (further warnings will be suppressed)";
11010831Ssteve.reinhardt@amd.com    }
111360SN/A
11210831Ssteve.reinhardt@amd.com    warn("ignoring syscall %s(%d, ...)%s", desc->name,
11310831Ssteve.reinhardt@amd.com         process->getSyscallArg(tc, index), extra_text);
1148149SChris.Emmons@ARM.com
1158149SChris.Emmons@ARM.com    return 0;
1168149SChris.Emmons@ARM.com}
1178149SChris.Emmons@ARM.com
1188149SChris.Emmons@ARM.com
1198149SChris.Emmons@ARM.comSyscallReturn
1203114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1212680Sktlim@umich.edu         ThreadContext *tc)
122360SN/A{
1236029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1246029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1256701Sgblack@eecs.umich.edu        int index = 0;
1265958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1276701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1286029Ssteve.reinhardt@amd.com    } else {
1296029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1306029Ssteve.reinhardt@amd.com        tc->halt();
1312834Sksewell@umich.edu    }
132360SN/A
1331458SN/A    return 1;
134360SN/A}
135360SN/A
136360SN/A
1371450SN/ASyscallReturn
1386109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1396109Ssanchezd@stanford.edu              ThreadContext *tc)
1406109Ssanchezd@stanford.edu{
14110483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
14210483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
14310483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
14410483Swiseveri@student.ethz.ch    }
14510483Swiseveri@student.ethz.ch
14610483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
14710483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
14810483Swiseveri@student.ethz.ch        int index = 0;
14910483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
15010483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
15110483Swiseveri@student.ethz.ch    }
1526109Ssanchezd@stanford.edu
1536109Ssanchezd@stanford.edu    return 1;
1546109Ssanchezd@stanford.edu}
1556109Ssanchezd@stanford.edu
1566109Ssanchezd@stanford.edu
1576109Ssanchezd@stanford.eduSyscallReturn
1583114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
159360SN/A{
16010318Sandreas.hansson@arm.com    return (int)PageBytes;
161360SN/A}
162360SN/A
163360SN/A
1641450SN/ASyscallReturn
1655748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
166360SN/A{
167360SN/A    // change brk addr to first arg
1686701Sgblack@eecs.umich.edu    int index = 0;
1696701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1705748SSteve.Reinhardt@amd.com
1715748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1725748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1735748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1745748SSteve.Reinhardt@amd.com        return p->brk_point;
1755748SSteve.Reinhardt@amd.com
1765748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1775748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1782474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
17910318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1805748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
18110318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1826687Stjones1@inf.ed.ac.uk
1836687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1846687Stjones1@inf.ed.ac.uk            else {
1856687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1868852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1876687Stjones1@inf.ed.ac.uk
1886687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
18910318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1906687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1918852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
19210318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1936687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1946687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1956687Stjones1@inf.ed.ac.uk                {
19610318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1978852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1986687Stjones1@inf.ed.ac.uk                }
1996687Stjones1@inf.ed.ac.uk            }
2002474SN/A        }
2011450SN/A    }
2025748SSteve.Reinhardt@amd.com
2035748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
2041458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
2051458SN/A    return p->brk_point;
206360SN/A}
207360SN/A
208360SN/A
2091450SN/ASyscallReturn
2103114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
211360SN/A{
2126701Sgblack@eecs.umich.edu    int index = 0;
21310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
21410931Sbrandon.potter@amd.com
21510931Sbrandon.potter@amd.com    int sim_fd = p->sim_fd(tgt_fd);
21610931Sbrandon.potter@amd.com    if (sim_fd < 0)
21710931Sbrandon.potter@amd.com        return -EBADF;
21810931Sbrandon.potter@amd.com
2197508Stjones1@inf.ed.ac.uk    int status = 0;
2207508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2217508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2221970SN/A    if (status >= 0)
22310931Sbrandon.potter@amd.com        p->reset_fd_entry(tgt_fd);
2241970SN/A    return status;
225360SN/A}
226360SN/A
227360SN/A
2281450SN/ASyscallReturn
2293114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
230360SN/A{
2316701Sgblack@eecs.umich.edu    int index = 0;
23210931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2336701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2346701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2356701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
236360SN/A
23710931Sbrandon.potter@amd.com    int sim_fd = p->sim_fd(tgt_fd);
23810931Sbrandon.potter@amd.com    if (sim_fd < 0)
23910931Sbrandon.potter@amd.com        return -EBADF;
24010931Sbrandon.potter@amd.com
24110931Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes);
242360SN/A
243360SN/A    if (bytes_read != -1)
2448706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
245360SN/A
2461458SN/A    return bytes_read;
247360SN/A}
248360SN/A
2491450SN/ASyscallReturn
2503114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
251360SN/A{
2526701Sgblack@eecs.umich.edu    int index = 0;
25310931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2546701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2556701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2566701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
257360SN/A
25810931Sbrandon.potter@amd.com    int sim_fd = p->sim_fd(tgt_fd);
25910931Sbrandon.potter@amd.com    if (sim_fd < 0)
26010931Sbrandon.potter@amd.com        return -EBADF;
26110931Sbrandon.potter@amd.com
2628706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
263360SN/A
26410931Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes);
265360SN/A
26610931Sbrandon.potter@amd.com    fsync(sim_fd);
267360SN/A
2681458SN/A    return bytes_written;
269360SN/A}
270360SN/A
271360SN/A
2721450SN/ASyscallReturn
2733114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
274360SN/A{
2756701Sgblack@eecs.umich.edu    int index = 0;
27610931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2776701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2786701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
279360SN/A
28010931Sbrandon.potter@amd.com    int sim_fd = p->sim_fd(tgt_fd);
28110931Sbrandon.potter@amd.com    if (sim_fd < 0)
28210931Sbrandon.potter@amd.com        return -EBADF;
28310931Sbrandon.potter@amd.com
28410931Sbrandon.potter@amd.com    off_t result = lseek(sim_fd, offs, whence);
285360SN/A
2861458SN/A    return (result == (off_t)-1) ? -errno : result;
287360SN/A}
288360SN/A
289360SN/A
2901450SN/ASyscallReturn
2914118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2924118Sgblack@eecs.umich.edu{
2936701Sgblack@eecs.umich.edu    int index = 0;
29410931Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
2956701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2966701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2976701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2986701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2994118Sgblack@eecs.umich.edu
30010931Sbrandon.potter@amd.com    int sim_fd = p->sim_fd(tgt_fd);
30110931Sbrandon.potter@amd.com    if (sim_fd < 0)
30210931Sbrandon.potter@amd.com        return -EBADF;
30310931Sbrandon.potter@amd.com
3044118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
3054118Sgblack@eecs.umich.edu
30610931Sbrandon.potter@amd.com    uint64_t result = lseek(sim_fd, offset, whence);
3074118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
3084118Sgblack@eecs.umich.edu
3094118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
3104118Sgblack@eecs.umich.edu        //The seek failed.
3114118Sgblack@eecs.umich.edu        return -errno;
3124118Sgblack@eecs.umich.edu    } else {
3136111Ssteve.reinhardt@amd.com        // The seek succeeded.
3146111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
3156111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
3166111Ssteve.reinhardt@amd.com        // target platform
3174118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
3184118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
3198706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
3204118Sgblack@eecs.umich.edu        return 0;
3214118Sgblack@eecs.umich.edu    }
3224118Sgblack@eecs.umich.edu}
3234118Sgblack@eecs.umich.edu
3244118Sgblack@eecs.umich.edu
3254118Sgblack@eecs.umich.eduSyscallReturn
3263114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
327360SN/A{
328360SN/A    // given that we don't really implement mmap, munmap is really easy
3291458SN/A    return 0;
330360SN/A}
331360SN/A
332360SN/A
333360SN/Aconst char *hostname = "m5.eecs.umich.edu";
334360SN/A
3351450SN/ASyscallReturn
3363114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
337360SN/A{
3386701Sgblack@eecs.umich.edu    int index = 0;
3396701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3406701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3416701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
342360SN/A
343360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
344360SN/A
3458706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
346360SN/A
3471458SN/A    return 0;
348360SN/A}
349360SN/A
3501450SN/ASyscallReturn
3515513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3525513SMichael.Adler@intel.com{
3535513SMichael.Adler@intel.com    int result = 0;
3546731Svince@csl.cornell.edu    int index = 0;
3556701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3566701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3576701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3585513SMichael.Adler@intel.com
3595513SMichael.Adler@intel.com    // Is current working directory defined?
3605513SMichael.Adler@intel.com    string cwd = p->getcwd();
3615513SMichael.Adler@intel.com    if (!cwd.empty()) {
3625513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3635513SMichael.Adler@intel.com            // Buffer too small
3645513SMichael.Adler@intel.com            return -ERANGE;
3655513SMichael.Adler@intel.com        }
3665513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3675513SMichael.Adler@intel.com        result = cwd.length();
3685513SMichael.Adler@intel.com    }
3695513SMichael.Adler@intel.com    else {
3705513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3715513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3725513SMichael.Adler@intel.com        }
3735513SMichael.Adler@intel.com        else {
3745513SMichael.Adler@intel.com            result = -1;
3755513SMichael.Adler@intel.com        }
3765513SMichael.Adler@intel.com    }
3775513SMichael.Adler@intel.com
3788706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3795513SMichael.Adler@intel.com
3805513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3815513SMichael.Adler@intel.com}
3825513SMichael.Adler@intel.com
38310203SAli.Saidi@ARM.com/// Target open() handler.
38410203SAli.Saidi@ARM.comSyscallReturn
38510203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
38610203SAli.Saidi@ARM.com         ThreadContext *tc)
38710203SAli.Saidi@ARM.com{
38810203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
38910203SAli.Saidi@ARM.com}
3905513SMichael.Adler@intel.com
3915513SMichael.Adler@intel.comSyscallReturn
39210203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
39310203SAli.Saidi@ARM.com        int index)
3945513SMichael.Adler@intel.com{
3955513SMichael.Adler@intel.com    string path;
3965513SMichael.Adler@intel.com
3978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
39810223Ssteve.reinhardt@amd.com        return -EFAULT;
3995513SMichael.Adler@intel.com
4005513SMichael.Adler@intel.com    // Adjust path for current working directory
4015513SMichael.Adler@intel.com    path = p->fullPath(path);
4025513SMichael.Adler@intel.com
4036701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
4046701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
4056701Sgblack@eecs.umich.edu
4066701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
4075513SMichael.Adler@intel.com
4085513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
4095513SMichael.Adler@intel.com
4108706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
4115513SMichael.Adler@intel.com
4125513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4135513SMichael.Adler@intel.com}
4145513SMichael.Adler@intel.com
4155513SMichael.Adler@intel.comSyscallReturn
4163114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
417511SN/A{
41810633Smichaelupton@gmail.com    return unlinkHelper(desc, num, p, tc, 0);
41910633Smichaelupton@gmail.com}
42010633Smichaelupton@gmail.com
42110633Smichaelupton@gmail.comSyscallReturn
42210633Smichaelupton@gmail.comunlinkHelper(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
42310633Smichaelupton@gmail.com           int index)
42410633Smichaelupton@gmail.com{
4251706SN/A    string path;
426360SN/A
4278852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
42810223Ssteve.reinhardt@amd.com        return -EFAULT;
429511SN/A
4303669Sbinkertn@umich.edu    // Adjust path for current working directory
4313669Sbinkertn@umich.edu    path = p->fullPath(path);
4323669Sbinkertn@umich.edu
433511SN/A    int result = unlink(path.c_str());
4341458SN/A    return (result == -1) ? -errno : result;
435511SN/A}
436511SN/A
4375513SMichael.Adler@intel.com
4385513SMichael.Adler@intel.comSyscallReturn
4395513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4405513SMichael.Adler@intel.com{
4415513SMichael.Adler@intel.com    string path;
4425513SMichael.Adler@intel.com
4436701Sgblack@eecs.umich.edu    int index = 0;
4448852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
44510223Ssteve.reinhardt@amd.com        return -EFAULT;
4465513SMichael.Adler@intel.com
4475513SMichael.Adler@intel.com    // Adjust path for current working directory
4485513SMichael.Adler@intel.com    path = p->fullPath(path);
4495513SMichael.Adler@intel.com
4506701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4515513SMichael.Adler@intel.com
4525513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4535513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4545513SMichael.Adler@intel.com}
4555513SMichael.Adler@intel.com
4561450SN/ASyscallReturn
4573114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
458511SN/A{
4591706SN/A    string old_name;
460511SN/A
4616701Sgblack@eecs.umich.edu    int index = 0;
4628852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4631458SN/A        return -EFAULT;
464511SN/A
4651706SN/A    string new_name;
466511SN/A
4678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4681458SN/A        return -EFAULT;
469511SN/A
4703669Sbinkertn@umich.edu    // Adjust path for current working directory
4713669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4723669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4733669Sbinkertn@umich.edu
4741706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4751458SN/A    return (result == -1) ? -errno : result;
476511SN/A}
477511SN/A
4781706SN/ASyscallReturn
4793114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4801706SN/A{
4811706SN/A    string path;
4821706SN/A
4836701Sgblack@eecs.umich.edu    int index = 0;
4848852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4851706SN/A        return -EFAULT;
4861706SN/A
4876701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4881706SN/A
4893669Sbinkertn@umich.edu    // Adjust path for current working directory
4903669Sbinkertn@umich.edu    path = p->fullPath(path);
4913669Sbinkertn@umich.edu
4921706SN/A    int result = truncate(path.c_str(), length);
4931706SN/A    return (result == -1) ? -errno : result;
4941706SN/A}
4951706SN/A
4961706SN/ASyscallReturn
4976111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4986111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4991706SN/A{
5006701Sgblack@eecs.umich.edu    int index = 0;
50110931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
50210931Sbrandon.potter@amd.com    off_t length = process->getSyscallArg(tc, index);
5031706SN/A
50410931Sbrandon.potter@amd.com    int sim_fd = process->sim_fd(tgt_fd);
50510931Sbrandon.potter@amd.com    if (sim_fd < 0)
5061706SN/A        return -EBADF;
5071706SN/A
50810931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5091706SN/A    return (result == -1) ? -errno : result;
5101706SN/A}
5111999SN/A
5121999SN/ASyscallReturn
5136703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
5146703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
5156703Svince@csl.cornell.edu{
5166703Svince@csl.cornell.edu    int index = 0;
5176703Svince@csl.cornell.edu    string path;
5186703Svince@csl.cornell.edu
5198852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
5206703Svince@csl.cornell.edu       return -EFAULT;
5216703Svince@csl.cornell.edu
5226744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5236703Svince@csl.cornell.edu
5246703Svince@csl.cornell.edu    // Adjust path for current working directory
5256703Svince@csl.cornell.edu    path = process->fullPath(path);
5266703Svince@csl.cornell.edu
5276744SAli.Saidi@arm.com#if NO_STAT64
5286744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5296744SAli.Saidi@arm.com#else
5306703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5316744SAli.Saidi@arm.com#endif
5326703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5336703Svince@csl.cornell.edu}
5346703Svince@csl.cornell.edu
5356703Svince@csl.cornell.eduSyscallReturn
5366685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5376685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5386685Stjones1@inf.ed.ac.uk{
5396701Sgblack@eecs.umich.edu    int index = 0;
54010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
54110931Sbrandon.potter@amd.com    int64_t length = process->getSyscallArg(tc, index, 64);
5426685Stjones1@inf.ed.ac.uk
54310931Sbrandon.potter@amd.com    int sim_fd = process->sim_fd(tgt_fd);
54410931Sbrandon.potter@amd.com    if (sim_fd < 0)
5456685Stjones1@inf.ed.ac.uk        return -EBADF;
5466685Stjones1@inf.ed.ac.uk
5476744SAli.Saidi@arm.com#if NO_STAT64
54810931Sbrandon.potter@amd.com    int result = ftruncate(sim_fd, length);
5496744SAli.Saidi@arm.com#else
55010931Sbrandon.potter@amd.com    int result = ftruncate64(sim_fd, length);
5516744SAli.Saidi@arm.com#endif
5526685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5536685Stjones1@inf.ed.ac.uk}
5546685Stjones1@inf.ed.ac.uk
5556685Stjones1@inf.ed.ac.ukSyscallReturn
5565513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5575513SMichael.Adler@intel.com{
5585513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5595513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5605513SMichael.Adler@intel.com    // changing anything.
5615513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5625513SMichael.Adler@intel.com    umask(oldMask);
5635521Snate@binkert.org    return (int)oldMask;
5645513SMichael.Adler@intel.com}
5655513SMichael.Adler@intel.com
5665513SMichael.Adler@intel.comSyscallReturn
5673114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5681999SN/A{
5691999SN/A    string path;
5701999SN/A
5716701Sgblack@eecs.umich.edu    int index = 0;
5728852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5731999SN/A        return -EFAULT;
5741999SN/A
5751999SN/A    /* XXX endianess */
5766701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5771999SN/A    uid_t hostOwner = owner;
5786701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5791999SN/A    gid_t hostGroup = group;
5801999SN/A
5813669Sbinkertn@umich.edu    // Adjust path for current working directory
5823669Sbinkertn@umich.edu    path = p->fullPath(path);
5833669Sbinkertn@umich.edu
5841999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5851999SN/A    return (result == -1) ? -errno : result;
5861999SN/A}
5871999SN/A
5881999SN/ASyscallReturn
5893114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5901999SN/A{
5916701Sgblack@eecs.umich.edu    int index = 0;
59210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5931999SN/A
59410931Sbrandon.potter@amd.com    int sim_fd = process->sim_fd(tgt_fd);
59510931Sbrandon.potter@amd.com    if (sim_fd < 0)
5961999SN/A        return -EBADF;
5971999SN/A
5981999SN/A    /* XXX endianess */
5996701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
6001999SN/A    uid_t hostOwner = owner;
6016701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
6021999SN/A    gid_t hostGroup = group;
6031999SN/A
60410931Sbrandon.potter@amd.com    int result = fchown(sim_fd, hostOwner, hostGroup);
6051999SN/A    return (result == -1) ? -errno : result;
6061999SN/A}
6072093SN/A
6082093SN/A
6092093SN/ASyscallReturn
6103114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
6113079Sstever@eecs.umich.edu{
6126701Sgblack@eecs.umich.edu    int index = 0;
61310781Snilay@cs.wisc.edu    int tgt_fd = process->getSyscallArg(tc, index);
61410931Sbrandon.potter@amd.com
61510781Snilay@cs.wisc.edu    int sim_fd = process->sim_fd(tgt_fd);
61610781Snilay@cs.wisc.edu    if (sim_fd < 0)
6173079Sstever@eecs.umich.edu        return -EBADF;
6183079Sstever@eecs.umich.edu
61910930Sbrandon.potter@amd.com    FDEntry *fde = process->get_fd_entry(tgt_fd);
6205282Srstrong@cs.ucsd.edu
62110781Snilay@cs.wisc.edu    int result = dup(sim_fd);
6226111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
62310930Sbrandon.potter@amd.com        process->alloc_fd(result, fde->filename, fde->flags, fde->mode, false);
6243079Sstever@eecs.umich.edu}
6253079Sstever@eecs.umich.edu
6263079Sstever@eecs.umich.edu
6273079Sstever@eecs.umich.eduSyscallReturn
6283114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6292680Sktlim@umich.edu          ThreadContext *tc)
6302093SN/A{
6316701Sgblack@eecs.umich.edu    int index = 0;
63210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6332093SN/A
63410931Sbrandon.potter@amd.com    int sim_fd = process->sim_fd(tgt_fd);
63510931Sbrandon.potter@amd.com    if (sim_fd < 0)
6362093SN/A        return -EBADF;
6372093SN/A
6386701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6392093SN/A    switch (cmd) {
6402093SN/A      case 0: // F_DUPFD
6412093SN/A        // if we really wanted to support this, we'd need to do it
6422093SN/A        // in the target fd space.
64310931Sbrandon.potter@amd.com        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd);
6442093SN/A        return -EMFILE;
6452093SN/A
6462093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6472093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6482093SN/A        return 0;
6492093SN/A
6502093SN/A      case 3: // F_GETFL (get file flags)
6512093SN/A      case 4: // F_SETFL (set file flags)
6522093SN/A        // not sure if this is totally valid, but we'll pass it through
6532093SN/A        // to the underlying OS
65410931Sbrandon.potter@amd.com        warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd);
65510931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6562093SN/A        // return 0;
6572093SN/A
6582093SN/A      case 7: // F_GETLK  (get lock)
6592093SN/A      case 8: // F_SETLK  (set lock)
6602093SN/A      case 9: // F_SETLKW (set lock and wait)
6612093SN/A        // don't mess with file locking... just act like it's OK
66210931Sbrandon.potter@amd.com        warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd);
6632093SN/A        return 0;
6642093SN/A
6652093SN/A      default:
6662093SN/A        warn("Unknown fcntl command %d\n", cmd);
6672093SN/A        return 0;
6682093SN/A    }
6692093SN/A}
6702093SN/A
6712238SN/ASyscallReturn
6723114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6732687Sksewell@umich.edu            ThreadContext *tc)
6742687Sksewell@umich.edu{
6756701Sgblack@eecs.umich.edu    int index = 0;
67610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
6772687Sksewell@umich.edu
67810931Sbrandon.potter@amd.com    int sim_fd = process->sim_fd(tgt_fd);
67910931Sbrandon.potter@amd.com    if (sim_fd < 0)
6802687Sksewell@umich.edu        return -EBADF;
6812687Sksewell@umich.edu
6826701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6832687Sksewell@umich.edu    switch (cmd) {
6842687Sksewell@umich.edu      case 33: //F_GETLK64
68510931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
6862687Sksewell@umich.edu        return -EMFILE;
6872687Sksewell@umich.edu
6882687Sksewell@umich.edu      case 34: // F_SETLK64
6892687Sksewell@umich.edu      case 35: // F_SETLKW64
69010931Sbrandon.potter@amd.com        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
69110931Sbrandon.potter@amd.com             tgt_fd);
6922687Sksewell@umich.edu        return -EMFILE;
6932687Sksewell@umich.edu
6942687Sksewell@umich.edu      default:
6952687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6962687Sksewell@umich.edu        // to the underlying OS
69710931Sbrandon.potter@amd.com        warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
69810931Sbrandon.potter@amd.com        return fcntl(sim_fd, cmd);
6992687Sksewell@umich.edu        // return 0;
7002687Sksewell@umich.edu    }
7012687Sksewell@umich.edu}
7022687Sksewell@umich.edu
7032687Sksewell@umich.eduSyscallReturn
7043114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7052680Sktlim@umich.edu         ThreadContext *tc)
7062238SN/A{
7072238SN/A    int fds[2], sim_fds[2];
7082238SN/A    int pipe_retval = pipe(fds);
7092093SN/A
7102238SN/A    if (pipe_retval < 0) {
7112238SN/A        // error
7122238SN/A        return pipe_retval;
7132238SN/A    }
7142238SN/A
7155282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
7165282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
7172238SN/A
7185282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
7192238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
7202238SN/A    // the return value of the function, and fd[1] is returned in r20.
7212680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
7222238SN/A    return sim_fds[0];
7232238SN/A}
7242238SN/A
7252238SN/A
7262238SN/ASyscallReturn
7273114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7282680Sktlim@umich.edu           ThreadContext *tc)
7292238SN/A{
7302238SN/A    // Make up a PID.  There's no interprocess communication in
7312238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7322238SN/A    // not getting a unique value.
7332238SN/A
7343114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7353114Sgblack@eecs.umich.edu    return process->pid();
7362238SN/A}
7372238SN/A
7382238SN/A
7392238SN/ASyscallReturn
7403114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7412680Sktlim@umich.edu           ThreadContext *tc)
7422238SN/A{
7432238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7442238SN/A    // simulation to be deterministic.
7452238SN/A
7462238SN/A    // EUID goes in r20.
7473114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7485543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7492238SN/A}
7502238SN/A
7512238SN/A
7522238SN/ASyscallReturn
7533114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7542680Sktlim@umich.edu           ThreadContext *tc)
7552238SN/A{
7562238SN/A    // Get current group ID.  EGID goes in r20.
7573114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7583114Sgblack@eecs.umich.edu    return process->gid();
7592238SN/A}
7602238SN/A
7612238SN/A
7622238SN/ASyscallReturn
7633114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7642680Sktlim@umich.edu           ThreadContext *tc)
7652238SN/A{
7662238SN/A    // can't fathom why a benchmark would call this.
7676701Sgblack@eecs.umich.edu    int index = 0;
7686701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7692238SN/A    return 0;
7702238SN/A}
7712238SN/A
7722238SN/ASyscallReturn
7733114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7742680Sktlim@umich.edu           ThreadContext *tc)
7752238SN/A{
7762238SN/A    // Make up a PID.  There's no interprocess communication in
7772238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7782238SN/A    // not getting a unique value.
7792238SN/A
7803114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7813114Sgblack@eecs.umich.edu    return process->pid();
7822238SN/A}
7832238SN/A
7842238SN/ASyscallReturn
7853114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7862680Sktlim@umich.edu           ThreadContext *tc)
7872238SN/A{
7883114Sgblack@eecs.umich.edu    return process->ppid();
7892238SN/A}
7902238SN/A
7912238SN/ASyscallReturn
7923114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7932680Sktlim@umich.edu           ThreadContext *tc)
7942238SN/A{
7955543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7962238SN/A}
7972238SN/A
7982238SN/ASyscallReturn
7993114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8002680Sktlim@umich.edu           ThreadContext *tc)
8012238SN/A{
8025543Ssaidi@eecs.umich.edu    return process->euid();             // UID
8032238SN/A}
8042238SN/A
8052238SN/ASyscallReturn
8063114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8072680Sktlim@umich.edu           ThreadContext *tc)
8082238SN/A{
8093114Sgblack@eecs.umich.edu    return process->gid();
8102238SN/A}
8112238SN/A
8122238SN/ASyscallReturn
8133114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8142680Sktlim@umich.edu           ThreadContext *tc)
8152238SN/A{
8163114Sgblack@eecs.umich.edu    return process->egid();
8172238SN/A}
8182238SN/A
8192238SN/A
8206109Ssanchezd@stanford.eduSyscallReturn
8216109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8226109Ssanchezd@stanford.edu           ThreadContext *tc)
8236109Ssanchezd@stanford.edu{
8246701Sgblack@eecs.umich.edu    int index = 0;
8256701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
8266701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
8276701Sgblack@eecs.umich.edu
8286109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
8296701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
8306701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
8316109Ssanchezd@stanford.edu
8326109Ssanchezd@stanford.edu
8336701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8346111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8356111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8366111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8376701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8386109Ssanchezd@stanford.edu    }
8396109Ssanchezd@stanford.edu
8406111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8416109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8426109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8436109Ssanchezd@stanford.edu
8446109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8456109Ssanchezd@stanford.edu
8466111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8476109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8486111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8496109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8506109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8516109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8526109Ssanchezd@stanford.edu
8536111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8546109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8556109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8566109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8576109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8586337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8596109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8606109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8619375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8626109Ssanchezd@stanford.edu
8636109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8646109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8658149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8668149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8676109Ssanchezd@stanford.edu        #else
8686109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8696109Ssanchezd@stanford.edu        #endif
8706109Ssanchezd@stanford.edu
8716111Ssteve.reinhardt@amd.com        // Set up stack register
8726701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8736109Ssanchezd@stanford.edu
8746111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8756111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8766109Ssanchezd@stanford.edu
8776111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8786109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8796110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8806109Ssanchezd@stanford.edu        #endif
8816109Ssanchezd@stanford.edu
8826111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8836111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8846109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8856109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8866109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8876109Ssanchezd@stanford.edu        #endif
8886109Ssanchezd@stanford.edu
8897720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8906109Ssanchezd@stanford.edu
8916109Ssanchezd@stanford.edu        ctc->activate();
8926109Ssanchezd@stanford.edu
8936109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8946109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8956109Ssanchezd@stanford.edu        return 1;
8966109Ssanchezd@stanford.edu    } else {
8976109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8986109Ssanchezd@stanford.edu        return 0;
8996109Ssanchezd@stanford.edu    }
9006109Ssanchezd@stanford.edu}
9016109Ssanchezd@stanford.edu
9029455Smitch.hayenga+gem5@gmail.comSyscallReturn
90310203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
90410203SAli.Saidi@ARM.com        int index)
9059455Smitch.hayenga+gem5@gmail.com{
9069455Smitch.hayenga+gem5@gmail.com    string path;
9079455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
90810223Ssteve.reinhardt@amd.com        return -EFAULT;
9099455Smitch.hayenga+gem5@gmail.com
9109455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
9119455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
9129455Smitch.hayenga+gem5@gmail.com
9139455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
9149455Smitch.hayenga+gem5@gmail.com
9159455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
9169455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
9179455Smitch.hayenga+gem5@gmail.com}
91810203SAli.Saidi@ARM.com
91910203SAli.Saidi@ARM.comSyscallReturn
92010203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
92110203SAli.Saidi@ARM.com{
92210203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
92310203SAli.Saidi@ARM.com}
92410203SAli.Saidi@ARM.com
925