syscall_emul.cc revision 8232
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{
586702Sgblack@eecs.umich.edu#if TRACING_ON
596701Sgblack@eecs.umich.edu    int index = 0;
606702Sgblack@eecs.umich.edu#endif
616111Ssteve.reinhardt@amd.com    DPRINTFR(SyscallVerbose,
626111Ssteve.reinhardt@amd.com             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
637823Ssteve.reinhardt@amd.com             curTick(), tc->getCpuPtr()->name(), name,
646701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
656701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
666701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index),
676701Sgblack@eecs.umich.edu             process->getSyscallArg(tc, index));
68360SN/A
692680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
70360SN/A
712495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
727823Ssteve.reinhardt@amd.com             curTick(),tc->getCpuPtr()->name(), name, retval.value());
73360SN/A
741450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
755958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
76360SN/A}
77360SN/A
78360SN/A
791450SN/ASyscallReturn
803114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
812680Sktlim@umich.edu                  ThreadContext *tc)
82360SN/A{
831969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
842484SN/A
852484SN/A    return 1;
86360SN/A}
87360SN/A
88360SN/A
891450SN/ASyscallReturn
903114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
912680Sktlim@umich.edu           ThreadContext *tc)
92360SN/A{
936701Sgblack@eecs.umich.edu    int index = 0;
941969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
956701Sgblack@eecs.umich.edu         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
96360SN/A
971458SN/A    return 0;
98360SN/A}
99360SN/A
100360SN/A
1011450SN/ASyscallReturn
1028149SChris.Emmons@ARM.comignoreWarnOnceFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1038149SChris.Emmons@ARM.com           ThreadContext *tc)
1048149SChris.Emmons@ARM.com{
1058149SChris.Emmons@ARM.com    int index = 0;
1068149SChris.Emmons@ARM.com    warn_once("ignoring syscall %s(%d, %d, ...)", desc->name,
1078149SChris.Emmons@ARM.com         process->getSyscallArg(tc, index), process->getSyscallArg(tc, index));
1088149SChris.Emmons@ARM.com
1098149SChris.Emmons@ARM.com    return 0;
1108149SChris.Emmons@ARM.com}
1118149SChris.Emmons@ARM.com
1128149SChris.Emmons@ARM.com
1138149SChris.Emmons@ARM.comSyscallReturn
1143114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1152680Sktlim@umich.edu         ThreadContext *tc)
116360SN/A{
1176029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
1186029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
1196701Sgblack@eecs.umich.edu        int index = 0;
1205958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
1216701Sgblack@eecs.umich.edu                    process->getSyscallArg(tc, index) & 0xff);
1226029Ssteve.reinhardt@amd.com    } else {
1236029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1246029Ssteve.reinhardt@amd.com        tc->halt();
1252834Sksewell@umich.edu    }
126360SN/A
1271458SN/A    return 1;
128360SN/A}
129360SN/A
130360SN/A
1311450SN/ASyscallReturn
1326109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1336109Ssanchezd@stanford.edu              ThreadContext *tc)
1346109Ssanchezd@stanford.edu{
1356109Ssanchezd@stanford.edu    // really should just halt all thread contexts belonging to this
1366109Ssanchezd@stanford.edu    // process in case there's another process running...
1376701Sgblack@eecs.umich.edu    int index = 0;
1386109Ssanchezd@stanford.edu    exitSimLoop("target called exit()",
1396701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index) & 0xff);
1406109Ssanchezd@stanford.edu
1416109Ssanchezd@stanford.edu    return 1;
1426109Ssanchezd@stanford.edu}
1436109Ssanchezd@stanford.edu
1446109Ssanchezd@stanford.edu
1456109Ssanchezd@stanford.eduSyscallReturn
1463114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
147360SN/A{
1482107SN/A    return (int)VMPageSize;
149360SN/A}
150360SN/A
151360SN/A
1521450SN/ASyscallReturn
1535748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
154360SN/A{
155360SN/A    // change brk addr to first arg
1566701Sgblack@eecs.umich.edu    int index = 0;
1576701Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, index);
1585748SSteve.Reinhardt@amd.com
1595748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1605748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1615748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1625748SSteve.Reinhardt@amd.com        return p->brk_point;
1635748SSteve.Reinhardt@amd.com
1645748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1655748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1662474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1672474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1685748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
1692474SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1702474SN/A                                    VMPageSize);
1716687Stjones1@inf.ed.ac.uk
1726687Stjones1@inf.ed.ac.uk            // if the address is already there, zero it out
1736687Stjones1@inf.ed.ac.uk            else {
1746687Stjones1@inf.ed.ac.uk                uint8_t zero  = 0;
1756687Stjones1@inf.ed.ac.uk                TranslatingPort *tp = tc->getMemPort();
1766687Stjones1@inf.ed.ac.uk
1776687Stjones1@inf.ed.ac.uk                // split non-page aligned accesses
1786687Stjones1@inf.ed.ac.uk                Addr next_page = roundUp(gen.addr(), VMPageSize);
1796687Stjones1@inf.ed.ac.uk                uint32_t size_needed = next_page - gen.addr();
1806687Stjones1@inf.ed.ac.uk                tp->memsetBlob(gen.addr(), zero, size_needed);
1816687Stjones1@inf.ed.ac.uk                if (gen.addr() + VMPageSize > next_page &&
1826687Stjones1@inf.ed.ac.uk                    next_page < new_brk &&
1836687Stjones1@inf.ed.ac.uk                    p->pTable->translate(next_page))
1846687Stjones1@inf.ed.ac.uk                {
1856687Stjones1@inf.ed.ac.uk                    size_needed = VMPageSize - size_needed;
1866687Stjones1@inf.ed.ac.uk                    tp->memsetBlob(next_page, zero, size_needed);
1876687Stjones1@inf.ed.ac.uk                }
1886687Stjones1@inf.ed.ac.uk            }
1892474SN/A        }
1901450SN/A    }
1915748SSteve.Reinhardt@amd.com
1925748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
1931458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1941458SN/A    return p->brk_point;
195360SN/A}
196360SN/A
197360SN/A
1981450SN/ASyscallReturn
1993114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
200360SN/A{
2016701Sgblack@eecs.umich.edu    int index = 0;
2026701Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, index);
2037508Stjones1@inf.ed.ac.uk    int sim_fd = p->sim_fd(target_fd);
2047508Stjones1@inf.ed.ac.uk    int status = 0;
2057508Stjones1@inf.ed.ac.uk    if (sim_fd > 2)
2067508Stjones1@inf.ed.ac.uk        status = close(sim_fd);
2071970SN/A    if (status >= 0)
2081970SN/A        p->free_fd(target_fd);
2091970SN/A    return status;
210360SN/A}
211360SN/A
212360SN/A
2131450SN/ASyscallReturn
2143114Sgblack@eecs.umich.edureadFunc(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
222360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
223360SN/A
224360SN/A    if (bytes_read != -1)
2252680Sktlim@umich.edu        bufArg.copyOut(tc->getMemPort());
226360SN/A
2271458SN/A    return bytes_read;
228360SN/A}
229360SN/A
2301450SN/ASyscallReturn
2313114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
232360SN/A{
2336701Sgblack@eecs.umich.edu    int index = 0;
2346701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2356701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
2366701Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, index);
2376701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
238360SN/A
2392680Sktlim@umich.edu    bufArg.copyIn(tc->getMemPort());
240360SN/A
241360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
242360SN/A
243360SN/A    fsync(fd);
244360SN/A
2451458SN/A    return bytes_written;
246360SN/A}
247360SN/A
248360SN/A
2491450SN/ASyscallReturn
2503114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
251360SN/A{
2526701Sgblack@eecs.umich.edu    int index = 0;
2536701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2546701Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, index);
2556701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
256360SN/A
257360SN/A    off_t result = lseek(fd, offs, whence);
258360SN/A
2591458SN/A    return (result == (off_t)-1) ? -errno : result;
260360SN/A}
261360SN/A
262360SN/A
2631450SN/ASyscallReturn
2644118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2654118Sgblack@eecs.umich.edu{
2666701Sgblack@eecs.umich.edu    int index = 0;
2676701Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, index));
2686701Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, index);
2696701Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, index);
2706701Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, index);
2716701Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, index);
2724118Sgblack@eecs.umich.edu
2734118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2744118Sgblack@eecs.umich.edu
2754118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2764118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2774118Sgblack@eecs.umich.edu
2784118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2794118Sgblack@eecs.umich.edu        //The seek failed.
2804118Sgblack@eecs.umich.edu        return -errno;
2814118Sgblack@eecs.umich.edu    } else {
2826111Ssteve.reinhardt@amd.com        // The seek succeeded.
2836111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2846111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2856111Ssteve.reinhardt@amd.com        // target platform
2864118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2874118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2884118Sgblack@eecs.umich.edu        result_buf.copyOut(tc->getMemPort());
2894118Sgblack@eecs.umich.edu        return 0;
2904118Sgblack@eecs.umich.edu    }
2914118Sgblack@eecs.umich.edu
2924118Sgblack@eecs.umich.edu
2934118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2944118Sgblack@eecs.umich.edu}
2954118Sgblack@eecs.umich.edu
2964118Sgblack@eecs.umich.edu
2974118Sgblack@eecs.umich.eduSyscallReturn
2983114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
299360SN/A{
300360SN/A    // given that we don't really implement mmap, munmap is really easy
3011458SN/A    return 0;
302360SN/A}
303360SN/A
304360SN/A
305360SN/Aconst char *hostname = "m5.eecs.umich.edu";
306360SN/A
3071450SN/ASyscallReturn
3083114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
309360SN/A{
3106701Sgblack@eecs.umich.edu    int index = 0;
3116701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3126701Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, index);
3136701Sgblack@eecs.umich.edu    BufferArg name(bufPtr, name_len);
314360SN/A
315360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
316360SN/A
3172680Sktlim@umich.edu    name.copyOut(tc->getMemPort());
318360SN/A
3191458SN/A    return 0;
320360SN/A}
321360SN/A
3221450SN/ASyscallReturn
3235513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3245513SMichael.Adler@intel.com{
3255513SMichael.Adler@intel.com    int result = 0;
3266731Svince@csl.cornell.edu    int index = 0;
3276701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3286701Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, index);
3296701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, size);
3305513SMichael.Adler@intel.com
3315513SMichael.Adler@intel.com    // Is current working directory defined?
3325513SMichael.Adler@intel.com    string cwd = p->getcwd();
3335513SMichael.Adler@intel.com    if (!cwd.empty()) {
3345513SMichael.Adler@intel.com        if (cwd.length() >= size) {
3355513SMichael.Adler@intel.com            // Buffer too small
3365513SMichael.Adler@intel.com            return -ERANGE;
3375513SMichael.Adler@intel.com        }
3385513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
3395513SMichael.Adler@intel.com        result = cwd.length();
3405513SMichael.Adler@intel.com    }
3415513SMichael.Adler@intel.com    else {
3425513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
3435513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
3445513SMichael.Adler@intel.com        }
3455513SMichael.Adler@intel.com        else {
3465513SMichael.Adler@intel.com            result = -1;
3475513SMichael.Adler@intel.com        }
3485513SMichael.Adler@intel.com    }
3495513SMichael.Adler@intel.com
3505513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3515513SMichael.Adler@intel.com
3525513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3535513SMichael.Adler@intel.com}
3545513SMichael.Adler@intel.com
3555513SMichael.Adler@intel.com
3565513SMichael.Adler@intel.comSyscallReturn
3575513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3585513SMichael.Adler@intel.com{
3595513SMichael.Adler@intel.com    string path;
3605513SMichael.Adler@intel.com
3616701Sgblack@eecs.umich.edu    int index = 0;
3626701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3635513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3645513SMichael.Adler@intel.com
3655513SMichael.Adler@intel.com    // Adjust path for current working directory
3665513SMichael.Adler@intel.com    path = p->fullPath(path);
3675513SMichael.Adler@intel.com
3686701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
3696701Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, index);
3706701Sgblack@eecs.umich.edu
3716701Sgblack@eecs.umich.edu    BufferArg buf(bufPtr, bufsiz);
3725513SMichael.Adler@intel.com
3735513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3745513SMichael.Adler@intel.com
3755513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3765513SMichael.Adler@intel.com
3775513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3785513SMichael.Adler@intel.com}
3795513SMichael.Adler@intel.com
3805513SMichael.Adler@intel.comSyscallReturn
3813114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
382511SN/A{
3831706SN/A    string path;
384360SN/A
3856701Sgblack@eecs.umich.edu    int index = 0;
3866701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
3871450SN/A        return (TheISA::IntReg)-EFAULT;
388511SN/A
3893669Sbinkertn@umich.edu    // Adjust path for current working directory
3903669Sbinkertn@umich.edu    path = p->fullPath(path);
3913669Sbinkertn@umich.edu
392511SN/A    int result = unlink(path.c_str());
3931458SN/A    return (result == -1) ? -errno : result;
394511SN/A}
395511SN/A
3965513SMichael.Adler@intel.com
3975513SMichael.Adler@intel.comSyscallReturn
3985513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3995513SMichael.Adler@intel.com{
4005513SMichael.Adler@intel.com    string path;
4015513SMichael.Adler@intel.com
4026701Sgblack@eecs.umich.edu    int index = 0;
4036701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
4045513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
4055513SMichael.Adler@intel.com
4065513SMichael.Adler@intel.com    // Adjust path for current working directory
4075513SMichael.Adler@intel.com    path = p->fullPath(path);
4085513SMichael.Adler@intel.com
4096701Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, index);
4105513SMichael.Adler@intel.com
4115513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
4125513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
4135513SMichael.Adler@intel.com}
4145513SMichael.Adler@intel.com
4151450SN/ASyscallReturn
4163114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
417511SN/A{
4181706SN/A    string old_name;
419511SN/A
4206701Sgblack@eecs.umich.edu    int index = 0;
4216701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, index)))
4221458SN/A        return -EFAULT;
423511SN/A
4241706SN/A    string new_name;
425511SN/A
4266701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, index)))
4271458SN/A        return -EFAULT;
428511SN/A
4293669Sbinkertn@umich.edu    // Adjust path for current working directory
4303669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
4313669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
4323669Sbinkertn@umich.edu
4331706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
4341458SN/A    return (result == -1) ? -errno : result;
435511SN/A}
436511SN/A
4371706SN/ASyscallReturn
4383114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4391706SN/A{
4401706SN/A    string path;
4411706SN/A
4426701Sgblack@eecs.umich.edu    int index = 0;
4436701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
4441706SN/A        return -EFAULT;
4451706SN/A
4466701Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, index);
4471706SN/A
4483669Sbinkertn@umich.edu    // Adjust path for current working directory
4493669Sbinkertn@umich.edu    path = p->fullPath(path);
4503669Sbinkertn@umich.edu
4511706SN/A    int result = truncate(path.c_str(), length);
4521706SN/A    return (result == -1) ? -errno : result;
4531706SN/A}
4541706SN/A
4551706SN/ASyscallReturn
4566111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
4576111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
4581706SN/A{
4596701Sgblack@eecs.umich.edu    int index = 0;
4606701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
4611706SN/A
4621706SN/A    if (fd < 0)
4631706SN/A        return -EBADF;
4641706SN/A
4656701Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, index);
4661706SN/A
4671706SN/A    int result = ftruncate(fd, length);
4681706SN/A    return (result == -1) ? -errno : result;
4691706SN/A}
4701999SN/A
4711999SN/ASyscallReturn
4726703Svince@csl.cornell.edutruncate64Func(SyscallDesc *desc, int num,
4736703Svince@csl.cornell.edu                LiveProcess *process, ThreadContext *tc)
4746703Svince@csl.cornell.edu{
4756703Svince@csl.cornell.edu    int index = 0;
4766703Svince@csl.cornell.edu    string path;
4776703Svince@csl.cornell.edu
4786703Svince@csl.cornell.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, index)))
4796703Svince@csl.cornell.edu       return -EFAULT;
4806703Svince@csl.cornell.edu
4816744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
4826703Svince@csl.cornell.edu
4836703Svince@csl.cornell.edu    // Adjust path for current working directory
4846703Svince@csl.cornell.edu    path = process->fullPath(path);
4856703Svince@csl.cornell.edu
4866744SAli.Saidi@arm.com#if NO_STAT64
4876744SAli.Saidi@arm.com    int result = truncate(path.c_str(), length);
4886744SAli.Saidi@arm.com#else
4896703Svince@csl.cornell.edu    int result = truncate64(path.c_str(), length);
4906744SAli.Saidi@arm.com#endif
4916703Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
4926703Svince@csl.cornell.edu}
4936703Svince@csl.cornell.edu
4946703Svince@csl.cornell.eduSyscallReturn
4956685Stjones1@inf.ed.ac.ukftruncate64Func(SyscallDesc *desc, int num,
4966685Stjones1@inf.ed.ac.uk                LiveProcess *process, ThreadContext *tc)
4976685Stjones1@inf.ed.ac.uk{
4986701Sgblack@eecs.umich.edu    int index = 0;
4996701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5006685Stjones1@inf.ed.ac.uk
5016685Stjones1@inf.ed.ac.uk    if (fd < 0)
5026685Stjones1@inf.ed.ac.uk        return -EBADF;
5036685Stjones1@inf.ed.ac.uk
5046744SAli.Saidi@arm.com    int64_t length = process->getSyscallArg(tc, index, 64);
5056685Stjones1@inf.ed.ac.uk
5066744SAli.Saidi@arm.com#if NO_STAT64
5076744SAli.Saidi@arm.com    int result = ftruncate(fd, length);
5086744SAli.Saidi@arm.com#else
5096685Stjones1@inf.ed.ac.uk    int result = ftruncate64(fd, length);
5106744SAli.Saidi@arm.com#endif
5116685Stjones1@inf.ed.ac.uk    return (result == -1) ? -errno : result;
5126685Stjones1@inf.ed.ac.uk}
5136685Stjones1@inf.ed.ac.uk
5146685Stjones1@inf.ed.ac.ukSyscallReturn
5155513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5165513SMichael.Adler@intel.com{
5175513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
5185513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
5195513SMichael.Adler@intel.com    // changing anything.
5205513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
5215513SMichael.Adler@intel.com    umask(oldMask);
5225521Snate@binkert.org    return (int)oldMask;
5235513SMichael.Adler@intel.com}
5245513SMichael.Adler@intel.com
5255513SMichael.Adler@intel.comSyscallReturn
5263114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
5271999SN/A{
5281999SN/A    string path;
5291999SN/A
5306701Sgblack@eecs.umich.edu    int index = 0;
5316701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, index)))
5321999SN/A        return -EFAULT;
5331999SN/A
5341999SN/A    /* XXX endianess */
5356701Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, index);
5361999SN/A    uid_t hostOwner = owner;
5376701Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, index);
5381999SN/A    gid_t hostGroup = group;
5391999SN/A
5403669Sbinkertn@umich.edu    // Adjust path for current working directory
5413669Sbinkertn@umich.edu    path = p->fullPath(path);
5423669Sbinkertn@umich.edu
5431999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
5441999SN/A    return (result == -1) ? -errno : result;
5451999SN/A}
5461999SN/A
5471999SN/ASyscallReturn
5483114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5491999SN/A{
5506701Sgblack@eecs.umich.edu    int index = 0;
5516701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5521999SN/A
5531999SN/A    if (fd < 0)
5541999SN/A        return -EBADF;
5551999SN/A
5561999SN/A    /* XXX endianess */
5576701Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, index);
5581999SN/A    uid_t hostOwner = owner;
5596701Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, index);
5601999SN/A    gid_t hostGroup = group;
5611999SN/A
5621999SN/A    int result = fchown(fd, hostOwner, hostGroup);
5631999SN/A    return (result == -1) ? -errno : result;
5641999SN/A}
5652093SN/A
5662093SN/A
5672093SN/ASyscallReturn
5683114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
5693079Sstever@eecs.umich.edu{
5706701Sgblack@eecs.umich.edu    int index = 0;
5716701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
5723079Sstever@eecs.umich.edu    if (fd < 0)
5733079Sstever@eecs.umich.edu        return -EBADF;
5743079Sstever@eecs.umich.edu
5756701Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(fd);
5765282Srstrong@cs.ucsd.edu
5773079Sstever@eecs.umich.edu    int result = dup(fd);
5786111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
5796111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
5803079Sstever@eecs.umich.edu}
5813079Sstever@eecs.umich.edu
5823079Sstever@eecs.umich.edu
5833079Sstever@eecs.umich.eduSyscallReturn
5843114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
5852680Sktlim@umich.edu          ThreadContext *tc)
5862093SN/A{
5876701Sgblack@eecs.umich.edu    int index = 0;
5886701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5892093SN/A
5902093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
5912093SN/A        return -EBADF;
5922093SN/A
5936701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
5942093SN/A    switch (cmd) {
5952093SN/A      case 0: // F_DUPFD
5962093SN/A        // if we really wanted to support this, we'd need to do it
5972093SN/A        // in the target fd space.
5982093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
5992093SN/A        return -EMFILE;
6002093SN/A
6012093SN/A      case 1: // F_GETFD (get close-on-exec flag)
6022093SN/A      case 2: // F_SETFD (set close-on-exec flag)
6032093SN/A        return 0;
6042093SN/A
6052093SN/A      case 3: // F_GETFL (get file flags)
6062093SN/A      case 4: // F_SETFL (set file flags)
6072093SN/A        // not sure if this is totally valid, but we'll pass it through
6082093SN/A        // to the underlying OS
6092093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
6102093SN/A        return fcntl(process->sim_fd(fd), cmd);
6112093SN/A        // return 0;
6122093SN/A
6132093SN/A      case 7: // F_GETLK  (get lock)
6142093SN/A      case 8: // F_SETLK  (set lock)
6152093SN/A      case 9: // F_SETLKW (set lock and wait)
6162093SN/A        // don't mess with file locking... just act like it's OK
6172093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
6182093SN/A        return 0;
6192093SN/A
6202093SN/A      default:
6212093SN/A        warn("Unknown fcntl command %d\n", cmd);
6222093SN/A        return 0;
6232093SN/A    }
6242093SN/A}
6252093SN/A
6262238SN/ASyscallReturn
6273114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
6282687Sksewell@umich.edu            ThreadContext *tc)
6292687Sksewell@umich.edu{
6306701Sgblack@eecs.umich.edu    int index = 0;
6316701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6322687Sksewell@umich.edu
6332687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
6342687Sksewell@umich.edu        return -EBADF;
6352687Sksewell@umich.edu
6366701Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, index);
6372687Sksewell@umich.edu    switch (cmd) {
6382687Sksewell@umich.edu      case 33: //F_GETLK64
6392687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
6402687Sksewell@umich.edu        return -EMFILE;
6412687Sksewell@umich.edu
6422687Sksewell@umich.edu      case 34: // F_SETLK64
6432687Sksewell@umich.edu      case 35: // F_SETLKW64
6442687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
6452687Sksewell@umich.edu        return -EMFILE;
6462687Sksewell@umich.edu
6472687Sksewell@umich.edu      default:
6482687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
6492687Sksewell@umich.edu        // to the underlying OS
6502687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
6512687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
6522687Sksewell@umich.edu        // return 0;
6532687Sksewell@umich.edu    }
6542687Sksewell@umich.edu}
6552687Sksewell@umich.edu
6562687Sksewell@umich.eduSyscallReturn
6573114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6582680Sktlim@umich.edu         ThreadContext *tc)
6592238SN/A{
6602238SN/A    int fds[2], sim_fds[2];
6612238SN/A    int pipe_retval = pipe(fds);
6622093SN/A
6632238SN/A    if (pipe_retval < 0) {
6642238SN/A        // error
6652238SN/A        return pipe_retval;
6662238SN/A    }
6672238SN/A
6685282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
6695282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
6702238SN/A
6715282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
6722238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
6732238SN/A    // the return value of the function, and fd[1] is returned in r20.
6742680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
6752238SN/A    return sim_fds[0];
6762238SN/A}
6772238SN/A
6782238SN/A
6792238SN/ASyscallReturn
6803114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6812680Sktlim@umich.edu           ThreadContext *tc)
6822238SN/A{
6832238SN/A    // Make up a PID.  There's no interprocess communication in
6842238SN/A    // fake_syscall mode, so there's no way for a process to know it's
6852238SN/A    // not getting a unique value.
6862238SN/A
6873114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
6883114Sgblack@eecs.umich.edu    return process->pid();
6892238SN/A}
6902238SN/A
6912238SN/A
6922238SN/ASyscallReturn
6933114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6942680Sktlim@umich.edu           ThreadContext *tc)
6952238SN/A{
6962238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
6972238SN/A    // simulation to be deterministic.
6982238SN/A
6992238SN/A    // EUID goes in r20.
7003114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
7015543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7022238SN/A}
7032238SN/A
7042238SN/A
7052238SN/ASyscallReturn
7063114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7072680Sktlim@umich.edu           ThreadContext *tc)
7082238SN/A{
7092238SN/A    // Get current group ID.  EGID goes in r20.
7103114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
7113114Sgblack@eecs.umich.edu    return process->gid();
7122238SN/A}
7132238SN/A
7142238SN/A
7152238SN/ASyscallReturn
7163114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7172680Sktlim@umich.edu           ThreadContext *tc)
7182238SN/A{
7192238SN/A    // can't fathom why a benchmark would call this.
7206701Sgblack@eecs.umich.edu    int index = 0;
7216701Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
7222238SN/A    return 0;
7232238SN/A}
7242238SN/A
7252238SN/ASyscallReturn
7263114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7272680Sktlim@umich.edu           ThreadContext *tc)
7282238SN/A{
7292238SN/A    // Make up a PID.  There's no interprocess communication in
7302238SN/A    // fake_syscall mode, so there's no way for a process to know it's
7312238SN/A    // not getting a unique value.
7322238SN/A
7333114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
7343114Sgblack@eecs.umich.edu    return process->pid();
7352238SN/A}
7362238SN/A
7372238SN/ASyscallReturn
7383114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7392680Sktlim@umich.edu           ThreadContext *tc)
7402238SN/A{
7413114Sgblack@eecs.umich.edu    return process->ppid();
7422238SN/A}
7432238SN/A
7442238SN/ASyscallReturn
7453114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7462680Sktlim@umich.edu           ThreadContext *tc)
7472238SN/A{
7485543Ssaidi@eecs.umich.edu    return process->uid();              // UID
7492238SN/A}
7502238SN/A
7512238SN/ASyscallReturn
7523114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7532680Sktlim@umich.edu           ThreadContext *tc)
7542238SN/A{
7555543Ssaidi@eecs.umich.edu    return process->euid();             // UID
7562238SN/A}
7572238SN/A
7582238SN/ASyscallReturn
7593114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7602680Sktlim@umich.edu           ThreadContext *tc)
7612238SN/A{
7623114Sgblack@eecs.umich.edu    return process->gid();
7632238SN/A}
7642238SN/A
7652238SN/ASyscallReturn
7663114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7672680Sktlim@umich.edu           ThreadContext *tc)
7682238SN/A{
7693114Sgblack@eecs.umich.edu    return process->egid();
7702238SN/A}
7712238SN/A
7722238SN/A
7736109Ssanchezd@stanford.eduSyscallReturn
7746109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7756109Ssanchezd@stanford.edu           ThreadContext *tc)
7766109Ssanchezd@stanford.edu{
7776701Sgblack@eecs.umich.edu    int index = 0;
7786701Sgblack@eecs.umich.edu    IntReg flags = process->getSyscallArg(tc, index);
7796701Sgblack@eecs.umich.edu    IntReg newStack = process->getSyscallArg(tc, index);
7806701Sgblack@eecs.umich.edu
7816109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
7826701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Flags=%llx\n", flags);
7836701Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, " Child stack=%llx\n", newStack);
7846109Ssanchezd@stanford.edu
7856109Ssanchezd@stanford.edu
7866701Sgblack@eecs.umich.edu    if (flags != 0x10f00) {
7876111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
7886111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
7896111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
7906701Sgblack@eecs.umich.edu             "0x%llx\n", flags);
7916109Ssanchezd@stanford.edu    }
7926109Ssanchezd@stanford.edu
7936111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
7946109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
7956109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
7966109Ssanchezd@stanford.edu
7976109Ssanchezd@stanford.edu        ctc->clearArchRegs();
7986109Ssanchezd@stanford.edu
7996111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
8006109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
8016111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
8026109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
8036109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
8046109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
8056109Ssanchezd@stanford.edu
8066111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
8076109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
8086109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
8096109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
8106109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
8116337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
8126109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
8136109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
8146109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
8156109Ssanchezd@stanford.edu
8166109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
8176109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
8188149SChris.Emmons@ARM.com        #elif THE_ISA == ARM_ISA
8198149SChris.Emmons@ARM.com            TheISA::copyRegs(tc, ctc);
8206109Ssanchezd@stanford.edu        #else
8216109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
8226109Ssanchezd@stanford.edu        #endif
8236109Ssanchezd@stanford.edu
8246111Ssteve.reinhardt@amd.com        // Set up stack register
8256701Sgblack@eecs.umich.edu        ctc->setIntReg(TheISA::StackPointerReg, newStack);
8266109Ssanchezd@stanford.edu
8276111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
8286111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
8296109Ssanchezd@stanford.edu
8306111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
8316109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
8326110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
8336109Ssanchezd@stanford.edu        #endif
8346109Ssanchezd@stanford.edu
8356111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
8366111Ssteve.reinhardt@amd.com        // parent, non-zero if child
8376109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
8386109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
8396109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
8406109Ssanchezd@stanford.edu        #endif
8416109Ssanchezd@stanford.edu
8427720Sgblack@eecs.umich.edu        ctc->pcState(tc->nextInstAddr());
8436109Ssanchezd@stanford.edu
8446109Ssanchezd@stanford.edu        ctc->activate();
8456109Ssanchezd@stanford.edu
8466109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
8476109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
8486109Ssanchezd@stanford.edu        return 1;
8496109Ssanchezd@stanford.edu    } else {
8506109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
8516109Ssanchezd@stanford.edu        return 0;
8526109Ssanchezd@stanford.edu    }
8536109Ssanchezd@stanford.edu}
8546109Ssanchezd@stanford.edu
855