syscall_emul.cc revision 10483
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
742495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
7510223Ssteve.reinhardt@amd.com             curTick(), tc->getCpuPtr()->name(), name, retval.encodedValue());
76360SN/A
771450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
785958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
79360SN/A}
80360SN/A
81360SN/A
821450SN/ASyscallReturn
833114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
842680Sktlim@umich.edu                  ThreadContext *tc)
85360SN/A{
861969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
872484SN/A
882484SN/A    return 1;
89360SN/A}
90360SN/A
91360SN/A
921450SN/ASyscallReturn
933114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
942680Sktlim@umich.edu           ThreadContext *tc)
95360SN/A{
966701Sgblack@eecs.umich.edu    int index = 0;
9710253Ssteve.reinhardt@amd.com    warn("ignoring syscall %s(%d, ...)", desc->name,
9810253Ssteve.reinhardt@amd.com         process->getSyscallArg(tc, index));
99360SN/A
1001458SN/A    return 0;
101360SN/A}
102360SN/A
103360SN/A
1041450SN/ASyscallReturn
1058149SChris.Emmons@ARM.comignoreWarnOnceFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1068149SChris.Emmons@ARM.com           ThreadContext *tc)
1078149SChris.Emmons@ARM.com{
1088149SChris.Emmons@ARM.com    int index = 0;
10910253Ssteve.reinhardt@amd.com    warn_once("ignoring syscall %s(%d, ...)", desc->name,
11010253Ssteve.reinhardt@amd.com              process->getSyscallArg(tc, index));
1118149SChris.Emmons@ARM.com
1128149SChris.Emmons@ARM.com    return 0;
1138149SChris.Emmons@ARM.com}
1148149SChris.Emmons@ARM.com
1158149SChris.Emmons@ARM.com
1168149SChris.Emmons@ARM.comSyscallReturn
1173114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1182680Sktlim@umich.edu         ThreadContext *tc)
119360SN/A{
1206029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1216029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1226701Sgblack@eecs.umich.edu        int index = 0;
1235958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1246701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1256029Ssteve.reinhardt@amd.com    } else {
1266029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1276029Ssteve.reinhardt@amd.com        tc->halt();
1282834Sksewell@umich.edu    }
129360SN/A
1301458SN/A    return 1;
131360SN/A}
132360SN/A
133360SN/A
1341450SN/ASyscallReturn
1356109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1366109Ssanchezd@stanford.edu              ThreadContext *tc)
1376109Ssanchezd@stanford.edu{
13810483Swiseveri@student.ethz.ch    // halt all threads belonging to this process
13910483Swiseveri@student.ethz.ch    for (auto i: process->contextIds) {
14010483Swiseveri@student.ethz.ch        process->system->getThreadContext(i)->halt();
14110483Swiseveri@student.ethz.ch    }
14210483Swiseveri@student.ethz.ch
14310483Swiseveri@student.ethz.ch    if (!process->system->numRunningContexts()) {
14410483Swiseveri@student.ethz.ch        // all threads belonged to this process... exit simulator
14510483Swiseveri@student.ethz.ch        int index = 0;
14610483Swiseveri@student.ethz.ch        exitSimLoop("target called exit()",
14710483Swiseveri@student.ethz.ch                    process->getSyscallArg(tc, index) & 0xff);
14810483Swiseveri@student.ethz.ch    }
1496109Ssanchezd@stanford.edu
1506109Ssanchezd@stanford.edu    return 1;
1516109Ssanchezd@stanford.edu}
1526109Ssanchezd@stanford.edu
1536109Ssanchezd@stanford.edu
1546109Ssanchezd@stanford.eduSyscallReturn
1553114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
156360SN/A{
15710318Sandreas.hansson@arm.com    return (int)PageBytes;
158360SN/A}
159360SN/A
160360SN/A
1611450SN/ASyscallReturn
1625748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
163360SN/A{
164360SN/A    // change brk addr to first arg
1656701Sgblack@eecs.umich.edu    int index = 0;
1666701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1675748SSteve.Reinhardt@amd.com
1685748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1695748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1705748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1715748SSteve.Reinhardt@amd.com        return p->brk_point;
1725748SSteve.Reinhardt@amd.com
1735748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1745748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1752474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
17610318Sandreas.hansson@arm.com                                PageBytes); !gen.done(); gen.next()) {
1775748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
17810318Sandreas.hansson@arm.com                p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
1796687Stjones1@inf.ed.ac.uk
1806687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1816687Stjones1@inf.ed.ac.uk            else {
1826687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1838852Sandreas.hansson@arm.com                SETranslatingPortProxy &tp = tc->getMemProxy();
1846687Stjones1@inf.ed.ac.uk
1856687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
18610318Sandreas.hansson@arm.com                Addr next_page = roundUp(gen.addr(), PageBytes);
1876687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1888852Sandreas.hansson@arm.com                tp.memsetBlob(gen.addr(), zero, size_needed);
18910318Sandreas.hansson@arm.com                if (gen.addr() + PageBytes > next_page &&
1906687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1916687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1926687Stjones1@inf.ed.ac.uk                {
19310318Sandreas.hansson@arm.com                    size_needed = PageBytes - size_needed;
1948852Sandreas.hansson@arm.com                    tp.memsetBlob(next_page, zero, size_needed);
1956687Stjones1@inf.ed.ac.uk                }
1966687Stjones1@inf.ed.ac.uk            }
1972474SN/A        }
1981450SN/A    }
1995748SSteve.Reinhardt@amd.com
2005748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
2011458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
2021458SN/A    return p->brk_point;
203360SN/A}
204360SN/A
205360SN/A
2061450SN/ASyscallReturn
2073114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
208360SN/A{
2096701Sgblack@eecs.umich.edu    int index = 0;
2106701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
2117508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
2127508Stjones1@inf.ed.ac.uk    int status = 0;
2137508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2147508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2151970SN/A    if (status >= 0)
2161970SN/A        p->free_fd(target_fd);
2171970SN/A    return status;
218360SN/A}
219360SN/A
220360SN/A
2211450SN/ASyscallReturn
2223114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
223360SN/A{
2246701Sgblack@eecs.umich.edu    int index = 0;
2256701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2266701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2276701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2286701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
229360SN/A
230360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
231360SN/A
232360SN/A    if (bytes_read != -1)
2338706Sandreas.hansson@arm.com        bufArg.copyOut(tc->getMemProxy());
234360SN/A
2351458SN/A    return bytes_read;
236360SN/A}
237360SN/A
2381450SN/ASyscallReturn
2393114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
240360SN/A{
2416701Sgblack@eecs.umich.edu    int index = 0;
2426701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2436701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2446701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2456701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
246360SN/A
2478706Sandreas.hansson@arm.com    bufArg.copyIn(tc->getMemProxy());
248360SN/A
249360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
250360SN/A
251360SN/A    fsync(fd);
252360SN/A
2531458SN/A    return bytes_written;
254360SN/A}
255360SN/A
256360SN/A
2571450SN/ASyscallReturn
2583114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
259360SN/A{
2606701Sgblack@eecs.umich.edu    int index = 0;
2616701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2626701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2636701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
264360SN/A
265360SN/A    off_t result = lseek(fd, offs, whence);
266360SN/A
2671458SN/A    return (result == (off_t)-1) ? -errno : result;
268360SN/A}
269360SN/A
270360SN/A
2711450SN/ASyscallReturn
2724118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2734118Sgblack@eecs.umich.edu{
2746701Sgblack@eecs.umich.edu    int index = 0;
2756701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2766701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2776701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2786701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2796701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2804118Sgblack@eecs.umich.edu
2814118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2824118Sgblack@eecs.umich.edu
2834118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2844118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2854118Sgblack@eecs.umich.edu
2864118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2874118Sgblack@eecs.umich.edu        //The seek failed.
2884118Sgblack@eecs.umich.edu        return -errno;
2894118Sgblack@eecs.umich.edu    } else {
2906111Ssteve.reinhardt@amd.com        // The seek succeeded.
2916111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2926111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2936111Ssteve.reinhardt@amd.com        // target platform
2944118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2954118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2968706Sandreas.hansson@arm.com        result_buf.copyOut(tc->getMemProxy());
2974118Sgblack@eecs.umich.edu        return 0;
2984118Sgblack@eecs.umich.edu    }
2994118Sgblack@eecs.umich.edu
3004118Sgblack@eecs.umich.edu
3014118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
3024118Sgblack@eecs.umich.edu}
3034118Sgblack@eecs.umich.edu
3044118Sgblack@eecs.umich.edu
3054118Sgblack@eecs.umich.eduSyscallReturn
3063114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
307360SN/A{
308360SN/A    // given that we don't really implement mmap, munmap is really easy
3091458SN/A    return 0;
310360SN/A}
311360SN/A
312360SN/A
313360SN/Aconst char *hostname = "m5.eecs.umich.edu";
314360SN/A
3151450SN/ASyscallReturn
3163114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
317360SN/A{
3186701Sgblack@eecs.umich.edu    int index = 0;
3196701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3206701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3216701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
322360SN/A
323360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
324360SN/A
3258706Sandreas.hansson@arm.com    name.copyOut(tc->getMemProxy());
326360SN/A
3271458SN/A    return 0;
328360SN/A}
329360SN/A
3301450SN/ASyscallReturn
3315513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3325513SMichael.Adler@intel.com{
3335513SMichael.Adler@intel.com    int result = 0;
3346731Svince@csl.cornell.edu    int index = 0;
3356701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3366701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3376701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3385513SMichael.Adler@intel.com
3395513SMichael.Adler@intel.com    // Is current working directory defined?
3405513SMichael.Adler@intel.com    string cwd = p->getcwd();
3415513SMichael.Adler@intel.com    if (!cwd.empty()) {
3425513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3435513SMichael.Adler@intel.com            // Buffer too small
3445513SMichael.Adler@intel.com            return -ERANGE;
3455513SMichael.Adler@intel.com        }
3465513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3475513SMichael.Adler@intel.com        result = cwd.length();
3485513SMichael.Adler@intel.com    }
3495513SMichael.Adler@intel.com    else {
3505513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3515513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3525513SMichael.Adler@intel.com        }
3535513SMichael.Adler@intel.com        else {
3545513SMichael.Adler@intel.com            result = -1;
3555513SMichael.Adler@intel.com        }
3565513SMichael.Adler@intel.com    }
3575513SMichael.Adler@intel.com
3588706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3595513SMichael.Adler@intel.com
3605513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3615513SMichael.Adler@intel.com}
3625513SMichael.Adler@intel.com
36310203SAli.Saidi@ARM.com/// Target open() handler.
36410203SAli.Saidi@ARM.comSyscallReturn
36510203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
36610203SAli.Saidi@ARM.com         ThreadContext *tc)
36710203SAli.Saidi@ARM.com{
36810203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 0);
36910203SAli.Saidi@ARM.com}
3705513SMichael.Adler@intel.com
3715513SMichael.Adler@intel.comSyscallReturn
37210203SAli.Saidi@ARM.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
37310203SAli.Saidi@ARM.com        int index)
3745513SMichael.Adler@intel.com{
3755513SMichael.Adler@intel.com    string path;
3765513SMichael.Adler@intel.com
3778852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
37810223Ssteve.reinhardt@amd.com        return -EFAULT;
3795513SMichael.Adler@intel.com
3805513SMichael.Adler@intel.com    // Adjust path for current working directory
3815513SMichael.Adler@intel.com    path = p->fullPath(path);
3825513SMichael.Adler@intel.com
3836701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3846701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3856701Sgblack@eecs.umich.edu
3866701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3875513SMichael.Adler@intel.com
3885513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3895513SMichael.Adler@intel.com
3908706Sandreas.hansson@arm.com    buf.copyOut(tc->getMemProxy());
3915513SMichael.Adler@intel.com
3925513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3935513SMichael.Adler@intel.com}
3945513SMichael.Adler@intel.com
3955513SMichael.Adler@intel.comSyscallReturn
3963114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
397511SN/A{
3981706SN/A    string path;
399360SN/A
4006701Sgblack@eecs.umich.edu    int index = 0;
4018852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
40210223Ssteve.reinhardt@amd.com        return -EFAULT;
403511SN/A
4043669Sbinkertn@umich.edu    // Adjust path for current working directory
4053669Sbinkertn@umich.edu    path = p->fullPath(path);
4063669Sbinkertn@umich.edu
407511SN/A    int result = unlink(path.c_str());
4081458SN/A    return (result == -1) ? -errno : result;
409511SN/A}
410511SN/A
4115513SMichael.Adler@intel.com
4125513SMichael.Adler@intel.comSyscallReturn
4135513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4145513SMichael.Adler@intel.com{
4155513SMichael.Adler@intel.com    string path;
4165513SMichael.Adler@intel.com
4176701Sgblack@eecs.umich.edu    int index = 0;
4188852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
41910223Ssteve.reinhardt@amd.com        return -EFAULT;
4205513SMichael.Adler@intel.com
4215513SMichael.Adler@intel.com    // Adjust path for current working directory
4225513SMichael.Adler@intel.com    path = p->fullPath(path);
4235513SMichael.Adler@intel.com
4246701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4255513SMichael.Adler@intel.com
4265513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4275513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4285513SMichael.Adler@intel.com}
4295513SMichael.Adler@intel.com
4301450SN/ASyscallReturn
4313114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
432511SN/A{
4331706SN/A    string old_name;
434511SN/A
4356701Sgblack@eecs.umich.edu    int index = 0;
4368852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
4371458SN/A        return -EFAULT;
438511SN/A
4391706SN/A    string new_name;
440511SN/A
4418852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
4421458SN/A        return -EFAULT;
443511SN/A
4443669Sbinkertn@umich.edu    // Adjust path for current working directory
4453669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4463669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4473669Sbinkertn@umich.edu
4481706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4491458SN/A    return (result == -1) ? -errno : result;
450511SN/A}
451511SN/A
4521706SN/ASyscallReturn
4533114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4541706SN/A{
4551706SN/A    string path;
4561706SN/A
4576701Sgblack@eecs.umich.edu    int index = 0;
4588852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
4591706SN/A        return -EFAULT;
4601706SN/A
4616701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4621706SN/A
4633669Sbinkertn@umich.edu    // Adjust path for current working directory
4643669Sbinkertn@umich.edu    path = p->fullPath(path);
4653669Sbinkertn@umich.edu
4661706SN/A    int result = truncate(path.c_str(), length);
4671706SN/A    return (result == -1) ? -errno : result;
4681706SN/A}
4691706SN/A
4701706SN/ASyscallReturn
4716111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4726111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4731706SN/A{
4746701Sgblack@eecs.umich.edu    int index = 0;
4756701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4761706SN/A
4771706SN/A    if (fd < 0)
4781706SN/A        return -EBADF;
4791706SN/A
4806701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4811706SN/A
4821706SN/A    int result = ftruncate(fd, length);
4831706SN/A    return (result == -1) ? -errno : result;
4841706SN/A}
4851999SN/A
4861999SN/ASyscallReturn
4876703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4886703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4896703Svince@csl.cornell.edu{
4906703Svince@csl.cornell.edu    int index = 0;
4916703Svince@csl.cornell.edu    string path;
4926703Svince@csl.cornell.edu
4938852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
4946703Svince@csl.cornell.edu       return -EFAULT;
4956703Svince@csl.cornell.edu
4966744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
4976703Svince@csl.cornell.edu
4986703Svince@csl.cornell.edu    // Adjust path for current working directory
4996703Svince@csl.cornell.edu    path = process->fullPath(path);
5006703Svince@csl.cornell.edu
5016744SAli.Saidi@arm.com#if NO_STAT64
5026744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
5036744SAli.Saidi@arm.com#else
5046703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
5056744SAli.Saidi@arm.com#endif
5066703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
5076703Svince@csl.cornell.edu}
5086703Svince@csl.cornell.edu
5096703Svince@csl.cornell.eduSyscallReturn
5106685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
5116685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
5126685Stjones1@inf.ed.ac.uk{
5136701Sgblack@eecs.umich.edu    int index = 0;
5146701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5156685Stjones1@inf.ed.ac.uk
5166685Stjones1@inf.ed.ac.uk    if (fd < 0)
5176685Stjones1@inf.ed.ac.uk        return -EBADF;
5186685Stjones1@inf.ed.ac.uk
5196744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5206685Stjones1@inf.ed.ac.uk
5216744SAli.Saidi@arm.com#if NO_STAT64
5226744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5236744SAli.Saidi@arm.com#else
5246685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5256744SAli.Saidi@arm.com#endif
5266685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5276685Stjones1@inf.ed.ac.uk}
5286685Stjones1@inf.ed.ac.uk
5296685Stjones1@inf.ed.ac.ukSyscallReturn
5305513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5315513SMichael.Adler@intel.com{
5325513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5335513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5345513SMichael.Adler@intel.com    // changing anything.
5355513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5365513SMichael.Adler@intel.com    umask(oldMask);
5375521Snate@binkert.org    return (int)oldMask;
5385513SMichael.Adler@intel.com}
5395513SMichael.Adler@intel.com
5405513SMichael.Adler@intel.comSyscallReturn
5413114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5421999SN/A{
5431999SN/A    string path;
5441999SN/A
5456701Sgblack@eecs.umich.edu    int index = 0;
5468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
5471999SN/A        return -EFAULT;
5481999SN/A
5491999SN/A    /* XXX endianess */
5506701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5511999SN/A    uid_t hostOwner = owner;
5526701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5531999SN/A    gid_t hostGroup = group;
5541999SN/A
5553669Sbinkertn@umich.edu    // Adjust path for current working directory
5563669Sbinkertn@umich.edu    path = p->fullPath(path);
5573669Sbinkertn@umich.edu
5581999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5591999SN/A    return (result == -1) ? -errno : result;
5601999SN/A}
5611999SN/A
5621999SN/ASyscallReturn
5633114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5641999SN/A{
5656701Sgblack@eecs.umich.edu    int index = 0;
5666701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5671999SN/A
5681999SN/A    if (fd < 0)
5691999SN/A        return -EBADF;
5701999SN/A
5711999SN/A    /* XXX endianess */
5726701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5731999SN/A    uid_t hostOwner = owner;
5746701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5751999SN/A    gid_t hostGroup = group;
5761999SN/A
5771999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5781999SN/A    return (result == -1) ? -errno : result;
5791999SN/A}
5802093SN/A
5812093SN/A
5822093SN/ASyscallReturn
5833114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5843079Sstever@eecs.umich.edu{
5856701Sgblack@eecs.umich.edu    int index = 0;
5866701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5873079Sstever@eecs.umich.edu    if (fd < 0)
5883079Sstever@eecs.umich.edu        return -EBADF;
5893079Sstever@eecs.umich.edu
5906701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5915282Srstrong@cs.ucsd.edu
5923079Sstever@eecs.umich.edu    int result = dup(fd);
5936111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5946111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
5953079Sstever@eecs.umich.edu}
5963079Sstever@eecs.umich.edu
5973079Sstever@eecs.umich.edu
5983079Sstever@eecs.umich.eduSyscallReturn
5993114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
6002680Sktlim@umich.edu          ThreadContext *tc)
6012093SN/A{
6026701Sgblack@eecs.umich.edu    int index = 0;
6036701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6042093SN/A
6052093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
6062093SN/A        return -EBADF;
6072093SN/A
6086701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6092093SN/A    switch (cmd) {
6102093SN/A      case 0: // F_DUPFD
6112093SN/A        // if we really wanted to support this, we'd need to do it
6122093SN/A        // in the target fd space.
6132093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
6142093SN/A        return -EMFILE;
6152093SN/A
6162093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6172093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6182093SN/A        return 0;
6192093SN/A
6202093SN/A      case 3: // F_GETFL (get file flags)
6212093SN/A      case 4: // F_SETFL (set file flags)
6222093SN/A        // not sure if this is totally valid, but we'll pass it through
6232093SN/A        // to the underlying OS
6242093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6252093SN/A        return fcntl(process->sim_fd(fd), cmd);
6262093SN/A        // return 0;
6272093SN/A
6282093SN/A      case 7: // F_GETLK  (get lock)
6292093SN/A      case 8: // F_SETLK  (set lock)
6302093SN/A      case 9: // F_SETLKW (set lock and wait)
6312093SN/A        // don't mess with file locking... just act like it's OK
6322093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6332093SN/A        return 0;
6342093SN/A
6352093SN/A      default:
6362093SN/A        warn("Unknown fcntl command %d\n", cmd);
6372093SN/A        return 0;
6382093SN/A    }
6392093SN/A}
6402093SN/A
6412238SN/ASyscallReturn
6423114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6432687Sksewell@umich.edu            ThreadContext *tc)
6442687Sksewell@umich.edu{
6456701Sgblack@eecs.umich.edu    int index = 0;
6466701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6472687Sksewell@umich.edu
6482687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6492687Sksewell@umich.edu        return -EBADF;
6502687Sksewell@umich.edu
6516701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6522687Sksewell@umich.edu    switch (cmd) {
6532687Sksewell@umich.edu      case 33: //F_GETLK64
6542687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6552687Sksewell@umich.edu        return -EMFILE;
6562687Sksewell@umich.edu
6572687Sksewell@umich.edu      case 34: // F_SETLK64
6582687Sksewell@umich.edu      case 35: // F_SETLKW64
6592687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6602687Sksewell@umich.edu        return -EMFILE;
6612687Sksewell@umich.edu
6622687Sksewell@umich.edu      default:
6632687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6642687Sksewell@umich.edu        // to the underlying OS
6652687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6662687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6672687Sksewell@umich.edu        // return 0;
6682687Sksewell@umich.edu    }
6692687Sksewell@umich.edu}
6702687Sksewell@umich.edu
6712687Sksewell@umich.eduSyscallReturn
6723114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6732680Sktlim@umich.edu         ThreadContext *tc)
6742238SN/A{
6752238SN/A    int fds[2], sim_fds[2];
6762238SN/A    int pipe_retval = pipe(fds);
6772093SN/A
6782238SN/A    if (pipe_retval < 0) {
6792238SN/A        // error
6802238SN/A        return pipe_retval;
6812238SN/A    }
6822238SN/A
6835282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6845282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6852238SN/A
6865282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6872238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6882238SN/A    // the return value of the function, and fd[1] is returned in r20.
6892680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6902238SN/A    return sim_fds[0];
6912238SN/A}
6922238SN/A
6932238SN/A
6942238SN/ASyscallReturn
6953114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6962680Sktlim@umich.edu           ThreadContext *tc)
6972238SN/A{
6982238SN/A    // Make up a PID.  There's no interprocess communication in
6992238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7002238SN/A    // not getting a unique value.
7012238SN/A
7023114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
7033114Sgblack@eecs.umich.edu    return process->pid();
7042238SN/A}
7052238SN/A
7062238SN/A
7072238SN/ASyscallReturn
7083114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7092680Sktlim@umich.edu           ThreadContext *tc)
7102238SN/A{
7112238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
7122238SN/A    // simulation to be deterministic.
7132238SN/A
7142238SN/A    // EUID goes in r20.
7153114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7165543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7172238SN/A}
7182238SN/A
7192238SN/A
7202238SN/ASyscallReturn
7213114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7222680Sktlim@umich.edu           ThreadContext *tc)
7232238SN/A{
7242238SN/A    // Get current group ID.  EGID goes in r20.
7253114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7263114Sgblack@eecs.umich.edu    return process->gid();
7272238SN/A}
7282238SN/A
7292238SN/A
7302238SN/ASyscallReturn
7313114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7322680Sktlim@umich.edu           ThreadContext *tc)
7332238SN/A{
7342238SN/A    // can't fathom why a benchmark would call this.
7356701Sgblack@eecs.umich.edu    int index = 0;
7366701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7372238SN/A    return 0;
7382238SN/A}
7392238SN/A
7402238SN/ASyscallReturn
7413114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7422680Sktlim@umich.edu           ThreadContext *tc)
7432238SN/A{
7442238SN/A    // Make up a PID.  There's no interprocess communication in
7452238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7462238SN/A    // not getting a unique value.
7472238SN/A
7483114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7493114Sgblack@eecs.umich.edu    return process->pid();
7502238SN/A}
7512238SN/A
7522238SN/ASyscallReturn
7533114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7542680Sktlim@umich.edu           ThreadContext *tc)
7552238SN/A{
7563114Sgblack@eecs.umich.edu    return process->ppid();
7572238SN/A}
7582238SN/A
7592238SN/ASyscallReturn
7603114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7612680Sktlim@umich.edu           ThreadContext *tc)
7622238SN/A{
7635543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7642238SN/A}
7652238SN/A
7662238SN/ASyscallReturn
7673114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7682680Sktlim@umich.edu           ThreadContext *tc)
7692238SN/A{
7705543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7712238SN/A}
7722238SN/A
7732238SN/ASyscallReturn
7743114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7752680Sktlim@umich.edu           ThreadContext *tc)
7762238SN/A{
7773114Sgblack@eecs.umich.edu    return process->gid();
7782238SN/A}
7792238SN/A
7802238SN/ASyscallReturn
7813114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7822680Sktlim@umich.edu           ThreadContext *tc)
7832238SN/A{
7843114Sgblack@eecs.umich.edu    return process->egid();
7852238SN/A}
7862238SN/A
7872238SN/A
7886109Ssanchezd@stanford.eduSyscallReturn
7896109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7906109Ssanchezd@stanford.edu           ThreadContext *tc)
7916109Ssanchezd@stanford.edu{
7926701Sgblack@eecs.umich.edu    int index = 0;
7936701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7946701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
7956701Sgblack@eecs.umich.edu
7966109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
7976701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
7986701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
7996109Ssanchezd@stanford.edu
8006109Ssanchezd@stanford.edu
8016701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
8026111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
8036111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
8046111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
8056701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
8066109Ssanchezd@stanford.edu    }
8076109Ssanchezd@stanford.edu
8086111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
8096109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
8106109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
8116109Ssanchezd@stanford.edu
8126109Ssanchezd@stanford.edu        ctc->clearArchRegs();
8136109Ssanchezd@stanford.edu
8146111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8156109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8166111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8176109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8186109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8196109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8206109Ssanchezd@stanford.edu
8216111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8226109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8236109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8246109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8256109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8266337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8276109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8286109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8299375Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
8306109Ssanchezd@stanford.edu
8316109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8326109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8338149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8348149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8356109Ssanchezd@stanford.edu        #else
8366109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8376109Ssanchezd@stanford.edu        #endif
8386109Ssanchezd@stanford.edu
8396111Ssteve.reinhardt@amd.com        // Set up stack register
8406701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8416109Ssanchezd@stanford.edu
8426111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8436111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8446109Ssanchezd@stanford.edu
8456111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8466109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8476110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8486109Ssanchezd@stanford.edu        #endif
8496109Ssanchezd@stanford.edu
8506111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8516111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8526109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8536109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8546109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8556109Ssanchezd@stanford.edu        #endif
8566109Ssanchezd@stanford.edu
8577720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8586109Ssanchezd@stanford.edu
8596109Ssanchezd@stanford.edu        ctc->activate();
8606109Ssanchezd@stanford.edu
8616109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8626109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8636109Ssanchezd@stanford.edu        return 1;
8646109Ssanchezd@stanford.edu    } else {
8656109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8666109Ssanchezd@stanford.edu        return 0;
8676109Ssanchezd@stanford.edu    }
8686109Ssanchezd@stanford.edu}
8696109Ssanchezd@stanford.edu
8709455Smitch.hayenga+gem5@gmail.comSyscallReturn
87110203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc,
87210203SAli.Saidi@ARM.com        int index)
8739455Smitch.hayenga+gem5@gmail.com{
8749455Smitch.hayenga+gem5@gmail.com    string path;
8759455Smitch.hayenga+gem5@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
87610223Ssteve.reinhardt@amd.com        return -EFAULT;
8779455Smitch.hayenga+gem5@gmail.com
8789455Smitch.hayenga+gem5@gmail.com    // Adjust path for current working directory
8799455Smitch.hayenga+gem5@gmail.com    path = p->fullPath(path);
8809455Smitch.hayenga+gem5@gmail.com
8819455Smitch.hayenga+gem5@gmail.com    mode_t mode = p->getSyscallArg(tc, index);
8829455Smitch.hayenga+gem5@gmail.com
8839455Smitch.hayenga+gem5@gmail.com    int result = access(path.c_str(), mode);
8849455Smitch.hayenga+gem5@gmail.com    return (result == -1) ? -errno : result;
8859455Smitch.hayenga+gem5@gmail.com}
88610203SAli.Saidi@ARM.com
88710203SAli.Saidi@ARM.comSyscallReturn
88810203SAli.Saidi@ARM.comaccessFunc(SyscallDesc *desc, int callnum, LiveProcess *p, ThreadContext *tc)
88910203SAli.Saidi@ARM.com{
89010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, p, tc, 0);
89110203SAli.Saidi@ARM.com}
89210203SAli.Saidi@ARM.com
893