syscall_emul.cc revision 7680
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"
40360SN/A#include "sim/syscall_emul.hh"
412474SN/A#include "base/chunk_generator.hh"
42360SN/A#include "base/trace.hh"
436658Snate@binkert.org#include "config/the_isa.hh"
442680Sktlim@umich.edu#include "cpu/thread_context.hh"
451717SN/A#include "cpu/base.hh"
462474SN/A#include "mem/page_table.hh"
47360SN/A#include "sim/process.hh"
486029Ssteve.reinhardt@amd.com#include "sim/system.hh"
492667Sstever@eecs.umich.edu#include "sim/sim_exit.hh"
50360SN/A
51360SN/Ausing namespace std;
522107SN/Ausing namespace TheISA;
53360SN/A
54360SN/Avoid
553114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
56360SN/A{
576702Sgblack@eecs.umich.edu#if TRACING_ON
586701Sgblack@eecs.umich.edu    int index = 0;
596702Sgblack@eecs.umich.edu#endif
606111Ssteve.reinhardt@amd.com    DPRINTFR(SyscallVerbose,
616111Ssteve.reinhardt@amd.com             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
626111Ssteve.reinhardt@amd.com             curTick, tc->getCpuPtr()->name(), name,
636701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
646701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
656701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
666701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index));
67360SN/A
682680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
69360SN/A
702495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
712680Sktlim@umich.edu             curTick,tc->getCpuPtr()->name(), name, retval.value());
72360SN/A
731450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
745958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
75360SN/A}
76360SN/A
77360SN/A
781450SN/ASyscallReturn
793114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
802680Sktlim@umich.edu                  ThreadContext *tc)
81360SN/A{
821969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
832484SN/A
842484SN/A    return 1;
85360SN/A}
86360SN/A
87360SN/A
881450SN/ASyscallReturn
893114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
902680Sktlim@umich.edu           ThreadContext *tc)
91360SN/A{
926701Sgblack@eecs.umich.edu    int index = 0;
931969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
946701Sgblack@eecs.umich.edu         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
95360SN/A
961458SN/A    return 0;
97360SN/A}
98360SN/A
99360SN/A
1001450SN/ASyscallReturn
1013114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1022680Sktlim@umich.edu         ThreadContext *tc)
103360SN/A{
1046029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1056029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1066701Sgblack@eecs.umich.edu        int index = 0;
1075958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1086701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1096029Ssteve.reinhardt@amd.com    } else {
1106029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1116029Ssteve.reinhardt@amd.com        tc->halt();
1122834Sksewell@umich.edu    }
113360SN/A
1141458SN/A    return 1;
115360SN/A}
116360SN/A
117360SN/A
1181450SN/ASyscallReturn
1196109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1206109Ssanchezd@stanford.edu              ThreadContext *tc)
1216109Ssanchezd@stanford.edu{
1226109Ssanchezd@stanford.edu    // really should just halt all thread contexts belonging to this
1236109Ssanchezd@stanford.edu    // process in case there's another process running...
1246701Sgblack@eecs.umich.edu    int index = 0;
1256109Ssanchezd@stanford.edu    exitSimLoop("target called exit()",
1266701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index) & 0xff);
1276109Ssanchezd@stanford.edu
1286109Ssanchezd@stanford.edu    return 1;
1296109Ssanchezd@stanford.edu}
1306109Ssanchezd@stanford.edu
1316109Ssanchezd@stanford.edu
1326109Ssanchezd@stanford.eduSyscallReturn
1333114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
134360SN/A{
1352107SN/A    return (int)VMPageSize;
136360SN/A}
137360SN/A
138360SN/A
1391450SN/ASyscallReturn
1405748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
141360SN/A{
142360SN/A    // change brk addr to first arg
1436701Sgblack@eecs.umich.edu    int index = 0;
1446701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1455748SSteve.Reinhardt@amd.com
1465748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1475748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1485748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1495748SSteve.Reinhardt@amd.com        return p->brk_point;
1505748SSteve.Reinhardt@amd.com
1515748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1525748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1532474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1542474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1555748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
1562474SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1572474SN/A                                    VMPageSize);
1586687Stjones1@inf.ed.ac.uk
1596687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1606687Stjones1@inf.ed.ac.uk            else {
1616687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1626687Stjones1@inf.ed.ac.uk                TranslatingPort *tp = tc->getMemPort();
1636687Stjones1@inf.ed.ac.uk
1646687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
1656687Stjones1@inf.ed.ac.uk                Addr next_page = roundUp(gen.addr(), VMPageSize);
1666687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1676687Stjones1@inf.ed.ac.uk                tp->memsetBlob(gen.addr(), zero, size_needed);
1686687Stjones1@inf.ed.ac.uk                if (gen.addr() + VMPageSize > next_page &&
1696687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1706687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1716687Stjones1@inf.ed.ac.uk                {
1726687Stjones1@inf.ed.ac.uk                    size_needed = VMPageSize - size_needed;
1736687Stjones1@inf.ed.ac.uk                    tp->memsetBlob(next_page, zero, size_needed);
1746687Stjones1@inf.ed.ac.uk                }
1756687Stjones1@inf.ed.ac.uk            }
1762474SN/A        }
1771450SN/A    }
1785748SSteve.Reinhardt@amd.com
1795748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
1801458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1811458SN/A    return p->brk_point;
182360SN/A}
183360SN/A
184360SN/A
1851450SN/ASyscallReturn
1863114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
187360SN/A{
1886701Sgblack@eecs.umich.edu    int index = 0;
1896701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
1907508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
1917508Stjones1@inf.ed.ac.uk    int status = 0;
1927508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
1937508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
1941970SN/A    if (status >= 0)
1951970SN/A        p->free_fd(target_fd);
1961970SN/A    return status;
197360SN/A}
198360SN/A
199360SN/A
2001450SN/ASyscallReturn
2013114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
202360SN/A{
2036701Sgblack@eecs.umich.edu    int index = 0;
2046701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2056701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2066701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2076701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
208360SN/A
209360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
210360SN/A
211360SN/A    if (bytes_read != -1)
2122680Sktlim@umich.edu        bufArg.copyOut(tc->getMemPort());
213360SN/A
2141458SN/A    return bytes_read;
215360SN/A}
216360SN/A
2171450SN/ASyscallReturn
2183114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
219360SN/A{
2206701Sgblack@eecs.umich.edu    int index = 0;
2216701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2226701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2236701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2246701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
225360SN/A
2262680Sktlim@umich.edu    bufArg.copyIn(tc->getMemPort());
227360SN/A
228360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
229360SN/A
230360SN/A    fsync(fd);
231360SN/A
2321458SN/A    return bytes_written;
233360SN/A}
234360SN/A
235360SN/A
2361450SN/ASyscallReturn
2373114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
238360SN/A{
2396701Sgblack@eecs.umich.edu    int index = 0;
2406701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2416701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2426701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
243360SN/A
244360SN/A    off_t result = lseek(fd, offs, whence);
245360SN/A
2461458SN/A    return (result == (off_t)-1) ? -errno : result;
247360SN/A}
248360SN/A
249360SN/A
2501450SN/ASyscallReturn
2514118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2524118Sgblack@eecs.umich.edu{
2536701Sgblack@eecs.umich.edu    int index = 0;
2546701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2556701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2566701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2576701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2586701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2594118Sgblack@eecs.umich.edu
2604118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2614118Sgblack@eecs.umich.edu
2624118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2634118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2644118Sgblack@eecs.umich.edu
2654118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2664118Sgblack@eecs.umich.edu        //The seek failed.
2674118Sgblack@eecs.umich.edu        return -errno;
2684118Sgblack@eecs.umich.edu    } else {
2696111Ssteve.reinhardt@amd.com        // The seek succeeded.
2706111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2716111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2726111Ssteve.reinhardt@amd.com        // target platform
2734118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2744118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2754118Sgblack@eecs.umich.edu        result_buf.copyOut(tc->getMemPort());
2764118Sgblack@eecs.umich.edu        return 0;
2774118Sgblack@eecs.umich.edu    }
2784118Sgblack@eecs.umich.edu
2794118Sgblack@eecs.umich.edu
2804118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2814118Sgblack@eecs.umich.edu}
2824118Sgblack@eecs.umich.edu
2834118Sgblack@eecs.umich.edu
2844118Sgblack@eecs.umich.eduSyscallReturn
2853114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
286360SN/A{
287360SN/A    // given that we don't really implement mmap, munmap is really easy
2881458SN/A    return 0;
289360SN/A}
290360SN/A
291360SN/A
292360SN/Aconst char *hostname = "m5.eecs.umich.edu";
293360SN/A
2941450SN/ASyscallReturn
2953114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
296360SN/A{
2976701Sgblack@eecs.umich.edu    int index = 0;
2986701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2996701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3006701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
301360SN/A
302360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
303360SN/A
3042680Sktlim@umich.edu    name.copyOut(tc->getMemPort());
305360SN/A
3061458SN/A    return 0;
307360SN/A}
308360SN/A
3091450SN/ASyscallReturn
3105513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3115513SMichael.Adler@intel.com{
3125513SMichael.Adler@intel.com    int result = 0;
3136731Svince@csl.cornell.edu    int index = 0;
3146701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3156701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3166701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3175513SMichael.Adler@intel.com
3185513SMichael.Adler@intel.com    // Is current working directory defined?
3195513SMichael.Adler@intel.com    string cwd = p->getcwd();
3205513SMichael.Adler@intel.com    if (!cwd.empty()) {
3215513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3225513SMichael.Adler@intel.com            // Buffer too small
3235513SMichael.Adler@intel.com            return -ERANGE;
3245513SMichael.Adler@intel.com        }
3255513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3265513SMichael.Adler@intel.com        result = cwd.length();
3275513SMichael.Adler@intel.com    }
3285513SMichael.Adler@intel.com    else {
3295513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3305513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3315513SMichael.Adler@intel.com        }
3325513SMichael.Adler@intel.com        else {
3335513SMichael.Adler@intel.com            result = -1;
3345513SMichael.Adler@intel.com        }
3355513SMichael.Adler@intel.com    }
3365513SMichael.Adler@intel.com
3375513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3385513SMichael.Adler@intel.com
3395513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3405513SMichael.Adler@intel.com}
3415513SMichael.Adler@intel.com
3425513SMichael.Adler@intel.com
3435513SMichael.Adler@intel.comSyscallReturn
3445513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3455513SMichael.Adler@intel.com{
3465513SMichael.Adler@intel.com    string path;
3475513SMichael.Adler@intel.com
3486701Sgblack@eecs.umich.edu    int index = 0;
3496701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3505513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3515513SMichael.Adler@intel.com
3525513SMichael.Adler@intel.com    // Adjust path for current working directory
3535513SMichael.Adler@intel.com    path = p->fullPath(path);
3545513SMichael.Adler@intel.com
3556701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3566701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3576701Sgblack@eecs.umich.edu
3586701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3595513SMichael.Adler@intel.com
3605513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3615513SMichael.Adler@intel.com
3625513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3635513SMichael.Adler@intel.com
3645513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3655513SMichael.Adler@intel.com}
3665513SMichael.Adler@intel.com
3675513SMichael.Adler@intel.comSyscallReturn
3683114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
369511SN/A{
3701706SN/A    string path;
371360SN/A
3726701Sgblack@eecs.umich.edu    int index = 0;
3736701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3741450SN/A        return (TheISA::IntReg)-EFAULT;
375511SN/A
3763669Sbinkertn@umich.edu    // Adjust path for current working directory
3773669Sbinkertn@umich.edu    path = p->fullPath(path);
3783669Sbinkertn@umich.edu
379511SN/A    int result = unlink(path.c_str());
3801458SN/A    return (result == -1) ? -errno : result;
381511SN/A}
382511SN/A
3835513SMichael.Adler@intel.com
3845513SMichael.Adler@intel.comSyscallReturn
3855513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3865513SMichael.Adler@intel.com{
3875513SMichael.Adler@intel.com    string path;
3885513SMichael.Adler@intel.com
3896701Sgblack@eecs.umich.edu    int index = 0;
3906701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3915513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3925513SMichael.Adler@intel.com
3935513SMichael.Adler@intel.com    // Adjust path for current working directory
3945513SMichael.Adler@intel.com    path = p->fullPath(path);
3955513SMichael.Adler@intel.com
3966701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
3975513SMichael.Adler@intel.com
3985513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
3995513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4005513SMichael.Adler@intel.com}
4015513SMichael.Adler@intel.com
4021450SN/ASyscallReturn
4033114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
404511SN/A{
4051706SN/A    string old_name;
406511SN/A
4076701Sgblack@eecs.umich.edu    int index = 0;
4086701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index)))
4091458SN/A        return -EFAULT;
410511SN/A
4111706SN/A    string new_name;
412511SN/A
4136701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index)))
4141458SN/A        return -EFAULT;
415511SN/A
4163669Sbinkertn@umich.edu    // Adjust path for current working directory
4173669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4183669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4193669Sbinkertn@umich.edu
4201706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4211458SN/A    return (result == -1) ? -errno : result;
422511SN/A}
423511SN/A
4241706SN/ASyscallReturn
4253114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4261706SN/A{
4271706SN/A    string path;
4281706SN/A
4296701Sgblack@eecs.umich.edu    int index = 0;
4306701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
4311706SN/A        return -EFAULT;
4321706SN/A
4336701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4341706SN/A
4353669Sbinkertn@umich.edu    // Adjust path for current working directory
4363669Sbinkertn@umich.edu    path = p->fullPath(path);
4373669Sbinkertn@umich.edu
4381706SN/A    int result = truncate(path.c_str(), length);
4391706SN/A    return (result == -1) ? -errno : result;
4401706SN/A}
4411706SN/A
4421706SN/ASyscallReturn
4436111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4446111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4451706SN/A{
4466701Sgblack@eecs.umich.edu    int index = 0;
4476701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4481706SN/A
4491706SN/A    if (fd < 0)
4501706SN/A        return -EBADF;
4511706SN/A
4526701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4531706SN/A
4541706SN/A    int result = ftruncate(fd, length);
4551706SN/A    return (result == -1) ? -errno : result;
4561706SN/A}
4571999SN/A
4581999SN/ASyscallReturn
4596703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4606703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4616703Svince@csl.cornell.edu{
4626703Svince@csl.cornell.edu    int index = 0;
4636703Svince@csl.cornell.edu    string path;
4646703Svince@csl.cornell.edu
4656703Svince@csl.cornell.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index)))
4666703Svince@csl.cornell.edu       return -EFAULT;
4676703Svince@csl.cornell.edu
4686744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
4696703Svince@csl.cornell.edu
4706703Svince@csl.cornell.edu    // Adjust path for current working directory
4716703Svince@csl.cornell.edu    path = process->fullPath(path);
4726703Svince@csl.cornell.edu
4736744SAli.Saidi@arm.com#if NO_STAT64
4746744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
4756744SAli.Saidi@arm.com#else
4766703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
4776744SAli.Saidi@arm.com#endif
4786703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
4796703Svince@csl.cornell.edu}
4806703Svince@csl.cornell.edu
4816703Svince@csl.cornell.eduSyscallReturn
4826685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
4836685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
4846685Stjones1@inf.ed.ac.uk{
4856701Sgblack@eecs.umich.edu    int index = 0;
4866701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4876685Stjones1@inf.ed.ac.uk
4886685Stjones1@inf.ed.ac.uk    if (fd < 0)
4896685Stjones1@inf.ed.ac.uk        return -EBADF;
4906685Stjones1@inf.ed.ac.uk
4916744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
4926685Stjones1@inf.ed.ac.uk
4936744SAli.Saidi@arm.com#if NO_STAT64
4946744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
4956744SAli.Saidi@arm.com#else
4966685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
4976744SAli.Saidi@arm.com#endif
4986685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
4996685Stjones1@inf.ed.ac.uk}
5006685Stjones1@inf.ed.ac.uk
5016685Stjones1@inf.ed.ac.ukSyscallReturn
5025513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5035513SMichael.Adler@intel.com{
5045513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5055513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5065513SMichael.Adler@intel.com    // changing anything.
5075513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5085513SMichael.Adler@intel.com    umask(oldMask);
5095521Snate@binkert.org    return (int)oldMask;
5105513SMichael.Adler@intel.com}
5115513SMichael.Adler@intel.com
5125513SMichael.Adler@intel.comSyscallReturn
5133114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5141999SN/A{
5151999SN/A    string path;
5161999SN/A
5176701Sgblack@eecs.umich.edu    int index = 0;
5186701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
5191999SN/A        return -EFAULT;
5201999SN/A
5211999SN/A    /* XXX endianess */
5226701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5231999SN/A    uid_t hostOwner = owner;
5246701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5251999SN/A    gid_t hostGroup = group;
5261999SN/A
5273669Sbinkertn@umich.edu    // Adjust path for current working directory
5283669Sbinkertn@umich.edu    path = p->fullPath(path);
5293669Sbinkertn@umich.edu
5301999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5311999SN/A    return (result == -1) ? -errno : result;
5321999SN/A}
5331999SN/A
5341999SN/ASyscallReturn
5353114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5361999SN/A{
5376701Sgblack@eecs.umich.edu    int index = 0;
5386701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5391999SN/A
5401999SN/A    if (fd < 0)
5411999SN/A        return -EBADF;
5421999SN/A
5431999SN/A    /* XXX endianess */
5446701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5451999SN/A    uid_t hostOwner = owner;
5466701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5471999SN/A    gid_t hostGroup = group;
5481999SN/A
5491999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5501999SN/A    return (result == -1) ? -errno : result;
5511999SN/A}
5522093SN/A
5532093SN/A
5542093SN/ASyscallReturn
5553114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5563079Sstever@eecs.umich.edu{
5576701Sgblack@eecs.umich.edu    int index = 0;
5586701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5593079Sstever@eecs.umich.edu    if (fd < 0)
5603079Sstever@eecs.umich.edu        return -EBADF;
5613079Sstever@eecs.umich.edu
5626701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5635282Srstrong@cs.ucsd.edu
5643079Sstever@eecs.umich.edu    int result = dup(fd);
5656111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5666111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
5673079Sstever@eecs.umich.edu}
5683079Sstever@eecs.umich.edu
5693079Sstever@eecs.umich.edu
5703079Sstever@eecs.umich.eduSyscallReturn
5713114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
5722680Sktlim@umich.edu          ThreadContext *tc)
5732093SN/A{
5746701Sgblack@eecs.umich.edu    int index = 0;
5756701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5762093SN/A
5772093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
5782093SN/A        return -EBADF;
5792093SN/A
5806701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
5812093SN/A    switch (cmd) {
5822093SN/A      case 0: // F_DUPFD
5832093SN/A        // if we really wanted to support this, we'd need to do it
5842093SN/A        // in the target fd space.
5852093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
5862093SN/A        return -EMFILE;
5872093SN/A
5882093SN/A      case 1: // F_GETFD (get close-on-exec flag)
5892093SN/A      case 2: // F_SETFD (set close-on-exec flag)
5902093SN/A        return 0;
5912093SN/A
5922093SN/A      case 3: // F_GETFL (get file flags)
5932093SN/A      case 4: // F_SETFL (set file flags)
5942093SN/A        // not sure if this is totally valid, but we'll pass it through
5952093SN/A        // to the underlying OS
5962093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
5972093SN/A        return fcntl(process->sim_fd(fd), cmd);
5982093SN/A        // return 0;
5992093SN/A
6002093SN/A      case 7: // F_GETLK  (get lock)
6012093SN/A      case 8: // F_SETLK  (set lock)
6022093SN/A      case 9: // F_SETLKW (set lock and wait)
6032093SN/A        // don't mess with file locking... just act like it's OK
6042093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6052093SN/A        return 0;
6062093SN/A
6072093SN/A      default:
6082093SN/A        warn("Unknown fcntl command %d\n", cmd);
6092093SN/A        return 0;
6102093SN/A    }
6112093SN/A}
6122093SN/A
6132238SN/ASyscallReturn
6143114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6152687Sksewell@umich.edu            ThreadContext *tc)
6162687Sksewell@umich.edu{
6176701Sgblack@eecs.umich.edu    int index = 0;
6186701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6192687Sksewell@umich.edu
6202687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6212687Sksewell@umich.edu        return -EBADF;
6222687Sksewell@umich.edu
6236701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6242687Sksewell@umich.edu    switch (cmd) {
6252687Sksewell@umich.edu      case 33: //F_GETLK64
6262687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6272687Sksewell@umich.edu        return -EMFILE;
6282687Sksewell@umich.edu
6292687Sksewell@umich.edu      case 34: // F_SETLK64
6302687Sksewell@umich.edu      case 35: // F_SETLKW64
6312687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6322687Sksewell@umich.edu        return -EMFILE;
6332687Sksewell@umich.edu
6342687Sksewell@umich.edu      default:
6352687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6362687Sksewell@umich.edu        // to the underlying OS
6372687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6382687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6392687Sksewell@umich.edu        // return 0;
6402687Sksewell@umich.edu    }
6412687Sksewell@umich.edu}
6422687Sksewell@umich.edu
6432687Sksewell@umich.eduSyscallReturn
6443114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6452680Sktlim@umich.edu         ThreadContext *tc)
6462238SN/A{
6472238SN/A    int fds[2], sim_fds[2];
6482238SN/A    int pipe_retval = pipe(fds);
6492093SN/A
6502238SN/A    if (pipe_retval < 0) {
6512238SN/A        // error
6522238SN/A        return pipe_retval;
6532238SN/A    }
6542238SN/A
6555282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6565282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6572238SN/A
6585282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6592238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6602238SN/A    // the return value of the function, and fd[1] is returned in r20.
6612680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6622238SN/A    return sim_fds[0];
6632238SN/A}
6642238SN/A
6652238SN/A
6662238SN/ASyscallReturn
6673114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6682680Sktlim@umich.edu           ThreadContext *tc)
6692238SN/A{
6702238SN/A    // Make up a PID.  There's no interprocess communication in
6712238SN/A    // fake_syscall mode, so there's no way for a process to know it's
6722238SN/A    // not getting a unique value.
6732238SN/A
6743114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
6753114Sgblack@eecs.umich.edu    return process->pid();
6762238SN/A}
6772238SN/A
6782238SN/A
6792238SN/ASyscallReturn
6803114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6812680Sktlim@umich.edu           ThreadContext *tc)
6822238SN/A{
6832238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
6842238SN/A    // simulation to be deterministic.
6852238SN/A
6862238SN/A    // EUID goes in r20.
6873114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
6885543Ssaidi@eecs.umich.edu    return process->uid();              // UID
6892238SN/A}
6902238SN/A
6912238SN/A
6922238SN/ASyscallReturn
6933114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6942680Sktlim@umich.edu           ThreadContext *tc)
6952238SN/A{
6962238SN/A    // Get current group ID.  EGID goes in r20.
6973114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
6983114Sgblack@eecs.umich.edu    return process->gid();
6992238SN/A}
7002238SN/A
7012238SN/A
7022238SN/ASyscallReturn
7033114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7042680Sktlim@umich.edu           ThreadContext *tc)
7052238SN/A{
7062238SN/A    // can't fathom why a benchmark would call this.
7076701Sgblack@eecs.umich.edu    int index = 0;
7086701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7092238SN/A    return 0;
7102238SN/A}
7112238SN/A
7122238SN/ASyscallReturn
7133114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7142680Sktlim@umich.edu           ThreadContext *tc)
7152238SN/A{
7162238SN/A    // Make up a PID.  There's no interprocess communication in
7172238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7182238SN/A    // not getting a unique value.
7192238SN/A
7203114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7213114Sgblack@eecs.umich.edu    return process->pid();
7222238SN/A}
7232238SN/A
7242238SN/ASyscallReturn
7253114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7262680Sktlim@umich.edu           ThreadContext *tc)
7272238SN/A{
7283114Sgblack@eecs.umich.edu    return process->ppid();
7292238SN/A}
7302238SN/A
7312238SN/ASyscallReturn
7323114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7332680Sktlim@umich.edu           ThreadContext *tc)
7342238SN/A{
7355543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7362238SN/A}
7372238SN/A
7382238SN/ASyscallReturn
7393114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7402680Sktlim@umich.edu           ThreadContext *tc)
7412238SN/A{
7425543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7432238SN/A}
7442238SN/A
7452238SN/ASyscallReturn
7463114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7472680Sktlim@umich.edu           ThreadContext *tc)
7482238SN/A{
7493114Sgblack@eecs.umich.edu    return process->gid();
7502238SN/A}
7512238SN/A
7522238SN/ASyscallReturn
7533114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7542680Sktlim@umich.edu           ThreadContext *tc)
7552238SN/A{
7563114Sgblack@eecs.umich.edu    return process->egid();
7572238SN/A}
7582238SN/A
7592238SN/A
7606109Ssanchezd@stanford.eduSyscallReturn
7616109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7626109Ssanchezd@stanford.edu           ThreadContext *tc)
7636109Ssanchezd@stanford.edu{
7646701Sgblack@eecs.umich.edu    int index = 0;
7656701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7666701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
7676701Sgblack@eecs.umich.edu
7686109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
7696701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
7706701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
7716109Ssanchezd@stanford.edu
7726109Ssanchezd@stanford.edu
7736701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
7746111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
7756111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
7766111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
7776701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
7786109Ssanchezd@stanford.edu    }
7796109Ssanchezd@stanford.edu
7806111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
7816109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
7826109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
7836109Ssanchezd@stanford.edu
7846109Ssanchezd@stanford.edu        ctc->clearArchRegs();
7856109Ssanchezd@stanford.edu
7866111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
7876109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
7886111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
7896109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
7906109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
7916109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
7926109Ssanchezd@stanford.edu
7936111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
7946109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
7956109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
7966109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
7976109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
7986337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
7996109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8006109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8016109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
8026109Ssanchezd@stanford.edu
8036109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8046109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8056109Ssanchezd@stanford.edu        #else
8066109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8076109Ssanchezd@stanford.edu        #endif
8086109Ssanchezd@stanford.edu
8096111Ssteve.reinhardt@amd.com        // Set up stack register
8106701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8116109Ssanchezd@stanford.edu
8126111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8136111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8146109Ssanchezd@stanford.edu
8156111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8166109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8176110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8186109Ssanchezd@stanford.edu        #endif
8196109Ssanchezd@stanford.edu
8206111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8216111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8226109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8236109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8246109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8256109Ssanchezd@stanford.edu        #endif
8266109Ssanchezd@stanford.edu
8276109Ssanchezd@stanford.edu        ctc->setPC(tc->readNextPC());
8286109Ssanchezd@stanford.edu        ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
8296134Sgblack@eecs.umich.edu        ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst));
8306109Ssanchezd@stanford.edu
8316109Ssanchezd@stanford.edu        ctc->activate();
8326109Ssanchezd@stanford.edu
8336109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8346109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8356109Ssanchezd@stanford.edu        return 1;
8366109Ssanchezd@stanford.edu    } else {
8376109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8386109Ssanchezd@stanford.edu        return 0;
8396109Ssanchezd@stanford.edu    }
8406109Ssanchezd@stanford.edu}
8416109Ssanchezd@stanford.edu
842