syscall_emul.cc revision 6731
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
39360SN/A#include "sim/syscall_emul.hh"
402474SN/A#include "base/chunk_generator.hh"
41360SN/A#include "base/trace.hh"
426658Snate@binkert.org#include "config/the_isa.hh"
432680Sktlim@umich.edu#include "cpu/thread_context.hh"
441717SN/A#include "cpu/base.hh"
452474SN/A#include "mem/page_table.hh"
46360SN/A#include "sim/process.hh"
476029Ssteve.reinhardt@amd.com#include "sim/system.hh"
482667Sstever@eecs.umich.edu#include "sim/sim_exit.hh"
49360SN/A
50360SN/Ausing namespace std;
512107SN/Ausing namespace TheISA;
52360SN/A
53360SN/Avoid
543114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
55360SN/A{
566702Sgblack@eecs.umich.edu#if TRACING_ON
576701Sgblack@eecs.umich.edu    int index = 0;
586702Sgblack@eecs.umich.edu#endif
596111Ssteve.reinhardt@amd.com    DPRINTFR(SyscallVerbose,
606111Ssteve.reinhardt@amd.com             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
616111Ssteve.reinhardt@amd.com             curTick, tc->getCpuPtr()->name(), name,
626701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
636701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
646701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
656701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index));
66360SN/A
672680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
68360SN/A
692495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
702680Sktlim@umich.edu             curTick,tc->getCpuPtr()->name(), name, retval.value());
71360SN/A
721450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
735958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
74360SN/A}
75360SN/A
76360SN/A
771450SN/ASyscallReturn
783114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
792680Sktlim@umich.edu                  ThreadContext *tc)
80360SN/A{
811969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
822484SN/A
832484SN/A    return 1;
84360SN/A}
85360SN/A
86360SN/A
871450SN/ASyscallReturn
883114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
892680Sktlim@umich.edu           ThreadContext *tc)
90360SN/A{
916701Sgblack@eecs.umich.edu    int index = 0;
921969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
936701Sgblack@eecs.umich.edu         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
94360SN/A
951458SN/A    return 0;
96360SN/A}
97360SN/A
98360SN/A
991450SN/ASyscallReturn
1003114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1012680Sktlim@umich.edu         ThreadContext *tc)
102360SN/A{
1036029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1046029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1056701Sgblack@eecs.umich.edu        int index = 0;
1065958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1076701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1086029Ssteve.reinhardt@amd.com    } else {
1096029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1106029Ssteve.reinhardt@amd.com        tc->halt();
1112834Sksewell@umich.edu    }
112360SN/A
1131458SN/A    return 1;
114360SN/A}
115360SN/A
116360SN/A
1171450SN/ASyscallReturn
1186109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1196109Ssanchezd@stanford.edu              ThreadContext *tc)
1206109Ssanchezd@stanford.edu{
1216109Ssanchezd@stanford.edu    // really should just halt all thread contexts belonging to this
1226109Ssanchezd@stanford.edu    // process in case there's another process running...
1236701Sgblack@eecs.umich.edu    int index = 0;
1246109Ssanchezd@stanford.edu    exitSimLoop("target called exit()",
1256701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index) & 0xff);
1266109Ssanchezd@stanford.edu
1276109Ssanchezd@stanford.edu    return 1;
1286109Ssanchezd@stanford.edu}
1296109Ssanchezd@stanford.edu
1306109Ssanchezd@stanford.edu
1316109Ssanchezd@stanford.eduSyscallReturn
1323114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
133360SN/A{
1342107SN/A    return (int)VMPageSize;
135360SN/A}
136360SN/A
137360SN/A
1381450SN/ASyscallReturn
1395748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
140360SN/A{
141360SN/A    // change brk addr to first arg
1426701Sgblack@eecs.umich.edu    int index = 0;
1436701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1445748SSteve.Reinhardt@amd.com
1455748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1465748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1475748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1485748SSteve.Reinhardt@amd.com        return p->brk_point;
1495748SSteve.Reinhardt@amd.com
1505748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1515748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1522474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1532474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1545748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
1552474SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1562474SN/A                                    VMPageSize);
1576687Stjones1@inf.ed.ac.uk
1586687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1596687Stjones1@inf.ed.ac.uk            else {
1606687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1616687Stjones1@inf.ed.ac.uk                TranslatingPort *tp = tc->getMemPort();
1626687Stjones1@inf.ed.ac.uk
1636687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
1646687Stjones1@inf.ed.ac.uk                Addr next_page = roundUp(gen.addr(), VMPageSize);
1656687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1666687Stjones1@inf.ed.ac.uk                tp->memsetBlob(gen.addr(), zero, size_needed);
1676687Stjones1@inf.ed.ac.uk                if (gen.addr() + VMPageSize > next_page &&
1686687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1696687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1706687Stjones1@inf.ed.ac.uk                {
1716687Stjones1@inf.ed.ac.uk                    size_needed = VMPageSize - size_needed;
1726687Stjones1@inf.ed.ac.uk                    tp->memsetBlob(next_page, zero, size_needed);
1736687Stjones1@inf.ed.ac.uk                }
1746687Stjones1@inf.ed.ac.uk            }
1752474SN/A        }
1761450SN/A    }
1775748SSteve.Reinhardt@amd.com
1785748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
1791458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1801458SN/A    return p->brk_point;
181360SN/A}
182360SN/A
183360SN/A
1841450SN/ASyscallReturn
1853114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
186360SN/A{
1876701Sgblack@eecs.umich.edu    int index = 0;
1886701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
1891970SN/A    int status = close(p->sim_fd(target_fd));
1901970SN/A    if (status >= 0)
1911970SN/A        p->free_fd(target_fd);
1921970SN/A    return status;
193360SN/A}
194360SN/A
195360SN/A
1961450SN/ASyscallReturn
1973114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
198360SN/A{
1996701Sgblack@eecs.umich.edu    int index = 0;
2006701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2016701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2026701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2036701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
204360SN/A
205360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
206360SN/A
207360SN/A    if (bytes_read != -1)
2082680Sktlim@umich.edu        bufArg.copyOut(tc->getMemPort());
209360SN/A
2101458SN/A    return bytes_read;
211360SN/A}
212360SN/A
2131450SN/ASyscallReturn
2143114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
215360SN/A{
2166701Sgblack@eecs.umich.edu    int index = 0;
2176701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2186701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2196701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2206701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
221360SN/A
2222680Sktlim@umich.edu    bufArg.copyIn(tc->getMemPort());
223360SN/A
224360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
225360SN/A
226360SN/A    fsync(fd);
227360SN/A
2281458SN/A    return bytes_written;
229360SN/A}
230360SN/A
231360SN/A
2321450SN/ASyscallReturn
2333114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
234360SN/A{
2356701Sgblack@eecs.umich.edu    int index = 0;
2366701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2376701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2386701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
239360SN/A
240360SN/A    off_t result = lseek(fd, offs, whence);
241360SN/A
2421458SN/A    return (result == (off_t)-1) ? -errno : result;
243360SN/A}
244360SN/A
245360SN/A
2461450SN/ASyscallReturn
2474118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2484118Sgblack@eecs.umich.edu{
2496701Sgblack@eecs.umich.edu    int index = 0;
2506701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2516701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2526701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2536701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2546701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2554118Sgblack@eecs.umich.edu
2564118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2574118Sgblack@eecs.umich.edu
2584118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2594118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2604118Sgblack@eecs.umich.edu
2614118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2624118Sgblack@eecs.umich.edu        //The seek failed.
2634118Sgblack@eecs.umich.edu        return -errno;
2644118Sgblack@eecs.umich.edu    } else {
2656111Ssteve.reinhardt@amd.com        // The seek succeeded.
2666111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2676111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2686111Ssteve.reinhardt@amd.com        // target platform
2694118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2704118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2714118Sgblack@eecs.umich.edu        result_buf.copyOut(tc->getMemPort());
2724118Sgblack@eecs.umich.edu        return 0;
2734118Sgblack@eecs.umich.edu    }
2744118Sgblack@eecs.umich.edu
2754118Sgblack@eecs.umich.edu
2764118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2774118Sgblack@eecs.umich.edu}
2784118Sgblack@eecs.umich.edu
2794118Sgblack@eecs.umich.edu
2804118Sgblack@eecs.umich.eduSyscallReturn
2813114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
282360SN/A{
283360SN/A    // given that we don't really implement mmap, munmap is really easy
2841458SN/A    return 0;
285360SN/A}
286360SN/A
287360SN/A
288360SN/Aconst char *hostname = "m5.eecs.umich.edu";
289360SN/A
2901450SN/ASyscallReturn
2913114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
292360SN/A{
2936701Sgblack@eecs.umich.edu    int index = 0;
2946701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2956701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
2966701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
297360SN/A
298360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
299360SN/A
3002680Sktlim@umich.edu    name.copyOut(tc->getMemPort());
301360SN/A
3021458SN/A    return 0;
303360SN/A}
304360SN/A
3051450SN/ASyscallReturn
3065513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3075513SMichael.Adler@intel.com{
3085513SMichael.Adler@intel.com    int result = 0;
3096731Svince@csl.cornell.edu    int index = 0;
3106701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3116701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3126701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3135513SMichael.Adler@intel.com
3145513SMichael.Adler@intel.com    // Is current working directory defined?
3155513SMichael.Adler@intel.com    string cwd = p->getcwd();
3165513SMichael.Adler@intel.com    if (!cwd.empty()) {
3175513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3185513SMichael.Adler@intel.com            // Buffer too small
3195513SMichael.Adler@intel.com            return -ERANGE;
3205513SMichael.Adler@intel.com        }
3215513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3225513SMichael.Adler@intel.com        result = cwd.length();
3235513SMichael.Adler@intel.com    }
3245513SMichael.Adler@intel.com    else {
3255513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3265513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3275513SMichael.Adler@intel.com        }
3285513SMichael.Adler@intel.com        else {
3295513SMichael.Adler@intel.com            result = -1;
3305513SMichael.Adler@intel.com        }
3315513SMichael.Adler@intel.com    }
3325513SMichael.Adler@intel.com
3335513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3345513SMichael.Adler@intel.com
3355513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3365513SMichael.Adler@intel.com}
3375513SMichael.Adler@intel.com
3385513SMichael.Adler@intel.com
3395513SMichael.Adler@intel.comSyscallReturn
3405513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3415513SMichael.Adler@intel.com{
3425513SMichael.Adler@intel.com    string path;
3435513SMichael.Adler@intel.com
3446701Sgblack@eecs.umich.edu    int index = 0;
3456701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3465513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3475513SMichael.Adler@intel.com
3485513SMichael.Adler@intel.com    // Adjust path for current working directory
3495513SMichael.Adler@intel.com    path = p->fullPath(path);
3505513SMichael.Adler@intel.com
3516701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3526701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3536701Sgblack@eecs.umich.edu
3546701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3555513SMichael.Adler@intel.com
3565513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3575513SMichael.Adler@intel.com
3585513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3595513SMichael.Adler@intel.com
3605513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3615513SMichael.Adler@intel.com}
3625513SMichael.Adler@intel.com
3635513SMichael.Adler@intel.comSyscallReturn
3643114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
365511SN/A{
3661706SN/A    string path;
367360SN/A
3686701Sgblack@eecs.umich.edu    int index = 0;
3696701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3701450SN/A        return (TheISA::IntReg)-EFAULT;
371511SN/A
3723669Sbinkertn@umich.edu    // Adjust path for current working directory
3733669Sbinkertn@umich.edu    path = p->fullPath(path);
3743669Sbinkertn@umich.edu
375511SN/A    int result = unlink(path.c_str());
3761458SN/A    return (result == -1) ? -errno : result;
377511SN/A}
378511SN/A
3795513SMichael.Adler@intel.com
3805513SMichael.Adler@intel.comSyscallReturn
3815513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3825513SMichael.Adler@intel.com{
3835513SMichael.Adler@intel.com    string path;
3845513SMichael.Adler@intel.com
3856701Sgblack@eecs.umich.edu    int index = 0;
3866701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3875513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3885513SMichael.Adler@intel.com
3895513SMichael.Adler@intel.com    // Adjust path for current working directory
3905513SMichael.Adler@intel.com    path = p->fullPath(path);
3915513SMichael.Adler@intel.com
3926701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
3935513SMichael.Adler@intel.com
3945513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
3955513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3965513SMichael.Adler@intel.com}
3975513SMichael.Adler@intel.com
3981450SN/ASyscallReturn
3993114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
400511SN/A{
4011706SN/A    string old_name;
402511SN/A
4036701Sgblack@eecs.umich.edu    int index = 0;
4046701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index)))
4051458SN/A        return -EFAULT;
406511SN/A
4071706SN/A    string new_name;
408511SN/A
4096701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index)))
4101458SN/A        return -EFAULT;
411511SN/A
4123669Sbinkertn@umich.edu    // Adjust path for current working directory
4133669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4143669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4153669Sbinkertn@umich.edu
4161706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4171458SN/A    return (result == -1) ? -errno : result;
418511SN/A}
419511SN/A
4201706SN/ASyscallReturn
4213114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4221706SN/A{
4231706SN/A    string path;
4241706SN/A
4256701Sgblack@eecs.umich.edu    int index = 0;
4266701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
4271706SN/A        return -EFAULT;
4281706SN/A
4296701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4301706SN/A
4313669Sbinkertn@umich.edu    // Adjust path for current working directory
4323669Sbinkertn@umich.edu    path = p->fullPath(path);
4333669Sbinkertn@umich.edu
4341706SN/A    int result = truncate(path.c_str(), length);
4351706SN/A    return (result == -1) ? -errno : result;
4361706SN/A}
4371706SN/A
4381706SN/ASyscallReturn
4396111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4406111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4411706SN/A{
4426701Sgblack@eecs.umich.edu    int index = 0;
4436701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4441706SN/A
4451706SN/A    if (fd < 0)
4461706SN/A        return -EBADF;
4471706SN/A
4486701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4491706SN/A
4501706SN/A    int result = ftruncate(fd, length);
4511706SN/A    return (result == -1) ? -errno : result;
4521706SN/A}
4531999SN/A
4541999SN/ASyscallReturn
4556703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4566703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4576703Svince@csl.cornell.edu{
4586703Svince@csl.cornell.edu    int index = 0;
4596703Svince@csl.cornell.edu    string path;
4606703Svince@csl.cornell.edu
4616703Svince@csl.cornell.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index)))
4626703Svince@csl.cornell.edu       return -EFAULT;
4636703Svince@csl.cornell.edu
4646703Svince@csl.cornell.edu    loff_t length = process->getSyscallArg(tc, index, 64);
4656703Svince@csl.cornell.edu
4666703Svince@csl.cornell.edu    // Adjust path for current working directory
4676703Svince@csl.cornell.edu    path = process->fullPath(path);
4686703Svince@csl.cornell.edu
4696703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
4706703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
4716703Svince@csl.cornell.edu}
4726703Svince@csl.cornell.edu
4736703Svince@csl.cornell.eduSyscallReturn
4746685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
4756685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
4766685Stjones1@inf.ed.ac.uk{
4776701Sgblack@eecs.umich.edu    int index = 0;
4786701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4796685Stjones1@inf.ed.ac.uk
4806685Stjones1@inf.ed.ac.uk    if (fd < 0)
4816685Stjones1@inf.ed.ac.uk        return -EBADF;
4826685Stjones1@inf.ed.ac.uk
4836701Sgblack@eecs.umich.edu    loff_t length = process->getSyscallArg(tc, index, 64);
4846685Stjones1@inf.ed.ac.uk
4856685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
4866685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
4876685Stjones1@inf.ed.ac.uk}
4886685Stjones1@inf.ed.ac.uk
4896685Stjones1@inf.ed.ac.ukSyscallReturn
4905513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
4915513SMichael.Adler@intel.com{
4925513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
4935513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
4945513SMichael.Adler@intel.com    // changing anything.
4955513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
4965513SMichael.Adler@intel.com    umask(oldMask);
4975521Snate@binkert.org    return (int)oldMask;
4985513SMichael.Adler@intel.com}
4995513SMichael.Adler@intel.com
5005513SMichael.Adler@intel.comSyscallReturn
5013114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5021999SN/A{
5031999SN/A    string path;
5041999SN/A
5056701Sgblack@eecs.umich.edu    int index = 0;
5066701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
5071999SN/A        return -EFAULT;
5081999SN/A
5091999SN/A    /* XXX endianess */
5106701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5111999SN/A    uid_t hostOwner = owner;
5126701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5131999SN/A    gid_t hostGroup = group;
5141999SN/A
5153669Sbinkertn@umich.edu    // Adjust path for current working directory
5163669Sbinkertn@umich.edu    path = p->fullPath(path);
5173669Sbinkertn@umich.edu
5181999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5191999SN/A    return (result == -1) ? -errno : result;
5201999SN/A}
5211999SN/A
5221999SN/ASyscallReturn
5233114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5241999SN/A{
5256701Sgblack@eecs.umich.edu    int index = 0;
5266701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5271999SN/A
5281999SN/A    if (fd < 0)
5291999SN/A        return -EBADF;
5301999SN/A
5311999SN/A    /* XXX endianess */
5326701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5331999SN/A    uid_t hostOwner = owner;
5346701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5351999SN/A    gid_t hostGroup = group;
5361999SN/A
5371999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5381999SN/A    return (result == -1) ? -errno : result;
5391999SN/A}
5402093SN/A
5412093SN/A
5422093SN/ASyscallReturn
5433114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5443079Sstever@eecs.umich.edu{
5456701Sgblack@eecs.umich.edu    int index = 0;
5466701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5473079Sstever@eecs.umich.edu    if (fd < 0)
5483079Sstever@eecs.umich.edu        return -EBADF;
5493079Sstever@eecs.umich.edu
5506701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5515282Srstrong@cs.ucsd.edu
5523079Sstever@eecs.umich.edu    int result = dup(fd);
5536111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5546111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
5553079Sstever@eecs.umich.edu}
5563079Sstever@eecs.umich.edu
5573079Sstever@eecs.umich.edu
5583079Sstever@eecs.umich.eduSyscallReturn
5593114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
5602680Sktlim@umich.edu          ThreadContext *tc)
5612093SN/A{
5626701Sgblack@eecs.umich.edu    int index = 0;
5636701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5642093SN/A
5652093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
5662093SN/A        return -EBADF;
5672093SN/A
5686701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
5692093SN/A    switch (cmd) {
5702093SN/A      case 0: // F_DUPFD
5712093SN/A        // if we really wanted to support this, we'd need to do it
5722093SN/A        // in the target fd space.
5732093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
5742093SN/A        return -EMFILE;
5752093SN/A
5762093SN/A      case 1: // F_GETFD (get close-on-exec flag)
5772093SN/A      case 2: // F_SETFD (set close-on-exec flag)
5782093SN/A        return 0;
5792093SN/A
5802093SN/A      case 3: // F_GETFL (get file flags)
5812093SN/A      case 4: // F_SETFL (set file flags)
5822093SN/A        // not sure if this is totally valid, but we'll pass it through
5832093SN/A        // to the underlying OS
5842093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
5852093SN/A        return fcntl(process->sim_fd(fd), cmd);
5862093SN/A        // return 0;
5872093SN/A
5882093SN/A      case 7: // F_GETLK  (get lock)
5892093SN/A      case 8: // F_SETLK  (set lock)
5902093SN/A      case 9: // F_SETLKW (set lock and wait)
5912093SN/A        // don't mess with file locking... just act like it's OK
5922093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
5932093SN/A        return 0;
5942093SN/A
5952093SN/A      default:
5962093SN/A        warn("Unknown fcntl command %d\n", cmd);
5972093SN/A        return 0;
5982093SN/A    }
5992093SN/A}
6002093SN/A
6012238SN/ASyscallReturn
6023114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6032687Sksewell@umich.edu            ThreadContext *tc)
6042687Sksewell@umich.edu{
6056701Sgblack@eecs.umich.edu    int index = 0;
6066701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6072687Sksewell@umich.edu
6082687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6092687Sksewell@umich.edu        return -EBADF;
6102687Sksewell@umich.edu
6116701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6122687Sksewell@umich.edu    switch (cmd) {
6132687Sksewell@umich.edu      case 33: //F_GETLK64
6142687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6152687Sksewell@umich.edu        return -EMFILE;
6162687Sksewell@umich.edu
6172687Sksewell@umich.edu      case 34: // F_SETLK64
6182687Sksewell@umich.edu      case 35: // F_SETLKW64
6192687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6202687Sksewell@umich.edu        return -EMFILE;
6212687Sksewell@umich.edu
6222687Sksewell@umich.edu      default:
6232687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6242687Sksewell@umich.edu        // to the underlying OS
6252687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6262687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6272687Sksewell@umich.edu        // return 0;
6282687Sksewell@umich.edu    }
6292687Sksewell@umich.edu}
6302687Sksewell@umich.edu
6312687Sksewell@umich.eduSyscallReturn
6323114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6332680Sktlim@umich.edu         ThreadContext *tc)
6342238SN/A{
6352238SN/A    int fds[2], sim_fds[2];
6362238SN/A    int pipe_retval = pipe(fds);
6372093SN/A
6382238SN/A    if (pipe_retval < 0) {
6392238SN/A        // error
6402238SN/A        return pipe_retval;
6412238SN/A    }
6422238SN/A
6435282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6445282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6452238SN/A
6465282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6472238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6482238SN/A    // the return value of the function, and fd[1] is returned in r20.
6492680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6502238SN/A    return sim_fds[0];
6512238SN/A}
6522238SN/A
6532238SN/A
6542238SN/ASyscallReturn
6553114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6562680Sktlim@umich.edu           ThreadContext *tc)
6572238SN/A{
6582238SN/A    // Make up a PID.  There's no interprocess communication in
6592238SN/A    // fake_syscall mode, so there's no way for a process to know it's
6602238SN/A    // not getting a unique value.
6612238SN/A
6623114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
6633114Sgblack@eecs.umich.edu    return process->pid();
6642238SN/A}
6652238SN/A
6662238SN/A
6672238SN/ASyscallReturn
6683114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6692680Sktlim@umich.edu           ThreadContext *tc)
6702238SN/A{
6712238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
6722238SN/A    // simulation to be deterministic.
6732238SN/A
6742238SN/A    // EUID goes in r20.
6753114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
6765543Ssaidi@eecs.umich.edu    return process->uid();              // UID
6772238SN/A}
6782238SN/A
6792238SN/A
6802238SN/ASyscallReturn
6813114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6822680Sktlim@umich.edu           ThreadContext *tc)
6832238SN/A{
6842238SN/A    // Get current group ID.  EGID goes in r20.
6853114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
6863114Sgblack@eecs.umich.edu    return process->gid();
6872238SN/A}
6882238SN/A
6892238SN/A
6902238SN/ASyscallReturn
6913114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6922680Sktlim@umich.edu           ThreadContext *tc)
6932238SN/A{
6942238SN/A    // can't fathom why a benchmark would call this.
6956701Sgblack@eecs.umich.edu    int index = 0;
6966701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
6972238SN/A    return 0;
6982238SN/A}
6992238SN/A
7002238SN/ASyscallReturn
7013114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7022680Sktlim@umich.edu           ThreadContext *tc)
7032238SN/A{
7042238SN/A    // Make up a PID.  There's no interprocess communication in
7052238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7062238SN/A    // not getting a unique value.
7072238SN/A
7083114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7093114Sgblack@eecs.umich.edu    return process->pid();
7102238SN/A}
7112238SN/A
7122238SN/ASyscallReturn
7133114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7142680Sktlim@umich.edu           ThreadContext *tc)
7152238SN/A{
7163114Sgblack@eecs.umich.edu    return process->ppid();
7172238SN/A}
7182238SN/A
7192238SN/ASyscallReturn
7203114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7212680Sktlim@umich.edu           ThreadContext *tc)
7222238SN/A{
7235543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7242238SN/A}
7252238SN/A
7262238SN/ASyscallReturn
7273114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7282680Sktlim@umich.edu           ThreadContext *tc)
7292238SN/A{
7305543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7312238SN/A}
7322238SN/A
7332238SN/ASyscallReturn
7343114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7352680Sktlim@umich.edu           ThreadContext *tc)
7362238SN/A{
7373114Sgblack@eecs.umich.edu    return process->gid();
7382238SN/A}
7392238SN/A
7402238SN/ASyscallReturn
7413114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7422680Sktlim@umich.edu           ThreadContext *tc)
7432238SN/A{
7443114Sgblack@eecs.umich.edu    return process->egid();
7452238SN/A}
7462238SN/A
7472238SN/A
7486109Ssanchezd@stanford.eduSyscallReturn
7496109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7506109Ssanchezd@stanford.edu           ThreadContext *tc)
7516109Ssanchezd@stanford.edu{
7526701Sgblack@eecs.umich.edu    int index = 0;
7536701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7546701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
7556701Sgblack@eecs.umich.edu
7566109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
7576701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
7586701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
7596109Ssanchezd@stanford.edu
7606109Ssanchezd@stanford.edu
7616701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
7626111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
7636111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
7646111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
7656701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
7666109Ssanchezd@stanford.edu    }
7676109Ssanchezd@stanford.edu
7686111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
7696109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
7706109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
7716109Ssanchezd@stanford.edu
7726109Ssanchezd@stanford.edu        ctc->clearArchRegs();
7736109Ssanchezd@stanford.edu
7746111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
7756109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
7766111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
7776109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
7786109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
7796109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
7806109Ssanchezd@stanford.edu
7816111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
7826109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
7836109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
7846109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
7856109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
7866337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
7876109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
7886109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
7896109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
7906109Ssanchezd@stanford.edu
7916109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
7926109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
7936109Ssanchezd@stanford.edu        #else
7946109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
7956109Ssanchezd@stanford.edu        #endif
7966109Ssanchezd@stanford.edu
7976111Ssteve.reinhardt@amd.com        // Set up stack register
7986701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
7996109Ssanchezd@stanford.edu
8006111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8016111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8026109Ssanchezd@stanford.edu
8036111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8046109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8056110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8066109Ssanchezd@stanford.edu        #endif
8076109Ssanchezd@stanford.edu
8086111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8096111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8106109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8116109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8126109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8136109Ssanchezd@stanford.edu        #endif
8146109Ssanchezd@stanford.edu
8156109Ssanchezd@stanford.edu        ctc->setPC(tc->readNextPC());
8166109Ssanchezd@stanford.edu        ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
8176134Sgblack@eecs.umich.edu        ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst));
8186109Ssanchezd@stanford.edu
8196109Ssanchezd@stanford.edu        ctc->activate();
8206109Ssanchezd@stanford.edu
8216109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8226109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8236109Ssanchezd@stanford.edu        return 1;
8246109Ssanchezd@stanford.edu    } else {
8256109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8266109Ssanchezd@stanford.edu        return 0;
8276109Ssanchezd@stanford.edu    }
8286109Ssanchezd@stanford.edu}
8296109Ssanchezd@stanford.edu
830