syscall_emul.cc revision 6658
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
35360SN/A#include <string>
36360SN/A#include <iostream>
37360SN/A
38360SN/A#include "sim/syscall_emul.hh"
392474SN/A#include "base/chunk_generator.hh"
40360SN/A#include "base/trace.hh"
416658Snate@binkert.org#include "config/the_isa.hh"
422680Sktlim@umich.edu#include "cpu/thread_context.hh"
431717SN/A#include "cpu/base.hh"
442474SN/A#include "mem/page_table.hh"
45360SN/A#include "sim/process.hh"
466029Ssteve.reinhardt@amd.com#include "sim/system.hh"
472667Sstever@eecs.umich.edu#include "sim/sim_exit.hh"
48360SN/A
49360SN/Ausing namespace std;
502107SN/Ausing namespace TheISA;
51360SN/A
52360SN/Avoid
533114Sgblack@eecs.umich.eduSyscallDesc::doSyscall(int callnum, LiveProcess *process, ThreadContext *tc)
54360SN/A{
556111Ssteve.reinhardt@amd.com    DPRINTFR(SyscallVerbose,
566111Ssteve.reinhardt@amd.com             "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n",
576111Ssteve.reinhardt@amd.com             curTick, tc->getCpuPtr()->name(), name,
585958Sgblack@eecs.umich.edu             process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1),
595958Sgblack@eecs.umich.edu             process->getSyscallArg(tc, 2), process->getSyscallArg(tc, 3));
60360SN/A
612680Sktlim@umich.edu    SyscallReturn retval = (*funcPtr)(this, callnum, process, tc);
62360SN/A
632495SN/A    DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n",
642680Sktlim@umich.edu             curTick,tc->getCpuPtr()->name(), name, retval.value());
65360SN/A
661450SN/A    if (!(flags & SyscallDesc::SuppressReturnValue))
675958Sgblack@eecs.umich.edu        process->setSyscallReturn(tc, retval);
68360SN/A}
69360SN/A
70360SN/A
711450SN/ASyscallReturn
723114Sgblack@eecs.umich.eduunimplementedFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
732680Sktlim@umich.edu                  ThreadContext *tc)
74360SN/A{
751969SN/A    fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
762484SN/A
772484SN/A    return 1;
78360SN/A}
79360SN/A
80360SN/A
811450SN/ASyscallReturn
823114Sgblack@eecs.umich.eduignoreFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
832680Sktlim@umich.edu           ThreadContext *tc)
84360SN/A{
851969SN/A    warn("ignoring syscall %s(%d, %d, ...)", desc->name,
865958Sgblack@eecs.umich.edu         process->getSyscallArg(tc, 0), process->getSyscallArg(tc, 1));
87360SN/A
881458SN/A    return 0;
89360SN/A}
90360SN/A
91360SN/A
921450SN/ASyscallReturn
933114Sgblack@eecs.umich.eduexitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
942680Sktlim@umich.edu         ThreadContext *tc)
95360SN/A{
966029Ssteve.reinhardt@amd.com    if (process->system->numRunningContexts() == 1) {
976029Ssteve.reinhardt@amd.com        // Last running context... exit simulator
985958Sgblack@eecs.umich.edu        exitSimLoop("target called exit()",
996029Ssteve.reinhardt@amd.com                    process->getSyscallArg(tc, 0) & 0xff);
1006029Ssteve.reinhardt@amd.com    } else {
1016029Ssteve.reinhardt@amd.com        // other running threads... just halt this one
1026029Ssteve.reinhardt@amd.com        tc->halt();
1032834Sksewell@umich.edu    }
104360SN/A
1051458SN/A    return 1;
106360SN/A}
107360SN/A
108360SN/A
1091450SN/ASyscallReturn
1106109Ssanchezd@stanford.eduexitGroupFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1116109Ssanchezd@stanford.edu              ThreadContext *tc)
1126109Ssanchezd@stanford.edu{
1136109Ssanchezd@stanford.edu    // really should just halt all thread contexts belonging to this
1146109Ssanchezd@stanford.edu    // process in case there's another process running...
1156109Ssanchezd@stanford.edu    exitSimLoop("target called exit()",
1166109Ssanchezd@stanford.edu                process->getSyscallArg(tc, 0) & 0xff);
1176109Ssanchezd@stanford.edu
1186109Ssanchezd@stanford.edu    return 1;
1196109Ssanchezd@stanford.edu}
1206109Ssanchezd@stanford.edu
1216109Ssanchezd@stanford.edu
1226109Ssanchezd@stanford.eduSyscallReturn
1233114Sgblack@eecs.umich.edugetpagesizeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
124360SN/A{
1252107SN/A    return (int)VMPageSize;
126360SN/A}
127360SN/A
128360SN/A
1291450SN/ASyscallReturn
1305748SSteve.Reinhardt@amd.combrkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
131360SN/A{
132360SN/A    // change brk addr to first arg
1335958Sgblack@eecs.umich.edu    Addr new_brk = p->getSyscallArg(tc, 0);
1345748SSteve.Reinhardt@amd.com
1355748SSteve.Reinhardt@amd.com    // in Linux at least, brk(0) returns the current break value
1365748SSteve.Reinhardt@amd.com    // (note that the syscall and the glibc function have different behavior)
1375748SSteve.Reinhardt@amd.com    if (new_brk == 0)
1385748SSteve.Reinhardt@amd.com        return p->brk_point;
1395748SSteve.Reinhardt@amd.com
1405748SSteve.Reinhardt@amd.com    if (new_brk > p->brk_point) {
1415748SSteve.Reinhardt@amd.com        // might need to allocate some new pages
1422474SN/A        for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point,
1432474SN/A                                VMPageSize); !gen.done(); gen.next()) {
1445748SSteve.Reinhardt@amd.com            if (!p->pTable->translate(gen.addr()))
1452474SN/A                p->pTable->allocate(roundDown(gen.addr(), VMPageSize),
1462474SN/A                                    VMPageSize);
1472474SN/A        }
1481450SN/A    }
1495748SSteve.Reinhardt@amd.com
1505748SSteve.Reinhardt@amd.com    p->brk_point = new_brk;
1511458SN/A    DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
1521458SN/A    return p->brk_point;
153360SN/A}
154360SN/A
155360SN/A
1561450SN/ASyscallReturn
1573114Sgblack@eecs.umich.educloseFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
158360SN/A{
1595958Sgblack@eecs.umich.edu    int target_fd = p->getSyscallArg(tc, 0);
1601970SN/A    int status = close(p->sim_fd(target_fd));
1611970SN/A    if (status >= 0)
1621970SN/A        p->free_fd(target_fd);
1631970SN/A    return status;
164360SN/A}
165360SN/A
166360SN/A
1671450SN/ASyscallReturn
1683114Sgblack@eecs.umich.edureadFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
169360SN/A{
1705958Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1715958Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, 2);
1725958Sgblack@eecs.umich.edu    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
173360SN/A
174360SN/A    int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
175360SN/A
176360SN/A    if (bytes_read != -1)
1772680Sktlim@umich.edu        bufArg.copyOut(tc->getMemPort());
178360SN/A
1791458SN/A    return bytes_read;
180360SN/A}
181360SN/A
1821450SN/ASyscallReturn
1833114Sgblack@eecs.umich.eduwriteFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
184360SN/A{
1855958Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
1865958Sgblack@eecs.umich.edu    int nbytes = p->getSyscallArg(tc, 2);
1875958Sgblack@eecs.umich.edu    BufferArg bufArg(p->getSyscallArg(tc, 1), nbytes);
188360SN/A
1892680Sktlim@umich.edu    bufArg.copyIn(tc->getMemPort());
190360SN/A
191360SN/A    int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
192360SN/A
193360SN/A    fsync(fd);
194360SN/A
1951458SN/A    return bytes_written;
196360SN/A}
197360SN/A
198360SN/A
1991450SN/ASyscallReturn
2003114Sgblack@eecs.umich.edulseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
201360SN/A{
2025958Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
2035958Sgblack@eecs.umich.edu    uint64_t offs = p->getSyscallArg(tc, 1);
2045958Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, 2);
205360SN/A
206360SN/A    off_t result = lseek(fd, offs, whence);
207360SN/A
2081458SN/A    return (result == (off_t)-1) ? -errno : result;
209360SN/A}
210360SN/A
211360SN/A
2121450SN/ASyscallReturn
2134118Sgblack@eecs.umich.edu_llseekFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2144118Sgblack@eecs.umich.edu{
2155958Sgblack@eecs.umich.edu    int fd = p->sim_fd(p->getSyscallArg(tc, 0));
2165958Sgblack@eecs.umich.edu    uint64_t offset_high = p->getSyscallArg(tc, 1);
2175958Sgblack@eecs.umich.edu    uint32_t offset_low = p->getSyscallArg(tc, 2);
2185958Sgblack@eecs.umich.edu    Addr result_ptr = p->getSyscallArg(tc, 3);
2195958Sgblack@eecs.umich.edu    int whence = p->getSyscallArg(tc, 4);
2204118Sgblack@eecs.umich.edu
2214118Sgblack@eecs.umich.edu    uint64_t offset = (offset_high << 32) | offset_low;
2224118Sgblack@eecs.umich.edu
2234118Sgblack@eecs.umich.edu    uint64_t result = lseek(fd, offset, whence);
2244118Sgblack@eecs.umich.edu    result = TheISA::htog(result);
2254118Sgblack@eecs.umich.edu
2264118Sgblack@eecs.umich.edu    if (result == (off_t)-1) {
2274118Sgblack@eecs.umich.edu        //The seek failed.
2284118Sgblack@eecs.umich.edu        return -errno;
2294118Sgblack@eecs.umich.edu    } else {
2306111Ssteve.reinhardt@amd.com        // The seek succeeded.
2316111Ssteve.reinhardt@amd.com        // Copy "result" to "result_ptr"
2326111Ssteve.reinhardt@amd.com        // XXX We'll assume that the size of loff_t is 64 bits on the
2336111Ssteve.reinhardt@amd.com        // target platform
2344118Sgblack@eecs.umich.edu        BufferArg result_buf(result_ptr, sizeof(result));
2354118Sgblack@eecs.umich.edu        memcpy(result_buf.bufferPtr(), &result, sizeof(result));
2364118Sgblack@eecs.umich.edu        result_buf.copyOut(tc->getMemPort());
2374118Sgblack@eecs.umich.edu        return 0;
2384118Sgblack@eecs.umich.edu    }
2394118Sgblack@eecs.umich.edu
2404118Sgblack@eecs.umich.edu
2414118Sgblack@eecs.umich.edu    return (result == (off_t)-1) ? -errno : result;
2424118Sgblack@eecs.umich.edu}
2434118Sgblack@eecs.umich.edu
2444118Sgblack@eecs.umich.edu
2454118Sgblack@eecs.umich.eduSyscallReturn
2463114Sgblack@eecs.umich.edumunmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
247360SN/A{
248360SN/A    // given that we don't really implement mmap, munmap is really easy
2491458SN/A    return 0;
250360SN/A}
251360SN/A
252360SN/A
253360SN/Aconst char *hostname = "m5.eecs.umich.edu";
254360SN/A
2551450SN/ASyscallReturn
2563114Sgblack@eecs.umich.edugethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
257360SN/A{
2585958Sgblack@eecs.umich.edu    int name_len = p->getSyscallArg(tc, 1);
2595958Sgblack@eecs.umich.edu    BufferArg name(p->getSyscallArg(tc, 0), name_len);
260360SN/A
261360SN/A    strncpy((char *)name.bufferPtr(), hostname, name_len);
262360SN/A
2632680Sktlim@umich.edu    name.copyOut(tc->getMemPort());
264360SN/A
2651458SN/A    return 0;
266360SN/A}
267360SN/A
2681450SN/ASyscallReturn
2695513SMichael.Adler@intel.comgetcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
2705513SMichael.Adler@intel.com{
2715513SMichael.Adler@intel.com    int result = 0;
2725958Sgblack@eecs.umich.edu    unsigned long size = p->getSyscallArg(tc, 1);
2735958Sgblack@eecs.umich.edu    BufferArg buf(p->getSyscallArg(tc, 0), size);
2745513SMichael.Adler@intel.com
2755513SMichael.Adler@intel.com    // Is current working directory defined?
2765513SMichael.Adler@intel.com    string cwd = p->getcwd();
2775513SMichael.Adler@intel.com    if (!cwd.empty()) {
2785513SMichael.Adler@intel.com        if (cwd.length() >= size) {
2795513SMichael.Adler@intel.com            // Buffer too small
2805513SMichael.Adler@intel.com            return -ERANGE;
2815513SMichael.Adler@intel.com        }
2825513SMichael.Adler@intel.com        strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
2835513SMichael.Adler@intel.com        result = cwd.length();
2845513SMichael.Adler@intel.com    }
2855513SMichael.Adler@intel.com    else {
2865513SMichael.Adler@intel.com        if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
2875513SMichael.Adler@intel.com            result = strlen((char *)buf.bufferPtr());
2885513SMichael.Adler@intel.com        }
2895513SMichael.Adler@intel.com        else {
2905513SMichael.Adler@intel.com            result = -1;
2915513SMichael.Adler@intel.com        }
2925513SMichael.Adler@intel.com    }
2935513SMichael.Adler@intel.com
2945513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
2955513SMichael.Adler@intel.com
2965513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
2975513SMichael.Adler@intel.com}
2985513SMichael.Adler@intel.com
2995513SMichael.Adler@intel.com
3005513SMichael.Adler@intel.comSyscallReturn
3015513SMichael.Adler@intel.comreadlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3025513SMichael.Adler@intel.com{
3035513SMichael.Adler@intel.com    string path;
3045513SMichael.Adler@intel.com
3055958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
3065513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3075513SMichael.Adler@intel.com
3085513SMichael.Adler@intel.com    // Adjust path for current working directory
3095513SMichael.Adler@intel.com    path = p->fullPath(path);
3105513SMichael.Adler@intel.com
3115958Sgblack@eecs.umich.edu    size_t bufsiz = p->getSyscallArg(tc, 2);
3125958Sgblack@eecs.umich.edu    BufferArg buf(p->getSyscallArg(tc, 1), bufsiz);
3135513SMichael.Adler@intel.com
3145513SMichael.Adler@intel.com    int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
3155513SMichael.Adler@intel.com
3165513SMichael.Adler@intel.com    buf.copyOut(tc->getMemPort());
3175513SMichael.Adler@intel.com
3185513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3195513SMichael.Adler@intel.com}
3205513SMichael.Adler@intel.com
3215513SMichael.Adler@intel.comSyscallReturn
3223114Sgblack@eecs.umich.eduunlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
323511SN/A{
3241706SN/A    string path;
325360SN/A
3265958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
3271450SN/A        return (TheISA::IntReg)-EFAULT;
328511SN/A
3293669Sbinkertn@umich.edu    // Adjust path for current working directory
3303669Sbinkertn@umich.edu    path = p->fullPath(path);
3313669Sbinkertn@umich.edu
332511SN/A    int result = unlink(path.c_str());
3331458SN/A    return (result == -1) ? -errno : result;
334511SN/A}
335511SN/A
3365513SMichael.Adler@intel.com
3375513SMichael.Adler@intel.comSyscallReturn
3385513SMichael.Adler@intel.commkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3395513SMichael.Adler@intel.com{
3405513SMichael.Adler@intel.com    string path;
3415513SMichael.Adler@intel.com
3425958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
3435513SMichael.Adler@intel.com        return (TheISA::IntReg)-EFAULT;
3445513SMichael.Adler@intel.com
3455513SMichael.Adler@intel.com    // Adjust path for current working directory
3465513SMichael.Adler@intel.com    path = p->fullPath(path);
3475513SMichael.Adler@intel.com
3485958Sgblack@eecs.umich.edu    mode_t mode = p->getSyscallArg(tc, 1);
3495513SMichael.Adler@intel.com
3505513SMichael.Adler@intel.com    int result = mkdir(path.c_str(), mode);
3515513SMichael.Adler@intel.com    return (result == -1) ? -errno : result;
3525513SMichael.Adler@intel.com}
3535513SMichael.Adler@intel.com
3541450SN/ASyscallReturn
3553114Sgblack@eecs.umich.edurenameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
356511SN/A{
3571706SN/A    string old_name;
358511SN/A
3595958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(old_name, p->getSyscallArg(tc, 0)))
3601458SN/A        return -EFAULT;
361511SN/A
3621706SN/A    string new_name;
363511SN/A
3645958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(new_name, p->getSyscallArg(tc, 1)))
3651458SN/A        return -EFAULT;
366511SN/A
3673669Sbinkertn@umich.edu    // Adjust path for current working directory
3683669Sbinkertn@umich.edu    old_name = p->fullPath(old_name);
3693669Sbinkertn@umich.edu    new_name = p->fullPath(new_name);
3703669Sbinkertn@umich.edu
3711706SN/A    int64_t result = rename(old_name.c_str(), new_name.c_str());
3721458SN/A    return (result == -1) ? -errno : result;
373511SN/A}
374511SN/A
3751706SN/ASyscallReturn
3763114Sgblack@eecs.umich.edutruncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
3771706SN/A{
3781706SN/A    string path;
3791706SN/A
3805958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
3811706SN/A        return -EFAULT;
3821706SN/A
3835958Sgblack@eecs.umich.edu    off_t length = p->getSyscallArg(tc, 1);
3841706SN/A
3853669Sbinkertn@umich.edu    // Adjust path for current working directory
3863669Sbinkertn@umich.edu    path = p->fullPath(path);
3873669Sbinkertn@umich.edu
3881706SN/A    int result = truncate(path.c_str(), length);
3891706SN/A    return (result == -1) ? -errno : result;
3901706SN/A}
3911706SN/A
3921706SN/ASyscallReturn
3936111Ssteve.reinhardt@amd.comftruncateFunc(SyscallDesc *desc, int num,
3946111Ssteve.reinhardt@amd.com              LiveProcess *process, ThreadContext *tc)
3951706SN/A{
3965958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
3971706SN/A
3981706SN/A    if (fd < 0)
3991706SN/A        return -EBADF;
4001706SN/A
4015958Sgblack@eecs.umich.edu    off_t length = process->getSyscallArg(tc, 1);
4021706SN/A
4031706SN/A    int result = ftruncate(fd, length);
4041706SN/A    return (result == -1) ? -errno : result;
4051706SN/A}
4061999SN/A
4071999SN/ASyscallReturn
4085513SMichael.Adler@intel.comumaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
4095513SMichael.Adler@intel.com{
4105513SMichael.Adler@intel.com    // Letting the simulated program change the simulator's umask seems like
4115513SMichael.Adler@intel.com    // a bad idea.  Compromise by just returning the current umask but not
4125513SMichael.Adler@intel.com    // changing anything.
4135513SMichael.Adler@intel.com    mode_t oldMask = umask(0);
4145513SMichael.Adler@intel.com    umask(oldMask);
4155521Snate@binkert.org    return (int)oldMask;
4165513SMichael.Adler@intel.com}
4175513SMichael.Adler@intel.com
4185513SMichael.Adler@intel.comSyscallReturn
4193114Sgblack@eecs.umich.educhownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
4201999SN/A{
4211999SN/A    string path;
4221999SN/A
4235958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, p->getSyscallArg(tc, 0)))
4241999SN/A        return -EFAULT;
4251999SN/A
4261999SN/A    /* XXX endianess */
4275958Sgblack@eecs.umich.edu    uint32_t owner = p->getSyscallArg(tc, 1);
4281999SN/A    uid_t hostOwner = owner;
4295958Sgblack@eecs.umich.edu    uint32_t group = p->getSyscallArg(tc, 2);
4301999SN/A    gid_t hostGroup = group;
4311999SN/A
4323669Sbinkertn@umich.edu    // Adjust path for current working directory
4333669Sbinkertn@umich.edu    path = p->fullPath(path);
4343669Sbinkertn@umich.edu
4351999SN/A    int result = chown(path.c_str(), hostOwner, hostGroup);
4361999SN/A    return (result == -1) ? -errno : result;
4371999SN/A}
4381999SN/A
4391999SN/ASyscallReturn
4403114Sgblack@eecs.umich.edufchownFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
4411999SN/A{
4425958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
4431999SN/A
4441999SN/A    if (fd < 0)
4451999SN/A        return -EBADF;
4461999SN/A
4471999SN/A    /* XXX endianess */
4485958Sgblack@eecs.umich.edu    uint32_t owner = process->getSyscallArg(tc, 1);
4491999SN/A    uid_t hostOwner = owner;
4505958Sgblack@eecs.umich.edu    uint32_t group = process->getSyscallArg(tc, 2);
4511999SN/A    gid_t hostGroup = group;
4521999SN/A
4531999SN/A    int result = fchown(fd, hostOwner, hostGroup);
4541999SN/A    return (result == -1) ? -errno : result;
4551999SN/A}
4562093SN/A
4572093SN/A
4582093SN/ASyscallReturn
4593114Sgblack@eecs.umich.edudupFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
4603079Sstever@eecs.umich.edu{
4615958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
4623079Sstever@eecs.umich.edu    if (fd < 0)
4633079Sstever@eecs.umich.edu        return -EBADF;
4643079Sstever@eecs.umich.edu
4655958Sgblack@eecs.umich.edu    Process::FdMap *fdo = process->sim_fd_obj(process->getSyscallArg(tc, 0));
4665282Srstrong@cs.ucsd.edu
4673079Sstever@eecs.umich.edu    int result = dup(fd);
4686111Ssteve.reinhardt@amd.com    return (result == -1) ? -errno :
4696111Ssteve.reinhardt@amd.com        process->alloc_fd(result, fdo->filename, fdo->flags, fdo->mode, false);
4703079Sstever@eecs.umich.edu}
4713079Sstever@eecs.umich.edu
4723079Sstever@eecs.umich.edu
4733079Sstever@eecs.umich.eduSyscallReturn
4743114Sgblack@eecs.umich.edufcntlFunc(SyscallDesc *desc, int num, LiveProcess *process,
4752680Sktlim@umich.edu          ThreadContext *tc)
4762093SN/A{
4775958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
4782093SN/A
4792093SN/A    if (fd < 0 || process->sim_fd(fd) < 0)
4802093SN/A        return -EBADF;
4812093SN/A
4825958Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, 1);
4832093SN/A    switch (cmd) {
4842093SN/A      case 0: // F_DUPFD
4852093SN/A        // if we really wanted to support this, we'd need to do it
4862093SN/A        // in the target fd space.
4872093SN/A        warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd);
4882093SN/A        return -EMFILE;
4892093SN/A
4902093SN/A      case 1: // F_GETFD (get close-on-exec flag)
4912093SN/A      case 2: // F_SETFD (set close-on-exec flag)
4922093SN/A        return 0;
4932093SN/A
4942093SN/A      case 3: // F_GETFL (get file flags)
4952093SN/A      case 4: // F_SETFL (set file flags)
4962093SN/A        // not sure if this is totally valid, but we'll pass it through
4972093SN/A        // to the underlying OS
4982093SN/A        warn("fcntl(%d, %d) passed through to host\n", fd, cmd);
4992093SN/A        return fcntl(process->sim_fd(fd), cmd);
5002093SN/A        // return 0;
5012093SN/A
5022093SN/A      case 7: // F_GETLK  (get lock)
5032093SN/A      case 8: // F_SETLK  (set lock)
5042093SN/A      case 9: // F_SETLKW (set lock and wait)
5052093SN/A        // don't mess with file locking... just act like it's OK
5062093SN/A        warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd);
5072093SN/A        return 0;
5082093SN/A
5092093SN/A      default:
5102093SN/A        warn("Unknown fcntl command %d\n", cmd);
5112093SN/A        return 0;
5122093SN/A    }
5132093SN/A}
5142093SN/A
5152238SN/ASyscallReturn
5163114Sgblack@eecs.umich.edufcntl64Func(SyscallDesc *desc, int num, LiveProcess *process,
5172687Sksewell@umich.edu            ThreadContext *tc)
5182687Sksewell@umich.edu{
5195958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
5202687Sksewell@umich.edu
5212687Sksewell@umich.edu    if (fd < 0 || process->sim_fd(fd) < 0)
5222687Sksewell@umich.edu        return -EBADF;
5232687Sksewell@umich.edu
5245958Sgblack@eecs.umich.edu    int cmd = process->getSyscallArg(tc, 1);
5252687Sksewell@umich.edu    switch (cmd) {
5262687Sksewell@umich.edu      case 33: //F_GETLK64
5272687Sksewell@umich.edu        warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", fd);
5282687Sksewell@umich.edu        return -EMFILE;
5292687Sksewell@umich.edu
5302687Sksewell@umich.edu      case 34: // F_SETLK64
5312687Sksewell@umich.edu      case 35: // F_SETLKW64
5322687Sksewell@umich.edu        warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", fd);
5332687Sksewell@umich.edu        return -EMFILE;
5342687Sksewell@umich.edu
5352687Sksewell@umich.edu      default:
5362687Sksewell@umich.edu        // not sure if this is totally valid, but we'll pass it through
5372687Sksewell@umich.edu        // to the underlying OS
5382687Sksewell@umich.edu        warn("fcntl64(%d, %d) passed through to host\n", fd, cmd);
5392687Sksewell@umich.edu        return fcntl(process->sim_fd(fd), cmd);
5402687Sksewell@umich.edu        // return 0;
5412687Sksewell@umich.edu    }
5422687Sksewell@umich.edu}
5432687Sksewell@umich.edu
5442687Sksewell@umich.eduSyscallReturn
5453114Sgblack@eecs.umich.edupipePseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5462680Sktlim@umich.edu         ThreadContext *tc)
5472238SN/A{
5482238SN/A    int fds[2], sim_fds[2];
5492238SN/A    int pipe_retval = pipe(fds);
5502093SN/A
5512238SN/A    if (pipe_retval < 0) {
5522238SN/A        // error
5532238SN/A        return pipe_retval;
5542238SN/A    }
5552238SN/A
5565282Srstrong@cs.ucsd.edu    sim_fds[0] = process->alloc_fd(fds[0], "PIPE-READ", O_WRONLY, -1, true);
5575282Srstrong@cs.ucsd.edu    sim_fds[1] = process->alloc_fd(fds[1], "PIPE-WRITE", O_RDONLY, -1, true);
5582238SN/A
5595282Srstrong@cs.ucsd.edu    process->setReadPipeSource(sim_fds[0], sim_fds[1]);
5602238SN/A    // Alpha Linux convention for pipe() is that fd[0] is returned as
5612238SN/A    // the return value of the function, and fd[1] is returned in r20.
5622680Sktlim@umich.edu    tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]);
5632238SN/A    return sim_fds[0];
5642238SN/A}
5652238SN/A
5662238SN/A
5672238SN/ASyscallReturn
5683114Sgblack@eecs.umich.edugetpidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5692680Sktlim@umich.edu           ThreadContext *tc)
5702238SN/A{
5712238SN/A    // Make up a PID.  There's no interprocess communication in
5722238SN/A    // fake_syscall mode, so there's no way for a process to know it's
5732238SN/A    // not getting a unique value.
5742238SN/A
5753114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
5763114Sgblack@eecs.umich.edu    return process->pid();
5772238SN/A}
5782238SN/A
5792238SN/A
5802238SN/ASyscallReturn
5813114Sgblack@eecs.umich.edugetuidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5822680Sktlim@umich.edu           ThreadContext *tc)
5832238SN/A{
5842238SN/A    // Make up a UID and EUID... it shouldn't matter, and we want the
5852238SN/A    // simulation to be deterministic.
5862238SN/A
5872238SN/A    // EUID goes in r20.
5883114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->euid()); //EUID
5895543Ssaidi@eecs.umich.edu    return process->uid();              // UID
5902238SN/A}
5912238SN/A
5922238SN/A
5932238SN/ASyscallReturn
5943114Sgblack@eecs.umich.edugetgidPseudoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5952680Sktlim@umich.edu           ThreadContext *tc)
5962238SN/A{
5972238SN/A    // Get current group ID.  EGID goes in r20.
5983114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->egid()); //EGID
5993114Sgblack@eecs.umich.edu    return process->gid();
6002238SN/A}
6012238SN/A
6022238SN/A
6032238SN/ASyscallReturn
6043114Sgblack@eecs.umich.edusetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6052680Sktlim@umich.edu           ThreadContext *tc)
6062238SN/A{
6072238SN/A    // can't fathom why a benchmark would call this.
6085958Sgblack@eecs.umich.edu    warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, 0));
6092238SN/A    return 0;
6102238SN/A}
6112238SN/A
6122238SN/ASyscallReturn
6133114Sgblack@eecs.umich.edugetpidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6142680Sktlim@umich.edu           ThreadContext *tc)
6152238SN/A{
6162238SN/A    // Make up a PID.  There's no interprocess communication in
6172238SN/A    // fake_syscall mode, so there's no way for a process to know it's
6182238SN/A    // not getting a unique value.
6192238SN/A
6203114Sgblack@eecs.umich.edu    tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); //PID
6213114Sgblack@eecs.umich.edu    return process->pid();
6222238SN/A}
6232238SN/A
6242238SN/ASyscallReturn
6253114Sgblack@eecs.umich.edugetppidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6262680Sktlim@umich.edu           ThreadContext *tc)
6272238SN/A{
6283114Sgblack@eecs.umich.edu    return process->ppid();
6292238SN/A}
6302238SN/A
6312238SN/ASyscallReturn
6323114Sgblack@eecs.umich.edugetuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6332680Sktlim@umich.edu           ThreadContext *tc)
6342238SN/A{
6355543Ssaidi@eecs.umich.edu    return process->uid();              // UID
6362238SN/A}
6372238SN/A
6382238SN/ASyscallReturn
6393114Sgblack@eecs.umich.edugeteuidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6402680Sktlim@umich.edu           ThreadContext *tc)
6412238SN/A{
6425543Ssaidi@eecs.umich.edu    return process->euid();             // UID
6432238SN/A}
6442238SN/A
6452238SN/ASyscallReturn
6463114Sgblack@eecs.umich.edugetgidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6472680Sktlim@umich.edu           ThreadContext *tc)
6482238SN/A{
6493114Sgblack@eecs.umich.edu    return process->gid();
6502238SN/A}
6512238SN/A
6522238SN/ASyscallReturn
6533114Sgblack@eecs.umich.edugetegidFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6542680Sktlim@umich.edu           ThreadContext *tc)
6552238SN/A{
6563114Sgblack@eecs.umich.edu    return process->egid();
6572238SN/A}
6582238SN/A
6592238SN/A
6606109Ssanchezd@stanford.eduSyscallReturn
6616109Ssanchezd@stanford.educloneFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6626109Ssanchezd@stanford.edu           ThreadContext *tc)
6636109Ssanchezd@stanford.edu{
6646109Ssanchezd@stanford.edu    DPRINTF(SyscallVerbose, "In sys_clone:\n");
6656110Ssteve.reinhardt@amd.com    DPRINTF(SyscallVerbose, " Flags=%llx\n", process->getSyscallArg(tc, 0));
6666111Ssteve.reinhardt@amd.com    DPRINTF(SyscallVerbose, " Child stack=%llx\n",
6676111Ssteve.reinhardt@amd.com            process->getSyscallArg(tc, 1));
6686109Ssanchezd@stanford.edu
6696109Ssanchezd@stanford.edu
6706110Ssteve.reinhardt@amd.com    if (process->getSyscallArg(tc, 0) != 0x10f00) {
6716111Ssteve.reinhardt@amd.com        warn("This sys_clone implementation assumes flags "
6726111Ssteve.reinhardt@amd.com             "CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD "
6736111Ssteve.reinhardt@amd.com             "(0x10f00), and may not work correctly with given flags "
6746111Ssteve.reinhardt@amd.com             "0x%llx\n", process->getSyscallArg(tc, 0));
6756109Ssanchezd@stanford.edu    }
6766109Ssanchezd@stanford.edu
6776111Ssteve.reinhardt@amd.com    ThreadContext* ctc; // child thread context
6786109Ssanchezd@stanford.edu    if ( ( ctc = process->findFreeContext() ) != NULL ) {
6796109Ssanchezd@stanford.edu        DPRINTF(SyscallVerbose, " Found unallocated thread context\n");
6806109Ssanchezd@stanford.edu
6816109Ssanchezd@stanford.edu        ctc->clearArchRegs();
6826109Ssanchezd@stanford.edu
6836111Ssteve.reinhardt@amd.com        // Arch-specific cloning code
6846109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA or THE_ISA == X86_ISA
6856111Ssteve.reinhardt@amd.com            // Cloning the misc. regs for these archs is enough
6866109Ssanchezd@stanford.edu            TheISA::copyMiscRegs(tc, ctc);
6876109Ssanchezd@stanford.edu        #elif THE_ISA == SPARC_ISA
6886109Ssanchezd@stanford.edu            TheISA::copyRegs(tc, ctc);
6896109Ssanchezd@stanford.edu
6906111Ssteve.reinhardt@amd.com            // TODO: Explain what this code actually does :-)
6916109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 6, 0);
6926109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 4, 0);
6936109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
6946109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 5, NWindows);
6956337Sgblack@eecs.umich.edu            ctc->setMiscReg(MISCREG_CWP, 0);
6966109Ssanchezd@stanford.edu            ctc->setIntReg(NumIntArchRegs + 7, 0);
6976109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_TL, 0);
6986109Ssanchezd@stanford.edu            ctc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
6996109Ssanchezd@stanford.edu
7006109Ssanchezd@stanford.edu            for (int y = 8; y < 32; y++)
7016109Ssanchezd@stanford.edu                ctc->setIntReg(y, tc->readIntReg(y));
7026109Ssanchezd@stanford.edu        #else
7036109Ssanchezd@stanford.edu            fatal("sys_clone is not implemented for this ISA\n");
7046109Ssanchezd@stanford.edu        #endif
7056109Ssanchezd@stanford.edu
7066111Ssteve.reinhardt@amd.com        // Set up stack register
7076110Ssteve.reinhardt@amd.com        ctc->setIntReg(TheISA::StackPointerReg, process->getSyscallArg(tc, 1));
7086109Ssanchezd@stanford.edu
7096111Ssteve.reinhardt@amd.com        // Set up syscall return values in parent and child
7106111Ssteve.reinhardt@amd.com        ctc->setIntReg(ReturnValueReg, 0); // return value, child
7116109Ssanchezd@stanford.edu
7126111Ssteve.reinhardt@amd.com        // Alpha needs SyscallSuccessReg=0 in child
7136109Ssanchezd@stanford.edu        #if THE_ISA == ALPHA_ISA
7146110Ssteve.reinhardt@amd.com            ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
7156109Ssanchezd@stanford.edu        #endif
7166109Ssanchezd@stanford.edu
7176111Ssteve.reinhardt@amd.com        // In SPARC/Linux, clone returns 0 on pseudo-return register if
7186111Ssteve.reinhardt@amd.com        // parent, non-zero if child
7196109Ssanchezd@stanford.edu        #if THE_ISA == SPARC_ISA
7206109Ssanchezd@stanford.edu            tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
7216109Ssanchezd@stanford.edu            ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
7226109Ssanchezd@stanford.edu        #endif
7236109Ssanchezd@stanford.edu
7246109Ssanchezd@stanford.edu        ctc->setPC(tc->readNextPC());
7256109Ssanchezd@stanford.edu        ctc->setNextPC(tc->readNextPC() + sizeof(TheISA::MachInst));
7266134Sgblack@eecs.umich.edu        ctc->setNextNPC(tc->readNextNPC() + sizeof(TheISA::MachInst));
7276109Ssanchezd@stanford.edu
7286109Ssanchezd@stanford.edu        ctc->activate();
7296109Ssanchezd@stanford.edu
7306109Ssanchezd@stanford.edu        // Should return nonzero child TID in parent's syscall return register,
7316109Ssanchezd@stanford.edu        // but for our pthread library any non-zero value will work
7326109Ssanchezd@stanford.edu        return 1;
7336109Ssanchezd@stanford.edu    } else {
7346109Ssanchezd@stanford.edu        fatal("Called sys_clone, but no unallocated thread contexts found!\n");
7356109Ssanchezd@stanford.edu        return 0;
7366109Ssanchezd@stanford.edu    }
7376109Ssanchezd@stanford.edu}
7386109Ssanchezd@stanford.edu
739